xref: /openbsd-src/usr.sbin/ospf6d/packet.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: packet.c,v 1.12 2014/03/24 11:01:47 mpi 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 <netinet/ip6.h>
27 #include <arpa/inet.h>
28 #include <net/if_dl.h>
29 
30 #include <errno.h>
31 #include <event.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "ospf6d.h"
37 #include "ospf6.h"
38 #include "log.h"
39 #include "ospfe.h"
40 
41 int		 ip_hdr_sanity_check(const struct ip6_hdr *, u_int16_t);
42 int		 ospf_hdr_sanity_check(struct ospf_hdr *, u_int16_t,
43 		    const struct iface *, struct in6_addr *);
44 struct iface	*find_iface(struct ospfd_conf *, unsigned int,
45 		    struct in6_addr *);
46 
47 int
48 gen_ospf_hdr(struct ibuf *buf, struct iface *iface, u_int8_t type)
49 {
50 	struct ospf_hdr	ospf_hdr;
51 
52 	bzero(&ospf_hdr, sizeof(ospf_hdr));
53 	ospf_hdr.version = OSPF6_VERSION;
54 	ospf_hdr.type = type;
55 	ospf_hdr.rtr_id = ospfe_router_id();
56 	if (iface->type != IF_TYPE_VIRTUALLINK)
57 		ospf_hdr.area_id = iface->area_id.s_addr;
58 	ospf_hdr.instance = DEFAULT_INSTANCE_ID;
59 	ospf_hdr.zero = 0;		/* must be zero */
60 
61 	return (ibuf_add(buf, &ospf_hdr, sizeof(ospf_hdr)));
62 }
63 
64 int
65 upd_ospf_hdr(struct ibuf *buf, struct iface *iface)
66 {
67 	struct ospf_hdr	*ospf_hdr;
68 
69 	if ((ospf_hdr = ibuf_seek(buf, 0, sizeof(ospf_hdr))) == NULL)
70 		fatalx("upd_ospf_hdr: buf_seek failed");
71 
72 	/* update length */
73 	if (buf->wpos > USHRT_MAX)
74 		fatalx("upd_ospf_hdr: resulting ospf packet too big");
75 	ospf_hdr->len = htons((u_int16_t)buf->wpos);
76 	ospf_hdr->chksum = 0; /* calculated via IPV6_CHECKSUM */
77 
78 	return (0);
79 }
80 
81 /* send and receive packets */
82 int
83 send_packet(struct iface *iface, void *pkt, size_t len,
84     struct in6_addr *dst)
85 {
86 	struct sockaddr_in6	 sa6;
87 
88 	/* setup buffer */
89 	bzero(&sa6, sizeof(sa6));
90 
91 	sa6.sin6_family = AF_INET6;
92 	sa6.sin6_len = sizeof(sa6);
93 	sa6.sin6_addr = *dst;
94 
95 	/* don't we all love link local scope and all the needed hacks for it */
96 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst))
97 		sa6.sin6_scope_id = iface->ifindex;
98 
99 	/* set outgoing interface for multicast traffic */
100 	if (IN6_IS_ADDR_MULTICAST(dst))
101 		if (if_set_mcast(iface) == -1) {
102 			log_warn("send_packet: error setting multicast "
103 			    "interface, %s", iface->name);
104 			return (-1);
105 		}
106 
107 	if (sendto(iface->fd, pkt, len, 0, (struct sockaddr *)&sa6,
108 	    sizeof(sa6)) == -1) {
109 		log_warn("send_packet: error sending packet on interface %s",
110 		    iface->name);
111 		return (-1);
112 	}
113 
114 	return (0);
115 }
116 
117 void
118 recv_packet(int fd, short event, void *bula)
119 {
120 	union {
121 		struct cmsghdr hdr;
122 		char	buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
123 	} cmsgbuf;
124 	struct msghdr		 msg;
125 	struct iovec		 iov;
126 	struct in6_addr		 addr, dest;
127 	struct sockaddr_in6	 src;
128 	struct ospfd_conf	*xconf = bula;
129 	struct ospf_hdr		*ospf_hdr;
130 	struct iface		*iface;
131 	struct nbr		*nbr = NULL;
132 	char			*buf;
133 	struct cmsghdr		*cmsg;
134 	ssize_t			 r;
135 	u_int16_t		 len;
136 	int			 l;
137 	unsigned int		 ifindex = 0;
138 
139 	if (event != EV_READ)
140 		return;
141 
142 	/* setup buffer */
143 	bzero(&msg, sizeof(msg));
144 	iov.iov_base = buf = pkt_ptr;
145 	iov.iov_len = READ_BUF_SIZE;
146 	msg.msg_name = &src;
147 	msg.msg_namelen = sizeof(src);
148 	msg.msg_iov = &iov;
149 	msg.msg_iovlen = 1;
150 	msg.msg_control = &cmsgbuf.buf;
151 	msg.msg_controllen = sizeof(cmsgbuf.buf);
152 
153 	if ((r = recvmsg(fd, &msg, 0)) == -1) {
154 		if (errno != EAGAIN && errno != EINTR)
155 			log_debug("recv_packet: read error: %s",
156 			    strerror(errno));
157 		return;
158 	}
159 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
160 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
161 		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
162 		    cmsg->cmsg_type == IPV6_PKTINFO) {
163 			ifindex = ((struct in6_pktinfo *)
164 			    CMSG_DATA(cmsg))->ipi6_ifindex;
165 			dest = ((struct in6_pktinfo *)
166 			    CMSG_DATA(cmsg))->ipi6_addr;
167 			break;
168 		}
169 	}
170 
171 	/* find a matching interface */
172 	if ((iface = find_iface(xconf, ifindex, &src.sin6_addr)) == NULL) {
173 		/* XXX add a counter here */
174 		return;
175 	}
176 	/*
177 	 * Packet needs to be sent to AllSPFRouters or AllDRouters
178 	 * or to the address of the interface itself.
179 	 * AllDRouters is only valid for DR and BDR but this is checked later.
180 	 */
181 	inet_pton(AF_INET6, AllSPFRouters, &addr);
182 
183 	if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
184 		inet_pton(AF_INET6, AllDRouters, &addr);
185 		if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
186 			if (!IN6_ARE_ADDR_EQUAL(&dest, &iface->addr)) {
187 				log_debug("recv_packet: packet sent to wrong "
188 				    "address %s, interface %s",
189 				    log_in6addr(&dest), iface->name);
190 				return;
191 			}
192 		}
193 	}
194 
195 	len = (u_int16_t)r;
196 	/* OSPF header sanity checks */
197 	if (len < sizeof(*ospf_hdr)) {
198 		log_debug("recv_packet: bad packet size");
199 		return;
200 	}
201 	ospf_hdr = (struct ospf_hdr *)buf;
202 
203 	if ((l = ospf_hdr_sanity_check(ospf_hdr, len, iface, &dest)) == -1)
204 		return;
205 
206 	nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
207 	if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
208 		log_debug("recv_packet: unknown neighbor ID");
209 		return;
210 	}
211 
212 	buf += sizeof(*ospf_hdr);
213 	len = l - sizeof(*ospf_hdr);
214 
215 	/* switch OSPF packet type */
216 	switch (ospf_hdr->type) {
217 	case PACKET_TYPE_HELLO:
218 		inet_pton(AF_INET6, AllDRouters, &addr);
219 		if (IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
220 			log_debug("recv_packet: invalid destination IP "
221 			     "address");
222 			break;
223 		}
224 
225 		recv_hello(iface, &src.sin6_addr, ospf_hdr->rtr_id, buf, len);
226 		break;
227 	case PACKET_TYPE_DD:
228 		recv_db_description(nbr, buf, len);
229 		break;
230 	case PACKET_TYPE_LS_REQUEST:
231 		recv_ls_req(nbr, buf, len);
232 		break;
233 	case PACKET_TYPE_LS_UPDATE:
234 		recv_ls_update(nbr, buf, len);
235 		break;
236 	case PACKET_TYPE_LS_ACK:
237 		recv_ls_ack(nbr, buf, len);
238 		break;
239 	default:
240 		log_debug("recv_packet: unknown OSPF packet type, interface %s",
241 		    iface->name);
242 	}
243 }
244 
245 int
246 ospf_hdr_sanity_check(struct ospf_hdr *ospf_hdr, u_int16_t len,
247     const struct iface *iface, struct in6_addr *dst)
248 {
249 	struct in6_addr		 addr;
250 	struct in_addr		 id;
251 
252 	if (ospf_hdr->version != OSPF6_VERSION) {
253 		log_debug("recv_packet: invalid OSPF version %d",
254 		    ospf_hdr->version);
255 		return (-1);
256 	}
257 
258 	if (ntohs(ospf_hdr->len) > len ||
259 	    len <= sizeof(struct ospf_hdr)) {
260 		log_debug("recv_packet: invalid OSPF packet length %d",
261 		    ntohs(ospf_hdr->len));
262 		return (-1);
263 	}
264 
265 	if (iface->type != IF_TYPE_VIRTUALLINK) {
266 		if (ospf_hdr->area_id != iface->area_id.s_addr) {
267 			id.s_addr = ospf_hdr->area_id;
268 			log_debug("recv_packet: invalid area ID %s, "
269 			    "interface %s", inet_ntoa(id), iface->name);
270 			return (-1);
271 		}
272 	} else {
273 		if (ospf_hdr->area_id != 0) {
274 			id.s_addr = ospf_hdr->area_id;
275 			log_debug("recv_packet: invalid area ID %s, "
276 			    "interface %s", inet_ntoa(id), iface->name);
277 			return (-1);
278 		}
279 	}
280 
281 	if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
282 		if (inet_pton(AF_INET6, AllDRouters, &addr) == 0)
283 			fatalx("recv_packet: inet_pton");
284 		if (IN6_ARE_ADDR_EQUAL(dst, &addr) &&
285 		    (iface->state & IF_STA_DRORBDR) == 0) {
286 			log_debug("recv_packet: invalid destination IP in "
287 			    "state %s, interface %s",
288 			    if_state_name(iface->state), iface->name);
289 			return (-1);
290 		}
291 	}
292 
293 	return (ntohs(ospf_hdr->len));
294 }
295 
296 struct iface *
297 find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in6_addr *src)
298 {
299 	struct area	*area;
300 	struct iface	*iface, *match = NULL;
301 
302 	/*
303 	 * Returned interface needs to be active.
304 	 * Virtual-Links have higher precedence so the full interface
305 	 * list needs to be scanned for possible matches.
306 	 */
307 	LIST_FOREACH(area, &xconf->area_list, entry) {
308 		LIST_FOREACH(iface, &area->iface_list, entry) {
309 			switch (iface->type) {
310 			case IF_TYPE_VIRTUALLINK:
311 				if (IN6_ARE_ADDR_EQUAL(src, &iface->dst) &&
312 				    !(iface->cflags & F_IFACE_PASSIVE))
313 					return (iface);
314 				break;
315 			default:
316 				if (ifindex == iface->ifindex &&
317 				    !(iface->cflags & F_IFACE_PASSIVE))
318 					match = iface;
319 				break;
320 			}
321 		}
322 	}
323 
324 	return (match);
325 }
326