bgpd.c revision 1.145
1/*	$OpenBSD: bgpd.c,v 1.145 2008/05/12 19:15:02 pyr Exp $ */
2
3/*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <sys/wait.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <err.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <poll.h>
28#include <pwd.h>
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "mrt.h"
36#include "bgpd.h"
37#include "session.h"
38
39void		sighdlr(int);
40__dead void	usage(void);
41int		main(int, char *[]);
42int		check_child(pid_t, const char *);
43int		send_filterset(struct imsgbuf *, struct filter_set_head *);
44int		reconfigure(char *, struct bgpd_config *, struct mrt_head *,
45		    struct peer **, struct filter_head *);
46int		dispatch_imsg(struct imsgbuf *, int);
47
48int			 rfd = -1;
49int			 cflags = 0;
50struct filter_set_head	*connectset;
51struct filter_set_head	*connectset6;
52struct filter_set_head	*staticset;
53struct filter_set_head	*staticset6;
54volatile sig_atomic_t	 mrtdump = 0;
55volatile sig_atomic_t	 quit = 0;
56volatile sig_atomic_t	 sigchld = 0;
57volatile sig_atomic_t	 reconfig = 0;
58pid_t			 reconfpid = 0;
59struct imsgbuf		*ibuf_se;
60struct imsgbuf		*ibuf_rde;
61
62void
63sighdlr(int sig)
64{
65	switch (sig) {
66	case SIGTERM:
67	case SIGINT:
68		quit = 1;
69		break;
70	case SIGCHLD:
71		sigchld = 1;
72		break;
73	case SIGHUP:
74		reconfig = 1;
75		break;
76	case SIGALRM:
77	case SIGUSR1:
78		mrtdump = 1;
79		break;
80	}
81}
82
83__dead void
84usage(void)
85{
86	extern char *__progname;
87
88	fprintf(stderr, "usage: %s [-cdnv] ", __progname);
89	fprintf(stderr, "[-D macro=value] [-f file] [-r path] [-s path]\n");
90	exit(1);
91}
92
93#define PFD_PIPE_SESSION	0
94#define PFD_PIPE_ROUTE		1
95#define PFD_SOCK_ROUTE		2
96#define POLL_MAX		3
97#define MAX_TIMEOUT		3600
98
99int
100main(int argc, char *argv[])
101{
102	struct bgpd_config	 conf;
103	struct peer		*peer_l, *p;
104	struct mrt_head		 mrt_l;
105	struct network_head	 net_l;
106	struct filter_head	*rules_l;
107	struct network		*net;
108	struct filter_rule	*r;
109	struct mrt		*m;
110	struct listen_addr	*la;
111	struct pollfd		 pfd[POLL_MAX];
112	pid_t			 io_pid = 0, rde_pid = 0, pid;
113	char			*conffile;
114	int			 debug = 0;
115	int			 ch, timeout, nfds;
116	int			 pipe_m2s[2];
117	int			 pipe_m2r[2];
118	int			 pipe_s2r[2];
119	int			 pipe_s2r_c[2];
120
121	conffile = CONFFILE;
122	bgpd_process = PROC_MAIN;
123
124	log_init(1);		/* log to stderr until daemonized */
125
126	if ((rules_l = calloc(1, sizeof(struct filter_head))) == NULL)
127		err(1, NULL);
128
129	bzero(&conf, sizeof(conf));
130	LIST_INIT(&mrt_l);
131	TAILQ_INIT(&net_l);
132	TAILQ_INIT(rules_l);
133	peer_l = NULL;
134	conf.csock = SOCKET_NAME;
135
136	while ((ch = getopt(argc, argv, "cdD:f:nr:s:v")) != -1) {
137		switch (ch) {
138		case 'c':
139			conf.opts |= BGPD_OPT_FORCE_DEMOTE;
140			break;
141		case 'd':
142			debug = 1;
143			break;
144		case 'D':
145			if (cmdline_symset(optarg) < 0)
146				log_warnx("could not parse macro definition %s",
147				    optarg);
148			break;
149		case 'f':
150			conffile = optarg;
151			break;
152		case 'n':
153			conf.opts |= BGPD_OPT_NOACTION;
154			break;
155		case 'v':
156			if (conf.opts & BGPD_OPT_VERBOSE)
157				conf.opts |= BGPD_OPT_VERBOSE2;
158			conf.opts |= BGPD_OPT_VERBOSE;
159			break;
160		case 'r':
161			conf.rcsock = optarg;
162			break;
163		case 's':
164			conf.csock = optarg;
165			break;
166		default:
167			usage();
168			/* NOTREACHED */
169		}
170	}
171
172	argc -= optind;
173	argv += optind;
174	if (argc > 0)
175		usage();
176
177	if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l, rules_l)) {
178		free(rules_l);
179		exit(1);
180	}
181
182	if (conf.opts & BGPD_OPT_NOACTION) {
183		if (conf.opts & BGPD_OPT_VERBOSE)
184			print_config(&conf, &net_l, peer_l, rules_l, &mrt_l);
185		else
186			fprintf(stderr, "configuration OK\n");
187		exit(0);
188	}
189	cflags = conf.flags;
190	connectset = &conf.connectset;
191	staticset = &conf.staticset;
192	connectset6 = &conf.connectset6;
193	staticset6 = &conf.staticset6;
194
195	if (geteuid())
196		errx(1, "need root privileges");
197
198	if (getpwnam(BGPD_USER) == NULL)
199		errx(1, "unknown user %s", BGPD_USER);
200
201	log_init(debug);
202
203	if (!debug)
204		daemon(1, 0);
205
206	log_info("startup");
207
208	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1)
209		fatal("socketpair");
210	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1)
211		fatal("socketpair");
212	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1)
213		fatal("socketpair");
214	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r_c) == -1)
215		fatal("socketpair");
216	session_socket_blockmode(pipe_m2s[0], BM_NONBLOCK);
217	session_socket_blockmode(pipe_m2s[1], BM_NONBLOCK);
218	session_socket_blockmode(pipe_m2r[0], BM_NONBLOCK);
219	session_socket_blockmode(pipe_m2r[1], BM_NONBLOCK);
220	session_socket_blockmode(pipe_s2r[0], BM_NONBLOCK);
221	session_socket_blockmode(pipe_s2r[1], BM_NONBLOCK);
222	session_socket_blockmode(pipe_s2r_c[0], BM_NONBLOCK);
223	session_socket_blockmode(pipe_s2r_c[1], BM_NONBLOCK);
224
225	prepare_listeners(&conf);
226
227	/* fork children */
228	rde_pid = rde_main(&conf, peer_l, &net_l, rules_l, &mrt_l,
229	    pipe_m2r, pipe_s2r, pipe_m2s, pipe_s2r_c, debug);
230	io_pid = session_main(&conf, peer_l, &net_l, rules_l, &mrt_l,
231	    pipe_m2s, pipe_s2r, pipe_m2r, pipe_s2r_c);
232
233	setproctitle("parent");
234
235	signal(SIGTERM, sighdlr);
236	signal(SIGINT, sighdlr);
237	signal(SIGCHLD, sighdlr);
238	signal(SIGHUP, sighdlr);
239	signal(SIGALRM, sighdlr);
240	signal(SIGUSR1, sighdlr);
241	signal(SIGPIPE, SIG_IGN);
242
243	close(pipe_m2s[1]);
244	close(pipe_m2r[1]);
245	close(pipe_s2r[0]);
246	close(pipe_s2r[1]);
247
248	if ((ibuf_se = malloc(sizeof(struct imsgbuf))) == NULL ||
249	    (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL)
250		fatal(NULL);
251	imsg_init(ibuf_se, pipe_m2s[0]);
252	imsg_init(ibuf_rde, pipe_m2r[0]);
253	mrt_init(ibuf_rde, ibuf_se);
254	if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE),
255	    conf.rtableid)) == -1)
256		quit = 1;
257	if (pftable_clear_all() != 0)
258		quit = 1;
259
260	while ((net = TAILQ_FIRST(&net_l)) != NULL) {
261		TAILQ_REMOVE(&net_l, net, entry);
262		filterset_free(&net->net.attrset);
263		free(net);
264	}
265
266	while ((r = TAILQ_FIRST(rules_l)) != NULL) {
267		TAILQ_REMOVE(rules_l, r, entry);
268		free(r);
269	}
270	TAILQ_FOREACH(la, conf.listen_addrs, entry) {
271		close(la->fd);
272		la->fd = -1;
273	}
274
275	mrt_reconfigure(&mrt_l);
276
277	while (quit == 0) {
278		bzero(pfd, sizeof(pfd));
279		pfd[PFD_PIPE_SESSION].fd = ibuf_se->fd;
280		pfd[PFD_PIPE_SESSION].events = POLLIN;
281		if (ibuf_se->w.queued)
282			pfd[PFD_PIPE_SESSION].events |= POLLOUT;
283		pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd;
284		pfd[PFD_PIPE_ROUTE].events = POLLIN;
285		if (ibuf_rde->w.queued)
286			pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
287		pfd[PFD_SOCK_ROUTE].fd = rfd;
288		pfd[PFD_SOCK_ROUTE].events = POLLIN;
289
290		timeout = mrt_timeout(&mrt_l);
291		if (timeout > MAX_TIMEOUT)
292			timeout = MAX_TIMEOUT;
293
294		if ((nfds = poll(pfd, POLL_MAX, timeout * 1000)) == -1)
295			if (errno != EINTR) {
296				log_warn("poll error");
297				quit = 1;
298			}
299
300		if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLOUT)
301			if (msgbuf_write(&ibuf_se->w) < 0) {
302				log_warn("pipe write error (to SE)");
303				quit = 1;
304			}
305
306		if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
307			if (msgbuf_write(&ibuf_rde->w) < 0) {
308				log_warn("pipe write error (to RDE)");
309				quit = 1;
310			}
311
312		if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) {
313			if (dispatch_imsg(ibuf_se, PFD_PIPE_SESSION) == -1)
314				quit = 1;
315		}
316
317		if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
318			if (dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE) == -1)
319				quit = 1;
320		}
321
322		if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) {
323			if (kr_dispatch_msg() == -1)
324				quit = 1;
325		}
326
327		if (reconfig) {
328			u_int	error;
329
330			reconfig = 0;
331			log_info("rereading config");
332			switch (reconfigure(conffile, &conf, &mrt_l, &peer_l,
333			    rules_l)) {
334			case -1:	/* fatal error */
335				quit = 1;
336				break;
337			case 0:		/* all OK */
338				error = 0;
339				break;
340			default:	/* parse error */
341				error = CTL_RES_PARSE_ERROR;
342				break;
343			}
344			if (reconfpid != 0) {
345				send_imsg_session(IMSG_CTL_RESULT, reconfpid,
346				    &error, sizeof(error));
347				reconfpid = 0;
348			}
349		}
350
351		if (sigchld) {
352			sigchld = 0;
353			if (check_child(io_pid, "session engine")) {
354				quit = 1;
355				io_pid = 0;
356			}
357			if (check_child(rde_pid, "route decision engine")) {
358				quit = 1;
359				rde_pid = 0;
360			}
361		}
362
363		if (mrtdump) {
364			mrtdump = 0;
365			mrt_handler(&mrt_l);
366		}
367	}
368
369	signal(SIGCHLD, SIG_IGN);
370
371	if (io_pid)
372		kill(io_pid, SIGTERM);
373
374	if (rde_pid)
375		kill(rde_pid, SIGTERM);
376
377	while ((p = peer_l) != NULL) {
378		peer_l = p->next;
379		free(p);
380	}
381	while ((m = LIST_FIRST(&mrt_l)) != NULL) {
382		LIST_REMOVE(m, entry);
383		free(m);
384	}
385	while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) {
386		TAILQ_REMOVE(conf.listen_addrs, la, entry);
387		close(la->fd);
388		free(la);
389	}
390
391	free(rules_l);
392	control_cleanup(conf.csock);
393	control_cleanup(conf.rcsock);
394	carp_demote_shutdown();
395	kr_shutdown();
396	pftable_clear_all();
397	free(conf.listen_addrs);
398
399	do {
400		if ((pid = wait(NULL)) == -1 &&
401		    errno != EINTR && errno != ECHILD)
402			fatal("wait");
403	} while (pid != -1 || (pid == -1 && errno == EINTR));
404
405	msgbuf_clear(&ibuf_se->w);
406	free(ibuf_se);
407	msgbuf_clear(&ibuf_rde->w);
408	free(ibuf_rde);
409
410	log_info("Terminating");
411	return (0);
412}
413
414int
415check_child(pid_t pid, const char *pname)
416{
417	int	status;
418
419	if (waitpid(pid, &status, WNOHANG) > 0) {
420		if (WIFEXITED(status)) {
421			log_warnx("Lost child: %s exited", pname);
422			return (1);
423		}
424		if (WIFSIGNALED(status)) {
425			log_warnx("Lost child: %s terminated; signal %d",
426			    pname, WTERMSIG(status));
427			return (1);
428		}
429	}
430
431	return (0);
432}
433
434int
435send_filterset(struct imsgbuf *i, struct filter_set_head *set)
436{
437	struct filter_set	*s;
438
439	TAILQ_FOREACH(s, set, entry)
440		if (imsg_compose(i, IMSG_FILTER_SET, 0, 0, -1, s,
441		    sizeof(struct filter_set)) == -1)
442			return (-1);
443	return (0);
444}
445
446int
447reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l,
448    struct peer **peer_l, struct filter_head *rules_l)
449{
450	struct network_head	 net_l;
451	struct network		*n;
452	struct peer		*p;
453	struct filter_rule	*r;
454	struct listen_addr	*la;
455
456	if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) {
457		log_warnx("config file %s has errors, not reloading",
458		    conffile);
459		return (1);
460	}
461
462	cflags = conf->flags;
463	connectset = &conf->connectset;
464	staticset = &conf->staticset;
465	connectset6 = &conf->connectset6;
466	staticset6 = &conf->staticset6;
467
468	prepare_listeners(conf);
469
470	/* start reconfiguration */
471	if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 0, -1,
472	    conf, sizeof(struct bgpd_config)) == -1)
473		return (-1);
474	if (imsg_compose(ibuf_rde, IMSG_RECONF_CONF, 0, 0, -1,
475	    conf, sizeof(struct bgpd_config)) == -1)
476		return (-1);
477
478	/* send peer list and listeners to the SE */
479	for (p = *peer_l; p != NULL; p = p->next)
480		if (imsg_compose(ibuf_se, IMSG_RECONF_PEER, p->conf.id, 0, -1,
481		    &p->conf, sizeof(struct peer_config)) == -1)
482			return (-1);
483
484	TAILQ_FOREACH(la, conf->listen_addrs, entry) {
485		if (imsg_compose(ibuf_se, IMSG_RECONF_LISTENER, 0, 0, la->fd,
486		    la, sizeof(struct listen_addr)) == -1)
487			return (-1);
488		la->fd = -1;
489	}
490
491	/* networks for the RDE */
492	while ((n = TAILQ_FIRST(&net_l)) != NULL) {
493		if (imsg_compose(ibuf_rde, IMSG_NETWORK_ADD, 0, 0, -1,
494		    &n->net, sizeof(struct network_config)) == -1)
495			return (-1);
496		if (send_filterset(ibuf_rde, &n->net.attrset) == -1)
497			return (-1);
498		if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1,
499		    NULL, 0) == -1)
500			return (-1);
501		TAILQ_REMOVE(&net_l, n, entry);
502		filterset_free(&n->net.attrset);
503		free(n);
504	}
505
506	/* redistribute list needs to be reloaded too */
507	if (kr_reload() == -1)
508		return (-1);
509
510	/* filters for the RDE */
511	while ((r = TAILQ_FIRST(rules_l)) != NULL) {
512		if (imsg_compose(ibuf_rde, IMSG_RECONF_FILTER, 0, 0, -1,
513		    r, sizeof(struct filter_rule)) == -1)
514			return (-1);
515		if (send_filterset(ibuf_rde, &r->set) == -1)
516			return (-1);
517		TAILQ_REMOVE(rules_l, r, entry);
518		filterset_free(&r->set);
519		free(r);
520	}
521
522	/* signal both childs to replace their config */
523	if (imsg_compose(ibuf_se, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1 ||
524	    imsg_compose(ibuf_rde, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1)
525		return (-1);
526
527	/* mrt changes can be sent out of bound */
528	mrt_reconfigure(mrt_l);
529	return (0);
530}
531
532int
533dispatch_imsg(struct imsgbuf *ibuf, int idx)
534{
535	struct imsg		 imsg;
536	int			 n;
537	int			 rv;
538
539	if ((n = imsg_read(ibuf)) == -1)
540		return (-1);
541
542	if (n == 0) {	/* connection closed */
543		log_warnx("dispatch_imsg in main: pipe closed");
544		return (-1);
545	}
546
547	rv = 0;
548	for (;;) {
549		if ((n = imsg_get(ibuf, &imsg)) == -1)
550			return (-1);
551
552		if (n == 0)
553			break;
554
555		switch (imsg.hdr.type) {
556		case IMSG_KROUTE_CHANGE:
557			if (idx != PFD_PIPE_ROUTE)
558				log_warnx("route request not from RDE");
559			else if (kr_change(imsg.data))
560				rv = -1;
561			break;
562		case IMSG_KROUTE_DELETE:
563			if (idx != PFD_PIPE_ROUTE)
564				log_warnx("route request not from RDE");
565			else if (kr_delete(imsg.data))
566				rv = -1;
567			break;
568		case IMSG_KROUTE6_CHANGE:
569			if (idx != PFD_PIPE_ROUTE)
570				log_warnx("route request not from RDE");
571			else if (kr6_change(imsg.data))
572				rv = -1;
573			break;
574		case IMSG_KROUTE6_DELETE:
575			if (idx != PFD_PIPE_ROUTE)
576				log_warnx("route request not from RDE");
577			else if (kr6_delete(imsg.data))
578				rv = -1;
579			break;
580		case IMSG_NEXTHOP_ADD:
581			if (idx != PFD_PIPE_ROUTE)
582				log_warnx("nexthop request not from RDE");
583			else
584				if (imsg.hdr.len != IMSG_HEADER_SIZE +
585				    sizeof(struct bgpd_addr))
586					log_warnx("wrong imsg len");
587				else if (kr_nexthop_add(imsg.data) == -1)
588					rv = -1;
589			break;
590		case IMSG_NEXTHOP_REMOVE:
591			if (idx != PFD_PIPE_ROUTE)
592				log_warnx("nexthop request not from RDE");
593			else
594				if (imsg.hdr.len != IMSG_HEADER_SIZE +
595				    sizeof(struct bgpd_addr))
596					log_warnx("wrong imsg len");
597				else
598					kr_nexthop_delete(imsg.data);
599			break;
600		case IMSG_PFTABLE_ADD:
601			if (idx != PFD_PIPE_ROUTE)
602				log_warnx("pftable request not from RDE");
603			else
604				if (imsg.hdr.len != IMSG_HEADER_SIZE +
605				    sizeof(struct pftable_msg))
606					log_warnx("wrong imsg len");
607				else if (pftable_addr_add(imsg.data) != 0)
608					rv = -1;
609			break;
610		case IMSG_PFTABLE_REMOVE:
611			if (idx != PFD_PIPE_ROUTE)
612				log_warnx("pftable request not from RDE");
613			else
614				if (imsg.hdr.len != IMSG_HEADER_SIZE +
615				    sizeof(struct pftable_msg))
616					log_warnx("wrong imsg len");
617				else if (pftable_addr_remove(imsg.data) != 0)
618					rv = -1;
619			break;
620		case IMSG_PFTABLE_COMMIT:
621			if (idx != PFD_PIPE_ROUTE)
622				log_warnx("pftable request not from RDE");
623			else
624				if (imsg.hdr.len != IMSG_HEADER_SIZE)
625					log_warnx("wrong imsg len");
626				else if (pftable_commit() != 0)
627					rv = -1;
628			break;
629		case IMSG_CTL_RELOAD:
630			if (idx != PFD_PIPE_SESSION)
631				log_warnx("reload request not from SE");
632			else
633				reconfig = 1;
634				reconfpid = imsg.hdr.pid;
635			break;
636		case IMSG_CTL_FIB_COUPLE:
637			if (idx != PFD_PIPE_SESSION)
638				log_warnx("couple request not from SE");
639			else
640				kr_fib_couple();
641			break;
642		case IMSG_CTL_FIB_DECOUPLE:
643			if (idx != PFD_PIPE_SESSION)
644				log_warnx("decouple request not from SE");
645			else
646				kr_fib_decouple();
647			break;
648		case IMSG_CTL_KROUTE:
649		case IMSG_CTL_KROUTE_ADDR:
650		case IMSG_CTL_SHOW_NEXTHOP:
651		case IMSG_CTL_SHOW_INTERFACE:
652			if (idx != PFD_PIPE_SESSION)
653				log_warnx("kroute request not from SE");
654			else
655				kr_show_route(&imsg);
656			break;
657		case IMSG_IFINFO:
658			if (idx != PFD_PIPE_SESSION)
659				log_warnx("IFINFO request not from SE");
660			else if (imsg.hdr.len != IMSG_HEADER_SIZE + IFNAMSIZ)
661				log_warnx("IFINFO request with wrong len");
662			else
663				kr_ifinfo(imsg.data);
664			break;
665		case IMSG_DEMOTE:
666			if (idx != PFD_PIPE_SESSION)
667				log_warnx("demote request not from SE");
668			else if (imsg.hdr.len != IMSG_HEADER_SIZE +
669			    sizeof(struct demote_msg))
670				log_warnx("DEMOTE request with wrong len");
671			else {
672				struct demote_msg	*msg;
673
674				msg = imsg.data;
675				carp_demote_set(msg->demote_group, msg->level);
676			}
677			break;
678		default:
679			break;
680		}
681		imsg_free(&imsg);
682		if (rv != 0)
683			return (rv);
684	}
685	return (0);
686}
687
688void
689send_nexthop_update(struct kroute_nexthop *msg)
690{
691	char	*gw = NULL;
692
693	if (msg->gateway.af)
694		if (asprintf(&gw, ": via %s",
695		    log_addr(&msg->gateway)) == -1) {
696			log_warn("send_nexthop_update");
697			quit = 1;
698		}
699
700	log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop),
701	    msg->valid ? "valid" : "invalid",
702	    msg->connected ? ": directly connected" : "",
703	    msg->gateway.af ? gw : "");
704
705	free(gw);
706
707	if (imsg_compose(ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 0, -1,
708	    msg, sizeof(struct kroute_nexthop)) == -1)
709		quit = 1;
710}
711
712void
713send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen)
714{
715	imsg_compose(ibuf_se, type, 0, pid, -1, data, datalen);
716}
717
718int
719bgpd_redistribute(int type, struct kroute *kr, struct kroute6 *kr6)
720{
721	struct network_config	 net;
722	struct filter_set_head	*h;
723
724	if ((cflags & BGPD_FLAG_REDIST_CONNECTED) && kr &&
725	    (kr->flags & F_CONNECTED))
726		h = connectset;
727	else if ((cflags & BGPD_FLAG_REDIST_STATIC) && kr &&
728	    (kr->flags & F_STATIC))
729		h = staticset;
730	else if ((cflags & BGPD_FLAG_REDIST6_CONNECTED) && kr6 &&
731	    (kr6->flags & F_CONNECTED))
732		h = connectset6;
733	else if ((cflags & BGPD_FLAG_REDIST6_STATIC) && kr6 &&
734	    (kr6->flags & F_STATIC))
735		h = staticset6;
736	else
737		return (0);
738
739	bzero(&net, sizeof(net));
740	if (kr && kr6)
741		fatalx("bgpd_redistribute: unable to redistribute v4 and v6"
742		    "together");
743	if (kr != NULL) {
744		net.prefix.af = AF_INET;
745		net.prefix.v4.s_addr = kr->prefix.s_addr;
746		net.prefixlen = kr->prefixlen;
747	}
748	if (kr6 != NULL) {
749		net.prefix.af = AF_INET6;
750		memcpy(&net.prefix.v6, &kr6->prefix, sizeof(struct in6_addr));
751		net.prefixlen = kr6->prefixlen;
752	}
753
754
755	if (imsg_compose(ibuf_rde, type, 0, 0, -1, &net,
756	    sizeof(struct network_config)) == -1)
757		return (-1);
758
759	/* networks that get deleted don't need to send the filter set */
760	if (type == IMSG_NETWORK_REMOVE)
761		return (1);
762
763	if (send_filterset(ibuf_rde, h) == -1)
764		return (-1);
765	if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1, NULL, 0) == -1)
766		return (-1);
767
768	return (1);
769}
770
771int
772bgpd_filternexthop(struct kroute *kr, struct kroute6 *kr6)
773{
774	/* kernel routes are never filtered */
775	if (kr && kr->flags & F_KERNEL && kr->prefixlen != 0)
776		return (0);
777	if (kr6 && kr6->flags & F_KERNEL && kr6->prefixlen != 0)
778		return (0);
779
780	if (cflags & BGPD_FLAG_NEXTHOP_BGP) {
781		if (kr && kr->flags & F_BGPD_INSERTED)
782			return (0);
783		if (kr6 && kr6->flags & F_BGPD_INSERTED)
784			return (0);
785	}
786
787	if (cflags & BGPD_FLAG_NEXTHOP_DEFAULT) {
788		if (kr && kr->prefixlen == 0)
789			return (0);
790		if (kr6 && kr6->prefixlen == 0)
791			return (0);
792	}
793
794	return (1);
795}
796