]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_attr.h
bgpd: use loops to reduce code duplication
[mirror_frr.git] / bgpd / bgp_attr.h
CommitLineData
718e3744 1/* BGP attributes.
2 Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
00d252cb 21#ifndef _QUAGGA_BGP_ATTR_H
22#define _QUAGGA_BGP_ATTR_H
23
718e3744 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
3b424979 33#define BGP_MED_MAX UINT32_MAX
34
03e214c8 35
718e3744 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. */
370b64a2 47#define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */
03e214c8
PJ
48#define BGP_ATTR_DEFAULT_WEIGHT 32768
49
801a9bcc
DS
50/* Valid lengths for mp_nexthop_len */
51#define BGP_ATTR_NHLEN_IPV4 IPV4_MAX_BYTELEN
945c8fe9 52#define BGP_ATTR_NHLEN_VPNV4 8+IPV4_MAX_BYTELEN
801a9bcc
DS
53#define BGP_ATTR_NHLEN_IPV6_GLOBAL IPV6_MAX_BYTELEN
54#define BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL (IPV6_MAX_BYTELEN * 2)
945c8fe9
LB
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)
801a9bcc 57
f4c89855
LB
58
59struct 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
65efcfce
LB
66#if ENABLE_BGP_VNC
67/*
68 * old rfp<->rfapi representation
69 */
70struct bgp_tea_options {
71 struct bgp_tea_options *next;
72 uint8_t options_count;
73 uint16_t options_length; /* each TLV may be 256 in length */
74 uint8_t type;
75 uint8_t length;
76 void *value; /* pointer to data */
77};
78
79#endif
80
fb982c25
PJ
81/* Additional/uncommon BGP attributes.
82 * lazily allocated as and when a struct attr
83 * requires it.
84 */
85struct attr_extra
718e3744 86{
fb982c25 87 /* Multi-Protocol Nexthop, AFI IPv6 */
718e3744 88 struct in6_addr mp_nexthop_global;
89 struct in6_addr mp_nexthop_local;
718e3744 90
718e3744 91 /* Extended Communities attribute. */
92 struct ecommunity *ecommunity;
cbdfbaa5
PJ
93
94 /* Route-Reflector Cluster attribute */
95 struct cluster_list *cluster;
96
718e3744 97 /* Unknown transitive attribute. */
98 struct transit *transit;
cbdfbaa5 99
fb982c25 100 struct in_addr mp_nexthop_global_in;
fb982c25
PJ
101
102 /* Aggregator Router ID attribute */
103 struct in_addr aggregator_addr;
104
105 /* Route Reflector Originator attribute */
106 struct in_addr originator_id;
107
108 /* Local weight, not actually an attribute */
109 u_int32_t weight;
110
111 /* Aggregator ASN */
112 as_t aggregator_as;
113
114 /* MP Nexthop length */
115 u_char mp_nexthop_len;
0d9551dc 116
161995ea
DS
117 /* MP Nexthop preference */
118 u_char mp_nexthop_prefer_global;
119
0d9551dc 120 /* route tag */
dc9ffce8 121 route_tag_t tag;
f4c89855
LB
122
123 uint16_t encap_tunneltype; /* grr */
124 struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
65efcfce
LB
125
126#if ENABLE_BGP_VNC
127 struct bgp_attr_encap_subtlv *vnc_subtlvs; /* VNC-specific */
128#endif
fb982c25
PJ
129};
130
131/* BGP core attribute structure. */
132struct attr
133{
134 /* AS Path structure */
135 struct aspath *aspath;
136
137 /* Community structure */
138 struct community *community;
139
140 /* Lazily allocated pointer to extra attributes */
141 struct attr_extra *extra;
142
cbdfbaa5
PJ
143 /* Reference count of this attribute. */
144 unsigned long refcnt;
145
146 /* Flag of attribute is set or not. */
147 u_int32_t flag;
148
149 /* Apart from in6_addr, the remaining static attributes */
150 struct in_addr nexthop;
151 u_int32_t med;
152 u_int32_t local_pref;
b892f1dd 153 ifindex_t nh_ifindex;
41367172 154
fb982c25 155 /* Path origin attribute */
cbdfbaa5 156 u_char origin;
3f9c7369
DS
157
158 /* has the route-map changed any attribute?
159 Used on the peer outbound side. */
160 u_int32_t rmap_change_flags;
718e3744 161};
162
3f9c7369 163/* rmap_change_flags definition */
3811f1e2 164#define BATTR_RMAP_IPV4_NHOP_CHANGED (1 << 0)
3f9c7369
DS
165#define BATTR_RMAP_NEXTHOP_PEER_ADDRESS (1 << 1)
166#define BATTR_REFLECTED (1 << 2)
316e074d 167#define BATTR_RMAP_NEXTHOP_UNCHANGED (1 << 3)
3811f1e2
DS
168#define BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED (1 << 4)
169#define BATTR_RMAP_IPV6_LL_NHOP_CHANGED (1 << 5)
b2e03f7a 170#define BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED (1 << 6)
3f9c7369 171
718e3744 172/* Router Reflector related structure. */
173struct cluster_list
174{
175 unsigned long refcnt;
176 int length;
177 struct in_addr *list;
178};
179
180/* Unknown transit attribute. */
181struct transit
182{
183 unsigned long refcnt;
184 int length;
185 u_char *val;
186};
187
188#define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
189
5e242b0d
DS
190#define BGP_CLUSTER_LIST_LENGTH(attr) \
191 (((attr)->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) ? \
192 (attr)->extra->cluster->length : 0)
193
b881c707
PJ
194typedef enum {
195 BGP_ATTR_PARSE_PROCEED = 0,
196 BGP_ATTR_PARSE_ERROR = -1,
197 BGP_ATTR_PARSE_WITHDRAW = -2,
f57000c0
DL
198
199 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
200 BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
b881c707
PJ
201} bgp_attr_parse_ret_t;
202
3f9c7369
DS
203struct bpacket_attr_vec_arr;
204
718e3744 205/* Prototypes. */
94f2b392 206extern void bgp_attr_init (void);
228da428 207extern void bgp_attr_finish (void);
b881c707
PJ
208extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
209 bgp_size_t, struct bgp_nlri *,
52a51fd3 210 struct bgp_nlri *);
fb982c25
PJ
211extern struct attr_extra *bgp_attr_extra_get (struct attr *);
212extern void bgp_attr_extra_free (struct attr *);
213extern void bgp_attr_dup (struct attr *, struct attr *);
73ac8160
DS
214extern void bgp_attr_deep_dup (struct attr *, struct attr *);
215extern void bgp_attr_deep_free (struct attr *);
94f2b392 216extern struct attr *bgp_attr_intern (struct attr *attr);
3f9c7369 217extern struct attr *bgp_attr_refcount (struct attr *attr);
b881c707 218extern void bgp_attr_unintern_sub (struct attr *);
f6f434b2 219extern void bgp_attr_unintern (struct attr **);
94f2b392 220extern void bgp_attr_flush (struct attr *);
221extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
222extern struct attr *bgp_attr_default_intern (u_char);
223extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
5228ad27 224 struct aspath *,
42f7e184 225 struct community *, int as_set, u_char);
8c71e481
PM
226extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
227 struct stream *, struct attr *,
3f9c7369 228 struct bpacket_attr_vec_arr *vecarr,
8c71e481
PM
229 struct prefix *, afi_t, safi_t,
230 struct peer *, struct prefix_rd *,
adbac85e 231 u_char *, int, u_int32_t);
94f2b392 232extern void bgp_dump_routes_attr (struct stream *, struct attr *,
adbac85e 233 struct prefix *);
ffe11cfb 234extern int attrhash_cmp (const void *, const void *);
923de654 235extern unsigned int attrhash_key_make (void *);
94f2b392 236extern void attr_show_all (struct vty *);
cbdfbaa5
PJ
237extern unsigned long int attr_count (void);
238extern unsigned long int attr_unknown_count (void);
718e3744 239
240/* Cluster list prototypes. */
94f2b392 241extern int cluster_loop_check (struct cluster_list *, struct in_addr);
242extern void cluster_unintern (struct cluster_list *);
718e3744 243
244/* Transit attribute prototypes. */
245void transit_unintern (struct transit *);
00d252cb 246
835315bf
PJ
247/* Below exported for unit-test purposes only */
248struct bgp_attr_parser_args {
249 struct peer *peer;
250 bgp_size_t length; /* attribute data length; */
251 bgp_size_t total; /* total length, inc header */
252 struct attr *attr;
253 u_int8_t type;
254 u_int8_t flags;
255 u_char *startp;
256};
257extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
258 struct bgp_nlri *);
259extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
260 struct bgp_nlri *);
03292809 261
f4c89855
LB
262extern struct bgp_attr_encap_subtlv *
263encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
264
265extern void
266bgp_attr_flush_encap(struct attr *attr);
267
8c71e481
PM
268/**
269 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
270 * Typical call sequence is to call _start(), followed by multiple _prefix(),
271 * one for each NLRI that needs to be encoded into the UPDATE message, and
272 * finally the _end() function.
273 */
274extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
8a92a8a0
DS
275 afi_t nh_afi,
276 struct bpacket_attr_vec_arr *vecarr,
8c71e481
PM
277 struct attr *attr);
278extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
279 struct prefix *p, struct prefix_rd *prd,
adbac85e
DW
280 u_char *tag, int addpath_encode,
281 u_int32_t addpath_tx_id);
93b73dfa
LB
282extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
283 struct prefix *p);
8c71e481
PM
284extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
285
286extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
287 safi_t safi);
288extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
289 afi_t afi, safi_t safi, struct prefix_rd *prd,
adbac85e 290 u_char *tag, int, u_int32_t);
8c71e481
PM
291extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
292
3811f1e2
DS
293static inline int
294bgp_rmap_nhop_changed(u_int32_t out_rmap_flags, u_int32_t in_rmap_flags)
295{
296 return ((CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS) ||
297 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED) ||
298 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV4_NHOP_CHANGED) ||
299 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED) ||
b2e03f7a 300 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED) ||
3811f1e2
DS
301 CHECK_FLAG(out_rmap_flags, BATTR_RMAP_IPV6_LL_NHOP_CHANGED) ||
302 CHECK_FLAG(in_rmap_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) ? 1 : 0);
303}
304
00d252cb 305#endif /* _QUAGGA_BGP_ATTR_H */