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