bgpd.c revision 1.69
1/* $OpenBSD: bgpd.c,v 1.69 2004/01/20 09:44:33 henning 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); 40void usage(void); 41int main(int, char *[]); 42int check_child(pid_t, const char *); 43int reconfigure(char *, struct bgpd_config *, struct mrt_head *, 44 struct peer *); 45int dispatch_imsg(struct imsgbuf *, int, struct mrt_head *); 46 47int rfd = -1; 48volatile sig_atomic_t mrtdump = 0; 49volatile sig_atomic_t quit = 0; 50volatile sig_atomic_t reconfig = 0; 51volatile sig_atomic_t sigchld = 0; 52struct imsgbuf ibuf_se; 53struct imsgbuf ibuf_rde; 54 55void 56sighdlr(int sig) 57{ 58 switch (sig) { 59 case SIGTERM: 60 case SIGINT: 61 quit = 1; 62 break; 63 case SIGCHLD: 64 sigchld = 1; 65 break; 66 case SIGHUP: 67 reconfig = 1; 68 break; 69 case SIGALRM: 70 case SIGUSR1: 71 mrtdump = 1; 72 break; 73 } 74} 75 76void 77usage(void) 78{ 79 extern char *__progname; 80 81 fprintf(stderr, "usage: %s [-dnv] ", __progname); 82 fprintf(stderr, "[-D macro=value] [-f file]\n"); 83 exit(1); 84} 85 86#define POLL_MAX 8 87#define PFD_PIPE_SESSION 0 88#define PFD_PIPE_ROUTE 1 89#define PFD_SOCK_ROUTE 2 90#define PFD_MRT_START 3 91 92int 93main(int argc, char *argv[]) 94{ 95 struct bgpd_config conf; 96 struct peer *peer_l, *p, *next; 97 struct mrt_head mrt_l; 98 struct network_head net_l; 99 struct network *net; 100 struct mrt *(mrt[POLL_MAX]); 101 struct pollfd pfd[POLL_MAX]; 102 pid_t io_pid = 0, rde_pid = 0, pid; 103 char *conffile; 104 int debug = 0; 105 int ch, csock, i, j, n, nfds, timeout; 106 int pipe_m2s[2]; 107 int pipe_m2r[2]; 108 int pipe_s2r[2]; 109 110 conffile = CONFFILE; 111 bgpd_process = PROC_MAIN; 112 113 log_init(1); /* log to stderr until daemonized */ 114 115 bzero(&conf, sizeof(conf)); 116 LIST_INIT(&mrt_l); 117 TAILQ_INIT(&net_l); 118 peer_l = NULL; 119 120 while ((ch = getopt(argc, argv, "dD:f:nv")) != -1) { 121 switch (ch) { 122 case 'd': 123 debug = 1; 124 break; 125 case 'D': 126 if (cmdline_symset(optarg) < 0) 127 logit(LOG_CRIT, 128 "could not parse macro definition %s", 129 optarg); 130 break; 131 case 'f': 132 conffile = optarg; 133 break; 134 case 'n': 135 conf.opts |= BGPD_OPT_NOACTION; 136 break; 137 case 'v': 138 if (conf.opts & BGPD_OPT_VERBOSE) 139 conf.opts |= BGPD_OPT_VERBOSE2; 140 conf.opts |= BGPD_OPT_VERBOSE; 141 break; 142 default: 143 usage(); 144 /* NOTREACHED */ 145 } 146 } 147 148 if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l)) 149 exit(1); 150 151 if (conf.opts & BGPD_OPT_NOACTION) { 152 fprintf(stderr, "configuration OK\n"); 153 exit(0); 154 } 155 156 if (geteuid()) 157 errx(1, "need root privileges"); 158 159 if (getpwnam(BGPD_USER) == NULL) 160 errx(1, "unknown user %s", BGPD_USER); 161 endpwent(); 162 163 log_init(debug); 164 165 if (!debug) 166 daemon(1, 0); 167 168 logit(LOG_INFO, "startup"); 169 170 if (pipe(pipe_m2s) == -1) 171 fatal("pipe"); 172 if (fcntl(pipe_m2s[0], F_SETFL, O_NONBLOCK) == -1 || 173 fcntl(pipe_m2s[1], F_SETFL, O_NONBLOCK) == -1) 174 fatal("fcntl"); 175 if (pipe(pipe_m2r) == -1) 176 fatal("pipe"); 177 if (fcntl(pipe_m2r[0], F_SETFL, O_NONBLOCK) == -1 || 178 fcntl(pipe_m2r[1], F_SETFL, O_NONBLOCK) == -1) 179 fatal("fcntl"); 180 if (pipe(pipe_s2r) == -1) 181 fatal("pipe"); 182 if (fcntl(pipe_s2r[0], F_SETFL, O_NONBLOCK) == -1 || 183 fcntl(pipe_s2r[1], F_SETFL, O_NONBLOCK) == -1) 184 fatal("fcntl"); 185 186 if ((csock = control_init()) == -1) 187 fatalx("control socket setup failed"); 188 189 /* fork children */ 190 rde_pid = rde_main(&conf, peer_l, &net_l, pipe_m2r, pipe_s2r); 191 io_pid = session_main(&conf, peer_l, pipe_m2s, pipe_s2r); 192 193 setproctitle("parent"); 194 195 signal(SIGTERM, sighdlr); 196 signal(SIGINT, sighdlr); 197 signal(SIGCHLD, sighdlr); 198 signal(SIGHUP, sighdlr); 199 signal(SIGALRM, sighdlr); 200 signal(SIGUSR1, sighdlr); 201 202 close(pipe_m2s[1]); 203 close(pipe_m2r[1]); 204 close(pipe_s2r[0]); 205 close(pipe_s2r[1]); 206 close(csock); 207 208 imsg_init(&ibuf_se, pipe_m2s[0]); 209 imsg_init(&ibuf_rde, pipe_m2r[0]); 210 mrt_init(&ibuf_rde, &ibuf_se); 211 if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE))) == -1) 212 quit = 1; 213 214 for (p = peer_l; p != NULL; p = next) { 215 next = p->next; 216 free(p); 217 } 218 for (net = TAILQ_FIRST(&net_l); net != TAILQ_END(&net_l); 219 net = TAILQ_FIRST(&net_l)) { 220 TAILQ_REMOVE(&net_l, net, network_l); 221 free(net); 222 } 223 224 while (quit == 0) { 225 pfd[PFD_PIPE_SESSION].fd = ibuf_se.sock; 226 pfd[PFD_PIPE_SESSION].events = POLLIN; 227 if (ibuf_se.w.queued) 228 pfd[PFD_PIPE_SESSION].events |= POLLOUT; 229 pfd[PFD_PIPE_ROUTE].fd = ibuf_rde.sock; 230 pfd[PFD_PIPE_ROUTE].events = POLLIN; 231 if (ibuf_rde.w.queued) 232 pfd[PFD_PIPE_ROUTE].events |= POLLOUT; 233 pfd[PFD_SOCK_ROUTE].fd = rfd; 234 pfd[PFD_SOCK_ROUTE].events = POLLIN; 235 i = PFD_MRT_START; 236 i = mrt_select(&mrt_l, pfd, mrt, i, POLL_MAX, &timeout); 237 238 if ((nfds = poll(pfd, i, INFTIM)) == -1) 239 if (errno != EINTR) { 240 log_err("poll error"); 241 quit = 1; 242 } 243 244 if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT)) 245 if ((n = msgbuf_write(&ibuf_se.w)) < 0) { 246 log_err("pipe write error (to SE)"); 247 quit = 1; 248 } 249 250 if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)) 251 if ((n = msgbuf_write(&ibuf_rde.w)) < 0) { 252 log_err("pipe write error (to RDE)"); 253 quit = 1; 254 } 255 256 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) { 257 nfds--; 258 if (dispatch_imsg(&ibuf_se, PFD_PIPE_SESSION, 259 &mrt_l) == -1) 260 quit = 1; 261 } 262 263 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) { 264 nfds--; 265 if (dispatch_imsg(&ibuf_rde, PFD_PIPE_ROUTE, 266 &mrt_l) == -1) 267 quit = 1; 268 } 269 270 if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) { 271 nfds--; 272 if (kr_dispatch_msg() == -1) 273 quit = 1; 274 } 275 276 for (j = PFD_MRT_START; j < i && nfds > 0 ; j++) { 277 if (pfd[j].revents & POLLOUT) { 278 if ((n = mrt_write(mrt[j])) < 0) { 279 log_err("mrt write error"); 280 } 281 } 282 } 283 284 if (reconfig) { 285 logit(LOG_CRIT, "rereading config"); 286 reconfigure(conffile, &conf, &mrt_l, peer_l); 287 reconfig = 0; 288 } 289 290 if (sigchld) { 291 if (check_child(io_pid, "session engine")) 292 quit = 1; 293 if (check_child(rde_pid, "route decision engine")) 294 quit = 1; 295 sigchld = 0; 296 } 297 298 if (mrtdump == 1) { 299 mrt_handler(&mrt_l); 300 mrtdump = 0; 301 } 302 } 303 304 signal(SIGCHLD, SIG_IGN); 305 306 if (io_pid) 307 kill(io_pid, SIGTERM); 308 309 if (rde_pid) 310 kill(rde_pid, SIGTERM); 311 312 do { 313 pid = waitpid(-1, NULL, WNOHANG); 314 } while (pid > 0 || (pid == -1 && errno == EINTR)); 315 316 control_cleanup(); 317 kr_shutdown(); 318 319 logit(LOG_CRIT, "Terminating"); 320 return (0); 321} 322 323int 324check_child(pid_t pid, const char *pname) 325{ 326 int status; 327 328 if (waitpid(pid, &status, WNOHANG) > 0) { 329 if (WIFEXITED(status)) { 330 logit(LOG_CRIT, "Lost child: %s exited", pname); 331 return (1); 332 } 333 if (WIFSIGNALED(status)) { 334 logit(LOG_CRIT, "Lost child: %s terminated; signal %d", 335 pname, WTERMSIG(status)); 336 return (1); 337 } 338 } 339 340 return (0); 341} 342 343int 344reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l, 345 struct peer *peer_l) 346{ 347 struct network_head net_l; 348 struct network *n; 349 struct peer *p, *next; 350 351 if (parse_config(conffile, conf, mrt_l, &peer_l, &net_l)) { 352 logit(LOG_CRIT, "config file %s has errors, not reloading", 353 conffile); 354 return (-1); 355 } 356 357 if (imsg_compose(&ibuf_se, IMSG_RECONF_CONF, 0, 358 conf, sizeof(struct bgpd_config)) == -1) 359 return (-1); 360 if (imsg_compose(&ibuf_rde, IMSG_RECONF_CONF, 0, 361 conf, sizeof(struct bgpd_config)) == -1) 362 return (-1); 363 for (p = peer_l; p != NULL; p = next) { 364 next = p->next; 365 if (imsg_compose(&ibuf_se, IMSG_RECONF_PEER, p->conf.id, 366 &p->conf, sizeof(struct peer_config)) == -1) 367 return (-1); 368 if (imsg_compose(&ibuf_rde, IMSG_RECONF_PEER, p->conf.id, 369 &p->conf, sizeof(struct peer_config)) == -1) 370 return (-1); 371 free(p); 372 } 373 for (n = TAILQ_FIRST(&net_l); n != TAILQ_END(&net_l); 374 n = TAILQ_FIRST(&net_l)) { 375 if (imsg_compose(&ibuf_rde, IMSG_RECONF_NETWORK, 0, 376 &n->net, sizeof(struct network_config)) == -1) 377 return (-1); 378 TAILQ_REMOVE(&net_l, n, network_l); 379 free(n); 380 } 381 if (imsg_compose(&ibuf_se, IMSG_RECONF_DONE, 0, NULL, 0) == -1 || 382 imsg_compose(&ibuf_rde, IMSG_RECONF_DONE, 0, NULL, 0) == -1) 383 return (-1); 384 385 return (0); 386} 387 388int 389dispatch_imsg(struct imsgbuf *ibuf, int idx, struct mrt_head *mrt_l) 390{ 391 struct imsg imsg; 392 int n; 393 394 if ((n = imsg_read(ibuf)) == -1) 395 return (-1); 396 397 if (n == 0) { /* connection closed */ 398 logit(LOG_CRIT, "dispatch_imsg in main: pipe closed"); 399 return (-1); 400 } 401 402 for (;;) { 403 if ((n = imsg_get(ibuf, &imsg)) == -1) 404 return (-1); 405 406 if (n == 0) 407 break; 408 409 switch (imsg.hdr.type) { 410 case IMSG_MRT_MSG: 411 case IMSG_MRT_END: 412 if (mrt_queue(mrt_l, &imsg) == -1) 413 logit(LOG_CRIT, "mrt_queue failed."); 414 break; 415 case IMSG_KROUTE_CHANGE: 416 if (idx != PFD_PIPE_ROUTE) 417 logit(LOG_CRIT, "route request not from RDE"); 418 else if (kr_change(imsg.data)) 419 return (-1); 420 break; 421 case IMSG_KROUTE_DELETE: 422 if (idx != PFD_PIPE_ROUTE) 423 logit(LOG_CRIT, "route request not from RDE"); 424 else if (kr_delete(imsg.data)) 425 return (-1); 426 break; 427 case IMSG_NEXTHOP_ADD: 428 if (idx != PFD_PIPE_ROUTE) 429 logit(LOG_CRIT, "nexthop request not from RDE"); 430 else 431 if (imsg.hdr.len != IMSG_HEADER_SIZE + 432 sizeof(struct bgpd_addr)) 433 logit(LOG_CRIT, "wrong imsg len"); 434 else if (kr_nexthop_add(imsg.data) == -1) 435 return (-1); 436 break; 437 case IMSG_NEXTHOP_REMOVE: 438 if (idx != PFD_PIPE_ROUTE) 439 logit(LOG_CRIT, "nexthop request not from RDE"); 440 else 441 if (imsg.hdr.len != IMSG_HEADER_SIZE + 442 sizeof(struct bgpd_addr)) 443 logit(LOG_CRIT, "wrong imsg len"); 444 else kr_nexthop_delete(imsg.data); 445 break; 446 case IMSG_CTL_RELOAD: 447 if (idx != PFD_PIPE_SESSION) 448 logit(LOG_CRIT, "reload request not from SE"); 449 else 450 reconfig = 1; 451 break; 452 case IMSG_CTL_FIB_COUPLE: 453 if (idx != PFD_PIPE_SESSION) 454 logit(LOG_CRIT, "couple request not from SE"); 455 else 456 kr_fib_couple(); 457 break; 458 case IMSG_CTL_FIB_DECOUPLE: 459 if (idx != PFD_PIPE_SESSION) 460 logit(LOG_CRIT, "decouple request not from SE"); 461 else 462 kr_fib_decouple(); 463 break; 464 case IMSG_CTL_KROUTE: 465 case IMSG_CTL_KROUTE_ADDR: 466 case IMSG_CTL_SHOW_NEXTHOP: 467 case IMSG_CTL_SHOW_INTERFACE: 468 if (idx != PFD_PIPE_SESSION) 469 logit(LOG_CRIT, "kroute request not from SE"); 470 else 471 kr_show_route(&imsg); 472 break; 473 default: 474 break; 475 } 476 imsg_free(&imsg); 477 } 478 return (0); 479} 480 481void 482send_nexthop_update(struct kroute_nexthop *msg) 483{ 484 char *gw = NULL, *nh = NULL; 485 486 if (msg->gateway.af == AF_INET) 487 if (asprintf(&gw, ": via %s", 488 log_ntoa(msg->gateway.v4.s_addr)) == -1) { 489 log_err("send_nexthop_update"); 490 quit = 1; 491 } 492 493 if (msg->nexthop.af == AF_INET) 494 nh = log_ntoa(msg->nexthop.v4.s_addr); 495 496 logit(LOG_INFO, "nexthop %s now %s%s%s", nh, 497 msg->valid ? "valid" : "invalid", 498 msg->connected ? ": directly connected" : "", 499 msg->gateway.af ? gw : ""); 500 501 free(gw); 502 503 if (imsg_compose(&ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 504 msg, sizeof(struct kroute_nexthop)) == -1) 505 quit = 1; 506} 507 508void 509send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen) 510{ 511 imsg_compose_pid(&ibuf_se, type, pid, data, datalen); 512} 513 514 515