summaryrefslogtreecommitdiff
path: root/usr.sbin/eigrpd/rde.c
blob: bb9d32b96314435faeae734be4ab8dbe269b47ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*	$OpenBSD: rde.c,v 1.18 2016/08/08 16:48:53 renato Exp $ */

/*
 * Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <pwd.h>

#include "eigrp.h"
#include "eigrpd.h"
#include "eigrpe.h"
#include "log.h"
#include "rde.h"

void		 rde_sig_handler(int sig, short, void *);
void		 rde_shutdown(void);
void		 rde_dispatch_imsg(int, short, void *);
void		 rde_dispatch_parent(int, short, void *);

struct eigrpd_conf	*rdeconf = NULL, *nconf;
struct imsgev		*iev_eigrpe;
struct imsgev		*iev_main;

extern struct iface_id_head ifaces_by_id;
RB_PROTOTYPE(iface_id_head, eigrp_iface, id_tree, iface_id_compare)

RB_PROTOTYPE(rt_tree, rt_node, entry, rt_compare)

extern struct rde_nbr_head rde_nbrs;
RB_PROTOTYPE(rde_nbr_head, rde_nbr, entry, rde_nbr_compare)

/* ARGSUSED */
void
rde_sig_handler(int sig, short event, void *arg)
{
	/*
	 * signal handler rules don't apply, libevent decouples for us
	 */

	switch (sig) {
	case SIGINT:
	case SIGTERM:
		rde_shutdown();
		/* NOTREACHED */
	default:
		fatalx("unexpected signal");
	}
}

/* route decision engine */
pid_t
rde(int debug, int verbose)
{
	struct event		 ev_sigint, ev_sigterm;
	struct timeval		 now;
	struct passwd		*pw;

	rdeconf = config_new_empty();

	log_init(debug);
	log_verbose(verbose);

	if ((pw = getpwnam(EIGRPD_USER)) == NULL)
		fatal("getpwnam");

	if (chroot(pw->pw_dir) == -1)
		fatal("chroot");
	if (chdir("/") == -1)
		fatal("chdir(\"/\")");

	setproctitle("route decision engine");
	eigrpd_process = PROC_RDE_ENGINE;

	if (setgroups(1, &pw->pw_gid) ||
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
		fatal("can't drop privileges");

	if (pledge("stdio recvfd", NULL) == -1)
		fatal("pledge");

	event_init();

	/* setup signal handler */
	signal_set(&ev_sigint, SIGINT, rde_sig_handler, NULL);
	signal_set(&ev_sigterm, SIGTERM, rde_sig_handler, NULL);
	signal_add(&ev_sigint, NULL);
	signal_add(&ev_sigterm, NULL);
	signal(SIGPIPE, SIG_IGN);
	signal(SIGHUP, SIG_IGN);

	/* setup pipe and event handler to the parent process */
	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
		fatal(NULL);
	imsg_init(&iev_main->ibuf, 3);
	iev_main->handler = rde_dispatch_parent;
	iev_main->events = EV_READ;
	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
	    iev_main->handler, iev_main);
	event_add(&iev_main->ev, NULL);

	gettimeofday(&now, NULL);
	global.uptime = now.tv_sec;

	event_dispatch();

	rde_shutdown();
	/* NOTREACHED */

	return (0);
}

void
rde_shutdown(void)
{
	config_clear(rdeconf);

	msgbuf_clear(&iev_eigrpe->ibuf.w);
	free(iev_eigrpe);
	msgbuf_clear(&iev_main->ibuf.w);
	free(iev_main);

	log_info("route decision engine exiting");
	exit(0);
}

int
rde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen)
{
	return (imsg_compose_event(iev_main, type, 0, pid, -1,
	    data, datalen));
}

int
rde_imsg_compose_eigrpe(int type, uint32_t peerid, pid_t pid, void *data,
    uint16_t datalen)
{
	return (imsg_compose_event(iev_eigrpe, type, peerid, pid, -1,
	    data, datalen));
}

/* ARGSUSED */
void
rde_dispatch_imsg(int fd, short event, void *bula)
{
	struct imsgev		*iev = bula;
	struct imsgbuf		*ibuf;
	struct imsg		 imsg;
	struct rde_nbr		*nbr;
	struct rde_nbr		 new;
	struct rinfo		 rinfo;
	ssize_t			 n;
	int			 shut = 0, verbose;

	ibuf = &iev->ibuf;

	if (event & EV_READ) {
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
			fatal("imsg_read error");
		if (n == 0)	/* connection closed */
			shut = 1;
	}
	if (event & EV_WRITE) {
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
			fatal("msgbuf_write");
		if (n == 0)	/* connection closed */
			shut = 1;
	}

	for (;;) {
		if ((n = imsg_get(ibuf, &imsg)) == -1)
			fatal("rde_dispatch_imsg: imsg_get error");
		if (n == 0)
			break;

		switch (imsg.hdr.type) {
		case IMSG_NEIGHBOR_UP:
			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
			    sizeof(struct rde_nbr))
				fatalx("invalid size of neighbor request");
			memcpy(&new, imsg.data, sizeof(new));

			if (rde_nbr_find(imsg.hdr.peerid))
				fatalx("rde_dispatch_imsg: "
				    "neighbor already exists");
			rde_nbr_new(imsg.hdr.peerid, &new);
			break;
		case IMSG_NEIGHBOR_DOWN:
			nbr = rde_nbr_find(imsg.hdr.peerid);
			if (nbr == NULL) {
				log_debug("%s: cannot find rde neighbor",
				    __func__);
				break;
			}

			rde_check_link_down_nbr(nbr);
			rde_flush_queries();
			rde_nbr_del(rde_nbr_find(imsg.hdr.peerid), 0);
			break;
		case IMSG_RECV_UPDATE_INIT:
			nbr = rde_nbr_find(imsg.hdr.peerid);
			if (nbr == NULL) {
				log_debug("%s: cannot find rde neighbor",
				    __func__);
				break;
			}

			rt_snap(nbr);
			break;
		case IMSG_RECV_UPDATE:
		case IMSG_RECV_QUERY:
		case IMSG_RECV_REPLY:
		case IMSG_RECV_SIAQUERY:
		case IMSG_RECV_SIAREPLY:
			nbr = rde_nbr_find(imsg.hdr.peerid);
			if (nbr == NULL) {
				log_debug("%s: cannot find rde neighbor",
				    __func__);
				break;
			}

			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rinfo))
				fatalx("invalid size of rinfo");
			memcpy(&rinfo, imsg.data, sizeof(rinfo));

			switch (imsg.hdr.type) {
			case IMSG_RECV_UPDATE:
				rde_check_update(nbr, &rinfo);
				break;
			case IMSG_RECV_QUERY:
				rde_check_query(nbr, &rinfo, 0);
				break;
			case IMSG_RECV_REPLY:
				rde_check_reply(nbr, &rinfo, 0);
				break;
			case IMSG_RECV_SIAQUERY:
				rde_check_query(nbr, &rinfo, 1);
				break;
			case IMSG_RECV_SIAREPLY:
				rde_check_reply(nbr, &rinfo, 1);
				break;
			}
			break;
		case IMSG_CTL_SHOW_TOPOLOGY:
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
			    sizeof(struct ctl_show_topology_req)) {
				log_warnx("%s: wrong imsg len", __func__);
				break;
			}

			rt_dump(imsg.data, imsg.hdr.pid);
			rde_imsg_compose_eigrpe(IMSG_CTL_END, 0, imsg.hdr.pid,
			    NULL, 0);
			break;
		case IMSG_CTL_LOG_VERBOSE:
			/* already checked by eigrpe */
			memcpy(&verbose, imsg.data, sizeof(verbose));
			log_verbose(verbose);
			break;
		default:
			log_debug("rde_dispatch_imsg: unexpected imsg %d",
			    imsg.hdr.type);
			break;
		}
		imsg_free(&imsg);
	}
	if (!shut)
		imsg_event_add(iev);
	else {
		/* this pipe is dead, so remove the event handler */
		event_del(&iev->ev);
		event_loopexit(NULL);
	}
}

/* ARGSUSED */
void
rde_dispatch_parent(int fd, short event, void *bula)
{
	static struct iface	*niface = NULL;
	static struct eigrp	*neigrp;
	struct eigrp_iface	*nei;
	struct imsg		 imsg;
	struct imsgev		*iev = bula;
	struct imsgbuf		*ibuf;
	struct kif		*kif;
	ssize_t			 n;
	int			 shut = 0;

	ibuf = &iev->ibuf;

	if (event & EV_READ) {
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
			fatal("imsg_read error");
		if (n == 0)	/* connection closed */
			shut = 1;
	}
	if (event & EV_WRITE) {
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
			fatal("msgbuf_write");
		if (n == 0)	/* connection closed */
			shut = 1;
	}

	for (;;) {
		if ((n = imsg_get(ibuf, &imsg)) == -1)
			fatal("rde_dispatch_parent: imsg_get error");
		if (n == 0)
			break;

		switch (imsg.hdr.type) {
		case IMSG_IFDOWN:
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
			    sizeof(struct kif))
				fatalx("IFDOWN imsg with wrong len");
			kif = imsg.data;
			rde_check_link_down(kif->ifindex);
			break;
		case IMSG_NETWORK_ADD:
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
			    sizeof(struct kroute))
				fatalx("IMSG_NETWORK_ADD imsg with wrong len");
			rt_redist_set(imsg.data, 0);
			break;
		case IMSG_NETWORK_DEL:
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
			    sizeof(struct kroute))
				fatalx("IMSG_NETWORK_DEL imsg with wrong len");
			rt_redist_set(imsg.data, 1);
			break;
		case IMSG_SOCKET_IPC:
			if (iev_eigrpe) {
				log_warnx("%s: received unexpected imsg fd "
				    "to eigrpe", __func__);
				break;
			}
			if ((fd = imsg.fd) == -1) {
				log_warnx("%s: expected to receive imsg fd to "
				    "eigrpe but didn't receive any", __func__);
				break;
			}

			iev_eigrpe = malloc(sizeof(struct imsgev));
			if (iev_eigrpe == NULL)
				fatal(NULL);
			imsg_init(&iev_eigrpe->ibuf, fd);
			iev_eigrpe->handler = rde_dispatch_imsg;
			iev_eigrpe->events = EV_READ;
			event_set(&iev_eigrpe->ev, iev_eigrpe->ibuf.fd,
			    iev_eigrpe->events, iev_eigrpe->handler,
			    iev_eigrpe);
			event_add(&iev_eigrpe->ev, NULL);
			break;
		case IMSG_RECONF_CONF:
			if ((nconf = malloc(sizeof(struct eigrpd_conf))) ==
			    NULL)
				fatal(NULL);
			memcpy(nconf, imsg.data, sizeof(struct eigrpd_conf));

			TAILQ_INIT(&nconf->iface_list);
			TAILQ_INIT(&nconf->instances);
			break;
		case IMSG_RECONF_INSTANCE:
			if ((neigrp = malloc(sizeof(struct eigrp))) == NULL)
				fatal(NULL);
			memcpy(neigrp, imsg.data, sizeof(struct eigrp));

			SIMPLEQ_INIT(&neigrp->redist_list);
			TAILQ_INIT(&neigrp->ei_list);
			RB_INIT(&neigrp->nbrs);
			RB_INIT(&neigrp->topology);
			TAILQ_INSERT_TAIL(&nconf->instances, neigrp, entry);
			break;
		case IMSG_RECONF_IFACE:
			niface = imsg.data;
			niface = if_lookup(nconf, niface->ifindex);
			if (niface)
				break;

			if ((niface = malloc(sizeof(struct iface))) == NULL)
				fatal(NULL);
			memcpy(niface, imsg.data, sizeof(struct iface));

			TAILQ_INIT(&niface->ei_list);
			TAILQ_INIT(&niface->addr_list);
			TAILQ_INSERT_TAIL(&nconf->iface_list, niface, entry);
			break;
		case IMSG_RECONF_EIGRP_IFACE:
			if (niface == NULL)
				break;
			if ((nei = malloc(sizeof(struct eigrp_iface))) == NULL)
				fatal(NULL);
			memcpy(nei, imsg.data, sizeof(struct eigrp_iface));

			nei->iface = niface;
			nei->eigrp = neigrp;
			TAILQ_INIT(&nei->nbr_list);
			TAILQ_INIT(&nei->update_list);
			TAILQ_INIT(&nei->query_list);
			TAILQ_INIT(&nei->summary_list);
			TAILQ_INSERT_TAIL(&niface->ei_list, nei, i_entry);
			TAILQ_INSERT_TAIL(&neigrp->ei_list, nei, e_entry);
			if (RB_INSERT(iface_id_head, &ifaces_by_id, nei) !=
			    NULL)
				fatalx("rde_dispatch_parent: "
				    "RB_INSERT(ifaces_by_id) failed");
			break;
		case IMSG_RECONF_END:
			merge_config(rdeconf, nconf);
			nconf = NULL;
			break;
		default:
			log_debug("%s: unexpected imsg %d", __func__,
			    imsg.hdr.type);
			break;
		}
		imsg_free(&imsg);
	}
	if (!shut)
		imsg_event_add(iev);
	else {
		/* this pipe is dead, so remove the event handler */
		event_del(&iev->ev);
		event_loopexit(NULL);
	}
}

void
rde_instance_init(struct eigrp *eigrp)
{
	struct rde_nbr		 nbr;

	memset(&nbr, 0, sizeof(nbr));
	nbr.flags = F_RDE_NBR_SELF | F_RDE_NBR_REDIST;
	eigrp->rnbr_redist = rde_nbr_new(NBR_IDSELF, &nbr);
	eigrp->rnbr_redist->eigrp = eigrp;
	nbr.flags = F_RDE_NBR_SELF | F_RDE_NBR_SUMMARY;
	eigrp->rnbr_summary = rde_nbr_new(NBR_IDSELF, &nbr);
	eigrp->rnbr_summary->eigrp = eigrp;
}

void
rde_instance_del(struct eigrp *eigrp)
{
	struct rde_nbr		*nbr, *safe;
	struct rt_node		*rn;

	/* clear topology */
	while((rn = RB_MIN(rt_tree, &eigrp->topology)) != NULL)
		rt_del(rn);

	/* clear nbrs */
	RB_FOREACH_SAFE(nbr, rde_nbr_head, &rde_nbrs, safe)
		if (nbr->eigrp == eigrp)
			rde_nbr_del(nbr, 0);
	rde_nbr_del(eigrp->rnbr_redist, 0);
	rde_nbr_del(eigrp->rnbr_summary, 0);

	free(eigrp);
}

void
rde_send_change_kroute(struct rt_node *rn, struct eigrp_route *route)
{
	struct eigrp	*eigrp = route->nbr->eigrp;
	struct kroute	 kr;
	struct in6_addr	 lo6 = IN6ADDR_LOOPBACK_INIT;

	log_debug("%s: %s nbr %s", __func__, log_prefix(rn),
	    log_addr(eigrp->af, &route->nbr->addr));

	memset(&kr, 0, sizeof(kr));
	kr.af = eigrp->af;
	kr.prefix = rn->prefix;
	kr.prefixlen = rn->prefixlen;
	if (route->nbr->ei) {
		kr.nexthop = route->nexthop;
		kr.ifindex = route->nbr->ei->iface->ifindex;
	} else {
		switch (eigrp->af) {
		case AF_INET:
			kr.nexthop.v4.s_addr = htonl(INADDR_LOOPBACK);
			break;
		case AF_INET6:
			kr.nexthop.v6 = lo6;
			break;
		default:
			fatalx("rde_send_delete_kroute: unknown af");
			break;
		}
		kr.flags = F_BLACKHOLE;
	}
	if (route->type == EIGRP_ROUTE_EXTERNAL)
		kr.priority = rdeconf->fib_priority_external;
	else {
		if (route->nbr->flags & F_RDE_NBR_SUMMARY)
			kr.priority = rdeconf->fib_priority_summary;
		else
			kr.priority = rdeconf->fib_priority_internal;
	}

	rde_imsg_compose_parent(IMSG_KROUTE_CHANGE, 0, &kr, sizeof(kr));

	route->flags |= F_EIGRP_ROUTE_INSTALLED;
}

void
rde_send_delete_kroute(struct rt_node *rn, struct eigrp_route *route)
{
	struct eigrp	*eigrp = route->nbr->eigrp;
	struct kroute	 kr;
	struct in6_addr	 lo6 = IN6ADDR_LOOPBACK_INIT;

	log_debug("%s: %s nbr %s", __func__, log_prefix(rn),
	    log_addr(eigrp->af, &route->nbr->addr));

	memset(&kr, 0, sizeof(kr));
	kr.af = eigrp->af;
	kr.prefix = rn->prefix;
	kr.prefixlen = rn->prefixlen;
	if (route->nbr->ei) {
		kr.nexthop = route->nexthop;
		kr.ifindex = route->nbr->ei->iface->ifindex;
	} else {
		switch (eigrp->af) {
		case AF_INET:
			kr.nexthop.v4.s_addr = htonl(INADDR_LOOPBACK);
			break;
		case AF_INET6:
			kr.nexthop.v6 = lo6;
			break;
		default:
			fatalx("rde_send_delete_kroute: unknown af");
			break;
		}
		kr.flags = F_BLACKHOLE;
	}
	if (route->type == EIGRP_ROUTE_EXTERNAL)
		kr.priority = rdeconf->fib_priority_external;
	else {
		if (route->nbr->flags & F_RDE_NBR_SUMMARY)
			kr.priority = rdeconf->fib_priority_summary;
		else
			kr.priority = rdeconf->fib_priority_internal;
	}

	rde_imsg_compose_parent(IMSG_KROUTE_DELETE, 0, &kr, sizeof(kr));

	route->flags &= ~F_EIGRP_ROUTE_INSTALLED;
}

static struct redistribute *
eigrp_redistribute(struct eigrp *eigrp, struct kroute *kr)
{
	struct redistribute	*r;
	uint8_t			 is_default = 0;
	union eigrpd_addr	 addr;

	/* only allow the default route via REDIST_DEFAULT */
	if (!eigrp_addrisset(kr->af, &kr->prefix) && kr->prefixlen == 0)
		is_default = 1;

	SIMPLEQ_FOREACH(r, &eigrp->redist_list, entry) {
		switch (r->type & ~REDIST_NO) {
		case REDIST_STATIC:
			if (is_default)
				continue;
			if (kr->flags & F_STATIC)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		case REDIST_RIP:
			if (is_default)
				continue;
			if (kr->priority == RTP_RIP)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		case REDIST_OSPF:
			if (is_default)
				continue;
			if (kr->priority == RTP_OSPF)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		case REDIST_CONNECTED:
			if (is_default)
				continue;
			if (kr->flags & F_CONNECTED)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		case REDIST_ADDR:
			if (eigrp_addrisset(r->af, &r->addr) &&
			    r->prefixlen == 0) {
				if (is_default)
					return (r->type & REDIST_NO ? NULL : r);
				else
					return (0);
			}

			eigrp_applymask(kr->af, &addr, &kr->prefix,
			    r->prefixlen);
			if (eigrp_addrcmp(kr->af, &addr, &r->addr) == 0 &&
			    kr->prefixlen >= r->prefixlen)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		case REDIST_DEFAULT:
			if (is_default)
				return (r->type & REDIST_NO ? NULL : r);
			break;
		}
	}

	return (NULL);
}

void
rt_redist_set(struct kroute *kr, int withdraw)
{
	struct eigrp		*eigrp;
	struct redistribute	*r;
	struct redist_metric	*rmetric;
	struct rinfo		 ri;

	TAILQ_FOREACH(eigrp, &rdeconf->instances, entry) {
		if (eigrp->af != kr->af)
			continue;

		r = eigrp_redistribute(eigrp, kr);
		if (r == NULL)
			continue;

		if (r->metric)
			rmetric = r->metric;
		else if (eigrp->dflt_metric)
			rmetric = eigrp->dflt_metric;
		else
			continue;

		memset(&ri, 0, sizeof(ri));
		ri.af = kr->af;
		ri.type = EIGRP_ROUTE_EXTERNAL;
		ri.prefix = kr->prefix;
		ri.prefixlen = kr->prefixlen;

		/* metric */
		if (withdraw)
			ri.metric.delay = EIGRP_INFINITE_METRIC;
		else
			ri.metric.delay = eigrp_composite_delay(rmetric->delay);
		ri.metric.bandwidth =
		    eigrp_composite_bandwidth(rmetric->bandwidth);
		metric_encode_mtu(ri.metric.mtu, rmetric->mtu);
		ri.metric.hop_count = 0;
		ri.metric.reliability = rmetric->reliability;
		ri.metric.load = rmetric->load;
		ri.metric.tag = 0;
		ri.metric.flags = 0;

		/* external metric */
		ri.emetric.routerid = htonl(eigrp_router_id(rdeconf));
		ri.emetric.as = r->emetric.as;
		ri.emetric.tag = r->emetric.tag;
		ri.emetric.metric = r->emetric.metric;
		if (kr->priority == rdeconf->fib_priority_internal)
			ri.emetric.protocol = EIGRP_EXT_PROTO_EIGRP;
		else if (kr->priority == RTP_STATIC)
			ri.emetric.protocol = EIGRP_EXT_PROTO_STATIC;
		else if (kr->priority == RTP_RIP)
			ri.emetric.protocol = EIGRP_EXT_PROTO_RIP;
		else if (kr->priority == RTP_OSPF)
			ri.emetric.protocol = EIGRP_EXT_PROTO_OSPF;
		else
			ri.emetric.protocol = EIGRP_EXT_PROTO_CONN;
		ri.emetric.flags = 0;

		rde_check_update(eigrp->rnbr_redist, &ri);
	}
}

void
rt_summary_set(struct eigrp *eigrp, struct summary_addr *summary,
    struct classic_metric *metric)
{
	struct rinfo		 ri;

	memset(&ri, 0, sizeof(ri));
	ri.af = eigrp->af;
	ri.type = EIGRP_ROUTE_INTERNAL;
	ri.prefix = summary->prefix;
	ri.prefixlen = summary->prefixlen;
	ri.metric = *metric;

	rde_check_update(eigrp->rnbr_summary, &ri);
}

/* send all known routing information to new neighbor */
void
rt_snap(struct rde_nbr *nbr)
{
	struct eigrp		*eigrp = nbr->eigrp;
	struct rt_node		*rn;
	struct rinfo		 ri;

	RB_FOREACH(rn, rt_tree, &eigrp->topology)
		if (rn->state == DUAL_STA_PASSIVE &&
		    !rde_summary_check(nbr->ei, &rn->prefix, rn->prefixlen)) {
			rinfo_fill_successor(rn, &ri);
			rde_imsg_compose_eigrpe(IMSG_SEND_UPDATE,
			    nbr->peerid, 0, &ri, sizeof(ri));
		}

	rde_imsg_compose_eigrpe(IMSG_SEND_UPDATE_END, nbr->peerid, 0,
	    NULL, 0);
}

struct ctl_rt *
rt_to_ctl(struct rt_node *rn, struct eigrp_route *route)
{
	static struct ctl_rt	 rtctl;

	memset(&rtctl, 0, sizeof(rtctl));
	rtctl.af = route->nbr->eigrp->af;
	rtctl.as = route->nbr->eigrp->as;
	rtctl.prefix = rn->prefix;
	rtctl.prefixlen = rn->prefixlen;
	rtctl.type = route->type;
	rtctl.nexthop = route->nexthop;
	if (route->nbr->flags & F_RDE_NBR_REDIST)
		strlcpy(rtctl.ifname, "redistribute", sizeof(rtctl.ifname));
	else if (route->nbr->flags & F_RDE_NBR_SUMMARY)
		strlcpy(rtctl.ifname, "summary", sizeof(rtctl.ifname));
	else
		memcpy(rtctl.ifname, route->nbr->ei->iface->name,
		    sizeof(rtctl.ifname));
	rtctl.distance = route->distance;
	rtctl.rdistance = route->rdistance;
	rtctl.fdistance = rn->successor.fdistance;
	rtctl.state = rn->state;
	/* metric */
	rtctl.metric.delay = eigrp_real_delay(route->metric.delay);
	/* translate to microseconds */
	rtctl.metric.delay *= 10;
	rtctl.metric.bandwidth = eigrp_real_bandwidth(route->metric.bandwidth);
	rtctl.metric.mtu = metric_decode_mtu(route->metric.mtu);
	rtctl.metric.hop_count = route->metric.hop_count;
	rtctl.metric.reliability = route->metric.reliability;
	rtctl.metric.load = route->metric.load;
	/* external metric */
	rtctl.emetric = route->emetric;

	if (route->nbr == rn->successor.nbr)
		rtctl.flags |= F_CTL_RT_SUCCESSOR;
	else if (route->rdistance < rn->successor.fdistance)
		rtctl.flags |= F_CTL_RT_FSUCCESSOR;

	return (&rtctl);
}

void
rt_dump(struct ctl_show_topology_req *treq, pid_t pid)
{
	struct eigrp		*eigrp;
	struct rt_node		*rn;
	struct eigrp_route	*route;
	struct ctl_rt		*rtctl;
	int			 first = 1;

	TAILQ_FOREACH(eigrp, &rdeconf->instances, entry) {
		RB_FOREACH(rn, rt_tree, &eigrp->topology) {
			if (eigrp_addrisset(treq->af, &treq->prefix) &&
			    eigrp_addrcmp(treq->af, &treq->prefix,
			    &rn->prefix))
				continue;

			if (treq->prefixlen &&
			    (treq->prefixlen != rn->prefixlen))
				continue;

			first = 1;
			TAILQ_FOREACH(route, &rn->routes, entry) {
				if (treq->flags & F_CTL_ACTIVE &&
				    !(rn->state & DUAL_STA_ACTIVE_ALL))
					continue;
				if (!(treq->flags & F_CTL_ALLLINKS) &&
				    route->rdistance >= rn->successor.fdistance)
					continue;

				rtctl = rt_to_ctl(rn, route);
				if (first) {
					rtctl->flags |= F_CTL_RT_FIRST;
					first = 0;
				}
				rde_imsg_compose_eigrpe(IMSG_CTL_SHOW_TOPOLOGY,
				    0, pid, rtctl, sizeof(*rtctl));
			}
		}
	}
}