]>
Commit | Line | Data |
---|---|---|
1 | /* BGP attributes. | |
2 | * Copyright (C) 1996, 97, 98 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_BGP_ATTR_H | |
22 | #define _QUAGGA_BGP_ATTR_H | |
23 | ||
24 | #include "mpls.h" | |
25 | #include "bgp_attr_evpn.h" | |
26 | ||
27 | /* Simple bit mapping. */ | |
28 | #define BITMAP_NBBY 8 | |
29 | ||
30 | #define SET_BITMAP(MAP, NUM) \ | |
31 | SET_FLAG(MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY)) | |
32 | ||
33 | #define CHECK_BITMAP(MAP, NUM) \ | |
34 | CHECK_FLAG(MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY)) | |
35 | ||
36 | #define BGP_MED_MAX UINT32_MAX | |
37 | ||
38 | /* BGP Attribute type range. */ | |
39 | #define BGP_ATTR_TYPE_RANGE 256 | |
40 | #define BGP_ATTR_BITMAP_SIZE (BGP_ATTR_TYPE_RANGE / BITMAP_NBBY) | |
41 | ||
42 | /* BGP Attribute flags. */ | |
43 | #define BGP_ATTR_FLAG_OPTIONAL 0x80 /* Attribute is optional. */ | |
44 | #define BGP_ATTR_FLAG_TRANS 0x40 /* Attribute is transitive. */ | |
45 | #define BGP_ATTR_FLAG_PARTIAL 0x20 /* Attribute is partial. */ | |
46 | #define BGP_ATTR_FLAG_EXTLEN 0x10 /* Extended length flag. */ | |
47 | ||
48 | /* BGP attribute header must bigger than 2. */ | |
49 | #define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */ | |
50 | #define BGP_ATTR_DEFAULT_WEIGHT 32768 | |
51 | ||
52 | /* Valid lengths for mp_nexthop_len */ | |
53 | #define BGP_ATTR_NHLEN_IPV4 IPV4_MAX_BYTELEN | |
54 | #define BGP_ATTR_NHLEN_VPNV4 8+IPV4_MAX_BYTELEN | |
55 | #define BGP_ATTR_NHLEN_IPV6_GLOBAL IPV6_MAX_BYTELEN | |
56 | #define BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL (IPV6_MAX_BYTELEN * 2) | |
57 | #define BGP_ATTR_NHLEN_VPNV6_GLOBAL 8+IPV6_MAX_BYTELEN | |
58 | #define BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL ((8+IPV6_MAX_BYTELEN) * 2) | |
59 | ||
60 | /* Prefix SID types */ | |
61 | #define BGP_PREFIX_SID_LABEL_INDEX 1 | |
62 | #define BGP_PREFIX_SID_IPV6 2 | |
63 | #define BGP_PREFIX_SID_ORIGINATOR_SRGB 3 | |
64 | ||
65 | #define BGP_PREFIX_SID_LABEL_INDEX_LENGTH 7 | |
66 | #define BGP_PREFIX_SID_IPV6_LENGTH 19 | |
67 | #define BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH 6 | |
68 | ||
69 | /* PMSI tunnel types (RFC 6514) */ | |
70 | ||
71 | struct bgp_attr_encap_subtlv { | |
72 | struct bgp_attr_encap_subtlv *next; /* for chaining */ | |
73 | /* Reference count of this attribute. */ | |
74 | unsigned long refcnt; | |
75 | uint16_t type; | |
76 | uint16_t length; | |
77 | uint8_t value[0]; /* will be extended */ | |
78 | }; | |
79 | ||
80 | #if ENABLE_BGP_VNC | |
81 | /* | |
82 | * old rfp<->rfapi representation | |
83 | */ | |
84 | struct bgp_tea_options { | |
85 | struct bgp_tea_options *next; | |
86 | uint8_t options_count; | |
87 | uint16_t options_length; /* each TLV may be 256 in length */ | |
88 | uint8_t type; | |
89 | uint8_t length; | |
90 | void *value; /* pointer to data */ | |
91 | }; | |
92 | ||
93 | #endif | |
94 | ||
95 | /* Overlay Index Info */ | |
96 | struct overlay_index { | |
97 | struct eth_segment_id eth_s_id; | |
98 | union gw_addr gw_ip; | |
99 | }; | |
100 | ||
101 | enum pta_type { | |
102 | PMSI_TNLTYPE_NO_INFO = 0, | |
103 | PMSI_TNLTYPE_RSVP_TE_P2MP, | |
104 | PMSI_TNLTYPE_MLDP_P2MP, | |
105 | PMSI_TNLTYPE_PIM_SSM, | |
106 | PMSI_TNLTYPE_PIM_SM, | |
107 | PMSI_TNLTYPE_PIM_BIDIR, | |
108 | PMSI_TNLTYPE_INGR_REPL, | |
109 | PMSI_TNLTYPE_MLDP_MP2MP, | |
110 | PMSI_TNLTYPE_MAX = PMSI_TNLTYPE_MLDP_MP2MP | |
111 | }; | |
112 | ||
113 | /* BGP core attribute structure. */ | |
114 | struct attr { | |
115 | /* AS Path structure */ | |
116 | struct aspath *aspath; | |
117 | ||
118 | /* Community structure */ | |
119 | struct community *community; | |
120 | ||
121 | /* Reference count of this attribute. */ | |
122 | unsigned long refcnt; | |
123 | ||
124 | /* Flag of attribute is set or not. */ | |
125 | uint64_t flag; | |
126 | ||
127 | /* Apart from in6_addr, the remaining static attributes */ | |
128 | struct in_addr nexthop; | |
129 | uint32_t med; | |
130 | uint32_t local_pref; | |
131 | ifindex_t nh_ifindex; | |
132 | ||
133 | /* Path origin attribute */ | |
134 | uint8_t origin; | |
135 | ||
136 | /* PMSI tunnel type (RFC 6514). */ | |
137 | enum pta_type pmsi_tnl_type; | |
138 | ||
139 | /* has the route-map changed any attribute? | |
140 | Used on the peer outbound side. */ | |
141 | uint32_t rmap_change_flags; | |
142 | ||
143 | /* Multi-Protocol Nexthop, AFI IPv6 */ | |
144 | struct in6_addr mp_nexthop_global; | |
145 | struct in6_addr mp_nexthop_local; | |
146 | ||
147 | /* ifIndex corresponding to mp_nexthop_local. */ | |
148 | ifindex_t nh_lla_ifindex; | |
149 | ||
150 | /* Extended Communities attribute. */ | |
151 | struct ecommunity *ecommunity; | |
152 | ||
153 | /* Large Communities attribute. */ | |
154 | struct lcommunity *lcommunity; | |
155 | ||
156 | /* Route-Reflector Cluster attribute */ | |
157 | struct cluster_list *cluster; | |
158 | ||
159 | /* Unknown transitive attribute. */ | |
160 | struct transit *transit; | |
161 | ||
162 | struct in_addr mp_nexthop_global_in; | |
163 | ||
164 | /* Aggregator Router ID attribute */ | |
165 | struct in_addr aggregator_addr; | |
166 | ||
167 | /* Route Reflector Originator attribute */ | |
168 | struct in_addr originator_id; | |
169 | ||
170 | /* Local weight, not actually an attribute */ | |
171 | uint32_t weight; | |
172 | ||
173 | /* Aggregator ASN */ | |
174 | as_t aggregator_as; | |
175 | ||
176 | /* MP Nexthop length */ | |
177 | uint8_t mp_nexthop_len; | |
178 | ||
179 | /* MP Nexthop preference */ | |
180 | uint8_t mp_nexthop_prefer_global; | |
181 | ||
182 | /* Static MAC for EVPN */ | |
183 | uint8_t sticky; | |
184 | ||
185 | /* Flag for default gateway extended community in EVPN */ | |
186 | uint8_t default_gw; | |
187 | ||
188 | /* NA router flag (R-bit) support in EVPN */ | |
189 | uint8_t router_flag; | |
190 | ||
191 | /* route tag */ | |
192 | route_tag_t tag; | |
193 | ||
194 | /* Label index */ | |
195 | uint32_t label_index; | |
196 | ||
197 | /* MPLS label */ | |
198 | mpls_label_t label; | |
199 | ||
200 | uint16_t encap_tunneltype; /* grr */ | |
201 | struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */ | |
202 | ||
203 | #if ENABLE_BGP_VNC | |
204 | struct bgp_attr_encap_subtlv *vnc_subtlvs; /* VNC-specific */ | |
205 | #endif | |
206 | /* EVPN */ | |
207 | struct overlay_index evpn_overlay; | |
208 | ||
209 | /* EVPN MAC Mobility sequence number, if any. */ | |
210 | uint32_t mm_seqnum; | |
211 | ||
212 | /* EVPN local router-mac */ | |
213 | struct ethaddr rmac; | |
214 | }; | |
215 | ||
216 | /* rmap_change_flags definition */ | |
217 | #define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0) | |
218 | #define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1) | |
219 | #define BATTR_REFLECTED (1 << 2) | |
220 | #define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3) | |
221 | #define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4) | |
222 | #define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5) | |
223 | #define BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED (1 << 6) | |
224 | ||
225 | /* Router Reflector related structure. */ | |
226 | struct cluster_list { | |
227 | unsigned long refcnt; | |
228 | int length; | |
229 | struct in_addr *list; | |
230 | }; | |
231 | ||
232 | /* Unknown transit attribute. */ | |
233 | struct transit { | |
234 | unsigned long refcnt; | |
235 | int length; | |
236 | uint8_t *val; | |
237 | }; | |
238 | ||
239 | /* "(void) 0" will generate a compiler error. this is a safety check to | |
240 | * ensure we're not using a value that exceeds the bit size of attr->flag. */ | |
241 | #define ATTR_FLAG_BIT(X) \ | |
242 | __builtin_choose_expr((X) >= 1 && (X) <= 64, 1ULL << ((X)-1), (void)0) | |
243 | ||
244 | #define BGP_CLUSTER_LIST_LENGTH(attr) \ | |
245 | (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) \ | |
246 | ? (attr)->cluster->length \ | |
247 | : 0) | |
248 | ||
249 | typedef enum { | |
250 | BGP_ATTR_PARSE_PROCEED = 0, | |
251 | BGP_ATTR_PARSE_ERROR = -1, | |
252 | BGP_ATTR_PARSE_WITHDRAW = -2, | |
253 | ||
254 | /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR | |
255 | */ | |
256 | BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3, | |
257 | BGP_ATTR_PARSE_EOR = -4, | |
258 | } bgp_attr_parse_ret_t; | |
259 | ||
260 | struct bpacket_attr_vec_arr; | |
261 | ||
262 | /* Prototypes. */ | |
263 | extern void bgp_attr_init(void); | |
264 | extern void bgp_attr_finish(void); | |
265 | extern bgp_attr_parse_ret_t bgp_attr_parse(struct peer *, struct attr *, | |
266 | bgp_size_t, struct bgp_nlri *, | |
267 | struct bgp_nlri *); | |
268 | extern void bgp_attr_dup(struct attr *, struct attr *); | |
269 | extern void bgp_attr_undup(struct attr *new, struct attr *old); | |
270 | extern struct attr *bgp_attr_intern(struct attr *attr); | |
271 | extern void bgp_attr_unintern_sub(struct attr *); | |
272 | extern void bgp_attr_unintern(struct attr **); | |
273 | extern void bgp_attr_flush(struct attr *); | |
274 | extern struct attr *bgp_attr_default_set(struct attr *attr, uint8_t); | |
275 | extern struct attr *bgp_attr_aggregate_intern(struct bgp *bgp, uint8_t origin, | |
276 | struct aspath *aspath, | |
277 | struct community *community, | |
278 | struct ecommunity *ecommunity, | |
279 | struct lcommunity *lcommunity, | |
280 | int as_set, | |
281 | uint8_t atomic_aggregate); | |
282 | extern bgp_size_t bgp_packet_attribute(struct bgp *bgp, struct peer *, | |
283 | struct stream *, struct attr *, | |
284 | struct bpacket_attr_vec_arr *vecarr, | |
285 | struct prefix *, afi_t, safi_t, | |
286 | struct peer *, struct prefix_rd *, | |
287 | mpls_label_t *, uint32_t, int, uint32_t); | |
288 | extern void bgp_dump_routes_attr(struct stream *, struct attr *, | |
289 | struct prefix *); | |
290 | extern bool attrhash_cmp(const void *arg1, const void *arg2); | |
291 | extern unsigned int attrhash_key_make(void *); | |
292 | extern void attr_show_all(struct vty *); | |
293 | extern unsigned long int attr_count(void); | |
294 | extern unsigned long int attr_unknown_count(void); | |
295 | ||
296 | /* Cluster list prototypes. */ | |
297 | extern int cluster_loop_check(struct cluster_list *, struct in_addr); | |
298 | extern void cluster_unintern(struct cluster_list *); | |
299 | ||
300 | /* Transit attribute prototypes. */ | |
301 | void transit_unintern(struct transit *); | |
302 | ||
303 | /* Below exported for unit-test purposes only */ | |
304 | struct bgp_attr_parser_args { | |
305 | struct peer *peer; | |
306 | bgp_size_t length; /* attribute data length; */ | |
307 | bgp_size_t total; /* total length, inc header */ | |
308 | struct attr *attr; | |
309 | uint8_t type; | |
310 | uint8_t flags; | |
311 | uint8_t *startp; | |
312 | }; | |
313 | extern int bgp_mp_reach_parse(struct bgp_attr_parser_args *args, | |
314 | struct bgp_nlri *); | |
315 | extern int bgp_mp_unreach_parse(struct bgp_attr_parser_args *args, | |
316 | struct bgp_nlri *); | |
317 | extern bgp_attr_parse_ret_t | |
318 | bgp_attr_prefix_sid(int32_t tlength, struct bgp_attr_parser_args *args, | |
319 | struct bgp_nlri *mp_update); | |
320 | ||
321 | extern struct bgp_attr_encap_subtlv * | |
322 | encap_tlv_dup(struct bgp_attr_encap_subtlv *orig); | |
323 | ||
324 | extern void bgp_attr_flush_encap(struct attr *attr); | |
325 | ||
326 | /** | |
327 | * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes. | |
328 | * Typical call sequence is to call _start(), followed by multiple _prefix(), | |
329 | * one for each NLRI that needs to be encoded into the UPDATE message, and | |
330 | * finally the _end() function. | |
331 | */ | |
332 | extern size_t bgp_packet_mpattr_start(struct stream *s, struct peer *peer, | |
333 | afi_t afi, safi_t safi, | |
334 | struct bpacket_attr_vec_arr *vecarr, | |
335 | struct attr *attr); | |
336 | extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi, | |
337 | struct prefix *p, struct prefix_rd *prd, | |
338 | mpls_label_t *label, uint32_t num_labels, | |
339 | int addpath_encode, uint32_t addpath_tx_id, | |
340 | struct attr *); | |
341 | extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi, | |
342 | struct prefix *p); | |
343 | extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep); | |
344 | ||
345 | extern size_t bgp_packet_mpunreach_start(struct stream *s, afi_t afi, | |
346 | safi_t safi); | |
347 | extern void bgp_packet_mpunreach_prefix(struct stream *s, struct prefix *p, | |
348 | afi_t afi, safi_t safi, | |
349 | struct prefix_rd *prd, mpls_label_t *, | |
350 | uint32_t, int, uint32_t, struct attr *); | |
351 | extern void bgp_packet_mpunreach_end(struct stream *s, size_t attrlen_pnt); | |
352 | ||
353 | static inline int bgp_rmap_nhop_changed(uint32_t out_rmap_flags, | |
354 | uint32_t in_rmap_flags) | |
355 | { | |
356 | return ((CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS) | |
357 | || CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED) | |
358 | || CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV4_NHOP_CHANGED) | |
359 | || CHECK_FLAG(out_rmap_flags, | |
360 | BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED) | |
361 | || CHECK_FLAG(out_rmap_flags, | |
362 | BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED) | |
363 | || CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_LL_NHOP_CHANGED) | |
364 | || CHECK_FLAG(in_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) | |
365 | ? 1 | |
366 | : 0); | |
367 | } | |
368 | ||
369 | static inline uint32_t mac_mobility_seqnum(struct attr *attr) | |
370 | { | |
371 | return (attr) ? attr->mm_seqnum : 0; | |
372 | } | |
373 | ||
374 | #endif /* _QUAGGA_BGP_ATTR_H */ |