GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ospf6d/kroute.c Lines: 0 751 0.0 %
Date: 2017-11-07 Branches: 0 791 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: kroute.c,v 1.52 2017/06/19 19:55:57 friehm Exp $ */
2
3
/*
4
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5
 * Copyright (c) 2003, 2004 Henning Brauer <henning@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/socket.h>
22
#include <sys/sysctl.h>
23
#include <sys/tree.h>
24
#include <sys/uio.h>
25
#include <netinet/in.h>
26
#include <arpa/inet.h>
27
#include <net/if.h>
28
#include <net/if_dl.h>
29
#include <net/if_types.h>
30
#include <net/route.h>
31
#include <err.h>
32
#include <errno.h>
33
#include <fcntl.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <unistd.h>
38
#include <limits.h>
39
40
#include "ospf6d.h"
41
#include "ospfe.h"
42
#include "log.h"
43
44
struct {
45
	u_int32_t		rtseq;
46
	pid_t			pid;
47
	int			fib_sync;
48
	int			fd;
49
	struct event		ev;
50
} kr_state;
51
52
struct kroute_node {
53
	RB_ENTRY(kroute_node)	 entry;
54
	struct kroute_node	*next;
55
	struct kroute		 r;
56
};
57
58
void	kr_redist_remove(struct kroute_node *, struct kroute_node *);
59
int	kr_redist_eval(struct kroute *, struct kroute *);
60
void	kr_redistribute(struct kroute_node *);
61
int	kroute_compare(struct kroute_node *, struct kroute_node *);
62
int	kr_change_fib(struct kroute_node *, struct kroute *, int, int);
63
int	kr_delete_fib(struct kroute_node *);
64
65
struct kroute_node	*kroute_find(const struct in6_addr *, u_int8_t,
66
			    u_int8_t);
67
struct kroute_node	*kroute_matchgw(struct kroute_node *,
68
			    struct in6_addr *, unsigned int);
69
int			 kroute_insert(struct kroute_node *);
70
int			 kroute_remove(struct kroute_node *);
71
void			 kroute_clear(void);
72
73
struct iface		*kif_update(u_short, int, struct if_data *,
74
			   struct sockaddr_dl *);
75
int			 kif_validate(u_short);
76
77
struct kroute_node	*kroute_match(struct in6_addr *);
78
79
int		protect_lo(void);
80
void		get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
81
void		if_change(u_short, int, struct if_data *);
82
void		if_newaddr(u_short, struct sockaddr_in6 *,
83
		    struct sockaddr_in6 *, struct sockaddr_in6 *);
84
void		if_deladdr(u_short, struct sockaddr_in6 *,
85
		    struct sockaddr_in6 *, struct sockaddr_in6 *);
86
void		if_announce(void *);
87
88
int		send_rtmsg(int, int, struct kroute *);
89
int		dispatch_rtmsg(void);
90
int		fetchtable(void);
91
92
RB_HEAD(kroute_tree, kroute_node)	krt;
93
RB_PROTOTYPE(kroute_tree, kroute_node, entry, kroute_compare)
94
RB_GENERATE(kroute_tree, kroute_node, entry, kroute_compare)
95
96
int
97
kr_init(int fs)
98
{
99
	int		opt = 0, rcvbuf, default_rcvbuf;
100
	socklen_t	optlen;
101
102
	kr_state.fib_sync = fs;
103
104
	if ((kr_state.fd = socket(AF_ROUTE,
105
	    SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1) {
106
		log_warn("kr_init: socket");
107
		return (-1);
108
	}
109
110
	/* not interested in my own messages */
111
	if (setsockopt(kr_state.fd, SOL_SOCKET, SO_USELOOPBACK,
112
	    &opt, sizeof(opt)) == -1)
113
		log_warn("kr_init: setsockopt");	/* not fatal */
114
115
	/* grow receive buffer, don't wanna miss messages */
116
	optlen = sizeof(default_rcvbuf);
117
	if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF,
118
	    &default_rcvbuf, &optlen) == -1)
119
		log_warn("kr_init getsockopt SOL_SOCKET SO_RCVBUF");
120
	else
121
		for (rcvbuf = MAX_RTSOCK_BUF;
122
		    rcvbuf > default_rcvbuf &&
123
		    setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF,
124
		    &rcvbuf, sizeof(rcvbuf)) == -1 && errno == ENOBUFS;
125
		    rcvbuf /= 2)
126
			;	/* nothing */
127
128
	kr_state.pid = getpid();
129
	kr_state.rtseq = 1;
130
131
	RB_INIT(&krt);
132
133
	if (fetchtable() == -1)
134
		return (-1);
135
136
	if (protect_lo() == -1)
137
		return (-1);
138
139
	event_set(&kr_state.ev, kr_state.fd, EV_READ | EV_PERSIST,
140
	    kr_dispatch_msg, NULL);
141
	event_add(&kr_state.ev, NULL);
142
143
	return (0);
144
}
145
146
int
147
kr_change_fib(struct kroute_node *kr, struct kroute *kroute, int krcount,
148
    int action)
149
{
150
	int			 i;
151
	struct kroute_node	*kn, *nkn;
152
153
	if (action == RTM_ADD) {
154
		/*
155
		 * First remove all stale multipath routes.
156
		 * This step must be skipped when the action is RTM_CHANGE
157
		 * because it is already a single path route that will be
158
		 * changed.
159
		 */
160
		for (kn = kr; kn != NULL; kn = nkn) {
161
			for (i = 0; i < krcount; i++) {
162
				if (kn->r.scope == kroute[i].scope &&
163
				    IN6_ARE_ADDR_EQUAL(&kn->r.nexthop,
164
				    &kroute[i].nexthop))
165
					break;
166
			}
167
			nkn = kn->next;
168
			if (i == krcount) {
169
				/* stale route */
170
				if (kr_delete_fib(kn) == -1)
171
					log_warnx("kr_delete_fib failed");
172
				/*
173
				 * if head element was removed we need to adjust
174
				 * the head
175
				 */
176
				if (kr == kn)
177
					kr = nkn;
178
			}
179
		}
180
	}
181
182
	/*
183
	 * now add or change the route
184
	 */
185
	for (i = 0; i < krcount; i++) {
186
		/* nexthop ::1 -> ignore silently */
187
		if (IN6_IS_ADDR_LOOPBACK(&kroute[i].nexthop))
188
			continue;
189
190
		if (action == RTM_ADD && kr) {
191
			for (kn = kr; kn != NULL; kn = kn->next) {
192
				if (kn->r.scope == kroute[i].scope &&
193
				    IN6_ARE_ADDR_EQUAL(&kn->r.nexthop,
194
				    &kroute[i].nexthop))
195
					break;
196
			}
197
198
			if (kn != NULL)
199
				/* nexthop already present, skip it */
200
				continue;
201
		} else
202
			/* modify first entry */
203
			kn = kr;
204
205
		/* send update */
206
		if (send_rtmsg(kr_state.fd, action, &kroute[i]) == -1)
207
			return (-1);
208
209
		/* create new entry unless we are changing the first entry */
210
		if (action == RTM_ADD)
211
			if ((kn = calloc(1, sizeof(*kn))) == NULL)
212
				fatal(NULL);
213
214
		kn->r.prefix = kroute[i].prefix;
215
		kn->r.prefixlen = kroute[i].prefixlen;
216
		kn->r.nexthop = kroute[i].nexthop;
217
		kn->r.scope = kroute[i].scope;
218
		kn->r.flags = kroute[i].flags | F_OSPFD_INSERTED;
219
		kn->r.priority = RTP_OSPF;
220
		kn->r.ext_tag = kroute[i].ext_tag;
221
		rtlabel_unref(kn->r.rtlabel);	/* for RTM_CHANGE */
222
		kn->r.rtlabel = kroute[i].rtlabel;
223
224
		if (action == RTM_ADD)
225
			if (kroute_insert(kn) == -1) {
226
				log_debug("kr_update_fib: cannot insert %s",
227
				    log_in6addr(&kn->r.nexthop));
228
				free(kn);
229
			}
230
		action = RTM_ADD;
231
	}
232
	return  (0);
233
}
234
235
int
236
kr_change(struct kroute *kroute, int krcount)
237
{
238
	struct kroute_node	*kr;
239
	int			 action = RTM_ADD;
240
241
	kroute->rtlabel = rtlabel_tag2id(kroute->ext_tag);
242
243
	kr = kroute_find(&kroute->prefix, kroute->prefixlen, RTP_OSPF);
244
	if (kr != NULL && kr->next == NULL && krcount == 1)
245
		/* single path OSPF route */
246
		action = RTM_CHANGE;
247
248
	return (kr_change_fib(kr, kroute, krcount, action));
249
}
250
251
int
252
kr_delete_fib(struct kroute_node *kr)
253
{
254
	if (kr->r.priority != RTP_OSPF)
255
		log_warn("kr_delete_fib: %s/%d has wrong priority %d",
256
		    log_in6addr(&kr->r.prefix), kr->r.prefixlen,
257
		    kr->r.priority);
258
259
	if (send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r) == -1)
260
		return (-1);
261
262
	if (kroute_remove(kr) == -1)
263
		return (-1);
264
265
	return (0);
266
}
267
268
int
269
kr_delete(struct kroute *kroute)
270
{
271
	struct kroute_node	*kr, *nkr;
272
273
	if ((kr = kroute_find(&kroute->prefix, kroute->prefixlen,
274
	    RTP_OSPF)) == NULL)
275
		return (0);
276
277
	while (kr != NULL) {
278
		nkr = kr->next;
279
		if (kr_delete_fib(kr) == -1)
280
			return (-1);
281
		kr = nkr;
282
	}
283
284
	return (0);
285
}
286
287
void
288
kr_shutdown(void)
289
{
290
	kr_fib_decouple();
291
	kroute_clear();
292
}
293
294
void
295
kr_fib_couple(void)
296
{
297
	struct kroute_node	*kr;
298
	struct kroute_node	*kn;
299
300
	if (kr_state.fib_sync == 1)	/* already coupled */
301
		return;
302
303
	kr_state.fib_sync = 1;
304
305
	RB_FOREACH(kr, kroute_tree, &krt)
306
		if (kr->r.priority == RTP_OSPF)
307
			for (kn = kr; kn != NULL; kn = kn->next)
308
				send_rtmsg(kr_state.fd, RTM_ADD, &kn->r);
309
310
	log_info("kernel routing table coupled");
311
}
312
313
void
314
kr_fib_decouple(void)
315
{
316
	struct kroute_node	*kr;
317
	struct kroute_node	*kn;
318
319
	if (kr_state.fib_sync == 0)	/* already decoupled */
320
		return;
321
322
	RB_FOREACH(kr, kroute_tree, &krt)
323
		if (kr->r.priority == RTP_OSPF)
324
			for (kn = kr; kn != NULL; kn = kn->next)
325
				send_rtmsg(kr_state.fd, RTM_DELETE, &kn->r);
326
327
	kr_state.fib_sync = 0;
328
329
	log_info("kernel routing table decoupled");
330
}
331
332
/* ARGSUSED */
333
void
334
kr_dispatch_msg(int fd, short event, void *bula)
335
{
336
	/* XXX this is stupid */
337
	dispatch_rtmsg();
338
}
339
340
void
341
kr_show_route(struct imsg *imsg)
342
{
343
	struct kroute_node	*kr;
344
	struct kroute_node	*kn;
345
	int			 flags;
346
	struct in6_addr		 addr;
347
348
	switch (imsg->hdr.type) {
349
	case IMSG_CTL_KROUTE:
350
		if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(flags)) {
351
			log_warnx("kr_show_route: wrong imsg len");
352
			return;
353
		}
354
		memcpy(&flags, imsg->data, sizeof(flags));
355
		RB_FOREACH(kr, kroute_tree, &krt)
356
			if (!flags || kr->r.flags & flags) {
357
				kn = kr;
358
				do {
359
					main_imsg_compose_ospfe(IMSG_CTL_KROUTE,
360
					    imsg->hdr.pid,
361
					    &kn->r, sizeof(kn->r));
362
				} while ((kn = kn->next) != NULL);
363
			}
364
		break;
365
	case IMSG_CTL_KROUTE_ADDR:
366
		if (imsg->hdr.len != IMSG_HEADER_SIZE +
367
		    sizeof(struct in6_addr)) {
368
			log_warnx("kr_show_route: wrong imsg len");
369
			return;
370
		}
371
		memcpy(&addr, imsg->data, sizeof(addr));
372
		kr = NULL;
373
		kr = kroute_match(&addr);
374
		if (kr != NULL)
375
			main_imsg_compose_ospfe(IMSG_CTL_KROUTE, imsg->hdr.pid,
376
			    &kr->r, sizeof(kr->r));
377
		break;
378
	default:
379
		log_debug("kr_show_route: error handling imsg");
380
		break;
381
	}
382
383
	main_imsg_compose_ospfe(IMSG_CTL_END, imsg->hdr.pid, NULL, 0);
384
}
385
386
void
387
kr_redist_remove(struct kroute_node *kh, struct kroute_node *kn)
388
{
389
	struct kroute	 *kr;
390
391
	/* was the route redistributed? */
392
	if ((kn->r.flags & F_REDISTRIBUTED) == 0)
393
		return;
394
395
	/* remove redistributed flag */
396
	kn->r.flags &= ~F_REDISTRIBUTED;
397
	kr = &kn->r;
398
399
	/* probably inform the RDE (check if no other path is redistributed) */
400
	for (kn = kh; kn; kn = kn->next)
401
		if (kn->r.flags & F_REDISTRIBUTED)
402
			break;
403
404
	if (kn == NULL)
405
		main_imsg_compose_rde(IMSG_NETWORK_DEL, 0, kr,
406
		    sizeof(struct kroute));
407
}
408
409
int
410
kr_redist_eval(struct kroute *kr, struct kroute *new_kr)
411
{
412
	u_int32_t	 metric = 0;
413
414
	/* Only non-ospfd routes are considered for redistribution. */
415
	if (!(kr->flags & F_KERNEL))
416
		goto dont_redistribute;
417
418
	/* Dynamic routes are not redistributable. */
419
	if (kr->flags & F_DYNAMIC)
420
		goto dont_redistribute;
421
422
	/* interface is not up and running so don't announce */
423
	if (kr->flags & F_DOWN)
424
		goto dont_redistribute;
425
426
	/*
427
	 * We consider loopback, multicast, link- and site-local,
428
	 * IPv4 mapped and IPv4 compatible addresses as not redistributable.
429
	 */
430
	if (IN6_IS_ADDR_LOOPBACK(&kr->prefix) ||
431
	    IN6_IS_ADDR_MULTICAST(&kr->prefix) ||
432
	    IN6_IS_ADDR_LINKLOCAL(&kr->prefix) ||
433
	    IN6_IS_ADDR_SITELOCAL(&kr->prefix) ||
434
	    IN6_IS_ADDR_V4MAPPED(&kr->prefix) ||
435
	    IN6_IS_ADDR_V4COMPAT(&kr->prefix))
436
		goto dont_redistribute;
437
	/*
438
	 * Consider networks with nexthop loopback as not redistributable
439
	 * unless it is a reject or blackhole route.
440
	 */
441
	if (IN6_IS_ADDR_LOOPBACK(&kr->nexthop) &&
442
	    !(kr->flags & (F_BLACKHOLE|F_REJECT)))
443
		goto dont_redistribute;
444
445
	/* Should we redistribute this route? */
446
	if (!ospf_redistribute(kr, &metric))
447
		goto dont_redistribute;
448
449
	/* prefix should be redistributed */
450
	kr->flags |= F_REDISTRIBUTED;
451
	/*
452
	 * only one of all multipath routes can be redistributed so
453
	 * redistribute the best one.
454
	 */
455
	if (new_kr->metric > metric) {
456
		*new_kr = *kr;
457
		new_kr->metric = metric;
458
	}
459
460
	return (1);
461
462
dont_redistribute:
463
	/* was the route redistributed? */
464
	if ((kr->flags & F_REDISTRIBUTED) == 0)
465
		return (0);
466
467
	kr->flags &= ~F_REDISTRIBUTED;
468
	return (1);
469
}
470
471
void
472
kr_redistribute(struct kroute_node *kh)
473
{
474
	struct kroute_node	*kn;
475
	struct kroute		 kr;
476
	int			 redistribute = 0;
477
478
	/* only the highest prio route can be redistributed */
479
	if (kroute_find(&kh->r.prefix, kh->r.prefixlen, RTP_ANY) != kh)
480
		return;
481
482
	bzero(&kr, sizeof(kr));
483
	kr.metric = UINT_MAX;
484
	for (kn = kh; kn; kn = kn->next)
485
		if (kr_redist_eval(&kn->r, &kr))
486
			redistribute = 1;
487
488
	if (!redistribute)
489
		return;
490
491
	if (kr.flags & F_REDISTRIBUTED) {
492
		main_imsg_compose_rde(IMSG_NETWORK_ADD, 0, &kr,
493
		    sizeof(struct kroute));
494
	} else {
495
		kr = kh->r;
496
		main_imsg_compose_rde(IMSG_NETWORK_DEL, 0, &kr,
497
		    sizeof(struct kroute));
498
	}
499
}
500
501
void
502
kr_reload(void)
503
{
504
	struct kroute_node	*kr, *kn;
505
	u_int32_t		 dummy;
506
	int			 r;
507
508
	RB_FOREACH(kr, kroute_tree, &krt) {
509
		for (kn = kr; kn; kn = kn->next) {
510
			r = ospf_redistribute(&kn->r, &dummy);
511
			/*
512
			 * if it is redistributed, redistribute again metric
513
			 * may have changed.
514
			 */
515
			if ((kn->r.flags & F_REDISTRIBUTED && !r) || r)
516
				break;
517
		}
518
		if (kn) {
519
			/*
520
			 * kr_redistribute copes with removes and RDE with
521
			 * duplicates
522
			 */
523
			kr_redistribute(kr);
524
		}
525
	}
526
}
527
528
/* rb-tree compare */
529
int
530
kroute_compare(struct kroute_node *a, struct kroute_node *b)
531
{
532
	int	i;
533
534
	/* XXX maybe switch a & b */
535
	i = memcmp(&a->r.prefix, &b->r.prefix, sizeof(a->r.prefix));
536
	if (i)
537
		return (i);
538
	if (a->r.prefixlen < b->r.prefixlen)
539
		return (-1);
540
	if (a->r.prefixlen > b->r.prefixlen)
541
		return (1);
542
543
	/* if the priority is RTP_ANY finish on the first address hit */
544
	if (a->r.priority == RTP_ANY || b->r.priority == RTP_ANY)
545
		return (0);
546
	if (a->r.priority < b->r.priority)
547
		return (-1);
548
	if (a->r.priority > b->r.priority)
549
		return (1);
550
	return (0);
551
}
552
553
/* tree management */
554
struct kroute_node *
555
kroute_find(const struct in6_addr *prefix, u_int8_t prefixlen, u_int8_t prio)
556
{
557
	struct kroute_node	s;
558
	struct kroute_node	*kn, *tmp;
559
560
	s.r.prefix = *prefix;
561
	s.r.prefixlen = prefixlen;
562
	s.r.priority = prio;
563
564
	kn = RB_FIND(kroute_tree, &krt, &s);
565
	if (kn && prio == RTP_ANY) {
566
		tmp = RB_PREV(kroute_tree, &krt, kn);
567
		while (tmp) {
568
			if (kroute_compare(&s, tmp) == 0)
569
				kn = tmp;
570
			else
571
				break;
572
			tmp = RB_PREV(kroute_tree, &krt, kn);
573
		}
574
	}
575
	return (kn);
576
}
577
578
struct kroute_node *
579
kroute_matchgw(struct kroute_node *kr, struct in6_addr *nh, unsigned int scope)
580
{
581
	while (kr) {
582
		if (scope == kr->r.scope &&
583
		    IN6_ARE_ADDR_EQUAL(&kr->r.nexthop, nh))
584
			return (kr);
585
		kr = kr->next;
586
	}
587
588
	return (NULL);
589
}
590
591
int
592
kroute_insert(struct kroute_node *kr)
593
{
594
	struct kroute_node	*krm, *krh;
595
596
	if ((krh = RB_INSERT(kroute_tree, &krt, kr)) != NULL) {
597
		/*
598
		 * Multipath route, add at end of list.
599
		 */
600
		krm = krh;
601
		while (krm->next != NULL)
602
			krm = krm->next;
603
		krm->next = kr;
604
		kr->next = NULL; /* to be sure */
605
	} else
606
		krh = kr;
607
608
	if (!(kr->r.flags & F_KERNEL)) {
609
		/* don't validate or redistribute ospf route */
610
		kr->r.flags &= ~F_DOWN;
611
		return (0);
612
	}
613
614
	if (kif_validate(kr->r.ifindex))
615
		kr->r.flags &= ~F_DOWN;
616
	else
617
		kr->r.flags |= F_DOWN;
618
619
	kr_redistribute(krh);
620
	return (0);
621
}
622
623
int
624
kroute_remove(struct kroute_node *kr)
625
{
626
	struct kroute_node	*krm;
627
628
	if ((krm = RB_FIND(kroute_tree, &krt, kr)) == NULL) {
629
		log_warnx("kroute_remove failed to find %s/%u",
630
		    log_in6addr(&kr->r.prefix), kr->r.prefixlen);
631
		return (-1);
632
	}
633
634
	if (krm == kr) {
635
		/* head element */
636
		if (RB_REMOVE(kroute_tree, &krt, kr) == NULL) {
637
			log_warnx("kroute_remove failed for %s/%u",
638
			    log_in6addr(&kr->r.prefix), kr->r.prefixlen);
639
			return (-1);
640
		}
641
		if (kr->next != NULL) {
642
			if (RB_INSERT(kroute_tree, &krt, kr->next) != NULL) {
643
				log_warnx("kroute_remove failed to add %s/%u",
644
				    log_in6addr(&kr->r.prefix),
645
				    kr->r.prefixlen);
646
				return (-1);
647
			}
648
		}
649
	} else {
650
		/* somewhere in the list */
651
		while (krm->next != kr && krm->next != NULL)
652
			krm = krm->next;
653
		if (krm->next == NULL) {
654
			log_warnx("kroute_remove multipath list corrupted "
655
			    "for %s/%u", log_in6addr(&kr->r.prefix),
656
			    kr->r.prefixlen);
657
			return (-1);
658
		}
659
		krm->next = kr->next;
660
	}
661
662
	kr_redist_remove(krm, kr);
663
	rtlabel_unref(kr->r.rtlabel);
664
665
	free(kr);
666
	return (0);
667
}
668
669
void
670
kroute_clear(void)
671
{
672
	struct kroute_node	*kr;
673
674
	while ((kr = RB_MIN(kroute_tree, &krt)) != NULL)
675
		kroute_remove(kr);
676
}
677
678
struct iface *
679
kif_update(u_short ifindex, int flags, struct if_data *ifd,
680
    struct sockaddr_dl *sdl)
681
{
682
	struct iface	*iface;
683
	char		 ifname[IF_NAMESIZE];
684
685
	if ((iface = if_find(ifindex)) == NULL) {
686
		bzero(ifname, sizeof(ifname));
687
		if (sdl && sdl->sdl_family == AF_LINK) {
688
			if (sdl->sdl_nlen >= sizeof(ifname))
689
				memcpy(ifname, sdl->sdl_data,
690
				    sizeof(ifname) - 1);
691
			else if (sdl->sdl_nlen > 0)
692
				memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
693
			else
694
				return (NULL);
695
		} else
696
			return (NULL);
697
		if ((iface = if_new(ifindex, ifname)) == NULL)
698
			return (NULL);
699
		iface->cflags |= F_IFACE_AVAIL;
700
	}
701
702
	if_update(iface, ifd->ifi_mtu, flags, ifd->ifi_type,
703
	    ifd->ifi_link_state, ifd->ifi_baudrate);
704
705
	return (iface);
706
}
707
708
int
709
kif_validate(u_short ifindex)
710
{
711
	struct iface	*iface;
712
713
	if ((iface = if_find(ifindex)) == NULL) {
714
		log_warnx("interface with index %u not found", ifindex);
715
		return (-1);
716
	}
717
718
	return ((iface->flags & IFF_UP) && LINK_STATE_IS_UP(iface->linkstate));
719
}
720
721
struct kroute_node *
722
kroute_match(struct in6_addr *key)
723
{
724
	int			 i;
725
	struct kroute_node	*kr;
726
	struct in6_addr		 ina;
727
728
	/* we will never match the default route */
729
	for (i = 128; i > 0; i--) {
730
		inet6applymask(&ina, key, i);
731
		if ((kr = kroute_find(&ina, i, RTP_ANY)) != NULL)
732
			return (kr);
733
	}
734
735
	/* if we don't have a match yet, try to find a default route */
736
	if ((kr = kroute_find(&in6addr_any, 0, RTP_ANY)) != NULL)
737
			return (kr);
738
739
	return (NULL);
740
}
741
742
/* misc */
743
int
744
protect_lo(void)
745
{
746
	struct kroute_node	*kr;
747
748
	/* special protection for loopback */
749
	if ((kr = calloc(1, sizeof(struct kroute_node))) == NULL) {
750
		log_warn("protect_lo");
751
		return (-1);
752
	}
753
	memcpy(&kr->r.prefix, &in6addr_loopback, sizeof(kr->r.prefix));
754
	kr->r.prefixlen = 128;
755
	kr->r.flags = F_KERNEL|F_CONNECTED;
756
757
	if (RB_INSERT(kroute_tree, &krt, kr) != NULL)
758
		free(kr);	/* kernel route already there, no problem */
759
760
	return (0);
761
}
762
763
#define ROUNDUP(a) \
764
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
765
766
void
767
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
768
{
769
	int	i;
770
771
	for (i = 0; i < RTAX_MAX; i++) {
772
		if (addrs & (1 << i)) {
773
			rti_info[i] = sa;
774
			sa = (struct sockaddr *)((char *)(sa) +
775
			    ROUNDUP(sa->sa_len));
776
		} else
777
			rti_info[i] = NULL;
778
	}
779
}
780
781
void
782
if_change(u_short ifindex, int flags, struct if_data *ifd)
783
{
784
	struct kroute_node	*kr, *tkr;
785
	struct iface		*iface;
786
	u_int8_t		 wasvalid, isvalid;
787
788
	wasvalid = kif_validate(ifindex);
789
790
	if ((iface = kif_update(ifindex, flags, ifd, NULL)) == NULL) {
791
		log_warn("if_change: kif_update(%u)", ifindex);
792
		return;
793
	}
794
795
	/* inform engine and rde about state change if interface is used */
796
	if (iface->cflags & F_IFACE_CONFIGURED) {
797
		main_imsg_compose_ospfe(IMSG_IFINFO, 0, iface,
798
		    sizeof(struct iface));
799
		main_imsg_compose_rde(IMSG_IFINFO, 0, iface,
800
		    sizeof(struct iface));
801
	}
802
803
	isvalid = (iface->flags & IFF_UP) &&
804
	    LINK_STATE_IS_UP(iface->linkstate);
805
806
	if (wasvalid == isvalid)
807
		return;		/* nothing changed wrt validity */
808
809
	/* update redistribute list */
810
	RB_FOREACH(kr, kroute_tree, &krt) {
811
		for (tkr = kr; tkr != NULL; tkr = tkr->next) {
812
			if (tkr->r.ifindex == ifindex) {
813
				if (isvalid)
814
					tkr->r.flags &= ~F_DOWN;
815
				else
816
					tkr->r.flags |= F_DOWN;
817
818
			}
819
		}
820
		kr_redistribute(kr);
821
	}
822
}
823
824
void
825
if_newaddr(u_short ifindex, struct sockaddr_in6 *ifa, struct sockaddr_in6 *mask,
826
    struct sockaddr_in6 *brd)
827
{
828
	struct iface		*iface;
829
	struct iface_addr	*ia;
830
	struct ifaddrchange	 ifc;
831
832
	if (ifa == NULL || ifa->sin6_family != AF_INET6)
833
		return;
834
	if ((iface = if_find(ifindex)) == NULL) {
835
		log_warnx("if_newaddr: corresponding if %d not found", ifindex);
836
		return;
837
	}
838
839
	/* We only care about link-local and global-scope. */
840
	if (IN6_IS_ADDR_UNSPECIFIED(&ifa->sin6_addr) ||
841
	    IN6_IS_ADDR_LOOPBACK(&ifa->sin6_addr) ||
842
	    IN6_IS_ADDR_MULTICAST(&ifa->sin6_addr) ||
843
	    IN6_IS_ADDR_SITELOCAL(&ifa->sin6_addr) ||
844
	    IN6_IS_ADDR_V4MAPPED(&ifa->sin6_addr) ||
845
	    IN6_IS_ADDR_V4COMPAT(&ifa->sin6_addr))
846
		return;
847
848
	clearscope(&ifa->sin6_addr);
849
850
	if (IN6_IS_ADDR_LINKLOCAL(&ifa->sin6_addr) ||
851
	    iface->flags & IFF_LOOPBACK)
852
		iface->addr = ifa->sin6_addr;
853
854
	if ((ia = calloc(1, sizeof(struct iface_addr))) == NULL)
855
		fatal("if_newaddr");
856
857
	ia->addr = ifa->sin6_addr;
858
859
	if (mask)
860
		ia->prefixlen = mask2prefixlen(mask);
861
	else
862
		ia->prefixlen = 0;
863
	if (brd && brd->sin6_family == AF_INET6)
864
		ia->dstbrd = brd->sin6_addr;
865
	else
866
		bzero(&ia->dstbrd, sizeof(ia->dstbrd));
867
868
	switch (iface->type) {
869
	case IF_TYPE_BROADCAST:
870
	case IF_TYPE_NBMA:
871
		log_debug("if_newaddr: ifindex %u, addr %s/%d",
872
		    ifindex, log_in6addr(&ia->addr), ia->prefixlen);
873
		break;
874
	case IF_TYPE_VIRTUALLINK:	/* FIXME */
875
		break;
876
	case IF_TYPE_POINTOPOINT:
877
	case IF_TYPE_POINTOMULTIPOINT:
878
		log_debug("if_newaddr: ifindex %u, addr %s/%d, "
879
		    "dest %s", ifindex, log_in6addr(&ia->addr),
880
		    ia->prefixlen, log_in6addr(&ia->dstbrd));
881
		break;
882
	default:
883
		fatalx("if_newaddr: unknown interface type");
884
	}
885
886
	TAILQ_INSERT_TAIL(&iface->ifa_list, ia, entry);
887
	/* inform engine and rde if interface is used */
888
	if (iface->cflags & F_IFACE_CONFIGURED) {
889
		ifc.addr = ia->addr;
890
		ifc.dstbrd = ia->dstbrd;
891
		ifc.prefixlen = ia->prefixlen;
892
		ifc.ifindex = ifindex;
893
		main_imsg_compose_ospfe(IMSG_IFADDRNEW, 0, &ifc, sizeof(ifc));
894
		main_imsg_compose_rde(IMSG_IFADDRNEW, 0, &ifc, sizeof(ifc));
895
	}
896
}
897
898
void
899
if_deladdr(u_short ifindex, struct sockaddr_in6 *ifa, struct sockaddr_in6 *mask,
900
    struct sockaddr_in6 *brd)
901
{
902
	struct iface		*iface;
903
	struct iface_addr	*ia, *nia;
904
	struct ifaddrchange	 ifc;
905
906
	if (ifa == NULL || ifa->sin6_family != AF_INET6)
907
		return;
908
	if ((iface = if_find(ifindex)) == NULL) {
909
		log_warnx("if_deladdr: corresponding if %d not found", ifindex);
910
		return;
911
	}
912
913
	/* We only care about link-local and global-scope. */
914
	if (IN6_IS_ADDR_UNSPECIFIED(&ifa->sin6_addr) ||
915
	    IN6_IS_ADDR_LOOPBACK(&ifa->sin6_addr) ||
916
	    IN6_IS_ADDR_MULTICAST(&ifa->sin6_addr) ||
917
	    IN6_IS_ADDR_SITELOCAL(&ifa->sin6_addr) ||
918
	    IN6_IS_ADDR_V4MAPPED(&ifa->sin6_addr) ||
919
	    IN6_IS_ADDR_V4COMPAT(&ifa->sin6_addr))
920
		return;
921
922
	clearscope(&ifa->sin6_addr);
923
924
	for (ia = TAILQ_FIRST(&iface->ifa_list); ia != NULL; ia = nia) {
925
		nia = TAILQ_NEXT(ia, entry);
926
927
		if (IN6_ARE_ADDR_EQUAL(&ia->addr, &ifa->sin6_addr)) {
928
			log_debug("if_deladdr: ifindex %u, addr %s/%d",
929
			    ifindex, log_in6addr(&ia->addr), ia->prefixlen);
930
			TAILQ_REMOVE(&iface->ifa_list, ia, entry);
931
			/* inform engine and rde if interface is used */
932
			if (iface->cflags & F_IFACE_CONFIGURED) {
933
				ifc.addr = ia->addr;
934
				ifc.dstbrd = ia->dstbrd;
935
				ifc.prefixlen = ia->prefixlen;
936
				ifc.ifindex = ifindex;
937
				main_imsg_compose_ospfe(IMSG_IFADDRDEL, 0, &ifc,
938
				    sizeof(ifc));
939
				main_imsg_compose_rde(IMSG_IFADDRDEL, 0, &ifc,
940
				    sizeof(ifc));
941
			}
942
			free(ia);
943
			return;
944
		}
945
	}
946
}
947
948
void
949
if_announce(void *msg)
950
{
951
	struct if_announcemsghdr	*ifan;
952
	struct iface			*iface;
953
954
	ifan = msg;
955
956
	switch (ifan->ifan_what) {
957
	case IFAN_ARRIVAL:
958
		if ((iface = if_new(ifan->ifan_index, ifan->ifan_name)) == NULL)
959
			fatal("if_announce failed");
960
		iface->cflags |= F_IFACE_AVAIL;
961
		break;
962
	case IFAN_DEPARTURE:
963
		iface = if_find(ifan->ifan_index);
964
		if (iface->cflags & F_IFACE_CONFIGURED) {
965
			main_imsg_compose_rde(IMSG_IFDELETE, 0,
966
			    &iface->ifindex, sizeof(iface->ifindex));
967
			main_imsg_compose_ospfe(IMSG_IFDELETE, 0,
968
			    &iface->ifindex, sizeof(iface->ifindex));
969
		}
970
		if_del(iface);
971
		break;
972
	}
973
}
974
975
/* rtsock */
976
int
977
send_rtmsg(int fd, int action, struct kroute *kroute)
978
{
979
	struct iovec		iov[5];
980
	struct rt_msghdr	hdr;
981
	struct pad {
982
		struct sockaddr_in6	addr;
983
		char			pad[sizeof(long)]; /* thank you IPv6 */
984
	} prefix, nexthop, mask;
985
	struct {
986
		struct sockaddr_dl	addr;
987
		char			pad[sizeof(long)];
988
	} ifp;
989
	struct sockaddr_rtlabel	sa_rl;
990
	int			iovcnt = 0;
991
	const char		*label;
992
993
	if (kr_state.fib_sync == 0)
994
		return (0);
995
996
	/* initialize header */
997
	bzero(&hdr, sizeof(hdr));
998
	hdr.rtm_version = RTM_VERSION;
999
	hdr.rtm_type = action;
1000
	hdr.rtm_priority = RTP_OSPF;
1001
	if (action == RTM_CHANGE)
1002
		hdr.rtm_fmask = RTF_REJECT|RTF_BLACKHOLE;
1003
	else
1004
		hdr.rtm_flags = RTF_MPATH;
1005
	hdr.rtm_seq = kr_state.rtseq++;	/* overflow doesn't matter */
1006
	hdr.rtm_hdrlen = sizeof(hdr);
1007
	hdr.rtm_msglen = sizeof(hdr);
1008
	/* adjust iovec */
1009
	iov[iovcnt].iov_base = &hdr;
1010
	iov[iovcnt++].iov_len = sizeof(hdr);
1011
1012
	bzero(&prefix, sizeof(prefix));
1013
	prefix.addr.sin6_len = sizeof(struct sockaddr_in6);
1014
	prefix.addr.sin6_family = AF_INET6;
1015
	prefix.addr.sin6_addr = kroute->prefix;
1016
	/* adjust header */
1017
	hdr.rtm_addrs |= RTA_DST;
1018
	hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1019
	/* adjust iovec */
1020
	iov[iovcnt].iov_base = &prefix;
1021
	iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1022
1023
	if (!IN6_IS_ADDR_UNSPECIFIED(&kroute->nexthop)) {
1024
		bzero(&nexthop, sizeof(nexthop));
1025
		nexthop.addr.sin6_len = sizeof(struct sockaddr_in6);
1026
		nexthop.addr.sin6_family = AF_INET6;
1027
		nexthop.addr.sin6_addr = kroute->nexthop;
1028
		nexthop.addr.sin6_scope_id = kroute->scope;
1029
		/*
1030
		 * XXX we should set the sin6_scope_id but the kernel
1031
		 * XXX does not expect it that way. It must be fiddled
1032
		 * XXX into the sin6_addr. Welcome to the typical
1033
		 * XXX IPv6 insanity and all without wine bottles.
1034
		 */
1035
		embedscope(&nexthop.addr);
1036
1037
		/* adjust header */
1038
		hdr.rtm_flags |= RTF_GATEWAY;
1039
		hdr.rtm_addrs |= RTA_GATEWAY;
1040
		hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1041
		/* adjust iovec */
1042
		iov[iovcnt].iov_base = &nexthop;
1043
		iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1044
	} else if (kroute->ifindex) {
1045
		/*
1046
		 * We don't have an interface address in that network,
1047
		 * so we install a cloning route.  The kernel will then
1048
		 * do neigbor discovery.
1049
		 */
1050
		bzero(&ifp, sizeof(ifp));
1051
		ifp.addr.sdl_len = sizeof(struct sockaddr_dl);
1052
		ifp.addr.sdl_family = AF_LINK;
1053
1054
		ifp.addr.sdl_index  = kroute->ifindex;
1055
		/* adjust header */
1056
		hdr.rtm_flags |= RTF_CLONING;
1057
		hdr.rtm_addrs |= RTA_GATEWAY;
1058
		hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_dl));
1059
		/* adjust iovec */
1060
		iov[iovcnt].iov_base = &ifp;
1061
		iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_dl));
1062
	}
1063
1064
	bzero(&mask, sizeof(mask));
1065
	mask.addr.sin6_len = sizeof(struct sockaddr_in6);
1066
	mask.addr.sin6_family = AF_INET6;
1067
	mask.addr.sin6_addr = *prefixlen2mask(kroute->prefixlen);
1068
	/* adjust header */
1069
	if (kroute->prefixlen == 128)
1070
		hdr.rtm_flags |= RTF_HOST;
1071
	hdr.rtm_addrs |= RTA_NETMASK;
1072
	hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1073
	/* adjust iovec */
1074
	iov[iovcnt].iov_base = &mask;
1075
	iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1076
1077
	if (kroute->rtlabel != 0) {
1078
		sa_rl.sr_len = sizeof(sa_rl);
1079
		sa_rl.sr_family = AF_UNSPEC;
1080
		label = rtlabel_id2name(kroute->rtlabel);
1081
		if (strlcpy(sa_rl.sr_label, label,
1082
		    sizeof(sa_rl.sr_label)) >= sizeof(sa_rl.sr_label)) {
1083
			log_warnx("send_rtmsg: invalid rtlabel");
1084
			return (-1);
1085
		}
1086
		/* adjust header */
1087
		hdr.rtm_addrs |= RTA_LABEL;
1088
		hdr.rtm_msglen += sizeof(sa_rl);
1089
		/* adjust iovec */
1090
		iov[iovcnt].iov_base = &sa_rl;
1091
		iov[iovcnt++].iov_len = sizeof(sa_rl);
1092
	}
1093
1094
retry:
1095
	if (writev(fd, iov, iovcnt) == -1) {
1096
		if (errno == ESRCH) {
1097
			if (hdr.rtm_type == RTM_CHANGE) {
1098
				hdr.rtm_type = RTM_ADD;
1099
				goto retry;
1100
			} else if (hdr.rtm_type == RTM_DELETE) {
1101
				log_info("route %s/%u vanished before delete",
1102
				    log_sockaddr(&prefix), kroute->prefixlen);
1103
				return (0);
1104
			}
1105
		}
1106
		log_warn("send_rtmsg: action %u, prefix %s/%u", hdr.rtm_type,
1107
		    log_sockaddr(&prefix), kroute->prefixlen);
1108
		return (0);
1109
	}
1110
1111
	return (0);
1112
}
1113
1114
int
1115
fetchtable(void)
1116
{
1117
	size_t			 len;
1118
	int			 mib[7];
1119
	char			*buf, *next, *lim;
1120
	struct rt_msghdr	*rtm;
1121
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1122
	struct sockaddr_in6	*sa_in6;
1123
	struct sockaddr_rtlabel	*label;
1124
	struct kroute_node	*kr;
1125
1126
	mib[0] = CTL_NET;
1127
	mib[1] = PF_ROUTE;
1128
	mib[2] = 0;
1129
	mib[3] = AF_INET6;
1130
	mib[4] = NET_RT_DUMP;
1131
	mib[5] = 0;
1132
	mib[6] = 0;	/* rtableid */
1133
1134
	if (sysctl(mib, 7, NULL, &len, NULL, 0) == -1) {
1135
		log_warn("sysctl");
1136
		return (-1);
1137
	}
1138
	if ((buf = malloc(len)) == NULL) {
1139
		log_warn("fetchtable");
1140
		return (-1);
1141
	}
1142
	if (sysctl(mib, 7, buf, &len, NULL, 0) == -1) {
1143
		log_warn("sysctl");
1144
		free(buf);
1145
		return (-1);
1146
	}
1147
1148
	lim = buf + len;
1149
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1150
		rtm = (struct rt_msghdr *)next;
1151
		if (rtm->rtm_version != RTM_VERSION)
1152
			continue;
1153
		sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1154
		get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1155
1156
		if ((sa = rti_info[RTAX_DST]) == NULL)
1157
			continue;
1158
1159
		/* Skip ARP/ND cache and broadcast routes. */
1160
		if (rtm->rtm_flags & (RTF_LLINFO|RTF_BROADCAST))
1161
			continue;
1162
1163
		if ((kr = calloc(1, sizeof(struct kroute_node))) == NULL) {
1164
			log_warn("fetchtable");
1165
			free(buf);
1166
			return (-1);
1167
		}
1168
1169
		kr->r.flags = F_KERNEL;
1170
		kr->r.priority = rtm->rtm_priority;
1171
1172
		switch (sa->sa_family) {
1173
		case AF_INET6:
1174
			kr->r.prefix =
1175
			    ((struct sockaddr_in6 *)sa)->sin6_addr;
1176
			sa_in6 = (struct sockaddr_in6 *)rti_info[RTAX_NETMASK];
1177
			if (rtm->rtm_flags & RTF_STATIC)
1178
				kr->r.flags |= F_STATIC;
1179
			if (rtm->rtm_flags & RTF_BLACKHOLE)
1180
				kr->r.flags |= F_BLACKHOLE;
1181
			if (rtm->rtm_flags & RTF_REJECT)
1182
				kr->r.flags |= F_REJECT;
1183
			if (rtm->rtm_flags & RTF_DYNAMIC)
1184
				kr->r.flags |= F_DYNAMIC;
1185
			if (sa_in6 != NULL) {
1186
				if (sa_in6->sin6_len == 0)
1187
					break;
1188
				kr->r.prefixlen =
1189
				    mask2prefixlen(sa_in6);
1190
			} else if (rtm->rtm_flags & RTF_HOST)
1191
				kr->r.prefixlen = 128;
1192
			else
1193
				fatalx("classful IPv6 route?!!");
1194
			break;
1195
		default:
1196
			free(kr);
1197
			continue;
1198
		}
1199
1200
		kr->r.ifindex = rtm->rtm_index;
1201
		if ((sa = rti_info[RTAX_GATEWAY]) != NULL)
1202
			switch (sa->sa_family) {
1203
			case AF_INET6:
1204
				if (rtm->rtm_flags & RTF_CONNECTED) {
1205
					kr->r.flags |= F_CONNECTED;
1206
					break;
1207
				}
1208
1209
				sa_in6 = (struct sockaddr_in6 *)sa;
1210
				/*
1211
				 * XXX The kernel provides the scope via the
1212
				 * XXX kame hack instead of the scope_id field.
1213
				 */
1214
				recoverscope(sa_in6);
1215
				kr->r.nexthop = sa_in6->sin6_addr;
1216
				kr->r.scope = sa_in6->sin6_scope_id;
1217
				break;
1218
			case AF_LINK:
1219
				/*
1220
				 * Traditional BSD connected routes have
1221
				 * a gateway of type AF_LINK.
1222
				 */
1223
				kr->r.flags |= F_CONNECTED;
1224
				break;
1225
			}
1226
1227
		if (rtm->rtm_priority == RTP_OSPF)  {
1228
			send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r);
1229
			free(kr);
1230
		} else {
1231
			if ((label = (struct sockaddr_rtlabel *)
1232
			    rti_info[RTAX_LABEL]) != NULL) {
1233
				kr->r.rtlabel =
1234
				    rtlabel_name2id(label->sr_label);
1235
				kr->r.ext_tag =
1236
				    rtlabel_id2tag(kr->r.rtlabel);
1237
			}
1238
			kroute_insert(kr);
1239
		}
1240
1241
	}
1242
	free(buf);
1243
	return (0);
1244
}
1245
1246
int
1247
fetchifs(u_short ifindex)
1248
{
1249
	size_t			 len;
1250
	int			 mib[6];
1251
	char			*buf, *next, *lim;
1252
	struct rt_msghdr	*rtm;
1253
	struct if_msghdr	 ifm;
1254
	struct ifa_msghdr	*ifam;
1255
	struct iface		*iface;
1256
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1257
1258
	mib[0] = CTL_NET;
1259
	mib[1] = PF_ROUTE;
1260
	mib[2] = 0;
1261
	mib[3] = AF_INET6;
1262
	mib[4] = NET_RT_IFLIST;
1263
	mib[5] = ifindex;
1264
1265
	if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
1266
		log_warn("sysctl");
1267
		return (-1);
1268
	}
1269
	if ((buf = malloc(len)) == NULL) {
1270
		log_warn("fetchifs");
1271
		return (-1);
1272
	}
1273
	if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
1274
		log_warn("sysctl");
1275
		free(buf);
1276
		return (-1);
1277
	}
1278
1279
	lim = buf + len;
1280
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1281
		rtm = (struct rt_msghdr *)next;
1282
		if (rtm->rtm_version != RTM_VERSION)
1283
			continue;
1284
		switch (rtm->rtm_type) {
1285
		case RTM_IFINFO:
1286
			bcopy(rtm, &ifm, sizeof ifm);
1287
			sa = (struct sockaddr *)(next + sizeof(ifm));
1288
			get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
1289
1290
			if ((iface = kif_update(ifm.ifm_index,
1291
			    ifm.ifm_flags, &ifm.ifm_data,
1292
			    (struct sockaddr_dl *)rti_info[RTAX_IFP])) == NULL)
1293
			break;
1294
		case RTM_NEWADDR:
1295
			ifam = (struct ifa_msghdr *)rtm;
1296
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1297
			    RTA_BRD)) == 0)
1298
				break;
1299
			sa = (struct sockaddr *)(ifam + 1);
1300
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1301
1302
			if_newaddr(ifam->ifam_index,
1303
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1304
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1305
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1306
			break;
1307
		}
1308
	}
1309
	free(buf);
1310
	return (0);
1311
}
1312
1313
int
1314
dispatch_rtmsg(void)
1315
{
1316
	char			 buf[RT_BUF_SIZE];
1317
	ssize_t			 n;
1318
	char			*next, *lim;
1319
	struct rt_msghdr	*rtm;
1320
	struct if_msghdr	 ifm;
1321
	struct ifa_msghdr	*ifam;
1322
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1323
	struct sockaddr_in6	*sa_in6;
1324
	struct sockaddr_rtlabel	*label;
1325
	struct kroute_node	*kr, *okr;
1326
	struct in6_addr		 prefix, nexthop;
1327
	u_int8_t		 prefixlen, prio;
1328
	int			 flags, mpath;
1329
	unsigned int		 scope;
1330
	u_short			 ifindex = 0;
1331
1332
	if ((n = read(kr_state.fd, &buf, sizeof(buf))) == -1) {
1333
		if (errno == EAGAIN || errno == EINTR)
1334
			return (0);
1335
		log_warn("dispatch_rtmsg: read error");
1336
		return (-1);
1337
	}
1338
1339
	if (n == 0) {
1340
		log_warnx("routing socket closed");
1341
		return (-1);
1342
	}
1343
1344
	lim = buf + n;
1345
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1346
		rtm = (struct rt_msghdr *)next;
1347
		if (lim < next + sizeof(u_short) ||
1348
		    lim < next + rtm->rtm_msglen)
1349
			fatalx("dispatch_rtmsg: partial rtm in buffer");
1350
		if (rtm->rtm_version != RTM_VERSION)
1351
			continue;
1352
1353
		bzero(&prefix, sizeof(prefix));
1354
		bzero(&nexthop, sizeof(nexthop));
1355
		scope = 0;
1356
		prefixlen = 0;
1357
		flags = F_KERNEL;
1358
		mpath = 0;
1359
		prio = 0;
1360
1361
		if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE ||
1362
		    rtm->rtm_type == RTM_DELETE) {
1363
			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1364
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1365
1366
			if (rtm->rtm_tableid != 0)
1367
				continue;
1368
1369
			if (rtm->rtm_pid == kr_state.pid) /* caused by us */
1370
				continue;
1371
1372
			if (rtm->rtm_errno)		/* failed attempts... */
1373
				continue;
1374
1375
			/* Skip ARP/ND cache and broadcast routes. */
1376
			if (rtm->rtm_flags & (RTF_LLINFO|RTF_BROADCAST))
1377
				continue;
1378
1379
			if (rtm->rtm_flags & RTF_MPATH)
1380
				mpath = 1;
1381
			prio = rtm->rtm_priority;
1382
1383
			switch (sa->sa_family) {
1384
			case AF_INET6:
1385
				prefix =
1386
				    ((struct sockaddr_in6 *)sa)->sin6_addr;
1387
				sa_in6 = (struct sockaddr_in6 *)
1388
				    rti_info[RTAX_NETMASK];
1389
				if (sa_in6 != NULL) {
1390
					if (sa_in6->sin6_len != 0)
1391
						prefixlen = mask2prefixlen(
1392
						    sa_in6);
1393
				} else if (rtm->rtm_flags & RTF_HOST)
1394
					prefixlen = 128;
1395
				else
1396
					fatalx("classful IPv6 address?!!");
1397
				if (rtm->rtm_flags & RTF_STATIC)
1398
					flags |= F_STATIC;
1399
				if (rtm->rtm_flags & RTF_BLACKHOLE)
1400
					flags |= F_BLACKHOLE;
1401
				if (rtm->rtm_flags & RTF_REJECT)
1402
					flags |= F_REJECT;
1403
				if (rtm->rtm_flags & RTF_DYNAMIC)
1404
					flags |= F_DYNAMIC;
1405
				break;
1406
			default:
1407
				continue;
1408
			}
1409
1410
			ifindex = rtm->rtm_index;
1411
			if ((sa = rti_info[RTAX_GATEWAY]) != NULL) {
1412
				switch (sa->sa_family) {
1413
				case AF_INET6:
1414
					sa_in6 = (struct sockaddr_in6 *)sa;
1415
					/*
1416
					 * XXX The kernel provides the scope
1417
					 * XXX via the kame hack instead of
1418
					 * XXX the scope_id field.
1419
					 */
1420
					recoverscope(sa_in6);
1421
					nexthop = sa_in6->sin6_addr;
1422
					scope = sa_in6->sin6_scope_id;
1423
					break;
1424
				case AF_LINK:
1425
					flags |= F_CONNECTED;
1426
					break;
1427
				}
1428
			}
1429
		}
1430
1431
		switch (rtm->rtm_type) {
1432
		case RTM_ADD:
1433
		case RTM_CHANGE:
1434
			if (IN6_IS_ADDR_UNSPECIFIED(&nexthop) &&
1435
			    !(flags & F_CONNECTED)) {
1436
				log_warnx("dispatch_rtmsg no nexthop for %s/%u",
1437
				    log_in6addr(&prefix), prefixlen);
1438
				continue;
1439
			}
1440
1441
			if ((okr = kroute_find(&prefix, prefixlen, prio))
1442
			    != NULL) {
1443
				/* just add new multipath routes */
1444
				if (mpath && rtm->rtm_type == RTM_ADD)
1445
					goto add;
1446
				/* get the correct route */
1447
				kr = okr;
1448
				if (mpath && (kr = kroute_matchgw(okr,
1449
				    &nexthop, scope)) == NULL) {
1450
					log_warnx("dispatch_rtmsg mpath route"
1451
					    " not found");
1452
					/* add routes we missed out earlier */
1453
					goto add;
1454
				}
1455
1456
				if (kr->r.flags & F_REDISTRIBUTED)
1457
					flags |= F_REDISTRIBUTED;
1458
				kr->r.nexthop = nexthop;
1459
				kr->r.scope = scope;
1460
				kr->r.flags = flags;
1461
				kr->r.ifindex = ifindex;
1462
1463
				rtlabel_unref(kr->r.rtlabel);
1464
				kr->r.rtlabel = 0;
1465
				kr->r.ext_tag = 0;
1466
				if ((label = (struct sockaddr_rtlabel *)
1467
				    rti_info[RTAX_LABEL]) != NULL) {
1468
					kr->r.rtlabel =
1469
					    rtlabel_name2id(label->sr_label);
1470
					kr->r.ext_tag =
1471
					    rtlabel_id2tag(kr->r.rtlabel);
1472
				}
1473
1474
				if (kif_validate(kr->r.ifindex))
1475
					kr->r.flags &= ~F_DOWN;
1476
				else
1477
					kr->r.flags |= F_DOWN;
1478
1479
				/* just readd, the RDE will care */
1480
				kr_redistribute(okr);
1481
			} else {
1482
add:
1483
				if ((kr = calloc(1,
1484
				    sizeof(struct kroute_node))) == NULL) {
1485
					log_warn("dispatch_rtmsg");
1486
					return (-1);
1487
				}
1488
				kr->r.prefix = prefix;
1489
				kr->r.prefixlen = prefixlen;
1490
				kr->r.nexthop = nexthop;
1491
				kr->r.scope = scope;
1492
				kr->r.flags = flags;
1493
				kr->r.ifindex = ifindex;
1494
				kr->r.priority = prio;
1495
1496
				if ((label = (struct sockaddr_rtlabel *)
1497
				    rti_info[RTAX_LABEL]) != NULL) {
1498
					kr->r.rtlabel =
1499
					    rtlabel_name2id(label->sr_label);
1500
					kr->r.ext_tag =
1501
					    rtlabel_id2tag(kr->r.rtlabel);
1502
				}
1503
1504
				kroute_insert(kr);
1505
			}
1506
			break;
1507
		case RTM_DELETE:
1508
			if ((kr = kroute_find(&prefix, prefixlen, prio)) ==
1509
			    NULL)
1510
				continue;
1511
			if (!(kr->r.flags & F_KERNEL))
1512
				continue;
1513
			/* get the correct route */
1514
			okr = kr;
1515
			if (mpath && (kr = kroute_matchgw(kr, &nexthop,
1516
			    scope)) == NULL) {
1517
				log_warnx("dispatch_rtmsg mpath route"
1518
				    " not found");
1519
				return (-1);
1520
			}
1521
			if (kroute_remove(kr) == -1)
1522
				return (-1);
1523
			break;
1524
		case RTM_IFINFO:
1525
			memcpy(&ifm, next, sizeof(ifm));
1526
			if_change(ifm.ifm_index, ifm.ifm_flags,
1527
			    &ifm.ifm_data);
1528
			break;
1529
		case RTM_NEWADDR:
1530
			ifam = (struct ifa_msghdr *)rtm;
1531
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1532
			    RTA_BRD)) == 0)
1533
				break;
1534
			sa = (struct sockaddr *)(ifam + 1);
1535
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1536
1537
			if_newaddr(ifam->ifam_index,
1538
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1539
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1540
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1541
			break;
1542
		case RTM_DELADDR:
1543
			ifam = (struct ifa_msghdr *)rtm;
1544
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1545
			    RTA_BRD)) == 0)
1546
				break;
1547
			sa = (struct sockaddr *)(ifam + 1);
1548
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1549
1550
			if_deladdr(ifam->ifam_index,
1551
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1552
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1553
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1554
			break;
1555
		case RTM_IFANNOUNCE:
1556
			if_announce(next);
1557
			break;
1558
		default:
1559
			/* ignore for now */
1560
			break;
1561
		}
1562
	}
1563
	return (0);
1564
}