1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Main routine of bgpd.
3 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
13 #include <lib/version.h>
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"
55 #include "bgpd/rfapi/rfapi_backend.h"
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'},
70 /* signal definitions */
75 static void bgp_exit(int);
76 static void bgp_vrf_terminate(void);
78 static struct frr_signal_t bgp_signals
[] = {
98 static zebra_capabilities_t _caps_p
[] = {ZCAP_BIND
, ZCAP_NET_RAW
,
99 ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
};
101 struct zebra_privs_t bgpd_privs
= {
102 #if defined(FRR_USER) && defined(FRR_GROUP)
107 .vty_group
= VTY_GROUP
,
110 .cap_num_p
= array_size(_caps_p
),
114 static struct frr_daemon_info bgpd_di
;
116 /* SIGHUP handler. */
119 zlog_info("SIGHUP received, ignoring");
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.
131 /* Terminate all thread. */
135 * zlog_info("bgpd restarting!");
137 * Reload config file.
138 * vty_read_config(NULL, bgpd_di.config_file, config_default);
140 /* Try to return to normal operation. */
143 /* SIGINT handler. */
144 __attribute__((__noreturn__
)) void sigint(void)
146 zlog_notice("Terminating on signal");
147 assert(bm
->terminating
== false);
148 bm
->terminating
= true; /* global flag that shutting down */
150 /* Disable BFD events to avoid wasting processing. */
151 bfd_protocol_integration_set_shutdown(true);
160 /* SIGUSR1 handler. */
167 Try to free up allocations we know about so that diagnostic tools such as
168 valgrind are able to better illuminate leaks.
170 Zebra route removal and protocol teardown are not meant to be done here.
171 For example, "retain_mode" may be set.
173 static __attribute__((__noreturn__
)) void bgp_exit(int status
)
175 struct bgp
*bgp
, *bgp_default
, *bgp_evpn
;
176 struct listnode
*node
, *nnode
;
178 /* it only makes sense for this to be called on a clean exit */
185 bgp_default
= bgp_get_default();
186 bgp_evpn
= bgp_get_evpn();
188 /* reverse bgp_master_init */
189 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
190 if (bgp_default
== bgp
|| bgp_evpn
== bgp
)
194 if (bgp_evpn
&& bgp_evpn
!= bgp_default
)
195 bgp_delete(bgp_evpn
);
197 bgp_delete(bgp_default
);
199 bgp_evpn_mh_finish();
202 /* reverse bgp_dump_init */
205 /* BGP community aliases */
206 bgp_community_alias_finish();
208 /* reverse bgp_route_init */
211 /* cleanup route maps */
212 bgp_route_map_terminate();
214 /* reverse bgp_attr_init */
218 bgp_pthreads_finish();
220 /* reverse access_list_init */
221 access_list_add_hook(NULL
);
222 access_list_delete_hook(NULL
);
225 /* reverse bgp_filter_init */
226 as_list_add_hook(NULL
);
227 as_list_delete_hook(NULL
);
230 /* reverse prefix_list_init */
231 prefix_list_add_hook(NULL
);
232 prefix_list_delete_hook(NULL
);
235 /* reverse community_list_init */
236 community_list_terminate(bgp_clist
);
239 #ifdef ENABLE_BGP_VNC
244 bf_free(bm
->rd_idspace
);
245 list_delete(&bm
->bgp
);
246 list_delete(&bm
->addresses
);
250 memset(bm
, 0, sizeof(*bm
));
256 static int bgp_vrf_new(struct vrf
*vrf
)
258 if (BGP_DEBUG(zebra
, ZEBRA
))
259 zlog_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
264 static int bgp_vrf_delete(struct vrf
*vrf
)
266 if (BGP_DEBUG(zebra
, ZEBRA
))
267 zlog_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
272 static int bgp_vrf_enable(struct vrf
*vrf
)
277 if (BGP_DEBUG(zebra
, ZEBRA
))
278 zlog_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
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
);
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
);
305 static int bgp_vrf_disable(struct vrf
*vrf
)
309 if (vrf
->vrf_id
== VRF_DEFAULT
)
312 if (BGP_DEBUG(zebra
, ZEBRA
))
313 zlog_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
315 bgp
= bgp_lookup_by_name(vrf
->name
);
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
);
329 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
330 /* We have instance configured, unlink from VRF and make it
332 bgp_instance_down(bgp
);
333 bgp_vrf_unlink(bgp
, vrf
);
336 /* Note: This is a callback, the VRF will be deleted by the caller. */
340 static void bgp_vrf_init(void)
342 vrf_init(bgp_vrf_new
, bgp_vrf_enable
, bgp_vrf_disable
, bgp_vrf_delete
);
345 static void bgp_vrf_terminate(void)
350 static const struct frr_yang_module_info
*const bgpd_yang_modules
[] = {
355 &frr_bgp_route_map_info
,
358 FRR_DAEMON_INFO(bgpd
, BGP
, .vty_port
= BGP_VTY_PORT
,
360 .proghelp
= "Implementation of the BGP routing protocol.",
362 .signals
= bgp_signals
, .n_signals
= array_size(bgp_signals
),
364 .privs
= &bgpd_privs
, .yang_modules
= bgpd_yang_modules
,
365 .n_yang_modules
= array_size(bgpd_yang_modules
),
368 #define DEPRECATED_OPTIONS ""
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
)
377 int bgp_port
= BGP_PORT_DEFAULT
;
378 struct list
*addresses
= list_new();
380 int no_zebra_flag
= 0;
383 int buffer_size
= BGP_SOCKET_SNDBUF_SIZE
;
385 struct listnode
*node
;
387 addresses
->cmp
= (int (*)(void *, void *))strcmp
;
389 frr_preinit(&bgpd_di
, argc
, argv
);
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");
401 /* Command line argument treatment. */
403 opt
= frr_getopt(argc
, argv
, 0);
405 if (opt
&& opt
< 128 && strchr(DEPRECATED_OPTIONS
, opt
)) {
407 "The -%c option no longer exists.\nPlease refer to the manual.\n",
419 tmp_port
= atoi(optarg
);
420 if (tmp_port
< 0 || tmp_port
> 0xffff)
421 bgp_port
= BGP_PORT_DEFAULT
;
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
) {
433 "Multipath Number specified must be less than %u and greater than 0",
437 multipath_num
= parsed_multipath
;
441 listnode_add_sort_nodup(addresses
, optarg
);
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);
459 buffer_size
= atoi(optarg
);
466 memset(&bgpd_privs
, 0, sizeof(bgpd_privs
));
468 /* BGP master init. */
469 bgp_master_init(frr_init(), buffer_size
, addresses
);
472 bgp_option_set(BGP_OPT_NO_LISTEN
);
473 if (no_fib_flag
|| no_zebra_flag
)
474 bgp_option_set(BGP_OPT_NO_FIB
);
476 bgp_option_set(BGP_OPT_NO_ZEBRA
);
478 /* Initializations. */
481 #ifdef HAVE_SCRIPTING
485 /* BGP related initialization. */
486 bgp_init((unsigned short)instance
);
488 if (list_isempty(bm
->addresses
)) {
489 snprintf(bgpd_di
.startinfo
, sizeof(bgpd_di
.startinfo
),
490 ", bgp@<all>:%d", bm
->port
);
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
);
502 /* must be called after fork() */
503 bgp_gr_apply_running_config();