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