]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_attr.h
Merge pull request #30 from qlyoung/fix-cli-nits
[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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #ifndef _QUAGGA_BGP_ATTR_H
22 #define _QUAGGA_BGP_ATTR_H
23
24 /* Simple bit mapping. */
25 #define BITMAP_NBBY 8
26
27 #define SET_BITMAP(MAP, NUM) \
28 SET_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
29
30 #define CHECK_BITMAP(MAP, NUM) \
31 CHECK_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
32
33 #define BGP_MED_MAX UINT32_MAX
34
35
36 /* BGP Attribute type range. */
37 #define BGP_ATTR_TYPE_RANGE 256
38 #define BGP_ATTR_BITMAP_SIZE (BGP_ATTR_TYPE_RANGE / BITMAP_NBBY)
39
40 /* BGP Attribute flags. */
41 #define BGP_ATTR_FLAG_OPTIONAL 0x80 /* Attribute is optional. */
42 #define BGP_ATTR_FLAG_TRANS 0x40 /* Attribute is transitive. */
43 #define BGP_ATTR_FLAG_PARTIAL 0x20 /* Attribute is partial. */
44 #define BGP_ATTR_FLAG_EXTLEN 0x10 /* Extended length flag. */
45
46 /* BGP attribute header must bigger than 2. */
47 #define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */
48 #define BGP_ATTR_DEFAULT_WEIGHT 32768
49
50 /* Valid lengths for mp_nexthop_len */
51 #define BGP_ATTR_NHLEN_IPV4 IPV4_MAX_BYTELEN
52 #define BGP_ATTR_NHLEN_VPNV4 8+IPV4_MAX_BYTELEN
53 #define BGP_ATTR_NHLEN_IPV6_GLOBAL IPV6_MAX_BYTELEN
54 #define BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL (IPV6_MAX_BYTELEN * 2)
55 #define BGP_ATTR_NHLEN_VPNV6_GLOBAL 8+IPV6_MAX_BYTELEN
56 #define BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL ((8+IPV6_MAX_BYTELEN) * 2)
57
58
59 struct bgp_attr_encap_subtlv {
60 struct bgp_attr_encap_subtlv *next; /* for chaining */
61 /* Reference count of this attribute. */
62 unsigned long refcnt;
63 uint16_t type;
64 uint16_t length;
65 uint8_t value[1]; /* will be extended */
66 };
67
68 #if ENABLE_BGP_VNC
69 /*
70 * old rfp<->rfapi representation
71 */
72 struct bgp_tea_options {
73 struct bgp_tea_options *next;
74 uint8_t options_count;
75 uint16_t options_length; /* each TLV may be 256 in length */
76 uint8_t type;
77 uint8_t length;
78 void *value; /* pointer to data */
79 };
80
81 #endif
82
83 /* Additional/uncommon BGP attributes.
84 * lazily allocated as and when a struct attr
85 * requires it.
86 */
87 struct attr_extra
88 {
89 /* Multi-Protocol Nexthop, AFI IPv6 */
90 struct in6_addr mp_nexthop_global;
91 struct in6_addr mp_nexthop_local;
92
93 /* Extended Communities attribute. */
94 struct ecommunity *ecommunity;
95
96 /* Route-Reflector Cluster attribute */
97 struct cluster_list *cluster;
98
99 /* Unknown transitive attribute. */
100 struct transit *transit;
101
102 struct in_addr mp_nexthop_global_in;
103
104 /* Aggregator Router ID attribute */
105 struct in_addr aggregator_addr;
106
107 /* Route Reflector Originator attribute */
108 struct in_addr originator_id;
109
110 /* Local weight, not actually an attribute */
111 u_int32_t weight;
112
113 /* Aggregator ASN */
114 as_t aggregator_as;
115
116 /* MP Nexthop length */
117 u_char mp_nexthop_len;
118
119 /* MP Nexthop preference */
120 u_char mp_nexthop_prefer_global;
121
122 /* route tag */
123 route_tag_t tag;
124
125 uint16_t encap_tunneltype; /* grr */
126 struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
127
128 #if ENABLE_BGP_VNC
129 struct bgp_attr_encap_subtlv *vnc_subtlvs; /* VNC-specific */
130 #endif
131 };
132
133 /* BGP core attribute structure. */
134 struct attr
135 {
136 /* AS Path structure */
137 struct aspath *aspath;
138
139 /* Community structure */
140 struct community *community;
141
142 /* Lazily allocated pointer to extra attributes */
143 struct attr_extra *extra;
144
145 /* Reference count of this attribute. */
146 unsigned long refcnt;
147
148 /* Flag of attribute is set or not. */
149 u_int32_t flag;
150
151 /* Apart from in6_addr, the remaining static attributes */
152 struct in_addr nexthop;
153 u_int32_t med;
154 u_int32_t local_pref;
155 ifindex_t nh_ifindex;
156
157 /* Path origin attribute */
158 u_char origin;
159
160 /* has the route-map changed any attribute?
161 Used on the peer outbound side. */
162 u_int32_t rmap_change_flags;
163 };
164
165 /* rmap_change_flags definition */
166 #define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0)
167 #define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1)
168 #define BATTR_REFLECTED (1 << 2)
169 #define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3)
170 #define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4)
171 #define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5)
172 #define BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED (1 << 6)
173
174 /* Router Reflector related structure. */
175 struct cluster_list
176 {
177 unsigned long refcnt;
178 int length;
179 struct in_addr *list;
180 };
181
182 /* Unknown transit attribute. */
183 struct transit
184 {
185 unsigned long refcnt;
186 int length;
187 u_char *val;
188 };
189
190 #define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
191
192 #define BGP_CLUSTER_LIST_LENGTH(attr) \
193 (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) ? \
194 (attr)->extra->cluster->length : 0)
195
196 typedef enum {
197 BGP_ATTR_PARSE_PROCEED = 0,
198 BGP_ATTR_PARSE_ERROR = -1,
199 BGP_ATTR_PARSE_WITHDRAW = -2,
200
201 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
202 BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
203 } bgp_attr_parse_ret_t;
204
205 struct bpacket_attr_vec_arr;
206
207 /* Prototypes. */
208 extern void bgp_attr_init (void);
209 extern void bgp_attr_finish (void);
210 extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
211 bgp_size_t, struct bgp_nlri *,
212 struct bgp_nlri *);
213 extern struct attr_extra *bgp_attr_extra_get (struct attr *);
214 extern void bgp_attr_extra_free (struct attr *);
215 extern void bgp_attr_dup (struct attr *, struct attr *);
216 extern void bgp_attr_deep_dup (struct attr *, struct attr *);
217 extern void bgp_attr_deep_free (struct attr *);
218 extern struct attr *bgp_attr_intern (struct attr *attr);
219 extern struct attr *bgp_attr_refcount (struct attr *attr);
220 extern void bgp_attr_unintern_sub (struct attr *);
221 extern void bgp_attr_unintern (struct attr **);
222 extern void bgp_attr_flush (struct attr *);
223 extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
224 extern struct attr *bgp_attr_default_intern (u_char);
225 extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
226 struct aspath *,
227 struct community *, int as_set, u_char);
228 extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
229 struct stream *, struct attr *,
230 struct bpacket_attr_vec_arr *vecarr,
231 struct prefix *, afi_t, safi_t,
232 struct peer *, struct prefix_rd *,
233 u_char *, int, u_int32_t);
234 extern void bgp_dump_routes_attr (struct stream *, struct attr *,
235 struct prefix *);
236 extern int attrhash_cmp (const void *, const void *);
237 extern unsigned int attrhash_key_make (void *);
238 extern void attr_show_all (struct vty *);
239 extern unsigned long int attr_count (void);
240 extern unsigned long int attr_unknown_count (void);
241
242 /* Cluster list prototypes. */
243 extern int cluster_loop_check (struct cluster_list *, struct in_addr);
244 extern void cluster_unintern (struct cluster_list *);
245
246 /* Transit attribute prototypes. */
247 void transit_unintern (struct transit *);
248
249 /* Below exported for unit-test purposes only */
250 struct bgp_attr_parser_args {
251 struct peer *peer;
252 bgp_size_t length; /* attribute data length; */
253 bgp_size_t total; /* total length, inc header */
254 struct attr *attr;
255 u_int8_t type;
256 u_int8_t flags;
257 u_char *startp;
258 };
259 extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
260 struct bgp_nlri *);
261 extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
262 struct bgp_nlri *);
263
264 extern struct bgp_attr_encap_subtlv *
265 encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
266
267 extern void
268 bgp_attr_flush_encap(struct attr *attr);
269
270 /**
271 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
272 * Typical call sequence is to call _start(), followed by multiple _prefix(),
273 * one for each NLRI that needs to be encoded into the UPDATE message, and
274 * finally the _end() function.
275 */
276 extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
277 afi_t nh_afi,
278 struct bpacket_attr_vec_arr *vecarr,
279 struct attr *attr);
280 extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
281 struct prefix *p, struct prefix_rd *prd,
282 u_char *tag, int addpath_encode,
283 u_int32_t addpath_tx_id);
284 extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
285 struct prefix *p);
286 extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
287
288 extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
289 safi_t safi);
290 extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
291 afi_t afi, safi_t safi, struct prefix_rd *prd,
292 u_char *tag, int, u_int32_t);
293 extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
294
295 static inline int
296 bgp_rmap_nhop_changed(u_int32_t out_rmap_flags, u_int32_t in_rmap_flags)
297 {
298 return ((CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS) ||
299 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED) ||
300 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV4_NHOP_CHANGED) ||
301 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED) ||
302 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED) ||
303 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_LL_NHOP_CHANGED) ||
304 CHECK_FLAG(in_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) ? 1 : 0);
305 }
306
307 #endif /* _QUAGGA_BGP_ATTR_H */