bgpd.c revision 1.34
1/* $OpenBSD: bgpd.c,v 1.34 2003/12/26 14:55:03 henning Exp $ */ 2 3/* 4 * Copyright (c) 2003 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 <signal.h> 29#include <stdio.h> 30#include <stdlib.h> 31#include <string.h> 32#include <unistd.h> 33 34#include "mrt.h" 35#include "bgpd.h" 36 37void sighdlr(int); 38void usage(void); 39int main(int, char *[]); 40int reconfigure(char *, struct bgpd_config *, struct mrt_config *); 41int dispatch_imsg(struct imsgbuf *, int, struct mrt_config *); 42 43int mrtfd = -1; 44int rfd = -1; 45volatile sig_atomic_t mrtdump = 0; 46volatile sig_atomic_t quit = 0; 47volatile sig_atomic_t reconfig = 0; 48struct imsgbuf ibuf_se; 49struct imsgbuf ibuf_rde; 50 51void 52sighdlr(int sig) 53{ 54 switch (sig) { 55 case SIGTERM: 56 case SIGINT: 57 case SIGCHLD: 58 quit = 1; 59 break; 60 case SIGHUP: 61 reconfig = 1; 62 break; 63 case SIGALRM: 64 mrtdump = 1; 65 break; 66 case SIGUSR1: 67 mrtdump = 2; 68 break; 69 } 70} 71 72void 73usage(void) 74{ 75 extern char *__progname; 76 77 fprintf(stderr, "usage: %s [-dnv] ", __progname); 78 fprintf(stderr, "[-D macro=value] [-f file]\n"); 79 exit(1); 80} 81 82#define POLL_MAX 8 83#define PFD_PIPE_SESSION 0 84#define PFD_PIPE_ROUTE 1 85#define PFD_SOCK_ROUTE 2 86#define PFD_MRT_START 3 87 88int 89main(int argc, char *argv[]) 90{ 91 struct bgpd_config conf; 92 struct mrt_config mrtconf; 93 struct mrtdump_config *mconf, *(mrt[POLL_MAX]); 94 struct pollfd pfd[POLL_MAX]; 95 pid_t io_pid = 0, rde_pid = 0; 96 char *conffile; 97 int debug = 0; 98 int ch, i, j, n, nfds; 99 int pipe_m2s[2]; 100 int pipe_m2r[2]; 101 int pipe_s2r[2]; 102 103 conffile = CONFFILE; 104 bgpd_process = PROC_MAIN; 105 106 log_init(1); /* log to stderr until daemonized */ 107 108 bzero(&conf, sizeof(conf)); 109 bzero(&mrtconf, sizeof(mrtconf)); 110 LIST_INIT(&mrtconf); 111 112 while ((ch = getopt(argc, argv, "dD:f:nv")) != -1) { 113 switch (ch) { 114 case 'd': 115 debug = 1; 116 break; 117 case 'D': 118 if (cmdline_symset(optarg) < 0) 119 logit(LOG_CRIT, 120 "could not parse macro definition %s", 121 optarg); 122 break; 123 case 'f': 124 conffile = optarg; 125 break; 126 case 'n': 127 conf.opts |= BGPD_OPT_NOACTION; 128 break; 129 case 'v': 130 if (conf.opts & BGPD_OPT_VERBOSE) 131 conf.opts |= BGPD_OPT_VERBOSE2; 132 conf.opts |= BGPD_OPT_VERBOSE; 133 break; 134 default: 135 usage(); 136 /* NOTREACHED */ 137 } 138 } 139 140 if (parse_config(conffile, &conf, &mrtconf)) 141 exit(1); 142 143 if (conf.opts & BGPD_OPT_NOACTION) { 144 fprintf(stderr, "configuration OK\n"); 145 exit(0); 146 } 147 148 if (geteuid()) 149 errx(1, "need root privileges"); 150 151 log_init(debug); 152 153 if (!debug) 154 daemon(1, 0); 155 156 logit(LOG_INFO, "startup"); 157 158 if (pipe(pipe_m2s) == -1) 159 fatal("pipe", errno); 160 if (fcntl(pipe_m2s[0], F_SETFL, O_NONBLOCK) == -1 || 161 fcntl(pipe_m2s[1], F_SETFL, O_NONBLOCK) == -1) 162 fatal("fcntl", errno); 163 if (pipe(pipe_m2r) == -1) 164 fatal("pipe", errno); 165 if (fcntl(pipe_m2r[0], F_SETFL, O_NONBLOCK) == -1 || 166 fcntl(pipe_m2r[1], F_SETFL, O_NONBLOCK) == -1) 167 fatal("fcntl", errno); 168 if (pipe(pipe_s2r) == -1) 169 fatal("pipe", errno); 170 if (fcntl(pipe_s2r[0], F_SETFL, O_NONBLOCK) == -1 || 171 fcntl(pipe_s2r[1], F_SETFL, O_NONBLOCK) == -1) 172 fatal("fcntl", errno); 173 174 if ((rde_pid = rde_main(&conf, pipe_m2r, pipe_s2r)) < 0) 175 fatal("could not start route decision engine", 0); 176 177 if ((io_pid = session_main(&conf, pipe_m2s, pipe_s2r)) < 0) 178 fatal("could not start session engine", 0); 179 180 setproctitle("parent"); 181 182 signal(SIGTERM, sighdlr); 183 signal(SIGINT, sighdlr); 184 signal(SIGCHLD, sighdlr); 185 signal(SIGHUP, sighdlr); 186 signal(SIGALRM, sighdlr); 187 signal(SIGUSR1, sighdlr); 188 189 close(pipe_m2s[1]); 190 close(pipe_m2r[1]); 191 close(pipe_s2r[0]); 192 close(pipe_s2r[1]); 193 194 imsg_init(&ibuf_se, pipe_m2s[0]); 195 imsg_init(&ibuf_rde, pipe_m2r[0]); 196 rfd = kroute_init(); 197 198 while (quit == 0) { 199 pfd[PFD_PIPE_SESSION].fd = ibuf_se.sock; 200 pfd[PFD_PIPE_SESSION].events = POLLIN; 201 if (ibuf_se.w.queued) 202 pfd[PFD_PIPE_SESSION].events |= POLLOUT; 203 pfd[PFD_PIPE_ROUTE].fd = ibuf_rde.sock; 204 pfd[PFD_PIPE_ROUTE].events = POLLIN; 205 if (ibuf_rde.w.queued) 206 pfd[PFD_PIPE_ROUTE].events |= POLLOUT; 207 pfd[PFD_SOCK_ROUTE].fd = rfd; 208 pfd[PFD_SOCK_ROUTE].events = POLLIN; 209 i = PFD_MRT_START; 210 LIST_FOREACH(mconf, &mrtconf, list) 211 if (mconf->msgbuf.queued > 0) { 212 pfd[i].fd = mconf->msgbuf.sock; 213 pfd[i].events |= POLLOUT; 214 mrt[i++] = mconf; 215 } 216 217 if ((nfds = poll(pfd, i, INFTIM)) == -1) 218 if (errno != EINTR) 219 fatal("poll error", errno); 220 221 if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT)) 222 if ((n = msgbuf_write(&ibuf_se.w)) < 0) 223 fatal("pipe write error", errno); 224 225 if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)) 226 if ((n = msgbuf_write(&ibuf_rde.w)) < 0) 227 fatal("pipe write error", errno); 228 229 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) { 230 nfds--; 231 dispatch_imsg(&ibuf_se, PFD_PIPE_SESSION, &mrtconf); 232 } 233 234 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) { 235 nfds--; 236 dispatch_imsg(&ibuf_rde, PFD_PIPE_ROUTE, &mrtconf); 237 } 238 239 if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) { 240 nfds--; 241 kroute_dispatch_msg(rfd); 242 } 243 244 for (j = PFD_MRT_START; j < i && nfds > 0 ; j++) { 245 if (pfd[j].revents & POLLOUT) { 246 if ((n = msgbuf_write(&mrt[i]->msgbuf)) < 0) 247 fatal("pipe write error", errno); 248 } 249 } 250 251 if (reconfig) { 252 logit(LOG_CRIT, "rereading config"); 253 reconfigure(conffile, &conf, &mrtconf); 254 LIST_FOREACH(mconf, &mrtconf, list) 255 mrt_state(mconf, IMSG_NONE, &ibuf_rde); 256 reconfig = 0; 257 } 258 259 if (mrtdump == 1) { 260 mrt_alrm(&mrtconf, &ibuf_rde); 261 mrtdump = 0; 262 } else if (mrtdump == 2) { 263 mrt_usr1(&mrtconf, &ibuf_rde); 264 mrtdump = 0; 265 } 266 } 267 268 signal(SIGCHLD, SIG_IGN); 269 270 if (io_pid) 271 kill(io_pid, SIGTERM); 272 273 if (rde_pid) 274 kill(rde_pid, SIGTERM); 275 276 do { 277 i = waitpid(-1, NULL, WNOHANG); 278 } while (i > 0 || (i == -1 && errno == EINTR)); 279 280 kroute_shutdown(rfd); 281 282 logit(LOG_CRIT, "Terminating"); 283 return (0); 284} 285 286int 287reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_config *mrtc) 288{ 289 struct peer *p; 290 291 if (parse_config(conffile, conf, mrtc)) { 292 logit(LOG_CRIT, "config file %s has errors, not reloading", 293 conffile); 294 return (-1); 295 } 296 imsg_compose(&ibuf_se, IMSG_RECONF_CONF, 0, 297 conf, sizeof(struct bgpd_config)); 298 imsg_compose(&ibuf_rde, IMSG_RECONF_CONF, 0, 299 conf, sizeof(struct bgpd_config)); 300 for (p = conf->peers; p != NULL; p = p->next) { 301 imsg_compose(&ibuf_se, IMSG_RECONF_PEER, p->conf.id, 302 &p->conf, sizeof(struct peer_config)); 303 imsg_compose(&ibuf_rde, IMSG_RECONF_PEER, p->conf.id, 304 &p->conf, sizeof(struct peer_config)); 305 } 306 imsg_compose(&ibuf_se, IMSG_RECONF_DONE, 0, NULL, 0); 307 imsg_compose(&ibuf_rde, IMSG_RECONF_DONE, 0, NULL, 0); 308 309 return (0); 310} 311 312/* 313 * XXX currently messages are only buffered for mrt files. 314 */ 315int 316dispatch_imsg(struct imsgbuf *ibuf, int idx, struct mrt_config *conf) 317{ 318 struct imsg imsg; 319 struct buf *wbuf; 320 struct mrtdump_config *m; 321 ssize_t len; 322 int n; 323 in_addr_t ina; 324 325 if (imsg_get(ibuf, &imsg) > 0) { 326 switch (imsg.hdr.type) { 327 case IMSG_MRT_MSG: 328 case IMSG_MRT_END: 329 LIST_FOREACH(m, conf, list) { 330 if (m->id != imsg.hdr.peerid) 331 continue; 332 if (mrt_state(m, imsg.hdr.type, ibuf) == 0) 333 break; 334 if (m->msgbuf.sock == -1) 335 break; 336 len = imsg.hdr.len - IMSG_HEADER_SIZE; 337 wbuf = buf_open(len); 338 if (wbuf == NULL) 339 fatal("buf_open error", 0); 340 if (buf_add(wbuf, imsg.data, len) == -1) 341 fatal("buf_add error", 0); 342 if ((n = buf_close(&m->msgbuf, wbuf)) < 0) 343 fatal("buf_close error", 0); 344 break; 345 } 346 break; 347 case IMSG_KROUTE_CHANGE: 348 if (idx != PFD_PIPE_ROUTE) 349 fatal("route request not from RDE", 0); 350 if (kroute_change(rfd, imsg.data)) 351 fatal("kroute_change error", errno); 352 break; 353 case IMSG_KROUTE_DELETE: 354 if (idx != PFD_PIPE_ROUTE) 355 fatal("route request not from RDE", 0); 356 if (kroute_delete(rfd, imsg.data)) 357 fatal("kroute_delete error", errno); 358 break; 359 case IMSG_NEXTHOP_ADD: 360 if (idx != PFD_PIPE_ROUTE) 361 fatal("nexthop request not from RDE", 0); 362 memcpy(&ina, imsg.data, sizeof(ina)); 363 kroute_nexthop_add(ina); 364 break; 365 case IMSG_NEXTHOP_REMOVE: 366 if (idx != PFD_PIPE_ROUTE) 367 fatal("nexthop request not from RDE", 0); 368 memcpy(&ina, imsg.data, sizeof(ina)); 369 kroute_nexthop_delete(ina); 370 break; 371 default: 372 break; 373 } 374 imsg_free(&imsg); 375 } 376 return (0); 377} 378 379void 380send_nexthop_update(struct kroute_nexthop *msg) 381{ 382 logit(LOG_INFO, "nexthop %s now %s%s%s%s", log_ntoa(msg->nexthop), 383 msg->valid ? "valid" : "invalid", 384 msg->connected ? ": directly connected" : "", 385 msg->gateway ? ": via " : "", 386 msg->gateway ? log_ntoa(msg->gateway) : ""); 387 388 imsg_compose(&ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 389 msg, sizeof(struct kroute_nexthop)); 390} 391