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