2 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
21 #ifndef _QUAGGA_BGP_ATTR_H
22 #define _QUAGGA_BGP_ATTR_H
25 #include "bgp_attr_evpn.h"
26 #include "bgpd/bgp_encap_types.h"
28 /* Simple bit mapping. */
31 #define SET_BITMAP(MAP, NUM) \
32 SET_FLAG(MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
34 #define CHECK_BITMAP(MAP, NUM) \
35 CHECK_FLAG(MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
37 #define BGP_MED_MAX UINT32_MAX
39 /* BGP Attribute type range. */
40 #define BGP_ATTR_TYPE_RANGE 256
41 #define BGP_ATTR_BITMAP_SIZE (BGP_ATTR_TYPE_RANGE / BITMAP_NBBY)
43 /* BGP Attribute flags. */
44 #define BGP_ATTR_FLAG_OPTIONAL 0x80 /* Attribute is optional. */
45 #define BGP_ATTR_FLAG_TRANS 0x40 /* Attribute is transitive. */
46 #define BGP_ATTR_FLAG_PARTIAL 0x20 /* Attribute is partial. */
47 #define BGP_ATTR_FLAG_EXTLEN 0x10 /* Extended length flag. */
49 /* BGP attribute header must bigger than 2. */
50 #define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */
51 #define BGP_ATTR_DEFAULT_WEIGHT 32768
53 /* Valid lengths for mp_nexthop_len */
54 #define BGP_ATTR_NHLEN_IPV4 IPV4_MAX_BYTELEN
55 #define BGP_ATTR_NHLEN_VPNV4 8+IPV4_MAX_BYTELEN
56 #define BGP_ATTR_NHLEN_IPV6_GLOBAL IPV6_MAX_BYTELEN
57 #define BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL (IPV6_MAX_BYTELEN * 2)
58 #define BGP_ATTR_NHLEN_VPNV6_GLOBAL 8+IPV6_MAX_BYTELEN
59 #define BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL ((8+IPV6_MAX_BYTELEN) * 2)
61 /* Prefix SID types */
62 #define BGP_PREFIX_SID_LABEL_INDEX 1
63 #define BGP_PREFIX_SID_IPV6 2
64 #define BGP_PREFIX_SID_ORIGINATOR_SRGB 3
65 #define BGP_PREFIX_SID_SRV6_L3_SERVICE 5
66 #define BGP_PREFIX_SID_SRV6_L2_SERVICE 6
68 #define BGP_PREFIX_SID_LABEL_INDEX_LENGTH 7
69 #define BGP_PREFIX_SID_IPV6_LENGTH 19
70 #define BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH 6
72 #define BGP_ATTR_NH_AFI(afi, attr) \
73 ((afi != AFI_L2VPN) ? afi : \
74 ((attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4) ? AFI_IP : AFI_IP6))
76 /* PMSI tunnel types (RFC 6514) */
78 struct bgp_attr_encap_subtlv
{
79 struct bgp_attr_encap_subtlv
*next
; /* for chaining */
80 /* Reference count of this attribute. */
84 uint8_t value
[0]; /* will be extended */
89 * old rfp<->rfapi representation
91 struct bgp_tea_options
{
92 struct bgp_tea_options
*next
;
93 uint8_t options_count
;
94 uint16_t options_length
; /* each TLV may be 256 in length */
97 void *value
; /* pointer to data */
103 PMSI_TNLTYPE_NO_INFO
= 0,
104 PMSI_TNLTYPE_RSVP_TE_P2MP
,
105 PMSI_TNLTYPE_MLDP_P2MP
,
106 PMSI_TNLTYPE_PIM_SSM
,
108 PMSI_TNLTYPE_PIM_BIDIR
,
109 PMSI_TNLTYPE_INGR_REPL
,
110 PMSI_TNLTYPE_MLDP_MP2MP
,
111 PMSI_TNLTYPE_MAX
= PMSI_TNLTYPE_MLDP_MP2MP
114 /* BGP core attribute structure. */
116 /* AS Path structure */
117 struct aspath
*aspath
;
119 /* Community structure */
120 struct community
*community
;
122 /* Reference count of this attribute. */
123 unsigned long refcnt
;
125 /* Flag of attribute is set or not. */
128 /* Apart from in6_addr, the remaining static attributes */
129 struct in_addr nexthop
;
132 ifindex_t nh_ifindex
;
134 /* Path origin attribute */
137 /* PMSI tunnel type (RFC 6514). */
138 enum pta_type pmsi_tnl_type
;
140 /* has the route-map changed any attribute?
141 Used on the peer outbound side. */
142 uint32_t rmap_change_flags
;
144 /* Multi-Protocol Nexthop, AFI IPv6 */
145 struct in6_addr mp_nexthop_global
;
146 struct in6_addr mp_nexthop_local
;
148 /* ifIndex corresponding to mp_nexthop_local. */
149 ifindex_t nh_lla_ifindex
;
151 /* Extended Communities attribute. */
152 struct ecommunity
*ecommunity
;
154 /* Large Communities attribute. */
155 struct lcommunity
*lcommunity
;
157 /* Route-Reflector Cluster attribute */
158 struct cluster_list
*cluster
;
160 /* Unknown transitive attribute. */
161 struct transit
*transit
;
163 struct in_addr mp_nexthop_global_in
;
165 /* Aggregator Router ID attribute */
166 struct in_addr aggregator_addr
;
168 /* Route Reflector Originator attribute */
169 struct in_addr originator_id
;
171 /* Local weight, not actually an attribute */
177 /* MP Nexthop length */
178 uint8_t mp_nexthop_len
;
180 /* MP Nexthop preference */
181 uint8_t mp_nexthop_prefer_global
;
183 /* Static MAC for EVPN */
186 /* Flag for default gateway extended community in EVPN */
189 /* NA router flag (R-bit) support in EVPN */
196 uint32_t label_index
;
201 uint16_t encap_tunneltype
; /* grr */
202 struct bgp_attr_encap_subtlv
*encap_subtlvs
; /* rfc5512 */
205 struct bgp_attr_encap_subtlv
*vnc_subtlvs
; /* VNC-specific */
208 struct bgp_route_evpn evpn_overlay
;
210 /* EVPN MAC Mobility sequence number, if any. */
213 /* EVPN local router-mac */
216 /* Distance as applied by Route map */
220 uint32_t rmap_table_id
;
223 /* rmap_change_flags definition */
224 #define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0)
225 #define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1)
226 #define BATTR_REFLECTED (1 << 2)
227 #define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3)
228 #define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4)
229 #define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5)
230 #define BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED (1 << 6)
232 /* Router Reflector related structure. */
233 struct cluster_list
{
234 unsigned long refcnt
;
236 struct in_addr
*list
;
239 /* Unknown transit attribute. */
241 unsigned long refcnt
;
246 /* "(void) 0" will generate a compiler error. this is a safety check to
247 * ensure we're not using a value that exceeds the bit size of attr->flag. */
248 #define ATTR_FLAG_BIT(X) \
249 __builtin_choose_expr((X) >= 1 && (X) <= 64, 1ULL << ((X)-1), (void)0)
251 #define BGP_CLUSTER_LIST_LENGTH(attr) \
252 (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) \
253 ? (attr)->cluster->length \
257 BGP_ATTR_PARSE_PROCEED
= 0,
258 BGP_ATTR_PARSE_ERROR
= -1,
259 BGP_ATTR_PARSE_WITHDRAW
= -2,
261 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR
263 BGP_ATTR_PARSE_ERROR_NOTIFYPLS
= -3,
264 BGP_ATTR_PARSE_EOR
= -4,
265 } bgp_attr_parse_ret_t
;
267 struct bpacket_attr_vec_arr
;
270 extern void bgp_attr_init(void);
271 extern void bgp_attr_finish(void);
272 extern bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*, struct attr
*,
273 bgp_size_t
, struct bgp_nlri
*,
275 extern void bgp_attr_dup(struct attr
*, struct attr
*);
276 extern void bgp_attr_undup(struct attr
*new, struct attr
*old
);
277 extern struct attr
*bgp_attr_intern(struct attr
*attr
);
278 extern void bgp_attr_unintern_sub(struct attr
*);
279 extern void bgp_attr_unintern(struct attr
**);
280 extern void bgp_attr_flush(struct attr
*);
281 extern struct attr
*bgp_attr_default_set(struct attr
*attr
, uint8_t);
282 extern struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, uint8_t origin
,
283 struct aspath
*aspath
,
284 struct community
*community
,
285 struct ecommunity
*ecommunity
,
286 struct lcommunity
*lcommunity
,
287 struct bgp_aggregate
*aggregate
,
288 uint8_t atomic_aggregate
,
290 extern bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*,
291 struct stream
*, struct attr
*,
292 struct bpacket_attr_vec_arr
*vecarr
,
293 struct prefix
*, afi_t
, safi_t
,
294 struct peer
*, struct prefix_rd
*,
295 mpls_label_t
*, uint32_t, int, uint32_t);
296 extern void bgp_dump_routes_attr(struct stream
*, struct attr
*,
298 extern bool attrhash_cmp(const void *arg1
, const void *arg2
);
299 extern unsigned int attrhash_key_make(const void *);
300 extern void attr_show_all(struct vty
*);
301 extern unsigned long int attr_count(void);
302 extern unsigned long int attr_unknown_count(void);
304 /* Cluster list prototypes. */
305 extern int cluster_loop_check(struct cluster_list
*, struct in_addr
);
306 extern void cluster_unintern(struct cluster_list
*);
308 /* Transit attribute prototypes. */
309 void transit_unintern(struct transit
*);
311 /* Below exported for unit-test purposes only */
312 struct bgp_attr_parser_args
{
314 bgp_size_t length
; /* attribute data length; */
315 bgp_size_t total
; /* total length, inc header */
321 extern int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
323 extern int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
325 extern bgp_attr_parse_ret_t
326 bgp_attr_prefix_sid(int32_t tlength
, struct bgp_attr_parser_args
*args
,
327 struct bgp_nlri
*mp_update
);
329 extern struct bgp_attr_encap_subtlv
*
330 encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
);
332 extern void bgp_attr_flush_encap(struct attr
*attr
);
334 extern void bgp_attr_extcom_tunnel_type(struct attr
*attr
,
335 bgp_encap_types
*tunnel_type
);
338 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
339 * Typical call sequence is to call _start(), followed by multiple _prefix(),
340 * one for each NLRI that needs to be encoded into the UPDATE message, and
341 * finally the _end() function.
343 extern size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
,
344 afi_t afi
, safi_t safi
,
345 struct bpacket_attr_vec_arr
*vecarr
,
347 extern void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
348 struct prefix
*p
, struct prefix_rd
*prd
,
349 mpls_label_t
*label
, uint32_t num_labels
,
350 int addpath_encode
, uint32_t addpath_tx_id
,
352 extern size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
,
354 extern void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
);
356 extern size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
,
358 extern void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
,
359 afi_t afi
, safi_t safi
,
360 struct prefix_rd
*prd
, mpls_label_t
*,
361 uint32_t, int, uint32_t, struct attr
*);
362 extern void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
);
364 extern bgp_attr_parse_ret_t
bgp_attr_nexthop_valid(struct peer
*peer
,
367 static inline int bgp_rmap_nhop_changed(uint32_t out_rmap_flags
,
368 uint32_t in_rmap_flags
)
370 return ((CHECK_FLAG(out_rmap_flags
, BATTR_RMAP_NEXTHOP_PEER_ADDRESS
)
371 || CHECK_FLAG(out_rmap_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
)
372 || CHECK_FLAG(out_rmap_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
)
373 || CHECK_FLAG(out_rmap_flags
,
374 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
)
375 || CHECK_FLAG(out_rmap_flags
,
376 BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED
)
377 || CHECK_FLAG(out_rmap_flags
, BATTR_RMAP_IPV6_LL_NHOP_CHANGED
)
378 || CHECK_FLAG(in_rmap_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
383 static inline uint32_t mac_mobility_seqnum(struct attr
*attr
)
385 return (attr
) ? attr
->mm_seqnum
: 0;
388 #endif /* _QUAGGA_BGP_ATTR_H */