]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_attr.h
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / bgpd / bgp_attr.h
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 "bgp_attr_evpn.h"
25
26 /* Simple bit mapping. */
27 #define BITMAP_NBBY 8
28
29 #define SET_BITMAP(MAP, NUM) \
30 SET_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
31
32 #define CHECK_BITMAP(MAP, NUM) \
33 CHECK_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
34
35 #define BGP_MED_MAX UINT32_MAX
36
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 struct bgp_attr_encap_subtlv {
70 struct bgp_attr_encap_subtlv *next; /* for chaining */
71 /* Reference count of this attribute. */
72 unsigned long refcnt;
73 uint16_t type;
74 uint16_t length;
75 uint8_t value[1]; /* will be extended */
76 };
77
78 #if ENABLE_BGP_VNC
79 /*
80 * old rfp<->rfapi representation
81 */
82 struct bgp_tea_options {
83 struct bgp_tea_options *next;
84 uint8_t options_count;
85 uint16_t options_length; /* each TLV may be 256 in length */
86 uint8_t type;
87 uint8_t length;
88 void *value; /* pointer to data */
89 };
90
91 #endif
92
93 /* Overlay Index Info */
94 struct overlay_index
95 {
96 struct eth_segment_id eth_s_id;
97 union gw_addr gw_ip;
98 };
99
100 /* Additional/uncommon BGP attributes.
101 * lazily allocated as and when a struct attr
102 * requires it.
103 */
104 struct attr_extra
105 {
106 /* Multi-Protocol Nexthop, AFI IPv6 */
107 struct in6_addr mp_nexthop_global;
108 struct in6_addr mp_nexthop_local;
109
110 /* Extended Communities attribute. */
111 struct ecommunity *ecommunity;
112
113 /* Large Communities attribute. */
114 struct lcommunity *lcommunity;
115
116 /* Route-Reflector Cluster attribute */
117 struct cluster_list *cluster;
118
119 /* Unknown transitive attribute. */
120 struct transit *transit;
121
122 struct in_addr mp_nexthop_global_in;
123
124 /* Aggregator Router ID attribute */
125 struct in_addr aggregator_addr;
126
127 /* Route Reflector Originator attribute */
128 struct in_addr originator_id;
129
130 /* Local weight, not actually an attribute */
131 u_int32_t weight;
132
133 /* Aggregator ASN */
134 as_t aggregator_as;
135
136 /* MP Nexthop length */
137 u_char mp_nexthop_len;
138
139 /* MP Nexthop preference */
140 u_char mp_nexthop_prefer_global;
141
142 /* route tag */
143 route_tag_t tag;
144
145 /* Label index */
146 u_int32_t label_index;
147
148 uint16_t encap_tunneltype; /* grr */
149 struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
150
151 #if ENABLE_BGP_VNC
152 struct bgp_attr_encap_subtlv *vnc_subtlvs; /* VNC-specific */
153 #endif
154 /* EVPN */
155 struct overlay_index evpn_overlay;
156 };
157
158 /* BGP core attribute structure. */
159 struct attr
160 {
161 /* AS Path structure */
162 struct aspath *aspath;
163
164 /* Community structure */
165 struct community *community;
166
167 /* Lazily allocated pointer to extra attributes */
168 struct attr_extra *extra;
169
170 /* Reference count of this attribute. */
171 unsigned long refcnt;
172
173 /* Flag of attribute is set or not. */
174 uint64_t flag;
175
176 /* Apart from in6_addr, the remaining static attributes */
177 struct in_addr nexthop;
178 u_int32_t med;
179 u_int32_t local_pref;
180 ifindex_t nh_ifindex;
181
182 /* Path origin attribute */
183 u_char origin;
184
185 /* has the route-map changed any attribute?
186 Used on the peer outbound side. */
187 u_int32_t rmap_change_flags;
188 };
189
190 /* rmap_change_flags definition */
191 #define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0)
192 #define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1)
193 #define BATTR_REFLECTED (1 << 2)
194 #define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3)
195 #define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4)
196 #define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5)
197 #define BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED (1 << 6)
198
199 /* Router Reflector related structure. */
200 struct cluster_list
201 {
202 unsigned long refcnt;
203 int length;
204 struct in_addr *list;
205 };
206
207 /* Unknown transit attribute. */
208 struct transit
209 {
210 unsigned long refcnt;
211 int length;
212 u_char *val;
213 };
214
215 #define ATTR_FLAG_BIT(X) (1ULL << ((X) - 1))
216
217 #define BGP_CLUSTER_LIST_LENGTH(attr) \
218 (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) ? \
219 (attr)->extra->cluster->length : 0)
220
221 typedef enum {
222 BGP_ATTR_PARSE_PROCEED = 0,
223 BGP_ATTR_PARSE_ERROR = -1,
224 BGP_ATTR_PARSE_WITHDRAW = -2,
225
226 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
227 BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
228 } bgp_attr_parse_ret_t;
229
230 struct bpacket_attr_vec_arr;
231
232 /* Prototypes. */
233 extern void bgp_attr_init (void);
234 extern void bgp_attr_finish (void);
235 extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
236 bgp_size_t, struct bgp_nlri *,
237 struct bgp_nlri *);
238 extern struct attr_extra *bgp_attr_extra_get (struct attr *);
239 extern void bgp_attr_extra_free (struct attr *);
240 extern void bgp_attr_dup (struct attr *, struct attr *);
241 extern void bgp_attr_deep_dup (struct attr *, struct attr *);
242 extern void bgp_attr_deep_free (struct attr *);
243 extern struct attr *bgp_attr_intern (struct attr *attr);
244 extern struct attr *bgp_attr_refcount (struct attr *attr);
245 extern void bgp_attr_unintern_sub (struct attr *);
246 extern void bgp_attr_unintern (struct attr **);
247 extern void bgp_attr_flush (struct attr *);
248 extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
249 extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
250 struct aspath *,
251 struct community *, int as_set, u_char);
252 extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
253 struct stream *, struct attr *,
254 struct bpacket_attr_vec_arr *vecarr,
255 struct prefix *, afi_t, safi_t,
256 struct peer *, struct prefix_rd *,
257 u_char *, int, u_int32_t);
258 extern void bgp_dump_routes_attr (struct stream *, struct attr *,
259 struct prefix *);
260 extern int attrhash_cmp (const void *, const void *);
261 extern unsigned int attrhash_key_make (void *);
262 extern void attr_show_all (struct vty *);
263 extern unsigned long int attr_count (void);
264 extern unsigned long int attr_unknown_count (void);
265
266 /* Cluster list prototypes. */
267 extern int cluster_loop_check (struct cluster_list *, struct in_addr);
268 extern void cluster_unintern (struct cluster_list *);
269
270 /* Transit attribute prototypes. */
271 void transit_unintern (struct transit *);
272
273 /* Below exported for unit-test purposes only */
274 struct bgp_attr_parser_args {
275 struct peer *peer;
276 bgp_size_t length; /* attribute data length; */
277 bgp_size_t total; /* total length, inc header */
278 struct attr *attr;
279 u_int8_t type;
280 u_int8_t flags;
281 u_char *startp;
282 };
283 extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
284 struct bgp_nlri *);
285 extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
286 struct bgp_nlri *);
287
288 extern struct bgp_attr_encap_subtlv *
289 encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
290
291 extern void
292 bgp_attr_flush_encap(struct attr *attr);
293
294 /**
295 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
296 * Typical call sequence is to call _start(), followed by multiple _prefix(),
297 * one for each NLRI that needs to be encoded into the UPDATE message, and
298 * finally the _end() function.
299 */
300 extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
301 afi_t nh_afi,
302 struct bpacket_attr_vec_arr *vecarr,
303 struct attr *attr);
304 extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
305 struct prefix *p, struct prefix_rd *prd,
306 u_char *tag, int addpath_encode,
307 u_int32_t addpath_tx_id,
308 struct attr *);
309 extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
310 struct prefix *p);
311 extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
312
313 extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
314 safi_t safi);
315 extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
316 afi_t afi, safi_t safi, struct prefix_rd *prd,
317 u_char *tag, int, u_int32_t, struct attr *);
318 extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
319
320 static inline int
321 bgp_rmap_nhop_changed(u_int32_t out_rmap_flags, u_int32_t in_rmap_flags)
322 {
323 return ((CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS) ||
324 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED) ||
325 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV4_NHOP_CHANGED) ||
326 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED) ||
327 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED) ||
328 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_LL_NHOP_CHANGED) ||
329 CHECK_FLAG(in_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) ? 1 : 0);
330 }
331
332 #endif /* _QUAGGA_BGP_ATTR_H */