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