blob: 6a134de4fe324df54b2bf7e25dc6dff1a17a4bbd [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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
paul00d252c2005-05-23 14:19:54 +000021#ifndef _QUAGGA_BGP_ATTR_H
22#define _QUAGGA_BGP_ATTR_H
23
paul718e3742002-12-13 20:15:29 +000024/* 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
paul3b424972003-10-13 09:47:32 +000033#define BGP_MED_MAX UINT32_MAX
34
Paul Jakma03e214c2007-04-29 18:31:07 +000035
paul718e3742002-12-13 20:15:29 +000036/* 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. */
Paul Jakma370b64a2007-12-22 16:49:52 +000047#define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */
Paul Jakma03e214c2007-04-29 18:31:07 +000048#define BGP_ATTR_DEFAULT_WEIGHT 32768
49
Lou Bergerc3741782016-01-12 13:42:01 -050050struct bgp_attr_encap_subtlv {
51 struct bgp_attr_encap_subtlv *next; /* for chaining */
52 uint16_t type;
53 uint16_t length;
54 uint8_t value[1]; /* will be extended */
55};
56
Paul Jakmafb982c22007-05-04 20:15:47 +000057/* Additional/uncommon BGP attributes.
58 * lazily allocated as and when a struct attr
59 * requires it.
60 */
61struct attr_extra
paul718e3742002-12-13 20:15:29 +000062{
Paul Jakmafb982c22007-05-04 20:15:47 +000063 /* Multi-Protocol Nexthop, AFI IPv6 */
paul718e3742002-12-13 20:15:29 +000064#ifdef HAVE_IPV6
65 struct in6_addr mp_nexthop_global;
66 struct in6_addr mp_nexthop_local;
67#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +000068
paul718e3742002-12-13 20:15:29 +000069 /* Extended Communities attribute. */
70 struct ecommunity *ecommunity;
Paul Jakmacbdfbaa2006-03-30 13:20:48 +000071
72 /* Route-Reflector Cluster attribute */
73 struct cluster_list *cluster;
74
paul718e3742002-12-13 20:15:29 +000075 /* Unknown transitive attribute. */
76 struct transit *transit;
Paul Jakmacbdfbaa2006-03-30 13:20:48 +000077
Paul Jakmafb982c22007-05-04 20:15:47 +000078 struct in_addr mp_nexthop_global_in;
79 struct in_addr mp_nexthop_local_in;
80
81 /* Aggregator Router ID attribute */
82 struct in_addr aggregator_addr;
83
84 /* Route Reflector Originator attribute */
85 struct in_addr originator_id;
86
87 /* Local weight, not actually an attribute */
88 u_int32_t weight;
89
90 /* Aggregator ASN */
91 as_t aggregator_as;
92
93 /* MP Nexthop length */
94 u_char mp_nexthop_len;
Lou Bergerc3741782016-01-12 13:42:01 -050095
96 uint16_t encap_tunneltype; /* grr */
97 struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
Paul Jakmafb982c22007-05-04 20:15:47 +000098};
99
100/* BGP core attribute structure. */
101struct attr
102{
103 /* AS Path structure */
104 struct aspath *aspath;
105
106 /* Community structure */
107 struct community *community;
108
109 /* Lazily allocated pointer to extra attributes */
110 struct attr_extra *extra;
111
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000112 /* Reference count of this attribute. */
113 unsigned long refcnt;
114
115 /* Flag of attribute is set or not. */
116 u_int32_t flag;
117
118 /* Apart from in6_addr, the remaining static attributes */
119 struct in_addr nexthop;
120 u_int32_t med;
121 u_int32_t local_pref;
Paul Jakma41367172007-08-06 15:24:51 +0000122
Paul Jakmafb982c22007-05-04 20:15:47 +0000123 /* Path origin attribute */
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000124 u_char origin;
paul718e3742002-12-13 20:15:29 +0000125};
126
127/* Router Reflector related structure. */
128struct cluster_list
129{
130 unsigned long refcnt;
131 int length;
132 struct in_addr *list;
133};
134
135/* Unknown transit attribute. */
136struct transit
137{
138 unsigned long refcnt;
139 int length;
140 u_char *val;
141};
142
143#define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
144
Paul Jakmab881c702010-11-23 16:35:42 +0000145typedef enum {
146 BGP_ATTR_PARSE_PROCEED = 0,
147 BGP_ATTR_PARSE_ERROR = -1,
148 BGP_ATTR_PARSE_WITHDRAW = -2,
David Lamparterf57000c2014-06-04 01:01:10 +0200149
150 /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
151 BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
Paul Jakmab881c702010-11-23 16:35:42 +0000152} bgp_attr_parse_ret_t;
153
paul718e3742002-12-13 20:15:29 +0000154/* Prototypes. */
paul94f2b392005-06-28 12:44:16 +0000155extern void bgp_attr_init (void);
Chris Caputo228da422009-07-18 05:44:03 +0000156extern void bgp_attr_finish (void);
Paul Jakmab881c702010-11-23 16:35:42 +0000157extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
158 bgp_size_t, struct bgp_nlri *,
159 struct bgp_nlri *);
Paul Jakmafb982c22007-05-04 20:15:47 +0000160extern struct attr_extra *bgp_attr_extra_get (struct attr *);
161extern void bgp_attr_extra_free (struct attr *);
162extern void bgp_attr_dup (struct attr *, struct attr *);
paul94f2b392005-06-28 12:44:16 +0000163extern struct attr *bgp_attr_intern (struct attr *attr);
Paul Jakmab881c702010-11-23 16:35:42 +0000164extern void bgp_attr_unintern_sub (struct attr *);
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000165extern void bgp_attr_unintern (struct attr **);
paul94f2b392005-06-28 12:44:16 +0000166extern void bgp_attr_flush (struct attr *);
167extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
168extern struct attr *bgp_attr_default_intern (u_char);
169extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
paul5228ad22004-06-04 17:58:18 +0000170 struct aspath *,
171 struct community *, int as_set);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000172extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
173 struct stream *, struct attr *,
174 struct prefix *, afi_t, safi_t,
175 struct peer *, struct prefix_rd *,
176 u_char *);
paul94f2b392005-06-28 12:44:16 +0000177extern void bgp_dump_routes_attr (struct stream *, struct attr *,
178 struct prefix *);
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100179extern int attrhash_cmp (const void *, const void *);
Paul Jakma923de652007-04-29 18:25:17 +0000180extern unsigned int attrhash_key_make (void *);
paul94f2b392005-06-28 12:44:16 +0000181extern void attr_show_all (struct vty *);
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000182extern unsigned long int attr_count (void);
183extern unsigned long int attr_unknown_count (void);
paul718e3742002-12-13 20:15:29 +0000184
185/* Cluster list prototypes. */
paul94f2b392005-06-28 12:44:16 +0000186extern int cluster_loop_check (struct cluster_list *, struct in_addr);
187extern void cluster_unintern (struct cluster_list *);
paul718e3742002-12-13 20:15:29 +0000188
189/* Transit attribute prototypes. */
190void transit_unintern (struct transit *);
paul00d252c2005-05-23 14:19:54 +0000191
Paul Jakma835315b2012-01-18 12:28:30 +0000192/* Below exported for unit-test purposes only */
193struct bgp_attr_parser_args {
194 struct peer *peer;
195 bgp_size_t length; /* attribute data length; */
196 bgp_size_t total; /* total length, inc header */
197 struct attr *attr;
198 u_int8_t type;
199 u_int8_t flags;
200 u_char *startp;
201};
202extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
203 struct bgp_nlri *);
204extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
205 struct bgp_nlri *);
Paul Jakma03292802008-06-07 20:37:10 +0000206
Lou Bergerc3741782016-01-12 13:42:01 -0500207extern struct bgp_attr_encap_subtlv *
208encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
209
210extern void
211bgp_attr_flush_encap(struct attr *attr);
212
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000213/**
214 * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
215 * Typical call sequence is to call _start(), followed by multiple _prefix(),
216 * one for each NLRI that needs to be encoded into the UPDATE message, and
217 * finally the _end() function.
218 */
219extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
220 struct attr *attr);
221extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
222 struct prefix *p, struct prefix_rd *prd,
223 u_char *tag);
Lou Berger050defe2016-01-12 13:41:59 -0500224extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
225 struct prefix *p);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000226extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
227
228extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
229 safi_t safi);
230extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
231 afi_t afi, safi_t safi, struct prefix_rd *prd,
232 u_char *tag);
233extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
234
paul00d252c2005-05-23 14:19:54 +0000235#endif /* _QUAGGA_BGP_ATTR_H */