xref: /openbsd-src/usr.sbin/ospf6d/interface.c (revision 77fbfa19a8cee18aa3acb3e4a9ddee17335f1f53)
1 /*	$OpenBSD: interface.c,v 1.27 2019/12/23 07:33:49 denis 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 #if 0
72 /* TODO virtual links */
73 static int vlink_cnt = 0;
74 #endif
75 
76 TAILQ_HEAD(, iface)	iflist;
77 
78 const char * const if_event_names[] = {
79 	"NOTHING",
80 	"UP",
81 	"WAITTIMER",
82 	"BACKUPSEEN",
83 	"NEIGHBORCHANGE",
84 	"LOOP",
85 	"UNLOOP",
86 	"DOWN"
87 };
88 
89 const char * const if_action_names[] = {
90 	"NOTHING",
91 	"START",
92 	"ELECT",
93 	"RESET"
94 };
95 
96 int
97 if_fsm(struct iface *iface, enum iface_event event)
98 {
99 	int	old_state;
100 	int	new_state = 0;
101 	int	i, ret = 0;
102 
103 	old_state = iface->state;
104 
105 	for (i = 0; iface_fsm[i].state != -1; i++)
106 		if ((iface_fsm[i].state & old_state) &&
107 		    (iface_fsm[i].event == event)) {
108 			new_state = iface_fsm[i].new_state;
109 			break;
110 		}
111 
112 	if (iface_fsm[i].state == -1) {
113 		/* event outside of the defined fsm, ignore it. */
114 		log_debug("if_fsm: interface %s, "
115 		    "event %s not expected in state %s", iface->name,
116 		    if_event_names[event], if_state_name(old_state));
117 		return (0);
118 	}
119 
120 	switch (iface_fsm[i].action) {
121 	case IF_ACT_STRT:
122 		ret = if_act_start(iface);
123 		break;
124 	case IF_ACT_ELECT:
125 		ret = if_act_elect(iface);
126 		break;
127 	case IF_ACT_RST:
128 		ret = if_act_reset(iface);
129 		break;
130 	case IF_ACT_NOTHING:
131 		/* do nothing */
132 		break;
133 	}
134 
135 	if (ret) {
136 		log_debug("if_fsm: error changing state for interface %s, "
137 		    "event %s, state %s", iface->name, if_event_names[event],
138 		    if_state_name(old_state));
139 		return (-1);
140 	}
141 
142 	if (new_state != 0)
143 		iface->state = new_state;
144 
145 	if (iface->state != old_state) {
146 		area_track(iface->area);
147 		orig_rtr_lsa(iface);
148 		orig_link_lsa(iface);
149 
150 		/* state change inform RDE */
151 		ospfe_imsg_compose_rde(IMSG_IFINFO, iface->self->peerid, 0,
152 		    &iface->state, sizeof(iface->state));
153 	}
154 
155 	if (old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT) &&
156 	    (iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0)
157 		ospfe_demote_iface(iface, 0);
158 	if ((old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0 &&
159 	    iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT))
160 		ospfe_demote_iface(iface, 1);
161 
162 	log_debug("if_fsm: event %s resulted in action %s and changing "
163 	    "state for interface %s from %s to %s",
164 	    if_event_names[event], if_action_names[iface_fsm[i].action],
165 	    iface->name, if_state_name(old_state), if_state_name(iface->state));
166 
167 	return (ret);
168 }
169 
170 int
171 if_init(void)
172 {
173 	TAILQ_INIT(&iflist);
174 
175 	return (fetchifs(0));
176 }
177 
178 /* XXX using a linked list should be OK for now */
179 struct iface *
180 if_find(unsigned int ifindex)
181 {
182 	struct iface	*iface;
183 
184 	TAILQ_FOREACH(iface, &iflist, list) {
185 		if (ifindex == iface->ifindex)
186 			return (iface);
187 	}
188 	return (NULL);
189 }
190 
191 struct iface *
192 if_findname(char *name)
193 {
194 	struct iface	*iface;
195 
196 	TAILQ_FOREACH(iface, &iflist, list) {
197 		if (!strcmp(name, iface->name))
198 			return (iface);
199 	}
200 	return (NULL);
201 }
202 
203 struct iface *
204 if_new(u_short ifindex, char *ifname)
205 {
206 	struct iface		*iface;
207 
208 	if ((iface = calloc(1, sizeof(*iface))) == NULL)
209 		err(1, "if_new: calloc");
210 
211 	iface->state = IF_STA_DOWN;
212 
213 	LIST_INIT(&iface->nbr_list);
214 	TAILQ_INIT(&iface->ifa_list);
215 	TAILQ_INIT(&iface->ls_ack_list);
216 	RB_INIT(&iface->lsa_tree);
217 
218 #if 0
219 	/* TODO */
220 	if (virtual) {
221 		iface->type = IF_TYPE_VIRTUALLINK;
222 		snprintf(iface->name, sizeof(iface->name), "vlink%d",
223 		    vlink_cnt++);
224 		iface->flags |= IFF_UP;
225 		iface->mtu = IP_MSS;
226 		return (iface);
227 	}
228 #endif
229 	strlcpy(iface->name, ifname, sizeof(iface->name));
230 	iface->ifindex = ifindex;
231 
232 	TAILQ_INSERT_TAIL(&iflist, iface, list);
233 
234 	return (iface);
235 }
236 
237 void
238 if_update(struct iface *iface, int mtu, int flags, u_int8_t type,
239     u_int8_t state, u_int64_t rate, u_int32_t rdomain)
240 {
241 	iface->mtu = mtu;
242 	iface->flags = flags;
243 	iface->if_type = type;
244 	iface->linkstate = state;
245 	iface->baudrate = rate;
246 	iface->rdomain = rdomain;
247 
248 	/* set type */
249 	if (flags & IFF_POINTOPOINT)
250 		iface->type = IF_TYPE_POINTOPOINT;
251 	if (flags & IFF_BROADCAST && flags & IFF_MULTICAST)
252 		iface->type = IF_TYPE_BROADCAST;
253 	if (flags & IFF_LOOPBACK) {
254 		iface->type = IF_TYPE_POINTOPOINT;
255 		iface->cflags |= F_IFACE_PASSIVE;
256 	}
257 }
258 
259 void
260 if_del(struct iface *iface)
261 {
262 	struct nbr	*nbr = NULL;
263 
264 	log_debug("if_del: interface %s", iface->name);
265 
266 	/* revert the demotion when the interface is deleted */
267 	if ((iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0)
268 		ospfe_demote_iface(iface, 1);
269 
270 	/* clear lists etc */
271 	while ((nbr = LIST_FIRST(&iface->nbr_list)) != NULL)
272 		nbr_del(nbr);
273 
274 	if (evtimer_pending(&iface->hello_timer, NULL))
275 		evtimer_del(&iface->hello_timer);
276 	if (evtimer_pending(&iface->wait_timer, NULL))
277 		evtimer_del(&iface->wait_timer);
278 	if (evtimer_pending(&iface->lsack_tx_timer, NULL))
279 		evtimer_del(&iface->lsack_tx_timer);
280 
281 	ls_ack_list_clr(iface);
282 	TAILQ_REMOVE(&iflist, iface, list);
283 	free(iface);
284 }
285 
286 void
287 if_start(struct ospfd_conf *xconf, struct iface *iface)
288 {
289 	/* init the dummy local neighbor */
290 	iface->self = nbr_new(ospfe_router_id(), iface, iface->ifindex, 1,
291 			NULL);
292 
293 	/* set event handlers for interface */
294 	evtimer_set(&iface->lsack_tx_timer, ls_ack_tx_timer, iface);
295 	evtimer_set(&iface->hello_timer, if_hello_timer, iface);
296 	evtimer_set(&iface->wait_timer, if_wait_timer, iface);
297 
298 	iface->fd = xconf->ospf_socket;
299 
300 	ospfe_demote_iface(iface, 0);
301 
302 	if (if_fsm(iface, IF_EVT_UP))
303 		log_debug("error starting interface %s", iface->name);
304 }
305 
306 /* timers */
307 /* ARGSUSED */
308 void
309 if_hello_timer(int fd, short event, void *arg)
310 {
311 	struct iface *iface = arg;
312 	struct timeval tv;
313 
314 	send_hello(iface);
315 
316 	/* reschedule hello_timer */
317 	timerclear(&tv);
318 	tv.tv_sec = iface->hello_interval;
319 	if (evtimer_add(&iface->hello_timer, &tv) == -1)
320 		fatal("if_hello_timer");
321 }
322 
323 void
324 if_start_hello_timer(struct iface *iface)
325 {
326 	struct timeval tv;
327 
328 	timerclear(&tv);
329 	if (evtimer_add(&iface->hello_timer, &tv) == -1)
330 		fatal("if_start_hello_timer");
331 }
332 
333 void
334 if_stop_hello_timer(struct iface *iface)
335 {
336 	if (evtimer_del(&iface->hello_timer) == -1)
337 		fatal("if_stop_hello_timer");
338 }
339 
340 /* ARGSUSED */
341 void
342 if_wait_timer(int fd, short event, void *arg)
343 {
344 	struct iface *iface = arg;
345 
346 	if_fsm(iface, IF_EVT_WTIMER);
347 }
348 
349 void
350 if_start_wait_timer(struct iface *iface)
351 {
352 	struct timeval	tv;
353 
354 	timerclear(&tv);
355 	tv.tv_sec = iface->dead_interval;
356 	if (evtimer_add(&iface->wait_timer, &tv) == -1)
357 		fatal("if_start_wait_timer");
358 }
359 
360 void
361 if_stop_wait_timer(struct iface *iface)
362 {
363 	if (evtimer_del(&iface->wait_timer) == -1)
364 		fatal("if_stop_wait_timer");
365 }
366 
367 /* actions */
368 int
369 if_act_start(struct iface *iface)
370 {
371 	struct in6_addr		 addr;
372 	struct timeval		 now;
373 
374 	if (!((iface->flags & IFF_UP) &&
375 	    LINK_STATE_IS_UP(iface->linkstate))) {
376 		log_debug("if_act_start: interface %s link down",
377 		    iface->name);
378 		return (0);
379 	}
380 
381 	if (iface->if_type == IFT_CARP &&
382 	    !(iface->cflags & F_IFACE_PASSIVE)) {
383 		/* force passive mode on carp interfaces */
384 		log_warnx("if_act_start: forcing interface %s to passive",
385 		    iface->name);
386 		iface->cflags |= F_IFACE_PASSIVE;
387 	}
388 
389 	gettimeofday(&now, NULL);
390 	iface->uptime = now.tv_sec;
391 
392 	/* loopback interfaces have a special state */
393 	if (iface->flags & IFF_LOOPBACK)
394 		iface->state = IF_STA_LOOPBACK;
395 
396 	if (iface->cflags & F_IFACE_PASSIVE) {
397 		/* for an update of stub network entries */
398 		orig_rtr_lsa(iface);
399 		return (0);
400 	}
401 
402 	switch (iface->type) {
403 	case IF_TYPE_POINTOPOINT:
404 		inet_pton(AF_INET6, AllSPFRouters, &addr);
405 
406 		if (if_join_group(iface, &addr))
407 			return (-1);
408 		iface->state = IF_STA_POINTTOPOINT;
409 		break;
410 	case IF_TYPE_VIRTUALLINK:
411 		iface->state = IF_STA_POINTTOPOINT;
412 		break;
413 	case IF_TYPE_POINTOMULTIPOINT:
414 	case IF_TYPE_NBMA:
415 		log_debug("if_act_start: type %s not supported, interface %s",
416 		    if_type_name(iface->type), iface->name);
417 		return (-1);
418 	case IF_TYPE_BROADCAST:
419 		inet_pton(AF_INET6, AllSPFRouters, &addr);
420 
421 		if (if_join_group(iface, &addr))
422 			return (-1);
423 		if (iface->priority == 0) {
424 			iface->state = IF_STA_DROTHER;
425 		} else {
426 			iface->state = IF_STA_WAITING;
427 			if_start_wait_timer(iface);
428 		}
429 		break;
430 	default:
431 		fatalx("if_act_start: unknown interface type");
432 	}
433 
434 	/* hello timer needs to be started in any case */
435 	if_start_hello_timer(iface);
436 	return (0);
437 }
438 
439 struct nbr *
440 if_elect(struct nbr *a, struct nbr *b)
441 {
442 	if (a->priority > b->priority)
443 		return (a);
444 	if (a->priority < b->priority)
445 		return (b);
446 	if (ntohl(a->id.s_addr) > ntohl(b->id.s_addr))
447 		return (a);
448 	return (b);
449 }
450 
451 int
452 if_act_elect(struct iface *iface)
453 {
454 	struct in6_addr	 addr;
455 	struct nbr	*nbr, *bdr = NULL, *dr = NULL;
456 	int		 round = 0;
457 	int		 changed = 0;
458 	int		 old_state;
459 	char		 b1[16], b2[16], b3[16], b4[16];
460 
461 start:
462 	/* elect backup designated router */
463 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
464 		if (nbr->priority == 0 || nbr == dr ||	/* not electable */
465 		    nbr->state & NBR_STA_PRELIM ||	/* not available */
466 		    nbr->dr.s_addr == nbr->id.s_addr)	/* don't elect DR */
467 			continue;
468 		if (bdr != NULL) {
469 			/*
470 			 * routers announcing themselves as BDR have higher
471 			 * precedence over those routers announcing a
472 			 * different BDR.
473 			 */
474 			if (nbr->bdr.s_addr == nbr->id.s_addr) {
475 				if (bdr->bdr.s_addr == bdr->id.s_addr)
476 					bdr = if_elect(bdr, nbr);
477 				else
478 					bdr = nbr;
479 			} else if (bdr->bdr.s_addr != bdr->id.s_addr)
480 					bdr = if_elect(bdr, nbr);
481 		} else
482 			bdr = nbr;
483 	}
484 
485 	/* elect designated router */
486 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
487 		if (nbr->priority == 0 || nbr->state & NBR_STA_PRELIM ||
488 		    (nbr != dr && nbr->dr.s_addr != nbr->id.s_addr))
489 			/* only DR may be elected check priority too */
490 			continue;
491 		if (dr == NULL)
492 			dr = nbr;
493 		else
494 			dr = if_elect(dr, nbr);
495 	}
496 
497 	if (dr == NULL) {
498 		/* no designate router found use backup DR */
499 		dr = bdr;
500 		bdr = NULL;
501 	}
502 
503 	/*
504 	 * if we are involved in the election (e.g. new DR or no
505 	 * longer BDR) redo the election
506 	 */
507 	if (round == 0 &&
508 	    ((iface->self == dr && iface->self != iface->dr) ||
509 	    (iface->self != dr && iface->self == iface->dr) ||
510 	    (iface->self == bdr && iface->self != iface->bdr) ||
511 	    (iface->self != bdr && iface->self == iface->bdr))) {
512 		/*
513 		 * Reset announced DR/BDR to calculated one, so
514 		 * that we may get elected in the second round.
515 		 * This is needed to drop from a DR to a BDR.
516 		 */
517 		iface->self->dr.s_addr = dr->id.s_addr;
518 		if (bdr)
519 			iface->self->bdr.s_addr = bdr->id.s_addr;
520 		round = 1;
521 		goto start;
522 	}
523 
524 	log_debug("if_act_elect: interface %s old dr %s new dr %s, "
525 	    "old bdr %s new bdr %s", iface->name,
526 	    iface->dr ? inet_ntop(AF_INET, &iface->dr->id, b1, sizeof(b1)) :
527 	    "none", dr ? inet_ntop(AF_INET, &dr->id, b2, sizeof(b2)) : "none",
528 	    iface->bdr ? inet_ntop(AF_INET, &iface->bdr->id, b3, sizeof(b3)) :
529 	    "none", bdr ? inet_ntop(AF_INET, &bdr->id, b4, sizeof(b4)) :
530 	    "none");
531 
532 	/*
533 	 * After the second round still DR or BDR change state to DR or BDR,
534 	 * etc.
535 	 */
536 	old_state = iface->state;
537 	if (dr == iface->self)
538 		iface->state = IF_STA_DR;
539 	else if (bdr == iface->self)
540 		iface->state = IF_STA_BACKUP;
541 	else
542 		iface->state = IF_STA_DROTHER;
543 
544 	/* TODO if iface is NBMA send all non eligible neighbors event Start */
545 
546 	/*
547 	 * if DR or BDR changed issue a AdjOK? event for all neighbors > 2-Way
548 	 */
549 	if (iface->dr != dr || iface->bdr != bdr)
550 		changed = 1;
551 
552 	iface->dr = dr;
553 	iface->bdr = bdr;
554 
555 	if (changed) {
556 		inet_pton(AF_INET6, AllDRouters, &addr);
557 		if (old_state & IF_STA_DRORBDR &&
558 		    (iface->state & IF_STA_DRORBDR) == 0) {
559 			if (if_leave_group(iface, &addr))
560 				return (-1);
561 		} else if ((old_state & IF_STA_DRORBDR) == 0 &&
562 		    iface->state & IF_STA_DRORBDR) {
563 			if (if_join_group(iface, &addr))
564 				return (-1);
565 		}
566 
567 		LIST_FOREACH(nbr, &iface->nbr_list, entry) {
568 			if (nbr->state & NBR_STA_BIDIR)
569 				nbr_fsm(nbr, NBR_EVT_ADJ_OK);
570 		}
571 
572 		orig_rtr_lsa(iface);
573 		if (iface->state & IF_STA_DR || old_state & IF_STA_DR)
574 			orig_net_lsa(iface);
575 	}
576 
577 	if_start_hello_timer(iface);
578 	return (0);
579 }
580 
581 int
582 if_act_reset(struct iface *iface)
583 {
584 	struct nbr		*nbr = NULL;
585 	struct in6_addr		 addr;
586 
587 	if (iface->cflags & F_IFACE_PASSIVE) {
588 		/* for an update of stub network entries */
589 		orig_rtr_lsa(iface);
590 		return (0);
591 	}
592 
593 	switch (iface->type) {
594 	case IF_TYPE_POINTOPOINT:
595 	case IF_TYPE_BROADCAST:
596 		inet_pton(AF_INET6, AllSPFRouters, &addr);
597 		if (if_leave_group(iface, &addr)) {
598 			log_warnx("if_act_reset: error leaving group %s, "
599 			    "interface %s", log_in6addr(&addr), iface->name);
600 		}
601 		if (iface->state & IF_STA_DRORBDR) {
602 			inet_pton(AF_INET6, AllDRouters, &addr);
603 			if (if_leave_group(iface, &addr)) {
604 				log_warnx("if_act_reset: "
605 				    "error leaving group %s, interface %s",
606 				    log_in6addr(&addr), iface->name);
607 			}
608 		}
609 		break;
610 	case IF_TYPE_VIRTUALLINK:
611 		/* nothing */
612 		break;
613 	case IF_TYPE_NBMA:
614 	case IF_TYPE_POINTOMULTIPOINT:
615 		log_debug("if_act_reset: type %s not supported, interface %s",
616 		    if_type_name(iface->type), iface->name);
617 		return (-1);
618 	default:
619 		fatalx("if_act_reset: unknown interface type");
620 	}
621 
622 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
623 		if (nbr_fsm(nbr, NBR_EVT_KILL_NBR)) {
624 			log_debug("if_act_reset: error killing neighbor %s",
625 			    inet_ntoa(nbr->id));
626 		}
627 	}
628 
629 	iface->dr = NULL;
630 	iface->bdr = NULL;
631 
632 	ls_ack_list_clr(iface);
633 	stop_ls_ack_tx_timer(iface);
634 	if_stop_hello_timer(iface);
635 	if_stop_wait_timer(iface);
636 
637 	/* send empty hello to tell everybody that we are going down */
638 	send_hello(iface);
639 
640 	return (0);
641 }
642 
643 struct ctl_iface *
644 if_to_ctl(struct iface *iface)
645 {
646 	static struct ctl_iface	 ictl;
647 	struct timeval		 tv, now, res;
648 	struct nbr		*nbr;
649 
650 	memcpy(ictl.name, iface->name, sizeof(ictl.name));
651 	memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr));
652 	ictl.rtr_id.s_addr = ospfe_router_id();
653 	memcpy(&ictl.area, &iface->area->id, sizeof(ictl.area));
654 	if (iface->dr) {
655 		memcpy(&ictl.dr_id, &iface->dr->id, sizeof(ictl.dr_id));
656 		memcpy(&ictl.dr_addr, &iface->dr->addr, sizeof(ictl.dr_addr));
657 	} else {
658 		bzero(&ictl.dr_id, sizeof(ictl.dr_id));
659 		bzero(&ictl.dr_addr, sizeof(ictl.dr_addr));
660 	}
661 	if (iface->bdr) {
662 		memcpy(&ictl.bdr_id, &iface->bdr->id, sizeof(ictl.bdr_id));
663 		memcpy(&ictl.bdr_addr, &iface->bdr->addr,
664 		    sizeof(ictl.bdr_addr));
665 	} else {
666 		bzero(&ictl.bdr_id, sizeof(ictl.bdr_id));
667 		bzero(&ictl.bdr_addr, sizeof(ictl.bdr_addr));
668 	}
669 	ictl.ifindex = iface->ifindex;
670 	ictl.state = iface->state;
671 	ictl.mtu = iface->mtu;
672 	ictl.nbr_cnt = 0;
673 	ictl.adj_cnt = 0;
674 	ictl.baudrate = iface->baudrate;
675 	ictl.dead_interval = iface->dead_interval;
676 	ictl.transmit_delay = iface->transmit_delay;
677 	ictl.hello_interval = iface->hello_interval;
678 	ictl.flags = iface->flags;
679 	ictl.metric = iface->metric;
680 	ictl.rxmt_interval = iface->rxmt_interval;
681 	ictl.type = iface->type;
682 	ictl.linkstate = iface->linkstate;
683 	ictl.if_type = iface->if_type;
684 	ictl.priority = iface->priority;
685 	ictl.passive = (iface->cflags & F_IFACE_PASSIVE) == F_IFACE_PASSIVE;
686 
687 	gettimeofday(&now, NULL);
688 	if (evtimer_pending(&iface->hello_timer, &tv)) {
689 		timersub(&tv, &now, &res);
690 		ictl.hello_timer = res.tv_sec;
691 	} else
692 		ictl.hello_timer = -1;
693 
694 	if (iface->state != IF_STA_DOWN) {
695 		ictl.uptime = now.tv_sec - iface->uptime;
696 	} else
697 		ictl.uptime = 0;
698 
699 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
700 		if (nbr == iface->self)
701 			continue;
702 		ictl.nbr_cnt++;
703 		if (nbr->state & NBR_STA_ADJFORM)
704 			ictl.adj_cnt++;
705 	}
706 
707 	return (&ictl);
708 }
709 
710 /* misc */
711 void
712 if_set_sockbuf(int fd)
713 {
714 	int	bsize;
715 
716 	bsize = 256 * 1024;
717 	while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize,
718 	    sizeof(bsize)) == -1)
719 		bsize /= 2;
720 
721 	if (bsize != 256 * 1024)
722 		log_warnx("if_set_sockbuf: recvbuf size only %d", bsize);
723 
724 	bsize = 64 * 1024;
725 	while (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bsize,
726 	    sizeof(bsize)) == -1)
727 		bsize /= 2;
728 
729 	if (bsize != 64 * 1024)
730 		log_warnx("if_set_sockbuf: sendbuf size only %d", bsize);
731 }
732 
733 int
734 if_join_group(struct iface *iface, struct in6_addr *addr)
735 {
736 	struct ipv6_mreq	 mreq;
737 
738 	switch (iface->type) {
739 	case IF_TYPE_POINTOPOINT:
740 	case IF_TYPE_BROADCAST:
741 		log_debug("if_join_group: interface %s addr %s",
742 		    iface->name, log_in6addr(addr));
743 		mreq.ipv6mr_multiaddr = *addr;
744 		mreq.ipv6mr_interface = iface->ifindex;
745 
746 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
747 		    &mreq, sizeof(mreq)) == -1) {
748 			log_warn("if_join_group: error IPV6_JOIN_GROUP, "
749 			    "interface %s address %s", iface->name,
750 			    log_in6addr(addr));
751 			return (-1);
752 		}
753 		break;
754 	case IF_TYPE_POINTOMULTIPOINT:
755 	case IF_TYPE_VIRTUALLINK:
756 	case IF_TYPE_NBMA:
757 		log_debug("if_join_group: type %s not supported, interface %s",
758 		    if_type_name(iface->type), iface->name);
759 		return (-1);
760 	default:
761 		fatalx("if_join_group: unknown interface type");
762 	}
763 
764 	return (0);
765 }
766 
767 int
768 if_leave_group(struct iface *iface, struct in6_addr *addr)
769 {
770 	struct ipv6_mreq	 mreq;
771 
772 	switch (iface->type) {
773 	case IF_TYPE_POINTOPOINT:
774 	case IF_TYPE_BROADCAST:
775 		log_debug("if_leave_group: interface %s addr %s",
776 		    iface->name, log_in6addr(addr));
777 		mreq.ipv6mr_multiaddr = *addr;
778 		mreq.ipv6mr_interface = iface->ifindex;
779 
780 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
781 		    (void *)&mreq, sizeof(mreq)) == -1) {
782 			log_warn("if_leave_group: error IPV6_LEAVE_GROUP, "
783 			    "interface %s address %s", iface->name,
784 			    log_in6addr(addr));
785 			return (-1);
786 		}
787 		break;
788 	case IF_TYPE_POINTOMULTIPOINT:
789 	case IF_TYPE_VIRTUALLINK:
790 	case IF_TYPE_NBMA:
791 		log_debug("if_leave_group: type %s not supported, interface %s",
792 		    if_type_name(iface->type), iface->name);
793 		return (-1);
794 	default:
795 		fatalx("if_leave_group: unknown interface type");
796 	}
797 	return (0);
798 }
799 
800 int
801 if_set_mcast(struct iface *iface)
802 {
803 	switch (iface->type) {
804 	case IF_TYPE_POINTOPOINT:
805 	case IF_TYPE_BROADCAST:
806 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
807 		    &iface->ifindex, sizeof(iface->ifindex)) == -1) {
808 			log_debug("if_set_mcast: error setting "
809 			    "IP_MULTICAST_IF, interface %s", iface->name);
810 			return (-1);
811 		}
812 		break;
813 	case IF_TYPE_POINTOMULTIPOINT:
814 	case IF_TYPE_VIRTUALLINK:
815 	case IF_TYPE_NBMA:
816 		log_debug("if_set_mcast: type %s not supported, interface %s",
817 		    if_type_name(iface->type), iface->name);
818 		return (-1);
819 	default:
820 		fatalx("if_set_mcast: unknown interface type");
821 	}
822 
823 	return (0);
824 }
825 
826 int
827 if_set_mcast_loop(int fd)
828 {
829 	u_int	loop = 0;
830 
831 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
832 	    (u_int *)&loop, sizeof(loop)) == -1) {
833 		log_warn("if_set_mcast_loop: error setting "
834 		    "IPV6_MULTICAST_LOOP");
835 		return (-1);
836 	}
837 
838 	return (0);
839 }
840 
841 int
842 if_set_ipv6_pktinfo(int fd, int enable)
843 {
844 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &enable,
845 	    sizeof(enable)) == -1) {
846 		log_warn("if_set_ipv6_pktinfo: error setting IPV6_PKTINFO");
847 		return (-1);
848 	}
849 
850 	return (0);
851 }
852 
853 int
854 if_set_ipv6_checksum(int fd)
855 {
856 	int	offset = offsetof(struct ospf_hdr, chksum);
857 
858 	log_debug("if_set_ipv6_checksum setting cksum offset to %d", offset);
859 	if (setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset,
860 	     sizeof(offset)) == -1) {
861 		log_warn("if_set_ipv6_checksum: error setting IPV6_CHECKSUM");
862 		return (-1);
863 	}
864 	return (0);
865 }
866