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