]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
add support for the ATTR SET attribute per draft-marques-ppvpn-ibgp
[tcpdump] / print-bgp.c
1 /*
2 * Copyright (C) 1999 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
30 * complete BGP support.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #ifndef lint
38 static const char rcsid[] _U_ =
39 "@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.75 2003-12-23 22:07:56 hannes Exp $";
40 #endif
41
42 #include <tcpdump-stdinc.h>
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h"
50
51 struct bgp {
52 u_int8_t bgp_marker[16];
53 u_int16_t bgp_len;
54 u_int8_t bgp_type;
55 };
56 #define BGP_SIZE 19 /* unaligned */
57
58 #define BGP_OPEN 1
59 #define BGP_UPDATE 2
60 #define BGP_NOTIFICATION 3
61 #define BGP_KEEPALIVE 4
62 #define BGP_ROUTE_REFRESH 5
63
64 static struct tok bgp_msg_values[] = {
65 { BGP_OPEN, "Open"},
66 { BGP_UPDATE, "Update"},
67 { BGP_NOTIFICATION, "Notification"},
68 { BGP_KEEPALIVE, "Keepalive"},
69 { BGP_ROUTE_REFRESH, "Route Refresh"},
70 { 0, NULL}
71 };
72
73 struct bgp_open {
74 u_int8_t bgpo_marker[16];
75 u_int16_t bgpo_len;
76 u_int8_t bgpo_type;
77 u_int8_t bgpo_version;
78 u_int16_t bgpo_myas;
79 u_int16_t bgpo_holdtime;
80 u_int32_t bgpo_id;
81 u_int8_t bgpo_optlen;
82 /* options should follow */
83 };
84 #define BGP_OPEN_SIZE 29 /* unaligned */
85
86 struct bgp_opt {
87 u_int8_t bgpopt_type;
88 u_int8_t bgpopt_len;
89 /* variable length */
90 };
91 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
92
93 struct bgp_notification {
94 u_int8_t bgpn_marker[16];
95 u_int16_t bgpn_len;
96 u_int8_t bgpn_type;
97 u_int8_t bgpn_major;
98 u_int8_t bgpn_minor;
99 };
100 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
101
102 struct bgp_route_refresh {
103 u_int8_t bgp_marker[16];
104 u_int16_t len;
105 u_int8_t type;
106 u_int8_t afi[2]; /* the compiler messes this structure up */
107 u_int8_t res; /* when doing misaligned sequences of int8 and int16 */
108 u_int8_t safi; /* afi should be int16 - so we have to access it using */
109 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
110 #define BGP_ROUTE_REFRESH_SIZE 23
111
112 struct bgp_attr {
113 u_int8_t bgpa_flags;
114 u_int8_t bgpa_type;
115 union {
116 u_int8_t len;
117 u_int16_t elen;
118 } bgpa_len;
119 #define bgp_attr_len(p) \
120 (((p)->bgpa_flags & 0x10) ? \
121 EXTRACT_16BITS(&(p)->bgpa_len.elen) : (p)->bgpa_len.len)
122 #define bgp_attr_off(p) \
123 (((p)->bgpa_flags & 0x10) ? 4 : 3)
124 };
125
126 #define BGPTYPE_ORIGIN 1
127 #define BGPTYPE_AS_PATH 2
128 #define BGPTYPE_NEXT_HOP 3
129 #define BGPTYPE_MULTI_EXIT_DISC 4
130 #define BGPTYPE_LOCAL_PREF 5
131 #define BGPTYPE_ATOMIC_AGGREGATE 6
132 #define BGPTYPE_AGGREGATOR 7
133 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
134 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
135 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
136 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
137 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
138 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
139 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
140 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
141 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
142 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */
143
144 static struct tok bgp_attr_values[] = {
145 { BGPTYPE_ORIGIN, "Origin"},
146 { BGPTYPE_AS_PATH, "AS Path"},
147 { BGPTYPE_NEXT_HOP, "Next Hop"},
148 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
149 { BGPTYPE_LOCAL_PREF, "Local Preference"},
150 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
151 { BGPTYPE_AGGREGATOR, "Aggregator"},
152 { BGPTYPE_COMMUNITIES, "Community"},
153 { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
154 { BGPTYPE_CLUSTER_LIST, "Cluster List"},
155 { BGPTYPE_DPA, "DPA"},
156 { BGPTYPE_ADVERTISERS, "Advertisers"},
157 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
158 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
159 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
160 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
161 { BGPTYPE_ATTR_SET, "Attribute Set"},
162 { 255, "Reserved for development"},
163 { 0, NULL}
164 };
165
166 #define BGP_OPT_AUTH 1
167 #define BGP_OPT_CAP 2
168
169
170 static struct tok bgp_opt_values[] = {
171 { BGP_OPT_AUTH, "Authentication Information"},
172 { BGP_OPT_CAP, "Capabilities Advertisement"},
173 { 0, NULL}
174 };
175
176 #define BGP_CAPCODE_MP 1
177 #define BGP_CAPCODE_RR 2
178 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */
179 #define BGP_CAPCODE_RR_CISCO 128
180
181 static struct tok bgp_capcode_values[] = {
182 { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
183 { BGP_CAPCODE_RR, "Route Refresh"},
184 { BGP_CAPCODE_RESTART, "Graceful Restart"},
185 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
186 { 0, NULL}
187 };
188
189 #define BGP_NOTIFY_MAJOR_MSG 1
190 #define BGP_NOTIFY_MAJOR_OPEN 2
191 #define BGP_NOTIFY_MAJOR_UPDATE 3
192 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
193 #define BGP_NOTIFY_MAJOR_FSM 5
194 #define BGP_NOTIFY_MAJOR_CEASE 6
195 #define BGP_NOTIFY_MAJOR_CAP 7
196
197 static struct tok bgp_notify_major_values[] = {
198 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
199 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
200 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
201 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
202 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
203 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
204 { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"},
205 { 0, NULL}
206 };
207
208 /* draft-ietf-idr-cease-subcode-02 */
209 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
210 static struct tok bgp_notify_minor_cease_values[] = {
211 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"},
212 { 2, "Administratively Shutdown"},
213 { 3, "Peer Unconfigured"},
214 { 4, "Administratively Reset"},
215 { 5, "Connection Rejected"},
216 { 6, "Other Configuration Change"},
217 { 7, "Connection Collision Resolution"},
218 { 0, NULL}
219 };
220
221 static struct tok bgp_notify_minor_msg_values[] = {
222 { 1, "Connection Not Synchronized"},
223 { 2, "Bad Message Length"},
224 { 3, "Bad Message Type"},
225 { 0, NULL}
226 };
227
228 static struct tok bgp_notify_minor_open_values[] = {
229 { 1, "Unsupported Version Number"},
230 { 2, "Bad Peer AS"},
231 { 3, "Bad BGP Identifier"},
232 { 4, "Unsupported Optional Parameter"},
233 { 5, "Authentication Failure"},
234 { 6, "Unacceptable Hold Time"},
235 { 0, NULL}
236 };
237
238 static struct tok bgp_notify_minor_update_values[] = {
239 { 1, "Malformed Attribute List"},
240 { 2, "Unrecognized Well-known Attribute"},
241 { 3, "Missing Well-known Attribute"},
242 { 4, "Attribute Flags Error"},
243 { 5, "Attribute Length Error"},
244 { 6, "Invalid ORIGIN Attribute"},
245 { 7, "AS Routing Loop"},
246 { 8, "Invalid NEXT_HOP Attribute"},
247 { 9, "Optional Attribute Error"},
248 { 10, "Invalid Network Field"},
249 { 11, "Malformed AS_PATH"},
250 { 0, NULL}
251 };
252
253 static struct tok bgp_notify_minor_cap_values[] = {
254 { 1, "Invalid Action Value" },
255 { 2, "Invalid Capability Length" },
256 { 3, "Malformed Capability Value" },
257 { 4, "Unsupported Capability Code" },
258 { 0, NULL }
259 };
260
261 static struct tok bgp_origin_values[] = {
262 { 0, "IGP"},
263 { 1, "EGP"},
264 { 2, "Incomplete"},
265 { 0, NULL}
266 };
267
268 /* Subsequent address family identifier, RFC2283 section 7 */
269 #define SAFNUM_RES 0
270 #define SAFNUM_UNICAST 1
271 #define SAFNUM_MULTICAST 2
272 #define SAFNUM_UNIMULTICAST 3
273 /* labeled BGP RFC3107 */
274 #define SAFNUM_LABUNICAST 4
275 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
276 #define SAFNUM_VPNUNICAST 128
277 #define SAFNUM_VPNMULTICAST 129
278 #define SAFNUM_VPNUNIMULTICAST 130
279 /* draft-marques-ppvpn-rt-constrain-01.txt */
280 #define SAFNUM_RT_ROUTING_INFO 132
281
282 #define BGP_VPN_RD_LEN 8
283
284 static struct tok bgp_safi_values[] = {
285 { SAFNUM_RES, "Reserved"},
286 { SAFNUM_UNICAST, "Unicast"},
287 { SAFNUM_MULTICAST, "Multicast"},
288 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
289 { SAFNUM_LABUNICAST, "labeled Unicast"},
290 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
291 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
292 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
293 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"}, /* draft-marques-ppvpn-rt-constrain-01.txt */
294 { 0, NULL }
295 };
296
297 /* well-known community */
298 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
299 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
300 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
301
302 /* RFC1700 address family numbers */
303 #define AFNUM_INET 1
304 #define AFNUM_INET6 2
305 #define AFNUM_NSAP 3
306 #define AFNUM_HDLC 4
307 #define AFNUM_BBN1822 5
308 #define AFNUM_802 6
309 #define AFNUM_E163 7
310 #define AFNUM_E164 8
311 #define AFNUM_F69 9
312 #define AFNUM_X121 10
313 #define AFNUM_IPX 11
314 #define AFNUM_ATALK 12
315 #define AFNUM_DECNET 13
316 #define AFNUM_BANYAN 14
317 #define AFNUM_E164NSAP 15
318 /* draft-kompella-ppvpn-l2vpn */
319 #define AFNUM_L2VPN 196 /* still to be approved by IANA */
320
321 static struct tok bgp_afi_values[] = {
322 { 0, "Reserved"},
323 { AFNUM_INET, "IPv4"},
324 { AFNUM_INET6, "IPv6"},
325 { AFNUM_NSAP, "NSAP"},
326 { AFNUM_HDLC, "HDLC"},
327 { AFNUM_BBN1822, "BBN 1822"},
328 { AFNUM_802, "802"},
329 { AFNUM_E163, "E.163"},
330 { AFNUM_E164, "E.164"},
331 { AFNUM_F69, "F.69"},
332 { AFNUM_X121, "X.121"},
333 { AFNUM_IPX, "Novell IPX"},
334 { AFNUM_ATALK, "Appletalk"},
335 { AFNUM_DECNET, "Decnet IV"},
336 { AFNUM_BANYAN, "Banyan Vines"},
337 { AFNUM_E164NSAP, "E.164 with NSAP subaddress"},
338 { AFNUM_L2VPN, "Layer-2 VPN"},
339 { 0, NULL},
340 };
341
342 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
343 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
344 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
345 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
346 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
347 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
348 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
349 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
350 /* rfc2547 bgp-mpls-vpns */
351
352 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
353 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
354 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
355 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
356
357 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
358 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
359
360 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
361 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
362
363 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
364
365 static struct tok bgp_extd_comm_flag_values[] = {
366 { 0x8000, "vendor-specific"},
367 { 0x4000, "non-transitive"},
368 { 0, NULL},
369 };
370
371 static struct tok bgp_extd_comm_subtype_values[] = {
372 { BGP_EXT_COM_RT_0, "target"},
373 { BGP_EXT_COM_RT_1, "target"},
374 { BGP_EXT_COM_RT_2, "target"},
375 { BGP_EXT_COM_RO_0, "origin"},
376 { BGP_EXT_COM_RO_1, "origin"},
377 { BGP_EXT_COM_RO_2, "origin"},
378 { BGP_EXT_COM_LINKBAND, "link-BW"},
379 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"},
380 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"},
381 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"},
382 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"},
383 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"},
384 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"},
385 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"},
386 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"},
387 { BGP_EXT_COM_L2INFO, "layer2-info"},
388 { 0, NULL},
389 };
390
391 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
392 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
393 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
394 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
395 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
396 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
397 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
398 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
399
400 static struct tok bgp_extd_comm_ospf_rtype_values[] = {
401 { BGP_OSPF_RTYPE_RTR, "Router" },
402 { BGP_OSPF_RTYPE_NET, "Network" },
403 { BGP_OSPF_RTYPE_SUM, "Summary" },
404 { BGP_OSPF_RTYPE_EXT, "External" },
405 { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
406 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
407 { 0, NULL },
408 };
409
410 static struct tok bgp_l2vpn_encaps_values[] = {
411 { 0, "Reserved"},
412 { 1, "Frame Relay"},
413 { 2, "ATM AAL5 VCC transport"},
414 { 3, "ATM transparent cell transport"},
415 { 4, "Ethernet VLAN"},
416 { 5, "Ethernet"},
417 { 6, "Cisco-HDLC"},
418 { 7, "PPP"},
419 { 8, "CEM"},
420 { 9, "ATM VCC cell transport"},
421 { 10, "ATM VPC cell transport"},
422 { 11, "MPLS"},
423 { 12, "VPLS"},
424 { 64, "IP-interworking"},
425 { 0, NULL},
426 };
427
428 static int
429 decode_prefix4(const u_char *pptr, char *buf, u_int buflen)
430 {
431 struct in_addr addr;
432 u_int plen;
433
434 plen = pptr[0];
435 if (32 < plen)
436 return -1;
437
438 memset(&addr, 0, sizeof(addr));
439 memcpy(&addr, &pptr[1], (plen + 7) / 8);
440 if (plen % 8) {
441 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
442 ((0xff00 >> (plen % 8)) & 0xff);
443 }
444 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
445 return 1 + (plen + 7) / 8;
446 }
447
448 static int
449 decode_labeled_prefix4(const u_char *pptr, char *buf, u_int buflen)
450 {
451 struct in_addr addr;
452 u_int plen;
453
454 plen = pptr[0]; /* get prefix length */
455
456 /* this is one of the weirdnesses of rfc3107
457 the label length (actually the label + COS bits)
458 is added to the prefix length;
459 we also do only read out just one label -
460 there is no real application for advertisement of
461 stacked labels in a a single BGP message
462 */
463
464 plen-=24; /* adjust prefixlen - labellength */
465
466 if (32 < plen)
467 return -1;
468
469 memset(&addr, 0, sizeof(addr));
470 memcpy(&addr, &pptr[4], (plen + 7) / 8);
471 if (plen % 8) {
472 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
473 ((0xff00 >> (plen % 8)) & 0xff);
474 }
475 /* the label may get offsetted by 4 bits so lets shift it right */
476 snprintf(buf, buflen, "%s/%d, label:%u %s",
477 getname((u_char *)&addr),
478 plen,
479 EXTRACT_24BITS(pptr+1)>>4,
480 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
481
482 return 4 + (plen + 7) / 8;
483 }
484
485 /* RDs and RTs share the same semantics
486 * we use bgp_vpn_rd_print for
487 * printing route targets inside a NLRI */
488 static char *
489 bgp_vpn_rd_print (const u_char *pptr) {
490
491 /* allocate space for the following string
492 * xxx.xxx.xxx.xxx:xxxxx
493 * 21 bytes plus one termination byte */
494 static char rd[22];
495 char *pos = rd;
496
497 /* ok lets load the RD format */
498 switch (EXTRACT_16BITS(pptr)) {
499
500 /* AS:IP-address fmt*/
501 case 0:
502 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u.%u.%u.%u",
503 EXTRACT_16BITS(pptr+2), *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7));
504 break;
505 /* IP-address:AS fmt*/
506
507 case 1:
508 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
509 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
510 break;
511
512 /* 4-byte-AS:number fmt*/
513 case 2:
514 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u",
515 EXTRACT_32BITS(pptr+2), EXTRACT_16BITS(pptr+6));
516 break;
517 default:
518 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
519 break;
520 }
521 pos += strlen(pos);
522 *(pos) = '\0';
523 return (rd);
524 }
525
526 static int
527 decode_rt_routing_info(const u_char *pptr, char *buf, u_int buflen)
528 {
529 u_int8_t route_target[8];
530 u_int plen;
531
532 plen = pptr[0]; /* get prefix length */
533
534 plen-=32; /* adjust prefix length */
535
536 if (0 < plen)
537 return -1;
538
539 memset(&route_target, 0, sizeof(route_target));
540 memcpy(&route_target, &pptr[1], (plen + 7) / 8);
541 if (plen % 8) {
542 ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
543 ((0xff00 >> (plen % 8)) & 0xff);
544 }
545 snprintf(buf, buflen, "origin AS: %u, route target %s",
546 EXTRACT_32BITS(pptr+1),
547 bgp_vpn_rd_print((u_char *)&route_target));
548
549 return 5 + (plen + 7) / 8;
550 }
551
552 static int
553 decode_labeled_vpn_prefix4(const u_char *pptr, char *buf, u_int buflen)
554 {
555 struct in_addr addr;
556 u_int plen;
557
558 plen = pptr[0]; /* get prefix length */
559
560 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
561
562 if (32 < plen)
563 return -1;
564
565 memset(&addr, 0, sizeof(addr));
566 memcpy(&addr, &pptr[12], (plen + 7) / 8);
567 if (plen % 8) {
568 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
569 ((0xff00 >> (plen % 8)) & 0xff);
570 }
571 /* the label may get offsetted by 4 bits so lets shift it right */
572 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
573 bgp_vpn_rd_print(pptr+4),
574 getname((u_char *)&addr),
575 plen,
576 EXTRACT_24BITS(pptr+1)>>4,
577 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
578
579 return 12 + (plen + 7) / 8;
580 }
581
582 static int
583 decode_labeled_vpn_l2(const u_char *pptr, char *buf, u_int buflen)
584 {
585 int plen,tlen,strlen,tlv_type,tlv_len,ttlv_len;
586 plen=EXTRACT_16BITS(pptr);
587 tlen=plen;
588 pptr+=2;
589 strlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
590 bgp_vpn_rd_print(pptr),
591 EXTRACT_16BITS(pptr+8),
592 EXTRACT_16BITS(pptr+10),
593 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
594 pptr+=15;
595 tlen-=15;
596
597 /* ok now the variable part - lets read out TLVs*/
598 while (tlen>0) {
599 tlv_type=*pptr++;
600 tlv_len=EXTRACT_16BITS(pptr);
601 ttlv_len=tlv_len;
602 pptr+=2;
603
604 switch(tlv_type) {
605 case 1:
606 strlen+=snprintf(buf+strlen,buflen-strlen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
607 tlv_type,
608 tlv_len);
609 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
610 while (ttlv_len>0) {
611 strlen+=snprintf(buf+strlen,buflen-strlen, "%02x",*pptr++);
612 ttlv_len--;
613 }
614 break;
615 default:
616 snprintf(buf+strlen,buflen-strlen, "\n\t\tunknown TLV #%u, length: %u",
617 tlv_type,
618 tlv_len);
619 break;
620 }
621 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it tright */
622 }
623 return plen+2;
624 }
625
626 #ifdef INET6
627 static int
628 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
629 {
630 struct in6_addr addr;
631 u_int plen;
632
633 plen = pd[0];
634 if (128 < plen)
635 return -1;
636
637 memset(&addr, 0, sizeof(addr));
638 memcpy(&addr, &pd[1], (plen + 7) / 8);
639 if (plen % 8) {
640 addr.s6_addr[(plen + 7) / 8 - 1] &=
641 ((0xff00 >> (plen % 8)) & 0xff);
642 }
643 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
644 return 1 + (plen + 7) / 8;
645 }
646
647 static int
648 decode_labeled_prefix6(const u_char *pptr, char *buf, u_int buflen)
649 {
650 struct in6_addr addr;
651 u_int plen;
652
653 plen = pptr[0]; /* get prefix length */
654 plen-=24; /* adjust prefixlen - labellength */
655
656 if (128 < plen)
657 return -1;
658
659 memset(&addr, 0, sizeof(addr));
660 memcpy(&addr, &pptr[4], (plen + 7) / 8);
661 if (plen % 8) {
662 addr.s6_addr[(plen + 7) / 8 - 1] &=
663 ((0xff00 >> (plen % 8)) & 0xff);
664 }
665 /* the label may get offsetted by 4 bits so lets shift it right */
666 snprintf(buf, buflen, "%s/%d, label:%u %s",
667 getname6((u_char *)&addr),
668 plen,
669 EXTRACT_24BITS(pptr+1)>>4,
670 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
671
672 return 4 + (plen + 7) / 8;
673 }
674
675 static int
676 decode_labeled_vpn_prefix6(const u_char *pptr, char *buf, u_int buflen)
677 {
678 struct in6_addr addr;
679 u_int plen;
680
681 plen = pptr[0]; /* get prefix length */
682
683 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
684
685 if (128 < plen)
686 return -1;
687
688 memset(&addr, 0, sizeof(addr));
689 memcpy(&addr, &pptr[12], (plen + 7) / 8);
690 if (plen % 8) {
691 addr.s6_addr[(plen + 7) / 8 - 1] &=
692 ((0xff00 >> (plen % 8)) & 0xff);
693 }
694 /* the label may get offsetted by 4 bits so lets shift it right */
695 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
696 bgp_vpn_rd_print(pptr+4),
697 getname6((u_char *)&addr),
698 plen,
699 EXTRACT_24BITS(pptr+1)>>4,
700 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
701
702 return 12 + (plen + 7) / 8;
703 }
704 #endif
705
706 static void
707 bgp_attr_print(const struct bgp_attr *attr, const u_char *pptr, int len)
708 {
709 int i;
710 u_int16_t af;
711 u_int8_t safi, snpa;
712 union { /* copy buffer for bandwidth values */
713 float f;
714 u_int32_t i;
715 } bw;
716 int advance;
717 int tlen;
718 const u_char *tptr;
719 char buf[MAXHOSTNAMELEN + 100];
720
721 tptr = pptr;
722 tlen=len;
723
724 switch (attr->bgpa_type) {
725 case BGPTYPE_ORIGIN:
726 if (len != 1)
727 printf("invalid len");
728 else
729 printf("%s", tok2str(bgp_origin_values, "Unknown Origin Typecode", tptr[0]));
730 break;
731 case BGPTYPE_AS_PATH:
732 if (len % 2) {
733 printf("invalid len");
734 break;
735 }
736 if (!len) {
737 printf("empty");
738 break;
739 }
740 while (tptr < pptr + len) {
741 /*
742 * under RFC1965, p[0] means:
743 * 1: AS_SET 2: AS_SEQUENCE
744 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
745 */
746 if (tptr[0] == 3 || tptr[0] == 4)
747 printf("confed");
748 printf("%s", (tptr[0] & 1) ? "{" : "");
749 for (i = 0; i < tptr[1] * 2; i += 2) {
750 printf("%s%u", i == 0 ? "" : " ",
751 EXTRACT_16BITS(&tptr[2 + i]));
752 }
753 printf("%s", (tptr[0] & 1) ? "}" : "");
754 tptr += 2 + tptr[1] * 2;
755 }
756 break;
757 case BGPTYPE_NEXT_HOP:
758 if (len != 4)
759 printf("invalid len");
760 else
761 printf("%s", getname(tptr));
762 break;
763 case BGPTYPE_MULTI_EXIT_DISC:
764 case BGPTYPE_LOCAL_PREF:
765 if (len != 4)
766 printf("invalid len");
767 else
768 printf("%u", EXTRACT_32BITS(tptr));
769 break;
770 case BGPTYPE_ATOMIC_AGGREGATE:
771 if (len != 0)
772 printf("invalid len");
773 break;
774 case BGPTYPE_AGGREGATOR:
775 if (len != 6) {
776 printf("invalid len");
777 break;
778 }
779 printf(" AS #%u, origin %s", EXTRACT_16BITS(tptr),
780 getname(tptr + 2));
781 break;
782 case BGPTYPE_COMMUNITIES:
783 if (len % 4) {
784 printf("invalid len");
785 break;
786 }
787 while (tlen>0) {
788 u_int32_t comm;
789 comm = EXTRACT_32BITS(tptr);
790 switch (comm) {
791 case BGP_COMMUNITY_NO_EXPORT:
792 printf(" NO_EXPORT");
793 break;
794 case BGP_COMMUNITY_NO_ADVERT:
795 printf(" NO_ADVERTISE");
796 break;
797 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
798 printf(" NO_EXPORT_SUBCONFED");
799 break;
800 default:
801 printf("%u:%u%s",
802 (comm >> 16) & 0xffff,
803 comm & 0xffff,
804 (tlen>4) ? ", " : "");
805 break;
806 }
807 tlen -=4;
808 tptr +=4;
809 }
810 break;
811 case BGPTYPE_ORIGINATOR_ID:
812 if (len != 4) {
813 printf("invalid len");
814 break;
815 }
816 printf("%s",getname(tptr));
817 break;
818 case BGPTYPE_CLUSTER_LIST:
819 while (tlen>0) {
820 printf("%s%s",
821 getname(tptr),
822 (tlen>4) ? ", " : "");
823 tlen -=4;
824 tptr +=4;
825 }
826 break;
827 case BGPTYPE_MP_REACH_NLRI:
828 af = EXTRACT_16BITS(tptr);
829 safi = tptr[2];
830
831 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
832 tok2str(bgp_afi_values, "Unknown AFI", af),
833 af,
834 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
835 tok2str(bgp_safi_values, "Unknown SAFI", safi),
836 safi);
837
838 if (af == AFNUM_INET || af==AFNUM_L2VPN)
839 ;
840 #ifdef INET6
841 else if (af == AFNUM_INET6)
842 ;
843 #endif
844 else {
845 printf("\n\t no AFI %u decoder",af);
846 if (vflag <= 1)
847 print_unknown_data(tptr,"\n\t ",tlen);
848 break;
849 }
850
851 tptr +=3;
852
853 tlen = tptr[0];
854 tptr++;
855
856 if (tlen) {
857 printf("\n\t nexthop: ");
858 while (tlen > 0) {
859 switch (af) {
860 case AFNUM_INET:
861 switch(safi) {
862 case SAFNUM_UNICAST:
863 case SAFNUM_MULTICAST:
864 case SAFNUM_UNIMULTICAST:
865 case SAFNUM_LABUNICAST:
866 case SAFNUM_RT_ROUTING_INFO:
867 printf("%s",getname(tptr));
868 tlen -= sizeof(struct in_addr);
869 tptr += sizeof(struct in_addr);
870 break;
871 case SAFNUM_VPNUNICAST:
872 case SAFNUM_VPNMULTICAST:
873 case SAFNUM_VPNUNIMULTICAST:
874 printf("RD: %s, %s",
875 bgp_vpn_rd_print(tptr),
876 getname(tptr+BGP_VPN_RD_LEN));
877 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
878 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
879 break;
880 default:
881 printf("no SAFI %u decoder",safi);
882 if (vflag <= 1)
883 print_unknown_data(tptr,"\n\t ",tlen);
884 break;
885 }
886 break;
887 #ifdef INET6
888 case AFNUM_INET6:
889 switch(safi) {
890 case SAFNUM_UNICAST:
891 case SAFNUM_MULTICAST:
892 case SAFNUM_UNIMULTICAST:
893 case SAFNUM_LABUNICAST:
894 case SAFNUM_RT_ROUTING_INFO:
895 printf("%s", getname6(tptr));
896 tlen -= sizeof(struct in6_addr);
897 tptr += sizeof(struct in6_addr);
898 break;
899 case SAFNUM_VPNUNICAST:
900 case SAFNUM_VPNMULTICAST:
901 case SAFNUM_VPNUNIMULTICAST:
902 printf("RD: %s, %s",
903 bgp_vpn_rd_print(tptr),
904 getname6(tptr+BGP_VPN_RD_LEN));
905 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
906 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
907 break;
908 default:
909 printf("no SAFI %u decoder",safi);
910 if (vflag <= 1)
911 print_unknown_data(tptr,"\n\t ",tlen);
912 break;
913 }
914 break;
915 #endif
916 case AFNUM_L2VPN:
917 switch(safi) {
918 case SAFNUM_VPNUNICAST:
919 case SAFNUM_VPNMULTICAST:
920 case SAFNUM_VPNUNIMULTICAST:
921 printf("%s", getname(tptr));
922 tlen -= (sizeof(struct in_addr));
923 tptr += (sizeof(struct in_addr));
924 break;
925 default:
926 printf("no SAFI %u decoder",safi);
927 if (vflag <= 1)
928 print_unknown_data(tptr,"\n\t ",tlen);
929 break;
930 }
931 break;
932
933 default:
934 printf("no AFI %u decoder",af);
935 if (vflag <= 1)
936 print_unknown_data(tptr,"\n\t ",tlen);
937 break;
938 }
939 }
940 }
941 tptr += tlen;
942
943 snpa = tptr[0];
944 tptr++;
945
946 if (snpa) {
947 printf("\n\t %u SNPA", snpa);
948 for (/*nothing*/; snpa > 0; snpa--) {
949 printf("\n\t %d bytes", tptr[0]);
950 tptr += tptr[0] + 1;
951 }
952 } else {
953 printf(", no SNPA");
954 }
955
956 while (len - (tptr - pptr) > 0) {
957 switch (af) {
958 case AFNUM_INET:
959 switch (safi) {
960 case SAFNUM_UNICAST:
961 case SAFNUM_MULTICAST:
962 case SAFNUM_UNIMULTICAST:
963 advance = decode_prefix4(tptr, buf, sizeof(buf));
964 if (advance >= 0)
965 printf("\n\t %s", buf);
966 else
967 printf("\n\t (illegal prefix length)");
968 break;
969 case SAFNUM_LABUNICAST:
970 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
971 printf("\n\t %s", buf);
972 break;
973 case SAFNUM_VPNUNICAST:
974 case SAFNUM_VPNMULTICAST:
975 case SAFNUM_VPNUNIMULTICAST:
976 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
977 printf("\n\t %s", buf);
978 break;
979 case SAFNUM_RT_ROUTING_INFO:
980 advance = decode_rt_routing_info(tptr, buf, sizeof(buf));
981 printf("\n\t %s", buf);
982 break;
983 default:
984 printf("\n\t no SAFI %u decoder",safi);
985 if (vflag <= 1)
986 print_unknown_data(tptr-3,"\n\t ",tlen);
987 advance = 0;
988 tptr = pptr + len;
989 break;
990 }
991 break;
992 #ifdef INET6
993 case AFNUM_INET6:
994 switch (safi) {
995 case SAFNUM_UNICAST:
996 case SAFNUM_MULTICAST:
997 case SAFNUM_UNIMULTICAST:
998 advance = decode_prefix6(tptr, buf, sizeof(buf));
999 printf("\n\t %s", buf);
1000 break;
1001 case SAFNUM_LABUNICAST:
1002 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
1003 printf("\n\t %s", buf);
1004 break;
1005 case SAFNUM_VPNUNICAST:
1006 case SAFNUM_VPNMULTICAST:
1007 case SAFNUM_VPNUNIMULTICAST:
1008 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
1009 printf("\n\t %s", buf);
1010 break;
1011 case SAFNUM_RT_ROUTING_INFO:
1012 advance = decode_rt_routing_info(tptr, buf, sizeof(buf));
1013 printf("\n\t %s", buf);
1014 break;
1015 default:
1016 printf("\n\t no SAFI %u decoder ",safi);
1017 if (vflag <= 1)
1018 print_unknown_data(tptr-3,"\n\t ",tlen);
1019 advance = 0;
1020 tptr = pptr + len;
1021 break;
1022 }
1023 break;
1024 #endif
1025 case AFNUM_L2VPN:
1026 switch(safi) {
1027 case SAFNUM_VPNUNICAST:
1028 case SAFNUM_VPNMULTICAST:
1029 case SAFNUM_VPNUNIMULTICAST:
1030 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
1031 printf("\n\t %s", buf);
1032 break;
1033 default:
1034 printf("no SAFI %u decoder",safi);
1035 if (vflag <= 1)
1036 print_unknown_data(tptr,"\n\t ",tlen);
1037 advance = 0;
1038 tptr = pptr + len;
1039 break;
1040 }
1041 break;
1042
1043
1044 default:
1045 printf("\n\t no AFI %u decoder ",af);
1046 if (vflag <= 1)
1047 print_unknown_data(tptr-3,"\n\t ",tlen);
1048 advance = 0;
1049 tptr = pptr + len;
1050 break;
1051 }
1052 tptr += advance;
1053 }
1054 break;
1055
1056 case BGPTYPE_MP_UNREACH_NLRI:
1057 af = EXTRACT_16BITS(tptr);
1058 safi = tptr[2];
1059
1060 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1061 tok2str(bgp_afi_values, "Unknown AFI", af),
1062 af,
1063 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1064 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1065 safi);
1066
1067 tptr += 3;
1068
1069 while (len - (tptr - pptr) > 0) {
1070 switch (af) {
1071 case AFNUM_INET:
1072 switch (safi) {
1073 case SAFNUM_UNICAST:
1074 case SAFNUM_MULTICAST:
1075 case SAFNUM_UNIMULTICAST:
1076 advance = decode_prefix4(tptr, buf, sizeof(buf));
1077 if (advance >= 0)
1078 printf("\n\t %s", buf);
1079 else
1080 printf("\n\t (illegal prefix length)");
1081 break;
1082 case SAFNUM_LABUNICAST:
1083 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
1084 printf("\n\t %s", buf);
1085 break;
1086 case SAFNUM_VPNUNICAST:
1087 case SAFNUM_VPNMULTICAST:
1088 case SAFNUM_VPNUNIMULTICAST:
1089 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
1090 printf("\n\t %s", buf);
1091 break;
1092 default:
1093 printf("\n\t no SAFI %u decoder",safi);
1094 if (vflag <= 1)
1095 print_unknown_data(tptr-3,"\n\t ",tlen);
1096 advance = 0;
1097 tptr = pptr + len;
1098 break;
1099 }
1100 break;
1101
1102 #ifdef INET6
1103 case AFNUM_INET6:
1104 switch (safi) {
1105 case SAFNUM_UNICAST:
1106 case SAFNUM_MULTICAST:
1107 case SAFNUM_UNIMULTICAST:
1108 advance = decode_prefix6(tptr, buf, sizeof(buf));
1109 printf("\n\t %s", buf);
1110 break;
1111 case SAFNUM_LABUNICAST:
1112 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
1113 printf("\n\t %s", buf);
1114 break;
1115 case SAFNUM_VPNUNICAST:
1116 case SAFNUM_VPNMULTICAST:
1117 case SAFNUM_VPNUNIMULTICAST:
1118 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
1119 printf("\n\t %s", buf);
1120 break;
1121 default:
1122 printf("\n\t no SAFI %u decoder",safi);
1123 if (vflag <= 1)
1124 print_unknown_data(tptr-3,"\n\t ",tlen);
1125 advance = 0;
1126 tptr = pptr + len;
1127 break;
1128 }
1129 break;
1130 #endif
1131
1132 case AFNUM_L2VPN:
1133 switch(safi) {
1134 case SAFNUM_VPNUNICAST:
1135 case SAFNUM_VPNMULTICAST:
1136 case SAFNUM_VPNUNIMULTICAST:
1137 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
1138 printf("\n\t %s", buf);
1139 break;
1140 default:
1141 printf("no SAFI %u decoder",safi);
1142 if (vflag <= 1)
1143 print_unknown_data(tptr-3,"\n\t ",tlen);
1144 advance = 0;
1145 tptr = pptr + len;
1146 break;
1147 }
1148 break;
1149
1150 default:
1151 printf("\n\t no AFI %u decoder",af);
1152 if (vflag <= 1)
1153 print_unknown_data(tptr-3,"\n\t ",tlen);
1154 advance = 0;
1155 tptr = pptr + len;
1156 break;
1157 }
1158
1159 tptr += advance;
1160 }
1161 break;
1162 case BGPTYPE_EXTD_COMMUNITIES:
1163 if (len % 8) {
1164 printf("invalid len");
1165 break;
1166 }
1167 while (tlen>0) {
1168 u_int16_t extd_comm;
1169 extd_comm=EXTRACT_16BITS(tptr);
1170
1171 printf("\n\t %s (0x%04x), Flags [%s]",
1172 tok2str(bgp_extd_comm_subtype_values, "unknown extd community typecode", extd_comm),
1173 extd_comm,
1174 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm));
1175
1176 switch(extd_comm) {
1177 case BGP_EXT_COM_RT_0:
1178 case BGP_EXT_COM_RO_0:
1179 printf(": %u:%s",
1180 EXTRACT_16BITS(tptr+2),
1181 getname(tptr+4));
1182 break;
1183 case BGP_EXT_COM_RT_1:
1184 case BGP_EXT_COM_RO_1:
1185 printf(": %s:%u",
1186 getname(tptr+2),
1187 EXTRACT_16BITS(tptr+6));
1188 break;
1189 case BGP_EXT_COM_RT_2:
1190 case BGP_EXT_COM_RO_2:
1191 printf(": %u:%u",
1192 EXTRACT_32BITS(tptr+2),
1193 EXTRACT_16BITS(tptr+6));
1194 break;
1195 case BGP_EXT_COM_LINKBAND:
1196 bw.i = EXTRACT_32BITS(tptr+2);
1197 printf(": bandwidth: %.3f Mbps",
1198 bw.f*8/1000000);
1199 break;
1200 case BGP_EXT_COM_VPN_ORIGIN:
1201 case BGP_EXT_COM_VPN_ORIGIN2:
1202 case BGP_EXT_COM_VPN_ORIGIN3:
1203 case BGP_EXT_COM_VPN_ORIGIN4:
1204 case BGP_EXT_COM_OSPF_RID:
1205 case BGP_EXT_COM_OSPF_RID2:
1206 printf("%s", getname(tptr+2));
1207 break;
1208 case BGP_EXT_COM_OSPF_RTYPE:
1209 case BGP_EXT_COM_OSPF_RTYPE2:
1210 printf(": area:%s, router-type:%s, metric-type:%s%s",
1211 getname(tptr+2),
1212 tok2str(bgp_extd_comm_ospf_rtype_values,
1213 "unknown (0x%02x)",
1214 *(tptr+6)),
1215 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
1216 (*(tptr+6) == (BGP_OSPF_RTYPE_EXT ||BGP_OSPF_RTYPE_NSSA )) ? "E1" : "");
1217 break;
1218 case BGP_EXT_COM_L2INFO:
1219 printf(": %s Control Flags [0x%02x]:MTU %u",
1220 tok2str(bgp_l2vpn_encaps_values,
1221 "unknown encaps",
1222 *(tptr+2)),
1223 *(tptr+3),
1224 EXTRACT_16BITS(tptr+4));
1225 break;
1226 default:
1227 print_unknown_data(tptr,"\n\t ",8);
1228 break;
1229 }
1230 tlen -=8;
1231 tptr +=8;
1232 }
1233 break;
1234
1235 case BGPTYPE_ATTR_SET:
1236 printf("\n\t Origin AS: %u", EXTRACT_32BITS(tptr));
1237 tptr+=4;
1238 len -=4;
1239
1240 while (len >= 2 ) {
1241 int alen;
1242 struct bgp_attr bgpa;
1243
1244 memcpy(&bgpa, tptr, sizeof(bgpa));
1245 alen = bgp_attr_len(&bgpa);
1246 tptr += bgp_attr_off(&bgpa);
1247 len -= bgp_attr_off(&bgpa);
1248
1249 printf("\n\t %s (%u), length: %u",
1250 tok2str(bgp_attr_values, "Unknown Attribute", bgpa.bgpa_type),
1251 bgpa.bgpa_type,
1252 alen);
1253
1254 if (bgpa.bgpa_flags) {
1255 printf(", Flags [%s%s%s%s",
1256 bgpa.bgpa_flags & 0x80 ? "O" : "",
1257 bgpa.bgpa_flags & 0x40 ? "T" : "",
1258 bgpa.bgpa_flags & 0x20 ? "P" : "",
1259 bgpa.bgpa_flags & 0x10 ? "E" : "");
1260 if (bgpa.bgpa_flags & 0xf)
1261 printf("+%x", bgpa.bgpa_flags & 0xf);
1262 printf("]: ");
1263 }
1264 bgp_attr_print(&bgpa, tptr, alen); /* FIXME check for recursion */
1265 tptr += alen;
1266 len -= alen;
1267 }
1268
1269
1270
1271 default:
1272 printf("\n\t no Attribute %u decoder",attr->bgpa_type); /* we have no decoder for the attribute */
1273 if (vflag <= 1)
1274 print_unknown_data(pptr,"\n\t ",len);
1275 break;
1276 }
1277 if (vflag > 1 && len) /* omit zero length attributes*/
1278 print_unknown_data(pptr,"\n\t ",len);
1279 }
1280
1281 static void
1282 bgp_open_print(const u_char *dat, int length)
1283 {
1284 struct bgp_open bgpo;
1285 struct bgp_opt bgpopt;
1286 int hlen;
1287 const u_char *opt;
1288 int i,cap_type,cap_len,tcap_len,cap_offset;
1289
1290 TCHECK2(dat[0], BGP_OPEN_SIZE);
1291 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
1292 hlen = ntohs(bgpo.bgpo_len);
1293
1294 printf("\n\t Version %d, ", bgpo.bgpo_version);
1295 printf("my AS %u, ", ntohs(bgpo.bgpo_myas));
1296 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime));
1297 printf("ID %s", getname((u_char *)&bgpo.bgpo_id));
1298 printf("\n\t Optional parameters, length: %u", bgpo.bgpo_optlen);
1299
1300 /* some little sanity checking */
1301 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
1302 return;
1303
1304 /* ugly! */
1305 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
1306 opt++;
1307
1308 i = 0;
1309 while (i < bgpo.bgpo_optlen) {
1310 TCHECK2(opt[i], BGP_OPT_SIZE);
1311 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
1312 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
1313 printf("\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len);
1314 break;
1315 }
1316
1317 printf("\n\t Option %s (%u), length: %u",
1318 tok2str(bgp_opt_values,"Unknown", bgpopt.bgpopt_type),
1319 bgpopt.bgpopt_type,
1320 bgpopt.bgpopt_len);
1321
1322 /* now lets decode the options we know*/
1323 switch(bgpopt.bgpopt_type) {
1324 case BGP_OPT_CAP:
1325 cap_type=opt[i+BGP_OPT_SIZE];
1326 cap_len=opt[i+BGP_OPT_SIZE+1];
1327 tcap_len=cap_len;
1328 printf("\n\t %s, length: %u",
1329 tok2str(bgp_capcode_values,"Unknown", cap_type),
1330 cap_len);
1331 switch(cap_type) {
1332 case BGP_CAPCODE_MP:
1333 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
1334 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)),
1335 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
1336 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+5]),
1337 opt[i+BGP_OPT_SIZE+5]);
1338 break;
1339 case BGP_CAPCODE_RESTART:
1340 printf("\n\t\tRestart Flags: [%s], Restart Time %us",
1341 ((opt[i+BGP_OPT_SIZE+2])&0x80) ? "R" : "none",
1342 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)&0xfff);
1343 tcap_len-=2;
1344 cap_offset=4;
1345 while(tcap_len>=4) {
1346 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
1347 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset)),
1348 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset),
1349 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+cap_offset+2]),
1350 opt[i+BGP_OPT_SIZE+cap_offset+2],
1351 ((opt[i+BGP_OPT_SIZE+cap_offset+3])&0x80) ? "yes" : "no" );
1352 tcap_len-=4;
1353 cap_offset+=4;
1354 }
1355 break;
1356 case BGP_CAPCODE_RR:
1357 case BGP_CAPCODE_RR_CISCO:
1358 break;
1359 default:
1360 printf("\n\t\tno decoder for Capability %u",
1361 cap_type);
1362 if (vflag <= 1)
1363 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1364 break;
1365 }
1366 if (vflag > 1)
1367 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1368 break;
1369 case BGP_OPT_AUTH:
1370 default:
1371 printf("\n\t no decoder for option %u",
1372 bgpopt.bgpopt_type);
1373 break;
1374 }
1375
1376 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
1377 }
1378 return;
1379 trunc:
1380 printf("[|BGP]");
1381 }
1382
1383 static void
1384 bgp_update_print(const u_char *dat, int length)
1385 {
1386 struct bgp bgp;
1387 struct bgp_attr bgpa;
1388 int hlen;
1389 const u_char *p;
1390 int len;
1391 int i;
1392
1393 TCHECK2(dat[0], BGP_SIZE);
1394 memcpy(&bgp, dat, BGP_SIZE);
1395 hlen = ntohs(bgp.bgp_len);
1396 p = dat + BGP_SIZE; /*XXX*/
1397
1398 /* Unfeasible routes */
1399 len = EXTRACT_16BITS(p);
1400 if (len) {
1401 /*
1402 * Without keeping state from the original NLRI message,
1403 * it's not possible to tell if this a v4 or v6 route,
1404 * so only try to decode it if we're not v6 enabled.
1405 */
1406 #ifdef INET6
1407 printf("\n\t Withdrawn routes: %d bytes", len);
1408 #else
1409 char buf[MAXHOSTNAMELEN + 100];
1410 int wpfx;
1411
1412 TCHECK2(p[2], len);
1413 i = 2;
1414
1415 printf("\n\t Withdrawn routes:");
1416
1417 while(i < 2 + len) {
1418 wpfx = decode_prefix4(&p[i], buf, sizeof(buf));
1419 if (wpfx >= 0) {
1420 i += wpfx;
1421 printf("\n\t %s", buf);
1422 } else {
1423 printf("\n\t (illegal prefix length)");
1424 break;
1425 }
1426 }
1427 #endif
1428 }
1429 p += 2 + len;
1430
1431 TCHECK2(p[0], 2);
1432 len = EXTRACT_16BITS(p);
1433 if (len) {
1434 /* do something more useful!*/
1435 i = 2;
1436 while (i < 2 + len) {
1437 int alen, aoff;
1438
1439 TCHECK2(p[i], sizeof(bgpa));
1440 memcpy(&bgpa, &p[i], sizeof(bgpa));
1441 alen = bgp_attr_len(&bgpa);
1442 aoff = bgp_attr_off(&bgpa);
1443
1444 printf("\n\t %s (%u), length: %u",
1445 tok2str(bgp_attr_values, "Unknown Attribute", bgpa.bgpa_type),
1446 bgpa.bgpa_type,
1447 alen);
1448
1449 if (bgpa.bgpa_flags) {
1450 printf(", Flags [%s%s%s%s",
1451 bgpa.bgpa_flags & 0x80 ? "O" : "",
1452 bgpa.bgpa_flags & 0x40 ? "T" : "",
1453 bgpa.bgpa_flags & 0x20 ? "P" : "",
1454 bgpa.bgpa_flags & 0x10 ? "E" : "");
1455 if (bgpa.bgpa_flags & 0xf)
1456 printf("+%x", bgpa.bgpa_flags & 0xf);
1457 printf("]: ");
1458 }
1459 bgp_attr_print(&bgpa, &p[i + aoff], alen);
1460 i += aoff + alen;
1461 }
1462 }
1463 p += 2 + len;
1464
1465 if (dat + length > p) {
1466 printf("\n\t Updated routes:");
1467 while (dat + length > p) {
1468 char buf[MAXHOSTNAMELEN + 100];
1469 i = decode_prefix4(p, buf, sizeof(buf));
1470 if (i >= 0) {
1471 printf("\n\t %s", buf);
1472 p += i;
1473 } else {
1474 printf("\n\t (illegal prefix length)");
1475 break;
1476 }
1477 }
1478 }
1479 return;
1480 trunc:
1481 printf("[|BGP]");
1482 }
1483
1484 static void
1485 bgp_notification_print(const u_char *dat, int length)
1486 {
1487 struct bgp_notification bgpn;
1488 int hlen;
1489 const u_char *tptr;
1490
1491 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
1492 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
1493 hlen = ntohs(bgpn.bgpn_len);
1494
1495 /* some little sanity checking */
1496 if (length<BGP_NOTIFICATION_SIZE)
1497 return;
1498
1499 printf(", %s (%u)",
1500 tok2str(bgp_notify_major_values, "Unknown Error", bgpn.bgpn_major),
1501 bgpn.bgpn_major);
1502
1503 switch (bgpn.bgpn_major) {
1504
1505 case BGP_NOTIFY_MAJOR_MSG:
1506 printf(", subcode %s (%u)",
1507 tok2str(bgp_notify_minor_msg_values, "Unknown", bgpn.bgpn_minor),
1508 bgpn.bgpn_minor);
1509 break;
1510 case BGP_NOTIFY_MAJOR_OPEN:
1511 printf(", subcode %s (%u)",
1512 tok2str(bgp_notify_minor_open_values, "Unknown", bgpn.bgpn_minor),
1513 bgpn.bgpn_minor);
1514 break;
1515 case BGP_NOTIFY_MAJOR_UPDATE:
1516 printf(", subcode %s (%u)",
1517 tok2str(bgp_notify_minor_update_values, "Unknown", bgpn.bgpn_minor),
1518 bgpn.bgpn_minor);
1519 break;
1520 case BGP_NOTIFY_MAJOR_CAP:
1521 printf(" subcode %s (%u)",
1522 tok2str(bgp_notify_minor_cap_values, "Unknown", bgpn.bgpn_minor),
1523 bgpn.bgpn_minor);
1524 case BGP_NOTIFY_MAJOR_CEASE:
1525 printf(", subcode %s (%u)",
1526 tok2str(bgp_notify_minor_cease_values, "Unknown", bgpn.bgpn_minor),
1527 bgpn.bgpn_minor);
1528
1529 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
1530 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
1531 */
1532 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
1533 tptr = dat + BGP_NOTIFICATION_SIZE;
1534 TCHECK2(*tptr, 7);
1535 printf(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
1536 tok2str(bgp_afi_values, "Unknown", EXTRACT_16BITS(tptr)),
1537 EXTRACT_16BITS(tptr),
1538 tok2str(bgp_safi_values, "Unknown", *(tptr+2)),
1539 *(tptr+2),
1540 EXTRACT_32BITS(tptr+3));
1541 }
1542 break;
1543 default:
1544 break;
1545 }
1546
1547 return;
1548 trunc:
1549 printf("[|BGP]");
1550 }
1551
1552 static void
1553 bgp_route_refresh_print(const u_char *pptr, int len) {
1554
1555 const struct bgp_route_refresh *bgp_route_refresh_header;
1556 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
1557
1558 printf("\n\t AFI %s (%u), SAFI %s (%u)",
1559 tok2str(bgp_afi_values,"Unknown",
1560 EXTRACT_16BITS(&bgp_route_refresh_header->afi)), /* this stinks but the compiler pads the structure weird */
1561 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
1562 tok2str(bgp_safi_values,"Unknown",
1563 bgp_route_refresh_header->safi),
1564 bgp_route_refresh_header->safi);
1565
1566 if (vflag > 1)
1567 print_unknown_data(pptr,"\n\t ", len);
1568
1569 return;
1570 }
1571
1572 static void
1573 bgp_header_print(const u_char *dat, int length)
1574 {
1575 struct bgp bgp;
1576
1577 TCHECK2(dat[0], BGP_SIZE);
1578 memcpy(&bgp, dat, BGP_SIZE);
1579 printf("\n\t%s Message (%u), length: %u",
1580 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
1581 bgp.bgp_type,
1582 length);
1583
1584 switch (bgp.bgp_type) {
1585 case BGP_OPEN:
1586 bgp_open_print(dat, length);
1587 break;
1588 case BGP_UPDATE:
1589 bgp_update_print(dat, length);
1590 break;
1591 case BGP_NOTIFICATION:
1592 bgp_notification_print(dat, length);
1593 break;
1594 case BGP_KEEPALIVE:
1595 break;
1596 case BGP_ROUTE_REFRESH:
1597 bgp_route_refresh_print(dat, length);
1598 break;
1599 default:
1600 /* we have no decoder for the BGP message */
1601 printf("\n\t no Message %u decoder",bgp.bgp_type);
1602 print_unknown_data(dat,"\n\t ",length);
1603 break;
1604 }
1605 return;
1606 trunc:
1607 printf("[|BGP]");
1608 }
1609
1610 void
1611 bgp_print(const u_char *dat, int length)
1612 {
1613 const u_char *p;
1614 const u_char *ep;
1615 const u_char *start;
1616 const u_char marker[] = {
1617 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1618 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1619 };
1620 struct bgp bgp;
1621 u_int16_t hlen;
1622
1623 ep = dat + length;
1624 if (snapend < dat + length)
1625 ep = snapend;
1626
1627 printf(": BGP, length: %u",length);
1628
1629 if (vflag < 1) /* lets be less chatty */
1630 return;
1631
1632 p = dat;
1633 start = p;
1634 while (p < snapend) {
1635 if (!TTEST2(p[0], 1))
1636 break;
1637 if (p[0] != 0xff) {
1638 p++;
1639 continue;
1640 }
1641
1642 if (!TTEST2(p[0], sizeof(marker)))
1643 break;
1644 if (memcmp(p, marker, sizeof(marker)) != 0) {
1645 p++;
1646 continue;
1647 }
1648
1649 /* found BGP header */
1650 TCHECK2(p[0], BGP_SIZE); /*XXX*/
1651 memcpy(&bgp, p, BGP_SIZE);
1652
1653 if (start != p)
1654 printf(" [|BGP]");
1655
1656 hlen = ntohs(bgp.bgp_len);
1657 if (hlen < BGP_SIZE) {
1658 printf("\n[|BGP Bogus header length %u < %u]", hlen,
1659 BGP_SIZE);
1660 break;
1661 }
1662
1663 if (TTEST2(p[0], hlen)) {
1664 bgp_header_print(p, hlen);
1665 p += hlen;
1666 start = p;
1667 } else {
1668 printf("\n[|BGP %s]", tok2str(bgp_msg_values, "Unknown Message Type",bgp.bgp_type));
1669 break;
1670 }
1671 }
1672
1673 return;
1674
1675 trunc:
1676 printf(" [|BGP]");
1677 }