xref: /openbsd-src/usr.sbin/ospf6d/interface.c (revision 3897a7489f35ad36869f385ef2881fea1487df84)
1 /*	$OpenBSD: interface.c,v 1.6 2007/11/24 16:35:16 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5  * Copyright (c) 2004, 2005, 2007 Esben Norby <norby@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/time.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <net/if.h>
27 #include <net/if_types.h>
28 #include <ctype.h>
29 #include <err.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <event.h>
36 
37 #include "ospf6d.h"
38 #include "ospf6.h"
39 #include "log.h"
40 #include "ospfe.h"
41 
42 void		 if_hello_timer(int, short, void *);
43 void		 if_start_hello_timer(struct iface *);
44 void		 if_stop_hello_timer(struct iface *);
45 void		 if_stop_wait_timer(struct iface *);
46 void		 if_wait_timer(int, short, void *);
47 void		 if_start_wait_timer(struct iface *);
48 void		 if_stop_wait_timer(struct iface *);
49 struct nbr	*if_elect(struct nbr *, struct nbr *);
50 
51 struct {
52 	int			state;
53 	enum iface_event	event;
54 	enum iface_action	action;
55 	int			new_state;
56 } iface_fsm[] = {
57     /* current state	event that happened	action to take	resulting state */
58     {IF_STA_DOWN,	IF_EVT_UP,		IF_ACT_STRT,	0},
59     {IF_STA_WAITING,	IF_EVT_BACKUP_SEEN,	IF_ACT_ELECT,	0},
60     {IF_STA_WAITING,	IF_EVT_WTIMER,		IF_ACT_ELECT,	0},
61     {IF_STA_ANY,	IF_EVT_WTIMER,		IF_ACT_NOTHING,	0},
62     {IF_STA_WAITING,	IF_EVT_NBR_CHNG,	IF_ACT_NOTHING,	0},
63     {IF_STA_MULTI,	IF_EVT_NBR_CHNG,	IF_ACT_ELECT,	0},
64     {IF_STA_ANY,	IF_EVT_NBR_CHNG,	IF_ACT_NOTHING,	0},
65     {IF_STA_ANY,	IF_EVT_DOWN,		IF_ACT_RST,	IF_STA_DOWN},
66     {IF_STA_ANY,	IF_EVT_LOOP,		IF_ACT_RST,	IF_STA_LOOPBACK},
67     {IF_STA_LOOPBACK,	IF_EVT_UNLOOP,		IF_ACT_NOTHING,	IF_STA_DOWN},
68     {-1,		IF_EVT_NOTHING,		IF_ACT_NOTHING,	0},
69 };
70 
71 static int vlink_cnt = 0;
72 
73 const char * const if_event_names[] = {
74 	"NOTHING",
75 	"UP",
76 	"WAITTIMER",
77 	"BACKUPSEEN",
78 	"NEIGHBORCHANGE",
79 	"LOOP",
80 	"UNLOOP",
81 	"DOWN"
82 };
83 
84 const char * const if_action_names[] = {
85 	"NOTHING",
86 	"START",
87 	"ELECT",
88 	"RESET"
89 };
90 
91 int
92 if_fsm(struct iface *iface, enum iface_event event)
93 {
94 	int	old_state;
95 	int	new_state = 0;
96 	int	i, ret = 0;
97 
98 	old_state = iface->state;
99 
100 	for (i = 0; iface_fsm[i].state != -1; i++)
101 		if ((iface_fsm[i].state & old_state) &&
102 		    (iface_fsm[i].event == event)) {
103 			new_state = iface_fsm[i].new_state;
104 			break;
105 		}
106 
107 	if (iface_fsm[i].state == -1) {
108 		/* event outside of the defined fsm, ignore it. */
109 		log_debug("if_fsm: interface %s, "
110 		    "event %s not expected in state %s", iface->name,
111 		    if_event_names[event], if_state_name(old_state));
112 		return (0);
113 	}
114 
115 	switch (iface_fsm[i].action) {
116 	case IF_ACT_STRT:
117 		ret = if_act_start(iface);
118 		break;
119 	case IF_ACT_ELECT:
120 		ret = if_act_elect(iface);
121 		break;
122 	case IF_ACT_RST:
123 		ret = if_act_reset(iface);
124 		break;
125 	case IF_ACT_NOTHING:
126 		/* do nothing */
127 		break;
128 	}
129 
130 	if (ret) {
131 		log_debug("if_fsm: error changing state for interface %s, "
132 		    "event %s, state %s", iface->name, if_event_names[event],
133 		    if_state_name(old_state));
134 		return (-1);
135 	}
136 
137 	if (new_state != 0)
138 		iface->state = new_state;
139 
140 	if (iface->state != old_state)
141 		orig_rtr_lsa(iface->area);
142 
143 	if (old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT) &&
144 	    (iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0)
145 		ospfe_demote_iface(iface, 0);
146 	if ((old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0 &&
147 	    iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT))
148 		ospfe_demote_iface(iface, 1);
149 
150 	log_debug("if_fsm: event %s resulted in action %s and changing "
151 	    "state for interface %s from %s to %s",
152 	    if_event_names[event], if_action_names[iface_fsm[i].action],
153 	    iface->name, if_state_name(old_state), if_state_name(iface->state));
154 
155 	return (ret);
156 }
157 
158 struct iface *
159 if_new(struct kif *kif, struct kif_addr *ka)
160 {
161 	struct iface		*iface;
162 
163 	if ((iface = calloc(1, sizeof(*iface))) == NULL)
164 		err(1, "if_new: calloc");
165 
166 	iface->state = IF_STA_DOWN;
167 
168 	LIST_INIT(&iface->nbr_list);
169 	TAILQ_INIT(&iface->ls_ack_list);
170 
171 	if (kif == NULL) {
172 		iface->type = IF_TYPE_VIRTUALLINK;
173 		snprintf(iface->name, sizeof(iface->name), "vlink%d",
174 		    vlink_cnt++);
175 		iface->flags |= IFF_UP;
176 		iface->mtu = IP_MSS;
177 		return (iface);
178 	}
179 
180 	strlcpy(iface->name, kif->ifname, sizeof(iface->name));
181 
182 	/* get type */
183 	if (kif->flags & IFF_POINTOPOINT)
184 		iface->type = IF_TYPE_POINTOPOINT;
185 	if (kif->flags & IFF_BROADCAST &&
186 	    kif->flags & IFF_MULTICAST)
187 		iface->type = IF_TYPE_BROADCAST;
188 	if (kif->flags & IFF_LOOPBACK) {
189 		iface->type = IF_TYPE_POINTOPOINT;
190 		iface->state = IF_STA_LOOPBACK;
191 	}
192 
193 	/* get mtu, index and flags */
194 	iface->mtu = kif->mtu;
195 	iface->ifindex = kif->ifindex;
196 	iface->flags = kif->flags;
197 	iface->linkstate = kif->link_state;
198 	iface->media_type = kif->media_type;
199 	iface->baudrate = kif->baudrate;
200 
201 	/* set address, mask and p2p addr */
202 	iface->addr = ka->addr;
203 	if (kif->flags & IFF_POINTOPOINT) {
204 		iface->dst = ka->dstbrd;
205 	}
206 
207 	return (iface);
208 }
209 
210 void
211 if_del(struct iface *iface)
212 {
213 	struct nbr	*nbr = NULL;
214 
215 	log_debug("if_del: interface %s", iface->name);
216 
217 	/* revert the demotion when the interface is deleted */
218 	if ((iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0)
219 		ospfe_demote_iface(iface, 1);
220 
221 	/* clear lists etc */
222 	while ((nbr = LIST_FIRST(&iface->nbr_list)) != NULL)
223 		nbr_del(nbr);
224 
225 	if (evtimer_pending(&iface->hello_timer, NULL))
226 		evtimer_del(&iface->hello_timer);
227 	if (evtimer_pending(&iface->wait_timer, NULL))
228 		evtimer_del(&iface->wait_timer);
229 	if (evtimer_pending(&iface->lsack_tx_timer, NULL))
230 		evtimer_del(&iface->lsack_tx_timer);
231 
232 	ls_ack_list_clr(iface);
233 	free(iface);
234 }
235 
236 void
237 if_init(struct ospfd_conf *xconf, struct iface *iface)
238 {
239 	/* init the dummy local neighbor */
240 	iface->self = nbr_new(ospfe_router_id(), iface, 1);
241 
242 	/* set event handlers for interface */
243 	evtimer_set(&iface->lsack_tx_timer, ls_ack_tx_timer, iface);
244 	evtimer_set(&iface->hello_timer, if_hello_timer, iface);
245 	evtimer_set(&iface->wait_timer, if_wait_timer, iface);
246 
247 	iface->fd = xconf->ospf_socket;
248 
249 	ospfe_demote_iface(iface, 0);
250 }
251 
252 /* timers */
253 /* ARGSUSED */
254 void
255 if_hello_timer(int fd, short event, void *arg)
256 {
257 	struct iface *iface = arg;
258 	struct timeval tv;
259 
260 	send_hello(iface);
261 
262 	/* reschedule hello_timer */
263 	timerclear(&tv);
264 	tv.tv_sec = iface->hello_interval;
265 	if (evtimer_add(&iface->hello_timer, &tv) == -1)
266 		fatal("if_hello_timer");
267 }
268 
269 void
270 if_start_hello_timer(struct iface *iface)
271 {
272 	struct timeval tv;
273 
274 	timerclear(&tv);
275 	if (evtimer_add(&iface->hello_timer, &tv) == -1)
276 		fatal("if_start_hello_timer");
277 }
278 
279 void
280 if_stop_hello_timer(struct iface *iface)
281 {
282 	if (evtimer_del(&iface->hello_timer) == -1)
283 		fatal("if_stop_hello_timer");
284 }
285 
286 /* ARGSUSED */
287 void
288 if_wait_timer(int fd, short event, void *arg)
289 {
290 	struct iface *iface = arg;
291 
292 	if_fsm(iface, IF_EVT_WTIMER);
293 }
294 
295 void
296 if_start_wait_timer(struct iface *iface)
297 {
298 	struct timeval	tv;
299 
300 	timerclear(&tv);
301 	tv.tv_sec = iface->dead_interval;
302 	if (evtimer_add(&iface->wait_timer, &tv) == -1)
303 		fatal("if_start_wait_timer");
304 }
305 
306 void
307 if_stop_wait_timer(struct iface *iface)
308 {
309 	if (evtimer_del(&iface->wait_timer) == -1)
310 		fatal("if_stop_wait_timer");
311 }
312 
313 /* actions */
314 int
315 if_act_start(struct iface *iface)
316 {
317 	struct in6_addr		 addr;
318 	struct timeval		 now;
319 
320 	if (!((iface->flags & IFF_UP) &&
321 	    (LINK_STATE_IS_UP(iface->linkstate) ||
322 	    (iface->linkstate == LINK_STATE_UNKNOWN &&
323 	    iface->media_type != IFT_CARP)))) {
324 		log_debug("if_act_start: interface %s link down",
325 		    iface->name);
326 		return (0);
327 	}
328 
329 	if (iface->media_type == IFT_CARP && iface->passive == 0) {
330 		/* force passive mode on carp interfaces */
331 		log_warnx("if_act_start: forcing interface %s to passive",
332 		    iface->name);
333 		iface->passive = 1;
334 	}
335 
336 	if (iface->passive) {
337 		/* for an update of stub network entries */
338 		orig_rtr_lsa(iface->area);
339 		return (0);
340 	}
341 
342 	gettimeofday(&now, NULL);
343 	iface->uptime = now.tv_sec;
344 
345 	switch (iface->type) {
346 	case IF_TYPE_POINTOPOINT:
347 		inet_pton(AF_INET6, AllSPFRouters, &addr);
348 
349 		if (if_join_group(iface, &addr))
350 			return (-1);
351 		iface->state = IF_STA_POINTTOPOINT;
352 		break;
353 	case IF_TYPE_VIRTUALLINK:
354 		iface->state = IF_STA_POINTTOPOINT;
355 		break;
356 	case IF_TYPE_POINTOMULTIPOINT:
357 	case IF_TYPE_NBMA:
358 		log_debug("if_act_start: type %s not supported, interface %s",
359 		    if_type_name(iface->type), iface->name);
360 		return (-1);
361 	case IF_TYPE_BROADCAST:
362 		inet_pton(AF_INET6, AllSPFRouters, &addr);
363 
364 		if (if_join_group(iface, &addr))
365 			return (-1);
366 		if (iface->priority == 0) {
367 			iface->state = IF_STA_DROTHER;
368 		} else {
369 			iface->state = IF_STA_WAITING;
370 			if_start_wait_timer(iface);
371 		}
372 		break;
373 	default:
374 		fatalx("if_act_start: unknown interface type");
375 	}
376 
377 	/* hello timer needs to be started in any case */
378 	if_start_hello_timer(iface);
379 	return (0);
380 }
381 
382 struct nbr *
383 if_elect(struct nbr *a, struct nbr *b)
384 {
385 	if (a->priority > b->priority)
386 		return (a);
387 	if (a->priority < b->priority)
388 		return (b);
389 	if (ntohl(a->id.s_addr) > ntohl(b->id.s_addr))
390 		return (a);
391 	return (b);
392 }
393 
394 int
395 if_act_elect(struct iface *iface)
396 {
397 	struct in6_addr	 addr;
398 	struct nbr	*nbr, *bdr = NULL, *dr = NULL;
399 	int		 round = 0;
400 	int		 changed = 0;
401 	int		 old_state;
402 	char		 b1[16], b2[16], b3[16], b4[16];
403 
404 start:
405 	/* elect backup designated router */
406 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
407 		if (nbr->priority == 0 || nbr == dr ||	/* not electable */
408 		    nbr->state & NBR_STA_PRELIM ||	/* not available */
409 		    nbr->dr.s_addr == nbr->id.s_addr)	/* don't elect DR */
410 			continue;
411 		if (bdr != NULL) {
412 			/*
413 			 * routers announcing themselves as BDR have higher
414 			 * precedence over those routers announcing a
415 			 * different BDR.
416 			 */
417 			if (nbr->bdr.s_addr == nbr->id.s_addr) {
418 				if (bdr->bdr.s_addr == bdr->id.s_addr)
419 					bdr = if_elect(bdr, nbr);
420 				else
421 					bdr = nbr;
422 			} else if (bdr->bdr.s_addr != bdr->id.s_addr)
423 					bdr = if_elect(bdr, nbr);
424 		} else
425 			bdr = nbr;
426 	}
427 
428 	/* elect designated router */
429 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
430 		if (nbr->priority == 0 || nbr->state & NBR_STA_PRELIM ||
431 		    (nbr != dr && nbr->dr.s_addr != nbr->id.s_addr))
432 			/* only DR may be elected check priority too */
433 			continue;
434 		if (dr == NULL)
435 			dr = nbr;
436 		else
437 			dr = if_elect(dr, nbr);
438 	}
439 
440 	if (dr == NULL) {
441 		/* no designate router found use backup DR */
442 		dr = bdr;
443 		bdr = NULL;
444 	}
445 
446 	/*
447 	 * if we are involved in the election (e.g. new DR or no
448 	 * longer BDR) redo the election
449 	 */
450 	if (round == 0 &&
451 	    ((iface->self == dr && iface->self != iface->dr) ||
452 	    (iface->self != dr && iface->self == iface->dr) ||
453 	    (iface->self == bdr && iface->self != iface->bdr) ||
454 	    (iface->self != bdr && iface->self == iface->bdr))) {
455 		/*
456 		 * Reset announced DR/BDR to calculated one, so
457 		 * that we may get elected in the second round.
458 		 * This is needed to drop from a DR to a BDR.
459 		 */
460 		iface->self->dr.s_addr = dr->id.s_addr;
461 		if (bdr)
462 			iface->self->bdr.s_addr = bdr->id.s_addr;
463 		round = 1;
464 		goto start;
465 	}
466 
467 	log_debug("if_act_elect: interface %s old dr %s new dr %s, "
468 	    "old bdr %s new bdr %s", iface->name,
469 	    iface->dr ? inet_ntop(AF_INET, &iface->dr->id, b1, sizeof(b1)) :
470 	    "none", dr ? inet_ntop(AF_INET, &dr->id, b2, sizeof(b2)) : "none",
471 	    iface->bdr ? inet_ntop(AF_INET, &iface->bdr->id, b3, sizeof(b3)) :
472 	    "none", bdr ? inet_ntop(AF_INET, &bdr->id, b4, sizeof(b4)) :
473 	    "none");
474 
475 	/*
476 	 * After the second round still DR or BDR change state to DR or BDR,
477 	 * etc.
478 	 */
479 	old_state = iface->state;
480 	if (dr == iface->self)
481 		iface->state = IF_STA_DR;
482 	else if (bdr == iface->self)
483 		iface->state = IF_STA_BACKUP;
484 	else
485 		iface->state = IF_STA_DROTHER;
486 
487 	/* TODO if iface is NBMA send all non eligible neighbors event Start */
488 
489 	/*
490 	 * if DR or BDR changed issue a AdjOK? event for all neighbors > 2-Way
491 	 */
492 	if (iface->dr != dr || iface->bdr != bdr)
493 		changed = 1;
494 
495 	iface->dr = dr;
496 	iface->bdr = bdr;
497 
498 	if (changed) {
499 		inet_pton(AF_INET6, AllDRouters, &addr);
500 		if (old_state & IF_STA_DRORBDR &&
501 		    (iface->state & IF_STA_DRORBDR) == 0) {
502 			if (if_leave_group(iface, &addr))
503 				return (-1);
504 		} else if ((old_state & IF_STA_DRORBDR) == 0 &&
505 		    iface->state & IF_STA_DRORBDR) {
506 			if (if_join_group(iface, &addr))
507 				return (-1);
508 		}
509 
510 		LIST_FOREACH(nbr, &iface->nbr_list, entry) {
511 			if (nbr->state & NBR_STA_BIDIR)
512 				nbr_fsm(nbr, NBR_EVT_ADJ_OK);
513 		}
514 
515 		orig_rtr_lsa(iface->area);
516 		if (iface->state & IF_STA_DR || old_state & IF_STA_DR)
517 			orig_net_lsa(iface);
518 	}
519 
520 	if_start_hello_timer(iface);
521 	return (0);
522 }
523 
524 int
525 if_act_reset(struct iface *iface)
526 {
527 	struct nbr		*nbr = NULL;
528 	struct in6_addr		 addr;
529 
530 	if (iface->passive) {
531 		/* for an update of stub network entries */
532 		orig_rtr_lsa(iface->area);
533 		return (0);
534 	}
535 
536 	switch (iface->type) {
537 	case IF_TYPE_POINTOPOINT:
538 	case IF_TYPE_BROADCAST:
539 		inet_pton(AF_INET6, AllSPFRouters, &addr);
540 		if (if_leave_group(iface, &addr)) {
541 			log_warnx("if_act_reset: error leaving group %s, "
542 			    "interface %s", log_in6addr(&addr), iface->name);
543 		}
544 		if (iface->state & IF_STA_DRORBDR) {
545 			inet_pton(AF_INET6, AllDRouters, &addr);
546 			if (if_leave_group(iface, &addr)) {
547 				log_warnx("if_act_reset: "
548 				    "error leaving group %s, interface %s",
549 				    log_in6addr(&addr), iface->name);
550 			}
551 		}
552 		break;
553 	case IF_TYPE_VIRTUALLINK:
554 		/* nothing */
555 		break;
556 	case IF_TYPE_NBMA:
557 	case IF_TYPE_POINTOMULTIPOINT:
558 		log_debug("if_act_reset: type %s not supported, interface %s",
559 		    if_type_name(iface->type), iface->name);
560 		return (-1);
561 	default:
562 		fatalx("if_act_reset: unknown interface type");
563 	}
564 
565 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
566 		if (nbr_fsm(nbr, NBR_EVT_KILL_NBR)) {
567 			log_debug("if_act_reset: error killing neighbor %s",
568 			    inet_ntoa(nbr->id));
569 		}
570 	}
571 
572 	iface->dr = NULL;
573 	iface->bdr = NULL;
574 
575 	ls_ack_list_clr(iface);
576 	stop_ls_ack_tx_timer(iface);
577 	if_stop_hello_timer(iface);
578 	if_stop_wait_timer(iface);
579 
580 	/* send empty hello to tell everybody that we are going down */
581 	send_hello(iface);
582 
583 	return (0);
584 }
585 
586 struct ctl_iface *
587 if_to_ctl(struct iface *iface)
588 {
589 	static struct ctl_iface	 ictl;
590 	struct timeval		 tv, now, res;
591 	struct nbr		*nbr;
592 
593 	memcpy(ictl.name, iface->name, sizeof(ictl.name));
594 	memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr));
595 	ictl.rtr_id.s_addr = ospfe_router_id();
596 	memcpy(&ictl.area, &iface->area->id, sizeof(ictl.area));
597 	if (iface->dr) {
598 		memcpy(&ictl.dr_id, &iface->dr->id, sizeof(ictl.dr_id));
599 		memcpy(&ictl.dr_addr, &iface->dr->addr, sizeof(ictl.dr_addr));
600 	} else {
601 		bzero(&ictl.dr_id, sizeof(ictl.dr_id));
602 		bzero(&ictl.dr_addr, sizeof(ictl.dr_addr));
603 	}
604 	if (iface->bdr) {
605 		memcpy(&ictl.bdr_id, &iface->bdr->id, sizeof(ictl.bdr_id));
606 		memcpy(&ictl.bdr_addr, &iface->bdr->addr,
607 		    sizeof(ictl.bdr_addr));
608 	} else {
609 		bzero(&ictl.bdr_id, sizeof(ictl.bdr_id));
610 		bzero(&ictl.bdr_addr, sizeof(ictl.bdr_addr));
611 	}
612 	ictl.ifindex = iface->ifindex;
613 	ictl.state = iface->state;
614 	ictl.mtu = iface->mtu;
615 	ictl.nbr_cnt = 0;
616 	ictl.adj_cnt = 0;
617 	ictl.baudrate = iface->baudrate;
618 	ictl.dead_interval = iface->dead_interval;
619 	ictl.transmit_delay = iface->transmit_delay;
620 	ictl.hello_interval = iface->hello_interval;
621 	ictl.flags = iface->flags;
622 	ictl.metric = iface->metric;
623 	ictl.rxmt_interval = iface->rxmt_interval;
624 	ictl.type = iface->type;
625 	ictl.linkstate = iface->linkstate;
626 	ictl.mediatype = iface->media_type;
627 	ictl.priority = iface->priority;
628 	ictl.passive = iface->passive;
629 
630 	gettimeofday(&now, NULL);
631 	if (evtimer_pending(&iface->hello_timer, &tv)) {
632 		timersub(&tv, &now, &res);
633 		ictl.hello_timer = res.tv_sec;
634 	} else
635 		ictl.hello_timer = -1;
636 
637 	if (iface->state != IF_STA_DOWN) {
638 		ictl.uptime = now.tv_sec - iface->uptime;
639 	} else
640 		ictl.uptime = 0;
641 
642 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
643 		if (nbr == iface->self)
644 			continue;
645 		ictl.nbr_cnt++;
646 		if (nbr->state & NBR_STA_ADJFORM)
647 			ictl.adj_cnt++;
648 	}
649 
650 	return (&ictl);
651 }
652 
653 /* misc */
654 void
655 if_set_recvbuf(int fd)
656 {
657 	int	bsize;
658 
659 	bsize = 65535;
660 	while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize,
661 	    sizeof(bsize)) == -1)
662 		bsize /= 2;
663 }
664 
665 int
666 if_join_group(struct iface *iface, struct in6_addr *addr)
667 {
668 	struct ipv6_mreq	 mreq;
669 
670 	switch (iface->type) {
671 	case IF_TYPE_POINTOPOINT:
672 	case IF_TYPE_BROADCAST:
673 		log_debug("if_join_group: interface %s addr %s",
674 		    iface->name, log_in6addr(addr));
675 		mreq.ipv6mr_multiaddr = *addr;
676 		mreq.ipv6mr_interface = iface->ifindex;
677 
678 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
679 		    &mreq, sizeof(mreq)) < 0) {
680 			log_warn("if_join_group: error IPV6_JOIN_GROUP, "
681 			    "interface %s address %s", iface->name,
682 			    log_in6addr(addr));
683 			return (-1);
684 		}
685 		break;
686 	case IF_TYPE_POINTOMULTIPOINT:
687 	case IF_TYPE_VIRTUALLINK:
688 	case IF_TYPE_NBMA:
689 		log_debug("if_join_group: type %s not supported, interface %s",
690 		    if_type_name(iface->type), iface->name);
691 		return (-1);
692 	default:
693 		fatalx("if_join_group: unknown interface type");
694 	}
695 
696 	return (0);
697 }
698 
699 int
700 if_leave_group(struct iface *iface, struct in6_addr *addr)
701 {
702 	struct ipv6_mreq	 mreq;
703 
704 	switch (iface->type) {
705 	case IF_TYPE_POINTOPOINT:
706 	case IF_TYPE_BROADCAST:
707 		log_debug("if_leave_group: interface %s addr %s",
708 		    iface->name, log_in6addr(addr));
709 		mreq.ipv6mr_multiaddr = *addr;
710 		mreq.ipv6mr_interface = iface->ifindex;
711 
712 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
713 		    (void *)&mreq, sizeof(mreq)) < 0) {
714 			log_warn("if_leave_group: error IPV6_LEAVE_GROUP, "
715 			    "interface %s address %s", iface->name,
716 			    log_in6addr(addr));
717 			return (-1);
718 		}
719 		break;
720 	case IF_TYPE_POINTOMULTIPOINT:
721 	case IF_TYPE_VIRTUALLINK:
722 	case IF_TYPE_NBMA:
723 		log_debug("if_leave_group: type %s not supported, interface %s",
724 		    if_type_name(iface->type), iface->name);
725 		return (-1);
726 	default:
727 		fatalx("if_leave_group: unknown interface type");
728 	}
729 	return (0);
730 }
731 
732 int
733 if_set_mcast(struct iface *iface)
734 {
735 	switch (iface->type) {
736 	case IF_TYPE_POINTOPOINT:
737 	case IF_TYPE_BROADCAST:
738 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
739 		    &iface->ifindex, sizeof(iface->ifindex)) < 0) {
740 			log_debug("if_set_mcast: error setting "
741 			    "IP_MULTICAST_IF, interface %s", iface->name);
742 			return (-1);
743 		}
744 		break;
745 	case IF_TYPE_POINTOMULTIPOINT:
746 	case IF_TYPE_VIRTUALLINK:
747 	case IF_TYPE_NBMA:
748 		log_debug("if_set_mcast: type %s not supported, interface %s",
749 		    if_type_name(iface->type), iface->name);
750 		return (-1);
751 	default:
752 		fatalx("if_set_mcast: unknown interface type");
753 	}
754 
755 	return (0);
756 }
757 
758 int
759 if_set_mcast_loop(int fd)
760 {
761 	u_int	loop = 0;
762 
763 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
764 	    (u_int *)&loop, sizeof(loop)) < 0) {
765 		log_warn("if_set_mcast_loop: error setting "
766 		    "IPV6_MULTICAST_LOOP");
767 		return (-1);
768 	}
769 
770 	return (0);
771 }
772 
773 int
774 if_set_ipv6_pktinfo(int fd, int enable)
775 {
776 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &enable,
777 	    sizeof(enable)) < 0) {
778 		log_warn("if_set_ipv6_pktinfo: error setting IPV6_PKTINFO");
779 		return (-1);
780 	}
781 
782 	return (0);
783 }
784 
785 int
786 if_set_ipv6_checksum(int fd)
787 {
788 	int	offset = offsetof(struct ospf_hdr, chksum);
789 
790 	log_debug("if_set_ipv6_checksum setting cksum offset to %i", offset);
791 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset,
792 	     sizeof(offset)) < 0) {
793 		log_warn("if_set_ipv6_checksum: error setting IPV6_CHECKSUM");
794 		return (-1);
795 	}
796 	return (0);
797 }
798