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

Line Branch Exec Source
1
/*	$OpenBSD: kroute.c,v 1.53 2017/11/05 14:33:27 jca 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 = kroute_match(&addr);
373
		if (kr != NULL)
374
			main_imsg_compose_ospfe(IMSG_CTL_KROUTE, imsg->hdr.pid,
375
			    &kr->r, sizeof(kr->r));
376
		break;
377
	default:
378
		log_debug("kr_show_route: error handling imsg");
379
		break;
380
	}
381
382
	main_imsg_compose_ospfe(IMSG_CTL_END, imsg->hdr.pid, NULL, 0);
383
}
384
385
void
386
kr_redist_remove(struct kroute_node *kh, struct kroute_node *kn)
387
{
388
	struct kroute	 *kr;
389
390
	/* was the route redistributed? */
391
	if ((kn->r.flags & F_REDISTRIBUTED) == 0)
392
		return;
393
394
	/* remove redistributed flag */
395
	kn->r.flags &= ~F_REDISTRIBUTED;
396
	kr = &kn->r;
397
398
	/* probably inform the RDE (check if no other path is redistributed) */
399
	for (kn = kh; kn; kn = kn->next)
400
		if (kn->r.flags & F_REDISTRIBUTED)
401
			break;
402
403
	if (kn == NULL)
404
		main_imsg_compose_rde(IMSG_NETWORK_DEL, 0, kr,
405
		    sizeof(struct kroute));
406
}
407
408
int
409
kr_redist_eval(struct kroute *kr, struct kroute *new_kr)
410
{
411
	u_int32_t	 metric = 0;
412
413
	/* Only non-ospfd routes are considered for redistribution. */
414
	if (!(kr->flags & F_KERNEL))
415
		goto dont_redistribute;
416
417
	/* Dynamic routes are not redistributable. */
418
	if (kr->flags & F_DYNAMIC)
419
		goto dont_redistribute;
420
421
	/* interface is not up and running so don't announce */
422
	if (kr->flags & F_DOWN)
423
		goto dont_redistribute;
424
425
	/*
426
	 * We consider loopback, multicast, link- and site-local,
427
	 * IPv4 mapped and IPv4 compatible addresses as not redistributable.
428
	 */
429
	if (IN6_IS_ADDR_LOOPBACK(&kr->prefix) ||
430
	    IN6_IS_ADDR_MULTICAST(&kr->prefix) ||
431
	    IN6_IS_ADDR_LINKLOCAL(&kr->prefix) ||
432
	    IN6_IS_ADDR_SITELOCAL(&kr->prefix) ||
433
	    IN6_IS_ADDR_V4MAPPED(&kr->prefix) ||
434
	    IN6_IS_ADDR_V4COMPAT(&kr->prefix))
435
		goto dont_redistribute;
436
	/*
437
	 * Consider networks with nexthop loopback as not redistributable
438
	 * unless it is a reject or blackhole route.
439
	 */
440
	if (IN6_IS_ADDR_LOOPBACK(&kr->nexthop) &&
441
	    !(kr->flags & (F_BLACKHOLE|F_REJECT)))
442
		goto dont_redistribute;
443
444
	/* Should we redistribute this route? */
445
	if (!ospf_redistribute(kr, &metric))
446
		goto dont_redistribute;
447
448
	/* prefix should be redistributed */
449
	kr->flags |= F_REDISTRIBUTED;
450
	/*
451
	 * only one of all multipath routes can be redistributed so
452
	 * redistribute the best one.
453
	 */
454
	if (new_kr->metric > metric) {
455
		*new_kr = *kr;
456
		new_kr->metric = metric;
457
	}
458
459
	return (1);
460
461
dont_redistribute:
462
	/* was the route redistributed? */
463
	if ((kr->flags & F_REDISTRIBUTED) == 0)
464
		return (0);
465
466
	kr->flags &= ~F_REDISTRIBUTED;
467
	return (1);
468
}
469
470
void
471
kr_redistribute(struct kroute_node *kh)
472
{
473
	struct kroute_node	*kn;
474
	struct kroute		 kr;
475
	int			 redistribute = 0;
476
477
	/* only the highest prio route can be redistributed */
478
	if (kroute_find(&kh->r.prefix, kh->r.prefixlen, RTP_ANY) != kh)
479
		return;
480
481
	bzero(&kr, sizeof(kr));
482
	kr.metric = UINT_MAX;
483
	for (kn = kh; kn; kn = kn->next)
484
		if (kr_redist_eval(&kn->r, &kr))
485
			redistribute = 1;
486
487
	if (!redistribute)
488
		return;
489
490
	if (kr.flags & F_REDISTRIBUTED) {
491
		main_imsg_compose_rde(IMSG_NETWORK_ADD, 0, &kr,
492
		    sizeof(struct kroute));
493
	} else {
494
		kr = kh->r;
495
		main_imsg_compose_rde(IMSG_NETWORK_DEL, 0, &kr,
496
		    sizeof(struct kroute));
497
	}
498
}
499
500
void
501
kr_reload(void)
502
{
503
	struct kroute_node	*kr, *kn;
504
	u_int32_t		 dummy;
505
	int			 r;
506
507
	RB_FOREACH(kr, kroute_tree, &krt) {
508
		for (kn = kr; kn; kn = kn->next) {
509
			r = ospf_redistribute(&kn->r, &dummy);
510
			/*
511
			 * if it is redistributed, redistribute again metric
512
			 * may have changed.
513
			 */
514
			if ((kn->r.flags & F_REDISTRIBUTED && !r) || r)
515
				break;
516
		}
517
		if (kn) {
518
			/*
519
			 * kr_redistribute copes with removes and RDE with
520
			 * duplicates
521
			 */
522
			kr_redistribute(kr);
523
		}
524
	}
525
}
526
527
/* rb-tree compare */
528
int
529
kroute_compare(struct kroute_node *a, struct kroute_node *b)
530
{
531
	int	i;
532
533
	/* XXX maybe switch a & b */
534
	i = memcmp(&a->r.prefix, &b->r.prefix, sizeof(a->r.prefix));
535
	if (i)
536
		return (i);
537
	if (a->r.prefixlen < b->r.prefixlen)
538
		return (-1);
539
	if (a->r.prefixlen > b->r.prefixlen)
540
		return (1);
541
542
	/* if the priority is RTP_ANY finish on the first address hit */
543
	if (a->r.priority == RTP_ANY || b->r.priority == RTP_ANY)
544
		return (0);
545
	if (a->r.priority < b->r.priority)
546
		return (-1);
547
	if (a->r.priority > b->r.priority)
548
		return (1);
549
	return (0);
550
}
551
552
/* tree management */
553
struct kroute_node *
554
kroute_find(const struct in6_addr *prefix, u_int8_t prefixlen, u_int8_t prio)
555
{
556
	struct kroute_node	s;
557
	struct kroute_node	*kn, *tmp;
558
559
	s.r.prefix = *prefix;
560
	s.r.prefixlen = prefixlen;
561
	s.r.priority = prio;
562
563
	kn = RB_FIND(kroute_tree, &krt, &s);
564
	if (kn && prio == RTP_ANY) {
565
		tmp = RB_PREV(kroute_tree, &krt, kn);
566
		while (tmp) {
567
			if (kroute_compare(&s, tmp) == 0)
568
				kn = tmp;
569
			else
570
				break;
571
			tmp = RB_PREV(kroute_tree, &krt, kn);
572
		}
573
	}
574
	return (kn);
575
}
576
577
struct kroute_node *
578
kroute_matchgw(struct kroute_node *kr, struct in6_addr *nh, unsigned int scope)
579
{
580
	while (kr) {
581
		if (scope == kr->r.scope &&
582
		    IN6_ARE_ADDR_EQUAL(&kr->r.nexthop, nh))
583
			return (kr);
584
		kr = kr->next;
585
	}
586
587
	return (NULL);
588
}
589
590
int
591
kroute_insert(struct kroute_node *kr)
592
{
593
	struct kroute_node	*krm, *krh;
594
595
	if ((krh = RB_INSERT(kroute_tree, &krt, kr)) != NULL) {
596
		/*
597
		 * Multipath route, add at end of list.
598
		 */
599
		krm = krh;
600
		while (krm->next != NULL)
601
			krm = krm->next;
602
		krm->next = kr;
603
		kr->next = NULL; /* to be sure */
604
	} else
605
		krh = kr;
606
607
	if (!(kr->r.flags & F_KERNEL)) {
608
		/* don't validate or redistribute ospf route */
609
		kr->r.flags &= ~F_DOWN;
610
		return (0);
611
	}
612
613
	if (kif_validate(kr->r.ifindex))
614
		kr->r.flags &= ~F_DOWN;
615
	else
616
		kr->r.flags |= F_DOWN;
617
618
	kr_redistribute(krh);
619
	return (0);
620
}
621
622
int
623
kroute_remove(struct kroute_node *kr)
624
{
625
	struct kroute_node	*krm;
626
627
	if ((krm = RB_FIND(kroute_tree, &krt, kr)) == NULL) {
628
		log_warnx("kroute_remove failed to find %s/%u",
629
		    log_in6addr(&kr->r.prefix), kr->r.prefixlen);
630
		return (-1);
631
	}
632
633
	if (krm == kr) {
634
		/* head element */
635
		if (RB_REMOVE(kroute_tree, &krt, kr) == NULL) {
636
			log_warnx("kroute_remove failed for %s/%u",
637
			    log_in6addr(&kr->r.prefix), kr->r.prefixlen);
638
			return (-1);
639
		}
640
		if (kr->next != NULL) {
641
			if (RB_INSERT(kroute_tree, &krt, kr->next) != NULL) {
642
				log_warnx("kroute_remove failed to add %s/%u",
643
				    log_in6addr(&kr->r.prefix),
644
				    kr->r.prefixlen);
645
				return (-1);
646
			}
647
		}
648
	} else {
649
		/* somewhere in the list */
650
		while (krm->next != kr && krm->next != NULL)
651
			krm = krm->next;
652
		if (krm->next == NULL) {
653
			log_warnx("kroute_remove multipath list corrupted "
654
			    "for %s/%u", log_in6addr(&kr->r.prefix),
655
			    kr->r.prefixlen);
656
			return (-1);
657
		}
658
		krm->next = kr->next;
659
	}
660
661
	kr_redist_remove(krm, kr);
662
	rtlabel_unref(kr->r.rtlabel);
663
664
	free(kr);
665
	return (0);
666
}
667
668
void
669
kroute_clear(void)
670
{
671
	struct kroute_node	*kr;
672
673
	while ((kr = RB_MIN(kroute_tree, &krt)) != NULL)
674
		kroute_remove(kr);
675
}
676
677
struct iface *
678
kif_update(u_short ifindex, int flags, struct if_data *ifd,
679
    struct sockaddr_dl *sdl)
680
{
681
	struct iface	*iface;
682
	char		 ifname[IF_NAMESIZE];
683
684
	if ((iface = if_find(ifindex)) == NULL) {
685
		bzero(ifname, sizeof(ifname));
686
		if (sdl && sdl->sdl_family == AF_LINK) {
687
			if (sdl->sdl_nlen >= sizeof(ifname))
688
				memcpy(ifname, sdl->sdl_data,
689
				    sizeof(ifname) - 1);
690
			else if (sdl->sdl_nlen > 0)
691
				memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
692
			else
693
				return (NULL);
694
		} else
695
			return (NULL);
696
		if ((iface = if_new(ifindex, ifname)) == NULL)
697
			return (NULL);
698
		iface->cflags |= F_IFACE_AVAIL;
699
	}
700
701
	if_update(iface, ifd->ifi_mtu, flags, ifd->ifi_type,
702
	    ifd->ifi_link_state, ifd->ifi_baudrate);
703
704
	return (iface);
705
}
706
707
int
708
kif_validate(u_short ifindex)
709
{
710
	struct iface	*iface;
711
712
	if ((iface = if_find(ifindex)) == NULL) {
713
		log_warnx("interface with index %u not found", ifindex);
714
		return (-1);
715
	}
716
717
	return ((iface->flags & IFF_UP) && LINK_STATE_IS_UP(iface->linkstate));
718
}
719
720
struct kroute_node *
721
kroute_match(struct in6_addr *key)
722
{
723
	int			 i;
724
	struct kroute_node	*kr;
725
	struct in6_addr		 ina;
726
727
	/* we will never match the default route */
728
	for (i = 128; i > 0; i--) {
729
		inet6applymask(&ina, key, i);
730
		if ((kr = kroute_find(&ina, i, RTP_ANY)) != NULL)
731
			return (kr);
732
	}
733
734
	/* if we don't have a match yet, try to find a default route */
735
	if ((kr = kroute_find(&in6addr_any, 0, RTP_ANY)) != NULL)
736
			return (kr);
737
738
	return (NULL);
739
}
740
741
/* misc */
742
int
743
protect_lo(void)
744
{
745
	struct kroute_node	*kr;
746
747
	/* special protection for loopback */
748
	if ((kr = calloc(1, sizeof(struct kroute_node))) == NULL) {
749
		log_warn("protect_lo");
750
		return (-1);
751
	}
752
	memcpy(&kr->r.prefix, &in6addr_loopback, sizeof(kr->r.prefix));
753
	kr->r.prefixlen = 128;
754
	kr->r.flags = F_KERNEL|F_CONNECTED;
755
756
	if (RB_INSERT(kroute_tree, &krt, kr) != NULL)
757
		free(kr);	/* kernel route already there, no problem */
758
759
	return (0);
760
}
761
762
#define ROUNDUP(a) \
763
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
764
765
void
766
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
767
{
768
	int	i;
769
770
	for (i = 0; i < RTAX_MAX; i++) {
771
		if (addrs & (1 << i)) {
772
			rti_info[i] = sa;
773
			sa = (struct sockaddr *)((char *)(sa) +
774
			    ROUNDUP(sa->sa_len));
775
		} else
776
			rti_info[i] = NULL;
777
	}
778
}
779
780
void
781
if_change(u_short ifindex, int flags, struct if_data *ifd)
782
{
783
	struct kroute_node	*kr, *tkr;
784
	struct iface		*iface;
785
	u_int8_t		 wasvalid, isvalid;
786
787
	wasvalid = kif_validate(ifindex);
788
789
	if ((iface = kif_update(ifindex, flags, ifd, NULL)) == NULL) {
790
		log_warn("if_change: kif_update(%u)", ifindex);
791
		return;
792
	}
793
794
	/* inform engine and rde about state change if interface is used */
795
	if (iface->cflags & F_IFACE_CONFIGURED) {
796
		main_imsg_compose_ospfe(IMSG_IFINFO, 0, iface,
797
		    sizeof(struct iface));
798
		main_imsg_compose_rde(IMSG_IFINFO, 0, iface,
799
		    sizeof(struct iface));
800
	}
801
802
	isvalid = (iface->flags & IFF_UP) &&
803
	    LINK_STATE_IS_UP(iface->linkstate);
804
805
	if (wasvalid == isvalid)
806
		return;		/* nothing changed wrt validity */
807
808
	/* update redistribute list */
809
	RB_FOREACH(kr, kroute_tree, &krt) {
810
		for (tkr = kr; tkr != NULL; tkr = tkr->next) {
811
			if (tkr->r.ifindex == ifindex) {
812
				if (isvalid)
813
					tkr->r.flags &= ~F_DOWN;
814
				else
815
					tkr->r.flags |= F_DOWN;
816
817
			}
818
		}
819
		kr_redistribute(kr);
820
	}
821
}
822
823
void
824
if_newaddr(u_short ifindex, struct sockaddr_in6 *ifa, struct sockaddr_in6 *mask,
825
    struct sockaddr_in6 *brd)
826
{
827
	struct iface		*iface;
828
	struct iface_addr	*ia;
829
	struct ifaddrchange	 ifc;
830
831
	if (ifa == NULL || ifa->sin6_family != AF_INET6)
832
		return;
833
	if ((iface = if_find(ifindex)) == NULL) {
834
		log_warnx("if_newaddr: corresponding if %d not found", ifindex);
835
		return;
836
	}
837
838
	/* We only care about link-local and global-scope. */
839
	if (IN6_IS_ADDR_UNSPECIFIED(&ifa->sin6_addr) ||
840
	    IN6_IS_ADDR_LOOPBACK(&ifa->sin6_addr) ||
841
	    IN6_IS_ADDR_MULTICAST(&ifa->sin6_addr) ||
842
	    IN6_IS_ADDR_SITELOCAL(&ifa->sin6_addr) ||
843
	    IN6_IS_ADDR_V4MAPPED(&ifa->sin6_addr) ||
844
	    IN6_IS_ADDR_V4COMPAT(&ifa->sin6_addr))
845
		return;
846
847
	clearscope(&ifa->sin6_addr);
848
849
	if (IN6_IS_ADDR_LINKLOCAL(&ifa->sin6_addr) ||
850
	    iface->flags & IFF_LOOPBACK)
851
		iface->addr = ifa->sin6_addr;
852
853
	if ((ia = calloc(1, sizeof(struct iface_addr))) == NULL)
854
		fatal("if_newaddr");
855
856
	ia->addr = ifa->sin6_addr;
857
858
	if (mask)
859
		ia->prefixlen = mask2prefixlen(mask);
860
	else
861
		ia->prefixlen = 0;
862
	if (brd && brd->sin6_family == AF_INET6)
863
		ia->dstbrd = brd->sin6_addr;
864
	else
865
		bzero(&ia->dstbrd, sizeof(ia->dstbrd));
866
867
	switch (iface->type) {
868
	case IF_TYPE_BROADCAST:
869
	case IF_TYPE_NBMA:
870
		log_debug("if_newaddr: ifindex %u, addr %s/%d",
871
		    ifindex, log_in6addr(&ia->addr), ia->prefixlen);
872
		break;
873
	case IF_TYPE_VIRTUALLINK:	/* FIXME */
874
		break;
875
	case IF_TYPE_POINTOPOINT:
876
	case IF_TYPE_POINTOMULTIPOINT:
877
		log_debug("if_newaddr: ifindex %u, addr %s/%d, "
878
		    "dest %s", ifindex, log_in6addr(&ia->addr),
879
		    ia->prefixlen, log_in6addr(&ia->dstbrd));
880
		break;
881
	default:
882
		fatalx("if_newaddr: unknown interface type");
883
	}
884
885
	TAILQ_INSERT_TAIL(&iface->ifa_list, ia, entry);
886
	/* inform engine and rde if interface is used */
887
	if (iface->cflags & F_IFACE_CONFIGURED) {
888
		ifc.addr = ia->addr;
889
		ifc.dstbrd = ia->dstbrd;
890
		ifc.prefixlen = ia->prefixlen;
891
		ifc.ifindex = ifindex;
892
		main_imsg_compose_ospfe(IMSG_IFADDRNEW, 0, &ifc, sizeof(ifc));
893
		main_imsg_compose_rde(IMSG_IFADDRNEW, 0, &ifc, sizeof(ifc));
894
	}
895
}
896
897
void
898
if_deladdr(u_short ifindex, struct sockaddr_in6 *ifa, struct sockaddr_in6 *mask,
899
    struct sockaddr_in6 *brd)
900
{
901
	struct iface		*iface;
902
	struct iface_addr	*ia, *nia;
903
	struct ifaddrchange	 ifc;
904
905
	if (ifa == NULL || ifa->sin6_family != AF_INET6)
906
		return;
907
	if ((iface = if_find(ifindex)) == NULL) {
908
		log_warnx("if_deladdr: corresponding if %d not found", ifindex);
909
		return;
910
	}
911
912
	/* We only care about link-local and global-scope. */
913
	if (IN6_IS_ADDR_UNSPECIFIED(&ifa->sin6_addr) ||
914
	    IN6_IS_ADDR_LOOPBACK(&ifa->sin6_addr) ||
915
	    IN6_IS_ADDR_MULTICAST(&ifa->sin6_addr) ||
916
	    IN6_IS_ADDR_SITELOCAL(&ifa->sin6_addr) ||
917
	    IN6_IS_ADDR_V4MAPPED(&ifa->sin6_addr) ||
918
	    IN6_IS_ADDR_V4COMPAT(&ifa->sin6_addr))
919
		return;
920
921
	clearscope(&ifa->sin6_addr);
922
923
	for (ia = TAILQ_FIRST(&iface->ifa_list); ia != NULL; ia = nia) {
924
		nia = TAILQ_NEXT(ia, entry);
925
926
		if (IN6_ARE_ADDR_EQUAL(&ia->addr, &ifa->sin6_addr)) {
927
			log_debug("if_deladdr: ifindex %u, addr %s/%d",
928
			    ifindex, log_in6addr(&ia->addr), ia->prefixlen);
929
			TAILQ_REMOVE(&iface->ifa_list, ia, entry);
930
			/* inform engine and rde if interface is used */
931
			if (iface->cflags & F_IFACE_CONFIGURED) {
932
				ifc.addr = ia->addr;
933
				ifc.dstbrd = ia->dstbrd;
934
				ifc.prefixlen = ia->prefixlen;
935
				ifc.ifindex = ifindex;
936
				main_imsg_compose_ospfe(IMSG_IFADDRDEL, 0, &ifc,
937
				    sizeof(ifc));
938
				main_imsg_compose_rde(IMSG_IFADDRDEL, 0, &ifc,
939
				    sizeof(ifc));
940
			}
941
			free(ia);
942
			return;
943
		}
944
	}
945
}
946
947
void
948
if_announce(void *msg)
949
{
950
	struct if_announcemsghdr	*ifan;
951
	struct iface			*iface;
952
953
	ifan = msg;
954
955
	switch (ifan->ifan_what) {
956
	case IFAN_ARRIVAL:
957
		if ((iface = if_new(ifan->ifan_index, ifan->ifan_name)) == NULL)
958
			fatal("if_announce failed");
959
		iface->cflags |= F_IFACE_AVAIL;
960
		break;
961
	case IFAN_DEPARTURE:
962
		iface = if_find(ifan->ifan_index);
963
		if (iface->cflags & F_IFACE_CONFIGURED) {
964
			main_imsg_compose_rde(IMSG_IFDELETE, 0,
965
			    &iface->ifindex, sizeof(iface->ifindex));
966
			main_imsg_compose_ospfe(IMSG_IFDELETE, 0,
967
			    &iface->ifindex, sizeof(iface->ifindex));
968
		}
969
		if_del(iface);
970
		break;
971
	}
972
}
973
974
/* rtsock */
975
int
976
send_rtmsg(int fd, int action, struct kroute *kroute)
977
{
978
	struct iovec		iov[5];
979
	struct rt_msghdr	hdr;
980
	struct pad {
981
		struct sockaddr_in6	addr;
982
		char			pad[sizeof(long)]; /* thank you IPv6 */
983
	} prefix, nexthop, mask;
984
	struct {
985
		struct sockaddr_dl	addr;
986
		char			pad[sizeof(long)];
987
	} ifp;
988
	struct sockaddr_rtlabel	sa_rl;
989
	int			iovcnt = 0;
990
	const char		*label;
991
992
	if (kr_state.fib_sync == 0)
993
		return (0);
994
995
	/* initialize header */
996
	bzero(&hdr, sizeof(hdr));
997
	hdr.rtm_version = RTM_VERSION;
998
	hdr.rtm_type = action;
999
	hdr.rtm_priority = RTP_OSPF;
1000
	if (action == RTM_CHANGE)
1001
		hdr.rtm_fmask = RTF_REJECT|RTF_BLACKHOLE;
1002
	else
1003
		hdr.rtm_flags = RTF_MPATH;
1004
	hdr.rtm_seq = kr_state.rtseq++;	/* overflow doesn't matter */
1005
	hdr.rtm_hdrlen = sizeof(hdr);
1006
	hdr.rtm_msglen = sizeof(hdr);
1007
	/* adjust iovec */
1008
	iov[iovcnt].iov_base = &hdr;
1009
	iov[iovcnt++].iov_len = sizeof(hdr);
1010
1011
	bzero(&prefix, sizeof(prefix));
1012
	prefix.addr.sin6_len = sizeof(struct sockaddr_in6);
1013
	prefix.addr.sin6_family = AF_INET6;
1014
	prefix.addr.sin6_addr = kroute->prefix;
1015
	/* adjust header */
1016
	hdr.rtm_addrs |= RTA_DST;
1017
	hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1018
	/* adjust iovec */
1019
	iov[iovcnt].iov_base = &prefix;
1020
	iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1021
1022
	if (!IN6_IS_ADDR_UNSPECIFIED(&kroute->nexthop)) {
1023
		bzero(&nexthop, sizeof(nexthop));
1024
		nexthop.addr.sin6_len = sizeof(struct sockaddr_in6);
1025
		nexthop.addr.sin6_family = AF_INET6;
1026
		nexthop.addr.sin6_addr = kroute->nexthop;
1027
		nexthop.addr.sin6_scope_id = kroute->scope;
1028
		/*
1029
		 * XXX we should set the sin6_scope_id but the kernel
1030
		 * XXX does not expect it that way. It must be fiddled
1031
		 * XXX into the sin6_addr. Welcome to the typical
1032
		 * XXX IPv6 insanity and all without wine bottles.
1033
		 */
1034
		embedscope(&nexthop.addr);
1035
1036
		/* adjust header */
1037
		hdr.rtm_flags |= RTF_GATEWAY;
1038
		hdr.rtm_addrs |= RTA_GATEWAY;
1039
		hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1040
		/* adjust iovec */
1041
		iov[iovcnt].iov_base = &nexthop;
1042
		iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1043
	} else if (kroute->ifindex) {
1044
		/*
1045
		 * We don't have an interface address in that network,
1046
		 * so we install a cloning route.  The kernel will then
1047
		 * do neigbor discovery.
1048
		 */
1049
		bzero(&ifp, sizeof(ifp));
1050
		ifp.addr.sdl_len = sizeof(struct sockaddr_dl);
1051
		ifp.addr.sdl_family = AF_LINK;
1052
1053
		ifp.addr.sdl_index  = kroute->ifindex;
1054
		/* adjust header */
1055
		hdr.rtm_flags |= RTF_CLONING;
1056
		hdr.rtm_addrs |= RTA_GATEWAY;
1057
		hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_dl));
1058
		/* adjust iovec */
1059
		iov[iovcnt].iov_base = &ifp;
1060
		iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_dl));
1061
	}
1062
1063
	bzero(&mask, sizeof(mask));
1064
	mask.addr.sin6_len = sizeof(struct sockaddr_in6);
1065
	mask.addr.sin6_family = AF_INET6;
1066
	mask.addr.sin6_addr = *prefixlen2mask(kroute->prefixlen);
1067
	/* adjust header */
1068
	if (kroute->prefixlen == 128)
1069
		hdr.rtm_flags |= RTF_HOST;
1070
	hdr.rtm_addrs |= RTA_NETMASK;
1071
	hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
1072
	/* adjust iovec */
1073
	iov[iovcnt].iov_base = &mask;
1074
	iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
1075
1076
	if (kroute->rtlabel != 0) {
1077
		sa_rl.sr_len = sizeof(sa_rl);
1078
		sa_rl.sr_family = AF_UNSPEC;
1079
		label = rtlabel_id2name(kroute->rtlabel);
1080
		if (strlcpy(sa_rl.sr_label, label,
1081
		    sizeof(sa_rl.sr_label)) >= sizeof(sa_rl.sr_label)) {
1082
			log_warnx("send_rtmsg: invalid rtlabel");
1083
			return (-1);
1084
		}
1085
		/* adjust header */
1086
		hdr.rtm_addrs |= RTA_LABEL;
1087
		hdr.rtm_msglen += sizeof(sa_rl);
1088
		/* adjust iovec */
1089
		iov[iovcnt].iov_base = &sa_rl;
1090
		iov[iovcnt++].iov_len = sizeof(sa_rl);
1091
	}
1092
1093
retry:
1094
	if (writev(fd, iov, iovcnt) == -1) {
1095
		if (errno == ESRCH) {
1096
			if (hdr.rtm_type == RTM_CHANGE) {
1097
				hdr.rtm_type = RTM_ADD;
1098
				goto retry;
1099
			} else if (hdr.rtm_type == RTM_DELETE) {
1100
				log_info("route %s/%u vanished before delete",
1101
				    log_sockaddr(&prefix), kroute->prefixlen);
1102
				return (0);
1103
			}
1104
		}
1105
		log_warn("send_rtmsg: action %u, prefix %s/%u", hdr.rtm_type,
1106
		    log_sockaddr(&prefix), kroute->prefixlen);
1107
		return (0);
1108
	}
1109
1110
	return (0);
1111
}
1112
1113
int
1114
fetchtable(void)
1115
{
1116
	size_t			 len;
1117
	int			 mib[7];
1118
	char			*buf, *next, *lim;
1119
	struct rt_msghdr	*rtm;
1120
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1121
	struct sockaddr_in6	*sa_in6;
1122
	struct sockaddr_rtlabel	*label;
1123
	struct kroute_node	*kr;
1124
1125
	mib[0] = CTL_NET;
1126
	mib[1] = PF_ROUTE;
1127
	mib[2] = 0;
1128
	mib[3] = AF_INET6;
1129
	mib[4] = NET_RT_DUMP;
1130
	mib[5] = 0;
1131
	mib[6] = 0;	/* rtableid */
1132
1133
	if (sysctl(mib, 7, NULL, &len, NULL, 0) == -1) {
1134
		log_warn("sysctl");
1135
		return (-1);
1136
	}
1137
	if ((buf = malloc(len)) == NULL) {
1138
		log_warn("fetchtable");
1139
		return (-1);
1140
	}
1141
	if (sysctl(mib, 7, buf, &len, NULL, 0) == -1) {
1142
		log_warn("sysctl");
1143
		free(buf);
1144
		return (-1);
1145
	}
1146
1147
	lim = buf + len;
1148
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1149
		rtm = (struct rt_msghdr *)next;
1150
		if (rtm->rtm_version != RTM_VERSION)
1151
			continue;
1152
		sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1153
		get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1154
1155
		if ((sa = rti_info[RTAX_DST]) == NULL)
1156
			continue;
1157
1158
		/* Skip ARP/ND cache and broadcast routes. */
1159
		if (rtm->rtm_flags & (RTF_LLINFO|RTF_BROADCAST))
1160
			continue;
1161
1162
		if ((kr = calloc(1, sizeof(struct kroute_node))) == NULL) {
1163
			log_warn("fetchtable");
1164
			free(buf);
1165
			return (-1);
1166
		}
1167
1168
		kr->r.flags = F_KERNEL;
1169
		kr->r.priority = rtm->rtm_priority;
1170
1171
		switch (sa->sa_family) {
1172
		case AF_INET6:
1173
			kr->r.prefix =
1174
			    ((struct sockaddr_in6 *)sa)->sin6_addr;
1175
			sa_in6 = (struct sockaddr_in6 *)rti_info[RTAX_NETMASK];
1176
			if (rtm->rtm_flags & RTF_STATIC)
1177
				kr->r.flags |= F_STATIC;
1178
			if (rtm->rtm_flags & RTF_BLACKHOLE)
1179
				kr->r.flags |= F_BLACKHOLE;
1180
			if (rtm->rtm_flags & RTF_REJECT)
1181
				kr->r.flags |= F_REJECT;
1182
			if (rtm->rtm_flags & RTF_DYNAMIC)
1183
				kr->r.flags |= F_DYNAMIC;
1184
			if (sa_in6 != NULL) {
1185
				if (sa_in6->sin6_len == 0)
1186
					break;
1187
				kr->r.prefixlen =
1188
				    mask2prefixlen(sa_in6);
1189
			} else if (rtm->rtm_flags & RTF_HOST)
1190
				kr->r.prefixlen = 128;
1191
			else
1192
				fatalx("classful IPv6 route?!!");
1193
			break;
1194
		default:
1195
			free(kr);
1196
			continue;
1197
		}
1198
1199
		kr->r.ifindex = rtm->rtm_index;
1200
		if ((sa = rti_info[RTAX_GATEWAY]) != NULL)
1201
			switch (sa->sa_family) {
1202
			case AF_INET6:
1203
				if (rtm->rtm_flags & RTF_CONNECTED) {
1204
					kr->r.flags |= F_CONNECTED;
1205
					break;
1206
				}
1207
1208
				sa_in6 = (struct sockaddr_in6 *)sa;
1209
				/*
1210
				 * XXX The kernel provides the scope via the
1211
				 * XXX kame hack instead of the scope_id field.
1212
				 */
1213
				recoverscope(sa_in6);
1214
				kr->r.nexthop = sa_in6->sin6_addr;
1215
				kr->r.scope = sa_in6->sin6_scope_id;
1216
				break;
1217
			case AF_LINK:
1218
				/*
1219
				 * Traditional BSD connected routes have
1220
				 * a gateway of type AF_LINK.
1221
				 */
1222
				kr->r.flags |= F_CONNECTED;
1223
				break;
1224
			}
1225
1226
		if (rtm->rtm_priority == RTP_OSPF)  {
1227
			send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r);
1228
			free(kr);
1229
		} else {
1230
			if ((label = (struct sockaddr_rtlabel *)
1231
			    rti_info[RTAX_LABEL]) != NULL) {
1232
				kr->r.rtlabel =
1233
				    rtlabel_name2id(label->sr_label);
1234
				kr->r.ext_tag =
1235
				    rtlabel_id2tag(kr->r.rtlabel);
1236
			}
1237
			kroute_insert(kr);
1238
		}
1239
1240
	}
1241
	free(buf);
1242
	return (0);
1243
}
1244
1245
int
1246
fetchifs(u_short ifindex)
1247
{
1248
	size_t			 len;
1249
	int			 mib[6];
1250
	char			*buf, *next, *lim;
1251
	struct rt_msghdr	*rtm;
1252
	struct if_msghdr	 ifm;
1253
	struct ifa_msghdr	*ifam;
1254
	struct iface		*iface;
1255
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1256
1257
	mib[0] = CTL_NET;
1258
	mib[1] = PF_ROUTE;
1259
	mib[2] = 0;
1260
	mib[3] = AF_INET6;
1261
	mib[4] = NET_RT_IFLIST;
1262
	mib[5] = ifindex;
1263
1264
	if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
1265
		log_warn("sysctl");
1266
		return (-1);
1267
	}
1268
	if ((buf = malloc(len)) == NULL) {
1269
		log_warn("fetchifs");
1270
		return (-1);
1271
	}
1272
	if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
1273
		log_warn("sysctl");
1274
		free(buf);
1275
		return (-1);
1276
	}
1277
1278
	lim = buf + len;
1279
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1280
		rtm = (struct rt_msghdr *)next;
1281
		if (rtm->rtm_version != RTM_VERSION)
1282
			continue;
1283
		switch (rtm->rtm_type) {
1284
		case RTM_IFINFO:
1285
			bcopy(rtm, &ifm, sizeof ifm);
1286
			sa = (struct sockaddr *)(next + sizeof(ifm));
1287
			get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
1288
1289
			if ((iface = kif_update(ifm.ifm_index,
1290
			    ifm.ifm_flags, &ifm.ifm_data,
1291
			    (struct sockaddr_dl *)rti_info[RTAX_IFP])) == NULL)
1292
			break;
1293
		case RTM_NEWADDR:
1294
			ifam = (struct ifa_msghdr *)rtm;
1295
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1296
			    RTA_BRD)) == 0)
1297
				break;
1298
			sa = (struct sockaddr *)(ifam + 1);
1299
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1300
1301
			if_newaddr(ifam->ifam_index,
1302
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1303
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1304
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1305
			break;
1306
		}
1307
	}
1308
	free(buf);
1309
	return (0);
1310
}
1311
1312
int
1313
dispatch_rtmsg(void)
1314
{
1315
	char			 buf[RT_BUF_SIZE];
1316
	ssize_t			 n;
1317
	char			*next, *lim;
1318
	struct rt_msghdr	*rtm;
1319
	struct if_msghdr	 ifm;
1320
	struct ifa_msghdr	*ifam;
1321
	struct sockaddr		*sa, *rti_info[RTAX_MAX];
1322
	struct sockaddr_in6	*sa_in6;
1323
	struct sockaddr_rtlabel	*label;
1324
	struct kroute_node	*kr, *okr;
1325
	struct in6_addr		 prefix, nexthop;
1326
	u_int8_t		 prefixlen, prio;
1327
	int			 flags, mpath;
1328
	unsigned int		 scope;
1329
	u_short			 ifindex = 0;
1330
1331
	if ((n = read(kr_state.fd, &buf, sizeof(buf))) == -1) {
1332
		if (errno == EAGAIN || errno == EINTR)
1333
			return (0);
1334
		log_warn("dispatch_rtmsg: read error");
1335
		return (-1);
1336
	}
1337
1338
	if (n == 0) {
1339
		log_warnx("routing socket closed");
1340
		return (-1);
1341
	}
1342
1343
	lim = buf + n;
1344
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1345
		rtm = (struct rt_msghdr *)next;
1346
		if (lim < next + sizeof(u_short) ||
1347
		    lim < next + rtm->rtm_msglen)
1348
			fatalx("dispatch_rtmsg: partial rtm in buffer");
1349
		if (rtm->rtm_version != RTM_VERSION)
1350
			continue;
1351
1352
		bzero(&prefix, sizeof(prefix));
1353
		bzero(&nexthop, sizeof(nexthop));
1354
		scope = 0;
1355
		prefixlen = 0;
1356
		flags = F_KERNEL;
1357
		mpath = 0;
1358
		prio = 0;
1359
1360
		if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE ||
1361
		    rtm->rtm_type == RTM_DELETE) {
1362
			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1363
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1364
1365
			if (rtm->rtm_tableid != 0)
1366
				continue;
1367
1368
			if (rtm->rtm_pid == kr_state.pid) /* caused by us */
1369
				continue;
1370
1371
			if (rtm->rtm_errno)		/* failed attempts... */
1372
				continue;
1373
1374
			/* Skip ARP/ND cache and broadcast routes. */
1375
			if (rtm->rtm_flags & (RTF_LLINFO|RTF_BROADCAST))
1376
				continue;
1377
1378
			if (rtm->rtm_flags & RTF_MPATH)
1379
				mpath = 1;
1380
			prio = rtm->rtm_priority;
1381
1382
			switch (sa->sa_family) {
1383
			case AF_INET6:
1384
				prefix =
1385
				    ((struct sockaddr_in6 *)sa)->sin6_addr;
1386
				sa_in6 = (struct sockaddr_in6 *)
1387
				    rti_info[RTAX_NETMASK];
1388
				if (sa_in6 != NULL) {
1389
					if (sa_in6->sin6_len != 0)
1390
						prefixlen = mask2prefixlen(
1391
						    sa_in6);
1392
				} else if (rtm->rtm_flags & RTF_HOST)
1393
					prefixlen = 128;
1394
				else
1395
					fatalx("classful IPv6 address?!!");
1396
				if (rtm->rtm_flags & RTF_STATIC)
1397
					flags |= F_STATIC;
1398
				if (rtm->rtm_flags & RTF_BLACKHOLE)
1399
					flags |= F_BLACKHOLE;
1400
				if (rtm->rtm_flags & RTF_REJECT)
1401
					flags |= F_REJECT;
1402
				if (rtm->rtm_flags & RTF_DYNAMIC)
1403
					flags |= F_DYNAMIC;
1404
				break;
1405
			default:
1406
				continue;
1407
			}
1408
1409
			ifindex = rtm->rtm_index;
1410
			if ((sa = rti_info[RTAX_GATEWAY]) != NULL) {
1411
				switch (sa->sa_family) {
1412
				case AF_INET6:
1413
					sa_in6 = (struct sockaddr_in6 *)sa;
1414
					/*
1415
					 * XXX The kernel provides the scope
1416
					 * XXX via the kame hack instead of
1417
					 * XXX the scope_id field.
1418
					 */
1419
					recoverscope(sa_in6);
1420
					nexthop = sa_in6->sin6_addr;
1421
					scope = sa_in6->sin6_scope_id;
1422
					break;
1423
				case AF_LINK:
1424
					flags |= F_CONNECTED;
1425
					break;
1426
				}
1427
			}
1428
		}
1429
1430
		switch (rtm->rtm_type) {
1431
		case RTM_ADD:
1432
		case RTM_CHANGE:
1433
			if (IN6_IS_ADDR_UNSPECIFIED(&nexthop) &&
1434
			    !(flags & F_CONNECTED)) {
1435
				log_warnx("dispatch_rtmsg no nexthop for %s/%u",
1436
				    log_in6addr(&prefix), prefixlen);
1437
				continue;
1438
			}
1439
1440
			if ((okr = kroute_find(&prefix, prefixlen, prio))
1441
			    != NULL) {
1442
				/* just add new multipath routes */
1443
				if (mpath && rtm->rtm_type == RTM_ADD)
1444
					goto add;
1445
				/* get the correct route */
1446
				kr = okr;
1447
				if (mpath && (kr = kroute_matchgw(okr,
1448
				    &nexthop, scope)) == NULL) {
1449
					log_warnx("dispatch_rtmsg mpath route"
1450
					    " not found");
1451
					/* add routes we missed out earlier */
1452
					goto add;
1453
				}
1454
1455
				if (kr->r.flags & F_REDISTRIBUTED)
1456
					flags |= F_REDISTRIBUTED;
1457
				kr->r.nexthop = nexthop;
1458
				kr->r.scope = scope;
1459
				kr->r.flags = flags;
1460
				kr->r.ifindex = ifindex;
1461
1462
				rtlabel_unref(kr->r.rtlabel);
1463
				kr->r.rtlabel = 0;
1464
				kr->r.ext_tag = 0;
1465
				if ((label = (struct sockaddr_rtlabel *)
1466
				    rti_info[RTAX_LABEL]) != NULL) {
1467
					kr->r.rtlabel =
1468
					    rtlabel_name2id(label->sr_label);
1469
					kr->r.ext_tag =
1470
					    rtlabel_id2tag(kr->r.rtlabel);
1471
				}
1472
1473
				if (kif_validate(kr->r.ifindex))
1474
					kr->r.flags &= ~F_DOWN;
1475
				else
1476
					kr->r.flags |= F_DOWN;
1477
1478
				/* just readd, the RDE will care */
1479
				kr_redistribute(okr);
1480
			} else {
1481
add:
1482
				if ((kr = calloc(1,
1483
				    sizeof(struct kroute_node))) == NULL) {
1484
					log_warn("dispatch_rtmsg");
1485
					return (-1);
1486
				}
1487
				kr->r.prefix = prefix;
1488
				kr->r.prefixlen = prefixlen;
1489
				kr->r.nexthop = nexthop;
1490
				kr->r.scope = scope;
1491
				kr->r.flags = flags;
1492
				kr->r.ifindex = ifindex;
1493
				kr->r.priority = prio;
1494
1495
				if ((label = (struct sockaddr_rtlabel *)
1496
				    rti_info[RTAX_LABEL]) != NULL) {
1497
					kr->r.rtlabel =
1498
					    rtlabel_name2id(label->sr_label);
1499
					kr->r.ext_tag =
1500
					    rtlabel_id2tag(kr->r.rtlabel);
1501
				}
1502
1503
				kroute_insert(kr);
1504
			}
1505
			break;
1506
		case RTM_DELETE:
1507
			if ((kr = kroute_find(&prefix, prefixlen, prio)) ==
1508
			    NULL)
1509
				continue;
1510
			if (!(kr->r.flags & F_KERNEL))
1511
				continue;
1512
			/* get the correct route */
1513
			okr = kr;
1514
			if (mpath && (kr = kroute_matchgw(kr, &nexthop,
1515
			    scope)) == NULL) {
1516
				log_warnx("dispatch_rtmsg mpath route"
1517
				    " not found");
1518
				return (-1);
1519
			}
1520
			if (kroute_remove(kr) == -1)
1521
				return (-1);
1522
			break;
1523
		case RTM_IFINFO:
1524
			memcpy(&ifm, next, sizeof(ifm));
1525
			if_change(ifm.ifm_index, ifm.ifm_flags,
1526
			    &ifm.ifm_data);
1527
			break;
1528
		case RTM_NEWADDR:
1529
			ifam = (struct ifa_msghdr *)rtm;
1530
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1531
			    RTA_BRD)) == 0)
1532
				break;
1533
			sa = (struct sockaddr *)(ifam + 1);
1534
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1535
1536
			if_newaddr(ifam->ifam_index,
1537
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1538
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1539
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1540
			break;
1541
		case RTM_DELADDR:
1542
			ifam = (struct ifa_msghdr *)rtm;
1543
			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
1544
			    RTA_BRD)) == 0)
1545
				break;
1546
			sa = (struct sockaddr *)(ifam + 1);
1547
			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
1548
1549
			if_deladdr(ifam->ifam_index,
1550
			    (struct sockaddr_in6 *)rti_info[RTAX_IFA],
1551
			    (struct sockaddr_in6 *)rti_info[RTAX_NETMASK],
1552
			    (struct sockaddr_in6 *)rti_info[RTAX_BRD]);
1553
			break;
1554
		case RTM_IFANNOUNCE:
1555
			if_announce(next);
1556
			break;
1557
		default:
1558
			/* ignore for now */
1559
			break;
1560
		}
1561
	}
1562
	return (0);
1563
}