xref: /openbsd/usr.sbin/ospf6d/packet.c (revision ef2d0f96)
1 /*	$OpenBSD: packet.c,v 1.19 2021/01/19 09:43:40 claudio 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 
86 	/* setup sockaddr */
87 	bzero(&sa6, sizeof(sa6));
88 	sa6.sin6_family = AF_INET6;
89 	sa6.sin6_len = sizeof(sa6);
90 	sa6.sin6_addr = *dst;
91 
92 	/* don't we all love link local scope and all the needed hacks for it */
93 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst))
94 		sa6.sin6_scope_id = iface->ifindex;
95 
96 	/* set outgoing interface for multicast traffic */
97 	if (IN6_IS_ADDR_MULTICAST(dst))
98 		if (if_set_mcast(iface) == -1) {
99 			log_warn("send_packet: error setting multicast "
100 			    "interface, %s", iface->name);
101 			return (-1);
102 		}
103 
104 	if (sendto(iface->fd, buf->buf, ibuf_size(buf), 0,
105 	    (struct sockaddr *)&sa6, sizeof(sa6)) == -1) {
106 		log_warn("send_packet: error sending packet on interface %s",
107 		    iface->name);
108 		return (-1);
109 	}
110 
111 	return (0);
112 }
113 
114 void
115 recv_packet(int fd, short event, void *bula)
116 {
117 	static char pkt_ptr[READ_BUF_SIZE];
118 	union {
119 		struct cmsghdr hdr;
120 		char	buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
121 	} cmsgbuf;
122 	struct msghdr		 msg;
123 	struct iovec		 iov;
124 	struct in6_addr		 addr, dest;
125 	struct sockaddr_in6	 src;
126 	struct ospfd_conf	*xconf = bula;
127 	struct ospf_hdr		*ospf_hdr;
128 	struct iface		*iface;
129 	struct nbr		*nbr = NULL;
130 	char			*buf;
131 	struct cmsghdr		*cmsg;
132 	ssize_t			 r;
133 	u_int16_t		 len;
134 	int			 l;
135 	unsigned int		 ifindex = 0;
136 
137 	if (event != EV_READ)
138 		return;
139 
140 	/* setup buffer */
141 	bzero(&msg, sizeof(msg));
142 	iov.iov_base = buf = pkt_ptr;
143 	iov.iov_len = READ_BUF_SIZE;
144 	msg.msg_name = &src;
145 	msg.msg_namelen = sizeof(src);
146 	msg.msg_iov = &iov;
147 	msg.msg_iovlen = 1;
148 	msg.msg_control = &cmsgbuf.buf;
149 	msg.msg_controllen = sizeof(cmsgbuf.buf);
150 
151 	if ((r = recvmsg(fd, &msg, 0)) == -1) {
152 		if (errno != EAGAIN && errno != EINTR)
153 			log_debug("recv_packet: read error: %s",
154 			    strerror(errno));
155 		return;
156 	}
157 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
158 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
159 		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
160 		    cmsg->cmsg_type == IPV6_PKTINFO) {
161 			ifindex = ((struct in6_pktinfo *)
162 			    CMSG_DATA(cmsg))->ipi6_ifindex;
163 			dest = ((struct in6_pktinfo *)
164 			    CMSG_DATA(cmsg))->ipi6_addr;
165 			break;
166 		}
167 	}
168 
169 	/* find a matching interface */
170 	if ((iface = find_iface(xconf, ifindex, &src.sin6_addr)) == NULL) {
171 		/* XXX add a counter here */
172 		return;
173 	}
174 	/*
175 	 * Packet needs to be sent to AllSPFRouters or AllDRouters
176 	 * or to the address of the interface itself.
177 	 * AllDRouters is only valid for DR and BDR but this is checked later.
178 	 */
179 	inet_pton(AF_INET6, AllSPFRouters, &addr);
180 	if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
181 		inet_pton(AF_INET6, AllDRouters, &addr);
182 		if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
183 			struct iface_addr *ia;
184 
185 			TAILQ_FOREACH(ia, &iface->ifa_list, entry) {
186 				if (IN6_ARE_ADDR_EQUAL(&dest, &ia->addr))
187 					break;
188 			}
189 			if (ia == NULL) {
190 				log_debug("recv_packet: packet sent to wrong "
191 				    "address %s, interface %s",
192 				    log_in6addr(&dest), iface->name);
193 				return;
194 			}
195 		}
196 	}
197 
198 	len = (u_int16_t)r;
199 	/* OSPF header sanity checks */
200 	if (len < sizeof(*ospf_hdr)) {
201 		log_debug("recv_packet: bad packet size");
202 		return;
203 	}
204 	ospf_hdr = (struct ospf_hdr *)buf;
205 
206 	if ((l = ospf_hdr_sanity_check(ospf_hdr, len, iface, &dest)) == -1)
207 		return;
208 
209 	nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
210 	if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
211 		log_debug("recv_packet: unknown neighbor ID");
212 		return;
213 	}
214 
215 	buf += sizeof(*ospf_hdr);
216 	len = l - sizeof(*ospf_hdr);
217 
218 	/* switch OSPF packet type */
219 	switch (ospf_hdr->type) {
220 	case PACKET_TYPE_HELLO:
221 		inet_pton(AF_INET6, AllDRouters, &addr);
222 		if (IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
223 			log_debug("recv_packet: invalid destination IP "
224 			     "address");
225 			break;
226 		}
227 
228 		recv_hello(iface, &src.sin6_addr, ospf_hdr->rtr_id, buf, len);
229 		break;
230 	case PACKET_TYPE_DD:
231 		recv_db_description(nbr, buf, len);
232 		break;
233 	case PACKET_TYPE_LS_REQUEST:
234 		recv_ls_req(nbr, buf, len);
235 		break;
236 	case PACKET_TYPE_LS_UPDATE:
237 		recv_ls_update(nbr, buf, len);
238 		break;
239 	case PACKET_TYPE_LS_ACK:
240 		recv_ls_ack(nbr, buf, len);
241 		break;
242 	default:
243 		log_debug("recv_packet: unknown OSPF packet type, interface %s",
244 		    iface->name);
245 	}
246 }
247 
248 int
249 ospf_hdr_sanity_check(struct ospf_hdr *ospf_hdr, u_int16_t len,
250     const struct iface *iface, struct in6_addr *dst)
251 {
252 	struct in6_addr		 addr;
253 	struct in_addr		 id;
254 
255 	if (ospf_hdr->version != OSPF6_VERSION) {
256 		log_debug("recv_packet: invalid OSPF version %d",
257 		    ospf_hdr->version);
258 		return (-1);
259 	}
260 
261 	if (ntohs(ospf_hdr->len) > len ||
262 	    len <= sizeof(struct ospf_hdr)) {
263 		log_debug("recv_packet: invalid OSPF packet length %d",
264 		    ntohs(ospf_hdr->len));
265 		return (-1);
266 	}
267 
268 	if (iface->type != IF_TYPE_VIRTUALLINK) {
269 		if (ospf_hdr->area_id != iface->area->id.s_addr) {
270 			id.s_addr = ospf_hdr->area_id;
271 			log_debug("recv_packet: invalid area ID %s, "
272 			    "interface %s", inet_ntoa(id), iface->name);
273 			return (-1);
274 		}
275 	} else {
276 		if (ospf_hdr->area_id != 0) {
277 			id.s_addr = ospf_hdr->area_id;
278 			log_debug("recv_packet: invalid area ID %s, "
279 			    "interface %s", inet_ntoa(id), iface->name);
280 			return (-1);
281 		}
282 	}
283 
284 	if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
285 		if (inet_pton(AF_INET6, AllDRouters, &addr) == 0)
286 			fatalx("recv_packet: inet_pton");
287 		if (IN6_ARE_ADDR_EQUAL(dst, &addr) &&
288 		    (iface->state & IF_STA_DRORBDR) == 0) {
289 			log_debug("recv_packet: invalid destination IP in "
290 			    "state %s, interface %s",
291 			    if_state_name(iface->state), iface->name);
292 			return (-1);
293 		}
294 	}
295 
296 	return (ntohs(ospf_hdr->len));
297 }
298 
299 struct iface *
300 find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in6_addr *src)
301 {
302 	struct area	*area;
303 	struct iface	*iface, *match = NULL;
304 
305 	/*
306 	 * Returned interface needs to be active.
307 	 * Virtual-Links have higher precedence so the full interface
308 	 * list needs to be scanned for possible matches.
309 	 */
310 	LIST_FOREACH(area, &xconf->area_list, entry) {
311 		LIST_FOREACH(iface, &area->iface_list, entry) {
312 			switch (iface->type) {
313 			case IF_TYPE_VIRTUALLINK:
314 				if (IN6_ARE_ADDR_EQUAL(src, &iface->dst) &&
315 				    !(iface->cflags & F_IFACE_PASSIVE))
316 					return (iface);
317 				break;
318 			default:
319 				if (ifindex == iface->ifindex &&
320 				    !(iface->cflags & F_IFACE_PASSIVE))
321 					match = iface;
322 				break;
323 			}
324 		}
325 	}
326 
327 	return (match);
328 }
329