1 /* Main routine of bgpd.
2 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
28 #include <lib/version.h>
45 #include "bgpd/bgpd.h"
46 #include "bgpd/bgp_attr.h"
47 #include "bgpd/bgp_route.h"
48 #include "bgpd/bgp_mplsvpn.h"
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"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_filter.h"
58 #include "bgpd/bgp_zebra.h"
59 #include "bgpd/bgp_packet.h"
60 #include "bgpd/bgp_keepalives.h"
61 #include "bgpd/bgp_network.h"
62 #include "bgpd/bgp_errors.h"
63 #include "bgpd/bgp_script.h"
64 #include "lib/routing_nb.h"
65 #include "bgpd/bgp_nb.h"
66 #include "bgpd/bgp_evpn_mh.h"
67 #include "bgpd/bgp_nht.h"
68 #include "bgpd/bgp_routemap_nb.h"
69 #include "bgpd/bgp_community_alias.h"
72 #include "bgpd/rfapi/rfapi_backend.h"
75 /* bgpd options, we use GNU getopt library. */
76 static const struct option longopts
[] = {
77 {"bgp_port", required_argument
, NULL
, 'p'},
78 {"listenon", required_argument
, NULL
, 'l'},
79 {"no_kernel", no_argument
, NULL
, 'n'},
80 {"skip_runas", no_argument
, NULL
, 'S'},
81 {"ecmp", required_argument
, NULL
, 'e'},
82 {"int_num", required_argument
, NULL
, 'I'},
83 {"no_zebra", no_argument
, NULL
, 'Z'},
84 {"socket_size", required_argument
, NULL
, 's'},
87 /* signal definitions */
92 static void bgp_exit(int);
93 static void bgp_vrf_terminate(void);
95 static struct quagga_signal_t bgp_signals
[] = {
115 static zebra_capabilities_t _caps_p
[] = {ZCAP_BIND
, ZCAP_NET_RAW
,
116 ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
};
118 struct zebra_privs_t bgpd_privs
= {
119 #if defined(FRR_USER) && defined(FRR_GROUP)
124 .vty_group
= VTY_GROUP
,
127 .cap_num_p
= array_size(_caps_p
),
131 static struct frr_daemon_info bgpd_di
;
133 /* SIGHUP handler. */
136 zlog_info("SIGHUP received, ignoring");
141 * This is turned off for the moment. There is all
142 * sorts of config turned off by bgp_terminate
143 * that is not setup properly again in bgp_reset.
144 * I see no easy way to do this nor do I see that
145 * this is a desirable way to reload config
146 * given the yang work.
148 /* Terminate all thread. */
152 * zlog_info("bgpd restarting!");
154 * Reload config file.
155 * vty_read_config(NULL, bgpd_di.config_file, config_default);
157 /* Try to return to normal operation. */
160 /* SIGINT handler. */
161 __attribute__((__noreturn__
)) void sigint(void)
163 zlog_notice("Terminating on signal");
164 assert(bm
->terminating
== false);
165 bm
->terminating
= true; /* global flag that shutting down */
167 /* Disable BFD events to avoid wasting processing. */
168 bfd_protocol_integration_set_shutdown(true);
177 /* SIGUSR1 handler. */
184 Try to free up allocations we know about so that diagnostic tools such as
185 valgrind are able to better illuminate leaks.
187 Zebra route removal and protocol teardown are not meant to be done here.
188 For example, "retain_mode" may be set.
190 static __attribute__((__noreturn__
)) void bgp_exit(int status
)
192 struct bgp
*bgp
, *bgp_default
, *bgp_evpn
;
193 struct listnode
*node
, *nnode
;
195 /* it only makes sense for this to be called on a clean exit */
202 bgp_default
= bgp_get_default();
203 bgp_evpn
= bgp_get_evpn();
205 /* reverse bgp_master_init */
206 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
207 if (bgp_default
== bgp
|| bgp_evpn
== bgp
)
211 if (bgp_evpn
&& bgp_evpn
!= bgp_default
)
212 bgp_delete(bgp_evpn
);
214 bgp_delete(bgp_default
);
216 bgp_evpn_mh_finish();
219 /* reverse bgp_dump_init */
222 /* BGP community aliases */
223 bgp_community_alias_finish();
225 /* reverse bgp_route_init */
228 /* cleanup route maps */
229 bgp_route_map_terminate();
231 /* reverse bgp_attr_init */
235 bgp_pthreads_finish();
237 /* reverse access_list_init */
238 access_list_add_hook(NULL
);
239 access_list_delete_hook(NULL
);
242 /* reverse bgp_filter_init */
243 as_list_add_hook(NULL
);
244 as_list_delete_hook(NULL
);
247 /* reverse prefix_list_init */
248 prefix_list_add_hook(NULL
);
249 prefix_list_delete_hook(NULL
);
252 /* reverse community_list_init */
253 community_list_terminate(bgp_clist
);
256 #ifdef ENABLE_BGP_VNC
261 bf_free(bm
->rd_idspace
);
262 list_delete(&bm
->bgp
);
263 list_delete(&bm
->addresses
);
267 memset(bm
, 0, sizeof(*bm
));
273 static int bgp_vrf_new(struct vrf
*vrf
)
275 if (BGP_DEBUG(zebra
, ZEBRA
))
276 zlog_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
281 static int bgp_vrf_delete(struct vrf
*vrf
)
283 if (BGP_DEBUG(zebra
, ZEBRA
))
284 zlog_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
289 static int bgp_vrf_enable(struct vrf
*vrf
)
294 if (BGP_DEBUG(zebra
, ZEBRA
))
295 zlog_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
297 bgp
= bgp_lookup_by_name(vrf
->name
);
298 if (bgp
&& bgp
->vrf_id
!= vrf
->vrf_id
) {
299 if (bgp
->name
&& strmatch(vrf
->name
, VRF_DEFAULT_NAME
)) {
300 XFREE(MTYPE_BGP
, bgp
->name
);
301 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
302 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
303 bgp
->inst_type
= BGP_INSTANCE_TYPE_DEFAULT
;
304 #ifdef ENABLE_BGP_VNC
306 bgp
->rfapi
= bgp_rfapi_new(bgp
);
308 assert(bgp
->rfapi_cfg
);
310 #endif /* ENABLE_BGP_VNC */
312 old_vrf_id
= bgp
->vrf_id
;
313 /* We have instance configured, link to VRF and make it "up". */
314 bgp_vrf_link(bgp
, vrf
);
316 bgp_handle_socket(bgp
, vrf
, old_vrf_id
, true);
317 bgp_instance_up(bgp
);
318 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP
);
319 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP6
);
320 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP
);
321 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP6
);
322 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
,
323 bgp_get_default(), bgp
);
324 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN
, AFI_IP
,
325 bgp_get_default(), bgp
);
326 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
,
327 bgp_get_default(), bgp
);
328 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN
, AFI_IP6
,
329 bgp_get_default(), bgp
);
335 static int bgp_vrf_disable(struct vrf
*vrf
)
339 if (vrf
->vrf_id
== VRF_DEFAULT
)
342 if (BGP_DEBUG(zebra
, ZEBRA
))
343 zlog_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
345 bgp
= bgp_lookup_by_name(vrf
->name
);
348 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
349 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
350 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
,
351 bgp_get_default(), bgp
);
352 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN
, AFI_IP
,
353 bgp_get_default(), bgp
);
354 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
,
355 bgp_get_default(), bgp
);
356 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN
, AFI_IP6
,
357 bgp_get_default(), bgp
);
359 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
360 /* We have instance configured, unlink from VRF and make it
362 bgp_instance_down(bgp
);
363 bgp_vrf_unlink(bgp
, vrf
);
366 /* Note: This is a callback, the VRF will be deleted by the caller. */
370 static void bgp_vrf_init(void)
372 vrf_init(bgp_vrf_new
, bgp_vrf_enable
, bgp_vrf_disable
,
373 bgp_vrf_delete
, bgp_vrf_enable
);
376 static void bgp_vrf_terminate(void)
381 static const struct frr_yang_module_info
*const bgpd_yang_modules
[] = {
388 &frr_bgp_route_map_info
,
391 FRR_DAEMON_INFO(bgpd
, BGP
, .vty_port
= BGP_VTY_PORT
,
393 .proghelp
= "Implementation of the BGP routing protocol.",
395 .signals
= bgp_signals
, .n_signals
= array_size(bgp_signals
),
397 .privs
= &bgpd_privs
, .yang_modules
= bgpd_yang_modules
,
398 .n_yang_modules
= array_size(bgpd_yang_modules
),
401 #define DEPRECATED_OPTIONS ""
403 /* Main routine of bgpd. Treatment of argument and start bgp finite
404 state machine is handled at here. */
405 int main(int argc
, char **argv
)
410 int bgp_port
= BGP_PORT_DEFAULT
;
411 struct list
*addresses
= list_new();
413 int no_zebra_flag
= 0;
416 int buffer_size
= BGP_SOCKET_SNDBUF_SIZE
;
418 struct listnode
*node
;
420 addresses
->cmp
= (int (*)(void *, void *))strcmp
;
422 frr_preinit(&bgpd_di
, argc
, argv
);
424 "p:l:SnZe:I:s:" DEPRECATED_OPTIONS
, longopts
,
425 " -p, --bgp_port Set BGP listen port number (0 means do not listen).\n"
426 " -l, --listenon Listen on specified address (implies -n)\n"
427 " -n, --no_kernel Do not install route to kernel.\n"
428 " -Z, --no_zebra Do not communicate with Zebra.\n"
429 " -S, --skip_runas Skip capabilities checks, and changing user and group IDs.\n"
430 " -e, --ecmp Specify ECMP to use.\n"
431 " -I, --int_num Set instance number (label-manager)\n"
432 " -s, --socket_size Set BGP peer socket send buffer size\n");
434 /* Command line argument treatment. */
436 opt
= frr_getopt(argc
, argv
, 0);
438 if (opt
&& opt
< 128 && strchr(DEPRECATED_OPTIONS
, opt
)) {
440 "The -%c option no longer exists.\nPlease refer to the manual.\n",
452 tmp_port
= atoi(optarg
);
453 if (tmp_port
< 0 || tmp_port
> 0xffff)
454 bgp_port
= BGP_PORT_DEFAULT
;
459 unsigned long int parsed_multipath
=
460 strtoul(optarg
, NULL
, 10);
461 if (parsed_multipath
== 0
462 || parsed_multipath
> MULTIPATH_NUM
463 || parsed_multipath
> UINT_MAX
) {
466 "Multipath Number specified must be less than %u and greater than 0",
470 multipath_num
= parsed_multipath
;
474 listnode_add_sort_nodup(addresses
, optarg
);
475 /* listenon implies -n */
487 instance
= atoi(optarg
);
488 if (instance
> (unsigned short)-1)
489 zlog_err("Instance %i out of range (0..%u)",
490 instance
, (unsigned short)-1);
493 buffer_size
= atoi(optarg
);
501 memset(&bgpd_privs
, 0, sizeof(bgpd_privs
));
503 /* BGP master init. */
504 bgp_master_init(frr_init(), buffer_size
, addresses
);
507 bgp_option_set(BGP_OPT_NO_LISTEN
);
508 if (no_fib_flag
|| no_zebra_flag
)
509 bgp_option_set(BGP_OPT_NO_FIB
);
511 bgp_option_set(BGP_OPT_NO_ZEBRA
);
513 /* Initializations. */
516 #ifdef HAVE_SCRIPTING
520 hook_register(routing_conf_event
,
521 routing_control_plane_protocols_name_validate
);
524 /* BGP related initialization. */
525 bgp_init((unsigned short)instance
);
527 if (list_isempty(bm
->addresses
)) {
528 snprintf(bgpd_di
.startinfo
, sizeof(bgpd_di
.startinfo
),
529 ", bgp@<all>:%d", bm
->port
);
531 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
532 snprintf(bgpd_di
.startinfo
+ strlen(bgpd_di
.startinfo
),
533 sizeof(bgpd_di
.startinfo
)
534 - strlen(bgpd_di
.startinfo
),
535 ", bgp@%s:%d", address
, bm
->port
);
539 /* must be called after fork() */
540 bgp_gr_apply_running_config();