xref: /openbsd-src/usr.sbin/ospf6d/packet.c (revision 088a2cd995ac556ad6adb0ab83270324fa08ad97)
1*088a2cd9Sjsg /*	$OpenBSD: packet.c,v 1.23 2024/05/18 11:17:30 jsg Exp $ */
2a1a4e97bSnorby 
3a1a4e97bSnorby /*
4a1a4e97bSnorby  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5a1a4e97bSnorby  *
6a1a4e97bSnorby  * Permission to use, copy, modify, and distribute this software for any
7a1a4e97bSnorby  * purpose with or without fee is hereby granted, provided that the above
8a1a4e97bSnorby  * copyright notice and this permission notice appear in all copies.
9a1a4e97bSnorby  *
10a1a4e97bSnorby  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11a1a4e97bSnorby  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12a1a4e97bSnorby  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13a1a4e97bSnorby  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14a1a4e97bSnorby  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a1a4e97bSnorby  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16a1a4e97bSnorby  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17a1a4e97bSnorby  */
18a1a4e97bSnorby 
19a1a4e97bSnorby #include <sys/types.h>
20a1a4e97bSnorby #include <sys/socket.h>
21a1a4e97bSnorby #include <sys/uio.h>
22a1a4e97bSnorby 
23a1a4e97bSnorby #include <netinet/in.h>
24a1a4e97bSnorby #include <netinet/ip.h>
25a1a4e97bSnorby #include <netinet/ip6.h>
26a1a4e97bSnorby #include <arpa/inet.h>
27a1a4e97bSnorby 
28a1a4e97bSnorby #include <errno.h>
29a1a4e97bSnorby #include <event.h>
30b2cd1366Sguenther #include <limits.h>
31a1a4e97bSnorby #include <stdlib.h>
32a1a4e97bSnorby #include <string.h>
33a1a4e97bSnorby 
34a1a4e97bSnorby #include "ospf6d.h"
35a1a4e97bSnorby #include "ospf6.h"
36a1a4e97bSnorby #include "log.h"
37a1a4e97bSnorby #include "ospfe.h"
38a1a4e97bSnorby 
39448b61faSclaudio int		 ospf_hdr_sanity_check(struct ospf_hdr *, u_int16_t,
40448b61faSclaudio 		    const struct iface *, struct in6_addr *);
41448b61faSclaudio struct iface	*find_iface(struct ospfd_conf *, unsigned int,
42448b61faSclaudio 		    struct in6_addr *);
43a1a4e97bSnorby 
448efa32dfSclaudio static u_int8_t	*recv_buf;
458efa32dfSclaudio 
46a1a4e97bSnorby int
gen_ospf_hdr(struct ibuf * buf,struct iface * iface,u_int8_t type)47e39620e5Snicm gen_ospf_hdr(struct ibuf *buf, struct iface *iface, u_int8_t type)
48a1a4e97bSnorby {
49a1a4e97bSnorby 	struct ospf_hdr	ospf_hdr;
50a1a4e97bSnorby 
51a1a4e97bSnorby 	bzero(&ospf_hdr, sizeof(ospf_hdr));
52a1a4e97bSnorby 	ospf_hdr.version = OSPF6_VERSION;
53a1a4e97bSnorby 	ospf_hdr.type = type;
54a1a4e97bSnorby 	ospf_hdr.rtr_id = ospfe_router_id();
55a1a4e97bSnorby 	if (iface->type != IF_TYPE_VIRTUALLINK)
5677fbfa19Sdenis 		ospf_hdr.area_id = iface->area->id.s_addr;
57a1a4e97bSnorby 	ospf_hdr.instance = DEFAULT_INSTANCE_ID;
58a1a4e97bSnorby 	ospf_hdr.zero = 0;		/* must be zero */
59a1a4e97bSnorby 
60e39620e5Snicm 	return (ibuf_add(buf, &ospf_hdr, sizeof(ospf_hdr)));
61a1a4e97bSnorby }
62a1a4e97bSnorby 
63a1a4e97bSnorby int
upd_ospf_hdr(struct ibuf * buf,struct iface * iface)64e39620e5Snicm upd_ospf_hdr(struct ibuf *buf, struct iface *iface)
65a1a4e97bSnorby {
66a1a4e97bSnorby 	/* update length */
67297c3ba2Sclaudio 	if (ibuf_size(buf) > USHRT_MAX)
68a1a4e97bSnorby 		fatalx("upd_ospf_hdr: resulting ospf packet too big");
69297c3ba2Sclaudio 	if (ibuf_set_n16(buf, offsetof(struct ospf_hdr, len),
70297c3ba2Sclaudio 	    ibuf_size(buf)) == -1)
71297c3ba2Sclaudio 		fatalx("upd_ospf_hdr: ibuf_set_n16 failed");
72297c3ba2Sclaudio 
73297c3ba2Sclaudio 	/* checksum calculated via IPV6_CHECKSUM */
74297c3ba2Sclaudio 	if (ibuf_set_n16(buf, offsetof(struct ospf_hdr, chksum), 0) == -1)
75297c3ba2Sclaudio 		fatalx("upd_ospf_hdr: ibuf_set_n16 failed");
76a1a4e97bSnorby 
77a1a4e97bSnorby 	return (0);
78a1a4e97bSnorby }
79a1a4e97bSnorby 
80a1a4e97bSnorby /* send and receive packets */
81a1a4e97bSnorby int
send_packet(struct iface * iface,struct ibuf * buf,struct in6_addr * dst)8290d98d51Sdenis send_packet(struct iface *iface, struct ibuf *buf,
8352bf80cbSclaudio     struct in6_addr *dst)
84a1a4e97bSnorby {
8552bf80cbSclaudio 	struct sockaddr_in6	sa6;
8652bf80cbSclaudio 
874a6e6f20Sclaudio 	/* setup sockaddr */
8852bf80cbSclaudio 	bzero(&sa6, sizeof(sa6));
8952bf80cbSclaudio 	sa6.sin6_family = AF_INET6;
9052bf80cbSclaudio 	sa6.sin6_len = sizeof(sa6);
9152bf80cbSclaudio 	sa6.sin6_addr = *dst;
9252bf80cbSclaudio 
9352bf80cbSclaudio 	/* don't we all love link local scope and all the needed hacks for it */
9452bf80cbSclaudio 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst))
9552bf80cbSclaudio 		sa6.sin6_scope_id = iface->ifindex;
96a1a4e97bSnorby 
97a1a4e97bSnorby 	/* set outgoing interface for multicast traffic */
9852bf80cbSclaudio 	if (IN6_IS_ADDR_MULTICAST(dst))
99a1a4e97bSnorby 		if (if_set_mcast(iface) == -1) {
100a1a4e97bSnorby 			log_warn("send_packet: error setting multicast "
101a1a4e97bSnorby 			    "interface, %s", iface->name);
102a1a4e97bSnorby 			return (-1);
103a1a4e97bSnorby 		}
104a1a4e97bSnorby 
105bf2cf305Sclaudio 	if (sendto(iface->fd, ibuf_data(buf), ibuf_size(buf), 0,
1064a6e6f20Sclaudio 	    (struct sockaddr *)&sa6, sizeof(sa6)) == -1) {
107a1a4e97bSnorby 		log_warn("send_packet: error sending packet on interface %s",
108a1a4e97bSnorby 		    iface->name);
109a1a4e97bSnorby 		return (-1);
110a1a4e97bSnorby 	}
111a1a4e97bSnorby 
112a1a4e97bSnorby 	return (0);
113a1a4e97bSnorby }
114a1a4e97bSnorby 
115a1a4e97bSnorby void
recv_packet(int fd,short event,void * bula)116a1a4e97bSnorby recv_packet(int fd, short event, void *bula)
117a1a4e97bSnorby {
1180827ab61Sderaadt 	union {
1190827ab61Sderaadt 		struct cmsghdr hdr;
1200827ab61Sderaadt 		char	buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1210827ab61Sderaadt 	} cmsgbuf;
122a1a4e97bSnorby 	struct msghdr		 msg;
123a1a4e97bSnorby 	struct iovec		 iov;
124448b61faSclaudio 	struct in6_addr		 addr, dest;
125448b61faSclaudio 	struct sockaddr_in6	 src;
126a1a4e97bSnorby 	struct ospfd_conf	*xconf = bula;
127a1a4e97bSnorby 	struct ospf_hdr		*ospf_hdr;
128a1a4e97bSnorby 	struct iface		*iface;
129a1a4e97bSnorby 	struct nbr		*nbr = NULL;
130a1a4e97bSnorby 	char			*buf;
131a1a4e97bSnorby 	struct cmsghdr		*cmsg;
132a1a4e97bSnorby 	ssize_t			 r;
133a1a4e97bSnorby 	u_int16_t		 len;
134a1a4e97bSnorby 	int			 l;
135a1a4e97bSnorby 	unsigned int		 ifindex = 0;
136a1a4e97bSnorby 
137a1a4e97bSnorby 	if (event != EV_READ)
138a1a4e97bSnorby 		return;
139a1a4e97bSnorby 
1408efa32dfSclaudio 	if (recv_buf == NULL)
1418efa32dfSclaudio 		if ((recv_buf = malloc(READ_BUF_SIZE)) == NULL)
1428efa32dfSclaudio 			fatal(__func__);
1438efa32dfSclaudio 
144a1a4e97bSnorby 	/* setup buffer */
145a1a4e97bSnorby 	bzero(&msg, sizeof(msg));
1468efa32dfSclaudio 	iov.iov_base = buf = recv_buf;
147a1a4e97bSnorby 	iov.iov_len = READ_BUF_SIZE;
148448b61faSclaudio 	msg.msg_name = &src;
149448b61faSclaudio 	msg.msg_namelen = sizeof(src);
150a1a4e97bSnorby 	msg.msg_iov = &iov;
151a1a4e97bSnorby 	msg.msg_iovlen = 1;
1520827ab61Sderaadt 	msg.msg_control = &cmsgbuf.buf;
153da15c7b9Sderaadt 	msg.msg_controllen = sizeof(cmsgbuf.buf);
154a1a4e97bSnorby 
155a1a4e97bSnorby 	if ((r = recvmsg(fd, &msg, 0)) == -1) {
156a1a4e97bSnorby 		if (errno != EAGAIN && errno != EINTR)
157a1a4e97bSnorby 			log_debug("recv_packet: read error: %s",
158a1a4e97bSnorby 			    strerror(errno));
159a1a4e97bSnorby 		return;
160a1a4e97bSnorby 	}
161a1a4e97bSnorby 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
162a1a4e97bSnorby 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
163a1a4e97bSnorby 		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
164448b61faSclaudio 		    cmsg->cmsg_type == IPV6_PKTINFO) {
165448b61faSclaudio 			ifindex = ((struct in6_pktinfo *)
166448b61faSclaudio 			    CMSG_DATA(cmsg))->ipi6_ifindex;
167448b61faSclaudio 			dest = ((struct in6_pktinfo *)
168448b61faSclaudio 			    CMSG_DATA(cmsg))->ipi6_addr;
169a1a4e97bSnorby 			break;
170a1a4e97bSnorby 		}
171a1a4e97bSnorby 	}
172a1a4e97bSnorby 
173a1a4e97bSnorby 	/* find a matching interface */
174448b61faSclaudio 	if ((iface = find_iface(xconf, ifindex, &src.sin6_addr)) == NULL) {
175a1a4e97bSnorby 		/* XXX add a counter here */
176a1a4e97bSnorby 		return;
177a1a4e97bSnorby 	}
178a1a4e97bSnorby 	/*
179a1a4e97bSnorby 	 * Packet needs to be sent to AllSPFRouters or AllDRouters
180a1a4e97bSnorby 	 * or to the address of the interface itself.
181a1a4e97bSnorby 	 * AllDRouters is only valid for DR and BDR but this is checked later.
182a1a4e97bSnorby 	 */
183a1a4e97bSnorby 	inet_pton(AF_INET6, AllSPFRouters, &addr);
184448b61faSclaudio 	if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
185a1a4e97bSnorby 		inet_pton(AF_INET6, AllDRouters, &addr);
186448b61faSclaudio 		if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
1874a6e6f20Sclaudio 			struct iface_addr *ia;
1884a6e6f20Sclaudio 
1894a6e6f20Sclaudio 			TAILQ_FOREACH(ia, &iface->ifa_list, entry) {
1904a6e6f20Sclaudio 				if (IN6_ARE_ADDR_EQUAL(&dest, &ia->addr))
1914a6e6f20Sclaudio 					break;
1924a6e6f20Sclaudio 			}
1934a6e6f20Sclaudio 			if (ia == NULL) {
194a1a4e97bSnorby 				log_debug("recv_packet: packet sent to wrong "
195a1a4e97bSnorby 				    "address %s, interface %s",
196448b61faSclaudio 				    log_in6addr(&dest), iface->name);
197a1a4e97bSnorby 				return;
198a1a4e97bSnorby 			}
199a1a4e97bSnorby 		}
200a1a4e97bSnorby 	}
201448b61faSclaudio 
202448b61faSclaudio 	len = (u_int16_t)r;
203a1a4e97bSnorby 	/* OSPF header sanity checks */
204a1a4e97bSnorby 	if (len < sizeof(*ospf_hdr)) {
205a1a4e97bSnorby 		log_debug("recv_packet: bad packet size");
206a1a4e97bSnorby 		return;
207a1a4e97bSnorby 	}
208a1a4e97bSnorby 	ospf_hdr = (struct ospf_hdr *)buf;
209a1a4e97bSnorby 
210448b61faSclaudio 	if ((l = ospf_hdr_sanity_check(ospf_hdr, len, iface, &dest)) == -1)
211a1a4e97bSnorby 		return;
212a1a4e97bSnorby 
213a1a4e97bSnorby 	nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
214a1a4e97bSnorby 	if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
215a1a4e97bSnorby 		log_debug("recv_packet: unknown neighbor ID");
216a1a4e97bSnorby 		return;
217a1a4e97bSnorby 	}
218a1a4e97bSnorby 
219a1a4e97bSnorby 	buf += sizeof(*ospf_hdr);
220a1a4e97bSnorby 	len = l - sizeof(*ospf_hdr);
221a1a4e97bSnorby 
222a1a4e97bSnorby 	/* switch OSPF packet type */
223a1a4e97bSnorby 	switch (ospf_hdr->type) {
224a1a4e97bSnorby 	case PACKET_TYPE_HELLO:
225a1a4e97bSnorby 		inet_pton(AF_INET6, AllDRouters, &addr);
226448b61faSclaudio 		if (IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
227a1a4e97bSnorby 			log_debug("recv_packet: invalid destination IP "
228a1a4e97bSnorby 			     "address");
229a1a4e97bSnorby 			break;
230a1a4e97bSnorby 		}
231a1a4e97bSnorby 
232448b61faSclaudio 		recv_hello(iface, &src.sin6_addr, ospf_hdr->rtr_id, buf, len);
233a1a4e97bSnorby 		break;
234a1a4e97bSnorby 	case PACKET_TYPE_DD:
235a1a4e97bSnorby 		recv_db_description(nbr, buf, len);
236a1a4e97bSnorby 		break;
237a1a4e97bSnorby 	case PACKET_TYPE_LS_REQUEST:
238a1a4e97bSnorby 		recv_ls_req(nbr, buf, len);
239a1a4e97bSnorby 		break;
240a1a4e97bSnorby 	case PACKET_TYPE_LS_UPDATE:
241a1a4e97bSnorby 		recv_ls_update(nbr, buf, len);
242a1a4e97bSnorby 		break;
243a1a4e97bSnorby 	case PACKET_TYPE_LS_ACK:
244a1a4e97bSnorby 		recv_ls_ack(nbr, buf, len);
245a1a4e97bSnorby 		break;
246a1a4e97bSnorby 	default:
247a1a4e97bSnorby 		log_debug("recv_packet: unknown OSPF packet type, interface %s",
248a1a4e97bSnorby 		    iface->name);
249a1a4e97bSnorby 	}
250a1a4e97bSnorby }
251a1a4e97bSnorby 
252a1a4e97bSnorby int
ospf_hdr_sanity_check(struct ospf_hdr * ospf_hdr,u_int16_t len,const struct iface * iface,struct in6_addr * dst)253448b61faSclaudio ospf_hdr_sanity_check(struct ospf_hdr *ospf_hdr, u_int16_t len,
254448b61faSclaudio     const struct iface *iface, struct in6_addr *dst)
255a1a4e97bSnorby {
256a1a4e97bSnorby 	struct in6_addr		 addr;
257a1a4e97bSnorby 	struct in_addr		 id;
258a1a4e97bSnorby 
259a1a4e97bSnorby 	if (ospf_hdr->version != OSPF6_VERSION) {
260a1a4e97bSnorby 		log_debug("recv_packet: invalid OSPF version %d",
261a1a4e97bSnorby 		    ospf_hdr->version);
262a1a4e97bSnorby 		return (-1);
263a1a4e97bSnorby 	}
264a1a4e97bSnorby 
265a1a4e97bSnorby 	if (ntohs(ospf_hdr->len) > len ||
266a1a4e97bSnorby 	    len <= sizeof(struct ospf_hdr)) {
267a1a4e97bSnorby 		log_debug("recv_packet: invalid OSPF packet length %d",
268a1a4e97bSnorby 		    ntohs(ospf_hdr->len));
269a1a4e97bSnorby 		return (-1);
270a1a4e97bSnorby 	}
271a1a4e97bSnorby 
272a1a4e97bSnorby 	if (iface->type != IF_TYPE_VIRTUALLINK) {
27377fbfa19Sdenis 		if (ospf_hdr->area_id != iface->area->id.s_addr) {
274a1a4e97bSnorby 			id.s_addr = ospf_hdr->area_id;
275a1a4e97bSnorby 			log_debug("recv_packet: invalid area ID %s, "
276a1a4e97bSnorby 			    "interface %s", inet_ntoa(id), iface->name);
277a1a4e97bSnorby 			return (-1);
278a1a4e97bSnorby 		}
279a1a4e97bSnorby 	} else {
280a1a4e97bSnorby 		if (ospf_hdr->area_id != 0) {
281a1a4e97bSnorby 			id.s_addr = ospf_hdr->area_id;
282a1a4e97bSnorby 			log_debug("recv_packet: invalid area ID %s, "
283a1a4e97bSnorby 			    "interface %s", inet_ntoa(id), iface->name);
284a1a4e97bSnorby 			return (-1);
285a1a4e97bSnorby 		}
286a1a4e97bSnorby 	}
287a1a4e97bSnorby 
288a1a4e97bSnorby 	if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
289a1a4e97bSnorby 		if (inet_pton(AF_INET6, AllDRouters, &addr) == 0)
290a1a4e97bSnorby 			fatalx("recv_packet: inet_pton");
291448b61faSclaudio 		if (IN6_ARE_ADDR_EQUAL(dst, &addr) &&
292a1a4e97bSnorby 		    (iface->state & IF_STA_DRORBDR) == 0) {
293a1a4e97bSnorby 			log_debug("recv_packet: invalid destination IP in "
294a1a4e97bSnorby 			    "state %s, interface %s",
295a1a4e97bSnorby 			    if_state_name(iface->state), iface->name);
296a1a4e97bSnorby 			return (-1);
297a1a4e97bSnorby 		}
298a1a4e97bSnorby 	}
299a1a4e97bSnorby 
300a1a4e97bSnorby 	return (ntohs(ospf_hdr->len));
301a1a4e97bSnorby }
302a1a4e97bSnorby 
303a1a4e97bSnorby struct iface *
find_iface(struct ospfd_conf * xconf,unsigned int ifindex,struct in6_addr * src)304448b61faSclaudio find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in6_addr *src)
305a1a4e97bSnorby {
306448b61faSclaudio 	struct area	*area;
307448b61faSclaudio 	struct iface	*iface, *match = NULL;
308a1a4e97bSnorby 
309448b61faSclaudio 	/*
310448b61faSclaudio 	 * Returned interface needs to be active.
311448b61faSclaudio 	 * Virtual-Links have higher precedence so the full interface
312448b61faSclaudio 	 * list needs to be scanned for possible matches.
313448b61faSclaudio 	 */
314a1a4e97bSnorby 	LIST_FOREACH(area, &xconf->area_list, entry) {
315a1a4e97bSnorby 		LIST_FOREACH(iface, &area->iface_list, entry) {
316a1a4e97bSnorby 			switch (iface->type) {
317a1a4e97bSnorby 			case IF_TYPE_VIRTUALLINK:
318448b61faSclaudio 				if (IN6_ARE_ADDR_EQUAL(src, &iface->dst) &&
3196c9e7a5bSclaudio 				    !(iface->cflags & F_IFACE_PASSIVE))
320a1a4e97bSnorby 					return (iface);
321a1a4e97bSnorby 				break;
322a1a4e97bSnorby 			default:
323a1a4e97bSnorby 				if (ifindex == iface->ifindex &&
3246c9e7a5bSclaudio 				    !(iface->cflags & F_IFACE_PASSIVE))
325448b61faSclaudio 					match = iface;
326a1a4e97bSnorby 				break;
327a1a4e97bSnorby 			}
328a1a4e97bSnorby 		}
329a1a4e97bSnorby 	}
330a1a4e97bSnorby 
331448b61faSclaudio 	return (match);
332a1a4e97bSnorby }
333