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_FORMATS_H
29 #define _BGPDUMP_FORMATS_H
30 
31 #include "bgpdump_attr.h"
32 
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <string.h>
36 #include <netinet/in.h>
37 
38 /* type and subtypes values */
39 /* RFC6396 */
40 #define BGPDUMP_TYPE_MRTD_BGP			5
41 #define BGPDUMP_SUBTYPE_MRTD_BGP_NULL		0
42 #define BGPDUMP_SUBTYPE_MRTD_BGP_UPDATE		1
43 #define BGPDUMP_SUBTYPE_MRTD_BGP_PREFUPDATE	2
44 #define BGPDUMP_SUBTYPE_MRTD_BGP_STATE_CHANGE	3
45 #define BGPDUMP_SUBTYPE_MRTD_BGP_SYNC		4
46 #define BGPDUMP_SUBTYPE_MRTD_BGP_OPEN		5
47 #define BGPDUMP_SUBTYPE_MRTD_BGP_NOTIFICATION	6
48 #define BGPDUMP_SUBTYPE_MRTD_BGP_KEEPALIVE	7
49 #define BGPDUMP_SUBTYPE_MRTD_BGP_ROUT_REFRESH	133
50 
51 #define BGPDUMP_TYPE_MRTD_TABLE_DUMP				12
52 #define BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP			1
53 #define BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6			2
54 #define BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS		3
55 #define BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS	4
56 
57 #define BGPDUMP_TYPE_TABLE_DUMP_V2                       13
58 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_PEER_INDEX_TABLE    1
59 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_UNICAST    2
60 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_MULTICAST  3
61 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_UNICAST    4
62 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_MULTICAST  5
63 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_GENERIC         6
64 #define BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AFI_IP             0
65 #define BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AFI_IP6            1
66 #define BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AS2                0
67 #define BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AS4                2
68 #define BGPDUMP_TYPE_TABLE_DUMP_V2_MAX_VIEWNAME_LEN     255
69 
70 /* Zebra record types */
71 #define BGPDUMP_TYPE_ZEBRA_BGP			16 /* MSG_PROTOCOL_BGP4MP */
72 #define BGPDUMP_TYPE_ZEBRA_BGP_ET       17 /* MSG_PROTOCOL_BGP4MP_ET */
73 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE	0  /* BGP4MP_STATE_CHANGE */
74 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE	1  /* BGP4MP_MESSAGE */
75 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_ENTRY		2  /* BGP4MP_ENTRY */
76 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_SNAPSHOT	3  /* BGP4MP_SNAPSHOT */
77 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4	4  /* BGP4MP_MESSAGE_AS4 */
78 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE_AS4	5  /* BGP4MP_STATE_CHANGE_AS4 */
79 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL	6  /* BGP4MP_MESSAGE_LOCAL */
80 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL	7  /* BGP4MP_MESSAGE_AS4_LOCAL */
81 
82 /* RFC8050 add-path extensions */
83 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_ADDPATH            8   /* BGP4MP_MESSAGE_ADDPATH */
84 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_ADDPATH        9   /* BGP4MP_MESSAGE_AS4_ADDPATH */
85 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL_ADDPATH      10  /* BGP4MP_MESSAGE_LOCAL_ADDPATH */
86 #define BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL_ADDPATH  11  /* BGP4MP_MESSAGE_AS4_LOCAL */
87 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_UNICAST_ADDPATH    8    /* RIB_IPV4_UNICAST_ADDPATH */
88 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_MULTICAST_ADDPATH  9    /* RIB_IPV4_MULTICAST_ADDPATH */
89 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_UNICAST_ADDPATH    10   /* RIB_IPV6_UNICAST_ADDPATH */
90 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_MULTICAST_ADDPATH  11   /* RIB_IPV6_MULTICAST_ADDPATH */
91 #define BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_GENERIC_ADDPATH         12   /* RIB_GENERIC_ADDPATH */
92 
93 /* BGP state - defined in RFC1771 */
94 #define BGP_STATE_IDLE		1
95 #define BGP_STATE_CONNECT	2
96 #define BGP_STATE_ACTIVE	3
97 #define BGP_STATE_OPENSENT	4
98 #define BGP_STATE_OPENCONFIRM	5
99 #define BGP_STATE_ESTABLISHED	6
100 
101 /* BGP message types */
102 #define	BGP_MSG_OPEN		           1
103 #define	BGP_MSG_UPDATE		           2
104 #define	BGP_MSG_NOTIFY		           3
105 #define	BGP_MSG_KEEPALIVE	           4
106 #define BGP_MSG_ROUTE_REFRESH_01           5
107 #define BGP_MSG_ROUTE_REFRESH	         128
108 
109 typedef struct struct_BGPDUMP_MRTD_TABLE_DUMP {
110     u_int16_t		view;
111     u_int16_t		sequence;
112     BGPDUMP_IP_ADDRESS	prefix;
113     u_char		mask;
114     u_char		status;
115     time_t		uptime;
116     BGPDUMP_IP_ADDRESS	peer_ip;
117     as_t		peer_as;
118     u_int16_t		attr_len;
119 } BGPDUMP_MRTD_TABLE_DUMP;
120 
121 
122 typedef struct struct_BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE_ENTRY {
123 	u_char              afi;
124 	BGPDUMP_IP_ADDRESS  peer_ip;
125 	struct in_addr      peer_bgp_id;
126 	as_t                peer_as;
127 } BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE_ENTRY;
128 
129 typedef struct struct_BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE {
130 	struct in_addr      local_bgp_id;
131 	char                view_name[BGPDUMP_TYPE_TABLE_DUMP_V2_MAX_VIEWNAME_LEN];
132 	uint16_t            peer_count;
133 	BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE_ENTRY  *entries;
134 } BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE;
135 
136 typedef struct struct_BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY {
137 	uint16_t            peer_index;
138 	uint32_t            originated_time;
139     pathid_t            path_id;
140 	BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE_ENTRY *peer;
141         attributes_t        *attr;
142 } BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY;
143 
144 typedef struct struct_BGPDUMP_TABLE_DUMP_V2_PREFIX {
145 	uint32_t            seq;
146 	uint16_t            afi;
147 	uint8_t             safi;
148 	u_char              prefix_length;
149 	BGPDUMP_IP_ADDRESS  prefix;
150 	uint16_t            entry_count;
151 	BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY *entries;
152 } BGPDUMP_TABLE_DUMP_V2_PREFIX;
153 
154 
155 
156 /* For Zebra BGP4MP_STATE_CHANGE */
157 typedef struct struct_BGPDUMP_ZEBRA_STATE_CHANGE {
158     as_t		source_as;
159     as_t		destination_as;
160     u_int16_t		interface_index;
161     u_int16_t		address_family;
162     BGPDUMP_IP_ADDRESS	source_ip;
163     BGPDUMP_IP_ADDRESS	destination_ip;
164     u_int16_t		old_state;
165     u_int16_t		new_state;
166 } BGPDUMP_ZEBRA_STATE_CHANGE;
167 
168 struct zebra_incomplete {
169     u_int16_t afi;
170     u_int8_t orig_len;
171     struct prefix prefix;
172 };
173 
174 /* For Zebra BGP4MP_MESSAGE */
175 typedef struct struct_BGPDUMP_ZEBRA_MESSAGE {
176     /* Zebra header */
177     as_t		source_as;
178     as_t		destination_as;
179     u_int16_t		interface_index;
180     u_int16_t		address_family;
181     BGPDUMP_IP_ADDRESS	source_ip;
182     BGPDUMP_IP_ADDRESS	destination_ip;
183 
184     /* BGP packet header fields */
185     u_int16_t		size;
186     u_char		type;
187 
188     /* For OPEN packets */
189     u_char	version;
190     as_t	my_as;
191     u_int16_t	hold_time;
192     struct	in_addr bgp_id;
193     u_char	opt_len;
194     u_char	*opt_data;
195 
196     /* For UPDATE packets */
197     u_int16_t		withdraw_count;
198     u_int16_t		announce_count;
199     struct prefix	withdraw[MAX_PREFIXES];
200     struct prefix	announce[MAX_PREFIXES];
201 
202     /* For corrupt update dumps */
203     u_int16_t cut_bytes;
204     struct zebra_incomplete incomplete;
205 
206     /* For NOTIFY packets */
207     u_char error_code;
208     u_char sub_error_code;
209     u_int16_t notify_len;
210     u_char *notify_data;
211 
212 } BGPDUMP_ZEBRA_MESSAGE;
213 
214 /* For Zebra BGP4MP_ENTRY */
215 typedef struct struct_BGPDUMP_ZEBRA_ENTRY {
216     u_int16_t	view;
217     u_int16_t	status;
218     time_t	time_last_change;
219     u_int16_t	address_family;
220     u_char	SAFI;
221     u_char	next_hop_len;
222     u_char	prefix_length;
223     u_char	*address_prefix;
224     u_int16_t	empty;
225     u_char	*bgp_atribute;
226 } BGPDUMP_ZEBRA_ENTRY;
227 
228 /* For Zebra BGP4MP_SNAPSHOT */
229 typedef struct struct_BGPDUMP_ZEBRA_SNAPSHOT {
230     u_int16_t	view;
231     u_int16_t	file;
232 } BGPDUMP_ZEBRA_SNAPSHOT;
233 
234 typedef struct struct_BGPDUMP_MRTD_MESSAGE {
235     u_int16_t		source_as;
236     struct in_addr	source_ip;
237     u_int16_t		destination_as;
238     struct in_addr	destination_ip;
239 
240     u_int16_t		withdraw_count;
241     u_int16_t		announce_count;
242     struct prefix	withdraw[MAX_PREFIXES];
243     struct prefix	announce[MAX_PREFIXES];
244 
245     /* For corrupt update dumps */
246     struct zebra_incomplete incomplete;
247 } BGPDUMP_MRTD_MESSAGE;
248 
249 typedef struct struct_BGPDUMP_MRTD_STATE_CHANGE {
250     u_int16_t           destination_as;
251     struct in_addr      destination_ip;
252     u_int16_t           old_state;
253     u_int16_t           new_state;
254 } BGPDUMP_MRTD_STATE_CHANGE;
255 
256 typedef union union_BGPDUMP_BODY {
257 	BGPDUMP_MRTD_MESSAGE		mrtd_message;
258         BGPDUMP_MRTD_STATE_CHANGE       mrtd_state_change;
259 	BGPDUMP_MRTD_TABLE_DUMP		mrtd_table_dump;
260 	BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE		mrtd_table_dump_v2_peer_table;
261 	BGPDUMP_TABLE_DUMP_V2_PREFIX		mrtd_table_dump_v2_prefix;
262 	BGPDUMP_ZEBRA_STATE_CHANGE	zebra_state_change;
263 	BGPDUMP_ZEBRA_MESSAGE		zebra_message;
264 	BGPDUMP_ZEBRA_ENTRY		zebra_entry;
265 	BGPDUMP_ZEBRA_SNAPSHOT		zebra_snapshot;
266 } BGPDUMP_BODY;
267 
268 /* The MRT header. Common to all records. */
269 typedef struct struct_BGPDUMP_ENTRY {
270     time_t time;
271     long ms;
272     u_int16_t type;
273     u_int16_t subtype;
274     u_int32_t length;
275     attributes_t *attr;
276     BGPDUMP_BODY body;
277     struct struct_BGPDUMP * dump;
278 } BGPDUMP_ENTRY;
279 
280 #endif
281