]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_main.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bgpd / bgp_main.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/* Main routine of bgpd.
896014f4 3 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
896014f4 4 */
718e3744 5
6#include <zebra.h>
7
03014d48 8#include <pthread.h>
718e3744 9#include "vector.h"
718e3744 10#include "command.h"
11#include "getopt.h"
12#include "thread.h"
5e4fa164 13#include <lib/version.h>
718e3744 14#include "memory.h"
15#include "prefix.h"
16#include "log.h"
edd7c245 17#include "privs.h"
2d75d052 18#include "sigevent.h"
228da428
CC
19#include "zclient.h"
20#include "routemap.h"
21#include "filter.h"
22#include "plist.h"
8196f13d 23#include "stream.h"
3f9c7369 24#include "queue.h"
6a69b354 25#include "vrf.h"
567b877d 26#include "bfd.h"
4f04a76b 27#include "libfrr.h"
61cf4b37 28#include "ns.h"
718e3744 29
30#include "bgpd/bgpd.h"
31#include "bgpd/bgp_attr.h"
b2f0fa55 32#include "bgpd/bgp_route.h"
718e3744 33#include "bgpd/bgp_mplsvpn.h"
228da428
CC
34#include "bgpd/bgp_aspath.h"
35#include "bgpd/bgp_dump.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_nexthop.h"
38#include "bgpd/bgp_regex.h"
39#include "bgpd/bgp_clist.h"
40#include "bgpd/bgp_debug.h"
14454c9f 41#include "bgpd/bgp_errors.h"
228da428 42#include "bgpd/bgp_filter.h"
8196f13d 43#include "bgpd/bgp_zebra.h"
d3ecc69e 44#include "bgpd/bgp_packet.h"
03014d48 45#include "bgpd/bgp_keepalives.h"
61cf4b37 46#include "bgpd/bgp_network.h"
def31c13 47#include "bgpd/bgp_errors.h"
b4becb06 48#include "bgpd/bgp_script.h"
45a859f1 49#include "bgpd/bgp_evpn_mh.h"
c589d847 50#include "bgpd/bgp_nht.h"
48cb7ea9 51#include "bgpd/bgp_routemap_nb.h"
ed0e57e3 52#include "bgpd/bgp_community_alias.h"
718e3744 53
65efcfce 54#ifdef ENABLE_BGP_VNC
f8b6f499 55#include "bgpd/rfapi/rfapi_backend.h"
65efcfce
LB
56#endif
57
718e3744 58/* bgpd options, we use GNU getopt library. */
d62a17ae 59static const struct option longopts[] = {
60 {"bgp_port", required_argument, NULL, 'p'},
61 {"listenon", required_argument, NULL, 'l'},
d62a17ae 62 {"no_kernel", no_argument, NULL, 'n'},
63 {"skip_runas", no_argument, NULL, 'S'},
64 {"ecmp", required_argument, NULL, 'e'},
f533be73 65 {"int_num", required_argument, NULL, 'I'},
c0064d2a 66 {"no_zebra", no_argument, NULL, 'Z'},
c2d020ad 67 {"socket_size", required_argument, NULL, 's'},
d62a17ae 68 {0}};
718e3744 69
2d75d052 70/* signal definitions */
d62a17ae 71void sighup(void);
72void sigint(void);
73void sigusr1(void);
2d75d052 74
d62a17ae 75static void bgp_exit(int);
76static void bgp_vrf_terminate(void);
228da428 77
7cc91e67 78static struct frr_signal_t bgp_signals[] = {
d62a17ae 79 {
80 .signal = SIGHUP,
81 .handler = &sighup,
82 },
83 {
84 .signal = SIGUSR1,
85 .handler = &sigusr1,
86 },
87 {
88 .signal = SIGINT,
89 .handler = &sigint,
90 },
91 {
92 .signal = SIGTERM,
93 .handler = &sigint,
94 },
2d75d052 95};
96
edd7c245 97/* privileges */
996c9314
LB
98static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_NET_RAW,
99 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN};
edd7c245 100
d62a17ae 101struct zebra_privs_t bgpd_privs = {
b2f36157 102#if defined(FRR_USER) && defined(FRR_GROUP)
d62a17ae 103 .user = FRR_USER,
104 .group = FRR_GROUP,
d81fadfd 105#endif
106#ifdef VTY_GROUP
d62a17ae 107 .vty_group = VTY_GROUP,
edd7c245 108#endif
d62a17ae 109 .caps_p = _caps_p,
110 .cap_num_p = array_size(_caps_p),
111 .cap_num_i = 0,
edd7c245 112};
113
eb05883f
DL
114static struct frr_daemon_info bgpd_di;
115
718e3744 116/* SIGHUP handler. */
d62a17ae 117void sighup(void)
718e3744 118{
23ca3269
DS
119 zlog_info("SIGHUP received, ignoring");
120
121 return;
122
123 /*
124 * This is turned off for the moment. There is all
125 * sorts of config turned off by bgp_terminate
3130e286 126 * that is not setup properly again in bgp_reset.
23ca3269
DS
127 * I see no easy way to do this nor do I see that
128 * this is a desirable way to reload config
129 * given the yang work.
130 */
d62a17ae 131 /* Terminate all thread. */
3130e286
DS
132 /*
133 * bgp_terminate();
134 * bgp_reset();
135 * zlog_info("bgpd restarting!");
718e3744 136
3130e286
DS
137 * Reload config file.
138 * vty_read_config(NULL, bgpd_di.config_file, config_default);
139 */
d62a17ae 140 /* Try to return to normal operation. */
718e3744 141}
142
143/* SIGINT handler. */
d62a17ae 144__attribute__((__noreturn__)) void sigint(void)
718e3744 145{
d62a17ae 146 zlog_notice("Terminating on signal");
97b4a0ec
LB
147 assert(bm->terminating == false);
148 bm->terminating = true; /* global flag that shutting down */
718e3744 149
21bfce98
RZ
150 /* Disable BFD events to avoid wasting processing. */
151 bfd_protocol_integration_set_shutdown(true);
152
c8dde10f 153 bgp_terminate();
718e3744 154
d62a17ae 155 bgp_exit(0);
540766e7 156
d62a17ae 157 exit(0);
718e3744 158}
159
160/* SIGUSR1 handler. */
d62a17ae 161void sigusr1(void)
718e3744 162{
d62a17ae 163 zlog_rotate();
718e3744 164}
228da428
CC
165
166/*
167 Try to free up allocations we know about so that diagnostic tools such as
168 valgrind are able to better illuminate leaks.
169
170 Zebra route removal and protocol teardown are not meant to be done here.
171 For example, "retain_mode" may be set.
172*/
d62a17ae 173static __attribute__((__noreturn__)) void bgp_exit(int status)
228da428 174{
e2f3a930 175 struct bgp *bgp, *bgp_default, *bgp_evpn;
d62a17ae 176 struct listnode *node, *nnode;
228da428 177
d62a17ae 178 /* it only makes sense for this to be called on a clean exit */
179 assert(status == 0);
228da428 180
03951374
DL
181 frr_early_fini();
182
d62a17ae 183 bgp_close();
1ff9a340 184
0e42e319 185 bgp_default = bgp_get_default();
e2f3a930 186 bgp_evpn = bgp_get_evpn();
0e42e319 187
d62a17ae 188 /* reverse bgp_master_init */
0e42e319 189 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
e2f3a930 190 if (bgp_default == bgp || bgp_evpn == bgp)
0e42e319 191 continue;
d62a17ae 192 bgp_delete(bgp);
0e42e319 193 }
e2f3a930
T
194 if (bgp_evpn && bgp_evpn != bgp_default)
195 bgp_delete(bgp_evpn);
1f4b2cce
DS
196 if (bgp_default)
197 bgp_delete(bgp_default);
46abd3e3 198
45a859f1 199 bgp_evpn_mh_finish();
c589d847 200 bgp_l3nhg_finish();
45a859f1 201
d62a17ae 202 /* reverse bgp_dump_init */
203 bgp_dump_finish();
ed0e57e3
DA
204
205 /* BGP community aliases */
206 bgp_community_alias_finish();
228da428 207
d62a17ae 208 /* reverse bgp_route_init */
209 bgp_route_finish();
228da428 210
d62a17ae 211 /* cleanup route maps */
212 bgp_route_map_terminate();
228da428 213
d62a17ae 214 /* reverse bgp_attr_init */
215 bgp_attr_finish();
7b8def58 216
2d4ee774
QY
217 /* stop pthreads */
218 bgp_pthreads_finish();
219
d62a17ae 220 /* reverse access_list_init */
221 access_list_add_hook(NULL);
222 access_list_delete_hook(NULL);
223 access_list_reset();
228da428 224
d62a17ae 225 /* reverse bgp_filter_init */
226 as_list_add_hook(NULL);
227 as_list_delete_hook(NULL);
228 bgp_filter_reset();
228da428 229
d62a17ae 230 /* reverse prefix_list_init */
231 prefix_list_add_hook(NULL);
232 prefix_list_delete_hook(NULL);
233 prefix_list_reset();
228da428 234
d62a17ae 235 /* reverse community_list_init */
236 community_list_terminate(bgp_clist);
228da428 237
d62a17ae 238 bgp_vrf_terminate();
49e5a4a0 239#ifdef ENABLE_BGP_VNC
d62a17ae 240 vnc_zebra_destroy();
65efcfce 241#endif
d62a17ae 242 bgp_zebra_destroy();
228da428 243
3d57c994 244 bf_free(bm->rd_idspace);
6a154c88 245 list_delete(&bm->bgp);
85e9cd9a 246 list_delete(&bm->addresses);
0768f289
PG
247
248 bgp_lp_finish();
249
d62a17ae 250 memset(bm, 0, sizeof(*bm));
46857efe 251
03951374 252 frr_fini();
d62a17ae 253 exit(status);
228da428 254}
6b0655a2 255
d62a17ae 256static int bgp_vrf_new(struct vrf *vrf)
2fcc254e 257{
d62a17ae 258 if (BGP_DEBUG(zebra, ZEBRA))
a8bf7d9c 259 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
2fcc254e 260
d62a17ae 261 return 0;
2fcc254e
DS
262}
263
d62a17ae 264static int bgp_vrf_delete(struct vrf *vrf)
2fcc254e 265{
d62a17ae 266 if (BGP_DEBUG(zebra, ZEBRA))
a8bf7d9c 267 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
2fcc254e 268
d62a17ae 269 return 0;
2fcc254e
DS
270}
271
d62a17ae 272static int bgp_vrf_enable(struct vrf *vrf)
2fcc254e 273{
d62a17ae 274 struct bgp *bgp;
275 vrf_id_t old_vrf_id;
276
277 if (BGP_DEBUG(zebra, ZEBRA))
a8bf7d9c 278 zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
d62a17ae 279
280 bgp = bgp_lookup_by_name(vrf->name);
d5f31d5f 281 if (bgp && bgp->vrf_id != vrf->vrf_id) {
d62a17ae 282 old_vrf_id = bgp->vrf_id;
283 /* We have instance configured, link to VRF and make it "up". */
284 bgp_vrf_link(bgp, vrf);
285
e5619c28 286 bgp_handle_socket(bgp, vrf, old_vrf_id, true);
d62a17ae 287 bgp_instance_up(bgp);
ddb5b488
PZ
288 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP);
289 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP6);
b72c9e14
HS
290 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP);
291 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP6);
e504cf3b
DS
292 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP,
293 bgp_get_default(), bgp);
294 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP,
295 bgp_get_default(), bgp);
296 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6,
297 bgp_get_default(), bgp);
298 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP6,
299 bgp_get_default(), bgp);
d62a17ae 300 }
301
302 return 0;
2fcc254e
DS
303}
304
d62a17ae 305static int bgp_vrf_disable(struct vrf *vrf)
2fcc254e 306{
d62a17ae 307 struct bgp *bgp;
d62a17ae 308
309 if (vrf->vrf_id == VRF_DEFAULT)
310 return 0;
311
312 if (BGP_DEBUG(zebra, ZEBRA))
313 zlog_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id);
314
315 bgp = bgp_lookup_by_name(vrf->name);
316 if (bgp) {
ddb5b488
PZ
317
318 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP);
319 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP6);
e504cf3b
DS
320 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP,
321 bgp_get_default(), bgp);
322 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP,
323 bgp_get_default(), bgp);
324 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6,
325 bgp_get_default(), bgp);
326 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP6,
327 bgp_get_default(), bgp);
ddb5b488 328
e5619c28 329 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
d62a17ae 330 /* We have instance configured, unlink from VRF and make it
331 * "down". */
d62a17ae 332 bgp_instance_down(bgp);
6cc0114b 333 bgp_vrf_unlink(bgp, vrf);
d62a17ae 334 }
335
336 /* Note: This is a callback, the VRF will be deleted by the caller. */
337 return 0;
2fcc254e
DS
338}
339
d62a17ae 340static void bgp_vrf_init(void)
2fcc254e 341{
ac2cb9bf 342 vrf_init(bgp_vrf_new, bgp_vrf_enable, bgp_vrf_disable, bgp_vrf_delete);
2fcc254e
DS
343}
344
d62a17ae 345static void bgp_vrf_terminate(void)
021530c1 346{
d62a17ae 347 vrf_terminate();
021530c1 348}
349
0d8c7a26 350static const struct frr_yang_module_info *const bgpd_yang_modules[] = {
c2aab693 351 &frr_filter_info,
91835f1f
RZ
352 &frr_interface_info,
353 &frr_route_map_info,
6fd8972a 354 &frr_vrf_info,
48cb7ea9 355 &frr_bgp_route_map_info,
8fcdd0d6
RW
356};
357
d62a17ae 358FRR_DAEMON_INFO(bgpd, BGP, .vty_port = BGP_VTY_PORT,
4f04a76b 359
d62a17ae 360 .proghelp = "Implementation of the BGP routing protocol.",
4f04a76b 361
d62a17ae 362 .signals = bgp_signals, .n_signals = array_size(bgp_signals),
4f04a76b 363
8fcdd0d6 364 .privs = &bgpd_privs, .yang_modules = bgpd_yang_modules,
80413c20
DL
365 .n_yang_modules = array_size(bgpd_yang_modules),
366);
4f04a76b 367
f28963f7 368#define DEPRECATED_OPTIONS ""
c8dde10f 369
718e3744 370/* Main routine of bgpd. Treatment of argument and start bgp finite
371 state machine is handled at here. */
d62a17ae 372int main(int argc, char **argv)
718e3744 373{
d62a17ae 374 int opt;
375 int tmp_port;
376
377 int bgp_port = BGP_PORT_DEFAULT;
85e9cd9a 378 struct list *addresses = list_new();
d62a17ae 379 int no_fib_flag = 0;
0b014ea6 380 int no_zebra_flag = 0;
d62a17ae 381 int skip_runas = 0;
f533be73 382 int instance = 0;
c2d020ad 383 int buffer_size = BGP_SOCKET_SNDBUF_SIZE;
85e9cd9a
AMR
384 char *address;
385 struct listnode *node;
386
387 addresses->cmp = (int (*)(void *, void *))strcmp;
d62a17ae 388
389 frr_preinit(&bgpd_di, argc, argv);
390 frr_opt_add(
c2d020ad 391 "p:l:SnZe:I:s:" DEPRECATED_OPTIONS, longopts,
580f8636 392 " -p, --bgp_port Set BGP listen port number (0 means do not listen).\n"
d62a17ae 393 " -l, --listenon Listen on specified address (implies -n)\n"
d62a17ae 394 " -n, --no_kernel Do not install route to kernel.\n"
0b014ea6 395 " -Z, --no_zebra Do not communicate with Zebra.\n"
d62a17ae 396 " -S, --skip_runas Skip capabilities checks, and changing user and group IDs.\n"
f533be73 397 " -e, --ecmp Specify ECMP to use.\n"
c2d020ad
DS
398 " -I, --int_num Set instance number (label-manager)\n"
399 " -s, --socket_size Set BGP peer socket send buffer size\n");
d62a17ae 400
401 /* Command line argument treatment. */
402 while (1) {
403 opt = frr_getopt(argc, argv, 0);
404
c8dde10f
QY
405 if (opt && opt < 128 && strchr(DEPRECATED_OPTIONS, opt)) {
406 fprintf(stderr,
407 "The -%c option no longer exists.\nPlease refer to the manual.\n",
408 opt);
409 continue;
410 }
411
d62a17ae 412 if (opt == EOF)
413 break;
414
415 switch (opt) {
416 case 0:
417 break;
418 case 'p':
419 tmp_port = atoi(optarg);
580f8636 420 if (tmp_port < 0 || tmp_port > 0xffff)
d62a17ae 421 bgp_port = BGP_PORT_DEFAULT;
422 else
423 bgp_port = tmp_port;
424 break;
1e03d6bc
QY
425 case 'e': {
426 unsigned long int parsed_multipath =
427 strtoul(optarg, NULL, 10);
428 if (parsed_multipath == 0
429 || parsed_multipath > MULTIPATH_NUM
430 || parsed_multipath > UINT_MAX) {
af4c2728 431 flog_err(
e50f7cfd 432 EC_BGP_MULTIPATH,
1e03d6bc 433 "Multipath Number specified must be less than %u and greater than 0",
d62a17ae 434 MULTIPATH_NUM);
435 return 1;
436 }
1e03d6bc 437 multipath_num = parsed_multipath;
d62a17ae 438 break;
1e03d6bc 439 }
d62a17ae 440 case 'l':
85e9cd9a 441 listnode_add_sort_nodup(addresses, optarg);
6595c229 442 break;
d62a17ae 443 case 'n':
444 no_fib_flag = 1;
445 break;
0b014ea6
PG
446 case 'Z':
447 no_zebra_flag = 1;
448 break;
d62a17ae 449 case 'S':
450 skip_runas = 1;
451 break;
f533be73 452 case 'I':
453 instance = atoi(optarg);
454 if (instance > (unsigned short)-1)
455 zlog_err("Instance %i out of range (0..%u)",
456 instance, (unsigned short)-1);
457 break;
c2d020ad
DS
458 case 's':
459 buffer_size = atoi(optarg);
460 break;
d62a17ae 461 default:
462 frr_help_exit(1);
d62a17ae 463 }
718e3744 464 }
d62a17ae 465 if (skip_runas)
466 memset(&bgpd_privs, 0, sizeof(bgpd_privs));
718e3744 467
d62a17ae 468 /* BGP master init. */
85e9cd9a 469 bgp_master_init(frr_init(), buffer_size, addresses);
d62a17ae 470 bm->port = bgp_port;
580f8636 471 if (bgp_port == 0)
472 bgp_option_set(BGP_OPT_NO_LISTEN);
0b014ea6 473 if (no_fib_flag || no_zebra_flag)
d62a17ae 474 bgp_option_set(BGP_OPT_NO_FIB);
0b014ea6
PG
475 if (no_zebra_flag)
476 bgp_option_set(BGP_OPT_NO_ZEBRA);
def31c13 477 bgp_error_init();
d62a17ae 478 /* Initializations. */
479 bgp_vrf_init();
718e3744 480
fa22080d 481#ifdef HAVE_SCRIPTING
b4becb06 482 bgp_script_init();
fa22080d 483#endif
b4becb06 484
d62a17ae 485 /* BGP related initialization. */
f533be73 486 bgp_init((unsigned short)instance);
718e3744 487
85e9cd9a
AMR
488 if (list_isempty(bm->addresses)) {
489 snprintf(bgpd_di.startinfo, sizeof(bgpd_di.startinfo),
490 ", bgp@<all>:%d", bm->port);
491 } else {
492 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
493 snprintf(bgpd_di.startinfo + strlen(bgpd_di.startinfo),
494 sizeof(bgpd_di.startinfo)
495 - strlen(bgpd_di.startinfo),
496 ", bgp@%s:%d", address, bm->port);
497 }
718e3744 498
4cd690ae
PG
499 bgp_if_init();
500
d62a17ae 501 frr_config_fork();
2d4ee774 502 /* must be called after fork() */
034e185d 503 bgp_gr_apply_running_config();
419dfe6a 504 bgp_pthreads_run();
d62a17ae 505 frr_run(bm->master);
718e3744 506
d62a17ae 507 /* Not reached. */
95f7965d 508 return 0;
718e3744 509}