bgpd.c revision 1.127
1/*	$OpenBSD: bgpd.c,v 1.127 2005/12/24 14:11:13 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		bzero(pfd, sizeof(pfd));
256		pfd[PFD_PIPE_SESSION].fd = ibuf_se->fd;
257		pfd[PFD_PIPE_SESSION].events = POLLIN;
258		if (ibuf_se->w.queued)
259			pfd[PFD_PIPE_SESSION].events |= POLLOUT;
260		pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd;
261		pfd[PFD_PIPE_ROUTE].events = POLLIN;
262		if (ibuf_rde->w.queued)
263			pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
264		pfd[PFD_SOCK_ROUTE].fd = rfd;
265		pfd[PFD_SOCK_ROUTE].events = POLLIN;
266
267		timeout = mrt_timeout(&mrt_l);
268		if (timeout > MAX_TIMEOUT)
269			timeout = MAX_TIMEOUT;
270
271		if (poll(pfd, POLL_MAX, timeout * 1000) == -1)
272			if (errno != EINTR) {
273				log_warn("poll error");
274				quit = 1;
275			}
276
277		if (pfd[PFD_PIPE_SESSION].revents & POLLOUT)
278			if (msgbuf_write(&ibuf_se->w) < 0) {
279				log_warn("pipe write error (to SE)");
280				quit = 1;
281			}
282
283		if (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
284			if (msgbuf_write(&ibuf_rde->w) < 0) {
285				log_warn("pipe write error (to RDE)");
286				quit = 1;
287			}
288
289		if (pfd[PFD_PIPE_SESSION].revents & POLLIN) {
290			if (dispatch_imsg(ibuf_se, PFD_PIPE_SESSION) == -1)
291				quit = 1;
292		}
293
294		if (pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
295			if (dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE) == -1)
296				quit = 1;
297		}
298
299		if (pfd[PFD_SOCK_ROUTE].revents & POLLIN) {
300			if (kr_dispatch_msg() == -1)
301				quit = 1;
302		}
303
304		if (reconfig) {
305			reconfig = 0;
306			log_info("rereading config");
307			reconfigure(conffile, &conf, &mrt_l, &peer_l, rules_l);
308		}
309
310		if (sigchld) {
311			sigchld = 0;
312			if (check_child(io_pid, "session engine")) {
313				quit = 1;
314				io_pid = 0;
315			}
316			if (check_child(rde_pid, "route decision engine")) {
317				quit = 1;
318				rde_pid = 0;
319			}
320		}
321
322		if (mrtdump == 1) {
323			mrtdump = 0;
324			mrt_handler(&mrt_l);
325		}
326	}
327
328	signal(SIGCHLD, SIG_IGN);
329
330	if (io_pid)
331		kill(io_pid, SIGTERM);
332
333	if (rde_pid)
334		kill(rde_pid, SIGTERM);
335
336	while ((p = peer_l) != NULL) {
337		peer_l = p->next;
338		free(p);
339	}
340	while ((m = LIST_FIRST(&mrt_l)) != NULL) {
341		LIST_REMOVE(m, entry);
342		free(m);
343	}
344	while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) {
345		TAILQ_REMOVE(conf.listen_addrs, la, entry);
346		close(la->fd);
347		free(la);
348	}
349
350	free(rules_l);
351	control_cleanup();
352	kr_shutdown();
353	pftable_clear_all();
354	free(conf.listen_addrs);
355
356	do {
357		if ((pid = wait(NULL)) == -1 &&
358		    errno != EINTR && errno != ECHILD)
359			fatal("wait");
360	} while (pid != -1 || (pid == -1 && errno == EINTR));
361
362	msgbuf_clear(&ibuf_se->w);
363	free(ibuf_se);
364	msgbuf_clear(&ibuf_rde->w);
365	free(ibuf_rde);
366
367	log_info("Terminating");
368	return (0);
369}
370
371int
372check_child(pid_t pid, const char *pname)
373{
374	int	status;
375
376	if (waitpid(pid, &status, WNOHANG) > 0) {
377		if (WIFEXITED(status)) {
378			log_warnx("Lost child: %s exited", pname);
379			return (1);
380		}
381		if (WIFSIGNALED(status)) {
382			log_warnx("Lost child: %s terminated; signal %d",
383			    pname, WTERMSIG(status));
384			return (1);
385		}
386	}
387
388	return (0);
389}
390
391int
392send_filterset(struct imsgbuf *i, struct filter_set_head *set, int id)
393{
394	struct filter_set	*s;
395
396	TAILQ_FOREACH(s, set, entry)
397		if (imsg_compose(i, IMSG_FILTER_SET, id, 0, -1, s,
398		    sizeof(struct filter_set)) == -1)
399			return (-1);
400	return (0);
401}
402
403int
404reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l,
405    struct peer **peer_l, struct filter_head *rules_l)
406{
407	struct network_head	 net_l;
408	struct network		*n;
409	struct peer		*p;
410	struct filter_rule	*r;
411	struct listen_addr	*la;
412
413	if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) {
414		log_warnx("config file %s has errors, not reloading",
415		    conffile);
416		return (-1);
417	}
418
419	cflags = conf->flags;
420	connectset = &conf->connectset;
421	staticset = &conf->staticset;
422	connectset6 = &conf->connectset6;
423	staticset6 = &conf->staticset6;
424
425	prepare_listeners(conf);
426
427	/* start reconfiguration */
428	if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 0, -1,
429	    conf, sizeof(struct bgpd_config)) == -1)
430		return (-1);
431	if (imsg_compose(ibuf_rde, IMSG_RECONF_CONF, 0, 0, -1,
432	    conf, sizeof(struct bgpd_config)) == -1)
433		return (-1);
434
435	/* send peer list and listeners to the SE */
436	for (p = *peer_l; p != NULL; p = p->next)
437		if (imsg_compose(ibuf_se, IMSG_RECONF_PEER, p->conf.id, 0, -1,
438		    &p->conf, sizeof(struct peer_config)) == -1)
439			return (-1);
440
441	TAILQ_FOREACH(la, conf->listen_addrs, entry) {
442		if (imsg_compose(ibuf_se, IMSG_RECONF_LISTENER, 0, 0, la->fd,
443		    la, sizeof(struct listen_addr)) == -1)
444			return (-1);
445		la->fd = -1;
446	}
447
448	/* networks for the RDE */
449	while ((n = TAILQ_FIRST(&net_l)) != NULL) {
450		if (imsg_compose(ibuf_rde, IMSG_NETWORK_ADD, 0, 0, -1,
451		    &n->net, sizeof(struct network_config)) == -1)
452			return (-1);
453		if (send_filterset(ibuf_rde, &n->net.attrset, 0) == -1)
454			return (-1);
455		if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1,
456		    NULL, 0) == -1)
457			return (-1);
458		TAILQ_REMOVE(&net_l, n, entry);
459		filterset_free(&n->net.attrset);
460		free(n);
461	}
462
463	/* redistribute list needs to be reloaded too */
464	if (kr_redist_reload() == -1)
465		return (-1);
466
467	/* filters for the RDE */
468	while ((r = TAILQ_FIRST(rules_l)) != NULL) {
469		if (imsg_compose(ibuf_rde, IMSG_RECONF_FILTER, 0, 0, -1,
470		    r, sizeof(struct filter_rule)) == -1)
471			return (-1);
472		if (send_filterset(ibuf_rde, &r->set, 0) == -1)
473			return (-1);
474		TAILQ_REMOVE(rules_l, r, entry);
475		filterset_free(&r->set);
476		free(r);
477	}
478
479	/* singal both childs to replace their config */
480	if (imsg_compose(ibuf_se, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1 ||
481	    imsg_compose(ibuf_rde, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1)
482		return (-1);
483
484	/* mrt changes can be sent out of bound */
485	mrt_reconfigure(mrt_l);
486	return (0);
487}
488
489int
490dispatch_imsg(struct imsgbuf *ibuf, int idx)
491{
492	struct imsg		 imsg;
493	int			 n;
494	int			 rv;
495
496	if ((n = imsg_read(ibuf)) == -1)
497		return (-1);
498
499	if (n == 0) {	/* connection closed */
500		log_warnx("dispatch_imsg in main: pipe closed");
501		return (-1);
502	}
503
504	rv = 0;
505	for (;;) {
506		if ((n = imsg_get(ibuf, &imsg)) == -1)
507			return (-1);
508
509		if (n == 0)
510			break;
511
512		switch (imsg.hdr.type) {
513		case IMSG_KROUTE_CHANGE:
514			if (idx != PFD_PIPE_ROUTE)
515				log_warnx("route request not from RDE");
516			else if (kr_change(imsg.data))
517				rv = -1;
518			break;
519		case IMSG_KROUTE_DELETE:
520			if (idx != PFD_PIPE_ROUTE)
521				log_warnx("route request not from RDE");
522			else if (kr_delete(imsg.data))
523				rv = -1;
524			break;
525		case IMSG_KROUTE6_CHANGE:
526			if (idx != PFD_PIPE_ROUTE)
527				log_warnx("route request not from RDE");
528			else if (kr6_change(imsg.data))
529				rv = -1;
530			break;
531		case IMSG_KROUTE6_DELETE:
532			if (idx != PFD_PIPE_ROUTE)
533				log_warnx("route request not from RDE");
534			else if (kr6_delete(imsg.data))
535				rv = -1;
536			break;
537		case IMSG_NEXTHOP_ADD:
538			if (idx != PFD_PIPE_ROUTE)
539				log_warnx("nexthop request not from RDE");
540			else
541				if (imsg.hdr.len != IMSG_HEADER_SIZE +
542				    sizeof(struct bgpd_addr))
543					log_warnx("wrong imsg len");
544				else if (kr_nexthop_add(imsg.data) == -1)
545					rv = -1;
546			break;
547		case IMSG_NEXTHOP_REMOVE:
548			if (idx != PFD_PIPE_ROUTE)
549				log_warnx("nexthop request not from RDE");
550			else
551				if (imsg.hdr.len != IMSG_HEADER_SIZE +
552				    sizeof(struct bgpd_addr))
553					log_warnx("wrong imsg len");
554				else
555					kr_nexthop_delete(imsg.data);
556			break;
557		case IMSG_PFTABLE_ADD:
558			if (idx != PFD_PIPE_ROUTE)
559				log_warnx("pftable request not from RDE");
560			else
561				if (imsg.hdr.len != IMSG_HEADER_SIZE +
562				    sizeof(struct pftable_msg))
563					log_warnx("wrong imsg len");
564				else if (pftable_addr_add(imsg.data) != 0)
565					rv = -1;
566			break;
567		case IMSG_PFTABLE_REMOVE:
568			if (idx != PFD_PIPE_ROUTE)
569				log_warnx("pftable request not from RDE");
570			else
571				if (imsg.hdr.len != IMSG_HEADER_SIZE +
572				    sizeof(struct pftable_msg))
573					log_warnx("wrong imsg len");
574				else if (pftable_addr_remove(imsg.data) != 0)
575					rv = -1;
576			break;
577		case IMSG_PFTABLE_COMMIT:
578			if (idx != PFD_PIPE_ROUTE)
579				log_warnx("pftable request not from RDE");
580			else
581				if (imsg.hdr.len != IMSG_HEADER_SIZE)
582					log_warnx("wrong imsg len");
583				else if (pftable_commit() != 0)
584					rv = -1;
585			break;
586		case IMSG_CTL_RELOAD:
587			if (idx != PFD_PIPE_SESSION)
588				log_warnx("reload request not from SE");
589			else
590				reconfig = 1;
591			break;
592		case IMSG_CTL_FIB_COUPLE:
593			if (idx != PFD_PIPE_SESSION)
594				log_warnx("couple request not from SE");
595			else
596				kr_fib_couple();
597			break;
598		case IMSG_CTL_FIB_DECOUPLE:
599			if (idx != PFD_PIPE_SESSION)
600				log_warnx("decouple request not from SE");
601			else
602				kr_fib_decouple();
603			break;
604		case IMSG_CTL_KROUTE:
605		case IMSG_CTL_KROUTE_ADDR:
606		case IMSG_CTL_SHOW_NEXTHOP:
607		case IMSG_CTL_SHOW_INTERFACE:
608			if (idx != PFD_PIPE_SESSION)
609				log_warnx("kroute request not from SE");
610			else
611				kr_show_route(&imsg);
612			break;
613		case IMSG_IFINFO:
614			if (idx != PFD_PIPE_SESSION)
615				log_warnx("IFINFO request not from SE");
616			else if (imsg.hdr.len != IMSG_HEADER_SIZE + IFNAMSIZ)
617				log_warnx("IFINFO request with wrong len");
618			else
619				kr_ifinfo(imsg.data);
620			break;
621		default:
622			break;
623		}
624		imsg_free(&imsg);
625		if (rv != 0)
626			return (rv);
627	}
628	return (0);
629}
630
631void
632send_nexthop_update(struct kroute_nexthop *msg)
633{
634	char	*gw = NULL;
635
636	if (msg->gateway.af)
637		if (asprintf(&gw, ": via %s",
638		    log_addr(&msg->gateway)) == -1) {
639			log_warn("send_nexthop_update");
640			quit = 1;
641		}
642
643	log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop),
644	    msg->valid ? "valid" : "invalid",
645	    msg->connected ? ": directly connected" : "",
646	    msg->gateway.af ? gw : "");
647
648	free(gw);
649
650	if (imsg_compose(ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 0, -1,
651	    msg, sizeof(struct kroute_nexthop)) == -1)
652		quit = 1;
653}
654
655void
656send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen)
657{
658	imsg_compose(ibuf_se, type, 0, pid, -1, data, datalen);
659}
660
661int
662bgpd_redistribute(int type, struct kroute *kr, struct kroute6 *kr6)
663{
664	struct network_config	 net;
665	struct filter_set_head	*h;
666
667	if ((cflags & BGPD_FLAG_REDIST_CONNECTED) && kr &&
668	    (kr->flags & F_CONNECTED))
669		h = connectset;
670	else if ((cflags & BGPD_FLAG_REDIST_STATIC) && kr &&
671	    (kr->flags & F_STATIC))
672		h = staticset;
673	else if ((cflags & BGPD_FLAG_REDIST6_CONNECTED) && kr6 &&
674	    (kr6->flags & F_CONNECTED))
675		h = connectset6;
676	else if ((cflags & BGPD_FLAG_REDIST6_STATIC) && kr6 &&
677	    (kr6->flags & F_STATIC))
678		h = staticset6;
679	else
680		return (0);
681
682	bzero(&net, sizeof(net));
683	if (kr && kr6)
684		fatalx("bgpd_redistribute: unable to redistribute v4 and v6"
685		    "together");
686	if (kr != NULL) {
687		net.prefix.af = AF_INET;
688		net.prefix.v4.s_addr = kr->prefix.s_addr;
689		net.prefixlen = kr->prefixlen;
690	}
691	if (kr6 != NULL) {
692		net.prefix.af = AF_INET6;
693		memcpy(&net.prefix.v6, &kr6->prefix, sizeof(struct in6_addr));
694		net.prefixlen = kr6->prefixlen;
695	}
696
697
698	if (imsg_compose(ibuf_rde, type, 0, 0, -1, &net,
699	    sizeof(struct network_config)) == -1)
700		return (-1);
701
702	/* networks that get deleted don't need to send the filter set */
703	if (type == IMSG_NETWORK_REMOVE)
704		return (1);
705
706	if (send_filterset(ibuf_rde, h, 0) == -1)
707		return (-1);
708	if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1, NULL, 0) == -1)
709		return (-1);
710
711	return (1);
712}
713
714