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