]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_main.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bgpd / bgp_main.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Main routine of bgpd.
3 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include <pthread.h>
9 #include "vector.h"
10 #include "command.h"
11 #include "getopt.h"
12 #include "thread.h"
13 #include <lib/version.h>
14 #include "memory.h"
15 #include "prefix.h"
16 #include "log.h"
17 #include "privs.h"
18 #include "sigevent.h"
19 #include "zclient.h"
20 #include "routemap.h"
21 #include "filter.h"
22 #include "plist.h"
23 #include "stream.h"
24 #include "queue.h"
25 #include "vrf.h"
26 #include "bfd.h"
27 #include "libfrr.h"
28 #include "ns.h"
29
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_attr.h"
32 #include "bgpd/bgp_route.h"
33 #include "bgpd/bgp_mplsvpn.h"
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"
41 #include "bgpd/bgp_errors.h"
42 #include "bgpd/bgp_filter.h"
43 #include "bgpd/bgp_zebra.h"
44 #include "bgpd/bgp_packet.h"
45 #include "bgpd/bgp_keepalives.h"
46 #include "bgpd/bgp_network.h"
47 #include "bgpd/bgp_errors.h"
48 #include "bgpd/bgp_script.h"
49 #include "bgpd/bgp_evpn_mh.h"
50 #include "bgpd/bgp_nht.h"
51 #include "bgpd/bgp_routemap_nb.h"
52 #include "bgpd/bgp_community_alias.h"
53
54 #ifdef ENABLE_BGP_VNC
55 #include "bgpd/rfapi/rfapi_backend.h"
56 #endif
57
58 /* bgpd options, we use GNU getopt library. */
59 static const struct option longopts[] = {
60 {"bgp_port", required_argument, NULL, 'p'},
61 {"listenon", required_argument, NULL, 'l'},
62 {"no_kernel", no_argument, NULL, 'n'},
63 {"skip_runas", no_argument, NULL, 'S'},
64 {"ecmp", required_argument, NULL, 'e'},
65 {"int_num", required_argument, NULL, 'I'},
66 {"no_zebra", no_argument, NULL, 'Z'},
67 {"socket_size", required_argument, NULL, 's'},
68 {0}};
69
70 /* signal definitions */
71 void sighup(void);
72 void sigint(void);
73 void sigusr1(void);
74
75 static void bgp_exit(int);
76 static void bgp_vrf_terminate(void);
77
78 static struct frr_signal_t bgp_signals[] = {
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 },
95 };
96
97 /* privileges */
98 static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_NET_RAW,
99 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN};
100
101 struct zebra_privs_t bgpd_privs = {
102 #if defined(FRR_USER) && defined(FRR_GROUP)
103 .user = FRR_USER,
104 .group = FRR_GROUP,
105 #endif
106 #ifdef VTY_GROUP
107 .vty_group = VTY_GROUP,
108 #endif
109 .caps_p = _caps_p,
110 .cap_num_p = array_size(_caps_p),
111 .cap_num_i = 0,
112 };
113
114 static struct frr_daemon_info bgpd_di;
115
116 /* SIGHUP handler. */
117 void sighup(void)
118 {
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
126 * that is not setup properly again in bgp_reset.
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 */
131 /* Terminate all thread. */
132 /*
133 * bgp_terminate();
134 * bgp_reset();
135 * zlog_info("bgpd restarting!");
136
137 * Reload config file.
138 * vty_read_config(NULL, bgpd_di.config_file, config_default);
139 */
140 /* Try to return to normal operation. */
141 }
142
143 /* SIGINT handler. */
144 __attribute__((__noreturn__)) void sigint(void)
145 {
146 zlog_notice("Terminating on signal");
147 assert(bm->terminating == false);
148 bm->terminating = true; /* global flag that shutting down */
149
150 /* Disable BFD events to avoid wasting processing. */
151 bfd_protocol_integration_set_shutdown(true);
152
153 bgp_terminate();
154
155 bgp_exit(0);
156
157 exit(0);
158 }
159
160 /* SIGUSR1 handler. */
161 void sigusr1(void)
162 {
163 zlog_rotate();
164 }
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 */
173 static __attribute__((__noreturn__)) void bgp_exit(int status)
174 {
175 struct bgp *bgp, *bgp_default, *bgp_evpn;
176 struct listnode *node, *nnode;
177
178 /* it only makes sense for this to be called on a clean exit */
179 assert(status == 0);
180
181 frr_early_fini();
182
183 bgp_close();
184
185 bgp_default = bgp_get_default();
186 bgp_evpn = bgp_get_evpn();
187
188 /* reverse bgp_master_init */
189 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
190 if (bgp_default == bgp || bgp_evpn == bgp)
191 continue;
192 bgp_delete(bgp);
193 }
194 if (bgp_evpn && bgp_evpn != bgp_default)
195 bgp_delete(bgp_evpn);
196 if (bgp_default)
197 bgp_delete(bgp_default);
198
199 bgp_evpn_mh_finish();
200 bgp_l3nhg_finish();
201
202 /* reverse bgp_dump_init */
203 bgp_dump_finish();
204
205 /* BGP community aliases */
206 bgp_community_alias_finish();
207
208 /* reverse bgp_route_init */
209 bgp_route_finish();
210
211 /* cleanup route maps */
212 bgp_route_map_terminate();
213
214 /* reverse bgp_attr_init */
215 bgp_attr_finish();
216
217 /* stop pthreads */
218 bgp_pthreads_finish();
219
220 /* reverse access_list_init */
221 access_list_add_hook(NULL);
222 access_list_delete_hook(NULL);
223 access_list_reset();
224
225 /* reverse bgp_filter_init */
226 as_list_add_hook(NULL);
227 as_list_delete_hook(NULL);
228 bgp_filter_reset();
229
230 /* reverse prefix_list_init */
231 prefix_list_add_hook(NULL);
232 prefix_list_delete_hook(NULL);
233 prefix_list_reset();
234
235 /* reverse community_list_init */
236 community_list_terminate(bgp_clist);
237
238 bgp_vrf_terminate();
239 #ifdef ENABLE_BGP_VNC
240 vnc_zebra_destroy();
241 #endif
242 bgp_zebra_destroy();
243
244 bf_free(bm->rd_idspace);
245 list_delete(&bm->bgp);
246 list_delete(&bm->addresses);
247
248 bgp_lp_finish();
249
250 memset(bm, 0, sizeof(*bm));
251
252 frr_fini();
253 exit(status);
254 }
255
256 static int bgp_vrf_new(struct vrf *vrf)
257 {
258 if (BGP_DEBUG(zebra, ZEBRA))
259 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
260
261 return 0;
262 }
263
264 static int bgp_vrf_delete(struct vrf *vrf)
265 {
266 if (BGP_DEBUG(zebra, ZEBRA))
267 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
268
269 return 0;
270 }
271
272 static int bgp_vrf_enable(struct vrf *vrf)
273 {
274 struct bgp *bgp;
275 vrf_id_t old_vrf_id;
276
277 if (BGP_DEBUG(zebra, ZEBRA))
278 zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
279
280 bgp = bgp_lookup_by_name(vrf->name);
281 if (bgp && bgp->vrf_id != vrf->vrf_id) {
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
286 bgp_handle_socket(bgp, vrf, old_vrf_id, true);
287 bgp_instance_up(bgp);
288 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP);
289 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP6);
290 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP);
291 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP6);
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);
300 }
301
302 return 0;
303 }
304
305 static int bgp_vrf_disable(struct vrf *vrf)
306 {
307 struct bgp *bgp;
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) {
317
318 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP);
319 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP6);
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);
328
329 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
330 /* We have instance configured, unlink from VRF and make it
331 * "down". */
332 bgp_instance_down(bgp);
333 bgp_vrf_unlink(bgp, vrf);
334 }
335
336 /* Note: This is a callback, the VRF will be deleted by the caller. */
337 return 0;
338 }
339
340 static void bgp_vrf_init(void)
341 {
342 vrf_init(bgp_vrf_new, bgp_vrf_enable, bgp_vrf_disable, bgp_vrf_delete);
343 }
344
345 static void bgp_vrf_terminate(void)
346 {
347 vrf_terminate();
348 }
349
350 static const struct frr_yang_module_info *const bgpd_yang_modules[] = {
351 &frr_filter_info,
352 &frr_interface_info,
353 &frr_route_map_info,
354 &frr_vrf_info,
355 &frr_bgp_route_map_info,
356 };
357
358 FRR_DAEMON_INFO(bgpd, BGP, .vty_port = BGP_VTY_PORT,
359
360 .proghelp = "Implementation of the BGP routing protocol.",
361
362 .signals = bgp_signals, .n_signals = array_size(bgp_signals),
363
364 .privs = &bgpd_privs, .yang_modules = bgpd_yang_modules,
365 .n_yang_modules = array_size(bgpd_yang_modules),
366 );
367
368 #define DEPRECATED_OPTIONS ""
369
370 /* Main routine of bgpd. Treatment of argument and start bgp finite
371 state machine is handled at here. */
372 int main(int argc, char **argv)
373 {
374 int opt;
375 int tmp_port;
376
377 int bgp_port = BGP_PORT_DEFAULT;
378 struct list *addresses = list_new();
379 int no_fib_flag = 0;
380 int no_zebra_flag = 0;
381 int skip_runas = 0;
382 int instance = 0;
383 int buffer_size = BGP_SOCKET_SNDBUF_SIZE;
384 char *address;
385 struct listnode *node;
386
387 addresses->cmp = (int (*)(void *, void *))strcmp;
388
389 frr_preinit(&bgpd_di, argc, argv);
390 frr_opt_add(
391 "p:l:SnZe:I:s:" DEPRECATED_OPTIONS, longopts,
392 " -p, --bgp_port Set BGP listen port number (0 means do not listen).\n"
393 " -l, --listenon Listen on specified address (implies -n)\n"
394 " -n, --no_kernel Do not install route to kernel.\n"
395 " -Z, --no_zebra Do not communicate with Zebra.\n"
396 " -S, --skip_runas Skip capabilities checks, and changing user and group IDs.\n"
397 " -e, --ecmp Specify ECMP to use.\n"
398 " -I, --int_num Set instance number (label-manager)\n"
399 " -s, --socket_size Set BGP peer socket send buffer size\n");
400
401 /* Command line argument treatment. */
402 while (1) {
403 opt = frr_getopt(argc, argv, 0);
404
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
412 if (opt == EOF)
413 break;
414
415 switch (opt) {
416 case 0:
417 break;
418 case 'p':
419 tmp_port = atoi(optarg);
420 if (tmp_port < 0 || tmp_port > 0xffff)
421 bgp_port = BGP_PORT_DEFAULT;
422 else
423 bgp_port = tmp_port;
424 break;
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) {
431 flog_err(
432 EC_BGP_MULTIPATH,
433 "Multipath Number specified must be less than %u and greater than 0",
434 MULTIPATH_NUM);
435 return 1;
436 }
437 multipath_num = parsed_multipath;
438 break;
439 }
440 case 'l':
441 listnode_add_sort_nodup(addresses, optarg);
442 break;
443 case 'n':
444 no_fib_flag = 1;
445 break;
446 case 'Z':
447 no_zebra_flag = 1;
448 break;
449 case 'S':
450 skip_runas = 1;
451 break;
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;
458 case 's':
459 buffer_size = atoi(optarg);
460 break;
461 default:
462 frr_help_exit(1);
463 }
464 }
465 if (skip_runas)
466 memset(&bgpd_privs, 0, sizeof(bgpd_privs));
467
468 /* BGP master init. */
469 bgp_master_init(frr_init(), buffer_size, addresses);
470 bm->port = bgp_port;
471 if (bgp_port == 0)
472 bgp_option_set(BGP_OPT_NO_LISTEN);
473 if (no_fib_flag || no_zebra_flag)
474 bgp_option_set(BGP_OPT_NO_FIB);
475 if (no_zebra_flag)
476 bgp_option_set(BGP_OPT_NO_ZEBRA);
477 bgp_error_init();
478 /* Initializations. */
479 bgp_vrf_init();
480
481 #ifdef HAVE_SCRIPTING
482 bgp_script_init();
483 #endif
484
485 /* BGP related initialization. */
486 bgp_init((unsigned short)instance);
487
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 }
498
499 bgp_if_init();
500
501 frr_config_fork();
502 /* must be called after fork() */
503 bgp_gr_apply_running_config();
504 bgp_pthreads_run();
505 frr_run(bm->master);
506
507 /* Not reached. */
508 return 0;
509 }