xref: /openbsd-src/usr.sbin/ospf6d/hello.c (revision 1a8020151d66eb8fd7f0485960624eb47e443f96)
1*1a802015Sdenis /*	$OpenBSD: hello.c,v 1.23 2020/07/15 14:47:41 denis Exp $ */
2a1a4e97bSnorby 
3a1a4e97bSnorby /*
4a1a4e97bSnorby  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5a1a4e97bSnorby  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
6a1a4e97bSnorby  *
7a1a4e97bSnorby  * Permission to use, copy, modify, and distribute this software for any
8a1a4e97bSnorby  * purpose with or without fee is hereby granted, provided that the above
9a1a4e97bSnorby  * copyright notice and this permission notice appear in all copies.
10a1a4e97bSnorby  *
11a1a4e97bSnorby  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12a1a4e97bSnorby  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13a1a4e97bSnorby  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14a1a4e97bSnorby  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15a1a4e97bSnorby  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16a1a4e97bSnorby  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17a1a4e97bSnorby  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18a1a4e97bSnorby  */
19a1a4e97bSnorby 
20a1a4e97bSnorby #include <sys/types.h>
21a1a4e97bSnorby #include <sys/socket.h>
22a1a4e97bSnorby #include <netinet/in.h>
23a1a4e97bSnorby #include <arpa/inet.h>
24a1a4e97bSnorby #include <sys/time.h>
25a1a4e97bSnorby #include <stdlib.h>
26a1a4e97bSnorby #include <string.h>
27a1a4e97bSnorby #include <event.h>
28a1a4e97bSnorby 
29a1a4e97bSnorby #include "ospf6d.h"
30a1a4e97bSnorby #include "ospf6.h"
31a1a4e97bSnorby #include "log.h"
32a1a4e97bSnorby #include "ospfe.h"
33a1a4e97bSnorby 
34a1a4e97bSnorby /* hello packet handling */
35a1a4e97bSnorby int
send_hello(struct iface * iface)36a1a4e97bSnorby send_hello(struct iface *iface)
37a1a4e97bSnorby {
3852bf80cbSclaudio 	struct in6_addr		 dst;
39a1a4e97bSnorby 	struct hello_hdr	 hello;
40a1a4e97bSnorby 	struct nbr		*nbr;
41e39620e5Snicm 	struct ibuf		*buf;
42a1a4e97bSnorby 
43a1a4e97bSnorby 	switch (iface->type) {
44a1a4e97bSnorby 	case IF_TYPE_POINTOPOINT:
45a1a4e97bSnorby 	case IF_TYPE_BROADCAST:
4652bf80cbSclaudio 		inet_pton(AF_INET6, AllSPFRouters, &dst);
47a1a4e97bSnorby 		break;
48a1a4e97bSnorby 	case IF_TYPE_NBMA:
49a1a4e97bSnorby 	case IF_TYPE_POINTOMULTIPOINT:
50a1a4e97bSnorby 		log_debug("send_hello: type %s not supported, interface %s",
51a1a4e97bSnorby 		    if_type_name(iface->type), iface->name);
52a1a4e97bSnorby 		return (-1);
53a1a4e97bSnorby 	case IF_TYPE_VIRTUALLINK:
5452bf80cbSclaudio 		dst = iface->dst;
55a1a4e97bSnorby 		break;
56a1a4e97bSnorby 	default:
57a1a4e97bSnorby 		fatalx("send_hello: unknown interface type");
58a1a4e97bSnorby 	}
59a1a4e97bSnorby 
60e39620e5Snicm 	/* XXX IBUF_READ_SIZE */
61e39620e5Snicm 	if ((buf = ibuf_dynamic(PKG_DEF_SIZE, IBUF_READ_SIZE)) == NULL)
62a1a4e97bSnorby 		fatal("send_hello");
63a1a4e97bSnorby 
64a1a4e97bSnorby 	/* OSPF header */
65a1a4e97bSnorby 	if (gen_ospf_hdr(buf, iface, PACKET_TYPE_HELLO))
66a1a4e97bSnorby 		goto fail;
67a1a4e97bSnorby 
68a1a4e97bSnorby 	/* hello header */
6942c65ec5Sclaudio 	hello.iface_id = htonl(iface->ifindex);
702773be1bSclaudio 	LSA_24_SETHI(hello.opts, iface->priority);
717e18f193Sdenis 	LSA_24_SETLO(hello.opts, area_ospf_options(iface->area));
722773be1bSclaudio 	hello.opts = htonl(hello.opts);
73a1a4e97bSnorby 	hello.hello_interval = htons(iface->hello_interval);
74a1a4e97bSnorby 	hello.rtr_dead_interval = htons(iface->dead_interval);
75a1a4e97bSnorby 
76a1a4e97bSnorby 	if (iface->dr) {
77a1a4e97bSnorby 		hello.d_rtr = iface->dr->id.s_addr;
78a1a4e97bSnorby 		iface->self->dr.s_addr = iface->dr->id.s_addr;
79a1a4e97bSnorby 	} else
80a1a4e97bSnorby 		hello.d_rtr = 0;
81a1a4e97bSnorby 	if (iface->bdr) {
82a1a4e97bSnorby 		hello.bd_rtr = iface->bdr->id.s_addr;
83a1a4e97bSnorby 		iface->self->bdr.s_addr = iface->bdr->id.s_addr;
84a1a4e97bSnorby 	} else
85a1a4e97bSnorby 		hello.bd_rtr = 0;
86a1a4e97bSnorby 
87e39620e5Snicm 	if (ibuf_add(buf, &hello, sizeof(hello)))
88a1a4e97bSnorby 		goto fail;
89a1a4e97bSnorby 
90a1a4e97bSnorby 	/* active neighbor(s) */
91a1a4e97bSnorby 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
92a1a4e97bSnorby 		if ((nbr->state >= NBR_STA_INIT) && (nbr != iface->self))
93e39620e5Snicm 			if (ibuf_add(buf, &nbr->id, sizeof(nbr->id)))
94a1a4e97bSnorby 				goto fail;
95a1a4e97bSnorby 	}
96a1a4e97bSnorby 
97a1a4e97bSnorby 	/* calculate checksum */
98a1a4e97bSnorby 	if (upd_ospf_hdr(buf, iface))
99a1a4e97bSnorby 		goto fail;
100a1a4e97bSnorby 
1017e18f193Sdenis 	if (send_packet(iface, buf, &dst) == -1)
1027e18f193Sdenis 		goto fail;
103a1a4e97bSnorby 
104e39620e5Snicm 	ibuf_free(buf);
1057e18f193Sdenis 	return (0);
106a1a4e97bSnorby fail:
107a1a4e97bSnorby 	log_warn("send_hello");
108e39620e5Snicm 	ibuf_free(buf);
109a1a4e97bSnorby 	return (-1);
110a1a4e97bSnorby }
111a1a4e97bSnorby 
112a1a4e97bSnorby void
recv_hello(struct iface * iface,struct in6_addr * src,u_int32_t rtr_id,char * buf,u_int16_t len)113448b61faSclaudio recv_hello(struct iface *iface, struct in6_addr *src, u_int32_t rtr_id,
114a1a4e97bSnorby     char *buf, u_int16_t len)
115a1a4e97bSnorby {
116a1a4e97bSnorby 	struct hello_hdr	 hello;
117a1a4e97bSnorby 	struct nbr		*nbr = NULL, *dr;
1182773be1bSclaudio 	u_int32_t		 nbr_id, opts;
119a1a4e97bSnorby 	int			 nbr_change = 0;
120a1a4e97bSnorby 
12165b16aecSjacekm 	if (len < sizeof(hello) || (len & 0x03)) {
122a1a4e97bSnorby 		log_warnx("recv_hello: bad packet size, interface %s",
123a1a4e97bSnorby 		    iface->name);
124a1a4e97bSnorby 		return;
125a1a4e97bSnorby 	}
126a1a4e97bSnorby 
127a1a4e97bSnorby 	memcpy(&hello, buf, sizeof(hello));
128a1a4e97bSnorby 	buf += sizeof(hello);
129a1a4e97bSnorby 	len -= sizeof(hello);
130a1a4e97bSnorby 
131a1a4e97bSnorby 	if (ntohs(hello.hello_interval) != iface->hello_interval) {
132a1a4e97bSnorby 		log_warnx("recv_hello: invalid hello-interval %d, "
133a1a4e97bSnorby 		    "interface %s", ntohs(hello.hello_interval),
134a1a4e97bSnorby 		    iface->name);
135a1a4e97bSnorby 		return;
136a1a4e97bSnorby 	}
137a1a4e97bSnorby 
138448b61faSclaudio 	if (ntohs(hello.rtr_dead_interval) != iface->dead_interval) {
139a1a4e97bSnorby 		log_warnx("recv_hello: invalid router-dead-interval %d, "
140a1a4e97bSnorby 		    "interface %s", ntohl(hello.rtr_dead_interval),
141a1a4e97bSnorby 		    iface->name);
142a1a4e97bSnorby 		return;
143a1a4e97bSnorby 	}
144a1a4e97bSnorby 
1452773be1bSclaudio 	opts = LSA_24_GETLO(ntohl(hello.opts));
1467e18f193Sdenis 	if ((opts & OSPF_OPTION_E && iface->area->stub) ||
1477e18f193Sdenis 	    ((opts & OSPF_OPTION_E) == 0 && !iface->area->stub)) {
148a1a4e97bSnorby 		log_warnx("recv_hello: ExternalRoutingCapability mismatch, "
149a1a4e97bSnorby 		    "interface %s", iface->name);
150a1a4e97bSnorby 		return;
151a1a4e97bSnorby 	}
152a1a4e97bSnorby 
153a1a4e97bSnorby 	/* match router-id */
154a1a4e97bSnorby 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
1557e18f193Sdenis 		if (nbr == iface->self) {
1567e18f193Sdenis 			if (nbr->id.s_addr == rtr_id) {
1577e18f193Sdenis 				log_warnx("recv_hello: Router-ID collision on "
1587e18f193Sdenis 				    "interface %s neighbor IP %s", iface->name,
1597e18f193Sdenis 				    log_in6addr(src));
1607e18f193Sdenis 				return;
1617e18f193Sdenis 			}
162a1a4e97bSnorby 			continue;
1637e18f193Sdenis 		}
164a1a4e97bSnorby 		if (nbr->id.s_addr == rtr_id)
165a1a4e97bSnorby 			break;
166a1a4e97bSnorby 	}
167a1a4e97bSnorby 
168a1a4e97bSnorby 	if (!nbr) {
169d2afa435Sstsp 		nbr = nbr_new(rtr_id, iface, ntohl(hello.iface_id), 0, src);
170a1a4e97bSnorby 		/* set neighbor parameters */
171a1a4e97bSnorby 		nbr->dr.s_addr = hello.d_rtr;
172a1a4e97bSnorby 		nbr->bdr.s_addr = hello.bd_rtr;
1732773be1bSclaudio 		nbr->priority = LSA_24_GETHI(ntohl(hello.opts));
1740a8c1314Sremi 		/* XXX neighbor address shouldn't be stored on virtual links */
1750a8c1314Sremi 		nbr->addr = *src;
176a1a4e97bSnorby 	}
177a1a4e97bSnorby 
1780a8c1314Sremi 	if (!IN6_ARE_ADDR_EQUAL(&nbr->addr, src)) {
1790a8c1314Sremi 		log_warnx("%s: neighbor ID %s changed its address to %s",
1800a8c1314Sremi 		    __func__, inet_ntoa(nbr->id), log_in6addr(src));
181448b61faSclaudio 		nbr->addr = *src;
1820a8c1314Sremi 	}
1830a8c1314Sremi 
1842773be1bSclaudio 	nbr->options = opts;
185a1a4e97bSnorby 
186a1a4e97bSnorby 	nbr_fsm(nbr, NBR_EVT_HELLO_RCVD);
187a1a4e97bSnorby 
188a1a4e97bSnorby 	while (len >= sizeof(nbr_id)) {
189a1a4e97bSnorby 		memcpy(&nbr_id, buf, sizeof(nbr_id));
190a1a4e97bSnorby 		if (nbr_id == ospfe_router_id()) {
191a1a4e97bSnorby 			/* seen myself */
192031a0b2eSclaudio 			if (nbr->state & NBR_STA_PRELIM) {
193a1a4e97bSnorby 				nbr_fsm(nbr, NBR_EVT_2_WAY_RCVD);
194031a0b2eSclaudio 				nbr_change = 1;
195031a0b2eSclaudio 			}
196a1a4e97bSnorby 			break;
197a1a4e97bSnorby 		}
198a1a4e97bSnorby 		buf += sizeof(nbr_id);
199a1a4e97bSnorby 		len -= sizeof(nbr_id);
200a1a4e97bSnorby 	}
201a1a4e97bSnorby 
202a1a4e97bSnorby 	if (len == 0) {
203a1a4e97bSnorby 		nbr_fsm(nbr, NBR_EVT_1_WAY_RCVD);
204a1a4e97bSnorby 		/* set neighbor parameters */
205a1a4e97bSnorby 		nbr->dr.s_addr = hello.d_rtr;
206a1a4e97bSnorby 		nbr->bdr.s_addr = hello.bd_rtr;
2072773be1bSclaudio 		nbr->priority = LSA_24_GETHI(ntohl(hello.opts));
208a1a4e97bSnorby 		return;
209a1a4e97bSnorby 	}
210a1a4e97bSnorby 
2112773be1bSclaudio 	if (nbr->priority != LSA_24_GETHI(ntohl(hello.opts))) {
2122773be1bSclaudio 		nbr->priority = LSA_24_GETHI(ntohl(hello.opts));
213a1a4e97bSnorby 		nbr_change = 1;
214a1a4e97bSnorby 	}
215a1a4e97bSnorby 
216a1a4e97bSnorby 	if (iface->state & IF_STA_WAITING &&
217a1a4e97bSnorby 	    hello.d_rtr == nbr->id.s_addr && hello.bd_rtr == 0)
218a1a4e97bSnorby 		if_fsm(iface, IF_EVT_BACKUP_SEEN);
219a1a4e97bSnorby 
220a1a4e97bSnorby 	if (iface->state & IF_STA_WAITING && hello.bd_rtr == nbr->id.s_addr) {
221a1a4e97bSnorby 		/*
222a1a4e97bSnorby 		 * In case we see the BDR make sure that the DR is around
223a1a4e97bSnorby 		 * with a bidirectional (2_WAY or better) connection
224a1a4e97bSnorby 		 */
225a1a4e97bSnorby 		LIST_FOREACH(dr, &iface->nbr_list, entry)
226a1a4e97bSnorby 			if (hello.d_rtr == dr->id.s_addr &&
227a1a4e97bSnorby 			    dr->state & NBR_STA_BIDIR)
228a1a4e97bSnorby 				if_fsm(iface, IF_EVT_BACKUP_SEEN);
229a1a4e97bSnorby 	}
230cfea0217Snorby 
231cfea0217Snorby 	if ((nbr->id.s_addr == nbr->dr.s_addr &&
232cfea0217Snorby 	    nbr->id.s_addr != hello.d_rtr) ||
233cfea0217Snorby 	    (nbr->id.s_addr != nbr->dr.s_addr &&
234cfea0217Snorby 	    nbr->id.s_addr == hello.d_rtr))
235a1a4e97bSnorby 		/* neighbor changed from or to DR */
236a1a4e97bSnorby 		nbr_change = 1;
237cfea0217Snorby 	if ((nbr->id.s_addr == nbr->bdr.s_addr &&
238cfea0217Snorby 	    nbr->id.s_addr != hello.bd_rtr) ||
239cfea0217Snorby 	    (nbr->id.s_addr != nbr->bdr.s_addr &&
240cfea0217Snorby 	    nbr->id.s_addr == hello.bd_rtr))
241a1a4e97bSnorby 		/* neighbor changed from or to BDR */
242a1a4e97bSnorby 		nbr_change = 1;
243cfea0217Snorby 
244a1a4e97bSnorby 	nbr->dr.s_addr = hello.d_rtr;
245a1a4e97bSnorby 	nbr->bdr.s_addr = hello.bd_rtr;
246a1a4e97bSnorby 
247a1a4e97bSnorby 	if (nbr_change)
248a1a4e97bSnorby 		if_fsm(iface, IF_EVT_NBR_CHNG);
249a1a4e97bSnorby 
250a1a4e97bSnorby 	/* TODO NBMA needs some special handling */
251a1a4e97bSnorby }
252