bgpd.c revision 1.124
1/*	$OpenBSD: bgpd.c,v 1.124 2005/10/13 09:09:20 claudio 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 *,
44		    int);
45int		reconfigure(char *, struct bgpd_config *, struct mrt_head *,
46		    struct peer **, struct filter_head *);
47int		dispatch_imsg(struct imsgbuf *, int);
48
49int			 rfd = -1;
50int			 cflags = 0;
51struct filter_set_head	*connectset;
52struct filter_set_head	*connectset6;
53struct filter_set_head	*staticset;
54struct filter_set_head	*staticset6;
55volatile sig_atomic_t	 mrtdump = 0;
56volatile sig_atomic_t	 quit = 0;
57volatile sig_atomic_t	 reconfig = 0;
58volatile sig_atomic_t	 sigchld = 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 [-dnv] ", __progname);
89	fprintf(stderr, "[-D macro=value] [-f file]\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;
116	int			 pipe_m2s[2];
117	int			 pipe_m2r[2];
118	int			 pipe_s2r[2];
119
120	conffile = CONFFILE;
121	bgpd_process = PROC_MAIN;
122
123	log_init(1);		/* log to stderr until daemonized */
124
125	if ((rules_l = calloc(1, sizeof(struct filter_head))) == NULL)
126		err(1, NULL);
127
128	bzero(&conf, sizeof(conf));
129	LIST_INIT(&mrt_l);
130	TAILQ_INIT(&net_l);
131	TAILQ_INIT(rules_l);
132	peer_l = NULL;
133
134	while ((ch = getopt(argc, argv, "dD:f:nv")) != -1) {
135		switch (ch) {
136		case 'd':
137			debug = 1;
138			break;
139		case 'D':
140			if (cmdline_symset(optarg) < 0)
141				log_warnx("could not parse macro definition %s",
142				    optarg);
143			break;
144		case 'f':
145			conffile = optarg;
146			break;
147		case 'n':
148			conf.opts |= BGPD_OPT_NOACTION;
149			break;
150		case 'v':
151			if (conf.opts & BGPD_OPT_VERBOSE)
152				conf.opts |= BGPD_OPT_VERBOSE2;
153			conf.opts |= BGPD_OPT_VERBOSE;
154			break;
155		default:
156			usage();
157			/* NOTREACHED */
158		}
159	}
160
161	if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l, rules_l)) {
162		free(rules_l);
163		exit(1);
164	}
165
166	if (conf.opts & BGPD_OPT_NOACTION) {
167		if (conf.opts & BGPD_OPT_VERBOSE)
168			print_config(&conf, &net_l, peer_l, rules_l, &mrt_l);
169		else
170			fprintf(stderr, "configuration OK\n");
171		exit(0);
172	}
173	cflags = conf.flags;
174	connectset = &conf.connectset;
175	staticset = &conf.staticset;
176	connectset6 = &conf.connectset6;
177	staticset6 = &conf.staticset6;
178
179	if (geteuid())
180		errx(1, "need root privileges");
181
182	if (getpwnam(BGPD_USER) == NULL)
183		errx(1, "unknown user %s", BGPD_USER);
184
185	log_init(debug);
186
187	if (!debug)
188		daemon(1, 0);
189
190	log_info("startup");
191
192	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1)
193		fatal("socketpair");
194	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1)
195		fatal("socketpair");
196	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1)
197		fatal("socketpair");
198	session_socket_blockmode(pipe_m2s[0], BM_NONBLOCK);
199	session_socket_blockmode(pipe_m2s[1], BM_NONBLOCK);
200	session_socket_blockmode(pipe_m2r[0], BM_NONBLOCK);
201	session_socket_blockmode(pipe_m2r[1], BM_NONBLOCK);
202	session_socket_blockmode(pipe_s2r[0], BM_NONBLOCK);
203	session_socket_blockmode(pipe_s2r[1], BM_NONBLOCK);
204
205	prepare_listeners(&conf);
206
207	/* fork children */
208	rde_pid = rde_main(&conf, peer_l, &net_l, rules_l, &mrt_l,
209	    pipe_m2r, pipe_s2r, pipe_m2s);
210	io_pid = session_main(&conf, peer_l, &net_l, rules_l, &mrt_l,
211	    pipe_m2s, pipe_s2r, pipe_m2r);
212
213	setproctitle("parent");
214
215	signal(SIGTERM, sighdlr);
216	signal(SIGINT, sighdlr);
217	signal(SIGCHLD, sighdlr);
218	signal(SIGHUP, sighdlr);
219	signal(SIGALRM, sighdlr);
220	signal(SIGUSR1, sighdlr);
221
222	close(pipe_m2s[1]);
223	close(pipe_m2r[1]);
224	close(pipe_s2r[0]);
225	close(pipe_s2r[1]);
226
227	if ((ibuf_se = malloc(sizeof(struct imsgbuf))) == NULL ||
228	    (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL)
229		fatal(NULL);
230	imsg_init(ibuf_se, pipe_m2s[0]);
231	imsg_init(ibuf_rde, pipe_m2r[0]);
232	mrt_init(ibuf_rde, ibuf_se);
233	if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE))) == -1)
234		quit = 1;
235	if (pftable_clear_all() != 0)
236		quit = 1;
237
238	while ((net = TAILQ_FIRST(&net_l)) != NULL) {
239		TAILQ_REMOVE(&net_l, net, entry);
240		free(net);
241	}
242
243	while ((r = TAILQ_FIRST(rules_l)) != NULL) {
244		TAILQ_REMOVE(rules_l, r, entry);
245		free(r);
246	}
247	TAILQ_FOREACH(la, conf.listen_addrs, entry) {
248		close(la->fd);
249		la->fd = -1;
250	}
251
252	mrt_reconfigure(&mrt_l);
253
254	while (quit == 0) {
255		pfd[PFD_PIPE_SESSION].fd = ibuf_se->fd;
256		pfd[PFD_PIPE_SESSION].events = POLLIN;
257		if (ibuf_se->w.queued)
258			pfd[PFD_PIPE_SESSION].events |= POLLOUT;
259		pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd;
260		pfd[PFD_PIPE_ROUTE].events = POLLIN;
261		if (ibuf_rde->w.queued)
262			pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
263		pfd[PFD_SOCK_ROUTE].fd = rfd;
264		pfd[PFD_SOCK_ROUTE].events = POLLIN;
265
266		timeout = mrt_timeout(&mrt_l);
267		if (timeout > MAX_TIMEOUT)
268			timeout = MAX_TIMEOUT;
269
270		if (poll(pfd, POLL_MAX, timeout * 1000) == -1)
271			if (errno != EINTR) {
272				log_warn("poll error");
273				quit = 1;
274			}
275
276		if (pfd[PFD_PIPE_SESSION].revents & POLLOUT)
277			if (msgbuf_write(&ibuf_se->w) < 0) {
278				log_warn("pipe write error (to SE)");
279				quit = 1;
280			}
281
282		if (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
283			if (msgbuf_write(&ibuf_rde->w) < 0) {
284				log_warn("pipe write error (to RDE)");
285				quit = 1;
286			}
287
288		if (pfd[PFD_PIPE_SESSION].revents & POLLIN) {
289			if (dispatch_imsg(ibuf_se, PFD_PIPE_SESSION) == -1)
290				quit = 1;
291		}
292
293		if (pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
294			if (dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE) == -1)
295				quit = 1;
296		}
297
298		if (pfd[PFD_SOCK_ROUTE].revents & POLLIN) {
299			if (kr_dispatch_msg() == -1)
300				quit = 1;
301		}
302
303		if (reconfig) {
304			reconfig = 0;
305			log_info("rereading config");
306			reconfigure(conffile, &conf, &mrt_l, &peer_l, rules_l);
307		}
308
309		if (sigchld) {
310			sigchld = 0;
311			if (check_child(io_pid, "session engine")) {
312				quit = 1;
313				io_pid = 0;
314			}
315			if (check_child(rde_pid, "route decision engine")) {
316				quit = 1;
317				rde_pid = 0;
318			}
319		}
320
321		if (mrtdump == 1) {
322			mrtdump = 0;
323			mrt_handler(&mrt_l);
324		}
325	}
326
327	signal(SIGCHLD, SIG_IGN);
328
329	if (io_pid)
330		kill(io_pid, SIGTERM);
331
332	if (rde_pid)
333		kill(rde_pid, SIGTERM);
334
335	while ((p = peer_l) != NULL) {
336		peer_l = p->next;
337		free(p);
338	}
339	while ((m = LIST_FIRST(&mrt_l)) != NULL) {
340		LIST_REMOVE(m, entry);
341		free(m);
342	}
343	while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) {
344		TAILQ_REMOVE(conf.listen_addrs, la, entry);
345		close(la->fd);
346		free(la);
347	}
348
349	free(rules_l);
350	control_cleanup();
351	kr_shutdown();
352	pftable_clear_all();
353	free(conf.listen_addrs);
354
355	do {
356		if ((pid = wait(NULL)) == -1 &&
357		    errno != EINTR && errno != ECHILD)
358			fatal("wait");
359	} while (pid != -1 || (pid == -1 && errno == EINTR));
360
361	msgbuf_clear(&ibuf_se->w);
362	free(ibuf_se);
363	msgbuf_clear(&ibuf_rde->w);
364	free(ibuf_rde);
365
366	log_info("Terminating");
367	return (0);
368}
369
370int
371check_child(pid_t pid, const char *pname)
372{
373	int	status;
374
375	if (waitpid(pid, &status, WNOHANG) > 0) {
376		if (WIFEXITED(status)) {
377			log_warnx("Lost child: %s exited", pname);
378			return (1);
379		}
380		if (WIFSIGNALED(status)) {
381			log_warnx("Lost child: %s terminated; signal %d",
382			    pname, WTERMSIG(status));
383			return (1);
384		}
385	}
386
387	return (0);
388}
389
390int
391send_filterset(struct imsgbuf *i, struct filter_set_head *set, int id)
392{
393	struct filter_set	*s;
394
395	TAILQ_FOREACH(s, set, entry)
396		if (imsg_compose(i, IMSG_FILTER_SET, id, 0, -1, s,
397		    sizeof(struct filter_set)) == -1)
398			return (-1);
399	return (0);
400}
401
402int
403reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l,
404    struct peer **peer_l, struct filter_head *rules_l)
405{
406	struct network_head	 net_l;
407	struct network		*n;
408	struct peer		*p;
409	struct filter_rule	*r;
410	struct listen_addr	*la;
411
412	if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) {
413		log_warnx("config file %s has errors, not reloading",
414		    conffile);
415		return (-1);
416	}
417
418	cflags = conf->flags;
419	connectset = &conf->connectset;
420	staticset = &conf->staticset;
421	connectset6 = &conf->connectset6;
422	staticset6 = &conf->staticset6;
423
424	prepare_listeners(conf);
425
426	if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 0, -1,
427	    conf, sizeof(struct bgpd_config)) == -1)
428		return (-1);
429	if (imsg_compose(ibuf_rde, IMSG_RECONF_CONF, 0, 0, -1,
430	    conf, sizeof(struct bgpd_config)) == -1)
431		return (-1);
432	for (p = *peer_l; p != NULL; p = p->next) {
433		if (imsg_compose(ibuf_se, IMSG_RECONF_PEER, p->conf.id, 0, -1,
434		    &p->conf, sizeof(struct peer_config)) == -1)
435			return (-1);
436		if (send_filterset(ibuf_se, &p->conf.attrset,
437		    p->conf.id) == -1)
438			return (-1);
439	}
440	while ((n = TAILQ_FIRST(&net_l)) != NULL) {
441		if (imsg_compose(ibuf_rde, IMSG_NETWORK_ADD, 0, 0, -1,
442		    &n->net, sizeof(struct network_config)) == -1)
443			return (-1);
444		if (send_filterset(ibuf_rde, &n->net.attrset, 0) == -1)
445			return (-1);
446		if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1,
447		    NULL, 0) == -1)
448			return (-1);
449		TAILQ_REMOVE(&net_l, n, entry);
450		filterset_free(&n->net.attrset);
451		free(n);
452	}
453	/* redistribute list needs to be reloaded too */
454	if (kr_redist_reload() == -1)
455		return (-1);
456
457	while ((r = TAILQ_FIRST(rules_l)) != NULL) {
458		if (imsg_compose(ibuf_rde, IMSG_RECONF_FILTER, 0, 0, -1,
459		    r, sizeof(struct filter_rule)) == -1)
460			return (-1);
461		if (send_filterset(ibuf_rde, &r->set, 0) == -1)
462			return (-1);
463		TAILQ_REMOVE(rules_l, r, entry);
464		filterset_free(&r->set);
465		free(r);
466	}
467	TAILQ_FOREACH(la, conf->listen_addrs, entry) {
468		if (imsg_compose(ibuf_se, IMSG_RECONF_LISTENER, 0, 0, la->fd,
469		    la, sizeof(struct listen_addr)) == -1)
470			return (-1);
471		la->fd = -1;
472	}
473
474	if (imsg_compose(ibuf_se, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1 ||
475	    imsg_compose(ibuf_rde, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1)
476		return (-1);
477
478	/* mrt changes can be sent out of bound */
479	mrt_reconfigure(mrt_l);
480	return (0);
481}
482
483int
484dispatch_imsg(struct imsgbuf *ibuf, int idx)
485{
486	struct imsg		 imsg;
487	int			 n;
488	int			 rv;
489
490	if ((n = imsg_read(ibuf)) == -1)
491		return (-1);
492
493	if (n == 0) {	/* connection closed */
494		log_warnx("dispatch_imsg in main: pipe closed");
495		return (-1);
496	}
497
498	rv = 0;
499	for (;;) {
500		if ((n = imsg_get(ibuf, &imsg)) == -1)
501			return (-1);
502
503		if (n == 0)
504			break;
505
506		switch (imsg.hdr.type) {
507		case IMSG_KROUTE_CHANGE:
508			if (idx != PFD_PIPE_ROUTE)
509				log_warnx("route request not from RDE");
510			else if (kr_change(imsg.data))
511				rv = -1;
512			break;
513		case IMSG_KROUTE_DELETE:
514			if (idx != PFD_PIPE_ROUTE)
515				log_warnx("route request not from RDE");
516			else if (kr_delete(imsg.data))
517				rv = -1;
518			break;
519		case IMSG_KROUTE6_CHANGE:
520			if (idx != PFD_PIPE_ROUTE)
521				log_warnx("route request not from RDE");
522			else if (kr6_change(imsg.data))
523				rv = -1;
524			break;
525		case IMSG_KROUTE6_DELETE:
526			if (idx != PFD_PIPE_ROUTE)
527				log_warnx("route request not from RDE");
528			else if (kr6_delete(imsg.data))
529				rv = -1;
530			break;
531		case IMSG_NEXTHOP_ADD:
532			if (idx != PFD_PIPE_ROUTE)
533				log_warnx("nexthop request not from RDE");
534			else
535				if (imsg.hdr.len != IMSG_HEADER_SIZE +
536				    sizeof(struct bgpd_addr))
537					log_warnx("wrong imsg len");
538				else if (kr_nexthop_add(imsg.data) == -1)
539					rv = -1;
540			break;
541		case IMSG_NEXTHOP_REMOVE:
542			if (idx != PFD_PIPE_ROUTE)
543				log_warnx("nexthop request not from RDE");
544			else
545				if (imsg.hdr.len != IMSG_HEADER_SIZE +
546				    sizeof(struct bgpd_addr))
547					log_warnx("wrong imsg len");
548				else
549					kr_nexthop_delete(imsg.data);
550			break;
551		case IMSG_PFTABLE_ADD:
552			if (idx != PFD_PIPE_ROUTE)
553				log_warnx("pftable request not from RDE");
554			else
555				if (imsg.hdr.len != IMSG_HEADER_SIZE +
556				    sizeof(struct pftable_msg))
557					log_warnx("wrong imsg len");
558				else if (pftable_addr_add(imsg.data) != 0)
559					rv = -1;
560			break;
561		case IMSG_PFTABLE_REMOVE:
562			if (idx != PFD_PIPE_ROUTE)
563				log_warnx("pftable request not from RDE");
564			else
565				if (imsg.hdr.len != IMSG_HEADER_SIZE +
566				    sizeof(struct pftable_msg))
567					log_warnx("wrong imsg len");
568				else if (pftable_addr_remove(imsg.data) != 0)
569					rv = -1;
570			break;
571		case IMSG_PFTABLE_COMMIT:
572			if (idx != PFD_PIPE_ROUTE)
573				log_warnx("pftable request not from RDE");
574			else
575				if (imsg.hdr.len != IMSG_HEADER_SIZE)
576					log_warnx("wrong imsg len");
577				else if (pftable_commit() != 0)
578					rv = -1;
579			break;
580		case IMSG_CTL_RELOAD:
581			if (idx != PFD_PIPE_SESSION)
582				log_warnx("reload request not from SE");
583			else
584				reconfig = 1;
585			break;
586		case IMSG_CTL_FIB_COUPLE:
587			if (idx != PFD_PIPE_SESSION)
588				log_warnx("couple request not from SE");
589			else
590				kr_fib_couple();
591			break;
592		case IMSG_CTL_FIB_DECOUPLE:
593			if (idx != PFD_PIPE_SESSION)
594				log_warnx("decouple request not from SE");
595			else
596				kr_fib_decouple();
597			break;
598		case IMSG_CTL_KROUTE:
599		case IMSG_CTL_KROUTE_ADDR:
600		case IMSG_CTL_SHOW_NEXTHOP:
601		case IMSG_CTL_SHOW_INTERFACE:
602			if (idx != PFD_PIPE_SESSION)
603				log_warnx("kroute request not from SE");
604			else
605				kr_show_route(&imsg);
606			break;
607		case IMSG_IFINFO:
608			if (idx != PFD_PIPE_SESSION)
609				log_warnx("IFINFO request not from SE");
610			else if (imsg.hdr.len != IMSG_HEADER_SIZE + IFNAMSIZ)
611				log_warnx("IFINFO request with wrong len");
612			else
613				kr_ifinfo(imsg.data);
614			break;
615		default:
616			break;
617		}
618		imsg_free(&imsg);
619		if (rv != 0)
620			return (rv);
621	}
622	return (0);
623}
624
625void
626send_nexthop_update(struct kroute_nexthop *msg)
627{
628	char	*gw = NULL;
629
630	if (msg->gateway.af)
631		if (asprintf(&gw, ": via %s",
632		    log_addr(&msg->gateway)) == -1) {
633			log_warn("send_nexthop_update");
634			quit = 1;
635		}
636
637	log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop),
638	    msg->valid ? "valid" : "invalid",
639	    msg->connected ? ": directly connected" : "",
640	    msg->gateway.af ? gw : "");
641
642	free(gw);
643
644	if (imsg_compose(ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 0, -1,
645	    msg, sizeof(struct kroute_nexthop)) == -1)
646		quit = 1;
647}
648
649void
650send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen)
651{
652	imsg_compose(ibuf_se, type, 0, pid, -1, data, datalen);
653}
654
655int
656bgpd_redistribute(int type, struct kroute *kr, struct kroute6 *kr6)
657{
658	struct network_config	 net;
659	struct filter_set_head	*h;
660
661	if ((cflags & BGPD_FLAG_REDIST_CONNECTED) && kr &&
662	    (kr->flags & F_CONNECTED))
663		h = connectset;
664	else if ((cflags & BGPD_FLAG_REDIST_STATIC) && kr &&
665	    (kr->flags & F_STATIC))
666		h = staticset;
667	else if ((cflags & BGPD_FLAG_REDIST6_CONNECTED) && kr6 &&
668	    (kr6->flags & F_CONNECTED))
669		h = connectset6;
670	else if ((cflags & BGPD_FLAG_REDIST6_STATIC) && kr6 &&
671	    (kr6->flags & F_STATIC))
672		h = staticset6;
673	else
674		return (0);
675
676	bzero(&net, sizeof(net));
677	if (kr && kr6)
678		fatalx("bgpd_redistribute: unable to redistribute v4 and v6"
679		    "together");
680	if (kr != NULL) {
681		net.prefix.af = AF_INET;
682		net.prefix.v4.s_addr = kr->prefix.s_addr;
683		net.prefixlen = kr->prefixlen;
684	}
685	if (kr6 != NULL) {
686		net.prefix.af = AF_INET6;
687		memcpy(&net.prefix.v6, &kr6->prefix, sizeof(struct in6_addr));
688		net.prefixlen = kr6->prefixlen;
689	}
690
691
692	if (imsg_compose(ibuf_rde, type, 0, 0, -1, &net,
693	    sizeof(struct network_config)) == -1)
694		return (-1);
695
696	/* networks that get deleted don't need to send the filter set */
697	if (type == IMSG_NETWORK_REMOVE)
698		return (1);
699
700	if (send_filterset(ibuf_rde, h, 0) == -1)
701		return (-1);
702	if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1, NULL, 0) == -1)
703		return (-1);
704
705	return (1);
706}
707
708