]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgpd.h
Merge pull request #13439 from anlancs/fix/pimd-use-macro-pimreg
[mirror_frr.git] / bgpd / bgpd.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP message definition header.
3 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 */
5
6 #ifndef _QUAGGA_BGPD_H
7 #define _QUAGGA_BGPD_H
8
9 #include "qobj.h"
10 #include <pthread.h>
11
12 #include "hook.h"
13 #include "frr_pthread.h"
14 #include "lib/json.h"
15 #include "vrf.h"
16 #include "vty.h"
17 #include "srv6.h"
18 #include "iana_afi.h"
19 #include "asn.h"
20
21 /* For union sockunion. */
22 #include "queue.h"
23 #include "sockunion.h"
24 #include "routemap.h"
25 #include "linklist.h"
26 #include "defaults.h"
27 #include "bgp_memory.h"
28 #include "bitfield.h"
29 #include "vxlan.h"
30 #include "bgp_labelpool.h"
31 #include "bgp_addpath_types.h"
32 #include "bgp_nexthop.h"
33 #include "bgp_io.h"
34
35 #include "lib/bfd.h"
36
37 #define BGP_MAX_HOSTNAME 64 /* Linux max, is larger than most other sys */
38 #define BGP_PEER_MAX_HASH_SIZE 16384
39
40 /* Default interval for IPv6 RAs when triggered by BGP unnumbered neighbor. */
41 #define BGP_UNNUM_DEFAULT_RA_INTERVAL 10
42
43 struct update_subgroup;
44 struct bpacket;
45 struct bgp_pbr_config;
46
47 /*
48 * Allow the neighbor XXXX remote-as to take internal or external
49 * AS_SPECIFIED is zero to auto-inherit original non-feature/enhancement
50 * behavior
51 * in the system.
52 */
53 enum { AS_UNSPECIFIED = 0,
54 AS_SPECIFIED,
55 AS_INTERNAL,
56 AS_EXTERNAL,
57 };
58
59 /* Zebra Gracaful Restart states */
60 enum zebra_gr_mode {
61 ZEBRA_GR_DISABLE = 0,
62 ZEBRA_GR_ENABLE
63 };
64
65 /* Typedef BGP specific types. */
66 typedef uint16_t as16_t; /* we may still encounter 16 Bit asnums */
67 typedef uint16_t bgp_size_t;
68
69 enum bgp_af_index {
70 BGP_AF_START,
71 BGP_AF_IPV4_UNICAST = BGP_AF_START,
72 BGP_AF_IPV4_MULTICAST,
73 BGP_AF_IPV4_VPN,
74 BGP_AF_IPV6_UNICAST,
75 BGP_AF_IPV6_MULTICAST,
76 BGP_AF_IPV6_VPN,
77 BGP_AF_IPV4_ENCAP,
78 BGP_AF_IPV6_ENCAP,
79 BGP_AF_L2VPN_EVPN,
80 BGP_AF_IPV4_LBL_UNICAST,
81 BGP_AF_IPV6_LBL_UNICAST,
82 BGP_AF_IPV4_FLOWSPEC,
83 BGP_AF_IPV6_FLOWSPEC,
84 BGP_AF_MAX
85 };
86
87 #define AF_FOREACH(af) for ((af) = BGP_AF_START; (af) < BGP_AF_MAX; (af)++)
88
89 #define FOREACH_SAFI(safi) \
90 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
91
92 extern struct frr_pthread *bgp_pth_io;
93 extern struct frr_pthread *bgp_pth_ka;
94
95 /* BGP master for system wide configurations and variables. */
96 struct bgp_master {
97 /* BGP instance list. */
98 struct list *bgp;
99
100 /* BGP thread master. */
101 struct event_loop *master;
102
103 /* Listening sockets */
104 struct list *listen_sockets;
105
106 /* BGP port number. */
107 uint16_t port;
108
109 /* Listener addresses */
110 struct list *addresses;
111
112 /* The Mac table */
113 struct hash *self_mac_hash;
114
115 /* BGP start time. */
116 time_t start_time;
117
118 /* Various BGP global configuration. */
119 uint8_t options;
120
121 #define BGP_OPT_NO_FIB (1 << 0)
122 #define BGP_OPT_NO_LISTEN (1 << 1)
123 #define BGP_OPT_NO_ZEBRA (1 << 2)
124
125 uint64_t updgrp_idspace;
126 uint64_t subgrp_idspace;
127
128 /* timer to dampen route map changes */
129 struct event *t_rmap_update; /* Handle route map updates */
130 uint32_t rmap_update_timer; /* Route map update timer */
131 #define RMAP_DEFAULT_UPDATE_TIMER 5 /* disabled by default */
132
133 /* Id space for automatic RD derivation for an EVI/VRF */
134 bitfield_t rd_idspace;
135
136 /* dynamic mpls label allocation pool */
137 struct labelpool labelpool;
138
139 /* BGP-EVPN VRF ID. Defaults to default VRF (if any) */
140 struct bgp* bgp_evpn;
141
142 /* How big should we set the socket buffer size */
143 uint32_t socket_buffer;
144
145 /* Should we do wait for fib install globally? */
146 bool wait_for_fib;
147
148 /* EVPN multihoming */
149 struct bgp_evpn_mh_info *mh_info;
150
151 /* global update-delay timer values */
152 uint16_t v_update_delay;
153 uint16_t v_establish_wait;
154
155 uint32_t flags;
156 #define BM_FLAG_GRACEFUL_SHUTDOWN (1 << 0)
157 #define BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA (1 << 1)
158
159 bool terminating; /* global flag that sigint terminate seen */
160
161 /* DSCP value for TCP sessions */
162 uint8_t tcp_dscp;
163
164 #define BM_DEFAULT_Q_LIMIT 10000
165 uint32_t inq_limit;
166 uint32_t outq_limit;
167
168 QOBJ_FIELDS;
169 };
170 DECLARE_QOBJ_TYPE(bgp_master);
171
172 /* BGP route-map structure. */
173 struct bgp_rmap {
174 char *name;
175 struct route_map *map;
176 };
177
178 struct bgp_redist {
179 unsigned short instance;
180
181 /* BGP redistribute metric configuration. */
182 uint8_t redist_metric_flag;
183 uint32_t redist_metric;
184
185 /* BGP redistribute route-map. */
186 struct bgp_rmap rmap;
187 };
188
189 enum vpn_policy_direction {
190 BGP_VPN_POLICY_DIR_FROMVPN = 0,
191 BGP_VPN_POLICY_DIR_TOVPN = 1,
192 BGP_VPN_POLICY_DIR_MAX = 2
193 };
194
195 struct vpn_policy {
196 struct bgp *bgp; /* parent */
197 afi_t afi;
198 struct ecommunity *rtlist[BGP_VPN_POLICY_DIR_MAX];
199 struct ecommunity *import_redirect_rtlist;
200 char *rmap_name[BGP_VPN_POLICY_DIR_MAX];
201 struct route_map *rmap[BGP_VPN_POLICY_DIR_MAX];
202
203 /* should be mpls_label_t? */
204 uint32_t tovpn_label; /* may be MPLS_LABEL_NONE */
205 uint32_t tovpn_zebra_vrf_label_last_sent;
206 char *tovpn_rd_pretty;
207 struct prefix_rd tovpn_rd;
208 struct prefix tovpn_nexthop; /* unset => set to 0 */
209 uint32_t flags;
210 #define BGP_VPN_POLICY_TOVPN_LABEL_AUTO (1 << 0)
211 #define BGP_VPN_POLICY_TOVPN_RD_SET (1 << 1)
212 #define BGP_VPN_POLICY_TOVPN_NEXTHOP_SET (1 << 2)
213 #define BGP_VPN_POLICY_TOVPN_SID_AUTO (1 << 3)
214 #define BGP_VPN_POLICY_TOVPN_LABEL_PER_NEXTHOP (1 << 4)
215
216 /*
217 * If we are importing another vrf into us keep a list of
218 * vrf names that are being imported into us.
219 */
220 struct list *import_vrf;
221
222 /*
223 * if we are being exported to another vrf keep a list of
224 * vrf names that we are being exported to.
225 */
226 struct list *export_vrf;
227
228 /*
229 * Segment-Routing SRv6 Mode
230 */
231 uint32_t tovpn_sid_index; /* unset => set to 0 */
232 struct in6_addr *tovpn_sid;
233 struct srv6_locator_chunk *tovpn_sid_locator;
234 uint32_t tovpn_sid_transpose_label;
235 struct in6_addr *tovpn_zebra_vrf_sid_last_sent;
236 };
237
238 /*
239 * Type of 'struct bgp'.
240 * - Default: The default instance
241 * - VRF: A specific (non-default) VRF
242 * - View: An instance used for route exchange
243 * The "default" instance is treated separately to simplify the code. Note
244 * that if deployed in a Multi-VRF environment, it may not exist.
245 */
246 enum bgp_instance_type {
247 BGP_INSTANCE_TYPE_DEFAULT,
248 BGP_INSTANCE_TYPE_VRF,
249 BGP_INSTANCE_TYPE_VIEW
250 };
251
252 #define BGP_SEND_EOR(bgp, afi, safi) \
253 (!CHECK_FLAG(bgp->flags, BGP_FLAG_GR_DISABLE_EOR) \
254 && ((bgp->gr_info[afi][safi].t_select_deferral == NULL) \
255 || (bgp->gr_info[afi][safi].eor_required \
256 == bgp->gr_info[afi][safi].eor_received)))
257
258 /* BGP GR Global ds */
259
260 #define BGP_GLOBAL_GR_MODE 4
261 #define BGP_GLOBAL_GR_EVENT_CMD 4
262
263 /* Graceful restart selection deferral timer info */
264 struct graceful_restart_info {
265 /* Count of EOR message expected */
266 uint32_t eor_required;
267 /* Count of EOR received */
268 uint32_t eor_received;
269 /* Deferral Timer */
270 struct event *t_select_deferral;
271 /* Routes Deferred */
272 uint32_t gr_deferred;
273 /* Best route select */
274 struct event *t_route_select;
275 /* AFI, SAFI enabled */
276 bool af_enabled[AFI_MAX][SAFI_MAX];
277 /* Route update completed */
278 bool route_sync[AFI_MAX][SAFI_MAX];
279 };
280
281 enum global_mode {
282 GLOBAL_HELPER = 0, /* This is the default mode */
283 GLOBAL_GR,
284 GLOBAL_DISABLE,
285 GLOBAL_INVALID
286 };
287
288 enum global_gr_command {
289 GLOBAL_GR_CMD = 0,
290 NO_GLOBAL_GR_CMD,
291 GLOBAL_DISABLE_CMD,
292 NO_GLOBAL_DISABLE_CMD
293 };
294
295 #define BGP_GR_SUCCESS 0
296 #define BGP_GR_FAILURE 1
297
298 /* Handling of BGP link bandwidth (LB) on receiver - whether and how to
299 * do weighted ECMP. Note: This applies after multipath computation.
300 */
301 enum bgp_link_bw_handling {
302 /* Do ECMP if some paths don't have LB - default */
303 BGP_LINK_BW_ECMP,
304 /* Completely ignore LB, just do regular ECMP */
305 BGP_LINK_BW_IGNORE_BW,
306 /* Skip paths without LB, do wECMP on others */
307 BGP_LINK_BW_SKIP_MISSING,
308 /* Do wECMP with default weight for paths not having LB */
309 BGP_LINK_BW_DEFWT_4_MISSING
310 };
311
312 RB_HEAD(bgp_es_vrf_rb_head, bgp_evpn_es_vrf);
313 RB_PROTOTYPE(bgp_es_vrf_rb_head, bgp_evpn_es_vrf, rb_node, bgp_es_vrf_rb_cmp);
314
315 struct bgp_snmp_stats {
316 /* SNMP variables for mplsL3Vpn*/
317 time_t creation_time;
318 time_t modify_time;
319 bool active;
320 uint32_t routes_added;
321 uint32_t routes_deleted;
322 };
323
324 struct bgp_srv6_function {
325 struct in6_addr sid;
326 char locator_name[SRV6_LOCNAME_SIZE];
327 };
328
329 struct as_confed {
330 as_t as;
331 char *as_pretty;
332 };
333
334 /* BGP instance structure. */
335 struct bgp {
336 /* AS number of this BGP instance. */
337 as_t as;
338 char *as_pretty;
339
340 /* Name of this BGP instance. */
341 char *name;
342 char *name_pretty; /* printable "VRF|VIEW name|default" */
343
344 /* Type of instance and VRF id. */
345 enum bgp_instance_type inst_type;
346 vrf_id_t vrf_id;
347
348 /* Reference count to allow peer_delete to finish after bgp_delete */
349 int lock;
350
351 /* Self peer. */
352 struct peer *peer_self;
353
354 /* BGP peer. */
355 struct list *peer;
356 struct hash *peerhash;
357
358 /* BGP peer group. */
359 struct list *group;
360
361 /* The maximum number of BGP dynamic neighbors that can be created */
362 int dynamic_neighbors_limit;
363
364 /* The current number of BGP dynamic neighbors */
365 int dynamic_neighbors_count;
366
367 struct hash *update_groups[BGP_AF_MAX];
368
369 /*
370 * Global statistics for update groups.
371 */
372 struct {
373 uint32_t join_events;
374 uint32_t prune_events;
375 uint32_t merge_events;
376 uint32_t split_events;
377 uint32_t updgrp_switch_events;
378 uint32_t peer_refreshes_combined;
379 uint32_t adj_count;
380 uint32_t merge_checks_triggered;
381
382 uint32_t updgrps_created;
383 uint32_t updgrps_deleted;
384 uint32_t subgrps_created;
385 uint32_t subgrps_deleted;
386 } update_group_stats;
387
388 struct bgp_snmp_stats *snmp_stats;
389
390 /* BGP configuration. */
391 uint16_t config;
392 #define BGP_CONFIG_CLUSTER_ID (1 << 0)
393 #define BGP_CONFIG_CONFEDERATION (1 << 1)
394 #define BGP_CONFIG_ASNOTATION (1 << 2)
395
396 /* BGP router identifier. */
397 struct in_addr router_id;
398 struct in_addr router_id_static;
399 struct in_addr router_id_zebra;
400
401 /* BGP route reflector cluster ID. */
402 struct in_addr cluster_id;
403
404 /* BGP confederation information. */
405 as_t confed_id;
406 char *confed_id_pretty;
407 struct as_confed *confed_peers;
408 int confed_peers_cnt;
409
410 /* start-up timer on only once at the beginning */
411 struct event *t_startup;
412
413 uint32_t v_maxmed_onstartup; /* Duration of max-med on start-up */
414 #define BGP_MAXMED_ONSTARTUP_UNCONFIGURED 0 /* 0 means off, its the default */
415 uint32_t maxmed_onstartup_value; /* Max-med value when active on
416 start-up */
417
418 /* non-null when max-med onstartup is on */
419 struct event *t_maxmed_onstartup;
420 uint8_t maxmed_onstartup_over; /* Flag to make it effective only once */
421
422 bool v_maxmed_admin; /* true/false if max-med administrative is on/off
423 */
424 #define BGP_MAXMED_ADMIN_UNCONFIGURED false /* Off by default */
425 uint32_t maxmed_admin_value; /* Max-med value when administrative in on
426 */
427 #define BGP_MAXMED_VALUE_DEFAULT 4294967294 /* Maximum by default */
428
429 uint8_t maxmed_active; /* 1/0 if max-med is active or not */
430 uint32_t maxmed_value; /* Max-med value when its active */
431
432 /* BGP update delay on startup */
433 struct event *t_update_delay;
434 struct event *t_establish_wait;
435 struct event *t_revalidate[AFI_MAX][SAFI_MAX];
436
437 uint8_t update_delay_over;
438 uint8_t main_zebra_update_hold;
439 uint8_t main_peers_update_hold;
440 uint16_t v_update_delay;
441 uint16_t v_establish_wait;
442 char update_delay_begin_time[64];
443 char update_delay_end_time[64];
444 char update_delay_zebra_resume_time[64];
445 char update_delay_peers_resume_time[64];
446 uint32_t established;
447 uint32_t restarted_peers;
448 uint32_t implicit_eors;
449 uint32_t explicit_eors;
450 #define BGP_UPDATE_DELAY_DEF 0
451 #define BGP_UPDATE_DELAY_MIN 0
452 #define BGP_UPDATE_DELAY_MAX 3600
453
454 /* Reference bandwidth for BGP link-bandwidth. Used when
455 * the LB value has to be computed based on some other
456 * factor (e.g., number of multipaths for the prefix)
457 * Value is in Mbps
458 */
459 uint32_t lb_ref_bw;
460 #define BGP_LINK_BW_REF_BW 1
461
462 /* BGP flags. */
463 uint64_t flags;
464 #define BGP_FLAG_ALWAYS_COMPARE_MED (1ULL << 0)
465 #define BGP_FLAG_DETERMINISTIC_MED (1ULL << 1)
466 #define BGP_FLAG_MED_MISSING_AS_WORST (1ULL << 2)
467 #define BGP_FLAG_MED_CONFED (1ULL << 3)
468 #define BGP_FLAG_NO_CLIENT_TO_CLIENT (1ULL << 4)
469 #define BGP_FLAG_COMPARE_ROUTER_ID (1ULL << 5)
470 #define BGP_FLAG_ASPATH_IGNORE (1ULL << 6)
471 #define BGP_FLAG_IMPORT_CHECK (1ULL << 7)
472 #define BGP_FLAG_NO_FAST_EXT_FAILOVER (1ULL << 8)
473 #define BGP_FLAG_LOG_NEIGHBOR_CHANGES (1ULL << 9)
474
475 /* This flag is set when we have full BGP Graceful-Restart mode enable */
476 #define BGP_FLAG_GRACEFUL_RESTART (1ULL << 10)
477
478 #define BGP_FLAG_ASPATH_CONFED (1ULL << 11)
479 #define BGP_FLAG_ASPATH_MULTIPATH_RELAX (1ULL << 12)
480 #define BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY (1ULL << 13)
481 #define BGP_FLAG_DISABLE_NH_CONNECTED_CHK (1ULL << 14)
482 #define BGP_FLAG_MULTIPATH_RELAX_AS_SET (1ULL << 15)
483 #define BGP_FLAG_FORCE_STATIC_PROCESS (1ULL << 16)
484 #define BGP_FLAG_SHOW_HOSTNAME (1ULL << 17)
485 #define BGP_FLAG_GR_PRESERVE_FWD (1ULL << 18)
486 #define BGP_FLAG_GRACEFUL_SHUTDOWN (1ULL << 19)
487 #define BGP_FLAG_DELETE_IN_PROGRESS (1ULL << 20)
488 #define BGP_FLAG_SELECT_DEFER_DISABLE (1ULL << 21)
489 #define BGP_FLAG_GR_DISABLE_EOR (1ULL << 22)
490 #define BGP_FLAG_EBGP_REQUIRES_POLICY (1ULL << 23)
491 #define BGP_FLAG_SHOW_NEXTHOP_HOSTNAME (1ULL << 24)
492
493 /* This flag is set if the instance is in administrative shutdown */
494 #define BGP_FLAG_SHUTDOWN (1ULL << 25)
495 #define BGP_FLAG_SUPPRESS_FIB_PENDING (1ULL << 26)
496 #define BGP_FLAG_SUPPRESS_DUPLICATES (1ULL << 27)
497 #define BGP_FLAG_PEERTYPE_MULTIPATH_RELAX (1ULL << 29)
498 /* Indicate Graceful Restart support for BGP NOTIFICATION messages */
499 #define BGP_FLAG_GRACEFUL_NOTIFICATION (1ULL << 30)
500 /* Send Hard Reset CEASE Notification for 'Administrative Reset' */
501 #define BGP_FLAG_HARD_ADMIN_RESET (1ULL << 31)
502 /* Evaluate the AIGP attribute during the best path selection process */
503 #define BGP_FLAG_COMPARE_AIGP (1ULL << 32)
504 /* For BGP-LU, force IPv4 local prefixes to use ipv4-explicit-null label */
505 #define BGP_FLAG_LU_IPV4_EXPLICIT_NULL (1ULL << 33)
506 /* For BGP-LU, force IPv6 local prefixes to use ipv6-explicit-null label */
507 #define BGP_FLAG_LU_IPV6_EXPLICIT_NULL (1ULL << 34)
508
509 /* BGP default address-families.
510 * New peers inherit enabled afi/safis from bgp instance.
511 */
512 uint16_t default_af[AFI_MAX][SAFI_MAX];
513
514 enum global_mode GLOBAL_GR_FSM[BGP_GLOBAL_GR_MODE]
515 [BGP_GLOBAL_GR_EVENT_CMD];
516 enum global_mode global_gr_present_state;
517
518 /* This variable stores the current Graceful Restart state of Zebra
519 * - ZEBRA_GR_ENABLE / ZEBRA_GR_DISABLE
520 */
521 enum zebra_gr_mode present_zebra_gr_state;
522
523 /* BGP Per AF flags */
524 uint16_t af_flags[AFI_MAX][SAFI_MAX];
525 #define BGP_CONFIG_DAMPENING (1 << 0)
526 /* l2vpn evpn flags - 1 << 0 is used for DAMPENNG */
527 #define BGP_L2VPN_EVPN_ADV_IPV4_UNICAST (1 << 1)
528 #define BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP (1 << 2)
529 #define BGP_L2VPN_EVPN_ADV_IPV6_UNICAST (1 << 3)
530 #define BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP (1 << 4)
531 #define BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4 (1 << 5)
532 #define BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6 (1 << 6)
533 /* import/export between address families */
534 #define BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT (1 << 7)
535 #define BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT (1 << 8)
536 /* vrf-route leaking flags */
537 #define BGP_CONFIG_VRF_TO_VRF_IMPORT (1 << 9)
538 #define BGP_CONFIG_VRF_TO_VRF_EXPORT (1 << 10)
539 /* vpnvx retain flag */
540 #define BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL (1 << 11)
541
542 /* BGP per AF peer count */
543 uint32_t af_peer_count[AFI_MAX][SAFI_MAX];
544
545 /* Tree for next-hop lookup cache. */
546 struct bgp_nexthop_cache_head nexthop_cache_table[AFI_MAX];
547
548 /* Tree for import-check */
549 struct bgp_nexthop_cache_head import_check_table[AFI_MAX];
550
551 struct bgp_table *connected_table[AFI_MAX];
552
553 struct hash *address_hash;
554
555 /* DB for all local tunnel-ips - used mainly for martian checks
556 Currently it only has all VxLan tunnel IPs*/
557 struct hash *tip_hash;
558
559 /* Static route configuration. */
560 struct bgp_table *route[AFI_MAX][SAFI_MAX];
561
562 /* Aggregate address configuration. */
563 struct bgp_table *aggregate[AFI_MAX][SAFI_MAX];
564
565 /* BGP routing information base. */
566 struct bgp_table *rib[AFI_MAX][SAFI_MAX];
567
568 /* BGP table route-map. */
569 struct bgp_rmap table_map[AFI_MAX][SAFI_MAX];
570
571 /* BGP redistribute configuration. */
572 struct list *redist[AFI_MAX][ZEBRA_ROUTE_MAX];
573
574 /* Allocate MPLS labels */
575 uint8_t allocate_mpls_labels[AFI_MAX][SAFI_MAX];
576
577 /* Tree for next-hop lookup cache. */
578 struct bgp_label_per_nexthop_cache_head
579 mpls_labels_per_nexthop[AFI_MAX];
580
581 /* Allocate hash entries to store policy routing information
582 * The hash are used to host pbr rules somewhere.
583 * Actually, pbr will only be used by flowspec
584 * those hash elements will have relationship together as
585 * illustrated in below diagram:
586 *
587 * pbr_action a <----- pbr_match i <--- pbr_match_entry 1..n
588 * <----- pbr_match j <--- pbr_match_entry 1..m
589 * <----- pbr_rule k
590 *
591 * - here in BGP structure, the list of match and actions will
592 * stand for the list of ipset sets, and table_ids in the kernel
593 * - the arrow above between pbr_match and pbr_action indicate
594 * that a backpointer permits match to find the action
595 * - the arrow betwen match_entry and match is a hash list
596 * contained in match, that lists the whole set of entries
597 */
598 struct hash *pbr_match_hash;
599 struct hash *pbr_rule_hash;
600 struct hash *pbr_action_hash;
601
602 /* timer to re-evaluate neighbor default-originate route-maps */
603 struct event *t_rmap_def_originate_eval;
604 #define RMAP_DEFAULT_ORIGINATE_EVAL_TIMER 5
605
606 /* BGP distance configuration. */
607 uint8_t distance_ebgp[AFI_MAX][SAFI_MAX];
608 uint8_t distance_ibgp[AFI_MAX][SAFI_MAX];
609 uint8_t distance_local[AFI_MAX][SAFI_MAX];
610
611 /* BGP default local-preference. */
612 uint32_t default_local_pref;
613
614 /* BGP default subgroup pkt queue max */
615 uint32_t default_subgroup_pkt_queue_max;
616
617 /* BGP default timer. */
618 uint32_t default_holdtime;
619 uint32_t default_keepalive;
620 uint32_t default_connect_retry;
621 uint32_t default_delayopen;
622
623 /* BGP minimum holdtime. */
624 uint16_t default_min_holdtime;
625
626 /* BGP graceful restart */
627 uint32_t restart_time;
628 uint32_t stalepath_time;
629 uint32_t select_defer_time;
630 struct graceful_restart_info gr_info[AFI_MAX][SAFI_MAX];
631 uint32_t rib_stale_time;
632
633 /* BGP Long-lived Graceful Restart */
634 uint32_t llgr_stale_time;
635
636 #define BGP_ROUTE_SELECT_DELAY 1
637 #define BGP_MAX_BEST_ROUTE_SELECT 10000
638 /* Maximum-paths configuration */
639 struct bgp_maxpaths_cfg {
640 uint16_t maxpaths_ebgp;
641 uint16_t maxpaths_ibgp;
642 bool same_clusterlen;
643 } maxpaths[AFI_MAX][SAFI_MAX];
644
645 _Atomic uint32_t wpkt_quanta; // max # packets to write per i/o cycle
646 _Atomic uint32_t rpkt_quanta; // max # packets to read per i/o cycle
647
648 /* Automatic coalesce adjust on/off */
649 bool heuristic_coalesce;
650 /* Actual coalesce time */
651 uint32_t coalesce_time;
652
653 /* Auto-shutdown new peers */
654 bool autoshutdown;
655
656 struct bgp_addpath_bgp_data tx_addpath;
657
658 #ifdef ENABLE_BGP_VNC
659 struct rfapi_cfg *rfapi_cfg;
660 struct rfapi *rfapi;
661 #endif
662
663 /* EVPN related information */
664
665 /* EVI hash table */
666 struct hash *vnihash;
667
668 /*
669 * VNI hash table based on SVI ifindex as its key.
670 * We use SVI ifindex as key to lookup a VNI table for gateway IP
671 * overlay index recursive lookup.
672 * For this purpose, a hashtable is added which optimizes this lookup.
673 */
674 struct hash *vni_svi_hash;
675
676 /* EVPN enable - advertise gateway macip routes */
677 int advertise_gw_macip;
678
679 /* EVPN enable - advertise local VNIs and their MACs etc. */
680 int advertise_all_vni;
681
682 /* draft-ietf-idr-deprecate-as-set-confed-set
683 * Reject aspaths with AS_SET and/or AS_CONFED_SET.
684 */
685 bool reject_as_sets;
686
687 struct bgp_evpn_info *evpn_info;
688
689 /* EVPN - use RFC 8365 to auto-derive RT */
690 int advertise_autort_rfc8365;
691
692 /*
693 * Flooding mechanism for BUM packets for VxLAN-EVPN.
694 */
695 enum vxlan_flood_control vxlan_flood_ctrl;
696
697 /* Hash table of Import RTs to EVIs */
698 struct hash *import_rt_hash;
699
700 /* Hash table of VRF import RTs to VRFs */
701 struct hash *vrf_import_rt_hash;
702
703 /* L3-VNI corresponding to this vrf */
704 vni_t l3vni;
705
706 /* router-mac to be used in mac-ip routes for this vrf */
707 struct ethaddr rmac;
708
709 /* originator ip - to be used as NH for type-5 routes */
710 struct in_addr originator_ip;
711
712 /* SVI associated with the L3-VNI corresponding to this vrf */
713 ifindex_t l3vni_svi_ifindex;
714
715 /* RB tree of ES-VRFs */
716 struct bgp_es_vrf_rb_head es_vrf_rb_tree;
717
718 /* Hash table of EVPN nexthops maintained per-tenant-VRF */
719 struct hash *evpn_nh_table;
720
721 /*
722 * Flag resolve_overlay_index is used for recursive resolution
723 * procedures for EVPN type-5 route's gateway IP overlay index.
724 * When this flag is set, we build remote-ip-hash for
725 * all L2VNIs and resolve overlay index nexthops using this hash.
726 * Overlay index nexthops remain unresolved if this flag is not set.
727 */
728 bool resolve_overlay_index;
729
730 /* vrf flags */
731 uint32_t vrf_flags;
732 #define BGP_VRF_AUTO (1 << 0)
733 #define BGP_VRF_IMPORT_RT_CFGD (1 << 1)
734 #define BGP_VRF_EXPORT_RT_CFGD (1 << 2)
735 #define BGP_VRF_IMPORT_AUTO_RT_CFGD (1 << 3) /* retain auto when cfgd */
736 #define BGP_VRF_EXPORT_AUTO_RT_CFGD (1 << 4) /* retain auto when cfgd */
737 #define BGP_VRF_RD_CFGD (1 << 5)
738 #define BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY (1 << 6)
739 /* per-VRF toVPN SID */
740 #define BGP_VRF_TOVPN_SID_AUTO (1 << 7)
741
742 /* unique ID for auto derivation of RD for this vrf */
743 uint16_t vrf_rd_id;
744
745 /* Automatically derived RD for this VRF */
746 struct prefix_rd vrf_prd_auto;
747
748 /* RD for this VRF */
749 struct prefix_rd vrf_prd;
750 char *vrf_prd_pretty;
751
752 /* import rt list for the vrf instance */
753 struct list *vrf_import_rtl;
754
755 /* export rt list for the vrf instance */
756 struct list *vrf_export_rtl;
757
758 /* list of corresponding l2vnis (struct bgpevpn) */
759 struct list *l2vnis;
760
761 /* route map for advertise ipv4/ipv6 unicast (type-5 routes) */
762 struct bgp_rmap adv_cmd_rmap[AFI_MAX][SAFI_MAX];
763
764 struct vpn_policy vpn_policy[AFI_MAX];
765
766 struct bgp_pbr_config *bgp_pbr_cfg;
767
768 /* Count of peers in established state */
769 uint32_t established_peers;
770
771 /* Weighted ECMP related config. */
772 enum bgp_link_bw_handling lb_handling;
773
774 /* Process Queue for handling routes */
775 struct work_queue *process_queue;
776
777 bool fast_convergence;
778
779 /* BGP Conditional advertisement */
780 uint32_t condition_check_period;
781 uint32_t condition_filter_count;
782 struct event *t_condition_check;
783
784 /* BGP VPN SRv6 backend */
785 bool srv6_enabled;
786 char srv6_locator_name[SRV6_LOCNAME_SIZE];
787 struct list *srv6_locator_chunks;
788 struct list *srv6_functions;
789 uint32_t tovpn_sid_index; /* unset => set to 0 */
790 struct in6_addr *tovpn_sid;
791 struct srv6_locator_chunk *tovpn_sid_locator;
792 uint32_t tovpn_sid_transpose_label;
793 struct in6_addr *tovpn_zebra_vrf_sid_last_sent;
794
795 /* TCP keepalive parameters for BGP connection */
796 uint16_t tcp_keepalive_idle;
797 uint16_t tcp_keepalive_intvl;
798 uint16_t tcp_keepalive_probes;
799
800 struct timeval ebgprequirespolicywarning;
801 #define FIFTEENMINUTE2USEC (int64_t)15 * 60 * 1000000
802
803 bool allow_martian;
804
805 enum asnotation_mode asnotation;
806
807 QOBJ_FIELDS;
808 };
809 DECLARE_QOBJ_TYPE(bgp);
810
811 struct bgp_interface {
812 #define BGP_INTERFACE_MPLS_BGP_FORWARDING (1 << 0)
813 uint32_t flags;
814 };
815
816 DECLARE_HOOK(bgp_inst_delete, (struct bgp *bgp), (bgp));
817 DECLARE_HOOK(bgp_inst_config_write,
818 (struct bgp *bgp, struct vty *vty),
819 (bgp, vty));
820 DECLARE_HOOK(bgp_config_end, (struct bgp *bgp), (bgp));
821
822 /* Thread callback information */
823 struct afi_safi_info {
824 afi_t afi;
825 safi_t safi;
826 struct bgp *bgp;
827 };
828
829 #define BGP_ROUTE_ADV_HOLD(bgp) (bgp->main_peers_update_hold)
830
831 #define IS_BGP_INST_KNOWN_TO_ZEBRA(bgp) \
832 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT \
833 || (bgp->inst_type == BGP_INSTANCE_TYPE_VRF \
834 && bgp->vrf_id != VRF_UNKNOWN))
835
836 #define BGP_SELECT_DEFER_DISABLE(bgp) \
837 (CHECK_FLAG(bgp->flags, BGP_FLAG_SELECT_DEFER_DISABLE))
838
839 #define BGP_SUPPRESS_FIB_ENABLED(bgp) \
840 (CHECK_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING) \
841 || bm->wait_for_fib)
842
843 /* BGP peer-group support. */
844 struct peer_group {
845 /* Name of the peer-group. */
846 char *name;
847
848 /* Pointer to BGP. */
849 struct bgp *bgp;
850
851 /* Peer-group client list. */
852 struct list *peer;
853
854 /** Dynamic neighbor listening ranges */
855 struct list *listen_range[AFI_MAX];
856
857 /* Peer-group config */
858 struct peer *conf;
859 };
860
861 /* BGP Notify message format. */
862 struct bgp_notify {
863 uint8_t code;
864 uint8_t subcode;
865 char *data;
866 bgp_size_t length;
867 uint8_t *raw_data;
868 bool hard_reset;
869 };
870
871 /* Next hop self address. */
872 struct bgp_nexthop {
873 struct interface *ifp;
874 struct in_addr v4;
875 struct in6_addr v6_global;
876 struct in6_addr v6_local;
877 };
878
879 /* BGP addpath values */
880 #define BGP_ADDPATH_RX 1
881 #define BGP_ADDPATH_TX 2
882 #define BGP_ADDPATH_ID_LEN 4
883
884 #define BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE 1
885
886 /* Route map direction */
887 #define RMAP_IN 0
888 #define RMAP_OUT 1
889 #define RMAP_MAX 2
890
891 #define BGP_DEFAULT_TTL 1
892 #define BGP_GTSM_HOPS_DISABLED 0
893 #define BGP_GTSM_HOPS_CONNECTED 1
894
895 /* Advertise map */
896 #define CONDITION_NON_EXIST false
897 #define CONDITION_EXIST true
898
899 enum update_type { UPDATE_TYPE_WITHDRAW, UPDATE_TYPE_ADVERTISE };
900
901 #include "filter.h"
902
903 /* BGP filter structure. */
904 struct bgp_filter {
905 /* Distribute-list. */
906 struct {
907 char *name;
908 struct access_list *alist;
909 } dlist[FILTER_MAX];
910
911 /* Prefix-list. */
912 struct {
913 char *name;
914 struct prefix_list *plist;
915 } plist[FILTER_MAX];
916
917 /* Filter-list. */
918 struct {
919 char *name;
920 struct as_list *aslist;
921 } aslist[FILTER_MAX];
922
923 /* Route-map. */
924 struct {
925 char *name;
926 struct route_map *map;
927 } map[RMAP_MAX];
928
929 /* Unsuppress-map. */
930 struct {
931 char *name;
932 struct route_map *map;
933 } usmap;
934
935 /* Advertise-map */
936 struct {
937 char *aname;
938 struct route_map *amap;
939
940 bool condition;
941
942 char *cname;
943 struct route_map *cmap;
944
945 enum update_type update_type;
946 } advmap;
947 };
948
949 /* IBGP/EBGP identifier. We also have a CONFED peer, which is to say,
950 a peer who's AS is part of our Confederation. */
951 enum bgp_peer_sort {
952 BGP_PEER_UNSPECIFIED,
953 BGP_PEER_IBGP,
954 BGP_PEER_EBGP,
955 BGP_PEER_INTERNAL,
956 BGP_PEER_CONFED,
957 };
958
959 /* BGP message header and packet size. */
960 #define BGP_MARKER_SIZE 16
961 #define BGP_HEADER_SIZE 19
962 #define BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE 4096
963 #define BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE 65535
964 #define BGP_MAX_PACKET_SIZE BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE
965 #define BGP_MAX_PACKET_SIZE_OVERFLOW 1024
966
967 /*
968 * Trigger delay for bgp_announce_route().
969 */
970 #define BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS 100
971 #define BGP_ANNOUNCE_ROUTE_DELAY_MS 500
972
973 struct peer_af {
974 /* back pointer to the peer */
975 struct peer *peer;
976
977 /* which subgroup the peer_af belongs to */
978 struct update_subgroup *subgroup;
979
980 /* for being part of an update subgroup's peer list */
981 LIST_ENTRY(peer_af) subgrp_train;
982
983 /* for being part of a packet's peer list */
984 LIST_ENTRY(peer_af) pkt_train;
985
986 struct bpacket *next_pkt_to_send;
987
988 /*
989 * Trigger timer for bgp_announce_route().
990 */
991 struct event *t_announce_route;
992
993 afi_t afi;
994 safi_t safi;
995 int afid;
996 };
997 /* BGP GR per peer ds */
998
999 #define BGP_PEER_GR_MODE 5
1000 #define BGP_PEER_GR_EVENT_CMD 6
1001
1002 enum peer_mode {
1003 PEER_HELPER = 0,
1004 PEER_GR,
1005 PEER_DISABLE,
1006 PEER_INVALID,
1007 PEER_GLOBAL_INHERIT /* This is the default mode */
1008
1009 };
1010
1011 enum peer_gr_command {
1012 PEER_GR_CMD = 0,
1013 NO_PEER_GR_CMD,
1014 PEER_DISABLE_CMD,
1015 NO_PEER_DISABLE_CMD,
1016 PEER_HELPER_CMD,
1017 NO_PEER_HELPER_CMD
1018 };
1019
1020 typedef unsigned int (*bgp_peer_gr_action_ptr)(struct peer *, int, int);
1021
1022 struct bgp_peer_gr {
1023 enum peer_mode next_state;
1024 bgp_peer_gr_action_ptr action_fun;
1025 };
1026
1027 /*
1028 * BGP FSM event codes, per RFC 4271 ss. 8.1
1029 */
1030 enum bgp_fsm_rfc_codes {
1031 BGP_FSM_ManualStart = 1,
1032 BGP_FSM_ManualStop = 2,
1033 BGP_FSM_AutomaticStart = 3,
1034 BGP_FSM_ManualStart_with_PassiveTcpEstablishment = 4,
1035 BGP_FSM_AutomaticStart_with_PassiveTcpEstablishment = 5,
1036 BGP_FSM_AutomaticStart_with_DampPeerOscillations = 6,
1037 BGP_FSM_AutomaticStart_with_DampPeerOscillations_and_PassiveTcpEstablishment =
1038 7,
1039 BGP_FSM_AutomaticStop = 8,
1040 BGP_FSM_ConnectRetryTimer_Expires = 9,
1041 BGP_FSM_HoldTimer_Expires = 10,
1042 BGP_FSM_KeepaliveTimer_Expires = 11,
1043 BGP_FSM_DelayOpenTimer_Expires = 12,
1044 BGP_FSM_IdleHoldTimer_Expires = 13,
1045 BGP_FSM_TcpConnection_Valid = 14,
1046 BGP_FSM_Tcp_CR_Invalid = 15,
1047 BGP_FSM_Tcp_CR_Acked = 16,
1048 BGP_FSM_TcpConnectionConfirmed = 17,
1049 BGP_FSM_TcpConnectionFails = 18,
1050 BGP_FSM_BGPOpen = 19,
1051 BGP_FSM_BGPOpen_with_DelayOpenTimer_running = 20,
1052 BGP_FSM_BGPHeaderErr = 21,
1053 BGP_FSM_BGPOpenMsgErr = 22,
1054 BGP_FSM_OpenCollisionDump = 23,
1055 BGP_FSM_NotifMsgVerErr = 24,
1056 BGP_FSM_NotifMsg = 25,
1057 BGP_FSM_KeepAliveMsg = 26,
1058 BGP_FSM_UpdateMsg = 27,
1059 BGP_FSM_UpdateMsgErr = 28
1060 };
1061
1062 /*
1063 * BGP finite state machine events
1064 *
1065 * Note: these do not correspond to RFC-defined event codes. Those are
1066 * defined elsewhere.
1067 */
1068 enum bgp_fsm_events {
1069 BGP_Start = 1,
1070 BGP_Stop,
1071 TCP_connection_open,
1072 TCP_connection_open_w_delay,
1073 TCP_connection_closed,
1074 TCP_connection_open_failed,
1075 TCP_fatal_error,
1076 ConnectRetry_timer_expired,
1077 Hold_Timer_expired,
1078 KeepAlive_timer_expired,
1079 DelayOpen_timer_expired,
1080 Receive_OPEN_message,
1081 Receive_KEEPALIVE_message,
1082 Receive_UPDATE_message,
1083 Receive_NOTIFICATION_message,
1084 Clearing_Completed,
1085 BGP_EVENTS_MAX,
1086 };
1087
1088 /* BGP finite state machine status. */
1089 enum bgp_fsm_status {
1090 Idle = 1,
1091 Connect,
1092 Active,
1093 OpenSent,
1094 OpenConfirm,
1095 Established,
1096 Clearing,
1097 Deleted,
1098 BGP_STATUS_MAX,
1099 };
1100
1101 #define PEER_HOSTNAME(peer) ((peer)->host ? (peer)->host : "(unknown peer)")
1102
1103 struct llgr_info {
1104 uint32_t stale_time;
1105 uint8_t flags;
1106 };
1107
1108 /* BGP neighbor structure. */
1109 struct peer {
1110 /* BGP structure. */
1111 struct bgp *bgp;
1112
1113 /* reference count, primarily to allow bgp_process'ing of route_node's
1114 * to be done after a struct peer is deleted.
1115 *
1116 * named 'lock' for hysterical reasons within Quagga.
1117 */
1118 int lock;
1119
1120 /* BGP peer group. */
1121 struct peer_group *group;
1122 uint64_t version[AFI_MAX][SAFI_MAX];
1123
1124 /* BGP peer_af structures, per configured AF on this peer */
1125 struct peer_af *peer_af_array[BGP_AF_MAX];
1126
1127 /* Peer's remote AS number. */
1128 int as_type;
1129 as_t as;
1130 /* for vty as format */
1131 char *as_pretty;
1132
1133 /* Peer's local AS number. */
1134 as_t local_as;
1135
1136 enum bgp_peer_sort sort;
1137
1138 /* Peer's Change local AS number. */
1139 as_t change_local_as;
1140 /* for vty as format */
1141 char *change_local_as_pretty;
1142
1143 /* Remote router ID. */
1144 struct in_addr remote_id;
1145
1146 /* Local router ID. */
1147 struct in_addr local_id;
1148
1149 /* Packet receive and send buffer. */
1150 pthread_mutex_t io_mtx; // guards ibuf, obuf
1151 struct stream_fifo *ibuf; // packets waiting to be processed
1152 struct stream_fifo *obuf; // packets waiting to be written
1153
1154 /* used as a block to deposit raw wire data to */
1155 uint8_t ibuf_scratch[BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE
1156 * BGP_READ_PACKET_MAX];
1157 struct ringbuf *ibuf_work; // WiP buffer used by bgp_read() only
1158 struct stream *obuf_work; // WiP buffer used to construct packets
1159
1160 struct stream *curr; // the current packet being parsed
1161
1162 /* We use a separate stream to encode MP_REACH_NLRI for efficient
1163 * NLRI packing. peer->obuf_work stores all the other attributes. The
1164 * actual packet is then constructed by concatenating the two.
1165 */
1166 struct stream *scratch;
1167
1168 /* the doppelganger peer structure, due to dual TCP conn setup */
1169 struct peer *doppelganger;
1170
1171 /* Status of the peer. */
1172 enum bgp_fsm_status status;
1173 enum bgp_fsm_status ostatus;
1174
1175 /* FSM events, stored for debug purposes.
1176 * Note: uchar used for reduced memory usage.
1177 */
1178 enum bgp_fsm_events cur_event;
1179 enum bgp_fsm_events last_event;
1180 enum bgp_fsm_events last_major_event;
1181
1182 /* Peer index, used for dumping TABLE_DUMP_V2 format */
1183 uint16_t table_dump_index;
1184
1185 /* Peer information */
1186 int fd; /* File descriptor */
1187 int ttl; /* TTL of TCP connection to the peer. */
1188 int rtt; /* Estimated round-trip-time from TCP_INFO */
1189 int rtt_expected; /* Expected round-trip-time for a peer */
1190 uint8_t rtt_keepalive_rcv; /* Received count for RTT shutdown */
1191 uint8_t rtt_keepalive_conf; /* Configured count for RTT shutdown */
1192 int gtsm_hops; /* minimum hopcount to peer */
1193 char *desc; /* Description of the peer. */
1194 unsigned short port; /* Destination port for peer */
1195 char *host; /* Printable address of the peer. */
1196 union sockunion su; /* Sockunion address of the peer. */
1197 #define BGP_PEER_SU_UNSPEC(peer) (peer->su.sa.sa_family == AF_UNSPEC)
1198 time_t uptime; /* Last Up/Down time */
1199 time_t readtime; /* Last read time */
1200 time_t resettime; /* Last reset time */
1201
1202 char *conf_if; /* neighbor interface config name. */
1203 struct interface *ifp; /* corresponding interface */
1204 char *ifname; /* bind interface name. */
1205 char *update_if;
1206 union sockunion *update_source;
1207
1208 union sockunion *su_local; /* Sockunion of local address. */
1209 union sockunion *su_remote; /* Sockunion of remote address. */
1210 int shared_network; /* Is this peer shared same network. */
1211 struct bgp_nexthop nexthop; /* Nexthop */
1212
1213 /* Roles in bgp session */
1214 uint8_t local_role;
1215 uint8_t remote_role;
1216 #define ROLE_PROVIDER 0
1217 #define ROLE_RS_SERVER 1
1218 #define ROLE_RS_CLIENT 2
1219 #define ROLE_CUSTOMER 3
1220 #define ROLE_PEER 4
1221 #define ROLE_UNDEFINED 255
1222
1223 #define ROLE_NAME_MAX_LEN 20
1224
1225 /* Peer address family configuration. */
1226 uint8_t afc[AFI_MAX][SAFI_MAX];
1227 uint8_t afc_nego[AFI_MAX][SAFI_MAX];
1228 uint8_t afc_adv[AFI_MAX][SAFI_MAX];
1229 uint8_t afc_recv[AFI_MAX][SAFI_MAX];
1230
1231 /* Capability flags (reset in bgp_stop) */
1232 uint32_t cap;
1233 #define PEER_CAP_REFRESH_ADV (1U << 0) /* refresh advertised */
1234 #define PEER_CAP_REFRESH_OLD_RCV (1U << 1) /* refresh old received */
1235 #define PEER_CAP_REFRESH_NEW_RCV (1U << 2) /* refresh rfc received */
1236 #define PEER_CAP_DYNAMIC_ADV (1U << 3) /* dynamic advertised */
1237 #define PEER_CAP_DYNAMIC_RCV (1U << 4) /* dynamic received */
1238 #define PEER_CAP_RESTART_ADV (1U << 5) /* restart advertised */
1239 #define PEER_CAP_RESTART_RCV (1U << 6) /* restart received */
1240 #define PEER_CAP_AS4_ADV (1U << 7) /* as4 advertised */
1241 #define PEER_CAP_AS4_RCV (1U << 8) /* as4 received */
1242 /* sent graceful-restart restart (R) bit */
1243 #define PEER_CAP_GRACEFUL_RESTART_R_BIT_ADV (1U << 9)
1244 /* received graceful-restart restart (R) bit */
1245 #define PEER_CAP_GRACEFUL_RESTART_R_BIT_RCV (1U << 10)
1246 #define PEER_CAP_ADDPATH_ADV (1U << 11) /* addpath advertised */
1247 #define PEER_CAP_ADDPATH_RCV (1U << 12) /* addpath received */
1248 #define PEER_CAP_ENHE_ADV (1U << 13) /* Extended nexthop advertised */
1249 #define PEER_CAP_ENHE_RCV (1U << 14) /* Extended nexthop received */
1250 #define PEER_CAP_HOSTNAME_ADV (1U << 15) /* hostname advertised */
1251 #define PEER_CAP_HOSTNAME_RCV (1U << 16) /* hostname received */
1252 #define PEER_CAP_ENHANCED_RR_ADV (1U << 17) /* enhanced rr advertised */
1253 #define PEER_CAP_ENHANCED_RR_RCV (1U << 18) /* enhanced rr received */
1254 #define PEER_CAP_EXTENDED_MESSAGE_ADV (1U << 19)
1255 #define PEER_CAP_EXTENDED_MESSAGE_RCV (1U << 20)
1256 #define PEER_CAP_LLGR_ADV (1U << 21)
1257 #define PEER_CAP_LLGR_RCV (1U << 22)
1258 /* sent graceful-restart notification (N) bit */
1259 #define PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV (1U << 23)
1260 /* received graceful-restart notification (N) bit */
1261 #define PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV (1U << 24)
1262 #define PEER_CAP_ROLE_ADV (1U << 25) /* role advertised */
1263 #define PEER_CAP_ROLE_RCV (1U << 26) /* role received */
1264 #define PEER_CAP_SOFT_VERSION_ADV (1U << 27)
1265 #define PEER_CAP_SOFT_VERSION_RCV (1U << 28)
1266
1267 /* Capability flags (reset in bgp_stop) */
1268 uint32_t af_cap[AFI_MAX][SAFI_MAX];
1269 #define PEER_CAP_ORF_PREFIX_SM_ADV (1U << 0) /* send-mode advertised */
1270 #define PEER_CAP_ORF_PREFIX_RM_ADV (1U << 1) /* receive-mode advertised */
1271 #define PEER_CAP_ORF_PREFIX_SM_RCV (1U << 2) /* send-mode received */
1272 #define PEER_CAP_ORF_PREFIX_RM_RCV (1U << 3) /* receive-mode received */
1273 #define PEER_CAP_ORF_PREFIX_SM_OLD_RCV (1U << 4) /* send-mode received */
1274 #define PEER_CAP_ORF_PREFIX_RM_OLD_RCV (1U << 5) /* receive-mode received */
1275 #define PEER_CAP_RESTART_AF_RCV (1U << 6) /* graceful restart afi/safi received */
1276 #define PEER_CAP_RESTART_AF_PRESERVE_RCV (1U << 7) /* graceful restart afi/safi F-bit received */
1277 #define PEER_CAP_ADDPATH_AF_TX_ADV (1U << 8) /* addpath tx advertised */
1278 #define PEER_CAP_ADDPATH_AF_TX_RCV (1U << 9) /* addpath tx received */
1279 #define PEER_CAP_ADDPATH_AF_RX_ADV (1U << 10) /* addpath rx advertised */
1280 #define PEER_CAP_ADDPATH_AF_RX_RCV (1U << 11) /* addpath rx received */
1281 #define PEER_CAP_ENHE_AF_ADV (1U << 12) /* Extended nexthopi afi/safi advertised */
1282 #define PEER_CAP_ENHE_AF_RCV (1U << 13) /* Extended nexthop afi/safi received */
1283 #define PEER_CAP_ENHE_AF_NEGO (1U << 14) /* Extended nexthop afi/safi negotiated */
1284 #define PEER_CAP_LLGR_AF_ADV (1U << 15)
1285 #define PEER_CAP_LLGR_AF_RCV (1U << 16)
1286
1287 /* Global configuration flags. */
1288 /*
1289 * Parallel array to flags that indicates whether each flag originates
1290 * from a peer-group or if it is config that is specific to this
1291 * individual peer. If a flag is set independent of the peer-group, the
1292 * same bit should be set here. If this peer is a peer-group, this
1293 * memory region should be all zeros.
1294 *
1295 * The assumption is that the default state for all flags is unset,
1296 * so if a flag is unset, the corresponding override flag is unset too.
1297 * However if a flag is set, the corresponding override flag is set.
1298 */
1299 uint64_t flags_override;
1300 /*
1301 * Parallel array to flags that indicates whether the default behavior
1302 * of *flags_override* should be inverted. If a flag is unset and the
1303 * corresponding invert flag is set, the corresponding override flag
1304 * would be set. However if a flag is set and the corresponding invert
1305 * flag is unset, the corresponding override flag would be unset.
1306 *
1307 * This can be used for attributes like *send-community*, which are
1308 * implicitely enabled and have to be disabled explicitely, compared to
1309 * 'normal' attributes like *next-hop-self* which are implicitely set.
1310 *
1311 * All operations dealing with flags should apply the following boolean
1312 * logic to keep the internal flag system in a sane state:
1313 *
1314 * value=0 invert=0 Inherit flag if member, otherwise unset flag
1315 * value=0 invert=1 Unset flag unconditionally
1316 * value=1 invert=0 Set flag unconditionally
1317 * value=1 invert=1 Inherit flag if member, otherwise set flag
1318 *
1319 * Contrary to the implementation of *flags_override*, the flag
1320 * inversion state can be set either on the peer OR the peer *and* the
1321 * peer-group. This was done on purpose, as the inversion state of a
1322 * flag can be determined on either the peer or the peer-group.
1323 *
1324 * Example: Enabling the cisco configuration mode inverts all flags
1325 * related to *send-community* unconditionally for both peer-groups and
1326 * peers.
1327 *
1328 * This behavior is different for interface peers though, which enable
1329 * the *extended-nexthop* flag by default, which regular peers do not.
1330 * As the peer-group can contain both regular and interface peers, the
1331 * flag inversion state must be set on the peer only.
1332 *
1333 * When a peer inherits the configuration from a peer-group and the
1334 * inversion state of the flag differs between peer and peer-group, the
1335 * newly set value must equal to the inverted state of the peer-group.
1336 */
1337 uint64_t flags_invert;
1338 /*
1339 * Effective array for storing the peer/peer-group flags. In case of a
1340 * peer-group, the peer-specific overrides (see flags_override and
1341 * flags_invert) must be respected.
1342 * When changing the structure of flags/af_flags, do not forget to
1343 * change flags_invert/flags_override too.
1344 */
1345 uint64_t flags;
1346 #define PEER_FLAG_PASSIVE (1ULL << 0) /* passive mode */
1347 #define PEER_FLAG_SHUTDOWN (1ULL << 1) /* shutdown */
1348 #define PEER_FLAG_DONT_CAPABILITY (1ULL << 2) /* dont-capability */
1349 #define PEER_FLAG_OVERRIDE_CAPABILITY (1ULL << 3) /* override-capability */
1350 #define PEER_FLAG_STRICT_CAP_MATCH (1ULL << 4) /* strict-match */
1351 #define PEER_FLAG_DYNAMIC_CAPABILITY (1ULL << 5) /* dynamic capability */
1352 #define PEER_FLAG_DISABLE_CONNECTED_CHECK (1ULL << 6) /* disable-connected-check */
1353 #define PEER_FLAG_LOCAL_AS_NO_PREPEND (1ULL << 7) /* local-as no-prepend */
1354 #define PEER_FLAG_LOCAL_AS_REPLACE_AS (1ULL << 8) /* local-as no-prepend replace-as */
1355 #define PEER_FLAG_DELETE (1ULL << 9) /* mark the peer for deleting */
1356 #define PEER_FLAG_CONFIG_NODE (1ULL << 10) /* the node to update configs on */
1357 #define PEER_FLAG_LONESOUL (1ULL << 11)
1358 #define PEER_FLAG_DYNAMIC_NEIGHBOR (1ULL << 12) /* dynamic neighbor */
1359 #define PEER_FLAG_CAPABILITY_ENHE (1ULL << 13) /* Extended next-hop (rfc 5549)*/
1360 #define PEER_FLAG_IFPEER_V6ONLY (1ULL << 14) /* if-based peer is v6 only */
1361 #define PEER_FLAG_IS_RFAPI_HD (1ULL << 15) /* attached to rfapi HD */
1362 #define PEER_FLAG_ENFORCE_FIRST_AS (1ULL << 16) /* enforce-first-as */
1363 #define PEER_FLAG_ROUTEADV (1ULL << 17) /* route advertise */
1364 #define PEER_FLAG_TIMER (1ULL << 18) /* keepalive & holdtime */
1365 #define PEER_FLAG_TIMER_CONNECT (1ULL << 19) /* connect timer */
1366 #define PEER_FLAG_PASSWORD (1ULL << 20) /* password */
1367 #define PEER_FLAG_LOCAL_AS (1ULL << 21) /* local-as */
1368 #define PEER_FLAG_UPDATE_SOURCE (1ULL << 22) /* update-source */
1369
1370 /* BGP-GR Peer related flags */
1371 #define PEER_FLAG_GRACEFUL_RESTART_HELPER (1ULL << 23) /* Helper */
1372 #define PEER_FLAG_GRACEFUL_RESTART (1ULL << 24) /* Graceful Restart */
1373 #define PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT (1ULL << 25) /* Global-Inherit */
1374 #define PEER_FLAG_RTT_SHUTDOWN (1ULL << 26) /* shutdown rtt */
1375 #define PEER_FLAG_TIMER_DELAYOPEN (1ULL << 27) /* delayopen timer */
1376 #define PEER_FLAG_TCP_MSS (1ULL << 28) /* tcp-mss */
1377 /* Disable IEEE floating-point link bandwidth encoding in
1378 * extended communities.
1379 */
1380 #define PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE (1ULL << 29)
1381 /* force the extended format for Optional Parameters in OPEN message */
1382 #define PEER_FLAG_EXTENDED_OPT_PARAMS (1ULL << 30)
1383
1384 /* BGP Open Policy flags.
1385 * Enforce using roles on both sides:
1386 * `local-role ROLE strict-mode` configured.
1387 */
1388 #define PEER_FLAG_ROLE_STRICT_MODE (1ULL << 31)
1389 /* `local-role` configured */
1390 #define PEER_FLAG_ROLE (1ULL << 32)
1391 #define PEER_FLAG_PORT (1ULL << 33)
1392 #define PEER_FLAG_AIGP (1ULL << 34)
1393 #define PEER_FLAG_GRACEFUL_SHUTDOWN (1ULL << 35)
1394 #define PEER_FLAG_CAPABILITY_SOFT_VERSION (1ULL << 36)
1395
1396 /*
1397 *GR-Disabled mode means unset PEER_FLAG_GRACEFUL_RESTART
1398 *& PEER_FLAG_GRACEFUL_RESTART_HELPER
1399 *and PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
1400 */
1401
1402 struct bgp_peer_gr PEER_GR_FSM[BGP_PEER_GR_MODE][BGP_PEER_GR_EVENT_CMD];
1403 enum peer_mode peer_gr_present_state;
1404 /* Non stop forwarding afi-safi count for BGP gr feature*/
1405 uint8_t nsf_af_count;
1406
1407 uint8_t peer_gr_new_status_flag;
1408 #define PEER_GRACEFUL_RESTART_NEW_STATE_HELPER (1U << 0)
1409 #define PEER_GRACEFUL_RESTART_NEW_STATE_RESTART (1U << 1)
1410 #define PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT (1U << 2)
1411
1412 /* outgoing message sent in CEASE_ADMIN_SHUTDOWN notify */
1413 char *tx_shutdown_message;
1414
1415 /* NSF mode (graceful restart) */
1416 uint8_t nsf[AFI_MAX][SAFI_MAX];
1417 /* EOR Send time */
1418 time_t eor_stime[AFI_MAX][SAFI_MAX];
1419 /* Last update packet sent time */
1420 time_t pkt_stime[AFI_MAX][SAFI_MAX];
1421
1422 /* Peer Per AF flags */
1423 /*
1424 * Please consult the comments for *flags_override*, *flags_invert* and
1425 * *flags* to understand what these three arrays do. The address-family
1426 * specific attributes are being treated the exact same way as global
1427 * peer attributes.
1428 */
1429 uint64_t af_flags_override[AFI_MAX][SAFI_MAX];
1430 uint64_t af_flags_invert[AFI_MAX][SAFI_MAX];
1431 uint64_t af_flags[AFI_MAX][SAFI_MAX];
1432 #define PEER_FLAG_SEND_COMMUNITY (1ULL << 0)
1433 #define PEER_FLAG_SEND_EXT_COMMUNITY (1ULL << 1)
1434 #define PEER_FLAG_NEXTHOP_SELF (1ULL << 2)
1435 #define PEER_FLAG_REFLECTOR_CLIENT (1ULL << 3)
1436 #define PEER_FLAG_RSERVER_CLIENT (1ULL << 4)
1437 #define PEER_FLAG_SOFT_RECONFIG (1ULL << 5)
1438 #define PEER_FLAG_AS_PATH_UNCHANGED (1ULL << 6)
1439 #define PEER_FLAG_NEXTHOP_UNCHANGED (1ULL << 7)
1440 #define PEER_FLAG_MED_UNCHANGED (1ULL << 8)
1441 #define PEER_FLAG_DEFAULT_ORIGINATE (1ULL << 9)
1442 #define PEER_FLAG_REMOVE_PRIVATE_AS (1ULL << 10)
1443 #define PEER_FLAG_ALLOWAS_IN (1ULL << 11)
1444 #define PEER_FLAG_ORF_PREFIX_SM (1ULL << 12)
1445 #define PEER_FLAG_ORF_PREFIX_RM (1ULL << 13)
1446 #define PEER_FLAG_MAX_PREFIX (1ULL << 14)
1447 #define PEER_FLAG_MAX_PREFIX_WARNING (1ULL << 15)
1448 #define PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED (1ULL << 16)
1449 #define PEER_FLAG_FORCE_NEXTHOP_SELF (1ULL << 17)
1450 #define PEER_FLAG_REMOVE_PRIVATE_AS_ALL (1ULL << 18)
1451 #define PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE (1ULL << 19)
1452 #define PEER_FLAG_AS_OVERRIDE (1ULL << 20)
1453 #define PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE (1ULL << 21)
1454 #define PEER_FLAG_WEIGHT (1ULL << 22)
1455 #define PEER_FLAG_ALLOWAS_IN_ORIGIN (1ULL << 23)
1456 #define PEER_FLAG_SEND_LARGE_COMMUNITY (1ULL << 24)
1457 #define PEER_FLAG_MAX_PREFIX_OUT (1ULL << 25)
1458 #define PEER_FLAG_MAX_PREFIX_FORCE (1ULL << 26)
1459 #define PEER_FLAG_DISABLE_ADDPATH_RX (1ULL << 27)
1460 #define PEER_FLAG_SOO (1ULL << 28)
1461 #define PEER_FLAG_ACCEPT_OWN (1ULL << 63)
1462
1463 enum bgp_addpath_strat addpath_type[AFI_MAX][SAFI_MAX];
1464
1465 /* MD5 password */
1466 char *password;
1467
1468 /* default-originate route-map. */
1469 struct {
1470 char *name;
1471 struct route_map *map;
1472 } default_rmap[AFI_MAX][SAFI_MAX];
1473
1474 /* Peer status flags. */
1475 uint16_t sflags;
1476 #define PEER_STATUS_ACCEPT_PEER (1U << 0) /* accept peer */
1477 #define PEER_STATUS_PREFIX_OVERFLOW (1U << 1) /* prefix-overflow */
1478 #define PEER_STATUS_CAPABILITY_OPEN (1U << 2) /* capability open send */
1479 #define PEER_STATUS_HAVE_ACCEPT (1U << 3) /* accept peer's parent */
1480 #define PEER_STATUS_GROUP (1U << 4) /* peer-group conf */
1481 #define PEER_STATUS_NSF_MODE (1U << 5) /* NSF aware peer */
1482 #define PEER_STATUS_NSF_WAIT (1U << 6) /* wait comeback peer */
1483 /* received extended format encoding for OPEN message */
1484 #define PEER_STATUS_EXT_OPT_PARAMS_LENGTH (1U << 7)
1485
1486 /* Peer status af flags (reset in bgp_stop) */
1487 uint16_t af_sflags[AFI_MAX][SAFI_MAX];
1488 #define PEER_STATUS_ORF_PREFIX_SEND (1U << 0) /* prefix-list send peer */
1489 #define PEER_STATUS_ORF_WAIT_REFRESH (1U << 1) /* wait refresh received peer */
1490 #define PEER_STATUS_PREFIX_THRESHOLD (1U << 2) /* exceed prefix-threshold */
1491 #define PEER_STATUS_PREFIX_LIMIT (1U << 3) /* exceed prefix-limit */
1492 #define PEER_STATUS_EOR_SEND (1U << 4) /* end-of-rib send to peer */
1493 #define PEER_STATUS_EOR_RECEIVED (1U << 5) /* end-of-rib received from peer */
1494 #define PEER_STATUS_ENHANCED_REFRESH (1U << 6) /* Enhanced Route Refresh */
1495 #define PEER_STATUS_BORR_SEND (1U << 7) /* BoRR send to peer */
1496 #define PEER_STATUS_BORR_RECEIVED (1U << 8) /* BoRR received from peer */
1497 #define PEER_STATUS_EORR_SEND (1U << 9) /* EoRR send to peer */
1498 #define PEER_STATUS_EORR_RECEIVED (1U << 10) /* EoRR received from peer */
1499 /* LLGR aware peer */
1500 #define PEER_STATUS_LLGR_WAIT (1U << 11)
1501 #define PEER_STATUS_REFRESH_PENDING (1U << 12) /* refresh request from peer */
1502 #define PEER_STATUS_RTT_SHUTDOWN (1U << 13) /* In shutdown state due to RTT */
1503
1504 /* Configured timer values. */
1505 _Atomic uint32_t holdtime;
1506 _Atomic uint32_t keepalive;
1507 _Atomic uint32_t connect;
1508 _Atomic uint32_t routeadv;
1509 _Atomic uint32_t delayopen;
1510
1511 /* Timer values. */
1512 _Atomic uint32_t v_start;
1513 _Atomic uint32_t v_connect;
1514 _Atomic uint32_t v_holdtime;
1515 _Atomic uint32_t v_keepalive;
1516 _Atomic uint32_t v_routeadv;
1517 _Atomic uint32_t v_delayopen;
1518 _Atomic uint32_t v_pmax_restart;
1519 _Atomic uint32_t v_gr_restart;
1520
1521 /* Threads. */
1522 struct event *t_read;
1523 struct event *t_write;
1524 struct event *t_start;
1525 struct event *t_connect_check_r;
1526 struct event *t_connect_check_w;
1527 struct event *t_connect;
1528 struct event *t_holdtime;
1529 struct event *t_routeadv;
1530 struct event *t_delayopen;
1531 struct event *t_pmax_restart;
1532 struct event *t_gr_restart;
1533 struct event *t_gr_stale;
1534 struct event *t_llgr_stale[AFI_MAX][SAFI_MAX];
1535 struct event *t_revalidate_all[AFI_MAX][SAFI_MAX];
1536 struct event *t_generate_updgrp_packets;
1537 struct event *t_process_packet;
1538 struct event *t_process_packet_error;
1539 struct event *t_refresh_stalepath;
1540
1541 /* Thread flags. */
1542 _Atomic uint32_t thread_flags;
1543 #define PEER_THREAD_WRITES_ON (1U << 0)
1544 #define PEER_THREAD_READS_ON (1U << 1)
1545 #define PEER_THREAD_KEEPALIVES_ON (1U << 2)
1546 #define PEER_THREAD_SUBGRP_ADV_DELAY (1U << 3)
1547
1548 /* workqueues */
1549 struct work_queue *clear_node_queue;
1550
1551 #define PEER_TOTAL_RX(peer) \
1552 atomic_load_explicit(&peer->open_in, memory_order_relaxed) \
1553 + atomic_load_explicit(&peer->update_in, memory_order_relaxed) \
1554 + atomic_load_explicit(&peer->notify_in, memory_order_relaxed) \
1555 + atomic_load_explicit(&peer->refresh_in, \
1556 memory_order_relaxed) \
1557 + atomic_load_explicit(&peer->keepalive_in, \
1558 memory_order_relaxed) \
1559 + atomic_load_explicit(&peer->dynamic_cap_in, \
1560 memory_order_relaxed)
1561
1562 #define PEER_TOTAL_TX(peer) \
1563 atomic_load_explicit(&peer->open_out, memory_order_relaxed) \
1564 + atomic_load_explicit(&peer->update_out, \
1565 memory_order_relaxed) \
1566 + atomic_load_explicit(&peer->notify_out, \
1567 memory_order_relaxed) \
1568 + atomic_load_explicit(&peer->refresh_out, \
1569 memory_order_relaxed) \
1570 + atomic_load_explicit(&peer->keepalive_out, \
1571 memory_order_relaxed) \
1572 + atomic_load_explicit(&peer->dynamic_cap_out, \
1573 memory_order_relaxed)
1574
1575 /* Statistics field */
1576 _Atomic uint32_t open_in; /* Open message input count */
1577 _Atomic uint32_t open_out; /* Open message output count */
1578 _Atomic uint32_t update_in; /* Update message input count */
1579 _Atomic uint32_t update_out; /* Update message ouput count */
1580 _Atomic time_t update_time; /* Update message received time. */
1581 _Atomic uint32_t keepalive_in; /* Keepalive input count */
1582 _Atomic uint32_t keepalive_out; /* Keepalive output count */
1583 _Atomic uint32_t notify_in; /* Notify input count */
1584 _Atomic uint32_t notify_out; /* Notify output count */
1585 _Atomic uint32_t refresh_in; /* Route Refresh input count */
1586 _Atomic uint32_t refresh_out; /* Route Refresh output count */
1587 _Atomic uint32_t dynamic_cap_in; /* Dynamic Capability input count. */
1588 _Atomic uint32_t dynamic_cap_out; /* Dynamic Capability output count. */
1589
1590 uint32_t stat_pfx_filter;
1591 uint32_t stat_pfx_aspath_loop;
1592 uint32_t stat_pfx_originator_loop;
1593 uint32_t stat_pfx_cluster_loop;
1594 uint32_t stat_pfx_nh_invalid;
1595 uint32_t stat_pfx_dup_withdraw;
1596 uint32_t stat_upd_7606; /* RFC7606: treat-as-withdraw */
1597
1598 /* BGP state count */
1599 uint32_t established; /* Established */
1600 uint32_t dropped; /* Dropped */
1601
1602 /* Update delay related fields */
1603 uint8_t update_delay_over; /* When this is set, BGP is no more waiting
1604 for EOR */
1605
1606 /* Syncronization list and time. */
1607 struct bgp_synchronize *sync[AFI_MAX][SAFI_MAX];
1608 time_t synctime;
1609 /* timestamp when the last UPDATE msg was written */
1610 _Atomic time_t last_write;
1611 /* timestamp when the last msg was written */
1612 _Atomic time_t last_update;
1613
1614 /* only updated under io_mtx.
1615 * last_sendq_warn is only for ratelimiting log warning messages.
1616 */
1617 time_t last_sendq_ok, last_sendq_warn;
1618
1619 /* Notify data. */
1620 struct bgp_notify notify;
1621
1622 /* Filter structure. */
1623 struct bgp_filter filter[AFI_MAX][SAFI_MAX];
1624
1625 /*
1626 * Parallel array to filter that indicates whether each filter
1627 * originates from a peer-group or if it is config that is specific to
1628 * this individual peer. If a filter is set independent of the
1629 * peer-group the appropriate bit should be set here. If this peer is a
1630 * peer-group, this memory region should be all zeros. The assumption
1631 * is that the default state for all flags is unset. Due to filters
1632 * having a direction (e.g. in/out/...), this array has a third
1633 * dimension for storing the overrides independently per direction.
1634 *
1635 * Notes:
1636 * - if a filter for an individual peer is unset, the corresponding
1637 * override flag is unset and the peer is considered to be back in
1638 * sync with the peer-group.
1639 * - This does *not* contain the filter values, rather it contains
1640 * whether the filter in filter (struct bgp_filter) is peer-specific.
1641 */
1642 uint8_t filter_override[AFI_MAX][SAFI_MAX][FILTER_MAX];
1643 #define PEER_FT_DISTRIBUTE_LIST (1U << 0) /* distribute-list */
1644 #define PEER_FT_FILTER_LIST (1U << 1) /* filter-list */
1645 #define PEER_FT_PREFIX_LIST (1U << 2) /* prefix-list */
1646 #define PEER_FT_ROUTE_MAP (1U << 3) /* route-map */
1647 #define PEER_FT_UNSUPPRESS_MAP (1U << 4) /* unsuppress-map */
1648 #define PEER_FT_ADVERTISE_MAP (1U << 5) /* advertise-map */
1649
1650 /* ORF Prefix-list */
1651 struct prefix_list *orf_plist[AFI_MAX][SAFI_MAX];
1652
1653 /* Text description of last attribute rcvd */
1654 char rcvd_attr_str[BUFSIZ];
1655
1656 /* Track if we printed the attribute in debugs */
1657 int rcvd_attr_printed;
1658
1659 /* Accepted prefix count */
1660 uint32_t pcount[AFI_MAX][SAFI_MAX];
1661
1662 /* Max prefix count. */
1663 uint32_t pmax[AFI_MAX][SAFI_MAX];
1664 uint8_t pmax_threshold[AFI_MAX][SAFI_MAX];
1665 uint16_t pmax_restart[AFI_MAX][SAFI_MAX];
1666 #define MAXIMUM_PREFIX_THRESHOLD_DEFAULT 75
1667
1668 /* Send prefix count. */
1669 uint32_t pmax_out[AFI_MAX][SAFI_MAX];
1670
1671 /* allowas-in. */
1672 char allowas_in[AFI_MAX][SAFI_MAX];
1673
1674 /* soo */
1675 struct ecommunity *soo[AFI_MAX][SAFI_MAX];
1676
1677 /* weight */
1678 unsigned long weight[AFI_MAX][SAFI_MAX];
1679
1680 /* peer reset cause */
1681 uint8_t last_reset;
1682 #define PEER_DOWN_RID_CHANGE 1U /* bgp router-id command */
1683 #define PEER_DOWN_REMOTE_AS_CHANGE 2U /* neighbor remote-as command */
1684 #define PEER_DOWN_LOCAL_AS_CHANGE 3U /* neighbor local-as command */
1685 #define PEER_DOWN_CLID_CHANGE 4U /* bgp cluster-id command */
1686 #define PEER_DOWN_CONFED_ID_CHANGE 5U /* bgp confederation id command */
1687 #define PEER_DOWN_CONFED_PEER_CHANGE 6U /* bgp confederation peer command */
1688 #define PEER_DOWN_RR_CLIENT_CHANGE 7U /* neighbor rr-client command */
1689 #define PEER_DOWN_RS_CLIENT_CHANGE 8U /* neighbor rs-client command */
1690 #define PEER_DOWN_UPDATE_SOURCE_CHANGE 9U /* neighbor update-source command */
1691 #define PEER_DOWN_AF_ACTIVATE 10U /* neighbor activate command */
1692 #define PEER_DOWN_USER_SHUTDOWN 11U /* neighbor shutdown command */
1693 #define PEER_DOWN_USER_RESET 12U /* clear ip bgp command */
1694 #define PEER_DOWN_NOTIFY_RECEIVED 13U /* notification received */
1695 #define PEER_DOWN_NOTIFY_SEND 14U /* notification send */
1696 #define PEER_DOWN_CLOSE_SESSION 15U /* tcp session close */
1697 #define PEER_DOWN_NEIGHBOR_DELETE 16U /* neghbor delete */
1698 #define PEER_DOWN_RMAP_BIND 17U /* neghbor peer-group command */
1699 #define PEER_DOWN_RMAP_UNBIND 18U /* no neighbor peer-group command */
1700 #define PEER_DOWN_CAPABILITY_CHANGE 19U /* neighbor capability command */
1701 #define PEER_DOWN_PASSIVE_CHANGE 20U /* neighbor passive command */
1702 #define PEER_DOWN_MULTIHOP_CHANGE 21U /* neighbor multihop command */
1703 #define PEER_DOWN_NSF_CLOSE_SESSION 22U /* NSF tcp session close */
1704 #define PEER_DOWN_V6ONLY_CHANGE 23U /* if-based peering v6only toggled */
1705 #define PEER_DOWN_BFD_DOWN 24U /* BFD down */
1706 #define PEER_DOWN_IF_DOWN 25U /* Interface down */
1707 #define PEER_DOWN_NBR_ADDR_DEL 26U /* Peer address lost */
1708 #define PEER_DOWN_WAITING_NHT 27U /* Waiting for NHT to resolve */
1709 #define PEER_DOWN_NBR_ADDR 28U /* Waiting for peer IPv6 IP Addr */
1710 #define PEER_DOWN_VRF_UNINIT 29U /* Associated VRF is not init yet */
1711 #define PEER_DOWN_NOAFI_ACTIVATED 30U /* No AFI/SAFI activated for peer */
1712 #define PEER_DOWN_AS_SETS_REJECT 31U /* Reject routes with AS_SET */
1713 #define PEER_DOWN_WAITING_OPEN 32U /* Waiting for open to succeed */
1714 #define PEER_DOWN_PFX_COUNT 33U /* Reached received prefix count */
1715 #define PEER_DOWN_SOCKET_ERROR 34U /* Some socket error happened */
1716 #define PEER_DOWN_RTT_SHUTDOWN 35U /* Automatically shutdown due to RTT */
1717 /*
1718 * Remember to update peer_down_str in bgp_fsm.c when you add
1719 * a new value to the last_reset reason
1720 */
1721
1722 uint16_t last_reset_cause_size;
1723 uint8_t last_reset_cause[BGP_MAX_PACKET_SIZE];
1724
1725 /* The kind of route-map Flags.*/
1726 uint16_t rmap_type;
1727 #define PEER_RMAP_TYPE_IN (1U << 0) /* neighbor route-map in */
1728 #define PEER_RMAP_TYPE_OUT (1U << 1) /* neighbor route-map out */
1729 #define PEER_RMAP_TYPE_NETWORK (1U << 2) /* network route-map */
1730 #define PEER_RMAP_TYPE_REDISTRIBUTE (1U << 3) /* redistribute route-map */
1731 #define PEER_RMAP_TYPE_DEFAULT (1U << 4) /* default-originate route-map */
1732 #define PEER_RMAP_TYPE_NOSET (1U << 5) /* not allow to set commands */
1733 #define PEER_RMAP_TYPE_IMPORT (1U << 6) /* neighbor route-map import */
1734 #define PEER_RMAP_TYPE_EXPORT (1U << 7) /* neighbor route-map export */
1735 #define PEER_RMAP_TYPE_AGGREGATE (1U << 8) /* aggregate-address route-map */
1736
1737 /** Peer overwrite configuration. */
1738 struct bfd_session_config {
1739 /**
1740 * Manual configuration bit.
1741 *
1742 * This flag only makes sense for real peers (and not groups),
1743 * it keeps track if the user explicitly configured BFD for a
1744 * peer.
1745 */
1746 bool manual;
1747 /** Control Plane Independent. */
1748 bool cbit;
1749 /** Detection multiplier. */
1750 uint8_t detection_multiplier;
1751 /** Minimum required RX interval. */
1752 uint32_t min_rx;
1753 /** Minimum required TX interval. */
1754 uint32_t min_tx;
1755 /** Profile name. */
1756 char profile[BFD_PROFILE_NAME_LEN];
1757 /** Peer BFD session */
1758 struct bfd_session_params *session;
1759 } * bfd_config;
1760
1761 /* hostname and domainname advertised by host */
1762 char *hostname;
1763 char *domainname;
1764
1765 /* Sender side AS path loop detection. */
1766 bool as_path_loop_detection;
1767
1768 /* Extended Message Support */
1769 uint16_t max_packet_size;
1770
1771 /* Conditional advertisement */
1772 bool advmap_config_change[AFI_MAX][SAFI_MAX];
1773 bool advmap_table_change;
1774
1775 /* set TCP max segment size */
1776 uint32_t tcp_mss;
1777
1778 /* Long-lived Graceful Restart */
1779 struct llgr_info llgr[AFI_MAX][SAFI_MAX];
1780
1781 bool shut_during_cfg;
1782
1783 #define BGP_ATTR_MAX 255
1784 /* Path attributes discard */
1785 bool discard_attrs[BGP_ATTR_MAX];
1786
1787 /* Path attributes treat-as-withdraw */
1788 bool withdraw_attrs[BGP_ATTR_MAX];
1789
1790 /* BGP Software Version Capability */
1791 #define BGP_MAX_SOFT_VERSION 64
1792 char *soft_version;
1793
1794 QOBJ_FIELDS;
1795 };
1796 DECLARE_QOBJ_TYPE(peer);
1797
1798 /* Inherit peer attribute from peer-group. */
1799 #define PEER_ATTR_INHERIT(peer, group, attr) \
1800 ((peer)->attr = (group)->conf->attr)
1801 #define PEER_STR_ATTR_INHERIT(peer, group, attr, mt) \
1802 do { \
1803 XFREE(mt, (peer)->attr); \
1804 if ((group)->conf->attr) \
1805 (peer)->attr = XSTRDUP(mt, (group)->conf->attr); \
1806 else \
1807 (peer)->attr = NULL; \
1808 } while (0)
1809 #define PEER_SU_ATTR_INHERIT(peer, group, attr) \
1810 do { \
1811 if ((peer)->attr) \
1812 sockunion_free((peer)->attr); \
1813 if ((group)->conf->attr) \
1814 (peer)->attr = sockunion_dup((group)->conf->attr); \
1815 else \
1816 (peer)->attr = NULL; \
1817 } while (0)
1818
1819 /* Check if suppress start/restart of sessions to peer. */
1820 #define BGP_PEER_START_SUPPRESSED(P) \
1821 (CHECK_FLAG((P)->flags, PEER_FLAG_SHUTDOWN) || \
1822 CHECK_FLAG((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW) || \
1823 CHECK_FLAG((P)->bgp->flags, BGP_FLAG_SHUTDOWN) || \
1824 (P)->shut_during_cfg)
1825
1826 #define PEER_ROUTE_ADV_DELAY(peer) \
1827 (CHECK_FLAG(peer->thread_flags, PEER_THREAD_SUBGRP_ADV_DELAY))
1828
1829 #define PEER_PASSWORD_MINLEN (1)
1830 #define PEER_PASSWORD_MAXLEN (80)
1831
1832 /* This structure's member directly points incoming packet data
1833 stream. */
1834 struct bgp_nlri {
1835 /* AFI. */
1836 uint16_t afi; /* iana_afi_t */
1837
1838 /* SAFI. */
1839 uint8_t safi; /* iana_safi_t */
1840
1841 /* Pointer to NLRI byte stream. */
1842 uint8_t *nlri;
1843
1844 /* Length of whole NLRI. */
1845 bgp_size_t length;
1846 };
1847
1848 /* BGP versions. */
1849 #define BGP_VERSION_4 4
1850
1851 /* Default BGP port number. */
1852 #define BGP_PORT_DEFAULT 179
1853
1854 /* Extended BGP Administrative Shutdown Communication */
1855 #define BGP_ADMIN_SHUTDOWN_MSG_LEN 255
1856
1857 /* BGP minimum message size. */
1858 #define BGP_MSG_OPEN_MIN_SIZE (BGP_HEADER_SIZE + 10)
1859 #define BGP_MSG_UPDATE_MIN_SIZE (BGP_HEADER_SIZE + 4)
1860 #define BGP_MSG_NOTIFY_MIN_SIZE (BGP_HEADER_SIZE + 2)
1861 #define BGP_MSG_KEEPALIVE_MIN_SIZE (BGP_HEADER_SIZE + 0)
1862 #define BGP_MSG_ROUTE_REFRESH_MIN_SIZE (BGP_HEADER_SIZE + 4)
1863 #define BGP_MSG_CAPABILITY_MIN_SIZE (BGP_HEADER_SIZE + 3)
1864
1865 /* BGP message types. */
1866 #define BGP_MSG_OPEN 1
1867 #define BGP_MSG_UPDATE 2
1868 #define BGP_MSG_NOTIFY 3
1869 #define BGP_MSG_KEEPALIVE 4
1870 #define BGP_MSG_ROUTE_REFRESH_NEW 5
1871 #define BGP_MSG_CAPABILITY 6
1872 #define BGP_MSG_ROUTE_REFRESH_OLD 128
1873
1874 /* BGP open optional parameter. */
1875 #define BGP_OPEN_OPT_CAP 2
1876
1877 /* BGP4 attribute type codes. */
1878 #define BGP_ATTR_ORIGIN 1
1879 #define BGP_ATTR_AS_PATH 2
1880 #define BGP_ATTR_NEXT_HOP 3
1881 #define BGP_ATTR_MULTI_EXIT_DISC 4
1882 #define BGP_ATTR_LOCAL_PREF 5
1883 #define BGP_ATTR_ATOMIC_AGGREGATE 6
1884 #define BGP_ATTR_AGGREGATOR 7
1885 #define BGP_ATTR_COMMUNITIES 8
1886 #define BGP_ATTR_ORIGINATOR_ID 9
1887 #define BGP_ATTR_CLUSTER_LIST 10
1888 #define BGP_ATTR_MP_REACH_NLRI 14
1889 #define BGP_ATTR_MP_UNREACH_NLRI 15
1890 #define BGP_ATTR_EXT_COMMUNITIES 16
1891 #define BGP_ATTR_AS4_PATH 17
1892 #define BGP_ATTR_AS4_AGGREGATOR 18
1893 #define BGP_ATTR_PMSI_TUNNEL 22
1894 #define BGP_ATTR_ENCAP 23
1895 #define BGP_ATTR_IPV6_EXT_COMMUNITIES 25
1896 #define BGP_ATTR_AIGP 26
1897 #define BGP_ATTR_LARGE_COMMUNITIES 32
1898 #define BGP_ATTR_OTC 35
1899 #define BGP_ATTR_PREFIX_SID 40
1900 #define BGP_ATTR_SRTE_COLOR 51
1901 #ifdef ENABLE_BGP_VNC_ATTR
1902 #define BGP_ATTR_VNC 255
1903 #endif
1904
1905 /* BGP update origin. */
1906 #define BGP_ORIGIN_IGP 0
1907 #define BGP_ORIGIN_EGP 1
1908 #define BGP_ORIGIN_INCOMPLETE 2
1909 #define BGP_ORIGIN_UNSPECIFIED 255
1910
1911 /* BGP notify message codes. */
1912 #define BGP_NOTIFY_HEADER_ERR 1
1913 #define BGP_NOTIFY_OPEN_ERR 2
1914 #define BGP_NOTIFY_UPDATE_ERR 3
1915 #define BGP_NOTIFY_HOLD_ERR 4
1916 #define BGP_NOTIFY_FSM_ERR 5
1917 #define BGP_NOTIFY_CEASE 6
1918 #define BGP_NOTIFY_ROUTE_REFRESH_ERR 7
1919
1920 /* Subcodes for BGP Finite State Machine Error */
1921 #define BGP_NOTIFY_FSM_ERR_SUBCODE_UNSPECIFIC 0
1922 #define BGP_NOTIFY_FSM_ERR_SUBCODE_OPENSENT 1
1923 #define BGP_NOTIFY_FSM_ERR_SUBCODE_OPENCONFIRM 2
1924 #define BGP_NOTIFY_FSM_ERR_SUBCODE_ESTABLISHED 3
1925
1926 #define BGP_NOTIFY_SUBCODE_UNSPECIFIC 0
1927
1928 /* BGP_NOTIFY_HEADER_ERR sub codes. */
1929 #define BGP_NOTIFY_HEADER_NOT_SYNC 1
1930 #define BGP_NOTIFY_HEADER_BAD_MESLEN 2
1931 #define BGP_NOTIFY_HEADER_BAD_MESTYPE 3
1932
1933 /* BGP_NOTIFY_OPEN_ERR sub codes. */
1934 #define BGP_NOTIFY_OPEN_MALFORMED_ATTR 0
1935 #define BGP_NOTIFY_OPEN_UNSUP_VERSION 1
1936 #define BGP_NOTIFY_OPEN_BAD_PEER_AS 2
1937 #define BGP_NOTIFY_OPEN_BAD_BGP_IDENT 3
1938 #define BGP_NOTIFY_OPEN_UNSUP_PARAM 4
1939 #define BGP_NOTIFY_OPEN_UNACEP_HOLDTIME 6
1940 #define BGP_NOTIFY_OPEN_UNSUP_CAPBL 7
1941 #define BGP_NOTIFY_OPEN_ROLE_MISMATCH 11
1942
1943 /* BGP_NOTIFY_UPDATE_ERR sub codes. */
1944 #define BGP_NOTIFY_UPDATE_MAL_ATTR 1
1945 #define BGP_NOTIFY_UPDATE_UNREC_ATTR 2
1946 #define BGP_NOTIFY_UPDATE_MISS_ATTR 3
1947 #define BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR 4
1948 #define BGP_NOTIFY_UPDATE_ATTR_LENG_ERR 5
1949 #define BGP_NOTIFY_UPDATE_INVAL_ORIGIN 6
1950 #define BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP 8
1951 #define BGP_NOTIFY_UPDATE_OPT_ATTR_ERR 9
1952 #define BGP_NOTIFY_UPDATE_INVAL_NETWORK 10
1953 #define BGP_NOTIFY_UPDATE_MAL_AS_PATH 11
1954
1955 /* BGP_NOTIFY_CEASE sub codes (RFC 4486). */
1956 #define BGP_NOTIFY_CEASE_MAX_PREFIX 1
1957 #define BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN 2
1958 #define BGP_NOTIFY_CEASE_PEER_UNCONFIG 3
1959 #define BGP_NOTIFY_CEASE_ADMIN_RESET 4
1960 #define BGP_NOTIFY_CEASE_CONNECT_REJECT 5
1961 #define BGP_NOTIFY_CEASE_CONFIG_CHANGE 6
1962 #define BGP_NOTIFY_CEASE_COLLISION_RESOLUTION 7
1963 #define BGP_NOTIFY_CEASE_OUT_OF_RESOURCE 8
1964 #define BGP_NOTIFY_CEASE_HARD_RESET 9
1965 #define BGP_NOTIFY_CEASE_BFD_DOWN 10
1966
1967 /* BGP_NOTIFY_ROUTE_REFRESH_ERR sub codes (RFC 7313). */
1968 #define BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN 1
1969
1970 /* BGP route refresh optional subtypes. */
1971 #define BGP_ROUTE_REFRESH_NORMAL 0
1972 #define BGP_ROUTE_REFRESH_BORR 1
1973 #define BGP_ROUTE_REFRESH_EORR 2
1974
1975 /* BGP timers default value. */
1976 #define BGP_INIT_START_TIMER 1
1977 /* The following 3 are RFC defaults that are overridden in bgp_vty.c with
1978 * version-/profile-specific values. The values here do not matter, they only
1979 * exist to provide a clear layering separation between core and CLI.
1980 */
1981 #define BGP_DEFAULT_HOLDTIME 180
1982 #define BGP_DEFAULT_KEEPALIVE 60
1983 #define BGP_DEFAULT_CONNECT_RETRY 120
1984
1985 #define BGP_DEFAULT_EBGP_ROUTEADV 0
1986 #define BGP_DEFAULT_IBGP_ROUTEADV 0
1987
1988 /* BGP RFC 4271 DelayOpenTime default value */
1989 #define BGP_DEFAULT_DELAYOPEN 120
1990
1991 /* BGP default local preference. */
1992 #define BGP_DEFAULT_LOCAL_PREF 100
1993
1994 /* BGP local-preference to send when 'bgp graceful-shutdown'
1995 * is configured */
1996 #define BGP_GSHUT_LOCAL_PREF 0
1997
1998 /* BGP default subgroup packet queue max . */
1999 #define BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX 40
2000
2001 /* BGP graceful restart */
2002 #define BGP_DEFAULT_RESTART_TIME 120
2003 #define BGP_DEFAULT_STALEPATH_TIME 360
2004 #define BGP_DEFAULT_SELECT_DEFERRAL_TIME 360
2005 #define BGP_DEFAULT_RIB_STALE_TIME 500
2006 #define BGP_DEFAULT_UPDATE_ADVERTISEMENT_TIME 1
2007
2008 /* BGP Long-lived Graceful Restart */
2009 #define BGP_DEFAULT_LLGR_STALE_TIME 0
2010
2011 /* BGP uptime string length. */
2012 #define BGP_UPTIME_LEN 25
2013
2014 /* Default configuration settings for bgpd. */
2015 #define BGP_VTY_PORT 2605
2016 #define BGP_DEFAULT_CONFIG "bgpd.conf"
2017
2018 /* BGP Dynamic Neighbors feature */
2019 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT 100
2020 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_MIN 1
2021 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_MAX 65535
2022
2023 /* BGP AIGP */
2024 #define BGP_AIGP_TLV_RESERVED 0 /* AIGP Reserved */
2025 #define BGP_AIGP_TLV_METRIC 1 /* AIGP Metric */
2026 #define BGP_AIGP_TLV_METRIC_LEN 11
2027 #define BGP_AIGP_TLV_METRIC_MAX 0xffffffffffffffffULL
2028 #define BGP_AIGP_TLV_METRIC_DESC "Accumulated IGP Metric"
2029
2030 /* Flag for peer_clear_soft(). */
2031 enum bgp_clear_type {
2032 BGP_CLEAR_SOFT_NONE,
2033 BGP_CLEAR_SOFT_OUT,
2034 BGP_CLEAR_SOFT_IN,
2035 BGP_CLEAR_SOFT_BOTH,
2036 BGP_CLEAR_SOFT_IN_ORF_PREFIX,
2037 BGP_CLEAR_MESSAGE_STATS
2038 };
2039
2040 /* Macros. */
2041 #define BGP_INPUT(P) ((P)->curr)
2042 #define BGP_INPUT_PNT(P) (stream_pnt(BGP_INPUT(P)))
2043 #define BGP_IS_VALID_STATE_FOR_NOTIF(S) \
2044 (((S) == OpenSent) || ((S) == OpenConfirm) || ((S) == Established))
2045
2046 /* BGP error codes. */
2047 enum bgp_create_error_code {
2048 BGP_SUCCESS = 0,
2049 BGP_CREATED = 1,
2050 BGP_ERR_INVALID_VALUE = -1,
2051 BGP_ERR_INVALID_FLAG = -2,
2052 BGP_ERR_INVALID_AS = -3,
2053 BGP_ERR_PEER_GROUP_MEMBER = -4,
2054 BGP_ERR_PEER_GROUP_NO_REMOTE_AS = -5,
2055 BGP_ERR_PEER_GROUP_CANT_CHANGE = -6,
2056 BGP_ERR_PEER_GROUP_MISMATCH = -7,
2057 BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT = -8,
2058 BGP_ERR_AS_MISMATCH = -9,
2059 BGP_ERR_PEER_FLAG_CONFLICT = -10,
2060 BGP_ERR_PEER_GROUP_SHUTDOWN = -11,
2061 BGP_ERR_PEER_FILTER_CONFLICT = -12,
2062 BGP_ERR_NOT_INTERNAL_PEER = -13,
2063 BGP_ERR_REMOVE_PRIVATE_AS = -14,
2064 BGP_ERR_AF_UNCONFIGURED = -15,
2065 BGP_ERR_SOFT_RECONFIG_UNCONFIGURED = -16,
2066 BGP_ERR_INSTANCE_MISMATCH = -17,
2067 BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS = -19,
2068 BGP_ERR_TCPSIG_FAILED = -20,
2069 BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK = -21,
2070 BGP_ERR_NO_IBGP_WITH_TTLHACK = -22,
2071 BGP_ERR_NO_INTERFACE_CONFIG = -23,
2072 BGP_ERR_AS_OVERRIDE = -25,
2073 BGP_ERR_INVALID_DYNAMIC_NEIGHBORS_LIMIT = -26,
2074 BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_EXISTS = -27,
2075 BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND = -28,
2076 BGP_ERR_INVALID_FOR_DYNAMIC_PEER = -29,
2077 BGP_ERR_INVALID_FOR_DIRECT_PEER = -30,
2078 BGP_ERR_PEER_SAFI_CONFLICT = -31,
2079
2080 /* BGP GR ERRORS */
2081 BGP_ERR_GR_INVALID_CMD = -32,
2082 BGP_ERR_GR_OPERATION_FAILED = -33,
2083 BGP_GR_NO_OPERATION = -34,
2084
2085 /*BGP Open Policy ERRORS */
2086 BGP_ERR_INVALID_ROLE_NAME = -35,
2087 BGP_ERR_INVALID_INTERNAL_ROLE = -36
2088 };
2089
2090 /*
2091 * Enumeration of different policy kinds a peer can be configured with.
2092 */
2093 enum bgp_policy_type {
2094 BGP_POLICY_ROUTE_MAP,
2095 BGP_POLICY_FILTER_LIST,
2096 BGP_POLICY_PREFIX_LIST,
2097 BGP_POLICY_DISTRIBUTE_LIST,
2098 };
2099
2100 /* peer_flag_change_type. */
2101 enum peer_change_type {
2102 peer_change_none,
2103 peer_change_reset,
2104 peer_change_reset_in,
2105 peer_change_reset_out,
2106 };
2107
2108 extern struct bgp_master *bm;
2109 extern unsigned int multipath_num;
2110
2111 /* Prototypes. */
2112 extern void bgp_terminate(void);
2113 extern void bgp_reset(void);
2114 extern void bgp_zclient_reset(void);
2115 extern struct bgp *bgp_get_default(void);
2116 extern struct bgp *bgp_lookup(as_t, const char *);
2117 extern struct bgp *bgp_lookup_by_name(const char *);
2118 extern struct bgp *bgp_lookup_by_vrf_id(vrf_id_t);
2119 extern struct bgp *bgp_get_evpn(void);
2120 extern void bgp_set_evpn(struct bgp *bgp);
2121 extern struct peer *peer_lookup(struct bgp *, union sockunion *);
2122 extern struct peer *peer_lookup_by_conf_if(struct bgp *, const char *);
2123 extern struct peer *peer_lookup_by_hostname(struct bgp *, const char *);
2124 extern void bgp_peer_conf_if_to_su_update(struct peer *);
2125 extern int peer_group_listen_range_del(struct peer_group *, struct prefix *);
2126 extern struct peer_group *peer_group_lookup(struct bgp *, const char *);
2127 extern struct peer_group *peer_group_get(struct bgp *, const char *);
2128 extern struct peer *peer_create_bind_dynamic_neighbor(struct bgp *,
2129 union sockunion *,
2130 struct peer_group *);
2131 extern struct prefix *
2132 peer_group_lookup_dynamic_neighbor_range(struct peer_group *, struct prefix *);
2133 extern struct peer_group *peer_group_lookup_dynamic_neighbor(struct bgp *,
2134 struct prefix *,
2135 struct prefix **);
2136 extern struct peer *peer_lookup_dynamic_neighbor(struct bgp *,
2137 union sockunion *);
2138
2139 /*
2140 * Peers are incredibly easy to memory leak
2141 * due to the various ways that they are actually used
2142 * Provide some functionality to debug locks and unlocks
2143 */
2144 extern struct peer *peer_lock_with_caller(const char *, struct peer *);
2145 extern struct peer *peer_unlock_with_caller(const char *, struct peer *);
2146 #define peer_unlock(A) peer_unlock_with_caller(__FUNCTION__, (A))
2147 #define peer_lock(B) peer_lock_with_caller(__FUNCTION__, (B))
2148
2149 extern enum bgp_peer_sort peer_sort(struct peer *peer);
2150 extern enum bgp_peer_sort peer_sort_lookup(struct peer *peer);
2151
2152 extern bool peer_active(struct peer *);
2153 extern bool peer_active_nego(struct peer *);
2154 extern bool peer_afc_received(struct peer *peer);
2155 extern bool peer_afc_advertised(struct peer *peer);
2156 extern void bgp_recalculate_all_bestpaths(struct bgp *bgp);
2157 extern struct peer *peer_create(union sockunion *su, const char *conf_if,
2158 struct bgp *bgp, as_t local_as, as_t remote_as,
2159 int as_type, struct peer_group *group,
2160 bool config_node, const char *as_str);
2161 extern struct peer *peer_create_accept(struct bgp *);
2162 extern void peer_xfer_config(struct peer *dst, struct peer *src);
2163 extern char *peer_uptime(time_t uptime2, char *buf, size_t len, bool use_json,
2164 json_object *json);
2165
2166 extern int bgp_config_write(struct vty *);
2167
2168 extern void bgp_master_init(struct event_loop *master, const int buffer_size,
2169 struct list *addresses);
2170
2171 extern void bgp_init(unsigned short instance);
2172 extern void bgp_pthreads_run(void);
2173 extern void bgp_pthreads_finish(void);
2174 extern void bgp_route_map_init(void);
2175 extern void bgp_session_reset(struct peer *);
2176
2177 extern int bgp_option_set(int);
2178 extern int bgp_option_unset(int);
2179 extern int bgp_option_check(int);
2180
2181 /* set the bgp no-rib option during runtime and remove installed routes */
2182 extern void bgp_option_norib_set_runtime(void);
2183
2184 /* unset the bgp no-rib option during runtime and reset all peers */
2185 extern void bgp_option_norib_unset_runtime(void);
2186
2187 extern int bgp_get(struct bgp **bgp, as_t *as, const char *name,
2188 enum bgp_instance_type kind, const char *as_pretty,
2189 enum asnotation_mode asnotation);
2190 extern void bgp_instance_up(struct bgp *);
2191 extern void bgp_instance_down(struct bgp *);
2192 extern int bgp_delete(struct bgp *);
2193
2194 extern int bgp_handle_socket(struct bgp *bgp, struct vrf *vrf,
2195 vrf_id_t old_vrf_id, bool create);
2196
2197 extern void bgp_router_id_zebra_bump(vrf_id_t, const struct prefix *);
2198 extern void bgp_router_id_static_set(struct bgp *, struct in_addr);
2199
2200 extern void bm_wait_for_fib_set(bool set);
2201 extern void bgp_suppress_fib_pending_set(struct bgp *bgp, bool set);
2202 extern void bgp_cluster_id_set(struct bgp *bgp, struct in_addr *cluster_id);
2203 extern void bgp_cluster_id_unset(struct bgp *bgp);
2204
2205 extern void bgp_confederation_id_set(struct bgp *bgp, as_t as,
2206 const char *as_str);
2207 extern void bgp_confederation_id_unset(struct bgp *bgp);
2208 extern bool bgp_confederation_peers_check(struct bgp *, as_t);
2209
2210 extern void bgp_confederation_peers_add(struct bgp *bgp, as_t as,
2211 const char *as_str);
2212 extern void bgp_confederation_peers_remove(struct bgp *bgp, as_t as);
2213
2214 extern void bgp_timers_set(struct bgp *, uint32_t keepalive, uint32_t holdtime,
2215 uint32_t connect_retry, uint32_t delayopen);
2216 extern void bgp_timers_unset(struct bgp *);
2217
2218 extern void bgp_default_local_preference_set(struct bgp *bgp,
2219 uint32_t local_pref);
2220 extern void bgp_default_local_preference_unset(struct bgp *bgp);
2221
2222 extern void bgp_default_subgroup_pkt_queue_max_set(struct bgp *bgp,
2223 uint32_t queue_size);
2224 extern void bgp_default_subgroup_pkt_queue_max_unset(struct bgp *bgp);
2225
2226 extern void bgp_listen_limit_set(struct bgp *bgp, int listen_limit);
2227 extern void bgp_listen_limit_unset(struct bgp *bgp);
2228
2229 extern bool bgp_update_delay_active(struct bgp *);
2230 extern bool bgp_update_delay_configured(struct bgp *);
2231 extern bool bgp_afi_safi_peer_exists(struct bgp *bgp, afi_t afi, safi_t safi);
2232 extern void peer_as_change(struct peer *peer, as_t as, int as_type,
2233 const char *as_str);
2234 extern int peer_remote_as(struct bgp *bgp, union sockunion *su,
2235 const char *conf_if, as_t *as, int as_type,
2236 const char *as_str);
2237 extern int peer_group_remote_as(struct bgp *bgp, const char *peer_str, as_t *as,
2238 int as_type, const char *as_str);
2239 extern int peer_delete(struct peer *peer);
2240 extern void peer_notify_unconfig(struct peer *peer);
2241 extern int peer_group_delete(struct peer_group *);
2242 extern int peer_group_remote_as_delete(struct peer_group *);
2243 extern int peer_group_listen_range_add(struct peer_group *, struct prefix *);
2244 extern void peer_group_notify_unconfig(struct peer_group *group);
2245
2246 extern int peer_activate(struct peer *, afi_t, safi_t);
2247 extern int peer_deactivate(struct peer *, afi_t, safi_t);
2248
2249 extern int peer_group_bind(struct bgp *, union sockunion *, struct peer *,
2250 struct peer_group *, as_t *);
2251
2252 extern int peer_flag_set(struct peer *peer, uint64_t flag);
2253 extern int peer_flag_unset(struct peer *peer, uint64_t flag);
2254 extern void peer_flag_inherit(struct peer *peer, uint64_t flag);
2255
2256 extern int peer_af_flag_set(struct peer *peer, afi_t afi, safi_t safi,
2257 uint64_t flag);
2258 extern int peer_af_flag_unset(struct peer *peer, afi_t afi, safi_t safi,
2259 uint64_t flag);
2260 extern bool peer_af_flag_check(struct peer *peer, afi_t afi, safi_t safi,
2261 uint64_t flag);
2262 extern void peer_af_flag_inherit(struct peer *peer, afi_t afi, safi_t safi,
2263 uint64_t flag);
2264 extern void peer_change_action(struct peer *peer, afi_t afi, safi_t safi,
2265 enum peer_change_type type);
2266
2267 extern int peer_ebgp_multihop_set(struct peer *, int);
2268 extern int peer_ebgp_multihop_unset(struct peer *);
2269 extern int is_ebgp_multihop_configured(struct peer *peer);
2270
2271 extern int peer_role_set(struct peer *peer, uint8_t role, bool strict_mode);
2272 extern int peer_role_unset(struct peer *peer);
2273
2274 extern void peer_description_set(struct peer *, const char *);
2275 extern void peer_description_unset(struct peer *);
2276
2277 extern int peer_update_source_if_set(struct peer *, const char *);
2278 extern void peer_update_source_addr_set(struct peer *peer,
2279 const union sockunion *su);
2280 extern void peer_update_source_unset(struct peer *peer);
2281
2282 extern int peer_default_originate_set(struct peer *peer, afi_t afi, safi_t safi,
2283 const char *rmap,
2284 struct route_map *route_map);
2285 extern int peer_default_originate_unset(struct peer *, afi_t, safi_t);
2286 extern void bgp_tcp_keepalive_set(struct bgp *bgp, uint16_t idle,
2287 uint16_t interval, uint16_t probes);
2288 extern void bgp_tcp_keepalive_unset(struct bgp *bgp);
2289
2290 extern void peer_port_set(struct peer *, uint16_t);
2291 extern void peer_port_unset(struct peer *);
2292
2293 extern int peer_weight_set(struct peer *, afi_t, safi_t, uint16_t);
2294 extern int peer_weight_unset(struct peer *, afi_t, safi_t);
2295
2296 extern int peer_timers_set(struct peer *, uint32_t keepalive,
2297 uint32_t holdtime);
2298 extern int peer_timers_unset(struct peer *);
2299
2300 extern int peer_timers_connect_set(struct peer *, uint32_t);
2301 extern int peer_timers_connect_unset(struct peer *);
2302
2303 extern int peer_advertise_interval_set(struct peer *, uint32_t);
2304 extern int peer_advertise_interval_unset(struct peer *);
2305
2306 extern int peer_timers_delayopen_set(struct peer *peer, uint32_t delayopen);
2307 extern int peer_timers_delayopen_unset(struct peer *peer);
2308
2309 extern void peer_interface_set(struct peer *, const char *);
2310 extern void peer_interface_unset(struct peer *);
2311
2312 extern int peer_distribute_set(struct peer *, afi_t, safi_t, int, const char *);
2313 extern int peer_distribute_unset(struct peer *, afi_t, safi_t, int);
2314
2315 extern int peer_allowas_in_set(struct peer *, afi_t, safi_t, int, int);
2316 extern int peer_allowas_in_unset(struct peer *, afi_t, safi_t);
2317
2318 extern int peer_local_as_set(struct peer *peer, as_t as, bool no_prepend,
2319 bool replace_as, const char *as_str);
2320 extern int peer_local_as_unset(struct peer *);
2321
2322 extern int peer_prefix_list_set(struct peer *, afi_t, safi_t, int,
2323 const char *);
2324 extern int peer_prefix_list_unset(struct peer *, afi_t, safi_t, int);
2325
2326 extern int peer_aslist_set(struct peer *, afi_t, safi_t, int, const char *);
2327 extern int peer_aslist_unset(struct peer *, afi_t, safi_t, int);
2328
2329 extern int peer_route_map_set(struct peer *peer, afi_t afi, safi_t safi, int,
2330 const char *name, struct route_map *route_map);
2331 extern int peer_route_map_unset(struct peer *, afi_t, safi_t, int);
2332
2333 extern int peer_unsuppress_map_set(struct peer *peer, afi_t afi, safi_t safi,
2334 const char *name,
2335 struct route_map *route_map);
2336
2337 extern int peer_advertise_map_set(struct peer *peer, afi_t afi, safi_t safi,
2338 const char *advertise_name,
2339 struct route_map *advertise_map,
2340 const char *condition_name,
2341 struct route_map *condition_map,
2342 bool condition);
2343
2344 extern int peer_password_set(struct peer *, const char *);
2345 extern int peer_password_unset(struct peer *);
2346
2347 extern int peer_unsuppress_map_unset(struct peer *, afi_t, safi_t);
2348
2349 extern int peer_advertise_map_unset(struct peer *peer, afi_t afi, safi_t safi,
2350 const char *advertise_name,
2351 struct route_map *advertise_map,
2352 const char *condition_name,
2353 struct route_map *condition_map,
2354 bool condition);
2355
2356 extern int peer_maximum_prefix_set(struct peer *, afi_t, safi_t, uint32_t,
2357 uint8_t, int, uint16_t, bool force);
2358 extern int peer_maximum_prefix_unset(struct peer *, afi_t, safi_t);
2359
2360 extern void peer_maximum_prefix_out_refresh_routes(struct peer *peer, afi_t afi,
2361 safi_t safi);
2362 extern int peer_maximum_prefix_out_set(struct peer *peer, afi_t afi,
2363 safi_t safi, uint32_t max);
2364 extern int peer_maximum_prefix_out_unset(struct peer *peer, afi_t afi,
2365 safi_t safi);
2366
2367 extern int peer_clear(struct peer *, struct listnode **);
2368 extern int peer_clear_soft(struct peer *, afi_t, safi_t, enum bgp_clear_type);
2369
2370 extern int peer_ttl_security_hops_set(struct peer *, int);
2371 extern int peer_ttl_security_hops_unset(struct peer *);
2372
2373 extern void peer_tx_shutdown_message_set(struct peer *, const char *msg);
2374 extern void peer_tx_shutdown_message_unset(struct peer *);
2375
2376 extern void bgp_route_map_update_timer(struct event *thread);
2377 extern const char *bgp_get_name_by_role(uint8_t role);
2378 extern enum asnotation_mode bgp_get_asnotation(struct bgp *bgp);
2379
2380 extern void bgp_route_map_terminate(void);
2381
2382 extern int peer_cmp(struct peer *p1, struct peer *p2);
2383
2384 extern int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi, iana_safi_t pkt_safi,
2385 afi_t *afi, safi_t *safi);
2386 extern int bgp_map_afi_safi_int2iana(afi_t afi, safi_t safi,
2387 iana_afi_t *pkt_afi,
2388 iana_safi_t *pkt_safi);
2389
2390 extern struct peer_af *peer_af_create(struct peer *, afi_t, safi_t);
2391 extern struct peer_af *peer_af_find(struct peer *, afi_t, safi_t);
2392 extern int peer_af_delete(struct peer *, afi_t, safi_t);
2393
2394 extern void bgp_shutdown_enable(struct bgp *bgp, const char *msg);
2395 extern void bgp_shutdown_disable(struct bgp *bgp);
2396
2397 extern void bgp_close(void);
2398 extern void bgp_free(struct bgp *);
2399 void bgp_gr_apply_running_config(void);
2400
2401 /* BGP GR */
2402 int bgp_global_gr_init(struct bgp *bgp);
2403 int bgp_peer_gr_init(struct peer *peer);
2404
2405
2406 #define BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(_bgp, _peer_list) \
2407 do { \
2408 struct peer *peer_loop; \
2409 bool gr_router_detected = false; \
2410 struct listnode *node = {0}; \
2411 for (ALL_LIST_ELEMENTS_RO(_peer_list, node, peer_loop)) { \
2412 if (CHECK_FLAG(peer_loop->flags, \
2413 PEER_FLAG_GRACEFUL_RESTART)) \
2414 gr_router_detected = true; \
2415 } \
2416 if (gr_router_detected \
2417 && _bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) { \
2418 bgp_zebra_send_capabilities(_bgp, false); \
2419 } else if (!gr_router_detected \
2420 && _bgp->present_zebra_gr_state \
2421 == ZEBRA_GR_ENABLE) { \
2422 bgp_zebra_send_capabilities(_bgp, true); \
2423 } \
2424 } while (0)
2425
2426 static inline struct bgp *bgp_lock(struct bgp *bgp)
2427 {
2428 bgp->lock++;
2429 return bgp;
2430 }
2431
2432 static inline void bgp_unlock(struct bgp *bgp)
2433 {
2434 assert(bgp->lock > 0);
2435 if (--bgp->lock == 0)
2436 bgp_free(bgp);
2437 }
2438
2439 static inline int afindex(afi_t afi, safi_t safi)
2440 {
2441 switch (afi) {
2442 case AFI_IP:
2443 switch (safi) {
2444 case SAFI_UNICAST:
2445 return BGP_AF_IPV4_UNICAST;
2446 case SAFI_MULTICAST:
2447 return BGP_AF_IPV4_MULTICAST;
2448 case SAFI_LABELED_UNICAST:
2449 return BGP_AF_IPV4_LBL_UNICAST;
2450 case SAFI_MPLS_VPN:
2451 return BGP_AF_IPV4_VPN;
2452 case SAFI_ENCAP:
2453 return BGP_AF_IPV4_ENCAP;
2454 case SAFI_FLOWSPEC:
2455 return BGP_AF_IPV4_FLOWSPEC;
2456 case SAFI_EVPN:
2457 case SAFI_UNSPEC:
2458 case SAFI_MAX:
2459 return BGP_AF_MAX;
2460 }
2461 break;
2462 case AFI_IP6:
2463 switch (safi) {
2464 case SAFI_UNICAST:
2465 return BGP_AF_IPV6_UNICAST;
2466 case SAFI_MULTICAST:
2467 return BGP_AF_IPV6_MULTICAST;
2468 case SAFI_LABELED_UNICAST:
2469 return BGP_AF_IPV6_LBL_UNICAST;
2470 case SAFI_MPLS_VPN:
2471 return BGP_AF_IPV6_VPN;
2472 case SAFI_ENCAP:
2473 return BGP_AF_IPV6_ENCAP;
2474 case SAFI_FLOWSPEC:
2475 return BGP_AF_IPV6_FLOWSPEC;
2476 case SAFI_EVPN:
2477 case SAFI_UNSPEC:
2478 case SAFI_MAX:
2479 return BGP_AF_MAX;
2480 }
2481 break;
2482 case AFI_L2VPN:
2483 switch (safi) {
2484 case SAFI_EVPN:
2485 return BGP_AF_L2VPN_EVPN;
2486 case SAFI_UNICAST:
2487 case SAFI_MULTICAST:
2488 case SAFI_LABELED_UNICAST:
2489 case SAFI_MPLS_VPN:
2490 case SAFI_ENCAP:
2491 case SAFI_FLOWSPEC:
2492 case SAFI_UNSPEC:
2493 case SAFI_MAX:
2494 return BGP_AF_MAX;
2495 }
2496 break;
2497 case AFI_UNSPEC:
2498 case AFI_MAX:
2499 return BGP_AF_MAX;
2500 }
2501
2502 assert(!"Reached end of function we should never hit");
2503 }
2504
2505 /* If the peer is not a peer-group but is bound to a peer-group return 1 */
2506 static inline int peer_group_active(struct peer *peer)
2507 {
2508 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP) && peer->group)
2509 return 1;
2510 return 0;
2511 }
2512
2513 /* If peer is negotiated at least one address family return 1. */
2514 static inline int peer_afi_active_nego(const struct peer *peer, afi_t afi)
2515 {
2516 if (peer->afc_nego[afi][SAFI_UNICAST]
2517 || peer->afc_nego[afi][SAFI_MULTICAST]
2518 || peer->afc_nego[afi][SAFI_LABELED_UNICAST]
2519 || peer->afc_nego[afi][SAFI_MPLS_VPN]
2520 || peer->afc_nego[afi][SAFI_ENCAP]
2521 || peer->afc_nego[afi][SAFI_FLOWSPEC]
2522 || peer->afc_nego[afi][SAFI_EVPN])
2523 return 1;
2524 return 0;
2525 }
2526
2527 /* If at least one address family activated for group, return 1. */
2528 static inline int peer_group_af_configured(struct peer_group *group)
2529 {
2530 struct peer *peer = group->conf;
2531
2532 if (peer->afc[AFI_IP][SAFI_UNICAST] || peer->afc[AFI_IP][SAFI_MULTICAST]
2533 || peer->afc[AFI_IP][SAFI_LABELED_UNICAST]
2534 || peer->afc[AFI_IP][SAFI_FLOWSPEC]
2535 || peer->afc[AFI_IP][SAFI_MPLS_VPN] || peer->afc[AFI_IP][SAFI_ENCAP]
2536 || peer->afc[AFI_IP6][SAFI_UNICAST]
2537 || peer->afc[AFI_IP6][SAFI_MULTICAST]
2538 || peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]
2539 || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
2540 || peer->afc[AFI_IP6][SAFI_ENCAP]
2541 || peer->afc[AFI_IP6][SAFI_FLOWSPEC]
2542 || peer->afc[AFI_L2VPN][SAFI_EVPN])
2543 return 1;
2544 return 0;
2545 }
2546
2547 static inline char *timestamp_string(time_t ts)
2548 {
2549 time_t tbuf;
2550 tbuf = time(NULL) - (monotime(NULL) - ts);
2551 return ctime(&tbuf);
2552 }
2553
2554 static inline bool peer_established(struct peer *peer)
2555 {
2556 return peer->status == Established;
2557 }
2558
2559 static inline bool peer_dynamic_neighbor(struct peer *peer)
2560 {
2561 return CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR);
2562 }
2563
2564 static inline bool peer_dynamic_neighbor_no_nsf(struct peer *peer)
2565 {
2566 return (peer_dynamic_neighbor(peer) &&
2567 !CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT));
2568 }
2569
2570 static inline int peer_cap_enhe(struct peer *peer, afi_t afi, safi_t safi)
2571 {
2572 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ENHE_AF_NEGO));
2573 }
2574
2575 /* Lookup VRF for BGP instance based on its type. */
2576 static inline struct vrf *bgp_vrf_lookup_by_instance_type(struct bgp *bgp)
2577 {
2578 struct vrf *vrf;
2579
2580 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
2581 vrf = vrf_lookup_by_id(VRF_DEFAULT);
2582 else if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
2583 vrf = vrf_lookup_by_name(bgp->name);
2584 else
2585 vrf = NULL;
2586
2587 return vrf;
2588 }
2589
2590 static inline uint32_t bgp_vrf_interfaces(struct bgp *bgp, bool active)
2591 {
2592 struct vrf *vrf;
2593 struct interface *ifp;
2594 uint32_t count = 0;
2595
2596 /* if there is one interface in the vrf which is up then it is deemed
2597 * active
2598 */
2599 vrf = bgp_vrf_lookup_by_instance_type(bgp);
2600 if (vrf == NULL)
2601 return 0;
2602 RB_FOREACH (ifp, if_name_head, &vrf->ifaces_by_name) {
2603 if (strcmp(ifp->name, bgp->name) == 0)
2604 continue;
2605 if (!active || if_is_up(ifp))
2606 count++;
2607 }
2608 return count;
2609 }
2610
2611 /* Link BGP instance to VRF. */
2612 static inline void bgp_vrf_link(struct bgp *bgp, struct vrf *vrf)
2613 {
2614 bgp->vrf_id = vrf->vrf_id;
2615 if (vrf->info != (void *)bgp)
2616 vrf->info = (void *)bgp_lock(bgp);
2617 }
2618
2619 /* Unlink BGP instance from VRF. */
2620 static inline void bgp_vrf_unlink(struct bgp *bgp, struct vrf *vrf)
2621 {
2622 if (vrf->info == (void *)bgp) {
2623 vrf->info = NULL;
2624 bgp_unlock(bgp);
2625 }
2626 bgp->vrf_id = VRF_UNKNOWN;
2627 }
2628
2629 static inline bool bgp_in_graceful_shutdown(struct bgp *bgp)
2630 {
2631 /* True if either set for this instance or globally */
2632 return (!!CHECK_FLAG(bgp->flags, BGP_FLAG_GRACEFUL_SHUTDOWN) ||
2633 !!CHECK_FLAG(bm->flags, BM_FLAG_GRACEFUL_SHUTDOWN));
2634 }
2635
2636 /* For benefit of rfapi */
2637 extern struct peer *peer_new(struct bgp *bgp);
2638
2639 extern struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
2640 const char *ip_str, bool use_json);
2641 extern int bgp_lookup_by_as_name_type(struct bgp **bgp_val, as_t *as,
2642 const char *name,
2643 enum bgp_instance_type inst_type);
2644
2645 /* Hooks */
2646 DECLARE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp),
2647 (bgp, ifp));
2648 DECLARE_HOOK(peer_status_changed, (struct peer *peer), (peer));
2649 DECLARE_HOOK(bgp_snmp_init_stats, (struct bgp *bgp), (bgp));
2650 DECLARE_HOOK(bgp_snmp_update_last_changed, (struct bgp *bgp), (bgp));
2651 DECLARE_HOOK(bgp_snmp_update_stats,
2652 (struct bgp_node *rn, struct bgp_path_info *pi, bool added),
2653 (rn, pi, added));
2654 DECLARE_HOOK(bgp_rpki_prefix_status,
2655 (struct peer * peer, struct attr *attr,
2656 const struct prefix *prefix),
2657 (peer, attr, prefix));
2658
2659 void peer_nsf_stop(struct peer *peer);
2660
2661 void peer_tcp_mss_set(struct peer *peer, uint32_t tcp_mss);
2662 void peer_tcp_mss_unset(struct peer *peer);
2663
2664 extern void bgp_recalculate_afi_safi_bestpaths(struct bgp *bgp, afi_t afi,
2665 safi_t safi);
2666 extern void peer_on_policy_change(struct peer *peer, afi_t afi, safi_t safi,
2667 int outbound);
2668 extern bool bgp_path_attribute_discard(struct peer *peer, char *buf,
2669 size_t size);
2670 extern bool bgp_path_attribute_treat_as_withdraw(struct peer *peer, char *buf,
2671 size_t size);
2672 #ifdef _FRR_ATTRIBUTE_PRINTFRR
2673 /* clang-format off */
2674 #pragma FRR printfrr_ext "%pBP" (struct peer *)
2675 /* clang-format on */
2676 #endif
2677
2678 #endif /* _QUAGGA_BGPD_H */