xref: /openbsd-src/usr.sbin/bgpd/mrt.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: mrt.h,v 1.31 2011/09/19 11:19:32 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #ifndef __MRT_H__
19 #define __MRT_H__
20 
21 /*
22  * MRT binary packet format
23  * For more info see:
24  * draft-ietf-grow-mrt-11.txt, "MRT routing information export format"
25  * http://www.quagga.net/docs/docs-multi/Packet-Binary-Dump-Format.html
26  */
27 
28 /*
29  * MRT header:
30  * +--------+--------+--------+--------+
31  * |             timestamp             |
32  * +--------+--------+--------+--------+
33  * |      type       |     subtype     |
34  * +--------+--------+--------+--------+
35  * |               length              | length of packet excluding this header
36  * +--------+--------+--------+--------+
37  *
38  * ET types include an additional 32bit microsecond field coming after the
39  * length field. Which is accounted in the length field.
40  */
41 #define MRT_HEADER_SIZE		12
42 
43 struct mrt_hdr {
44 	u_int32_t	timestamp;
45 	u_int16_t	type;
46 	u_int16_t	subtype;
47 	u_int32_t	length;
48 } __packed;
49 
50 enum MRT_MSG_TYPES {
51 	MSG_NULL,		/*  0 empty msg (deprecated) */
52 	MSG_START,		/*  1 sender is starting up */
53 	MSG_DIE,		/*  2 receiver should shut down (deprecated) */
54 	MSG_I_AM_DEAD,		/*  3 sender is shutting down */
55 	MSG_PEER_DOWN,		/*  4 sender's peer is down (deprecated) */
56 	MSG_PROTOCOL_BGP,	/*  5 msg is a BGP packet (deprecated) */
57 	MSG_PROTOCOL_RIP,	/*  6 msg is a RIP packet */
58 	MSG_PROTOCOL_IDRP,	/*  7 msg is an IDRP packet (deprecated) */
59 	MSG_PROTOCOL_RIPNG,	/*  8 msg is a RIPNG packet */
60 	MSG_PROTOCOL_BGP4PLUS,	/*  9 msg is a BGP4+ packet (deprecated) */
61 	MSG_PROTOCOL_BGP4PLUS1,	/* 10 msg is a BGP4+ (draft 01) (deprecated) */
62 	MSG_PROTOCOL_OSPF,	/* 11 msg is an OSPF packet */
63 	MSG_TABLE_DUMP,		/* 12 routing table dump */
64 	MSG_TABLE_DUMP_V2,	/* 13 routing table dump */
65 	MSG_PROTOCOL_BGP4MP=16,	/* 16 zebras own packet format */
66 	MSG_PROTOCOL_BGP4MP_ET=17,
67 	MSG_PROTOCOL_ISIS=32,	/* 32 msg is a ISIS package */
68 	MSG_PROTOCOL_ISIS_ET=33,
69 	MSG_PROTOCOL_OSPFV3=48,	/* 48 msg is a OSPFv3 package */
70 	MSG_PROTOCOL_OSPFV3_ET=49
71 };
72 
73 /*
74  * Main zebra dump format is in MSG_PROTOCOL_BGP4MP exceptions are table dumps
75  * that are normaly saved as MSG_TABLE_DUMP.
76  * In most cases this is the format to choose to dump updates et al.
77  */
78 enum MRT_BGP4MP_SUBTYPES {
79 	BGP4MP_STATE_CHANGE,	/* state change */
80 	BGP4MP_MESSAGE,		/* bgp message */
81 	BGP4MP_ENTRY,		/* table dumps (deprecated) */
82 	BGP4MP_SNAPSHOT,	/* file name for dump (deprecated) */
83 	BGP4MP_MESSAGE_AS4,	/* same as BGP4MP_MESSAGE with 4byte AS */
84 	BGP4MP_STATE_CHANGE_AS4,
85 	BGP4MP_MESSAGE_LOCAL,	  /* same as BGP4MP_MESSAGE but for self */
86 	BGP4MP_MESSAGE_AS4_LOCAL  /* originated updates. Not implemented */
87 };
88 
89 /* size of the BGP4MP headers without payload */
90 #define MRT_BGP4MP_IPv4_HEADER_SIZE	16
91 #define MRT_BGP4MP_IPv6_HEADER_SIZE	40
92 /* 4-byte AS variants of the previous */
93 #define MRT_BGP4MP_AS4_IPv4_HEADER_SIZE	20
94 #define MRT_BGP4MP_AS4_IPv6_HEADER_SIZE	44
95 
96 /* If the type is PROTOCOL_BGP4MP and the subtype is either BGP4MP_STATE_CHANGE
97  * or BGP4MP_MESSAGE the message consists of a common header plus the payload.
98  * Header format:
99  *
100  * +--------+--------+--------+--------+
101  * |    source_as    |     dest_as     |
102  * +--------+--------+--------+--------+
103  * |    if_index     |       afi       |
104  * +--------+--------+--------+--------+
105  * |             source_ip             |
106  * +--------+--------+--------+--------+
107  * |              dest_ip              |
108  * +--------+--------+--------+--------+
109  * |      message specific payload ...
110  * +--------+--------+--------+--------+
111  *
112  * The source_ip and dest_ip are dependant of the afi type. For IPv6 source_ip
113  * and dest_ip are both 16 bytes long.
114  * For the AS4 types the source_as and dest_as numbers are both 4 bytes long.
115  *
116  * Payload of a BGP4MP_STATE_CHANGE packet:
117  *
118  * +--------+--------+--------+--------+
119  * |    old_state    |    new_state    |
120  * +--------+--------+--------+--------+
121  *
122  * The state values are the same as in RFC 1771.
123  *
124  * The payload of a BGP4MP_MESSAGE is the full bgp message with header.
125  */
126 
127 /*
128  * size of the BGP4MP entries without variable stuff.
129  * All until nexthop plus attr_len, not included plen, prefix and bgp attrs.
130  */
131 #define MRT_BGP4MP_IPv4_ENTRY_SIZE	18
132 #define MRT_BGP4MP_IPv6_ENTRY_SIZE	30
133 #define MRT_BGP4MP_MAX_PREFIXLEN	17
134 /*
135  * The "new" table dump format consists of messages of type PROTOCOL_BGP4MP
136  * and subtype BGP4MP_ENTRY.
137  *
138  * +--------+--------+--------+--------+
139  * |      view       |     status      |
140  * +--------+--------+--------+--------+
141  * |            originated             |
142  * +--------+--------+--------+--------+
143  * |       afi       |  safi  | nhlen  |
144  * +--------+--------+--------+--------+
145  * |              nexthop              |
146  * +--------+--------+--------+--------+
147  * |  plen  |  prefix variable  ...    |
148  * +--------+--------+--------+--------+
149  * |    attr_len     | bgp attributes
150  * +--------+--------+--------+--------+
151  *  bgp attributes, attr_len bytes long
152  * +--------+--------+--------+--------+
153  *   ...                      |
154  * +--------+--------+--------+
155  *
156  * View is normaly 0 and originated the time of last change.
157  * The status seems to be 1 by default but probably something to indicate
158  * the status of a prefix would be more useful.
159  * The format of the nexthop address is defined via the afi value. For IPv6
160  * the nexthop field is 16 bytes long.
161  * The prefix field is as in the bgp update message variable length. It is
162  * plen bits long but rounded to the next octet.
163  */
164 
165 /*
166  * New MRT dump format MSG_TABLE_DUMP_V2, the dump is implemented with
167  * sub-tables for peers and NLRI entries just use the index into the peer
168  * table.
169  */
170 enum MRT_DUMP_V2_SUBTYPES {
171 	MRT_DUMP_V2_PEER_INDEX_TABLE=1,
172 	MRT_DUMP_V2_RIB_IPV4_UNICAST=2,
173 	MRT_DUMP_V2_RIB_IPV4_MULTICAST=3,
174 	MRT_DUMP_V2_RIB_IPV6_UNICAST=4,
175 	MRT_DUMP_V2_RIB_IPV6_MULTICAST=5,
176 	MRT_DUMP_V2_RIB_GENERIC=6
177 };
178 
179 /*
180  * Format of the MRT_DUMP_V2_PEER_INDEX_TABLE:
181  * If there is no view_name, view_name_len must be set to 0
182  *
183  * +--------+--------+--------+--------+
184  * |         collector_bgp_id          |
185  * +--------+--------+--------+--------+
186  * |  view_name_len  |    view_name
187  * +--------+--------+--------+--------+
188  *        view_name (variable) ...     |
189  * +--------+--------+--------+--------+
190  * |   peer_count    |   peer_entries
191  * +--------+--------+--------+--------+
192  *       peer_entries (variable) ...
193  * +--------+--------+--------+--------+
194  *
195  * The format of a peer_entry is the following:
196  *
197  * +--------+
198  * |  type  |
199  * +--------+--------+--------+--------+
200  * |            peer_bgp_id            |
201  * +--------+--------+--------+--------+
202  * |       peer_ip_addr (variable)     |
203  * +--------+--------+--------+--------+
204  * |            peer_as (variable)     |
205  * +--------+--------+--------+--------+
206  *
207  * The message is packed a bit strangely. The type byte defines what size
208  * the peer addr and peer AS have.
209  * The position of a peer in the PEER_INDEX_TABLE is used as the index for
210  * the other messages.
211  */
212 #define MRT_DUMP_V2_PEER_BIT_I	0x1	/* set for IPv6 addrs */
213 #define MRT_DUMP_V2_PEER_BIT_A	0x2	/* set for 32 bits AS number */
214 
215 /*
216  * AFI/SAFI specific RIB Subtypes are special to save a few bytes.
217  *
218  * +--------+--------+--------+--------+
219  * |              seq_num              |
220  * +--------+--------+--------+--------+
221  * |  plen  |  prefix (variable)
222  * +--------+--------+--------+--------+
223  * |     #entry      | rib entries (variable)
224  * +--------+--------+--------+--------+
225  *
226  * The RIB_GENERIC subtype is needed for the less common AFI/SAFI pairs
227  *
228  * +--------+--------+--------+--------+
229  * |              seq_num              |
230  * +--------+--------+--------+--------+
231  * |       AFI       |  SAFI  |  NLRI
232  * +--------+--------+--------+--------+
233  *     NLRI (variable) ...
234  * +--------+--------+--------+--------+
235  * |     #entry      | rib entries (variable)
236  * +--------+--------+--------+--------+
237  */
238 
239 /*
240  * The RIB entries have the following form.
241  *
242  * +--------+--------+
243  * |   peer index    |
244  * +--------+--------+--------+--------+
245  * |          originated_time          |
246  * +--------+--------+--------+--------+
247  * |    attr_len     |   bgp_attrs
248  * +--------+--------+--------+--------+
249  *      bgp_attrs (variable) ...
250  * +--------+--------+--------+--------+
251  *
252  * Some BGP path attributes need special encoding:
253  *  - the AS_PATH attribute MUST be encoded as 4-Byte AS
254  *  - the MP_REACH_NLRI only consists of the nexthop len and nexthop address
255  */
256 
257 /*
258  * Format for routing table dumps in "old" mrt format.
259  * Type MSG_TABLE_DUMP and subtype is AFI_IPv4 (1) for IPv4 and AFI_IPv6 (2)
260  * for IPv6. In the IPv6 case prefix and peer_ip are both 16 bytes long.
261  *
262  * +--------+--------+--------+--------+
263  * |      view       |      seqnum     |
264  * +--------+--------+--------+--------+
265  * |               prefix              |
266  * +--------+--------+--------+--------+
267  * |  plen  | status | originated time
268  * +--------+--------+--------+--------+
269  *   originated time |     peer_ip
270  * +--------+--------+--------+--------+
271  *       peer_ip     |     peer_as     |
272  * +--------+--------+--------+--------+
273  * |    attr_len     | bgp attributes
274  * +--------+--------+--------+--------+
275  *  bgp attributes, attr_len bytes long
276  * +--------+--------+--------+--------+
277  *   ...                      |
278  * +--------+--------+--------+
279  *
280  *
281  * View is normaly 0 and seqnum just a simple counter for this dump.
282  * The status field is unused and should be set to 1.
283  */
284 
285 enum MRT_DUMP_SUBTYPES {
286 	MRT_DUMP_AFI_IP=1,
287 	MRT_DUMP_AFI_IPv6=2
288 };
289 
290 /* size of the dump header until attr_len */
291 #define MRT_DUMP_HEADER_SIZE	22
292 #define MRT_DUMP_HEADER_SIZE_V6	46
293 
294 /*
295  * OLD MRT message headers. These structs are here for completion but
296  * will not be used to generate dumps. It seems that nobody uses those.
297  *
298  * Only for bgp messages (type 5, 9 and 10)
299  * Nota bene for bgp dumps MSG_PROTOCOL_BGP4MP should be used.
300  */
301 enum MRT_BGP_SUBTYPES {
302 	MSG_BGP_NULL,
303 	MSG_BGP_UPDATE,		/* raw update packet (contains both withdraws
304 				   and announcements) */
305 	MSG_BGP_PREF_UPDATE,	/* tlv preferences followed by raw update */
306 	MSG_BGP_STATE_CHANGE,	/* state change */
307 	MSG_BGP_SYNC,		/* file name for a table dump */
308 	MSG_BGP_OPEN,		/* BGP open messages */
309 	MSG_BGP_NOTIFY,		/* BGP notify messages */
310 	MSG_BGP_KEEPALIVE	/* BGP keepalives */
311 };
312 
313 /* if type MSG_PROTOCOL_BGP and subtype MSG_BGP_UPDATE, MSG_BGP_OPEN,
314  * MSG_BGP_NOTIFY or MSG_BGP_KEEPALIVE
315  *
316  * +--------+--------+--------+--------+
317  * |    source_as    |    source_ip
318  * +--------+--------+--------+--------+
319  *      source_ip    |    dest_as      |
320  * +--------+--------+--------+--------+
321  * |               dest_ip             |
322  * +--------+--------+--------+--------+
323  * | bgp update packet with header et
324  * +--------+--------+--------+--------+
325  *   al. (variable length) ...
326  * +--------+--------+--------+--------+
327  *
328  * For IPv6 the type is MSG_PROTOCOL_BGP4PLUS and the subtype remains
329  * MSG_BGP_UPDATE. The source_ip and dest_ip are again extended to 16 bytes.
330  *
331  * For subtype MSG_BGP_STATE_CHANGE (for all BGP types or just for the
332  * MSG_PROTOCOL_BGP4PLUS case? Unclear.)
333  *
334  * +--------+--------+--------+--------+
335  * |    source_as    |    source_ip
336  * +--------+--------+--------+--------+
337  *      source_ip    |    old_state    |
338  * +--------+--------+--------+--------+
339  * |    new_state    |
340  * +--------+--------+
341  *
342  * States are defined in RFC 1771/4271.
343  */
344 
345 /*
346  * if type MSG_PROTOCOL_BGP and subtype MSG_BGP_SYNC OR
347  * if type MSG_PROTOCOL_BGP4MP and subtype BGP4MP_SNAPSHOT
348  * *DEPRECATED*
349  *
350  * +--------+--------+--------+--------+
351  * |      view       |    filename
352  * +--------+--------+--------+--------+
353  *    filename variable length zero
354  * +--------+--------+--------+--------+
355  *    terminated ... |   0    |
356  * +--------+--------+--------+
357  */
358 #endif
359