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