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