]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_attr.h
BGP: support for addpath TX
[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 12
53 #define BGP_ATTR_NHLEN_IPV6_GLOBAL IPV6_MAX_BYTELEN
54 #define BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL (IPV6_MAX_BYTELEN * 2)
55
56 /* Additional/uncommon BGP attributes.
57 * lazily allocated as and when a struct attr
58 * requires it.
59 */
60 struct attr_extra
61 {
62 /* Multi-Protocol Nexthop, AFI IPv6 */
63 #ifdef HAVE_IPV6
64 struct in6_addr mp_nexthop_global;
65 struct in6_addr mp_nexthop_local;
66 #endif /* HAVE_IPV6 */
67
68 /* Extended Communities attribute. */
69 struct ecommunity *ecommunity;
70
71 /* Route-Reflector Cluster attribute */
72 struct cluster_list *cluster;
73
74 /* Unknown transitive attribute. */
75 struct transit *transit;
76
77 struct in_addr mp_nexthop_global_in;
78 struct in_addr mp_nexthop_local_in;
79
80 /* Aggregator Router ID attribute */
81 struct in_addr aggregator_addr;
82
83 /* Route Reflector Originator attribute */
84 struct in_addr originator_id;
85
86 /* Local weight, not actually an attribute */
87 u_int32_t weight;
88
89 /* Aggregator ASN */
90 as_t aggregator_as;
91
92 /* MP Nexthop length */
93 u_char mp_nexthop_len;
94
95 /* route tag */
96 u_short tag;
97 };
98
99 /* BGP core attribute structure. */
100 struct attr
101 {
102 /* AS Path structure */
103 struct aspath *aspath;
104
105 /* Community structure */
106 struct community *community;
107
108 /* Lazily allocated pointer to extra attributes */
109 struct attr_extra *extra;
110
111 /* Reference count of this attribute. */
112 unsigned long refcnt;
113
114 /* Flag of attribute is set or not. */
115 u_int32_t flag;
116
117 /* Apart from in6_addr, the remaining static attributes */
118 struct in_addr nexthop;
119 u_int32_t med;
120 u_int32_t local_pref;
121 u_int32_t nh_ifindex;
122
123 /* Path origin attribute */
124 u_char origin;
125
126 /* has the route-map changed any attribute?
127 Used on the peer outbound side. */
128 u_int32_t rmap_change_flags;
129 };
130
131 /* rmap_change_flags definition */
132 #define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0)
133 #define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1)
134 #define BATTR_REFLECTED (1 << 2)
135 #define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3)
136 #define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4)
137 #define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5)
138
139 /* Router Reflector related structure. */
140 struct cluster_list
141 {
142 unsigned long refcnt;
143 int length;
144 struct in_addr *list;
145 };
146
147 /* Unknown transit attribute. */
148 struct transit
149 {
150 unsigned long refcnt;
151 int length;
152 u_char *val;
153 };
154
155 #define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
156
157 #define BGP_CLUSTER_LIST_LENGTH(attr) \
158 (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) ? \
159 (attr)->extra->cluster->length : 0)
160
161 typedef enum {
162 BGP_ATTR_PARSE_PROCEED = 0,
163 BGP_ATTR_PARSE_ERROR = -1,
164 BGP_ATTR_PARSE_WITHDRAW = -2,
165
166 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
167 BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
168 } bgp_attr_parse_ret_t;
169
170 struct bpacket_attr_vec_arr;
171
172 /* Prototypes. */
173 extern void bgp_attr_init (void);
174 extern void bgp_attr_finish (void);
175 extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
176 bgp_size_t, struct bgp_nlri *,
177 struct bgp_nlri *);
178 extern int bgp_attr_check (struct peer *, struct attr *, bgp_size_t);
179 extern struct attr_extra *bgp_attr_extra_get (struct attr *);
180 extern void bgp_attr_extra_free (struct attr *);
181 extern void bgp_attr_dup (struct attr *, struct attr *);
182 extern void bgp_attr_deep_dup (struct attr *, struct attr *);
183 extern void bgp_attr_deep_free (struct attr *);
184 extern struct attr *bgp_attr_intern (struct attr *attr);
185 extern struct attr *bgp_attr_refcount (struct attr *attr);
186 extern void bgp_attr_unintern_sub (struct attr *);
187 extern void bgp_attr_unintern (struct attr **);
188 extern void bgp_attr_flush (struct attr *);
189 extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
190 extern struct attr *bgp_attr_default_intern (u_char);
191 extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
192 struct aspath *,
193 struct community *, int as_set, u_char);
194 extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
195 struct stream *, struct attr *,
196 struct bpacket_attr_vec_arr *vecarr,
197 struct prefix *, afi_t, safi_t,
198 struct peer *, struct prefix_rd *,
199 u_char *, int, u_int32_t);
200 extern void bgp_dump_routes_attr (struct stream *, struct attr *,
201 struct prefix *);
202 extern int attrhash_cmp (const void *, const void *);
203 extern unsigned int attrhash_key_make (void *);
204 extern void attr_show_all (struct vty *);
205 extern unsigned long int attr_count (void);
206 extern unsigned long int attr_unknown_count (void);
207
208 /* Cluster list prototypes. */
209 extern int cluster_loop_check (struct cluster_list *, struct in_addr);
210 extern void cluster_unintern (struct cluster_list *);
211
212 /* Transit attribute prototypes. */
213 void transit_unintern (struct transit *);
214
215 /* Below exported for unit-test purposes only */
216 struct bgp_attr_parser_args {
217 struct peer *peer;
218 bgp_size_t length; /* attribute data length; */
219 bgp_size_t total; /* total length, inc header */
220 struct attr *attr;
221 u_int8_t type;
222 u_int8_t flags;
223 u_char *startp;
224 };
225 extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
226 struct bgp_nlri *);
227 extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
228 struct bgp_nlri *);
229
230 /**
231 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
232 * Typical call sequence is to call _start(), followed by multiple _prefix(),
233 * one for each NLRI that needs to be encoded into the UPDATE message, and
234 * finally the _end() function.
235 */
236 extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
237 afi_t nh_afi,
238 struct bpacket_attr_vec_arr *vecarr,
239 struct attr *attr);
240 extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
241 struct prefix *p, struct prefix_rd *prd,
242 u_char *tag, int addpath_encode,
243 u_int32_t addpath_tx_id);
244 extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
245
246 extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
247 safi_t safi);
248 extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
249 afi_t afi, safi_t safi, struct prefix_rd *prd,
250 u_char *tag, int, u_int32_t);
251 extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
252
253 static inline int
254 bgp_rmap_nhop_changed(u_int32_t out_rmap_flags, u_int32_t in_rmap_flags)
255 {
256 return ((CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS) ||
257 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED) ||
258 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV4_NHOP_CHANGED) ||
259 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED) ||
260 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_LL_NHOP_CHANGED) ||
261 CHECK_FLAG(in_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) ? 1 : 0);
262 }
263
264 #endif /* _QUAGGA_BGP_ATTR_H */