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