xref: /openbsd-src/usr.sbin/ospfd/packet.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: packet.c,v 1.30 2010/05/26 13:56:08 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22 
23 #include <netinet/in.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/ip.h>
26 #include <arpa/inet.h>
27 #include <net/if_dl.h>
28 
29 #include <errno.h>
30 #include <event.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "ospfd.h"
35 #include "ospf.h"
36 #include "log.h"
37 #include "ospfe.h"
38 
39 int		 ip_hdr_sanity_check(const struct ip *, u_int16_t);
40 int		 ospf_hdr_sanity_check(const struct ip *,
41 		    struct ospf_hdr *, u_int16_t, const struct iface *);
42 struct iface	*find_iface(struct ospfd_conf *, unsigned int, struct in_addr);
43 
44 int
45 gen_ospf_hdr(struct ibuf *buf, struct iface *iface, u_int8_t type)
46 {
47 	struct ospf_hdr	ospf_hdr;
48 
49 	bzero(&ospf_hdr, sizeof(ospf_hdr));
50 	ospf_hdr.version = OSPF_VERSION;
51 	ospf_hdr.type = type;
52 	ospf_hdr.rtr_id = ospfe_router_id();
53 	if (iface->type != IF_TYPE_VIRTUALLINK)
54 		ospf_hdr.area_id = iface->area->id.s_addr;
55 	ospf_hdr.auth_type = htons(iface->auth_type);
56 
57 	return (ibuf_add(buf, &ospf_hdr, sizeof(ospf_hdr)));
58 }
59 
60 /* send and receive packets */
61 int
62 send_packet(struct iface *iface, struct ibuf *buf, struct sockaddr_in *dst)
63 {
64 	struct msghdr		 msg;
65 	struct iovec		 iov[2];
66 	struct ip		 ip_hdr;
67 
68 	/* setup IP hdr */
69 	bzero(&ip_hdr, sizeof(ip_hdr));
70 	ip_hdr.ip_v = IPVERSION;
71 	ip_hdr.ip_hl = sizeof(ip_hdr) >> 2;
72 	ip_hdr.ip_tos = IPTOS_PREC_INTERNETCONTROL;
73 	ip_hdr.ip_len = htons(ibuf_size(buf) + sizeof(ip_hdr));
74 	ip_hdr.ip_id = 0;  /* 0 means kernel set appropriate value */
75 	ip_hdr.ip_off = 0;
76 	ip_hdr.ip_ttl = iface->type != IF_TYPE_VIRTUALLINK ?
77 	    IP_DEFAULT_MULTICAST_TTL : MAXTTL;
78 	ip_hdr.ip_p = IPPROTO_OSPF;
79 	ip_hdr.ip_sum = 0;
80 	ip_hdr.ip_src = iface->addr;
81 	ip_hdr.ip_dst = dst->sin_addr;
82 
83 	/* setup buffer */
84 	bzero(&msg, sizeof(msg));
85 	iov[0].iov_base = &ip_hdr;
86 	iov[0].iov_len = sizeof(ip_hdr);
87 	iov[1].iov_base = buf->buf;
88 	iov[1].iov_len = ibuf_size(buf);
89 	msg.msg_name = dst;
90 	msg.msg_namelen = sizeof(*dst);
91 	msg.msg_iov = iov;
92 	msg.msg_iovlen = 2;
93 
94 	/* set outgoing interface for multicast traffic */
95 	if (IN_MULTICAST(ntohl(dst->sin_addr.s_addr)))
96 		if (if_set_mcast(iface) == -1)
97 			return (-1);
98 
99 	if (sendmsg(iface->fd, &msg, 0) == -1) {
100 		log_warn("send_packet: error sending packet on interface %s",
101 		    iface->name);
102 		return (-1);
103 	}
104 
105 	return (0);
106 }
107 
108 void
109 recv_packet(int fd, short event, void *bula)
110 {
111 	union {
112 		struct cmsghdr hdr;
113 		char	buf[CMSG_SPACE(sizeof(struct sockaddr_dl))];
114 	} cmsgbuf;
115 	struct msghdr		 msg;
116 	struct iovec		 iov;
117 	struct ip		 ip_hdr;
118 	struct in_addr		 addr;
119 	struct ospfd_conf	*xconf = bula;
120 	struct ospf_hdr		*ospf_hdr;
121 	struct iface		*iface;
122 	struct nbr		*nbr = NULL;
123 	char			*buf;
124 	struct cmsghdr		*cmsg;
125 	ssize_t			 r;
126 	u_int16_t		 len;
127 	int			 l;
128 	unsigned int		 ifindex = 0;
129 
130 	if (event != EV_READ)
131 		return;
132 
133 	/* setup buffer */
134 	bzero(&msg, sizeof(msg));
135 	iov.iov_base = buf = pkt_ptr;
136 	iov.iov_len = READ_BUF_SIZE;
137 	msg.msg_iov = &iov;
138 	msg.msg_iovlen = 1;
139 	msg.msg_control = &cmsgbuf.buf;
140 	msg.msg_controllen = sizeof(cmsgbuf.buf);
141 
142 	if ((r = recvmsg(fd, &msg, 0)) == -1) {
143 		if (errno != EAGAIN && errno != EINTR)
144 			log_debug("recv_packet: read error: %s",
145 			    strerror(errno));
146 		return;
147 	}
148 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
149 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
150 		if (cmsg->cmsg_level == IPPROTO_IP &&
151 		    cmsg->cmsg_type == IP_RECVIF) {
152 			ifindex = ((struct sockaddr_dl *)
153 			    CMSG_DATA(cmsg))->sdl_index;
154 			break;
155 		}
156 	}
157 
158 	len = (u_int16_t)r;
159 
160 	/* IP header sanity checks */
161 	if (len < sizeof(ip_hdr)) {
162 		log_warnx("recv_packet: bad packet size");
163 		return;
164 	}
165 	memcpy(&ip_hdr, buf, sizeof(ip_hdr));
166 	if ((l = ip_hdr_sanity_check(&ip_hdr, len)) == -1)
167 		return;
168 	buf += l;
169 	len -= l;
170 
171 	/* find a matching interface */
172 	if ((iface = find_iface(xconf, ifindex, ip_hdr.ip_src)) == NULL) {
173 		/* XXX add a counter here */
174 		return;
175 	}
176 
177 	/*
178 	 * Packet needs to be sent to AllSPFRouters or AllDRouters
179 	 * or to the address of the interface itself.
180 	 * AllDRouters is only valid for DR and BDR but this is checked later.
181 	 */
182 	inet_aton(AllSPFRouters, &addr);
183 	if (ip_hdr.ip_dst.s_addr != addr.s_addr) {
184 		inet_aton(AllDRouters, &addr);
185 		if (ip_hdr.ip_dst.s_addr != addr.s_addr) {
186 			if (ip_hdr.ip_dst.s_addr != iface->addr.s_addr) {
187 				log_debug("recv_packet: packet sent to wrong "
188 				    "address %s, interface %s",
189 				    inet_ntoa(ip_hdr.ip_dst), iface->name);
190 				return;
191 			}
192 		}
193 	}
194 
195 	/* OSPF header sanity checks */
196 	if (len < sizeof(*ospf_hdr)) {
197 		log_debug("recv_packet: bad packet size");
198 		return;
199 	}
200 	ospf_hdr = (struct ospf_hdr *)buf;
201 
202 	if ((l = ospf_hdr_sanity_check(&ip_hdr, ospf_hdr, len, iface)) == -1)
203 		return;
204 
205 	nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
206 	if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
207 		log_debug("recv_packet: unknown neighbor ID");
208 		return;
209 	}
210 
211 	if (auth_validate(buf, len, iface, nbr)) {
212 		log_warnx("recv_packet: authentication error, "
213 		    "interface %s", iface->name);
214 		return;
215 	}
216 
217 	buf += sizeof(*ospf_hdr);
218 	len = l - sizeof(*ospf_hdr);
219 
220 	/* switch OSPF packet type */
221 	switch (ospf_hdr->type) {
222 	case PACKET_TYPE_HELLO:
223 		inet_aton(AllDRouters, &addr);
224 		if (ip_hdr.ip_dst.s_addr == addr.s_addr) {
225 			log_debug("recv_packet: invalid destination IP "
226 			     "address");
227 			break;
228 		}
229 
230 		recv_hello(iface, ip_hdr.ip_src, ospf_hdr->rtr_id, buf, len);
231 		break;
232 	case PACKET_TYPE_DD:
233 		recv_db_description(nbr, buf, len);
234 		break;
235 	case PACKET_TYPE_LS_REQUEST:
236 		recv_ls_req(nbr, buf, len);
237 		break;
238 	case PACKET_TYPE_LS_UPDATE:
239 		recv_ls_update(nbr, buf, len);
240 		break;
241 	case PACKET_TYPE_LS_ACK:
242 		recv_ls_ack(nbr, buf, len);
243 		break;
244 	default:
245 		log_debug("recv_packet: unknown OSPF packet type, interface %s",
246 		    iface->name);
247 	}
248 }
249 
250 int
251 ip_hdr_sanity_check(const struct ip *ip_hdr, u_int16_t len)
252 {
253 	if (ntohs(ip_hdr->ip_len) != len) {
254 		log_debug("recv_packet: invalid IP packet length %u",
255 		    ntohs(ip_hdr->ip_len));
256 		return (-1);
257 	}
258 
259 	if (ip_hdr->ip_p != IPPROTO_OSPF)
260 		/* this is enforced by the socket itself */
261 		fatalx("recv_packet: invalid IP proto");
262 
263 	return (ip_hdr->ip_hl << 2);
264 }
265 
266 int
267 ospf_hdr_sanity_check(const struct ip *ip_hdr, struct ospf_hdr *ospf_hdr,
268     u_int16_t len, const struct iface *iface)
269 {
270 	struct in_addr		 addr;
271 
272 	if (ospf_hdr->version != OSPF_VERSION) {
273 		log_debug("recv_packet: invalid OSPF version %d",
274 		    ospf_hdr->version);
275 		return (-1);
276 	}
277 
278 	if (ntohs(ospf_hdr->len) > len ||
279 	    len <= sizeof(struct ospf_hdr)) {
280 		log_debug("recv_packet: invalid OSPF packet length %d",
281 		    ntohs(ospf_hdr->len));
282 		return (-1);
283 	}
284 
285 	if (iface->type != IF_TYPE_VIRTUALLINK) {
286 		if (ospf_hdr->area_id != iface->area->id.s_addr) {
287 			addr.s_addr = ospf_hdr->area_id;
288 			log_debug("recv_packet: invalid area ID %s, "
289 			    "interface %s", inet_ntoa(addr), iface->name);
290 			return (-1);
291 		}
292 	} else {
293 		if (ospf_hdr->area_id != 0) {
294 			addr.s_addr = ospf_hdr->area_id;
295 			log_debug("recv_packet: invalid area ID %s, "
296 			    "interface %s", inet_ntoa(addr), iface->name);
297 			return (-1);
298 		}
299 	}
300 
301 	if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
302 		if (inet_aton(AllDRouters, &addr) == 0)
303 			fatalx("recv_packet: inet_aton");
304 		if (ip_hdr->ip_dst.s_addr == addr.s_addr &&
305 		    (iface->state & IF_STA_DRORBDR) == 0) {
306 			log_debug("recv_packet: invalid destination IP in "
307 			    "state %s, interface %s",
308 			    if_state_name(iface->state), iface->name);
309 			return (-1);
310 		}
311 	}
312 
313 	return (ntohs(ospf_hdr->len));
314 }
315 
316 struct iface *
317 find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in_addr src)
318 {
319 	struct area	*area = NULL;
320 	struct iface	*iface = NULL;
321 
322 	/* returned interface needs to be active */
323 	LIST_FOREACH(area, &xconf->area_list, entry) {
324 		LIST_FOREACH(iface, &area->iface_list, entry) {
325 			switch (iface->type) {
326 			case IF_TYPE_VIRTUALLINK:
327 				if ((src.s_addr == iface->dst.s_addr) &&
328 				    !iface->passive)
329 					return (iface);
330 				break;
331 			case IF_TYPE_POINTOPOINT:
332 				if (ifindex == iface->ifindex &&
333 				    !iface->passive)
334 					return (iface);
335 				break;
336 			default:
337 				if (ifindex == iface->ifindex &&
338 				    (iface->addr.s_addr & iface->mask.s_addr) ==
339 				    (src.s_addr & iface->mask.s_addr) &&
340 				    !iface->passive)
341 					return (iface);
342 				break;
343 			}
344 		}
345 	}
346 
347 	return (NULL);
348 }
349