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