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