xref: /openbsd/usr.sbin/bgpd/mrt.h (revision 8932bfb7)
1 /*	$OpenBSD: mrt.h,v 1.27 2010/06/04 10:13:00 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 comming after the
39  * length field.
40  */
41 #define MRT_HEADER_SIZE		12
42 
43 enum MRT_MSG_TYPES {
44 	MSG_NULL,		/*  0 empty msg (deprecated) */
45 	MSG_START,		/*  1 sender is starting up */
46 	MSG_DIE,		/*  2 receiver should shut down (deprecated) */
47 	MSG_I_AM_DEAD,		/*  3 sender is shutting down */
48 	MSG_PEER_DOWN,		/*  4 sender's peer is down (deprecated) */
49 	MSG_PROTOCOL_BGP,	/*  5 msg is a BGP packet (deprecated) */
50 	MSG_PROTOCOL_RIP,	/*  6 msg is a RIP packet */
51 	MSG_PROTOCOL_IDRP,	/*  7 msg is an IDRP packet (deprecated) */
52 	MSG_PROTOCOL_RIPNG,	/*  8 msg is a RIPNG packet */
53 	MSG_PROTOCOL_BGP4PLUS,	/*  9 msg is a BGP4+ packet (deprecated) */
54 	MSG_PROTOCOL_BGP4PLUS1,	/* 10 msg is a BGP4+ (draft 01) (deprecated) */
55 	MSG_PROTOCOL_OSPF,	/* 11 msg is an OSPF packet */
56 	MSG_TABLE_DUMP,		/* 12 routing table dump */
57 	MSG_TABLE_DUMP_V2,	/* 13 routing table dump */
58 	MSG_PROTOCOL_BGP4MP=16,	/* 16 zebras own packet format */
59 	MSG_PROTOCOL_BGP4MP_ET=17,
60 	MSG_PROTOCOL_ISIS=32,	/* 32 msg is a ISIS package */
61 	MSG_PROTOCOL_ISIS_ET=33,
62 	MSG_PROTOCOL_OSPFV3=48,	/* 48 msg is a OSPFv3 package */
63 	MSG_PROTOCOL_OSPFV3_ET=49
64 };
65 
66 /*
67  * Main zebra dump format is in MSG_PROTOCOL_BGP4MP exceptions are table dumps
68  * that are normaly saved as MSG_TABLE_DUMP.
69  * In most cases this is the format to choose to dump updates et al.
70  */
71 enum MRT_BGP4MP_TYPES {
72 	BGP4MP_STATE_CHANGE,	/* state change */
73 	BGP4MP_MESSAGE,		/* bgp message */
74 	BGP4MP_ENTRY,		/* table dumps (deprecated) */
75 	BGP4MP_SNAPSHOT,	/* file name for dump (deprecated) */
76 	BGP4MP_MESSAGE_AS4,	/* same as BGP4MP_MESSAGE with 4byte AS */
77 	BGP4MP_STATE_CHANGE_AS4,
78 	BGP4MP_MESSAGE_LOCAL,	  /* same as BGP4MP_MESSAGE but for self */
79 	BGP4MP_MESSAGE_AS4_LOCAL  /* originated updates. Not implemented */
80 };
81 
82 /* size of the BGP4MP headers without payload */
83 #define MRT_BGP4MP_IPv4_HEADER_SIZE	16
84 #define MRT_BGP4MP_IPv6_HEADER_SIZE	40
85 /* 4-byte AS variants of the previous */
86 #define MRT_BGP4MP_AS4_IPv4_HEADER_SIZE	20
87 #define MRT_BGP4MP_AS4_IPv6_HEADER_SIZE	44
88 
89 /* If the type is PROTOCOL_BGP4MP and the subtype is either BGP4MP_STATE_CHANGE
90  * or BGP4MP_MESSAGE the message consists of a common header plus the payload.
91  * Header format:
92  *
93  * +--------+--------+--------+--------+
94  * |    source_as    |     dest_as     |
95  * +--------+--------+--------+--------+
96  * |    if_index     |       afi       |
97  * +--------+--------+--------+--------+
98  * |             source_ip             |
99  * +--------+--------+--------+--------+
100  * |              dest_ip              |
101  * +--------+--------+--------+--------+
102  * |      message specific payload ...
103  * +--------+--------+--------+--------+
104  *
105  * The source_ip and dest_ip are dependant of the afi type. For IPv6 source_ip
106  * and dest_ip are both 16 bytes long.
107  *
108  * Payload of a BGP4MP_STATE_CHANGE packet:
109  *
110  * +--------+--------+--------+--------+
111  * |    old_state    |    new_state    |
112  * +--------+--------+--------+--------+
113  *
114  * The state values are the same as in RFC 1771.
115  *
116  * The payload of a BGP4MP_MESSAGE is the full bgp message with header.
117  */
118 
119 /*
120  * size of the BGP4MP entries without variable stuff.
121  * All until nexthop plus attr_len, not included plen, prefix and bgp attrs.
122  */
123 #define MRT_BGP4MP_IPv4_ENTRY_SIZE	18
124 #define MRT_BGP4MP_IPv6_ENTRY_SIZE	30
125 #define MRT_BGP4MP_MAX_PREFIXLEN	17
126 /*
127  * The "new" table dump format consists of messages of type PROTOCOL_BGP4MP
128  * and subtype BGP4MP_ENTRY.
129  *
130  * +--------+--------+--------+--------+
131  * |      view       |     status      |
132  * +--------+--------+--------+--------+
133  * |            originated             |
134  * +--------+--------+--------+--------+
135  * |       afi       |  safi  | nhlen  |
136  * +--------+--------+--------+--------+
137  * |              nexthop              |
138  * +--------+--------+--------+--------+
139  * |  plen  |  prefix variable  ...    |
140  * +--------+--------+--------+--------+
141  * |    attr_len     | bgp attributes
142  * +--------+--------+--------+--------+
143  *  bgp attributes, attr_len bytes long
144  * +--------+--------+--------+--------+
145  *   ...                      |
146  * +--------+--------+--------+
147  *
148  * View is normaly 0 and originated the time of last change.
149  * The status seems to be 1 by default but probably something to indicate
150  * the status of a prefix would be more useful.
151  * The format of the nexthop address is defined via the afi value. For IPv6
152  * the nexthop field is 16 bytes long.
153  * The prefix field is as in the bgp update message variable length. It is
154  * plen bits long but rounded to the next octet.
155  */
156 
157 /*
158  * Format for routing table dumps in "old" mrt format.
159  * Type MSG_TABLE_DUMP and subtype is AFI_IPv4 (1) for IPv4 and AFI_IPv6 (2)
160  * for IPv6. In the IPv6 case prefix and peer_ip are both 16 bytes long.
161  *
162  * +--------+--------+--------+--------+
163  * |      view       |      seqnum     |
164  * +--------+--------+--------+--------+
165  * |               prefix              |
166  * +--------+--------+--------+--------+
167  * |  plen  | status | originated time
168  * +--------+--------+--------+--------+
169  *   originated time |     peer_ip
170  * +--------+--------+--------+--------+
171  *       peer_ip     |     peer_as     |
172  * +--------+--------+--------+--------+
173  * |    attr_len     | bgp attributes
174  * +--------+--------+--------+--------+
175  *  bgp attributes, attr_len bytes long
176  * +--------+--------+--------+--------+
177  *   ...                      |
178  * +--------+--------+--------+
179  *
180  *
181  * View is normaly 0 and seqnum just a simple counter for this dump.
182  * The status field is unused and should be set to 1.
183  */
184 
185 /* size of the dump header until attr_len */
186 #define MRT_DUMP_HEADER_SIZE	22
187 #define MRT_DUMP_HEADER_SIZE_V6	46
188 
189 /*
190  * OLD MRT message headers. These structs are here for completion but
191  * will not be used to generate dumps. It seems that nobody uses those.
192  *
193  * Only for bgp messages (type 5, 9 and 10)
194  * Nota bene for bgp dumps MSG_PROTOCOL_BGP4MP should be used.
195  */
196 enum MRT_BGP_TYPES {
197 	MSG_BGP_NULL,
198 	MSG_BGP_UPDATE,		/* raw update packet (contains both withdraws
199 				   and announcements) */
200 	MSG_BGP_PREF_UPDATE,	/* tlv preferences followed by raw update */
201 	MSG_BGP_STATE_CHANGE,	/* state change */
202 	MSG_BGP_SYNC,		/* file name for a table dump */
203 	MSG_BGP_OPEN,		/* BGP open messages */
204 	MSG_BGP_NOTIFY,		/* BGP notify messages */
205 	MSG_BGP_KEEPALIVE	/* BGP keepalives */
206 };
207 
208 /* if type MSG_PROTOCOL_BGP and subtype MSG_BGP_UPDATE, MSG_BGP_OPEN,
209  * MSG_BGP_NOTIFY or MSG_BGP_KEEPALIVE
210  *
211  * +--------+--------+--------+--------+
212  * |    source_as    |    source_ip
213  * +--------+--------+--------+--------+
214  *      source_ip    |    dest_as      |
215  * +--------+--------+--------+--------+
216  * |               dest_ip             |
217  * +--------+--------+--------+--------+
218  * | bgp update packet with header et
219  * +--------+--------+--------+--------+
220  *   al. (variable length) ...
221  * +--------+--------+--------+--------+
222  *
223  * For IPv6 the type is MSG_PROTOCOL_BGP4PLUS and the subtype remains
224  * MSG_BGP_UPDATE. The source_ip and dest_ip are again extended to 16 bytes.
225  */
226 
227 /*
228  * For subtype MSG_BGP_STATECHANGE (for all BGP types or just for the
229  * MSG_PROTOCOL_BGP4PLUS case? Unclear.)
230  *
231  * +--------+--------+--------+--------+
232  * |    source_as    |    source_ip
233  * +--------+--------+--------+--------+
234  *      source_ip    |    old_state    |
235  * +--------+--------+--------+--------+
236  * |    new_state    |
237  * +--------+--------+
238  *
239  * State are defined in RFC 1771/4271.
240  */
241 
242 /*
243  * if type MSG_PROTOCOL_BGP and subtype MSG_BGP_SYNC OR
244  * if type MSG_PROTOCOL_BGP4MP and subtype BGP4MP_SNAPSHOT
245  * *DEPRECATED*
246  *
247  * +--------+--------+--------+--------+
248  * |      view       |    filename
249  * +--------+--------+--------+--------+
250  *    filename variable length zero
251  * +--------+--------+--------+--------+
252  *    terminated ... |   0    |
253  * +--------+--------+--------+
254  */
255 
256 #define	MRT_FILE_LEN	512
257 enum mrt_type {
258 	MRT_NONE,
259 	MRT_TABLE_DUMP,
260 	MRT_TABLE_DUMP_MP,
261 	MRT_ALL_IN,
262 	MRT_ALL_OUT,
263 	MRT_UPDATE_IN,
264 	MRT_UPDATE_OUT
265 };
266 
267 enum mrt_state {
268 	MRT_STATE_RUNNING,
269 	MRT_STATE_OPEN,
270 	MRT_STATE_REOPEN,
271 	MRT_STATE_REMOVE
272 };
273 
274 struct mrt {
275 	char			rib[PEER_DESCR_LEN];
276 	struct msgbuf		wbuf;
277 	LIST_ENTRY(mrt)		entry;
278 	u_int32_t		peer_id;
279 	u_int32_t		group_id;
280 	enum mrt_type		type;
281 	enum mrt_state		state;
282 	u_int16_t		seqnum;
283 };
284 
285 struct mrt_config {
286 	struct mrt		conf;
287 	char			name[MRT_FILE_LEN];	/* base file name */
288 	char			file[MRT_FILE_LEN];	/* actual file name */
289 	time_t			ReopenTimer;
290 	time_t			ReopenTimerInterval;
291 };
292 
293 #define	MRT2MC(x)	((struct mrt_config *)(x))
294 #define	MRT_MAX_TIMEOUT	7200
295 
296 struct peer;
297 struct prefix;
298 struct rib_entry;
299 
300 /* prototypes */
301 void		 mrt_dump_bgp_msg(struct mrt *, void *, u_int16_t,
302 		     struct peer *);
303 void		 mrt_dump_state(struct mrt *, u_int16_t, u_int16_t,
304 		     struct peer *);
305 void		 mrt_clear_seq(void);
306 void		 mrt_dump_upcall(struct rib_entry *, void *);
307 void		 mrt_done(void *);
308 void		 mrt_write(struct mrt *);
309 void		 mrt_clean(struct mrt *);
310 void		 mrt_init(struct imsgbuf *, struct imsgbuf *);
311 int		 mrt_timeout(struct mrt_head *);
312 void		 mrt_reconfigure(struct mrt_head *);
313 void		 mrt_handler(struct mrt_head *);
314 struct mrt	*mrt_get(struct mrt_head *, struct mrt *);
315 int		 mrt_mergeconfig(struct mrt_head *, struct mrt_head *);
316 
317 #endif
318