]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgpd.h
bgpd: Adding BGP GR Global & Per Neighbour FSM changes
[mirror_frr.git] / bgpd / bgpd.h
1 /* BGP message definition header.
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _QUAGGA_BGPD_H
22 #define _QUAGGA_BGPD_H
23
24 #include "qobj.h"
25 #include <pthread.h>
26
27 #include "hook.h"
28 #include "frr_pthread.h"
29 #include "lib/json.h"
30 #include "vrf.h"
31 #include "vty.h"
32 #include "iana_afi.h"
33
34 /* For union sockunion. */
35 #include "queue.h"
36 #include "sockunion.h"
37 #include "routemap.h"
38 #include "linklist.h"
39 #include "defaults.h"
40 #include "bgp_memory.h"
41 #include "bitfield.h"
42 #include "vxlan.h"
43 #include "bgp_labelpool.h"
44 #include "bgp_addpath_types.h"
45
46 #define BGP_MAX_HOSTNAME 64 /* Linux max, is larger than most other sys */
47 #define BGP_PEER_MAX_HASH_SIZE 16384
48
49 /* Default interval for IPv6 RAs when triggered by BGP unnumbered neighbor. */
50 #define BGP_UNNUM_DEFAULT_RA_INTERVAL 10
51
52 struct update_subgroup;
53 struct bpacket;
54 struct bgp_pbr_config;
55
56 /*
57 * Allow the neighbor XXXX remote-as to take internal or external
58 * AS_SPECIFIED is zero to auto-inherit original non-feature/enhancement
59 * behavior
60 * in the system.
61 */
62 enum { AS_UNSPECIFIED = 0,
63 AS_SPECIFIED,
64 AS_INTERNAL,
65 AS_EXTERNAL,
66 };
67
68 /* Typedef BGP specific types. */
69 typedef uint32_t as_t;
70 typedef uint16_t as16_t; /* we may still encounter 16 Bit asnums */
71 typedef uint16_t bgp_size_t;
72
73 #define max(a, b) \
74 ({ \
75 __typeof__(a) _a = (a); \
76 __typeof__(b) _b = (b); \
77 _a > _b ? _a : _b; \
78 })
79
80 enum bgp_af_index {
81 BGP_AF_START,
82 BGP_AF_IPV4_UNICAST = BGP_AF_START,
83 BGP_AF_IPV4_MULTICAST,
84 BGP_AF_IPV4_VPN,
85 BGP_AF_IPV6_UNICAST,
86 BGP_AF_IPV6_MULTICAST,
87 BGP_AF_IPV6_VPN,
88 BGP_AF_IPV4_ENCAP,
89 BGP_AF_IPV6_ENCAP,
90 BGP_AF_L2VPN_EVPN,
91 BGP_AF_IPV4_LBL_UNICAST,
92 BGP_AF_IPV6_LBL_UNICAST,
93 BGP_AF_IPV4_FLOWSPEC,
94 BGP_AF_IPV6_FLOWSPEC,
95 BGP_AF_MAX
96 };
97
98 #define AF_FOREACH(af) for ((af) = BGP_AF_START; (af) < BGP_AF_MAX; (af)++)
99
100 #define FOREACH_AFI_SAFI(afi, safi) \
101 for (afi = AFI_IP; afi < AFI_MAX; afi++) \
102 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
103
104 #define FOREACH_SAFI(safi) \
105 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
106
107 extern struct frr_pthread *bgp_pth_io;
108 extern struct frr_pthread *bgp_pth_ka;
109
110 /* BGP master for system wide configurations and variables. */
111 struct bgp_master {
112 /* BGP instance list. */
113 struct list *bgp;
114
115 /* BGP thread master. */
116 struct thread_master *master;
117
118 /* work queues */
119 struct work_queue *process_main_queue;
120
121 /* Listening sockets */
122 struct list *listen_sockets;
123
124 /* BGP port number. */
125 uint16_t port;
126
127 /* Listener address */
128 char *address;
129
130 /* The Mac table */
131 struct hash *self_mac_hash;
132
133 /* BGP start time. */
134 time_t start_time;
135
136 /* Various BGP global configuration. */
137 uint8_t options;
138 #define BGP_OPT_NO_FIB (1 << 0)
139 #define BGP_OPT_NO_LISTEN (1 << 1)
140 #define BGP_OPT_NO_ZEBRA (1 << 2)
141
142 uint64_t updgrp_idspace;
143 uint64_t subgrp_idspace;
144
145 /* timer to dampen route map changes */
146 struct thread *t_rmap_update; /* Handle route map updates */
147 uint32_t rmap_update_timer; /* Route map update timer */
148 #define RMAP_DEFAULT_UPDATE_TIMER 5 /* disabled by default */
149
150 /* Id space for automatic RD derivation for an EVI/VRF */
151 bitfield_t rd_idspace;
152
153 /* dynamic mpls label allocation pool */
154 struct labelpool labelpool;
155
156 /* BGP-EVPN VRF ID. Defaults to default VRF (if any) */
157 struct bgp* bgp_evpn;
158
159 /* How big should we set the socket buffer size */
160 uint32_t socket_buffer;
161
162 bool terminating; /* global flag that sigint terminate seen */
163 QOBJ_FIELDS
164 };
165 DECLARE_QOBJ_TYPE(bgp_master)
166
167 /* BGP route-map structure. */
168 struct bgp_rmap {
169 char *name;
170 struct route_map *map;
171 };
172
173 struct bgp_redist {
174 unsigned short instance;
175
176 /* BGP redistribute metric configuration. */
177 uint8_t redist_metric_flag;
178 uint32_t redist_metric;
179
180 /* BGP redistribute route-map. */
181 struct bgp_rmap rmap;
182 };
183
184 typedef enum {
185 BGP_VPN_POLICY_DIR_FROMVPN = 0,
186 BGP_VPN_POLICY_DIR_TOVPN = 1,
187 BGP_VPN_POLICY_DIR_MAX = 2
188 } vpn_policy_direction_t;
189
190 struct vpn_policy {
191 struct bgp *bgp; /* parent */
192 afi_t afi;
193 struct ecommunity *rtlist[BGP_VPN_POLICY_DIR_MAX];
194 struct ecommunity *import_redirect_rtlist;
195 char *rmap_name[BGP_VPN_POLICY_DIR_MAX];
196 struct route_map *rmap[BGP_VPN_POLICY_DIR_MAX];
197
198 /* should be mpls_label_t? */
199 uint32_t tovpn_label; /* may be MPLS_LABEL_NONE */
200 uint32_t tovpn_zebra_vrf_label_last_sent;
201 struct prefix_rd tovpn_rd;
202 struct prefix tovpn_nexthop; /* unset => set to 0 */
203 uint32_t flags;
204 #define BGP_VPN_POLICY_TOVPN_LABEL_AUTO (1 << 0)
205 #define BGP_VPN_POLICY_TOVPN_RD_SET (1 << 1)
206 #define BGP_VPN_POLICY_TOVPN_NEXTHOP_SET (1 << 2)
207
208 /*
209 * If we are importing another vrf into us keep a list of
210 * vrf names that are being imported into us.
211 */
212 struct list *import_vrf;
213
214 /*
215 * if we are being exported to another vrf keep a list of
216 * vrf names that we are being exported to.
217 */
218 struct list *export_vrf;
219 };
220
221 /*
222 * Type of 'struct bgp'.
223 * - Default: The default instance
224 * - VRF: A specific (non-default) VRF
225 * - View: An instance used for route exchange
226 * The "default" instance is treated separately to simplify the code. Note
227 * that if deployed in a Multi-VRF environment, it may not exist.
228 */
229 enum bgp_instance_type {
230 BGP_INSTANCE_TYPE_DEFAULT,
231 BGP_INSTANCE_TYPE_VRF,
232 BGP_INSTANCE_TYPE_VIEW
233 };
234
235
236 /* BGP GR Global ds */
237
238 #define GLOBAL_MODE 4
239 #define EVENT_CMD 4
240
241 /* Graceful restart selection deferral timer info */
242 struct graceful_restart_info {
243 /* Count of EOR message expected */
244 uint32_t eor_required;
245 /* Count of EOR received */
246 uint32_t eor_received;
247 /* Deferral Timer */
248 struct thread *t_select_deferral;
249 /* Route list */
250 struct list *route_list;
251 /* Best route select */
252 struct thread *t_route_select;
253 };
254
255 enum global_mode {
256 GLOBAL_HELPER = 0, /* This is the default mode */
257 GLOBAL_GR,
258 GLOBAL_DISABLE,
259 GLOBAL_INVALID
260 };
261
262 enum global_gr_command {
263 GLOBAL_GR_CMD = 0,
264 NO_GLOBAL_GR_CMD,
265 GLOBAL_DISABLE_CMD,
266 NO_GLOBAL_DISABLE_CMD
267 };
268
269 #define BGP_GR_SUCCESS 0
270 #define BGP_GR_FAILURE 1
271
272 /* BGP instance structure. */
273 struct bgp {
274 /* AS number of this BGP instance. */
275 as_t as;
276
277 /* Name of this BGP instance. */
278 char *name;
279 char *name_pretty; /* printable "VRF|VIEW name|default" */
280
281 /* Type of instance and VRF id. */
282 enum bgp_instance_type inst_type;
283 vrf_id_t vrf_id;
284
285 /* Reference count to allow peer_delete to finish after bgp_delete */
286 int lock;
287
288 /* Self peer. */
289 struct peer *peer_self;
290
291 /* BGP peer. */
292 struct list *peer;
293 struct hash *peerhash;
294
295 /* BGP peer group. */
296 struct list *group;
297
298 /* The maximum number of BGP dynamic neighbors that can be created */
299 int dynamic_neighbors_limit;
300
301 /* The current number of BGP dynamic neighbors */
302 int dynamic_neighbors_count;
303
304 struct hash *update_groups[BGP_AF_MAX];
305
306 /*
307 * Global statistics for update groups.
308 */
309 struct {
310 uint32_t join_events;
311 uint32_t prune_events;
312 uint32_t merge_events;
313 uint32_t split_events;
314 uint32_t updgrp_switch_events;
315 uint32_t peer_refreshes_combined;
316 uint32_t adj_count;
317 uint32_t merge_checks_triggered;
318
319 uint32_t updgrps_created;
320 uint32_t updgrps_deleted;
321 uint32_t subgrps_created;
322 uint32_t subgrps_deleted;
323 } update_group_stats;
324
325 /* BGP configuration. */
326 uint16_t config;
327 #define BGP_CONFIG_CLUSTER_ID (1 << 0)
328 #define BGP_CONFIG_CONFEDERATION (1 << 1)
329
330 /* BGP router identifier. */
331 struct in_addr router_id;
332 struct in_addr router_id_static;
333 struct in_addr router_id_zebra;
334
335 /* BGP route reflector cluster ID. */
336 struct in_addr cluster_id;
337
338 /* BGP confederation information. */
339 as_t confed_id;
340 as_t *confed_peers;
341 int confed_peers_cnt;
342
343 struct thread
344 *t_startup; /* start-up timer on only once at the beginning */
345
346 uint32_t v_maxmed_onstartup; /* Duration of max-med on start-up */
347 #define BGP_MAXMED_ONSTARTUP_UNCONFIGURED 0 /* 0 means off, its the default */
348 uint32_t maxmed_onstartup_value; /* Max-med value when active on
349 start-up */
350 struct thread
351 *t_maxmed_onstartup; /* non-null when max-med onstartup is on */
352 uint8_t maxmed_onstartup_over; /* Flag to make it effective only once */
353
354 uint8_t v_maxmed_admin; /* 1/0 if max-med administrative is on/off */
355 #define BGP_MAXMED_ADMIN_UNCONFIGURED 0 /* Off by default */
356 uint32_t maxmed_admin_value; /* Max-med value when administrative in on
357 */
358 #define BGP_MAXMED_VALUE_DEFAULT 4294967294 /* Maximum by default */
359
360 uint8_t maxmed_active; /* 1/0 if max-med is active or not */
361 uint32_t maxmed_value; /* Max-med value when its active */
362
363 /* BGP update delay on startup */
364 struct thread *t_update_delay;
365 struct thread *t_establish_wait;
366 uint8_t update_delay_over;
367 uint8_t main_zebra_update_hold;
368 uint8_t main_peers_update_hold;
369 uint16_t v_update_delay;
370 uint16_t v_establish_wait;
371 char update_delay_begin_time[64];
372 char update_delay_end_time[64];
373 char update_delay_zebra_resume_time[64];
374 char update_delay_peers_resume_time[64];
375 uint32_t established;
376 uint32_t restarted_peers;
377 uint32_t implicit_eors;
378 uint32_t explicit_eors;
379 #define BGP_UPDATE_DELAY_DEF 0
380 #define BGP_UPDATE_DELAY_MIN 0
381 #define BGP_UPDATE_DELAY_MAX 3600
382
383 /* BGP flags. */
384 uint32_t flags;
385 #define BGP_FLAG_ALWAYS_COMPARE_MED (1 << 0)
386 #define BGP_FLAG_DETERMINISTIC_MED (1 << 1)
387 #define BGP_FLAG_MED_MISSING_AS_WORST (1 << 2)
388 #define BGP_FLAG_MED_CONFED (1 << 3)
389 #define BGP_FLAG_NO_DEFAULT_IPV4 (1 << 4)
390 #define BGP_FLAG_NO_CLIENT_TO_CLIENT (1 << 5)
391 #define BGP_FLAG_COMPARE_ROUTER_ID (1 << 7)
392 #define BGP_FLAG_ASPATH_IGNORE (1 << 8)
393 #define BGP_FLAG_IMPORT_CHECK (1 << 9)
394 #define BGP_FLAG_NO_FAST_EXT_FAILOVER (1 << 10)
395 #define BGP_FLAG_LOG_NEIGHBOR_CHANGES (1 << 11)
396
397 /* This flag is set when we have full BGP Graceful-Restart mode enable */
398 #define BGP_FLAG_GRACEFUL_RESTART (1 << 12)
399
400 #define BGP_FLAG_ASPATH_CONFED (1 << 13)
401 #define BGP_FLAG_ASPATH_MULTIPATH_RELAX (1 << 14)
402 #define BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY (1 << 15)
403 #define BGP_FLAG_DISABLE_NH_CONNECTED_CHK (1 << 16)
404 #define BGP_FLAG_MULTIPATH_RELAX_AS_SET (1 << 17)
405 #define BGP_FLAG_FORCE_STATIC_PROCESS (1 << 18)
406 #define BGP_FLAG_SHOW_HOSTNAME (1 << 19)
407 #define BGP_FLAG_GR_PRESERVE_FWD (1 << 20)
408 #define BGP_FLAG_GRACEFUL_SHUTDOWN (1 << 21)
409 #define BGP_FLAG_DELETE_IN_PROGRESS (1 << 22)
410
411
412 enum global_mode GLOBAL_GR_FSM[GLOBAL_MODE][EVENT_CMD];
413 enum global_mode global_gr_present_state;
414
415 /* BGP Per AF flags */
416 uint16_t af_flags[AFI_MAX][SAFI_MAX];
417 #define BGP_CONFIG_DAMPENING (1 << 0)
418 /* l2vpn evpn flags - 1 << 0 is used for DAMPENNG */
419 #define BGP_L2VPN_EVPN_ADVERTISE_IPV4_UNICAST (1 << 1)
420 #define BGP_L2VPN_EVPN_ADVERTISE_IPV6_UNICAST (1 << 2)
421 #define BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4 (1 << 3)
422 #define BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6 (1 << 4)
423 /* import/export between address families */
424 #define BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT (1 << 5)
425 #define BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT (1 << 6)
426 /* vrf-route leaking flags */
427 #define BGP_CONFIG_VRF_TO_VRF_IMPORT (1 << 7)
428 #define BGP_CONFIG_VRF_TO_VRF_EXPORT (1 << 8)
429
430 /* BGP per AF peer count */
431 uint32_t af_peer_count[AFI_MAX][SAFI_MAX];
432
433 /* Route table for next-hop lookup cache. */
434 struct bgp_table *nexthop_cache_table[AFI_MAX];
435
436 /* Route table for import-check */
437 struct bgp_table *import_check_table[AFI_MAX];
438
439 struct bgp_table *connected_table[AFI_MAX];
440
441 struct hash *address_hash;
442
443 /* DB for all local tunnel-ips - used mainly for martian checks
444 Currently it only has all VxLan tunnel IPs*/
445 struct hash *tip_hash;
446
447 /* Static route configuration. */
448 struct bgp_table *route[AFI_MAX][SAFI_MAX];
449
450 /* Aggregate address configuration. */
451 struct bgp_table *aggregate[AFI_MAX][SAFI_MAX];
452
453 /* BGP routing information base. */
454 struct bgp_table *rib[AFI_MAX][SAFI_MAX];
455
456 /* BGP table route-map. */
457 struct bgp_rmap table_map[AFI_MAX][SAFI_MAX];
458
459 /* BGP redistribute configuration. */
460 struct list *redist[AFI_MAX][ZEBRA_ROUTE_MAX];
461
462 /* Allocate MPLS labels */
463 uint8_t allocate_mpls_labels[AFI_MAX][SAFI_MAX];
464
465 /* Allocate hash entries to store policy routing information
466 * The hash are used to host pbr rules somewhere.
467 * Actually, pbr will only be used by flowspec
468 * those hash elements will have relationship together as
469 * illustrated in below diagram:
470 *
471 * pbr_action a <----- pbr_match i <--- pbr_match_entry 1..n
472 * <----- pbr_match j <--- pbr_match_entry 1..m
473 * <----- pbr_rule k
474 *
475 * - here in BGP structure, the list of match and actions will
476 * stand for the list of ipset sets, and table_ids in the kernel
477 * - the arrow above between pbr_match and pbr_action indicate
478 * that a backpointer permits match to find the action
479 * - the arrow betwen match_entry and match is a hash list
480 * contained in match, that lists the whole set of entries
481 */
482 struct hash *pbr_match_hash;
483 struct hash *pbr_rule_hash;
484 struct hash *pbr_action_hash;
485
486 /* timer to re-evaluate neighbor default-originate route-maps */
487 struct thread *t_rmap_def_originate_eval;
488 #define RMAP_DEFAULT_ORIGINATE_EVAL_TIMER 5
489
490 /* BGP distance configuration. */
491 uint8_t distance_ebgp[AFI_MAX][SAFI_MAX];
492 uint8_t distance_ibgp[AFI_MAX][SAFI_MAX];
493 uint8_t distance_local[AFI_MAX][SAFI_MAX];
494
495 /* BGP default local-preference. */
496 uint32_t default_local_pref;
497
498 /* BGP default subgroup pkt queue max */
499 uint32_t default_subgroup_pkt_queue_max;
500
501 /* BGP default timer. */
502 uint32_t default_holdtime;
503 uint32_t default_keepalive;
504 uint32_t default_connect_retry;
505
506 /* BGP graceful restart */
507 uint32_t restart_time;
508 uint32_t stalepath_time;
509 uint32_t select_defer_time;
510 struct graceful_restart_info gr_info[AFI_MAX][SAFI_MAX];
511
512 /* Maximum-paths configuration */
513 struct bgp_maxpaths_cfg {
514 uint16_t maxpaths_ebgp;
515 uint16_t maxpaths_ibgp;
516 uint16_t ibgp_flags;
517 #define BGP_FLAG_IBGP_MULTIPATH_SAME_CLUSTERLEN (1 << 0)
518 } maxpaths[AFI_MAX][SAFI_MAX];
519
520 _Atomic uint32_t wpkt_quanta; // max # packets to write per i/o cycle
521 _Atomic uint32_t rpkt_quanta; // max # packets to read per i/o cycle
522
523 /* Automatic coalesce adjust on/off */
524 bool heuristic_coalesce;
525 /* Actual coalesce time */
526 uint32_t coalesce_time;
527
528 /* Auto-shutdown new peers */
529 bool autoshutdown;
530
531 struct bgp_addpath_bgp_data tx_addpath;
532
533 #if ENABLE_BGP_VNC
534 struct rfapi_cfg *rfapi_cfg;
535 struct rfapi *rfapi;
536 #endif
537
538 /* EVPN related information */
539
540 /* EVI hash table */
541 struct hash *vnihash;
542
543 /* EVPN enable - advertise gateway macip routes */
544 int advertise_gw_macip;
545
546 /* EVPN enable - advertise local VNIs and their MACs etc. */
547 int advertise_all_vni;
548
549 /* RFC 8212 - prevent route leaks. */
550 int ebgp_requires_policy;
551 #define DEFAULT_EBGP_POLICY_DISABLED 0
552 #define DEFAULT_EBGP_POLICY_ENABLED 1
553
554 /* draft-ietf-idr-deprecate-as-set-confed-set
555 * Reject aspaths with AS_SET and/or AS_CONFED_SET.
556 */
557 bool reject_as_sets;
558 #define BGP_REJECT_AS_SETS_DISABLED 0
559 #define BGP_REJECT_AS_SETS_ENABLED 1
560
561 struct bgp_evpn_info *evpn_info;
562
563 /* EVPN - use RFC 8365 to auto-derive RT */
564 int advertise_autort_rfc8365;
565
566 /*
567 * Flooding mechanism for BUM packets for VxLAN-EVPN.
568 */
569 enum vxlan_flood_control vxlan_flood_ctrl;
570
571 /* Hash table of Import RTs to EVIs */
572 struct hash *import_rt_hash;
573
574 /* Hash table of VRF import RTs to VRFs */
575 struct hash *vrf_import_rt_hash;
576
577 /* L3-VNI corresponding to this vrf */
578 vni_t l3vni;
579
580 /* router-mac to be used in mac-ip routes for this vrf */
581 struct ethaddr rmac;
582
583 /* originator ip - to be used as NH for type-5 routes */
584 struct in_addr originator_ip;
585
586 /* SVI associated with the L3-VNI corresponding to this vrf */
587 ifindex_t l3vni_svi_ifindex;
588
589 /* vrf flags */
590 uint32_t vrf_flags;
591 #define BGP_VRF_AUTO (1 << 0)
592 #define BGP_VRF_IMPORT_RT_CFGD (1 << 1)
593 #define BGP_VRF_EXPORT_RT_CFGD (1 << 2)
594 #define BGP_VRF_RD_CFGD (1 << 3)
595 #define BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY (1 << 4)
596
597
598 /* unique ID for auto derivation of RD for this vrf */
599 uint16_t vrf_rd_id;
600
601 /* Automatically derived RD for this VRF */
602 struct prefix_rd vrf_prd_auto;
603
604 /* RD for this VRF */
605 struct prefix_rd vrf_prd;
606
607 /* import rt list for the vrf instance */
608 struct list *vrf_import_rtl;
609
610 /* export rt list for the vrf instance */
611 struct list *vrf_export_rtl;
612
613 /* list of corresponding l2vnis (struct bgpevpn) */
614 struct list *l2vnis;
615
616 /* route map for advertise ipv4/ipv6 unicast (type-5 routes) */
617 struct bgp_rmap adv_cmd_rmap[AFI_MAX][SAFI_MAX];
618
619 struct vpn_policy vpn_policy[AFI_MAX];
620
621 struct bgp_pbr_config *bgp_pbr_cfg;
622
623 /* local esi hash table */
624 struct hash *esihash;
625
626 /* Count of peers in established state */
627 uint32_t established_peers;
628
629 QOBJ_FIELDS
630 };
631 DECLARE_QOBJ_TYPE(bgp)
632
633 DECLARE_HOOK(bgp_inst_delete, (struct bgp *bgp), (bgp))
634 DECLARE_HOOK(bgp_inst_config_write,
635 (struct bgp *bgp, struct vty *vty),
636 (bgp, vty))
637
638 #define BGP_ROUTE_ADV_HOLD(bgp) (bgp->main_peers_update_hold)
639
640 #define IS_BGP_INST_KNOWN_TO_ZEBRA(bgp) \
641 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT \
642 || (bgp->inst_type == BGP_INSTANCE_TYPE_VRF \
643 && bgp->vrf_id != VRF_UNKNOWN))
644
645 /* BGP peer-group support. */
646 struct peer_group {
647 /* Name of the peer-group. */
648 char *name;
649
650 /* Pointer to BGP. */
651 struct bgp *bgp;
652
653 /* Peer-group client list. */
654 struct list *peer;
655
656 /** Dynamic neighbor listening ranges */
657 struct list *listen_range[AFI_MAX];
658
659 /* Peer-group config */
660 struct peer *conf;
661 };
662
663 /* BGP Notify message format. */
664 struct bgp_notify {
665 uint8_t code;
666 uint8_t subcode;
667 char *data;
668 bgp_size_t length;
669 uint8_t *raw_data;
670 };
671
672 /* Next hop self address. */
673 struct bgp_nexthop {
674 struct interface *ifp;
675 struct in_addr v4;
676 struct in6_addr v6_global;
677 struct in6_addr v6_local;
678 };
679
680 /* BGP addpath values */
681 #define BGP_ADDPATH_RX 1
682 #define BGP_ADDPATH_TX 2
683 #define BGP_ADDPATH_ID_LEN 4
684
685 #define BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE 1
686
687 /* Route map direction */
688 #define RMAP_IN 0
689 #define RMAP_OUT 1
690 #define RMAP_MAX 2
691
692 #define BGP_DEFAULT_TTL 1
693
694 #include "filter.h"
695
696 /* BGP filter structure. */
697 struct bgp_filter {
698 /* Distribute-list. */
699 struct {
700 char *name;
701 struct access_list *alist;
702 } dlist[FILTER_MAX];
703
704 /* Prefix-list. */
705 struct {
706 char *name;
707 struct prefix_list *plist;
708 } plist[FILTER_MAX];
709
710 /* Filter-list. */
711 struct {
712 char *name;
713 struct as_list *aslist;
714 } aslist[FILTER_MAX];
715
716 /* Route-map. */
717 struct {
718 char *name;
719 struct route_map *map;
720 } map[RMAP_MAX];
721
722 /* Unsuppress-map. */
723 struct {
724 char *name;
725 struct route_map *map;
726 } usmap;
727 };
728
729 /* IBGP/EBGP identifier. We also have a CONFED peer, which is to say,
730 a peer who's AS is part of our Confederation. */
731 typedef enum {
732 BGP_PEER_UNSPECIFIED,
733 BGP_PEER_IBGP,
734 BGP_PEER_EBGP,
735 BGP_PEER_INTERNAL,
736 BGP_PEER_CONFED,
737 } bgp_peer_sort_t;
738
739 /* BGP message header and packet size. */
740 #define BGP_MARKER_SIZE 16
741 #define BGP_HEADER_SIZE 19
742 #define BGP_MAX_PACKET_SIZE 4096
743 #define BGP_MAX_PACKET_SIZE_OVERFLOW 1024
744
745 /*
746 * Trigger delay for bgp_announce_route().
747 */
748 #define BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS 100
749 #define BGP_ANNOUNCE_ROUTE_DELAY_MS 500
750
751 struct peer_af {
752 /* back pointer to the peer */
753 struct peer *peer;
754
755 /* which subgroup the peer_af belongs to */
756 struct update_subgroup *subgroup;
757
758 /* for being part of an update subgroup's peer list */
759 LIST_ENTRY(peer_af) subgrp_train;
760
761 /* for being part of a packet's peer list */
762 LIST_ENTRY(peer_af) pkt_train;
763
764 struct bpacket *next_pkt_to_send;
765
766 /*
767 * Trigger timer for bgp_announce_route().
768 */
769 struct thread *t_announce_route;
770
771 afi_t afi;
772 safi_t safi;
773 int afid;
774 };
775 /* BGP GR per peer ds */
776
777 #define PEER_MODE 5
778 #define PEER_EVENT_CMD 6
779
780 enum peer_mode {
781 PEER_HELPER = 0,
782 PEER_GR,
783 PEER_DISABLE,
784 PEER_INVALID,
785 PEER_GLOBAL_INHERIT /* This is the default mode */
786
787 };
788
789 enum peer_gr_command {
790 PEER_GR_CMD = 0,
791 NO_PEER_GR_CMD,
792 PEER_DISABLE_cmd,
793 NO_PEER_DISABLE_CMD,
794 PEER_HELPER_CMD,
795 NO_PEER_HELPER_CMD
796 };
797
798 typedef unsigned int (*bgp_peer_gr_action_ptr)(struct peer *, int, int);
799
800 struct bgp_peer_gr {
801 enum peer_mode next_state;
802 bgp_peer_gr_action_ptr action_fun;
803 };
804
805
806 /* BGP neighbor structure. */
807 struct peer {
808 /* BGP structure. */
809 struct bgp *bgp;
810
811 /* reference count, primarily to allow bgp_process'ing of route_node's
812 * to be done after a struct peer is deleted.
813 *
814 * named 'lock' for hysterical reasons within Quagga.
815 */
816 int lock;
817
818 /* BGP peer group. */
819 struct peer_group *group;
820 uint64_t version[AFI_MAX][SAFI_MAX];
821
822 /* BGP peer_af structures, per configured AF on this peer */
823 struct peer_af *peer_af_array[BGP_AF_MAX];
824
825 /* Peer's remote AS number. */
826 int as_type;
827 as_t as;
828
829 /* Peer's local AS number. */
830 as_t local_as;
831
832 bgp_peer_sort_t sort;
833
834 /* Peer's Change local AS number. */
835 as_t change_local_as;
836
837 /* Remote router ID. */
838 struct in_addr remote_id;
839
840 /* Local router ID. */
841 struct in_addr local_id;
842
843 /* Packet receive and send buffer. */
844 pthread_mutex_t io_mtx; // guards ibuf, obuf
845 struct stream_fifo *ibuf; // packets waiting to be processed
846 struct stream_fifo *obuf; // packets waiting to be written
847
848 struct ringbuf *ibuf_work; // WiP buffer used by bgp_read() only
849 struct stream *obuf_work; // WiP buffer used to construct packets
850
851 struct stream *curr; // the current packet being parsed
852
853 /* We use a separate stream to encode MP_REACH_NLRI for efficient
854 * NLRI packing. peer->obuf_work stores all the other attributes. The
855 * actual packet is then constructed by concatenating the two.
856 */
857 struct stream *scratch;
858
859 /* the doppelganger peer structure, due to dual TCP conn setup */
860 struct peer *doppelganger;
861
862 /* Status of the peer. */
863 int status;
864 int ostatus;
865
866 /* FSM events, stored for debug purposes.
867 * Note: uchar used for reduced memory usage.
868 */
869 unsigned char cur_event;
870 unsigned char last_event;
871 unsigned char last_major_event;
872
873 /* Peer index, used for dumping TABLE_DUMP_V2 format */
874 uint16_t table_dump_index;
875
876 /* Peer information */
877 int fd; /* File descriptor */
878 int ttl; /* TTL of TCP connection to the peer. */
879 int rtt; /* Estimated round-trip-time from TCP_INFO */
880 int gtsm_hops; /* minimum hopcount to peer */
881 char *desc; /* Description of the peer. */
882 unsigned short port; /* Destination port for peer */
883 char *host; /* Printable address of the peer. */
884 union sockunion su; /* Sockunion address of the peer. */
885 #define BGP_PEER_SU_UNSPEC(peer) (peer->su.sa.sa_family == AF_UNSPEC)
886 time_t uptime; /* Last Up/Down time */
887 time_t readtime; /* Last read time */
888 time_t resettime; /* Last reset time */
889
890 char *conf_if; /* neighbor interface config name. */
891 struct interface *ifp; /* corresponding interface */
892 char *ifname; /* bind interface name. */
893 char *update_if;
894 union sockunion *update_source;
895
896 union sockunion *su_local; /* Sockunion of local address. */
897 union sockunion *su_remote; /* Sockunion of remote address. */
898 int shared_network; /* Is this peer shared same network. */
899 struct bgp_nexthop nexthop; /* Nexthop */
900
901 /* Peer address family configuration. */
902 uint8_t afc[AFI_MAX][SAFI_MAX];
903 uint8_t afc_nego[AFI_MAX][SAFI_MAX];
904 uint8_t afc_adv[AFI_MAX][SAFI_MAX];
905 uint8_t afc_recv[AFI_MAX][SAFI_MAX];
906
907 /* Capability flags (reset in bgp_stop) */
908 uint32_t cap;
909 #define PEER_CAP_REFRESH_ADV (1 << 0) /* refresh advertised */
910 #define PEER_CAP_REFRESH_OLD_RCV (1 << 1) /* refresh old received */
911 #define PEER_CAP_REFRESH_NEW_RCV (1 << 2) /* refresh rfc received */
912 #define PEER_CAP_DYNAMIC_ADV (1 << 3) /* dynamic advertised */
913 #define PEER_CAP_DYNAMIC_RCV (1 << 4) /* dynamic received */
914 #define PEER_CAP_RESTART_ADV (1 << 5) /* restart advertised */
915 #define PEER_CAP_RESTART_RCV (1 << 6) /* restart received */
916 #define PEER_CAP_AS4_ADV (1 << 7) /* as4 advertised */
917 #define PEER_CAP_AS4_RCV (1 << 8) /* as4 received */
918 #define PEER_CAP_RESTART_BIT_ADV (1 << 9) /* sent restart state */
919 #define PEER_CAP_RESTART_BIT_RCV (1 << 10) /* peer restart state */
920 #define PEER_CAP_ADDPATH_ADV (1 << 11) /* addpath advertised */
921 #define PEER_CAP_ADDPATH_RCV (1 << 12) /* addpath received */
922 #define PEER_CAP_ENHE_ADV (1 << 13) /* Extended nexthop advertised */
923 #define PEER_CAP_ENHE_RCV (1 << 14) /* Extended nexthop received */
924 #define PEER_CAP_HOSTNAME_ADV (1 << 15) /* hostname advertised */
925 #define PEER_CAP_HOSTNAME_RCV (1 << 16) /* hostname received */
926
927 /* Capability flags (reset in bgp_stop) */
928 uint32_t af_cap[AFI_MAX][SAFI_MAX];
929 #define PEER_CAP_ORF_PREFIX_SM_ADV (1 << 0) /* send-mode advertised */
930 #define PEER_CAP_ORF_PREFIX_RM_ADV (1 << 1) /* receive-mode advertised */
931 #define PEER_CAP_ORF_PREFIX_SM_RCV (1 << 2) /* send-mode received */
932 #define PEER_CAP_ORF_PREFIX_RM_RCV (1 << 3) /* receive-mode received */
933 #define PEER_CAP_ORF_PREFIX_SM_OLD_RCV (1 << 4) /* send-mode received */
934 #define PEER_CAP_ORF_PREFIX_RM_OLD_RCV (1 << 5) /* receive-mode received */
935 #define PEER_CAP_RESTART_AF_RCV (1 << 6) /* graceful restart afi/safi received */
936 #define PEER_CAP_RESTART_AF_PRESERVE_RCV (1 << 7) /* graceful restart afi/safi F-bit received */
937 #define PEER_CAP_ADDPATH_AF_TX_ADV (1 << 8) /* addpath tx advertised */
938 #define PEER_CAP_ADDPATH_AF_TX_RCV (1 << 9) /* addpath tx received */
939 #define PEER_CAP_ADDPATH_AF_RX_ADV (1 << 10) /* addpath rx advertised */
940 #define PEER_CAP_ADDPATH_AF_RX_RCV (1 << 11) /* addpath rx received */
941 #define PEER_CAP_ENHE_AF_ADV (1 << 12) /* Extended nexthopi afi/safi advertised */
942 #define PEER_CAP_ENHE_AF_RCV (1 << 13) /* Extended nexthop afi/safi received */
943 #define PEER_CAP_ENHE_AF_NEGO (1 << 14) /* Extended nexthop afi/safi negotiated */
944
945 /* Global configuration flags. */
946 /*
947 * Parallel array to flags that indicates whether each flag originates
948 * from a peer-group or if it is config that is specific to this
949 * individual peer. If a flag is set independent of the peer-group, the
950 * same bit should be set here. If this peer is a peer-group, this
951 * memory region should be all zeros.
952 *
953 * The assumption is that the default state for all flags is unset,
954 * so if a flag is unset, the corresponding override flag is unset too.
955 * However if a flag is set, the corresponding override flag is set.
956 */
957 uint32_t flags_override;
958 /*
959 * Parallel array to flags that indicates whether the default behavior
960 * of *flags_override* should be inverted. If a flag is unset and the
961 * corresponding invert flag is set, the corresponding override flag
962 * would be set. However if a flag is set and the corresponding invert
963 * flag is unset, the corresponding override flag would be unset.
964 *
965 * This can be used for attributes like *send-community*, which are
966 * implicitely enabled and have to be disabled explicitely, compared to
967 * 'normal' attributes like *next-hop-self* which are implicitely set.
968 *
969 * All operations dealing with flags should apply the following boolean
970 * logic to keep the internal flag system in a sane state:
971 *
972 * value=0 invert=0 Inherit flag if member, otherwise unset flag
973 * value=0 invert=1 Unset flag unconditionally
974 * value=1 invert=0 Set flag unconditionally
975 * value=1 invert=1 Inherit flag if member, otherwise set flag
976 *
977 * Contrary to the implementation of *flags_override*, the flag
978 * inversion state can be set either on the peer OR the peer *and* the
979 * peer-group. This was done on purpose, as the inversion state of a
980 * flag can be determined on either the peer or the peer-group.
981 *
982 * Example: Enabling the cisco configuration mode inverts all flags
983 * related to *send-community* unconditionally for both peer-groups and
984 * peers.
985 *
986 * This behavior is different for interface peers though, which enable
987 * the *extended-nexthop* flag by default, which regular peers do not.
988 * As the peer-group can contain both regular and interface peers, the
989 * flag inversion state must be set on the peer only.
990 *
991 * When a peer inherits the configuration from a peer-group and the
992 * inversion state of the flag differs between peer and peer-group, the
993 * newly set value must equal to the inverted state of the peer-group.
994 */
995 uint32_t flags_invert;
996 /*
997 * Effective array for storing the peer/peer-group flags. In case of a
998 * peer-group, the peer-specific overrides (see flags_override and
999 * flags_invert) must be respected.
1000 */
1001 uint32_t flags;
1002 #define PEER_FLAG_PASSIVE (1 << 0) /* passive mode */
1003 #define PEER_FLAG_SHUTDOWN (1 << 1) /* shutdown */
1004 #define PEER_FLAG_DONT_CAPABILITY (1 << 2) /* dont-capability */
1005 #define PEER_FLAG_OVERRIDE_CAPABILITY (1 << 3) /* override-capability */
1006 #define PEER_FLAG_STRICT_CAP_MATCH (1 << 4) /* strict-match */
1007 #define PEER_FLAG_DYNAMIC_CAPABILITY (1 << 5) /* dynamic capability */
1008 #define PEER_FLAG_DISABLE_CONNECTED_CHECK (1 << 6) /* disable-connected-check */
1009 #define PEER_FLAG_LOCAL_AS_NO_PREPEND (1 << 7) /* local-as no-prepend */
1010 #define PEER_FLAG_LOCAL_AS_REPLACE_AS (1 << 8) /* local-as no-prepend replace-as */
1011 #define PEER_FLAG_DELETE (1 << 9) /* mark the peer for deleting */
1012 #define PEER_FLAG_CONFIG_NODE (1 << 10) /* the node to update configs on */
1013 #define PEER_FLAG_LONESOUL (1 << 11)
1014 #define PEER_FLAG_DYNAMIC_NEIGHBOR (1 << 12) /* dynamic neighbor */
1015 #define PEER_FLAG_CAPABILITY_ENHE (1 << 13) /* Extended next-hop (rfc 5549)*/
1016 #define PEER_FLAG_IFPEER_V6ONLY (1 << 14) /* if-based peer is v6 only */
1017 #define PEER_FLAG_IS_RFAPI_HD (1 << 15) /* attached to rfapi HD */
1018 #define PEER_FLAG_ENFORCE_FIRST_AS (1 << 16) /* enforce-first-as */
1019 #define PEER_FLAG_ROUTEADV (1 << 17) /* route advertise */
1020 #define PEER_FLAG_TIMER (1 << 18) /* keepalive & holdtime */
1021 #define PEER_FLAG_TIMER_CONNECT (1 << 19) /* connect timer */
1022 #define PEER_FLAG_PASSWORD (1 << 20) /* password */
1023 #define PEER_FLAG_LOCAL_AS (1 << 21) /* local-as */
1024 #define PEER_FLAG_UPDATE_SOURCE (1 << 22) /* update-source */
1025
1026 /* BGP-GR Peer related flags */
1027 #define PEER_FLAG_GRACEFUL_RESTART_HELPER (1 << 23) /* Helper */
1028 #define PEER_FLAG_GRACEFUL_RESTART (1 << 24) /* Graceful Restart */
1029 #define PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT (1 << 25) /* Global-Inherit */
1030
1031 /*
1032 *GR-Disabled mode means unset PEER_FLAG_GRACEFUL_RESTART
1033 *& PEER_FLAG_GRACEFUL_RESTART_HELPER
1034 *and PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
1035 */
1036
1037 struct bgp_peer_gr PEER_GR_FSM[PEER_MODE][PEER_EVENT_CMD];
1038 enum peer_mode peer_gr_present_state;
1039 /* Non stop forwarding afi-safi count for BGP gr feature*/
1040 uint8_t nsf_af_count;
1041
1042 uint8_t peer_gr_new_status_flag;
1043 #define PEER_GRACEFUL_RESTART_NEW_STATE_HELPER (1 << 0)
1044 #define PEER_GRACEFUL_RESTART_NEW_STATE_RESTART (1 << 1)
1045 #define PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT (1 << 2)
1046
1047 /* outgoing message sent in CEASE_ADMIN_SHUTDOWN notify */
1048 char *tx_shutdown_message;
1049
1050 /* NSF mode (graceful restart) */
1051 uint8_t nsf[AFI_MAX][SAFI_MAX];
1052
1053 /* Peer Per AF flags */
1054 /*
1055 * Please consult the comments for *flags_override*, *flags_invert* and
1056 * *flags* to understand what these three arrays do. The address-family
1057 * specific attributes are being treated the exact same way as global
1058 * peer attributes.
1059 */
1060 uint32_t af_flags_override[AFI_MAX][SAFI_MAX];
1061 uint32_t af_flags_invert[AFI_MAX][SAFI_MAX];
1062 uint32_t af_flags[AFI_MAX][SAFI_MAX];
1063 #define PEER_FLAG_SEND_COMMUNITY (1 << 0) /* send-community */
1064 #define PEER_FLAG_SEND_EXT_COMMUNITY (1 << 1) /* send-community ext. */
1065 #define PEER_FLAG_NEXTHOP_SELF (1 << 2) /* next-hop-self */
1066 #define PEER_FLAG_REFLECTOR_CLIENT (1 << 3) /* reflector-client */
1067 #define PEER_FLAG_RSERVER_CLIENT (1 << 4) /* route-server-client */
1068 #define PEER_FLAG_SOFT_RECONFIG (1 << 5) /* soft-reconfiguration */
1069 #define PEER_FLAG_AS_PATH_UNCHANGED (1 << 6) /* transparent-as */
1070 #define PEER_FLAG_NEXTHOP_UNCHANGED (1 << 7) /* transparent-next-hop */
1071 #define PEER_FLAG_MED_UNCHANGED (1 << 8) /* transparent-next-hop */
1072 #define PEER_FLAG_DEFAULT_ORIGINATE (1 << 9) /* default-originate */
1073 #define PEER_FLAG_REMOVE_PRIVATE_AS (1 << 10) /* remove-private-as */
1074 #define PEER_FLAG_ALLOWAS_IN (1 << 11) /* set allowas-in */
1075 #define PEER_FLAG_ORF_PREFIX_SM (1 << 12) /* orf capability send-mode */
1076 #define PEER_FLAG_ORF_PREFIX_RM (1 << 13) /* orf capability receive-mode */
1077 #define PEER_FLAG_MAX_PREFIX (1 << 14) /* maximum prefix */
1078 #define PEER_FLAG_MAX_PREFIX_WARNING (1 << 15) /* maximum prefix warning-only */
1079 #define PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED (1 << 16) /* leave link-local nexthop unchanged */
1080 #define PEER_FLAG_FORCE_NEXTHOP_SELF (1 << 17) /* next-hop-self force */
1081 #define PEER_FLAG_REMOVE_PRIVATE_AS_ALL (1 << 18) /* remove-private-as all */
1082 #define PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE (1 << 19) /* remove-private-as replace-as */
1083 #define PEER_FLAG_AS_OVERRIDE (1 << 20) /* as-override */
1084 #define PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE (1 << 21) /* remove-private-as all replace-as */
1085 #define PEER_FLAG_WEIGHT (1 << 24) /* weight */
1086 #define PEER_FLAG_ALLOWAS_IN_ORIGIN (1 << 25) /* allowas-in origin */
1087 #define PEER_FLAG_SEND_LARGE_COMMUNITY (1 << 26) /* Send large Communities */
1088 #define PEER_FLAG_MAX_PREFIX_OUT (1 << 27) /* outgoing maximum prefix */
1089
1090 enum bgp_addpath_strat addpath_type[AFI_MAX][SAFI_MAX];
1091
1092 /* MD5 password */
1093 char *password;
1094
1095 /* default-originate route-map. */
1096 struct {
1097 char *name;
1098 struct route_map *map;
1099 } default_rmap[AFI_MAX][SAFI_MAX];
1100
1101 /* Peer status flags. */
1102 uint16_t sflags;
1103 #define PEER_STATUS_ACCEPT_PEER (1 << 0) /* accept peer */
1104 #define PEER_STATUS_PREFIX_OVERFLOW (1 << 1) /* prefix-overflow */
1105 #define PEER_STATUS_CAPABILITY_OPEN (1 << 2) /* capability open send */
1106 #define PEER_STATUS_HAVE_ACCEPT (1 << 3) /* accept peer's parent */
1107 #define PEER_STATUS_GROUP (1 << 4) /* peer-group conf */
1108 #define PEER_STATUS_NSF_MODE (1 << 5) /* NSF aware peer */
1109 #define PEER_STATUS_NSF_WAIT (1 << 6) /* wait comeback peer */
1110
1111 /* Peer status af flags (reset in bgp_stop) */
1112 uint16_t af_sflags[AFI_MAX][SAFI_MAX];
1113 #define PEER_STATUS_ORF_PREFIX_SEND (1 << 0) /* prefix-list send peer */
1114 #define PEER_STATUS_ORF_WAIT_REFRESH (1 << 1) /* wait refresh received peer */
1115 #define PEER_STATUS_PREFIX_THRESHOLD (1 << 2) /* exceed prefix-threshold */
1116 #define PEER_STATUS_PREFIX_LIMIT (1 << 3) /* exceed prefix-limit */
1117 #define PEER_STATUS_EOR_SEND (1 << 4) /* end-of-rib send to peer */
1118 #define PEER_STATUS_EOR_RECEIVED (1 << 5) /* end-of-rib received from peer */
1119
1120 /* Configured timer values. */
1121 _Atomic uint32_t holdtime;
1122 _Atomic uint32_t keepalive;
1123 _Atomic uint32_t connect;
1124 _Atomic uint32_t routeadv;
1125
1126 /* Timer values. */
1127 _Atomic uint32_t v_start;
1128 _Atomic uint32_t v_connect;
1129 _Atomic uint32_t v_holdtime;
1130 _Atomic uint32_t v_keepalive;
1131 _Atomic uint32_t v_routeadv;
1132 _Atomic uint32_t v_pmax_restart;
1133 _Atomic uint32_t v_gr_restart;
1134
1135 /* Threads. */
1136 struct thread *t_read;
1137 struct thread *t_write;
1138 struct thread *t_start;
1139 struct thread *t_connect_check_r;
1140 struct thread *t_connect_check_w;
1141 struct thread *t_connect;
1142 struct thread *t_holdtime;
1143 struct thread *t_routeadv;
1144 struct thread *t_pmax_restart;
1145 struct thread *t_gr_restart;
1146 struct thread *t_gr_stale;
1147 struct thread *t_generate_updgrp_packets;
1148 struct thread *t_process_packet;
1149
1150 /* Thread flags. */
1151 _Atomic uint32_t thread_flags;
1152 #define PEER_THREAD_WRITES_ON (1 << 0)
1153 #define PEER_THREAD_READS_ON (1 << 1)
1154 #define PEER_THREAD_KEEPALIVES_ON (1 << 2)
1155 /* workqueues */
1156 struct work_queue *clear_node_queue;
1157
1158 #define PEER_TOTAL_RX(peer) \
1159 atomic_load_explicit(&peer->open_in, memory_order_relaxed) \
1160 + atomic_load_explicit(&peer->update_in, memory_order_relaxed) \
1161 + atomic_load_explicit(&peer->notify_in, memory_order_relaxed) \
1162 + atomic_load_explicit(&peer->refresh_in, \
1163 memory_order_relaxed) \
1164 + atomic_load_explicit(&peer->keepalive_in, \
1165 memory_order_relaxed) \
1166 + atomic_load_explicit(&peer->dynamic_cap_in, \
1167 memory_order_relaxed)
1168
1169 #define PEER_TOTAL_TX(peer) \
1170 atomic_load_explicit(&peer->open_out, memory_order_relaxed) \
1171 + atomic_load_explicit(&peer->update_out, \
1172 memory_order_relaxed) \
1173 + atomic_load_explicit(&peer->notify_out, \
1174 memory_order_relaxed) \
1175 + atomic_load_explicit(&peer->refresh_out, \
1176 memory_order_relaxed) \
1177 + atomic_load_explicit(&peer->keepalive_out, \
1178 memory_order_relaxed) \
1179 + atomic_load_explicit(&peer->dynamic_cap_out, \
1180 memory_order_relaxed)
1181
1182 /* Statistics field */
1183 _Atomic uint32_t open_in; /* Open message input count */
1184 _Atomic uint32_t open_out; /* Open message output count */
1185 _Atomic uint32_t update_in; /* Update message input count */
1186 _Atomic uint32_t update_out; /* Update message ouput count */
1187 _Atomic time_t update_time; /* Update message received time. */
1188 _Atomic uint32_t keepalive_in; /* Keepalive input count */
1189 _Atomic uint32_t keepalive_out; /* Keepalive output count */
1190 _Atomic uint32_t notify_in; /* Notify input count */
1191 _Atomic uint32_t notify_out; /* Notify output count */
1192 _Atomic uint32_t refresh_in; /* Route Refresh input count */
1193 _Atomic uint32_t refresh_out; /* Route Refresh output count */
1194 _Atomic uint32_t dynamic_cap_in; /* Dynamic Capability input count. */
1195 _Atomic uint32_t dynamic_cap_out; /* Dynamic Capability output count. */
1196
1197 uint32_t stat_pfx_filter;
1198 uint32_t stat_pfx_aspath_loop;
1199 uint32_t stat_pfx_originator_loop;
1200 uint32_t stat_pfx_cluster_loop;
1201 uint32_t stat_pfx_nh_invalid;
1202 uint32_t stat_pfx_dup_withdraw;
1203 uint32_t stat_upd_7606; /* RFC7606: treat-as-withdraw */
1204
1205 /* BGP state count */
1206 uint32_t established; /* Established */
1207 uint32_t dropped; /* Dropped */
1208
1209 /* Update delay related fields */
1210 uint8_t update_delay_over; /* When this is set, BGP is no more waiting
1211 for EOR */
1212
1213 /* Syncronization list and time. */
1214 struct bgp_synchronize *sync[AFI_MAX][SAFI_MAX];
1215 time_t synctime;
1216 /* timestamp when the last UPDATE msg was written */
1217 _Atomic time_t last_write;
1218 /* timestamp when the last msg was written */
1219 _Atomic time_t last_update;
1220
1221 /* Notify data. */
1222 struct bgp_notify notify;
1223
1224 /* Filter structure. */
1225 struct bgp_filter filter[AFI_MAX][SAFI_MAX];
1226
1227 /*
1228 * Parallel array to filter that indicates whether each filter
1229 * originates from a peer-group or if it is config that is specific to
1230 * this individual peer. If a filter is set independent of the
1231 * peer-group the appropriate bit should be set here. If this peer is a
1232 * peer-group, this memory region should be all zeros. The assumption
1233 * is that the default state for all flags is unset. Due to filters
1234 * having a direction (e.g. in/out/...), this array has a third
1235 * dimension for storing the overrides independently per direction.
1236 *
1237 * Notes:
1238 * - if a filter for an individual peer is unset, the corresponding
1239 * override flag is unset and the peer is considered to be back in
1240 * sync with the peer-group.
1241 * - This does *not* contain the filter values, rather it contains
1242 * whether the filter in filter (struct bgp_filter) is peer-specific.
1243 */
1244 uint8_t filter_override[AFI_MAX][SAFI_MAX][(FILTER_MAX > RMAP_MAX)
1245 ? FILTER_MAX
1246 : RMAP_MAX];
1247 #define PEER_FT_DISTRIBUTE_LIST (1 << 0) /* distribute-list */
1248 #define PEER_FT_FILTER_LIST (1 << 1) /* filter-list */
1249 #define PEER_FT_PREFIX_LIST (1 << 2) /* prefix-list */
1250 #define PEER_FT_ROUTE_MAP (1 << 3) /* route-map */
1251 #define PEER_FT_UNSUPPRESS_MAP (1 << 4) /* unsuppress-map */
1252
1253 /* ORF Prefix-list */
1254 struct prefix_list *orf_plist[AFI_MAX][SAFI_MAX];
1255
1256 /* Text description of last attribute rcvd */
1257 char rcvd_attr_str[BUFSIZ];
1258
1259 /* Track if we printed the attribute in debugs */
1260 int rcvd_attr_printed;
1261
1262 /* Prefix count. */
1263 uint32_t pcount[AFI_MAX][SAFI_MAX];
1264
1265 /* Max prefix count. */
1266 uint32_t pmax[AFI_MAX][SAFI_MAX];
1267 uint8_t pmax_threshold[AFI_MAX][SAFI_MAX];
1268 uint16_t pmax_restart[AFI_MAX][SAFI_MAX];
1269 #define MAXIMUM_PREFIX_THRESHOLD_DEFAULT 75
1270
1271 /* Send prefix count. */
1272 uint32_t pmax_out[AFI_MAX][SAFI_MAX];
1273
1274 /* allowas-in. */
1275 char allowas_in[AFI_MAX][SAFI_MAX];
1276
1277 /* weight */
1278 unsigned long weight[AFI_MAX][SAFI_MAX];
1279
1280 /* peer reset cause */
1281 uint8_t last_reset;
1282 #define PEER_DOWN_RID_CHANGE 1 /* bgp router-id command */
1283 #define PEER_DOWN_REMOTE_AS_CHANGE 2 /* neighbor remote-as command */
1284 #define PEER_DOWN_LOCAL_AS_CHANGE 3 /* neighbor local-as command */
1285 #define PEER_DOWN_CLID_CHANGE 4 /* bgp cluster-id command */
1286 #define PEER_DOWN_CONFED_ID_CHANGE 5 /* bgp confederation identifier command */
1287 #define PEER_DOWN_CONFED_PEER_CHANGE 6 /* bgp confederation peer command */
1288 #define PEER_DOWN_RR_CLIENT_CHANGE 7 /* neighbor route-reflector-client command */
1289 #define PEER_DOWN_RS_CLIENT_CHANGE 8 /* neighbor route-server-client command */
1290 #define PEER_DOWN_UPDATE_SOURCE_CHANGE 9 /* neighbor update-source command */
1291 #define PEER_DOWN_AF_ACTIVATE 10 /* neighbor activate command */
1292 #define PEER_DOWN_USER_SHUTDOWN 11 /* neighbor shutdown command */
1293 #define PEER_DOWN_USER_RESET 12 /* clear ip bgp command */
1294 #define PEER_DOWN_NOTIFY_RECEIVED 13 /* notification received */
1295 #define PEER_DOWN_NOTIFY_SEND 14 /* notification send */
1296 #define PEER_DOWN_CLOSE_SESSION 15 /* tcp session close */
1297 #define PEER_DOWN_NEIGHBOR_DELETE 16 /* neghbor delete */
1298 #define PEER_DOWN_RMAP_BIND 17 /* neghbor peer-group command */
1299 #define PEER_DOWN_RMAP_UNBIND 18 /* no neighbor peer-group command */
1300 #define PEER_DOWN_CAPABILITY_CHANGE 19 /* neighbor capability command */
1301 #define PEER_DOWN_PASSIVE_CHANGE 20 /* neighbor passive command */
1302 #define PEER_DOWN_MULTIHOP_CHANGE 21 /* neighbor multihop command */
1303 #define PEER_DOWN_NSF_CLOSE_SESSION 22 /* NSF tcp session close */
1304 #define PEER_DOWN_V6ONLY_CHANGE 23 /* if-based peering v6only toggled */
1305 #define PEER_DOWN_BFD_DOWN 24 /* BFD down */
1306 #define PEER_DOWN_IF_DOWN 25 /* Interface down */
1307 #define PEER_DOWN_NBR_ADDR_DEL 26 /* Peer address lost */
1308 #define PEER_DOWN_WAITING_NHT 27 /* Waiting for NHT to resolve */
1309 #define PEER_DOWN_NBR_ADDR 28 /* Waiting for peer IPv6 IP Addr */
1310 #define PEER_DOWN_VRF_UNINIT 29 /* Associated VRF is not init yet */
1311 #define PEER_DOWN_NOAFI_ACTIVATED 30 /* No AFI/SAFI activated for peer */
1312 #define PEER_DOWN_AS_SETS_REJECT 31 /* Reject routes with AS_SET */
1313 size_t last_reset_cause_size;
1314 uint8_t last_reset_cause[BGP_MAX_PACKET_SIZE];
1315
1316 /* The kind of route-map Flags.*/
1317 uint16_t rmap_type;
1318 #define PEER_RMAP_TYPE_IN (1 << 0) /* neighbor route-map in */
1319 #define PEER_RMAP_TYPE_OUT (1 << 1) /* neighbor route-map out */
1320 #define PEER_RMAP_TYPE_NETWORK (1 << 2) /* network route-map */
1321 #define PEER_RMAP_TYPE_REDISTRIBUTE (1 << 3) /* redistribute route-map */
1322 #define PEER_RMAP_TYPE_DEFAULT (1 << 4) /* default-originate route-map */
1323 #define PEER_RMAP_TYPE_NOSET (1 << 5) /* not allow to set commands */
1324 #define PEER_RMAP_TYPE_IMPORT (1 << 6) /* neighbor route-map import */
1325 #define PEER_RMAP_TYPE_EXPORT (1 << 7) /* neighbor route-map export */
1326 #define PEER_RMAP_TYPE_AGGREGATE (1 << 8) /* aggregate-address route-map */
1327
1328 /* peer specific BFD information */
1329 struct bfd_info *bfd_info;
1330
1331 /* hostname and domainname advertised by host */
1332 char *hostname;
1333 char *domainname;
1334
1335 /* Sender side AS path loop detection. */
1336 bool as_path_loop_detection;
1337
1338 QOBJ_FIELDS
1339 };
1340 DECLARE_QOBJ_TYPE(peer)
1341
1342 /* Inherit peer attribute from peer-group. */
1343 #define PEER_ATTR_INHERIT(peer, group, attr) \
1344 ((peer)->attr = (group)->conf->attr)
1345 #define PEER_STR_ATTR_INHERIT(peer, group, attr, mt) \
1346 do { \
1347 if ((peer)->attr) \
1348 XFREE(mt, (peer)->attr); \
1349 if ((group)->conf->attr) \
1350 (peer)->attr = XSTRDUP(mt, (group)->conf->attr); \
1351 else \
1352 (peer)->attr = NULL; \
1353 } while (0)
1354 #define PEER_SU_ATTR_INHERIT(peer, group, attr) \
1355 do { \
1356 if ((peer)->attr) \
1357 sockunion_free((peer)->attr); \
1358 if ((group)->conf->attr) \
1359 (peer)->attr = sockunion_dup((group)->conf->attr); \
1360 else \
1361 (peer)->attr = NULL; \
1362 } while (0)
1363
1364 /* Check if suppress start/restart of sessions to peer. */
1365 #define BGP_PEER_START_SUPPRESSED(P) \
1366 (CHECK_FLAG((P)->flags, PEER_FLAG_SHUTDOWN) \
1367 || CHECK_FLAG((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW))
1368
1369 #define PEER_PASSWORD_MINLEN (1)
1370 #define PEER_PASSWORD_MAXLEN (80)
1371
1372 /* This structure's member directly points incoming packet data
1373 stream. */
1374 struct bgp_nlri {
1375 /* AFI. */
1376 uint16_t afi; /* iana_afi_t */
1377
1378 /* SAFI. */
1379 uint8_t safi; /* iana_safi_t */
1380
1381 /* Pointer to NLRI byte stream. */
1382 uint8_t *nlri;
1383
1384 /* Length of whole NLRI. */
1385 bgp_size_t length;
1386 };
1387
1388 /* BGP versions. */
1389 #define BGP_VERSION_4 4
1390
1391 /* Default BGP port number. */
1392 #define BGP_PORT_DEFAULT 179
1393
1394 /* BGP minimum message size. */
1395 #define BGP_MSG_OPEN_MIN_SIZE (BGP_HEADER_SIZE + 10)
1396 #define BGP_MSG_UPDATE_MIN_SIZE (BGP_HEADER_SIZE + 4)
1397 #define BGP_MSG_NOTIFY_MIN_SIZE (BGP_HEADER_SIZE + 2)
1398 #define BGP_MSG_KEEPALIVE_MIN_SIZE (BGP_HEADER_SIZE + 0)
1399 #define BGP_MSG_ROUTE_REFRESH_MIN_SIZE (BGP_HEADER_SIZE + 4)
1400 #define BGP_MSG_CAPABILITY_MIN_SIZE (BGP_HEADER_SIZE + 3)
1401
1402 /* BGP message types. */
1403 #define BGP_MSG_OPEN 1
1404 #define BGP_MSG_UPDATE 2
1405 #define BGP_MSG_NOTIFY 3
1406 #define BGP_MSG_KEEPALIVE 4
1407 #define BGP_MSG_ROUTE_REFRESH_NEW 5
1408 #define BGP_MSG_CAPABILITY 6
1409 #define BGP_MSG_ROUTE_REFRESH_OLD 128
1410
1411 /* BGP open optional parameter. */
1412 #define BGP_OPEN_OPT_AUTH 1
1413 #define BGP_OPEN_OPT_CAP 2
1414
1415 /* BGP4 attribute type codes. */
1416 #define BGP_ATTR_ORIGIN 1
1417 #define BGP_ATTR_AS_PATH 2
1418 #define BGP_ATTR_NEXT_HOP 3
1419 #define BGP_ATTR_MULTI_EXIT_DISC 4
1420 #define BGP_ATTR_LOCAL_PREF 5
1421 #define BGP_ATTR_ATOMIC_AGGREGATE 6
1422 #define BGP_ATTR_AGGREGATOR 7
1423 #define BGP_ATTR_COMMUNITIES 8
1424 #define BGP_ATTR_ORIGINATOR_ID 9
1425 #define BGP_ATTR_CLUSTER_LIST 10
1426 #define BGP_ATTR_DPA 11
1427 #define BGP_ATTR_ADVERTISER 12
1428 #define BGP_ATTR_RCID_PATH 13
1429 #define BGP_ATTR_MP_REACH_NLRI 14
1430 #define BGP_ATTR_MP_UNREACH_NLRI 15
1431 #define BGP_ATTR_EXT_COMMUNITIES 16
1432 #define BGP_ATTR_AS4_PATH 17
1433 #define BGP_ATTR_AS4_AGGREGATOR 18
1434 #define BGP_ATTR_AS_PATHLIMIT 21
1435 #define BGP_ATTR_PMSI_TUNNEL 22
1436 #define BGP_ATTR_ENCAP 23
1437 #define BGP_ATTR_LARGE_COMMUNITIES 32
1438 #define BGP_ATTR_PREFIX_SID 40
1439 #if ENABLE_BGP_VNC_ATTR
1440 #define BGP_ATTR_VNC 255
1441 #endif
1442
1443 /* BGP update origin. */
1444 #define BGP_ORIGIN_IGP 0
1445 #define BGP_ORIGIN_EGP 1
1446 #define BGP_ORIGIN_INCOMPLETE 2
1447
1448 /* BGP notify message codes. */
1449 #define BGP_NOTIFY_HEADER_ERR 1
1450 #define BGP_NOTIFY_OPEN_ERR 2
1451 #define BGP_NOTIFY_UPDATE_ERR 3
1452 #define BGP_NOTIFY_HOLD_ERR 4
1453 #define BGP_NOTIFY_FSM_ERR 5
1454 #define BGP_NOTIFY_CEASE 6
1455 #define BGP_NOTIFY_CAPABILITY_ERR 7
1456
1457 #define BGP_NOTIFY_SUBCODE_UNSPECIFIC 0
1458
1459 /* BGP_NOTIFY_HEADER_ERR sub codes. */
1460 #define BGP_NOTIFY_HEADER_NOT_SYNC 1
1461 #define BGP_NOTIFY_HEADER_BAD_MESLEN 2
1462 #define BGP_NOTIFY_HEADER_BAD_MESTYPE 3
1463
1464 /* BGP_NOTIFY_OPEN_ERR sub codes. */
1465 #define BGP_NOTIFY_OPEN_MALFORMED_ATTR 0
1466 #define BGP_NOTIFY_OPEN_UNSUP_VERSION 1
1467 #define BGP_NOTIFY_OPEN_BAD_PEER_AS 2
1468 #define BGP_NOTIFY_OPEN_BAD_BGP_IDENT 3
1469 #define BGP_NOTIFY_OPEN_UNSUP_PARAM 4
1470 #define BGP_NOTIFY_OPEN_AUTH_FAILURE 5
1471 #define BGP_NOTIFY_OPEN_UNACEP_HOLDTIME 6
1472 #define BGP_NOTIFY_OPEN_UNSUP_CAPBL 7
1473
1474 /* BGP_NOTIFY_UPDATE_ERR sub codes. */
1475 #define BGP_NOTIFY_UPDATE_MAL_ATTR 1
1476 #define BGP_NOTIFY_UPDATE_UNREC_ATTR 2
1477 #define BGP_NOTIFY_UPDATE_MISS_ATTR 3
1478 #define BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR 4
1479 #define BGP_NOTIFY_UPDATE_ATTR_LENG_ERR 5
1480 #define BGP_NOTIFY_UPDATE_INVAL_ORIGIN 6
1481 #define BGP_NOTIFY_UPDATE_AS_ROUTE_LOOP 7
1482 #define BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP 8
1483 #define BGP_NOTIFY_UPDATE_OPT_ATTR_ERR 9
1484 #define BGP_NOTIFY_UPDATE_INVAL_NETWORK 10
1485 #define BGP_NOTIFY_UPDATE_MAL_AS_PATH 11
1486
1487 /* BGP_NOTIFY_CEASE sub codes (RFC 4486). */
1488 #define BGP_NOTIFY_CEASE_MAX_PREFIX 1
1489 #define BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN 2
1490 #define BGP_NOTIFY_CEASE_PEER_UNCONFIG 3
1491 #define BGP_NOTIFY_CEASE_ADMIN_RESET 4
1492 #define BGP_NOTIFY_CEASE_CONNECT_REJECT 5
1493 #define BGP_NOTIFY_CEASE_CONFIG_CHANGE 6
1494 #define BGP_NOTIFY_CEASE_COLLISION_RESOLUTION 7
1495 #define BGP_NOTIFY_CEASE_OUT_OF_RESOURCE 8
1496
1497 /* BGP_NOTIFY_CAPABILITY_ERR sub codes (draft-ietf-idr-dynamic-cap-02). */
1498 #define BGP_NOTIFY_CAPABILITY_INVALID_ACTION 1
1499 #define BGP_NOTIFY_CAPABILITY_INVALID_LENGTH 2
1500 #define BGP_NOTIFY_CAPABILITY_MALFORMED_CODE 3
1501
1502 /* BGP finite state machine status. */
1503 #define Idle 1
1504 #define Connect 2
1505 #define Active 3
1506 #define OpenSent 4
1507 #define OpenConfirm 5
1508 #define Established 6
1509 #define Clearing 7
1510 #define Deleted 8
1511 #define BGP_STATUS_MAX 9
1512
1513 /* BGP finite state machine events. */
1514 #define BGP_Start 1
1515 #define BGP_Stop 2
1516 #define TCP_connection_open 3
1517 #define TCP_connection_closed 4
1518 #define TCP_connection_open_failed 5
1519 #define TCP_fatal_error 6
1520 #define ConnectRetry_timer_expired 7
1521 #define Hold_Timer_expired 8
1522 #define KeepAlive_timer_expired 9
1523 #define Receive_OPEN_message 10
1524 #define Receive_KEEPALIVE_message 11
1525 #define Receive_UPDATE_message 12
1526 #define Receive_NOTIFICATION_message 13
1527 #define Clearing_Completed 14
1528 #define BGP_EVENTS_MAX 15
1529
1530 /* BGP timers default value. */
1531 #define BGP_INIT_START_TIMER 1
1532 /* The following 3 are RFC defaults that are overridden in bgp_vty.c with
1533 * version-/profile-specific values. The values here do not matter, they only
1534 * exist to provide a clear layering separation between core and CLI.
1535 */
1536 #define BGP_DEFAULT_HOLDTIME 180
1537 #define BGP_DEFAULT_KEEPALIVE 60
1538 #define BGP_DEFAULT_CONNECT_RETRY 120
1539
1540 #define BGP_DEFAULT_EBGP_ROUTEADV 0
1541 #define BGP_DEFAULT_IBGP_ROUTEADV 0
1542
1543 /* BGP default local preference. */
1544 #define BGP_DEFAULT_LOCAL_PREF 100
1545
1546 /* BGP local-preference to send when 'bgp graceful-shutdown'
1547 * is configured */
1548 #define BGP_GSHUT_LOCAL_PREF 0
1549
1550 /* BGP default subgroup packet queue max . */
1551 #define BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX 40
1552
1553 /* BGP graceful restart */
1554 #define BGP_DEFAULT_RESTART_TIME 120
1555 #define BGP_DEFAULT_STALEPATH_TIME 360
1556
1557 /* BGP uptime string length. */
1558 #define BGP_UPTIME_LEN 25
1559
1560 /* Default configuration settings for bgpd. */
1561 #define BGP_VTY_PORT 2605
1562 #define BGP_DEFAULT_CONFIG "bgpd.conf"
1563
1564 /* BGP Dynamic Neighbors feature */
1565 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT 100
1566 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_MIN 1
1567 #define BGP_DYNAMIC_NEIGHBORS_LIMIT_MAX 5000
1568
1569 /* Flag for peer_clear_soft(). */
1570 enum bgp_clear_type {
1571 BGP_CLEAR_SOFT_NONE,
1572 BGP_CLEAR_SOFT_OUT,
1573 BGP_CLEAR_SOFT_IN,
1574 BGP_CLEAR_SOFT_BOTH,
1575 BGP_CLEAR_SOFT_IN_ORF_PREFIX
1576 };
1577
1578 /* Macros. */
1579 #define BGP_INPUT(P) ((P)->curr)
1580 #define BGP_INPUT_PNT(P) (stream_pnt(BGP_INPUT(P)))
1581 #define BGP_IS_VALID_STATE_FOR_NOTIF(S) \
1582 (((S) == OpenSent) || ((S) == OpenConfirm) || ((S) == Established))
1583
1584 /* BGP error codes. */
1585 #define BGP_SUCCESS 0
1586 #define BGP_CREATED 1
1587 #define BGP_ERR_INVALID_VALUE -1
1588 #define BGP_ERR_INVALID_FLAG -2
1589 #define BGP_ERR_INVALID_AS -3
1590 #define BGP_ERR_INVALID_BGP -4
1591 #define BGP_ERR_PEER_GROUP_MEMBER -5
1592 #define BGP_ERR_PEER_GROUP_NO_REMOTE_AS -7
1593 #define BGP_ERR_PEER_GROUP_CANT_CHANGE -8
1594 #define BGP_ERR_PEER_GROUP_MISMATCH -9
1595 #define BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT -10
1596 #define BGP_ERR_AS_MISMATCH -12
1597 #define BGP_ERR_PEER_FLAG_CONFLICT -13
1598 #define BGP_ERR_PEER_GROUP_SHUTDOWN -14
1599 #define BGP_ERR_PEER_FILTER_CONFLICT -15
1600 #define BGP_ERR_NOT_INTERNAL_PEER -16
1601 #define BGP_ERR_REMOVE_PRIVATE_AS -17
1602 #define BGP_ERR_AF_UNCONFIGURED -18
1603 #define BGP_ERR_SOFT_RECONFIG_UNCONFIGURED -19
1604 #define BGP_ERR_INSTANCE_MISMATCH -20
1605 #define BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP -21
1606 #define BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS -22
1607 #define BGP_ERR_TCPSIG_FAILED -23
1608 #define BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK -24
1609 #define BGP_ERR_NO_IBGP_WITH_TTLHACK -25
1610 #define BGP_ERR_NO_INTERFACE_CONFIG -26
1611 #define BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS -27
1612 #define BGP_ERR_AS_OVERRIDE -28
1613 #define BGP_ERR_INVALID_DYNAMIC_NEIGHBORS_LIMIT -29
1614 #define BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_EXISTS -30
1615 #define BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND -31
1616 #define BGP_ERR_INVALID_FOR_DYNAMIC_PEER -32
1617 #define BGP_ERR_MAX -33
1618 #define BGP_ERR_INVALID_FOR_DIRECT_PEER -34
1619 #define BGP_ERR_PEER_SAFI_CONFLICT -35
1620
1621 /* BGP GR ERRORS */
1622 #define BGP_ERR_GR_INVALID_CMD -36
1623 #define BGP_ERR_GR_OPERATION_FAILED -37
1624 #define BGP_GR_NO_OPERATION -38
1625
1626 /*
1627 * Enumeration of different policy kinds a peer can be configured with.
1628 */
1629 typedef enum {
1630 BGP_POLICY_ROUTE_MAP,
1631 BGP_POLICY_FILTER_LIST,
1632 BGP_POLICY_PREFIX_LIST,
1633 BGP_POLICY_DISTRIBUTE_LIST,
1634 } bgp_policy_type_e;
1635
1636 /* peer_flag_change_type. */
1637 enum peer_change_type {
1638 peer_change_none,
1639 peer_change_reset,
1640 peer_change_reset_in,
1641 peer_change_reset_out,
1642 };
1643
1644 extern struct bgp_master *bm;
1645 extern unsigned int multipath_num;
1646
1647 /* Prototypes. */
1648 extern void bgp_terminate(void);
1649 extern void bgp_reset(void);
1650 extern time_t bgp_clock(void);
1651 extern void bgp_zclient_reset(void);
1652 extern struct bgp *bgp_get_default(void);
1653 extern struct bgp *bgp_lookup(as_t, const char *);
1654 extern struct bgp *bgp_lookup_by_name(const char *);
1655 extern struct bgp *bgp_lookup_by_vrf_id(vrf_id_t);
1656 extern struct bgp *bgp_get_evpn(void);
1657 extern void bgp_set_evpn(struct bgp *bgp);
1658 extern struct peer *peer_lookup(struct bgp *, union sockunion *);
1659 extern struct peer *peer_lookup_by_conf_if(struct bgp *, const char *);
1660 extern struct peer *peer_lookup_by_hostname(struct bgp *, const char *);
1661 extern void bgp_peer_conf_if_to_su_update(struct peer *);
1662 extern int peer_group_listen_range_del(struct peer_group *, struct prefix *);
1663 extern struct peer_group *peer_group_lookup(struct bgp *, const char *);
1664 extern struct peer_group *peer_group_get(struct bgp *, const char *);
1665 extern struct peer *peer_create_bind_dynamic_neighbor(struct bgp *,
1666 union sockunion *,
1667 struct peer_group *);
1668 extern struct prefix *
1669 peer_group_lookup_dynamic_neighbor_range(struct peer_group *, struct prefix *);
1670 extern struct peer_group *peer_group_lookup_dynamic_neighbor(struct bgp *,
1671 struct prefix *,
1672 struct prefix **);
1673 extern struct peer *peer_lookup_dynamic_neighbor(struct bgp *,
1674 union sockunion *);
1675
1676 /*
1677 * Peers are incredibly easy to memory leak
1678 * due to the various ways that they are actually used
1679 * Provide some functionality to debug locks and unlocks
1680 */
1681 extern struct peer *peer_lock_with_caller(const char *, struct peer *);
1682 extern struct peer *peer_unlock_with_caller(const char *, struct peer *);
1683 #define peer_unlock(A) peer_unlock_with_caller(__FUNCTION__, (A))
1684 #define peer_lock(B) peer_lock_with_caller(__FUNCTION__, (B))
1685
1686 extern bgp_peer_sort_t peer_sort(struct peer *peer);
1687 extern int peer_active(struct peer *);
1688 extern int peer_active_nego(struct peer *);
1689 extern void bgp_recalculate_all_bestpaths(struct bgp *bgp);
1690 extern struct peer *peer_create(union sockunion *, const char *, struct bgp *,
1691 as_t, as_t, int, afi_t, safi_t,
1692 struct peer_group *);
1693 extern struct peer *peer_create_accept(struct bgp *);
1694 extern void peer_xfer_config(struct peer *dst, struct peer *src);
1695 extern char *peer_uptime(time_t uptime2, char *buf, size_t len, bool use_json,
1696 json_object *json);
1697
1698 extern int bgp_config_write(struct vty *);
1699
1700 extern void bgp_master_init(struct thread_master *master,
1701 const int buffer_size);
1702
1703 extern void bgp_init(unsigned short instance);
1704 extern void bgp_pthreads_run(void);
1705 extern void bgp_pthreads_finish(void);
1706 extern void bgp_route_map_init(void);
1707 extern void bgp_session_reset(struct peer *);
1708
1709 extern int bgp_option_set(int);
1710 extern int bgp_option_unset(int);
1711 extern int bgp_option_check(int);
1712
1713 extern int bgp_get(struct bgp **, as_t *, const char *, enum bgp_instance_type);
1714 extern void bgp_instance_up(struct bgp *);
1715 extern void bgp_instance_down(struct bgp *);
1716 extern int bgp_delete(struct bgp *);
1717
1718 extern int bgp_handle_socket(struct bgp *bgp, struct vrf *vrf,
1719 vrf_id_t old_vrf_id, bool create);
1720
1721 extern int bgp_flag_set(struct bgp *, int);
1722 extern int bgp_flag_unset(struct bgp *, int);
1723 extern int bgp_flag_check(struct bgp *, int);
1724
1725 extern void bgp_router_id_zebra_bump(vrf_id_t, const struct prefix *);
1726 extern int bgp_router_id_static_set(struct bgp *, struct in_addr);
1727
1728 extern int bgp_cluster_id_set(struct bgp *, struct in_addr *);
1729 extern int bgp_cluster_id_unset(struct bgp *);
1730
1731 extern int bgp_confederation_id_set(struct bgp *, as_t);
1732 extern int bgp_confederation_id_unset(struct bgp *);
1733 extern int bgp_confederation_peers_check(struct bgp *, as_t);
1734
1735 extern int bgp_confederation_peers_add(struct bgp *, as_t);
1736 extern int bgp_confederation_peers_remove(struct bgp *, as_t);
1737
1738 extern int bgp_timers_set(struct bgp *, uint32_t keepalive, uint32_t holdtime,
1739 uint32_t connect_retry);
1740 extern int bgp_timers_unset(struct bgp *);
1741
1742 extern int bgp_default_local_preference_set(struct bgp *, uint32_t);
1743 extern int bgp_default_local_preference_unset(struct bgp *);
1744
1745 extern int bgp_default_subgroup_pkt_queue_max_set(struct bgp *bgp, uint32_t);
1746 extern int bgp_default_subgroup_pkt_queue_max_unset(struct bgp *bgp);
1747
1748 extern int bgp_listen_limit_set(struct bgp *, int);
1749 extern int bgp_listen_limit_unset(struct bgp *);
1750
1751 extern int bgp_update_delay_active(struct bgp *);
1752 extern int bgp_update_delay_configured(struct bgp *);
1753 extern int bgp_afi_safi_peer_exists(struct bgp *bgp, afi_t afi, safi_t safi);
1754 extern void peer_as_change(struct peer *, as_t, int);
1755 extern int peer_remote_as(struct bgp *, union sockunion *, const char *, as_t *,
1756 int, afi_t, safi_t);
1757 extern int peer_group_remote_as(struct bgp *, const char *, as_t *, int);
1758 extern int peer_delete(struct peer *peer);
1759 extern int peer_notify_unconfig(struct peer *peer);
1760 extern int peer_group_delete(struct peer_group *);
1761 extern int peer_group_remote_as_delete(struct peer_group *);
1762 extern int peer_group_listen_range_add(struct peer_group *, struct prefix *);
1763 extern int peer_group_notify_unconfig(struct peer_group *group);
1764
1765 extern int peer_activate(struct peer *, afi_t, safi_t);
1766 extern int peer_deactivate(struct peer *, afi_t, safi_t);
1767 extern int peer_afc_set(struct peer *, afi_t, safi_t, int);
1768
1769 extern int peer_group_bind(struct bgp *, union sockunion *, struct peer *,
1770 struct peer_group *, as_t *);
1771
1772 extern int peer_flag_set(struct peer *, uint32_t);
1773 extern int peer_flag_unset(struct peer *, uint32_t);
1774 extern void peer_flag_inherit(struct peer *peer, uint32_t flag);
1775
1776 extern int peer_af_flag_set(struct peer *, afi_t, safi_t, uint32_t);
1777 extern int peer_af_flag_unset(struct peer *, afi_t, safi_t, uint32_t);
1778 extern int peer_af_flag_check(struct peer *, afi_t, safi_t, uint32_t);
1779 extern void peer_af_flag_inherit(struct peer *peer, afi_t afi, safi_t safi,
1780 uint32_t flag);
1781 extern void peer_change_action(struct peer *peer, afi_t afi, safi_t safi,
1782 enum peer_change_type type);
1783
1784 extern int peer_ebgp_multihop_set(struct peer *, int);
1785 extern int peer_ebgp_multihop_unset(struct peer *);
1786 extern int is_ebgp_multihop_configured(struct peer *peer);
1787
1788 extern int peer_description_set(struct peer *, const char *);
1789 extern int peer_description_unset(struct peer *);
1790
1791 extern int peer_update_source_if_set(struct peer *, const char *);
1792 extern int peer_update_source_addr_set(struct peer *, const union sockunion *);
1793 extern int peer_update_source_unset(struct peer *);
1794
1795 extern int peer_default_originate_set(struct peer *peer, afi_t afi, safi_t safi,
1796 const char *rmap,
1797 struct route_map *route_map);
1798 extern int peer_default_originate_unset(struct peer *, afi_t, safi_t);
1799
1800 extern int peer_port_set(struct peer *, uint16_t);
1801 extern int peer_port_unset(struct peer *);
1802
1803 extern int peer_weight_set(struct peer *, afi_t, safi_t, uint16_t);
1804 extern int peer_weight_unset(struct peer *, afi_t, safi_t);
1805
1806 extern int peer_timers_set(struct peer *, uint32_t keepalive,
1807 uint32_t holdtime);
1808 extern int peer_timers_unset(struct peer *);
1809
1810 extern int peer_timers_connect_set(struct peer *, uint32_t);
1811 extern int peer_timers_connect_unset(struct peer *);
1812
1813 extern int peer_advertise_interval_set(struct peer *, uint32_t);
1814 extern int peer_advertise_interval_unset(struct peer *);
1815
1816 extern void peer_interface_set(struct peer *, const char *);
1817 extern void peer_interface_unset(struct peer *);
1818
1819 extern int peer_distribute_set(struct peer *, afi_t, safi_t, int, const char *);
1820 extern int peer_distribute_unset(struct peer *, afi_t, safi_t, int);
1821
1822 extern int peer_allowas_in_set(struct peer *, afi_t, safi_t, int, int);
1823 extern int peer_allowas_in_unset(struct peer *, afi_t, safi_t);
1824
1825 extern int peer_local_as_set(struct peer *, as_t, int, int);
1826 extern int peer_local_as_unset(struct peer *);
1827
1828 extern int peer_prefix_list_set(struct peer *, afi_t, safi_t, int,
1829 const char *);
1830 extern int peer_prefix_list_unset(struct peer *, afi_t, safi_t, int);
1831
1832 extern int peer_aslist_set(struct peer *, afi_t, safi_t, int, const char *);
1833 extern int peer_aslist_unset(struct peer *, afi_t, safi_t, int);
1834
1835 extern int peer_route_map_set(struct peer *peer, afi_t afi, safi_t safi, int,
1836 const char *name, struct route_map *route_map);
1837 extern int peer_route_map_unset(struct peer *, afi_t, safi_t, int);
1838
1839 extern int peer_unsuppress_map_set(struct peer *peer, afi_t afi, safi_t safi,
1840 const char *name,
1841 struct route_map *route_map);
1842
1843 extern int peer_password_set(struct peer *, const char *);
1844 extern int peer_password_unset(struct peer *);
1845
1846 extern int peer_unsuppress_map_unset(struct peer *, afi_t, safi_t);
1847
1848 extern int peer_maximum_prefix_set(struct peer *, afi_t, safi_t, uint32_t,
1849 uint8_t, int, uint16_t);
1850 extern int peer_maximum_prefix_unset(struct peer *, afi_t, safi_t);
1851
1852 extern int peer_clear(struct peer *, struct listnode **);
1853 extern int peer_clear_soft(struct peer *, afi_t, safi_t, enum bgp_clear_type);
1854
1855 extern int peer_ttl_security_hops_set(struct peer *, int);
1856 extern int peer_ttl_security_hops_unset(struct peer *);
1857
1858 extern int peer_tx_shutdown_message_set(struct peer *, const char *msg);
1859 extern int peer_tx_shutdown_message_unset(struct peer *);
1860
1861 extern int bgp_route_map_update_timer(struct thread *thread);
1862 extern void bgp_route_map_terminate(void);
1863
1864 extern int peer_cmp(struct peer *p1, struct peer *p2);
1865
1866 extern int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi, iana_safi_t pkt_safi,
1867 afi_t *afi, safi_t *safi);
1868 extern int bgp_map_afi_safi_int2iana(afi_t afi, safi_t safi,
1869 iana_afi_t *pkt_afi,
1870 iana_safi_t *pkt_safi);
1871
1872 extern struct peer_af *peer_af_create(struct peer *, afi_t, safi_t);
1873 extern struct peer_af *peer_af_find(struct peer *, afi_t, safi_t);
1874 extern int peer_af_delete(struct peer *, afi_t, safi_t);
1875
1876 extern void bgp_close(void);
1877 extern void bgp_free(struct bgp *);
1878 void bgp_gr_apply_running_config(void);
1879
1880 /* BGP GR */
1881 void bgp_peer_flag_set(struct peer *peer, int flag_bit);
1882 void bgp_peer_flag_unset(struct peer *peer, int flag_bit);
1883 int bgp_peer_flag_check(struct peer *peer, int flag_bit);
1884 int bgp_global_gr_init(struct bgp *bgp);
1885 int bgp_peer_gr_init(struct peer *peer);
1886
1887 static inline struct bgp *bgp_lock(struct bgp *bgp)
1888 {
1889 bgp->lock++;
1890 return bgp;
1891 }
1892
1893 static inline void bgp_unlock(struct bgp *bgp)
1894 {
1895 assert(bgp->lock > 0);
1896 if (--bgp->lock == 0)
1897 bgp_free(bgp);
1898 }
1899
1900 static inline int afindex(afi_t afi, safi_t safi)
1901 {
1902 switch (afi) {
1903 case AFI_IP:
1904 switch (safi) {
1905 case SAFI_UNICAST:
1906 return BGP_AF_IPV4_UNICAST;
1907 break;
1908 case SAFI_MULTICAST:
1909 return BGP_AF_IPV4_MULTICAST;
1910 break;
1911 case SAFI_LABELED_UNICAST:
1912 return BGP_AF_IPV4_LBL_UNICAST;
1913 break;
1914 case SAFI_MPLS_VPN:
1915 return BGP_AF_IPV4_VPN;
1916 break;
1917 case SAFI_ENCAP:
1918 return BGP_AF_IPV4_ENCAP;
1919 break;
1920 case SAFI_FLOWSPEC:
1921 return BGP_AF_IPV4_FLOWSPEC;
1922 default:
1923 return BGP_AF_MAX;
1924 break;
1925 }
1926 break;
1927 case AFI_IP6:
1928 switch (safi) {
1929 case SAFI_UNICAST:
1930 return BGP_AF_IPV6_UNICAST;
1931 break;
1932 case SAFI_MULTICAST:
1933 return BGP_AF_IPV6_MULTICAST;
1934 break;
1935 case SAFI_LABELED_UNICAST:
1936 return BGP_AF_IPV6_LBL_UNICAST;
1937 break;
1938 case SAFI_MPLS_VPN:
1939 return BGP_AF_IPV6_VPN;
1940 break;
1941 case SAFI_ENCAP:
1942 return BGP_AF_IPV6_ENCAP;
1943 break;
1944 case SAFI_FLOWSPEC:
1945 return BGP_AF_IPV6_FLOWSPEC;
1946 default:
1947 return BGP_AF_MAX;
1948 break;
1949 }
1950 break;
1951 case AFI_L2VPN:
1952 switch (safi) {
1953 case SAFI_EVPN:
1954 return BGP_AF_L2VPN_EVPN;
1955 break;
1956 default:
1957 return BGP_AF_MAX;
1958 break;
1959 }
1960 default:
1961 return BGP_AF_MAX;
1962 break;
1963 }
1964 }
1965
1966 /* If the peer is not a peer-group but is bound to a peer-group return 1 */
1967 static inline int peer_group_active(struct peer *peer)
1968 {
1969 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP) && peer->group)
1970 return 1;
1971 return 0;
1972 }
1973
1974 /* If peer is negotiated at least one address family return 1. */
1975 static inline int peer_afi_active_nego(const struct peer *peer, afi_t afi)
1976 {
1977 if (peer->afc_nego[afi][SAFI_UNICAST]
1978 || peer->afc_nego[afi][SAFI_MULTICAST]
1979 || peer->afc_nego[afi][SAFI_LABELED_UNICAST]
1980 || peer->afc_nego[afi][SAFI_MPLS_VPN]
1981 || peer->afc_nego[afi][SAFI_ENCAP]
1982 || peer->afc_nego[afi][SAFI_FLOWSPEC]
1983 || peer->afc_nego[afi][SAFI_EVPN])
1984 return 1;
1985 return 0;
1986 }
1987
1988 /* If at least one address family activated for group, return 1. */
1989 static inline int peer_group_af_configured(struct peer_group *group)
1990 {
1991 struct peer *peer = group->conf;
1992
1993 if (peer->afc[AFI_IP][SAFI_UNICAST] || peer->afc[AFI_IP][SAFI_MULTICAST]
1994 || peer->afc[AFI_IP][SAFI_LABELED_UNICAST]
1995 || peer->afc[AFI_IP][SAFI_FLOWSPEC]
1996 || peer->afc[AFI_IP][SAFI_MPLS_VPN] || peer->afc[AFI_IP][SAFI_ENCAP]
1997 || peer->afc[AFI_IP6][SAFI_UNICAST]
1998 || peer->afc[AFI_IP6][SAFI_MULTICAST]
1999 || peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]
2000 || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
2001 || peer->afc[AFI_IP6][SAFI_ENCAP]
2002 || peer->afc[AFI_IP6][SAFI_FLOWSPEC]
2003 || peer->afc[AFI_L2VPN][SAFI_EVPN])
2004 return 1;
2005 return 0;
2006 }
2007
2008 static inline char *timestamp_string(time_t ts)
2009 {
2010 time_t tbuf;
2011 tbuf = time(NULL) - (bgp_clock() - ts);
2012 return ctime(&tbuf);
2013 }
2014
2015 static inline int peer_established(struct peer *peer)
2016 {
2017 if (peer->status == Established)
2018 return 1;
2019 return 0;
2020 }
2021
2022 static inline int peer_dynamic_neighbor(struct peer *peer)
2023 {
2024 return (CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR)) ? 1 : 0;
2025 }
2026
2027 static inline int peer_cap_enhe(struct peer *peer, afi_t afi, safi_t safi)
2028 {
2029 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ENHE_AF_NEGO));
2030 }
2031
2032 /* Lookup VRF for BGP instance based on its type. */
2033 static inline struct vrf *bgp_vrf_lookup_by_instance_type(struct bgp *bgp)
2034 {
2035 struct vrf *vrf;
2036
2037 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
2038 vrf = vrf_lookup_by_id(VRF_DEFAULT);
2039 else if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
2040 vrf = vrf_lookup_by_name(bgp->name);
2041 else
2042 vrf = NULL;
2043
2044 return vrf;
2045 }
2046
2047 /* Link BGP instance to VRF. */
2048 static inline void bgp_vrf_link(struct bgp *bgp, struct vrf *vrf)
2049 {
2050 bgp->vrf_id = vrf->vrf_id;
2051 if (vrf->info != (void *)bgp)
2052 vrf->info = (void *)bgp_lock(bgp);
2053 }
2054
2055 /* Unlink BGP instance from VRF. */
2056 static inline void bgp_vrf_unlink(struct bgp *bgp, struct vrf *vrf)
2057 {
2058 if (vrf->info == (void *)bgp) {
2059 vrf->info = NULL;
2060 bgp_unlock(bgp);
2061 }
2062 bgp->vrf_id = VRF_UNKNOWN;
2063 }
2064
2065 extern void bgp_unset_redist_vrf_bitmaps(struct bgp *, vrf_id_t);
2066
2067 /* For benefit of rfapi */
2068 extern struct peer *peer_new(struct bgp *bgp);
2069
2070 extern struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
2071 const char *ip_str, bool use_json);
2072
2073 /* Hooks */
2074 DECLARE_HOOK(peer_status_changed, (struct peer * peer), (peer))
2075
2076 #endif /* _QUAGGA_BGPD_H */