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