1 /*
2  Copyright (c) 2007 - 2010 RIPE NCC - All Rights Reserved
3 
4  Permission to use, copy, modify, and distribute this software and its
5  documentation for any purpose and without fee is hereby granted, provided
6  that the above copyright notice appear in all copies and that both that
7  copyright notice and this permission notice appear in supporting
8  documentation, and that the name of the author not be used in advertising or
9  publicity pertaining to distribution of the software without specific,
10  written prior permission.
11 
12  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
14  AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
15  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
16  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 
19 Parts of this code have been engineered after analiyzing GNU Zebra's
20 source code and therefore might contain declarations/code from GNU
21 Zebra, Copyright (C) 1999 Kunihiro Ishiguro. Zebra is a free routing
22 software, distributed under the GNU General Public License. A copy of
23 this license is included with libbgpdump.
24 
25 Original Author: Dan Ardelean (dan@ripe.net)
26 */
27 
28 #ifndef _BGPDUMP_ATTR_H
29 #define _BGPDUMP_ATTR_H
30 
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 
34 /* BGP Attribute flags. */
35 #define BGP_ATTR_FLAG_OPTIONAL  0x80	/* Attribute is optional. */
36 #define BGP_ATTR_FLAG_TRANS     0x40	/* Attribute is transitive. */
37 #define BGP_ATTR_FLAG_PARTIAL   0x20	/* Attribute is partial. */
38 #define BGP_ATTR_FLAG_EXTLEN    0x10	/* Extended length flag. */
39 
40 /* BGP attribute type codes.  */
41 #define BGP_ATTR_ORIGIN                    1
42 #define BGP_ATTR_AS_PATH                   2
43 #define BGP_ATTR_NEXT_HOP                  3
44 #define BGP_ATTR_MULTI_EXIT_DISC           4
45 #define BGP_ATTR_LOCAL_PREF                5
46 #define BGP_ATTR_ATOMIC_AGGREGATE          6
47 #define BGP_ATTR_AGGREGATOR                7
48 #define BGP_ATTR_COMMUNITIES               8
49 #define BGP_ATTR_ORIGINATOR_ID             9
50 #define BGP_ATTR_CLUSTER_LIST             10
51 #define BGP_ATTR_DPA                      11
52 #define BGP_ATTR_ADVERTISER               12
53 #define BGP_ATTR_RCID_PATH                13
54 #define BGP_ATTR_MP_REACH_NLRI            14
55 #define BGP_ATTR_MP_UNREACH_NLRI          15
56 #define BGP_ATTR_EXT_COMMUNITIES          16
57 #define BGP_ATTR_NEW_AS_PATH              17
58 #define BGP_ATTR_NEW_AGGREGATOR           18
59 #define BGP_ATTR_LARGE_COMMUNITIES        32
60 
61 /* Flag macro */
62 #define ATTR_FLAG_BIT(X)  (1 << ((X) - 1))
63 
64 /* BGP ASPATH attribute defines */
65 #define AS_HEADER_SIZE        2
66 
67 #define AS_SET             1
68 #define AS_SEQUENCE        2
69 #define AS_CONFED_SEQUENCE 3
70 #define AS_CONFED_SET      4
71 
72 #define AS_SEG_START 0
73 #define AS_SEG_END 1
74 
75 #define ASPATH_STR_DEFAULT_LEN 32
76 #define ASPATH_STR_ERROR       "! Error !"
77 
78 /* BGP COMMUNITY attribute defines */
79 
80 #define COMMUNITY_NO_EXPORT             0xFFFFFF01
81 #define COMMUNITY_NO_ADVERTISE          0xFFFFFF02
82 #define COMMUNITY_NO_EXPORT_SUBCONFED   0xFFFFFF03
83 #define COMMUNITY_LOCAL_AS              0xFFFFFF03
84 
85 #define com_nthval(X,n)  ((X)->val + (n))
86 
87 /* MP-BGP address families */
88 #ifdef BGPDUMP_HAVE_IPV6
89 #define AFI_IP 1
90 #define AFI_IP6 2
91 #define BGPDUMP_MAX_AFI AFI_IP6
92 #else
93 #define AFI_IP 1
94 #define BGPDUMP_MAX_AFI AFI_IP
95 #endif
96 
97 #define SAFI_UNICAST		1
98 #define SAFI_MULTICAST		2
99 #define SAFI_UNICAST_MULTICAST	3
100 #define BGPDUMP_MAX_SAFI SAFI_UNICAST_MULTICAST
101 
102 struct unknown_attr
103 {
104 	int	flag;
105 	int	type;
106 	int	len;
107 	u_char *raw;
108 };
109 
110 typedef u_int32_t as_t;
111 typedef u_int32_t pathid_t;
112 
113 typedef struct attr attributes_t;
114 struct attr
115 {
116   /* Flag of attribute is set or not. */
117   u_int32_t flag;
118 
119   /* Attributes. */
120   int                   origin;
121   struct in_addr 	nexthop;
122   u_int32_t 		med;
123   u_int32_t 		local_pref;
124   as_t 			aggregator_as;
125   struct in_addr 	aggregator_addr;
126   u_int32_t 		weight;
127   struct in_addr 	originator_id;
128   struct cluster_list	*cluster;
129 
130   struct aspath 	*aspath;
131   struct community 	*community;
132   struct ecommunity 	*ecommunity;
133   struct lcommunity   *lcommunity;
134   struct transit 	*transit;
135 
136   /* libbgpdump additions */
137 
138   struct mp_info	*mp_info;
139   u_int16_t		len;
140   caddr_t		data;
141 
142   u_int16_t		unknown_num;
143   struct unknown_attr	*unknown;
144 
145   /* ASN32 support */
146   struct aspath 	*new_aspath;
147   struct aspath 	*old_aspath;
148   as_t			new_aggregator_as;
149   as_t			old_aggregator_as;
150   struct in_addr 	new_aggregator_addr;
151   struct in_addr 	old_aggregator_addr;
152 };
153 
154 struct community
155 {
156   int 			size;
157   u_int32_t 		*val;
158   char			*str;
159 };
160 
161 struct lcommunity
162 {
163   int       size;
164   u_int32_t *val;
165   char      *str;
166 };
167 
168 struct cluster_list
169 {
170   int			length;
171   struct in_addr 	*list;
172 };
173 
174 struct transit
175 {
176   int 			length;
177   u_char 		*val;
178 };
179 
180 struct aspath
181 {
182   u_int8_t		asn_len;
183   int 			length;
184   int 			count;
185   caddr_t 		data;
186   char 			*str;
187 };
188 
189 struct assegment
190 {
191   u_char type;
192   u_char length;
193   char data[0];
194 };
195 
196 struct mp_info {
197   /* AFI and SAFI start from 1, so the arrays must be 1-based */
198   struct mp_nlri	*withdraw[BGPDUMP_MAX_AFI+1][BGPDUMP_MAX_SAFI+1];
199   struct mp_nlri	*announce[BGPDUMP_MAX_AFI+1][BGPDUMP_MAX_SAFI+1];
200 };
201 
202 #ifdef BGPDUMP_HAVE_IPV6
203 #define MP_IPV6_ANNOUNCE(m) ((m)->announce[AFI_IP6][SAFI_UNICAST])
204 #define MP_IPV6_WITHDRAW(m) ((m)->withdraw[AFI_IP6][SAFI_UNICAST])
205 #endif
206 
207 typedef union union_BGPDUMP_IP_ADDRESS {
208     struct in_addr	v4_addr;
209     struct in6_addr	v6_addr;
210 } BGPDUMP_IP_ADDRESS;
211 
212 
213 #define BGPDUMP_ADDRSTRLEN 46
214 
215 #define ASN16_LEN sizeof(u_int16_t)
216 #define ASN32_LEN sizeof(u_int32_t)
217 
218 #define AS_TRAN 23456
219 
220 struct prefix {
221     BGPDUMP_IP_ADDRESS	address;
222     u_char		len;
223     pathid_t    path_id;
224 };
225 
226 #define MAX_PREFIXES 2050
227 struct mp_nlri {
228   u_char		nexthop_len;
229 
230   BGPDUMP_IP_ADDRESS	nexthop;
231   BGPDUMP_IP_ADDRESS 	nexthop_local;
232 
233   u_int16_t		prefix_count;
234   struct prefix		nlri[MAX_PREFIXES];
235 };
236 
237 #endif /* _BGPDUMP_ATTR_H */
238