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