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