1 /* $OpenBSD: mrt.h,v 1.38 2022/12/28 21:30:16 jmc 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 uint32_t timestamp; 46 uint16_t type; 47 uint16_t subtype; 48 uint32_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 normally 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 BGP4MP_MESSAGE_ADDPATH, /* same as above but for add-path peers */ 89 BGP4MP_MESSAGE_AS4_ADDPATH, 90 BGP4MP_MESSAGE_LOCAL_ADDPATH, 91 BGP4MP_MESSAGE_AS4_LOCAL_ADDPATH, 92 }; 93 94 /* size of the BGP4MP headers without payload */ 95 #define MRT_BGP4MP_IPv4_HEADER_SIZE 16 96 #define MRT_BGP4MP_IPv6_HEADER_SIZE 40 97 #define MRT_BGP4MP_ET_IPv4_HEADER_SIZE 20 98 #define MRT_BGP4MP_ET_IPv6_HEADER_SIZE 44 99 /* 4-byte AS variants of the previous */ 100 #define MRT_BGP4MP_AS4_IPv4_HEADER_SIZE 20 101 #define MRT_BGP4MP_AS4_IPv6_HEADER_SIZE 44 102 #define MRT_BGP4MP_ET_AS4_IPv4_HEADER_SIZE 24 103 #define MRT_BGP4MP_ET_AS4_IPv6_HEADER_SIZE 48 104 105 /* If the type is PROTOCOL_BGP4MP and the subtype is either BGP4MP_STATE_CHANGE 106 * or BGP4MP_MESSAGE the message consists of a common header plus the payload. 107 * Header format: 108 * 109 * +--------+--------+--------+--------+ 110 * | source_as | dest_as | 111 * +--------+--------+--------+--------+ 112 * | if_index | afi | 113 * +--------+--------+--------+--------+ 114 * | source_ip | 115 * +--------+--------+--------+--------+ 116 * | dest_ip | 117 * +--------+--------+--------+--------+ 118 * | message specific payload ... 119 * +--------+--------+--------+--------+ 120 * 121 * The source_ip and dest_ip are dependant of the afi type. For IPv6 source_ip 122 * and dest_ip are both 16 bytes long. 123 * For the AS4 types the source_as and dest_as numbers are both 4 bytes long. 124 * 125 * Payload of a BGP4MP_STATE_CHANGE packet: 126 * 127 * +--------+--------+--------+--------+ 128 * | old_state | new_state | 129 * +--------+--------+--------+--------+ 130 * 131 * The state values are the same as in RFC 1771. 132 * 133 * The payload of a BGP4MP_MESSAGE is the full bgp message with header. 134 */ 135 136 /* 137 * size of the BGP4MP entries without variable stuff. 138 * All until nexthop plus attr_len, not included plen, prefix and bgp attrs. 139 */ 140 #define MRT_BGP4MP_IPv4_ENTRY_SIZE 18 141 #define MRT_BGP4MP_IPv6_ENTRY_SIZE 30 142 #define MRT_BGP4MP_MAX_PREFIXLEN 256 143 /* 144 * The "new" table dump format consists of messages of type PROTOCOL_BGP4MP 145 * and subtype BGP4MP_ENTRY. 146 * 147 * +--------+--------+--------+--------+ 148 * | view | status | 149 * +--------+--------+--------+--------+ 150 * | originated | 151 * +--------+--------+--------+--------+ 152 * | afi | safi | nhlen | 153 * +--------+--------+--------+--------+ 154 * | nexthop | 155 * +--------+--------+--------+--------+ 156 * | plen | prefix variable ... | 157 * +--------+--------+--------+--------+ 158 * | attr_len | bgp attributes 159 * +--------+--------+--------+--------+ 160 * bgp attributes, attr_len bytes long 161 * +--------+--------+--------+--------+ 162 * ... | 163 * +--------+--------+--------+ 164 * 165 * View is normally 0 and originated the time of last change. 166 * The status seems to be 1 by default but probably something to indicate 167 * the status of a prefix would be more useful. 168 * The format of the nexthop address is defined via the afi value. For IPv6 169 * the nexthop field is 16 bytes long. 170 * The prefix field is as in the bgp update message variable length. It is 171 * plen bits long but rounded to the next octet. 172 */ 173 174 /* 175 * New MRT dump format MSG_TABLE_DUMP_V2, the dump is implemented with 176 * sub-tables for peers and NLRI entries just use the index into the peer 177 * table. 178 */ 179 enum MRT_DUMP_V2_SUBTYPES { 180 MRT_DUMP_V2_PEER_INDEX_TABLE=1, 181 MRT_DUMP_V2_RIB_IPV4_UNICAST=2, 182 MRT_DUMP_V2_RIB_IPV4_MULTICAST=3, 183 MRT_DUMP_V2_RIB_IPV6_UNICAST=4, 184 MRT_DUMP_V2_RIB_IPV6_MULTICAST=5, 185 MRT_DUMP_V2_RIB_GENERIC=6, 186 MRT_DUMP_V2_RIB_IPV4_UNICAST_ADDPATH=8, 187 MRT_DUMP_V2_RIB_IPV4_MULTICAST_ADDPATH=9, 188 MRT_DUMP_V2_RIB_IPV6_UNICAST_ADDPATH=10, 189 MRT_DUMP_V2_RIB_IPV6_MULTICAST_ADDPATH=11, 190 MRT_DUMP_V2_RIB_GENERIC_ADDPATH=12, 191 }; 192 193 /* 194 * Format of the MRT_DUMP_V2_PEER_INDEX_TABLE: 195 * If there is no view_name, view_name_len must be set to 0 196 * 197 * +--------+--------+--------+--------+ 198 * | collector_bgp_id | 199 * +--------+--------+--------+--------+ 200 * | view_name_len | view_name 201 * +--------+--------+--------+--------+ 202 * view_name (variable) ... | 203 * +--------+--------+--------+--------+ 204 * | peer_count | peer_entries 205 * +--------+--------+--------+--------+ 206 * peer_entries (variable) ... 207 * +--------+--------+--------+--------+ 208 * 209 * The format of a peer_entry is the following: 210 * 211 * +--------+ 212 * | type | 213 * +--------+--------+--------+--------+ 214 * | peer_bgp_id | 215 * +--------+--------+--------+--------+ 216 * | peer_ip_addr (variable) | 217 * +--------+--------+--------+--------+ 218 * | peer_as (variable) | 219 * +--------+--------+--------+--------+ 220 * 221 * The message is packed a bit strangely. The type byte defines what size 222 * the peer addr and peer AS have. 223 * The position of a peer in the PEER_INDEX_TABLE is used as the index for 224 * the other messages. 225 */ 226 #define MRT_DUMP_V2_PEER_BIT_I 0x1 /* set for IPv6 addrs */ 227 #define MRT_DUMP_V2_PEER_BIT_A 0x2 /* set for 32 bits AS number */ 228 229 /* 230 * AFI/SAFI specific RIB Subtypes are special to save a few bytes. 231 * 232 * +--------+--------+--------+--------+ 233 * | seq_num | 234 * +--------+--------+--------+--------+ 235 * | plen | prefix (variable) 236 * +--------+--------+--------+--------+ 237 * | #entry | rib entries (variable) 238 * +--------+--------+--------+--------+ 239 * 240 * The RIB_GENERIC subtype is needed for the less common AFI/SAFI pairs. 241 * 242 * +--------+--------+--------+--------+ 243 * | seq_num | 244 * +--------+--------+--------+--------+ 245 * | AFI | SAFI | NLRI 246 * +--------+--------+--------+--------+ 247 * NLRI (variable) ... 248 * +--------+--------+--------+--------+ 249 * | #entry | rib entries (variable) 250 * +--------+--------+--------+--------+ 251 */ 252 253 /* 254 * The RIB entries have the following form. 255 * 256 * +--------+--------+ 257 * | peer index | 258 * +--------+--------+--------+--------+ 259 * | originated_time | 260 * +--------+--------+--------+--------+ 261 * [ path_id in _ADDPATH variants ] 262 * +--------+--------+--------+--------+ 263 * | attr_len | bgp_attrs 264 * +--------+--------+--------+--------+ 265 * bgp_attrs (variable) ... 266 * +--------+--------+--------+--------+ 267 * 268 * Some BGP path attributes need special encoding: 269 * - the AS_PATH attribute MUST be encoded as 4-Byte AS 270 * - the MP_REACH_NLRI only consists of the nexthop len and nexthop address 271 * 272 * The non generic ADDPATH variants add the path-identifier between 273 * originated_time and attr_len. For RIB_GENERIC_ADDPATH the path_id should 274 * be part of the NLRI. 275 */ 276 277 /* 278 * Format for routing table dumps in "old" mrt format. 279 * Type MSG_TABLE_DUMP and subtype is AFI_IPv4 (1) for IPv4 and AFI_IPv6 (2) 280 * for IPv6. In the IPv6 case prefix and peer_ip are both 16 bytes long. 281 * 282 * +--------+--------+--------+--------+ 283 * | view | seqnum | 284 * +--------+--------+--------+--------+ 285 * | prefix | 286 * +--------+--------+--------+--------+ 287 * | plen | status | originated time 288 * +--------+--------+--------+--------+ 289 * originated time | peer_ip 290 * +--------+--------+--------+--------+ 291 * peer_ip | peer_as | 292 * +--------+--------+--------+--------+ 293 * | attr_len | bgp attributes 294 * +--------+--------+--------+--------+ 295 * bgp attributes, attr_len bytes long 296 * +--------+--------+--------+--------+ 297 * ... | 298 * +--------+--------+--------+ 299 * 300 * 301 * View is normally 0 and seqnum just a simple counter for this dump. 302 * The status field is unused and should be set to 1. 303 */ 304 305 enum MRT_DUMP_SUBTYPES { 306 MRT_DUMP_AFI_IP=1, 307 MRT_DUMP_AFI_IPv6=2 308 }; 309 310 /* size of the dump header until attr_len */ 311 #define MRT_DUMP_HEADER_SIZE 22 312 #define MRT_DUMP_HEADER_SIZE_V6 46 313 314 /* 315 * OLD MRT message headers. These structs are here for completion but 316 * will not be used to generate dumps. It seems that nobody uses those. 317 * 318 * Only for bgp messages (type 5, 9 and 10) 319 * Nota bene for bgp dumps MSG_PROTOCOL_BGP4MP should be used. 320 */ 321 enum MRT_BGP_SUBTYPES { 322 MSG_BGP_NULL, 323 MSG_BGP_UPDATE, /* raw update packet (contains both withdraws 324 and announcements) */ 325 MSG_BGP_PREF_UPDATE, /* tlv preferences followed by raw update */ 326 MSG_BGP_STATE_CHANGE, /* state change */ 327 MSG_BGP_SYNC, /* file name for a table dump */ 328 MSG_BGP_OPEN, /* BGP open messages */ 329 MSG_BGP_NOTIFY, /* BGP notify messages */ 330 MSG_BGP_KEEPALIVE /* BGP keepalives */ 331 }; 332 333 /* if type MSG_PROTOCOL_BGP and subtype MSG_BGP_UPDATE, MSG_BGP_OPEN, 334 * MSG_BGP_NOTIFY or MSG_BGP_KEEPALIVE 335 * 336 * +--------+--------+--------+--------+ 337 * | source_as | source_ip 338 * +--------+--------+--------+--------+ 339 * source_ip | dest_as | 340 * +--------+--------+--------+--------+ 341 * | dest_ip | 342 * +--------+--------+--------+--------+ 343 * | bgp update packet with header et 344 * +--------+--------+--------+--------+ 345 * al. (variable length) ... 346 * +--------+--------+--------+--------+ 347 * 348 * For IPv6 the type is MSG_PROTOCOL_BGP4PLUS and the subtype remains 349 * MSG_BGP_UPDATE. The source_ip and dest_ip are again extended to 16 bytes. 350 * 351 * For subtype MSG_BGP_STATE_CHANGE (for all BGP types or just for the 352 * MSG_PROTOCOL_BGP4PLUS case? Unclear.) 353 * 354 * +--------+--------+--------+--------+ 355 * | source_as | source_ip 356 * +--------+--------+--------+--------+ 357 * source_ip | old_state | 358 * +--------+--------+--------+--------+ 359 * | new_state | 360 * +--------+--------+ 361 * 362 * States are defined in RFC 1771/4271. 363 */ 364 365 /* 366 * if type MSG_PROTOCOL_BGP and subtype MSG_BGP_SYNC OR 367 * if type MSG_PROTOCOL_BGP4MP and subtype BGP4MP_SNAPSHOT 368 * *DEPRECATED* 369 * 370 * +--------+--------+--------+--------+ 371 * | view | filename 372 * +--------+--------+--------+--------+ 373 * filename variable length zero 374 * +--------+--------+--------+--------+ 375 * terminated ... | 0 | 376 * +--------+--------+--------+ 377 */ 378 #endif 379