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