]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
Use the word 'invalid' for 'malformed' or 'corrupted' packets
[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 #include <tcpdump-stdinc.h>
38
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "netdissect.h"
43 #include "addrtoname.h"
44 #include "extract.h"
45 #include "af.h"
46 #include "l2vpn.h"
47
48 struct bgp {
49 uint8_t bgp_marker[16];
50 uint16_t bgp_len;
51 uint8_t bgp_type;
52 };
53 #define BGP_SIZE 19 /* unaligned */
54
55 #define BGP_OPEN 1
56 #define BGP_UPDATE 2
57 #define BGP_NOTIFICATION 3
58 #define BGP_KEEPALIVE 4
59 #define BGP_ROUTE_REFRESH 5
60
61 static const struct tok bgp_msg_values[] = {
62 { BGP_OPEN, "Open"},
63 { BGP_UPDATE, "Update"},
64 { BGP_NOTIFICATION, "Notification"},
65 { BGP_KEEPALIVE, "Keepalive"},
66 { BGP_ROUTE_REFRESH, "Route Refresh"},
67 { 0, NULL}
68 };
69
70 struct bgp_open {
71 uint8_t bgpo_marker[16];
72 uint16_t bgpo_len;
73 uint8_t bgpo_type;
74 uint8_t bgpo_version;
75 uint16_t bgpo_myas;
76 uint16_t bgpo_holdtime;
77 uint32_t bgpo_id;
78 uint8_t bgpo_optlen;
79 /* options should follow */
80 };
81 #define BGP_OPEN_SIZE 29 /* unaligned */
82
83 struct bgp_opt {
84 uint8_t bgpopt_type;
85 uint8_t bgpopt_len;
86 /* variable length */
87 };
88 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
89 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
90
91 struct bgp_notification {
92 uint8_t bgpn_marker[16];
93 uint16_t bgpn_len;
94 uint8_t bgpn_type;
95 uint8_t bgpn_major;
96 uint8_t bgpn_minor;
97 };
98 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
99
100 struct bgp_route_refresh {
101 uint8_t bgp_marker[16];
102 uint16_t len;
103 uint8_t type;
104 uint8_t afi[2]; /* the compiler messes this structure up */
105 uint8_t res; /* when doing misaligned sequences of int8 and int16 */
106 uint8_t safi; /* afi should be int16 - so we have to access it using */
107 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
108 #define BGP_ROUTE_REFRESH_SIZE 23
109
110 #define bgp_attr_lenlen(flags, p) \
111 (((flags) & 0x10) ? 2 : 1)
112 #define bgp_attr_len(flags, p) \
113 (((flags) & 0x10) ? EXTRACT_16BITS(p) : *(p))
114
115 #define BGPTYPE_ORIGIN 1
116 #define BGPTYPE_AS_PATH 2
117 #define BGPTYPE_NEXT_HOP 3
118 #define BGPTYPE_MULTI_EXIT_DISC 4
119 #define BGPTYPE_LOCAL_PREF 5
120 #define BGPTYPE_ATOMIC_AGGREGATE 6
121 #define BGPTYPE_AGGREGATOR 7
122 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
123 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
124 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
125 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
126 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
127 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
128 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
129 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
130 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
131 #define BGPTYPE_AS4_PATH 17 /* RFC4893 */
132 #define BGPTYPE_AGGREGATOR4 18 /* RFC4893 */
133 #define BGPTYPE_PMSI_TUNNEL 22 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
134 #define BGPTYPE_AIGP 26 /* RFC7311 */
135 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */
136
137 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
138
139 static const struct tok bgp_attr_values[] = {
140 { BGPTYPE_ORIGIN, "Origin"},
141 { BGPTYPE_AS_PATH, "AS Path"},
142 { BGPTYPE_AS4_PATH, "AS4 Path"},
143 { BGPTYPE_NEXT_HOP, "Next Hop"},
144 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
145 { BGPTYPE_LOCAL_PREF, "Local Preference"},
146 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
147 { BGPTYPE_AGGREGATOR, "Aggregator"},
148 { BGPTYPE_AGGREGATOR4, "Aggregator4"},
149 { BGPTYPE_COMMUNITIES, "Community"},
150 { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
151 { BGPTYPE_CLUSTER_LIST, "Cluster List"},
152 { BGPTYPE_DPA, "DPA"},
153 { BGPTYPE_ADVERTISERS, "Advertisers"},
154 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
155 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
156 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
157 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
158 { BGPTYPE_PMSI_TUNNEL, "PMSI Tunnel"},
159 { BGPTYPE_AIGP, "Accumulated IGP Metric"},
160 { BGPTYPE_ATTR_SET, "Attribute Set"},
161 { 255, "Reserved for development"},
162 { 0, NULL}
163 };
164
165 #define BGP_AS_SET 1
166 #define BGP_AS_SEQUENCE 2
167 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
168 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
169
170 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
171 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
172
173 static const struct tok bgp_as_path_segment_open_values[] = {
174 { BGP_AS_SEQUENCE, ""},
175 { BGP_AS_SET, "{ "},
176 { BGP_CONFED_AS_SEQUENCE, "( "},
177 { BGP_CONFED_AS_SET, "({ "},
178 { 0, NULL}
179 };
180
181 static const struct tok bgp_as_path_segment_close_values[] = {
182 { BGP_AS_SEQUENCE, ""},
183 { BGP_AS_SET, "}"},
184 { BGP_CONFED_AS_SEQUENCE, ")"},
185 { BGP_CONFED_AS_SET, "})"},
186 { 0, NULL}
187 };
188
189 #define BGP_OPT_AUTH 1
190 #define BGP_OPT_CAP 2
191
192
193 static const struct tok bgp_opt_values[] = {
194 { BGP_OPT_AUTH, "Authentication Information"},
195 { BGP_OPT_CAP, "Capabilities Advertisement"},
196 { 0, NULL}
197 };
198
199 #define BGP_CAPCODE_MP 1
200 #define BGP_CAPCODE_RR 2
201 #define BGP_CAPCODE_ORF 3 /* XXX */
202 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */
203 #define BGP_CAPCODE_AS_NEW 65 /* XXX */
204 #define BGP_CAPCODE_DYN_CAP 67 /* XXX */
205 #define BGP_CAPCODE_ADD_PATH 69 /* draft-ietf-idr-add-paths-10 */
206 #define BGP_CAPCODE_RR_CISCO 128
207
208 static const 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_ADD_PATH, "Multiple Paths"},
216 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
217 { 0, NULL}
218 };
219
220 #define BGP_NOTIFY_MAJOR_MSG 1
221 #define BGP_NOTIFY_MAJOR_OPEN 2
222 #define BGP_NOTIFY_MAJOR_UPDATE 3
223 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
224 #define BGP_NOTIFY_MAJOR_FSM 5
225 #define BGP_NOTIFY_MAJOR_CEASE 6
226 #define BGP_NOTIFY_MAJOR_CAP 7
227
228 static const struct tok bgp_notify_major_values[] = {
229 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
230 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
231 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
232 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
233 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
234 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
235 { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"},
236 { 0, NULL}
237 };
238
239 /* draft-ietf-idr-cease-subcode-02 */
240 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
241 static const struct tok bgp_notify_minor_cease_values[] = {
242 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"},
243 { 2, "Administratively Shutdown"},
244 { 3, "Peer Unconfigured"},
245 { 4, "Administratively Reset"},
246 { 5, "Connection Rejected"},
247 { 6, "Other Configuration Change"},
248 { 7, "Connection Collision Resolution"},
249 { 0, NULL}
250 };
251
252 static const struct tok bgp_notify_minor_msg_values[] = {
253 { 1, "Connection Not Synchronized"},
254 { 2, "Bad Message Length"},
255 { 3, "Bad Message Type"},
256 { 0, NULL}
257 };
258
259 static const struct tok bgp_notify_minor_open_values[] = {
260 { 1, "Unsupported Version Number"},
261 { 2, "Bad Peer AS"},
262 { 3, "Bad BGP Identifier"},
263 { 4, "Unsupported Optional Parameter"},
264 { 5, "Authentication Failure"},
265 { 6, "Unacceptable Hold Time"},
266 { 7, "Capability Message Error"},
267 { 0, NULL}
268 };
269
270 static const struct tok bgp_notify_minor_update_values[] = {
271 { 1, "Malformed Attribute List"},
272 { 2, "Unrecognized Well-known Attribute"},
273 { 3, "Missing Well-known Attribute"},
274 { 4, "Attribute Flags Error"},
275 { 5, "Attribute Length Error"},
276 { 6, "Invalid ORIGIN Attribute"},
277 { 7, "AS Routing Loop"},
278 { 8, "Invalid NEXT_HOP Attribute"},
279 { 9, "Optional Attribute Error"},
280 { 10, "Invalid Network Field"},
281 { 11, "Malformed AS_PATH"},
282 { 0, NULL}
283 };
284
285 static const struct tok bgp_notify_minor_cap_values[] = {
286 { 1, "Invalid Action Value" },
287 { 2, "Invalid Capability Length" },
288 { 3, "Malformed Capability Value" },
289 { 4, "Unsupported Capability Code" },
290 { 0, NULL }
291 };
292
293 static const struct tok bgp_origin_values[] = {
294 { 0, "IGP"},
295 { 1, "EGP"},
296 { 2, "Incomplete"},
297 { 0, NULL}
298 };
299
300 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
301 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
302 #define BGP_PMSI_TUNNEL_PIM_SSM 3
303 #define BGP_PMSI_TUNNEL_PIM_SM 4
304 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
305 #define BGP_PMSI_TUNNEL_INGRESS 6
306 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
307
308 static const struct tok bgp_pmsi_tunnel_values[] = {
309 { BGP_PMSI_TUNNEL_RSVP_P2MP, "RSVP-TE P2MP LSP"},
310 { BGP_PMSI_TUNNEL_LDP_P2MP, "LDP P2MP LSP"},
311 { BGP_PMSI_TUNNEL_PIM_SSM, "PIM-SSM Tree"},
312 { BGP_PMSI_TUNNEL_PIM_SM, "PIM-SM Tree"},
313 { BGP_PMSI_TUNNEL_PIM_BIDIR, "PIM-Bidir Tree"},
314 { BGP_PMSI_TUNNEL_INGRESS, "Ingress Replication"},
315 { BGP_PMSI_TUNNEL_LDP_MP2MP, "LDP MP2MP LSP"},
316 { 0, NULL}
317 };
318
319 static const struct tok bgp_pmsi_flag_values[] = {
320 { 0x01, "Leaf Information required"},
321 { 0, NULL}
322 };
323
324 #define BGP_AIGP_TLV 1
325
326 static const struct tok bgp_aigp_values[] = {
327 { BGP_AIGP_TLV, "AIGP"},
328 { 0, NULL}
329 };
330
331
332 /* Subsequent address family identifier, RFC2283 section 7 */
333 #define SAFNUM_RES 0
334 #define SAFNUM_UNICAST 1
335 #define SAFNUM_MULTICAST 2
336 #define SAFNUM_UNIMULTICAST 3
337 /* labeled BGP RFC3107 */
338 #define SAFNUM_LABUNICAST 4
339 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
340 #define SAFNUM_MULTICAST_VPN 5
341 #define SAFNUM_TUNNEL 64 /* XXX */
342 #define SAFNUM_VPLS 65 /* XXX */
343 /* draft-nalawade-idr-mdt-safi-03 */
344 #define SAFNUM_MDT 66
345 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
346 #define SAFNUM_VPNUNICAST 128
347 #define SAFNUM_VPNMULTICAST 129
348 #define SAFNUM_VPNUNIMULTICAST 130
349 /* draft-marques-ppvpn-rt-constrain-01.txt */
350 #define SAFNUM_RT_ROUTING_INFO 132
351
352 #define BGP_VPN_RD_LEN 8
353
354 static const struct tok bgp_safi_values[] = {
355 { SAFNUM_RES, "Reserved"},
356 { SAFNUM_UNICAST, "Unicast"},
357 { SAFNUM_MULTICAST, "Multicast"},
358 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
359 { SAFNUM_LABUNICAST, "labeled Unicast"},
360 { SAFNUM_TUNNEL, "Tunnel"},
361 { SAFNUM_VPLS, "VPLS"},
362 { SAFNUM_MDT, "MDT"},
363 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
364 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
365 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
366 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"},
367 { SAFNUM_MULTICAST_VPN, "Multicast VPN"},
368 { 0, NULL }
369 };
370
371 /* well-known community */
372 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
373 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
374 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
375
376 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
377 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
378 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
379 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
380 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
381 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
382 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
383 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
384 /* rfc2547 bgp-mpls-vpns */
385 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
386 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
387 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
388 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
389
390 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
391 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
392
393 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
394 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
395
396 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
397
398 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
399 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
400 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
401 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
402
403
404 /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
405 #define BGP_EXT_COM_EIGRP_GEN 0x8800
406 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
407 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
408 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
409 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
410 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
411
412 static const struct tok bgp_extd_comm_flag_values[] = {
413 { 0x8000, "vendor-specific"},
414 { 0x4000, "non-transitive"},
415 { 0, NULL},
416 };
417
418 static const struct tok bgp_extd_comm_subtype_values[] = {
419 { BGP_EXT_COM_RT_0, "target"},
420 { BGP_EXT_COM_RT_1, "target"},
421 { BGP_EXT_COM_RT_2, "target"},
422 { BGP_EXT_COM_RO_0, "origin"},
423 { BGP_EXT_COM_RO_1, "origin"},
424 { BGP_EXT_COM_RO_2, "origin"},
425 { BGP_EXT_COM_LINKBAND, "link-BW"},
426 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"},
427 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"},
428 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"},
429 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"},
430 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"},
431 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"},
432 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"},
433 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"},
434 { BGP_EXT_COM_L2INFO, "layer2-info"},
435 { BGP_EXT_COM_EIGRP_GEN , "eigrp-general-route (flag, tag)" },
436 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY , "eigrp-route-metric (AS, delay)" },
437 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW , "eigrp-route-metric (reliability, nexthop, bandwidth)" },
438 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU , "eigrp-route-metric (load, MTU)" },
439 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID , "eigrp-external-route (remote-AS, remote-ID)" },
440 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC , "eigrp-external-route (remote-proto, remote-metric)" },
441 { BGP_EXT_COM_SOURCE_AS, "source-AS" },
442 { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"},
443 { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"},
444 { BGP_EXT_COM_L2VPN_RT_1, "l2vpn-id"},
445 { 0, NULL},
446 };
447
448 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
449 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
450 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
451 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
452 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
453 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
454 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
455 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
456
457 static const struct tok bgp_extd_comm_ospf_rtype_values[] = {
458 { BGP_OSPF_RTYPE_RTR, "Router" },
459 { BGP_OSPF_RTYPE_NET, "Network" },
460 { BGP_OSPF_RTYPE_SUM, "Summary" },
461 { BGP_OSPF_RTYPE_EXT, "External" },
462 { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
463 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
464 { 0, NULL },
465 };
466
467 /* ADD-PATH Send/Receive field values */
468 static const struct tok bgp_add_path_recvsend[] = {
469 { 1, "Receive" },
470 { 2, "Send" },
471 { 3, "Both" },
472 { 0, NULL },
473 };
474
475 static char astostr[20];
476
477 /*
478 * as_printf
479 *
480 * Convert an AS number into a string and return string pointer.
481 *
482 * Depending on bflag is set or not, AS number is converted into ASDOT notation
483 * or plain number notation.
484 *
485 */
486 static char *
487 as_printf(netdissect_options *ndo,
488 char *str, int size, u_int asnum)
489 {
490 if (!ndo->ndo_bflag || asnum <= 0xFFFF) {
491 snprintf(str, size, "%u", asnum);
492 } else {
493 snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
494 }
495 return str;
496 }
497
498 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
499
500 int
501 decode_prefix4(netdissect_options *ndo,
502 const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
503 {
504 struct in_addr addr;
505 u_int plen, plenbytes;
506
507 ND_TCHECK(pptr[0]);
508 ITEMCHECK(1);
509 plen = pptr[0];
510 if (32 < plen)
511 return -1;
512 itemlen -= 1;
513
514 memset(&addr, 0, sizeof(addr));
515 plenbytes = (plen + 7) / 8;
516 ND_TCHECK2(pptr[1], plenbytes);
517 ITEMCHECK(plenbytes);
518 memcpy(&addr, &pptr[1], plenbytes);
519 if (plen % 8) {
520 ((u_char *)&addr)[plenbytes - 1] &=
521 ((0xff00 >> (plen % 8)) & 0xff);
522 }
523 snprintf(buf, buflen, "%s/%d", getname(ndo, (u_char *)&addr), plen);
524 return 1 + plenbytes;
525
526 trunc:
527 return -2;
528
529 badtlv:
530 return -3;
531 }
532
533 static int
534 decode_labeled_prefix4(netdissect_options *ndo,
535 const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
536 {
537 struct in_addr addr;
538 u_int plen, plenbytes;
539
540 /* prefix length and label = 4 bytes */
541 ND_TCHECK2(pptr[0], 4);
542 ITEMCHECK(4);
543 plen = pptr[0]; /* get prefix length */
544
545 /* this is one of the weirdnesses of rfc3107
546 the label length (actually the label + COS bits)
547 is added to the prefix length;
548 we also do only read out just one label -
549 there is no real application for advertisement of
550 stacked labels in a single BGP message
551 */
552
553 if (24 > plen)
554 return -1;
555
556 plen-=24; /* adjust prefixlen - labellength */
557
558 if (32 < plen)
559 return -1;
560 itemlen -= 4;
561
562 memset(&addr, 0, sizeof(addr));
563 plenbytes = (plen + 7) / 8;
564 ND_TCHECK2(pptr[4], plenbytes);
565 ITEMCHECK(plenbytes);
566 memcpy(&addr, &pptr[4], plenbytes);
567 if (plen % 8) {
568 ((u_char *)&addr)[plenbytes - 1] &=
569 ((0xff00 >> (plen % 8)) & 0xff);
570 }
571 /* the label may get offsetted by 4 bits so lets shift it right */
572 snprintf(buf, buflen, "%s/%d, label:%u %s",
573 getname(ndo, (u_char *)&addr),
574 plen,
575 EXTRACT_24BITS(pptr+1)>>4,
576 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
577
578 return 4 + plenbytes;
579
580 trunc:
581 return -2;
582
583 badtlv:
584 return -3;
585 }
586
587 /*
588 * bgp_vpn_ip_print
589 *
590 * print an ipv4 or ipv6 address into a buffer dependend on address length.
591 */
592 static char *
593 bgp_vpn_ip_print(netdissect_options *ndo,
594 const u_char *pptr, u_int addr_length)
595 {
596
597 /* worst case string is s fully formatted v6 address */
598 static char addr[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
599 char *pos = addr;
600
601 switch(addr_length) {
602 case (sizeof(struct in_addr) << 3): /* 32 */
603 ND_TCHECK2(pptr[0], sizeof(struct in_addr));
604 snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
605 break;
606 #ifdef INET6
607 case (sizeof(struct in6_addr) << 3): /* 128 */
608 ND_TCHECK2(pptr[0], sizeof(struct in6_addr));
609 snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
610 break;
611 #endif
612 default:
613 snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
614 break;
615 }
616 pos += strlen(pos);
617
618 trunc:
619 *(pos) = '\0';
620 return (addr);
621 }
622
623 /*
624 * bgp_vpn_sg_print
625 *
626 * print an multicast s,g entry into a buffer.
627 * the s,g entry is encoded like this.
628 *
629 * +-----------------------------------+
630 * | Multicast Source Length (1 octet) |
631 * +-----------------------------------+
632 * | Multicast Source (Variable) |
633 * +-----------------------------------+
634 * | Multicast Group Length (1 octet) |
635 * +-----------------------------------+
636 * | Multicast Group (Variable) |
637 * +-----------------------------------+
638 *
639 * return the number of bytes read from the wire.
640 */
641 static int
642 bgp_vpn_sg_print(netdissect_options *ndo,
643 const u_char *pptr, char *buf, u_int buflen)
644 {
645 uint8_t addr_length;
646 u_int total_length, offset;
647
648 total_length = 0;
649
650 /* Source address length, encoded in bits */
651 ND_TCHECK2(pptr[0], 1);
652 addr_length = *pptr++;
653
654 /* Source address */
655 ND_TCHECK2(pptr[0], (addr_length >> 3));
656 total_length += (addr_length >> 3) + 1;
657 offset = strlen(buf);
658 if (addr_length) {
659 snprintf(buf + offset, buflen - offset, ", Source %s",
660 bgp_vpn_ip_print(ndo, pptr, addr_length));
661 pptr += (addr_length >> 3);
662 }
663
664 /* Group address length, encoded in bits */
665 ND_TCHECK2(pptr[0], 1);
666 addr_length = *pptr++;
667
668 /* Group address */
669 ND_TCHECK2(pptr[0], (addr_length >> 3));
670 total_length += (addr_length >> 3) + 1;
671 offset = strlen(buf);
672 if (addr_length) {
673 snprintf(buf + offset, buflen - offset, ", Group %s",
674 bgp_vpn_ip_print(ndo, pptr, addr_length));
675 pptr += (addr_length >> 3);
676 }
677
678 trunc:
679 return (total_length);
680 }
681
682
683 /* RDs and RTs share the same semantics
684 * we use bgp_vpn_rd_print for
685 * printing route targets inside a NLRI */
686 char *
687 bgp_vpn_rd_print(netdissect_options *ndo,
688 const u_char *pptr)
689 {
690 /* allocate space for the largest possible string */
691 static char rd[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
692 char *pos = rd;
693
694 /* ok lets load the RD format */
695 switch (EXTRACT_16BITS(pptr)) {
696
697 /* 2-byte-AS:number fmt*/
698 case 0:
699 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
700 EXTRACT_16BITS(pptr+2),
701 EXTRACT_32BITS(pptr+4),
702 *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7));
703 break;
704 /* IP-address:AS fmt*/
705
706 case 1:
707 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
708 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
709 break;
710
711 /* 4-byte-AS:number fmt*/
712 case 2:
713 snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
714 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(pptr+2)),
715 EXTRACT_16BITS(pptr+6), *(pptr+2), *(pptr+3), *(pptr+4),
716 *(pptr+5), EXTRACT_16BITS(pptr+6));
717 break;
718 default:
719 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
720 break;
721 }
722 pos += strlen(pos);
723 *(pos) = '\0';
724 return (rd);
725 }
726
727 static int
728 decode_rt_routing_info(netdissect_options *ndo,
729 const u_char *pptr, char *buf, u_int buflen)
730 {
731 uint8_t route_target[8];
732 u_int plen;
733
734 ND_TCHECK(pptr[0]);
735 plen = pptr[0]; /* get prefix length */
736
737 if (0 == plen) {
738 snprintf(buf, buflen, "default route target");
739 return 1;
740 }
741
742 if (32 > plen)
743 return -1;
744
745 plen-=32; /* adjust prefix length */
746
747 if (64 < plen)
748 return -1;
749
750 memset(&route_target, 0, sizeof(route_target));
751 ND_TCHECK2(pptr[1], (plen + 7) / 8);
752 memcpy(&route_target, &pptr[1], (plen + 7) / 8);
753 if (plen % 8) {
754 ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
755 ((0xff00 >> (plen % 8)) & 0xff);
756 }
757 snprintf(buf, buflen, "origin AS: %s, route target %s",
758 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(pptr+1)),
759 bgp_vpn_rd_print(ndo, (u_char *)&route_target));
760
761 return 5 + (plen + 7) / 8;
762
763 trunc:
764 return -2;
765 }
766
767 static int
768 decode_labeled_vpn_prefix4(netdissect_options *ndo,
769 const u_char *pptr, char *buf, u_int buflen)
770 {
771 struct in_addr addr;
772 u_int plen;
773
774 ND_TCHECK(pptr[0]);
775 plen = pptr[0]; /* get prefix length */
776
777 if ((24+64) > plen)
778 return -1;
779
780 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
781
782 if (32 < plen)
783 return -1;
784
785 memset(&addr, 0, sizeof(addr));
786 ND_TCHECK2(pptr[12], (plen + 7) / 8);
787 memcpy(&addr, &pptr[12], (plen + 7) / 8);
788 if (plen % 8) {
789 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
790 ((0xff00 >> (plen % 8)) & 0xff);
791 }
792 /* the label may get offsetted by 4 bits so lets shift it right */
793 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
794 bgp_vpn_rd_print(ndo, pptr+4),
795 getname(ndo, (u_char *)&addr),
796 plen,
797 EXTRACT_24BITS(pptr+1)>>4,
798 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
799
800 return 12 + (plen + 7) / 8;
801
802 trunc:
803 return -2;
804 }
805
806 /*
807 * +-------------------------------+
808 * | |
809 * | RD:IPv4-address (12 octets) |
810 * | |
811 * +-------------------------------+
812 * | MDT Group-address (4 octets) |
813 * +-------------------------------+
814 */
815
816 #define MDT_VPN_NLRI_LEN 16
817
818 static int
819 decode_mdt_vpn_nlri(netdissect_options *ndo,
820 const u_char *pptr, char *buf, u_int buflen)
821 {
822
823 const u_char *rd;
824 const u_char *vpn_ip;
825
826 ND_TCHECK(pptr[0]);
827
828 /* if the NLRI is not predefined length, quit.*/
829 if (*pptr != MDT_VPN_NLRI_LEN * 8)
830 return -1;
831 pptr++;
832
833 /* RD */
834 ND_TCHECK2(pptr[0], 8);
835 rd = pptr;
836 pptr+=8;
837
838 /* IPv4 address */
839 ND_TCHECK2(pptr[0], sizeof(struct in_addr));
840 vpn_ip = pptr;
841 pptr+=sizeof(struct in_addr);
842
843 /* MDT Group Address */
844 ND_TCHECK2(pptr[0], sizeof(struct in_addr));
845
846 snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
847 bgp_vpn_rd_print(ndo, rd), ipaddr_string(ndo, vpn_ip), ipaddr_string(ndo, pptr));
848
849 return MDT_VPN_NLRI_LEN + 1;
850
851 trunc:
852
853 return -2;
854 }
855
856 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
857 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
858 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
859 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
860 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
861 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
862 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
863
864 static const struct tok bgp_multicast_vpn_route_type_values[] = {
865 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
866 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
867 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
868 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
869 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
870 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
871 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
872 };
873
874 static int
875 decode_multicast_vpn(netdissect_options *ndo,
876 const u_char *pptr, char *buf, u_int buflen)
877 {
878 uint8_t route_type, route_length, addr_length, sg_length;
879 u_int offset;
880
881 ND_TCHECK2(pptr[0], 2);
882 route_type = *pptr++;
883 route_length = *pptr++;
884
885 snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
886 tok2str(bgp_multicast_vpn_route_type_values,
887 "Unknown", route_type),
888 route_type, route_length);
889
890 switch(route_type) {
891 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
892 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
893 offset = strlen(buf);
894 snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
895 bgp_vpn_rd_print(ndo, pptr),
896 bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
897 (route_length - BGP_VPN_RD_LEN) << 3));
898 break;
899 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
900 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
901 offset = strlen(buf);
902 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
903 bgp_vpn_rd_print(ndo, pptr),
904 as_printf(ndo, astostr, sizeof(astostr),
905 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
906 break;
907
908 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
909 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
910 offset = strlen(buf);
911 snprintf(buf + offset, buflen - offset, ", RD: %s",
912 bgp_vpn_rd_print(ndo, pptr));
913 pptr += BGP_VPN_RD_LEN;
914
915 sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
916 addr_length = route_length - sg_length;
917
918 ND_TCHECK2(pptr[0], addr_length);
919 offset = strlen(buf);
920 snprintf(buf + offset, buflen - offset, ", Originator %s",
921 bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
922 break;
923
924 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
925 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
926 offset = strlen(buf);
927 snprintf(buf + offset, buflen - offset, ", RD: %s",
928 bgp_vpn_rd_print(ndo, pptr));
929 pptr += BGP_VPN_RD_LEN;
930
931 bgp_vpn_sg_print(ndo, pptr, buf, buflen);
932 break;
933
934 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
935 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
936 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
937 offset = strlen(buf);
938 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
939 bgp_vpn_rd_print(ndo, pptr),
940 as_printf(ndo, astostr, sizeof(astostr),
941 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
942 pptr += BGP_VPN_RD_LEN;
943
944 bgp_vpn_sg_print(ndo, pptr, buf, buflen);
945 break;
946
947 /*
948 * no per route-type printing yet.
949 */
950 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
951 default:
952 break;
953 }
954
955 return route_length + 2;
956
957 trunc:
958 return -2;
959 }
960
961 /*
962 * As I remember, some versions of systems have an snprintf() that
963 * returns -1 if the buffer would have overflowed. If the return
964 * value is negative, set buflen to 0, to indicate that we've filled
965 * the buffer up.
966 *
967 * If the return value is greater than buflen, that means that
968 * the buffer would have overflowed; again, set buflen to 0 in
969 * that case.
970 */
971 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
972 if (stringlen<0) \
973 buflen=0; \
974 else if ((u_int)stringlen>buflen) \
975 buflen=0; \
976 else { \
977 buflen-=stringlen; \
978 buf+=stringlen; \
979 }
980
981 static int
982 decode_labeled_vpn_l2(netdissect_options *ndo,
983 const u_char *pptr, char *buf, u_int buflen)
984 {
985 int plen,tlen,stringlen,tlv_type,tlv_len,ttlv_len;
986
987 ND_TCHECK2(pptr[0], 2);
988 plen=EXTRACT_16BITS(pptr);
989 tlen=plen;
990 pptr+=2;
991 /* Old and new L2VPN NLRI share AFI/SAFI
992 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
993 * and > 17 as old format. Complain for the middle case
994 */
995 if (plen==12) {
996 /* assume AD-only with RD, BGPNH */
997 ND_TCHECK2(pptr[0],12);
998 buf[0]='\0';
999 stringlen=snprintf(buf, buflen, "RD: %s, BGPNH: %s",
1000 bgp_vpn_rd_print(ndo, pptr),
1001 /* need something like getname(ndo, ) here */
1002 getname(ndo, pptr+8)
1003 );
1004 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1005 pptr+=12;
1006 tlen-=12;
1007 return plen;
1008 } else if (plen>17) {
1009 /* assume old format */
1010 /* RD, ID, LBLKOFF, LBLBASE */
1011
1012 ND_TCHECK2(pptr[0],15);
1013 buf[0]='\0';
1014 stringlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1015 bgp_vpn_rd_print(ndo, pptr),
1016 EXTRACT_16BITS(pptr+8),
1017 EXTRACT_16BITS(pptr+10),
1018 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1019 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1020 pptr+=15;
1021 tlen-=15;
1022
1023 /* ok now the variable part - lets read out TLVs*/
1024 while (tlen>0) {
1025 if (tlen < 3)
1026 return -1;
1027 ND_TCHECK2(pptr[0], 3);
1028 tlv_type=*pptr++;
1029 tlv_len=EXTRACT_16BITS(pptr);
1030 ttlv_len=tlv_len;
1031 pptr+=2;
1032
1033 switch(tlv_type) {
1034 case 1:
1035 if (buflen!=0) {
1036 stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1037 tlv_type,
1038 tlv_len);
1039 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1040 }
1041 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
1042 while (ttlv_len>0) {
1043 ND_TCHECK(pptr[0]);
1044 if (buflen!=0) {
1045 stringlen=snprintf(buf,buflen, "%02x",*pptr++);
1046 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1047 }
1048 ttlv_len--;
1049 }
1050 break;
1051 default:
1052 if (buflen!=0) {
1053 stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
1054 tlv_type,
1055 tlv_len);
1056 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1057 }
1058 break;
1059 }
1060 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
1061 }
1062 return plen+2;
1063
1064 } else {
1065 /* complain bitterly ? */
1066 /* fall through */
1067 goto trunc;
1068 }
1069
1070 trunc:
1071 return -2;
1072 }
1073
1074 #ifdef INET6
1075 int
1076 decode_prefix6(netdissect_options *ndo,
1077 const u_char *pd, u_int itemlen, char *buf, u_int buflen)
1078 {
1079 struct in6_addr addr;
1080 u_int plen, plenbytes;
1081
1082 ND_TCHECK(pd[0]);
1083 ITEMCHECK(1);
1084 plen = pd[0];
1085 if (128 < plen)
1086 return -1;
1087 itemlen -= 1;
1088
1089 memset(&addr, 0, sizeof(addr));
1090 plenbytes = (plen + 7) / 8;
1091 ND_TCHECK2(pd[1], plenbytes);
1092 ITEMCHECK(plenbytes);
1093 memcpy(&addr, &pd[1], plenbytes);
1094 if (plen % 8) {
1095 addr.s6_addr[plenbytes - 1] &=
1096 ((0xff00 >> (plen % 8)) & 0xff);
1097 }
1098 snprintf(buf, buflen, "%s/%d", getname6(ndo, (u_char *)&addr), plen);
1099 return 1 + plenbytes;
1100
1101 trunc:
1102 return -2;
1103
1104 badtlv:
1105 return -3;
1106 }
1107
1108 static int
1109 decode_labeled_prefix6(netdissect_options *ndo,
1110 const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
1111 {
1112 struct in6_addr addr;
1113 u_int plen, plenbytes;
1114
1115 /* prefix length and label = 4 bytes */
1116 ND_TCHECK2(pptr[0], 4);
1117 ITEMCHECK(4);
1118 plen = pptr[0]; /* get prefix length */
1119
1120 if (24 > plen)
1121 return -1;
1122
1123 plen-=24; /* adjust prefixlen - labellength */
1124
1125 if (128 < plen)
1126 return -1;
1127 itemlen -= 4;
1128
1129 memset(&addr, 0, sizeof(addr));
1130 plenbytes = (plen + 7) / 8;
1131 ND_TCHECK2(pptr[4], plenbytes);
1132 memcpy(&addr, &pptr[4], plenbytes);
1133 if (plen % 8) {
1134 addr.s6_addr[plenbytes - 1] &=
1135 ((0xff00 >> (plen % 8)) & 0xff);
1136 }
1137 /* the label may get offsetted by 4 bits so lets shift it right */
1138 snprintf(buf, buflen, "%s/%d, label:%u %s",
1139 getname6(ndo, (u_char *)&addr),
1140 plen,
1141 EXTRACT_24BITS(pptr+1)>>4,
1142 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1143
1144 return 4 + plenbytes;
1145
1146 trunc:
1147 return -2;
1148
1149 badtlv:
1150 return -3;
1151 }
1152
1153 static int
1154 decode_labeled_vpn_prefix6(netdissect_options *ndo,
1155 const u_char *pptr, char *buf, u_int buflen)
1156 {
1157 struct in6_addr addr;
1158 u_int plen;
1159
1160 ND_TCHECK(pptr[0]);
1161 plen = pptr[0]; /* get prefix length */
1162
1163 if ((24+64) > plen)
1164 return -1;
1165
1166 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1167
1168 if (128 < plen)
1169 return -1;
1170
1171 memset(&addr, 0, sizeof(addr));
1172 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1173 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1174 if (plen % 8) {
1175 addr.s6_addr[(plen + 7) / 8 - 1] &=
1176 ((0xff00 >> (plen % 8)) & 0xff);
1177 }
1178 /* the label may get offsetted by 4 bits so lets shift it right */
1179 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1180 bgp_vpn_rd_print(ndo, pptr+4),
1181 getname6(ndo, (u_char *)&addr),
1182 plen,
1183 EXTRACT_24BITS(pptr+1)>>4,
1184 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1185
1186 return 12 + (plen + 7) / 8;
1187
1188 trunc:
1189 return -2;
1190 }
1191 #endif
1192
1193 static int
1194 decode_clnp_prefix(netdissect_options *ndo,
1195 const u_char *pptr, char *buf, u_int buflen)
1196 {
1197 uint8_t addr[19];
1198 u_int plen;
1199
1200 ND_TCHECK(pptr[0]);
1201 plen = pptr[0]; /* get prefix length */
1202
1203 if (152 < plen)
1204 return -1;
1205
1206 memset(&addr, 0, sizeof(addr));
1207 ND_TCHECK2(pptr[4], (plen + 7) / 8);
1208 memcpy(&addr, &pptr[4], (plen + 7) / 8);
1209 if (plen % 8) {
1210 addr[(plen + 7) / 8 - 1] &=
1211 ((0xff00 >> (plen % 8)) & 0xff);
1212 }
1213 snprintf(buf, buflen, "%s/%d",
1214 isonsap_string(ndo, addr,(plen + 7) / 8),
1215 plen);
1216
1217 return 1 + (plen + 7) / 8;
1218
1219 trunc:
1220 return -2;
1221 }
1222
1223 static int
1224 decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
1225 const u_char *pptr, char *buf, u_int buflen)
1226 {
1227 uint8_t addr[19];
1228 u_int plen;
1229
1230 ND_TCHECK(pptr[0]);
1231 plen = pptr[0]; /* get prefix length */
1232
1233 if ((24+64) > plen)
1234 return -1;
1235
1236 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1237
1238 if (152 < plen)
1239 return -1;
1240
1241 memset(&addr, 0, sizeof(addr));
1242 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1243 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1244 if (plen % 8) {
1245 addr[(plen + 7) / 8 - 1] &=
1246 ((0xff00 >> (plen % 8)) & 0xff);
1247 }
1248 /* the label may get offsetted by 4 bits so lets shift it right */
1249 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1250 bgp_vpn_rd_print(ndo, pptr+4),
1251 isonsap_string(ndo, addr,(plen + 7) / 8),
1252 plen,
1253 EXTRACT_24BITS(pptr+1)>>4,
1254 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1255
1256 return 12 + (plen + 7) / 8;
1257
1258 trunc:
1259 return -2;
1260 }
1261
1262 /*
1263 * bgp_attr_get_as_size
1264 *
1265 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1266 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1267 * support, exchange AS-Path with the same path-attribute type value 0x02.
1268 */
1269 static int
1270 bgp_attr_get_as_size(netdissect_options *ndo,
1271 uint8_t bgpa_type, const u_char *pptr, int len)
1272 {
1273 const u_char *tptr = pptr;
1274
1275 /*
1276 * If the path attribute is the optional AS4 path type, then we already
1277 * know, that ASs must be encoded in 4 byte format.
1278 */
1279 if (bgpa_type == BGPTYPE_AS4_PATH) {
1280 return 4;
1281 }
1282
1283 /*
1284 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1285 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1286 * each.
1287 */
1288 while (tptr < pptr + len) {
1289 ND_TCHECK(tptr[0]);
1290
1291 /*
1292 * If we do not find a valid segment type, our guess might be wrong.
1293 */
1294 if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
1295 goto trunc;
1296 }
1297 ND_TCHECK(tptr[1]);
1298 tptr += 2 + tptr[1] * 2;
1299 }
1300
1301 /*
1302 * If we correctly reached end of the AS path attribute data content,
1303 * then most likely ASs were indeed encoded as 2 bytes.
1304 */
1305 if (tptr == pptr + len) {
1306 return 2;
1307 }
1308
1309 trunc:
1310
1311 /*
1312 * We can come here, either we did not have enough data, or if we
1313 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1314 * so that calller can try to decode each AS as of 4 bytes. If indeed
1315 * there was not enough data, it will crib and end the parse anyways.
1316 */
1317 return 4;
1318 }
1319
1320 static int
1321 bgp_attr_print(netdissect_options *ndo,
1322 u_int atype, const u_char *pptr, u_int len)
1323 {
1324 int i;
1325 uint16_t af;
1326 uint8_t safi, snpa, nhlen;
1327 union { /* copy buffer for bandwidth values */
1328 float f;
1329 uint32_t i;
1330 } bw;
1331 int advance;
1332 u_int tlen;
1333 const u_char *tptr;
1334 char buf[MAXHOSTNAMELEN + 100];
1335 int as_size;
1336
1337 tptr = pptr;
1338 tlen=len;
1339
1340 switch (atype) {
1341 case BGPTYPE_ORIGIN:
1342 if (len != 1)
1343 ND_PRINT((ndo, "invalid len"));
1344 else {
1345 ND_TCHECK(*tptr);
1346 ND_PRINT((ndo, "%s", tok2str(bgp_origin_values,
1347 "Unknown Origin Typecode",
1348 tptr[0])));
1349 }
1350 break;
1351
1352
1353 /*
1354 * Process AS4 byte path and AS2 byte path attributes here.
1355 */
1356 case BGPTYPE_AS4_PATH:
1357 case BGPTYPE_AS_PATH:
1358 if (len % 2) {
1359 ND_PRINT((ndo, "invalid len"));
1360 break;
1361 }
1362 if (!len) {
1363 ND_PRINT((ndo, "empty"));
1364 break;
1365 }
1366
1367 /*
1368 * BGP updates exchanged between New speakers that support 4
1369 * byte AS, ASs are always encoded in 4 bytes. There is no
1370 * definitive way to find this, just by the packet's
1371 * contents. So, check for packet's TLV's sanity assuming
1372 * 2 bytes first, and it does not pass, assume that ASs are
1373 * encoded in 4 bytes format and move on.
1374 */
1375 as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
1376
1377 while (tptr < pptr + len) {
1378 ND_TCHECK(tptr[0]);
1379 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_open_values,
1380 "?", tptr[0])));
1381 for (i = 0; i < tptr[1] * as_size; i += as_size) {
1382 ND_TCHECK2(tptr[2 + i], as_size);
1383 ND_PRINT((ndo, "%s ",
1384 as_printf(ndo, astostr, sizeof(astostr),
1385 as_size == 2 ?
1386 EXTRACT_16BITS(&tptr[2 + i]) :
1387 EXTRACT_32BITS(&tptr[2 + i]))));
1388 }
1389 ND_TCHECK(tptr[0]);
1390 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_close_values,
1391 "?", tptr[0])));
1392 ND_TCHECK(tptr[1]);
1393 tptr += 2 + tptr[1] * as_size;
1394 }
1395 break;
1396 case BGPTYPE_NEXT_HOP:
1397 if (len != 4)
1398 ND_PRINT((ndo, "invalid len"));
1399 else {
1400 ND_TCHECK2(tptr[0], 4);
1401 ND_PRINT((ndo, "%s", getname(ndo, tptr)));
1402 }
1403 break;
1404 case BGPTYPE_MULTI_EXIT_DISC:
1405 case BGPTYPE_LOCAL_PREF:
1406 if (len != 4)
1407 ND_PRINT((ndo, "invalid len"));
1408 else {
1409 ND_TCHECK2(tptr[0], 4);
1410 ND_PRINT((ndo, "%u", EXTRACT_32BITS(tptr)));
1411 }
1412 break;
1413 case BGPTYPE_ATOMIC_AGGREGATE:
1414 if (len != 0)
1415 ND_PRINT((ndo, "invalid len"));
1416 break;
1417 case BGPTYPE_AGGREGATOR:
1418
1419 /*
1420 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1421 * the length of this PA can be either 6 bytes or 8 bytes.
1422 */
1423 if (len != 6 && len != 8) {
1424 ND_PRINT((ndo, "invalid len"));
1425 break;
1426 }
1427 ND_TCHECK2(tptr[0], len);
1428 if (len == 6) {
1429 ND_PRINT((ndo, " AS #%s, origin %s",
1430 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_16BITS(tptr)),
1431 getname(ndo, tptr + 2)));
1432 } else {
1433 ND_PRINT((ndo, " AS #%s, origin %s",
1434 as_printf(ndo, astostr, sizeof(astostr),
1435 EXTRACT_32BITS(tptr)), getname(ndo, tptr + 4)));
1436 }
1437 break;
1438 case BGPTYPE_AGGREGATOR4:
1439 if (len != 8) {
1440 ND_PRINT((ndo, "invalid len"));
1441 break;
1442 }
1443 ND_TCHECK2(tptr[0], 8);
1444 ND_PRINT((ndo, " AS #%s, origin %s",
1445 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr)),
1446 getname(ndo, tptr + 4)));
1447 break;
1448 case BGPTYPE_COMMUNITIES:
1449 if (len % 4) {
1450 ND_PRINT((ndo, "invalid len"));
1451 break;
1452 }
1453 while (tlen>0) {
1454 uint32_t comm;
1455 ND_TCHECK2(tptr[0], 4);
1456 comm = EXTRACT_32BITS(tptr);
1457 switch (comm) {
1458 case BGP_COMMUNITY_NO_EXPORT:
1459 ND_PRINT((ndo, " NO_EXPORT"));
1460 break;
1461 case BGP_COMMUNITY_NO_ADVERT:
1462 ND_PRINT((ndo, " NO_ADVERTISE"));
1463 break;
1464 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
1465 ND_PRINT((ndo, " NO_EXPORT_SUBCONFED"));
1466 break;
1467 default:
1468 ND_PRINT((ndo, "%u:%u%s",
1469 (comm >> 16) & 0xffff,
1470 comm & 0xffff,
1471 (tlen>4) ? ", " : ""));
1472 break;
1473 }
1474 tlen -=4;
1475 tptr +=4;
1476 }
1477 break;
1478 case BGPTYPE_ORIGINATOR_ID:
1479 if (len != 4) {
1480 ND_PRINT((ndo, "invalid len"));
1481 break;
1482 }
1483 ND_TCHECK2(tptr[0], 4);
1484 ND_PRINT((ndo, "%s",getname(ndo, tptr)));
1485 break;
1486 case BGPTYPE_CLUSTER_LIST:
1487 if (len % 4) {
1488 ND_PRINT((ndo, "invalid len"));
1489 break;
1490 }
1491 while (tlen>0) {
1492 ND_TCHECK2(tptr[0], 4);
1493 ND_PRINT((ndo, "%s%s",
1494 getname(ndo, tptr),
1495 (tlen>4) ? ", " : ""));
1496 tlen -=4;
1497 tptr +=4;
1498 }
1499 break;
1500 case BGPTYPE_MP_REACH_NLRI:
1501 ND_TCHECK2(tptr[0], 3);
1502 af = EXTRACT_16BITS(tptr);
1503 safi = tptr[2];
1504
1505 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1506 tok2str(af_values, "Unknown AFI", af),
1507 af,
1508 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1509 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1510 safi));
1511
1512 switch(af<<8 | safi) {
1513 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1514 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1515 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1516 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1517 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1518 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1519 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1520 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1521 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1522 case (AFNUM_INET<<8 | SAFNUM_MDT):
1523 #ifdef INET6
1524 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1525 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1526 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1527 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1528 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1529 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1530 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1531 #endif
1532 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1533 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1534 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1535 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1536 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1537 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1538 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1539 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1540 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1541 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1542 break;
1543 default:
1544 ND_TCHECK2(tptr[0], tlen);
1545 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1546 if (ndo->ndo_vflag <= 1)
1547 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1548 goto done;
1549 break;
1550 }
1551
1552 tptr +=3;
1553
1554 ND_TCHECK(tptr[0]);
1555 nhlen = tptr[0];
1556 tlen = nhlen;
1557 tptr++;
1558
1559 if (tlen) {
1560 int nnh = 0;
1561 ND_PRINT((ndo, "\n\t nexthop: "));
1562 while (tlen > 0) {
1563 if ( nnh++ > 0 ) {
1564 ND_PRINT((ndo, ", " ));
1565 }
1566 switch(af<<8 | safi) {
1567 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1568 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1569 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1570 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1571 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1572 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1573 case (AFNUM_INET<<8 | SAFNUM_MDT):
1574 if (tlen < (int)sizeof(struct in_addr)) {
1575 ND_PRINT((ndo, "invalid len"));
1576 tlen = 0;
1577 } else {
1578 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1579 ND_PRINT((ndo, "%s",getname(ndo, tptr)));
1580 tlen -= sizeof(struct in_addr);
1581 tptr += sizeof(struct in_addr);
1582 }
1583 break;
1584 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1585 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1586 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1587 if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
1588 ND_PRINT((ndo, "invalid len"));
1589 tlen = 0;
1590 } else {
1591 ND_TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1592 ND_PRINT((ndo, "RD: %s, %s",
1593 bgp_vpn_rd_print(ndo, tptr),
1594 getname(ndo, tptr+BGP_VPN_RD_LEN)));
1595 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1596 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1597 }
1598 break;
1599 #ifdef INET6
1600 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1601 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1602 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1603 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1604 if (tlen < (int)sizeof(struct in6_addr)) {
1605 ND_PRINT((ndo, "invalid len"));
1606 tlen = 0;
1607 } else {
1608 ND_TCHECK2(tptr[0], sizeof(struct in6_addr));
1609 ND_PRINT((ndo, "%s", getname6(ndo, tptr)));
1610 tlen -= sizeof(struct in6_addr);
1611 tptr += sizeof(struct in6_addr);
1612 }
1613 break;
1614 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1615 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1616 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1617 if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
1618 ND_PRINT((ndo, "invalid len"));
1619 tlen = 0;
1620 } else {
1621 ND_TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1622 ND_PRINT((ndo, "RD: %s, %s",
1623 bgp_vpn_rd_print(ndo, tptr),
1624 getname6(ndo, tptr+BGP_VPN_RD_LEN)));
1625 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1626 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1627 }
1628 break;
1629 #endif
1630 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1631 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1632 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1633 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1634 if (tlen < (int)sizeof(struct in_addr)) {
1635 ND_PRINT((ndo, "invalid len"));
1636 tlen = 0;
1637 } else {
1638 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1639 ND_PRINT((ndo, "%s", getname(ndo, tptr)));
1640 tlen -= (sizeof(struct in_addr));
1641 tptr += (sizeof(struct in_addr));
1642 }
1643 break;
1644 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1645 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1646 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1647 ND_TCHECK2(tptr[0], tlen);
1648 ND_PRINT((ndo, "%s", isonsap_string(ndo, tptr, tlen)));
1649 tptr += tlen;
1650 tlen = 0;
1651 break;
1652
1653 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1654 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1655 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1656 if (tlen < BGP_VPN_RD_LEN+1) {
1657 ND_PRINT((ndo, "invalid len"));
1658 tlen = 0;
1659 } else {
1660 ND_TCHECK2(tptr[0], tlen);
1661 ND_PRINT((ndo, "RD: %s, %s",
1662 bgp_vpn_rd_print(ndo, tptr),
1663 isonsap_string(ndo, tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)));
1664 /* rfc986 mapped IPv4 address ? */
1665 if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) == 0x47000601)
1666 ND_PRINT((ndo, " = %s", getname(ndo, tptr+BGP_VPN_RD_LEN+4)));
1667 #ifdef INET6
1668 /* rfc1888 mapped IPv6 address ? */
1669 else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) == 0x350000)
1670 ND_PRINT((ndo, " = %s", getname6(ndo, tptr+BGP_VPN_RD_LEN+3)));
1671 #endif
1672 tptr += tlen;
1673 tlen = 0;
1674 }
1675 break;
1676 default:
1677 ND_TCHECK2(tptr[0], tlen);
1678 ND_PRINT((ndo, "no AFI %u/SAFI %u decoder", af, safi));
1679 if (ndo->ndo_vflag <= 1)
1680 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1681 tptr += tlen;
1682 tlen = 0;
1683 goto done;
1684 break;
1685 }
1686 }
1687 }
1688 ND_PRINT((ndo, ", nh-length: %u", nhlen));
1689 tptr += tlen;
1690
1691 ND_TCHECK(tptr[0]);
1692 snpa = tptr[0];
1693 tptr++;
1694
1695 if (snpa) {
1696 ND_PRINT((ndo, "\n\t %u SNPA", snpa));
1697 for (/*nothing*/; snpa > 0; snpa--) {
1698 ND_TCHECK(tptr[0]);
1699 ND_PRINT((ndo, "\n\t %d bytes", tptr[0]));
1700 tptr += tptr[0] + 1;
1701 }
1702 } else {
1703 ND_PRINT((ndo, ", no SNPA"));
1704 }
1705
1706 while (len - (tptr - pptr) > 0) {
1707 switch (af<<8 | safi) {
1708 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1709 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1710 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1711 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1712 if (advance == -1)
1713 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1714 else if (advance == -2)
1715 goto trunc;
1716 else if (advance == -3)
1717 break; /* bytes left, but not enough */
1718 else
1719 ND_PRINT((ndo, "\n\t %s", buf));
1720 break;
1721 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1722 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1723 if (advance == -1)
1724 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1725 else if (advance == -2)
1726 goto trunc;
1727 else if (advance == -3)
1728 break; /* bytes left, but not enough */
1729 else
1730 ND_PRINT((ndo, "\n\t %s", buf));
1731 break;
1732 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1733 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1734 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1735 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1736 if (advance == -1)
1737 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1738 else if (advance == -2)
1739 goto trunc;
1740 else
1741 ND_PRINT((ndo, "\n\t %s", buf));
1742 break;
1743 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1744 advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
1745 if (advance == -1)
1746 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1747 else if (advance == -2)
1748 goto trunc;
1749 else
1750 ND_PRINT((ndo, "\n\t %s", buf));
1751 break;
1752 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1753 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
1754 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
1755 if (advance == -1)
1756 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1757 else if (advance == -2)
1758 goto trunc;
1759 else
1760 ND_PRINT((ndo, "\n\t %s", buf));
1761 break;
1762
1763 case (AFNUM_INET<<8 | SAFNUM_MDT):
1764 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
1765 if (advance == -1)
1766 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1767 else if (advance == -2)
1768 goto trunc;
1769 else
1770 ND_PRINT((ndo, "\n\t %s", buf));
1771 break;
1772 #ifdef INET6
1773 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1774 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1775 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1776 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1777 if (advance == -1)
1778 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1779 else if (advance == -2)
1780 goto trunc;
1781 else if (advance == -3)
1782 break; /* bytes left, but not enough */
1783 else
1784 ND_PRINT((ndo, "\n\t %s", buf));
1785 break;
1786 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1787 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1788 if (advance == -1)
1789 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1790 else if (advance == -2)
1791 goto trunc;
1792 else if (advance == -3)
1793 break; /* bytes left, but not enough */
1794 else
1795 ND_PRINT((ndo, "\n\t %s", buf));
1796 break;
1797 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1798 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1799 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1800 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1801 if (advance == -1)
1802 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1803 else if (advance == -2)
1804 goto trunc;
1805 else
1806 ND_PRINT((ndo, "\n\t %s", buf));
1807 break;
1808 #endif
1809 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1810 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1811 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1812 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1813 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1814 if (advance == -1)
1815 ND_PRINT((ndo, "\n\t (illegal length)"));
1816 else if (advance == -2)
1817 goto trunc;
1818 else
1819 ND_PRINT((ndo, "\n\t %s", buf));
1820 break;
1821 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1822 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1823 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1824 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1825 if (advance == -1)
1826 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1827 else if (advance == -2)
1828 goto trunc;
1829 else
1830 ND_PRINT((ndo, "\n\t %s", buf));
1831 break;
1832 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1833 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1834 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1835 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1836 if (advance == -1)
1837 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1838 else if (advance == -2)
1839 goto trunc;
1840 else
1841 ND_PRINT((ndo, "\n\t %s", buf));
1842 break;
1843 default:
1844 ND_TCHECK2(*tptr,tlen);
1845 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1846 if (ndo->ndo_vflag <= 1)
1847 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1848 advance = 0;
1849 tptr = pptr + len;
1850 break;
1851 }
1852 if (advance < 0)
1853 break;
1854 tptr += advance;
1855 }
1856 done:
1857 break;
1858
1859 case BGPTYPE_MP_UNREACH_NLRI:
1860 ND_TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
1861 af = EXTRACT_16BITS(tptr);
1862 safi = tptr[2];
1863
1864 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1865 tok2str(af_values, "Unknown AFI", af),
1866 af,
1867 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1868 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1869 safi));
1870
1871 if (len == BGP_MP_NLRI_MINSIZE)
1872 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
1873
1874 tptr += 3;
1875
1876 while (len - (tptr - pptr) > 0) {
1877 switch (af<<8 | safi) {
1878 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1879 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1880 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1881 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1882 if (advance == -1)
1883 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1884 else if (advance == -2)
1885 goto trunc;
1886 else if (advance == -3)
1887 break; /* bytes left, but not enough */
1888 else
1889 ND_PRINT((ndo, "\n\t %s", buf));
1890 break;
1891 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1892 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1893 if (advance == -1)
1894 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1895 else if (advance == -2)
1896 goto trunc;
1897 else if (advance == -3)
1898 break; /* bytes left, but not enough */
1899 else
1900 ND_PRINT((ndo, "\n\t %s", buf));
1901 break;
1902 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1903 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1904 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1905 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1906 if (advance == -1)
1907 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1908 else if (advance == -2)
1909 goto trunc;
1910 else
1911 ND_PRINT((ndo, "\n\t %s", buf));
1912 break;
1913 #ifdef INET6
1914 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1915 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1916 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1917 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1918 if (advance == -1)
1919 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1920 else if (advance == -2)
1921 goto trunc;
1922 else if (advance == -3)
1923 break; /* bytes left, but not enough */
1924 else
1925 ND_PRINT((ndo, "\n\t %s", buf));
1926 break;
1927 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1928 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1929 if (advance == -1)
1930 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1931 else if (advance == -2)
1932 goto trunc;
1933 else if (advance == -3)
1934 break; /* bytes left, but not enough */
1935 else
1936 ND_PRINT((ndo, "\n\t %s", buf));
1937 break;
1938 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1939 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1940 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1941 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1942 if (advance == -1)
1943 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1944 else if (advance == -2)
1945 goto trunc;
1946 else
1947 ND_PRINT((ndo, "\n\t %s", buf));
1948 break;
1949 #endif
1950 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1951 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1952 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1953 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1954 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1955 if (advance == -1)
1956 ND_PRINT((ndo, "\n\t (illegal length)"));
1957 else if (advance == -2)
1958 goto trunc;
1959 else
1960 ND_PRINT((ndo, "\n\t %s", buf));
1961 break;
1962 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1963 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1964 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1965 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1966 if (advance == -1)
1967 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1968 else if (advance == -2)
1969 goto trunc;
1970 else
1971 ND_PRINT((ndo, "\n\t %s", buf));
1972 break;
1973 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1974 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1975 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1976 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1977 if (advance == -1)
1978 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1979 else if (advance == -2)
1980 goto trunc;
1981 else
1982 ND_PRINT((ndo, "\n\t %s", buf));
1983 break;
1984 case (AFNUM_INET<<8 | SAFNUM_MDT):
1985 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
1986 if (advance == -1)
1987 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1988 else if (advance == -2)
1989 goto trunc;
1990 else
1991 ND_PRINT((ndo, "\n\t %s", buf));
1992 break;
1993 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1994 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
1995 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
1996 if (advance == -1)
1997 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1998 else if (advance == -2)
1999 goto trunc;
2000 else
2001 ND_PRINT((ndo, "\n\t %s", buf));
2002 break;
2003 default:
2004 ND_TCHECK2(*(tptr-3),tlen);
2005 ND_PRINT((ndo, "no AFI %u / SAFI %u decoder", af, safi));
2006 if (ndo->ndo_vflag <= 1)
2007 print_unknown_data(ndo, tptr-3, "\n\t ", tlen);
2008 advance = 0;
2009 tptr = pptr + len;
2010 break;
2011 }
2012 if (advance < 0)
2013 break;
2014 tptr += advance;
2015 }
2016 break;
2017 case BGPTYPE_EXTD_COMMUNITIES:
2018 if (len % 8) {
2019 ND_PRINT((ndo, "invalid len"));
2020 break;
2021 }
2022 while (tlen>0) {
2023 uint16_t extd_comm;
2024
2025 ND_TCHECK2(tptr[0], 2);
2026 extd_comm=EXTRACT_16BITS(tptr);
2027
2028 ND_PRINT((ndo, "\n\t %s (0x%04x), Flags [%s]",
2029 tok2str(bgp_extd_comm_subtype_values,
2030 "unknown extd community typecode",
2031 extd_comm),
2032 extd_comm,
2033 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)));
2034
2035 ND_TCHECK2(*(tptr+2), 6);
2036 switch(extd_comm) {
2037 case BGP_EXT_COM_RT_0:
2038 case BGP_EXT_COM_RO_0:
2039 case BGP_EXT_COM_L2VPN_RT_0:
2040 ND_PRINT((ndo, ": %u:%u (= %s)",
2041 EXTRACT_16BITS(tptr+2),
2042 EXTRACT_32BITS(tptr+4),
2043 getname(ndo, tptr+4)));
2044 break;
2045 case BGP_EXT_COM_RT_1:
2046 case BGP_EXT_COM_RO_1:
2047 case BGP_EXT_COM_L2VPN_RT_1:
2048 case BGP_EXT_COM_VRF_RT_IMP:
2049 ND_PRINT((ndo, ": %s:%u",
2050 getname(ndo, tptr+2),
2051 EXTRACT_16BITS(tptr+6)));
2052 break;
2053 case BGP_EXT_COM_RT_2:
2054 case BGP_EXT_COM_RO_2:
2055 ND_PRINT((ndo, ": %s:%u",
2056 as_printf(ndo, astostr, sizeof(astostr),
2057 EXTRACT_32BITS(tptr+2)), EXTRACT_16BITS(tptr+6)));
2058 break;
2059 case BGP_EXT_COM_LINKBAND:
2060 bw.i = EXTRACT_32BITS(tptr+2);
2061 ND_PRINT((ndo, ": bandwidth: %.3f Mbps",
2062 bw.f*8/1000000));
2063 break;
2064 case BGP_EXT_COM_VPN_ORIGIN:
2065 case BGP_EXT_COM_VPN_ORIGIN2:
2066 case BGP_EXT_COM_VPN_ORIGIN3:
2067 case BGP_EXT_COM_VPN_ORIGIN4:
2068 case BGP_EXT_COM_OSPF_RID:
2069 case BGP_EXT_COM_OSPF_RID2:
2070 ND_PRINT((ndo, "%s", getname(ndo, tptr+2)));
2071 break;
2072 case BGP_EXT_COM_OSPF_RTYPE:
2073 case BGP_EXT_COM_OSPF_RTYPE2:
2074 ND_PRINT((ndo, ": area:%s, router-type:%s, metric-type:%s%s",
2075 getname(ndo, tptr+2),
2076 tok2str(bgp_extd_comm_ospf_rtype_values,
2077 "unknown (0x%02x)",
2078 *(tptr+6)),
2079 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
2080 ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""));
2081 break;
2082 case BGP_EXT_COM_L2INFO:
2083 ND_PRINT((ndo, ": %s Control Flags [0x%02x]:MTU %u",
2084 tok2str(l2vpn_encaps_values,
2085 "unknown encaps",
2086 *(tptr+2)),
2087 *(tptr+3),
2088 EXTRACT_16BITS(tptr+4)));
2089 break;
2090 case BGP_EXT_COM_SOURCE_AS:
2091 ND_PRINT((ndo, ": AS %u", EXTRACT_16BITS(tptr+2)));
2092 break;
2093 default:
2094 ND_TCHECK2(*tptr,8);
2095 print_unknown_data(ndo, tptr, "\n\t ", 8);
2096 break;
2097 }
2098 tlen -=8;
2099 tptr +=8;
2100 }
2101 break;
2102
2103 case BGPTYPE_PMSI_TUNNEL:
2104 {
2105 uint8_t tunnel_type, flags;
2106
2107 tunnel_type = *(tptr+1);
2108 flags = *tptr;
2109 tlen = len;
2110
2111 ND_TCHECK2(tptr[0], 5);
2112 ND_PRINT((ndo, "\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2113 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
2114 tunnel_type,
2115 bittok2str(bgp_pmsi_flag_values, "none", flags),
2116 EXTRACT_24BITS(tptr+2)>>4));
2117
2118 tptr +=5;
2119 tlen -= 5;
2120
2121 switch (tunnel_type) {
2122 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
2123 case BGP_PMSI_TUNNEL_PIM_BIDIR:
2124 ND_TCHECK2(tptr[0], 8);
2125 ND_PRINT((ndo, "\n\t Sender %s, P-Group %s",
2126 ipaddr_string(ndo, tptr),
2127 ipaddr_string(ndo, tptr+4)));
2128 break;
2129
2130 case BGP_PMSI_TUNNEL_PIM_SSM:
2131 ND_TCHECK2(tptr[0], 8);
2132 ND_PRINT((ndo, "\n\t Root-Node %s, P-Group %s",
2133 ipaddr_string(ndo, tptr),
2134 ipaddr_string(ndo, tptr+4)));
2135 break;
2136 case BGP_PMSI_TUNNEL_INGRESS:
2137 ND_TCHECK2(tptr[0], 4);
2138 ND_PRINT((ndo, "\n\t Tunnel-Endpoint %s",
2139 ipaddr_string(ndo, tptr)));
2140 break;
2141 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
2142 case BGP_PMSI_TUNNEL_LDP_MP2MP:
2143 ND_TCHECK2(tptr[0], 8);
2144 ND_PRINT((ndo, "\n\t Root-Node %s, LSP-ID 0x%08x",
2145 ipaddr_string(ndo, tptr),
2146 EXTRACT_32BITS(tptr+4)));
2147 break;
2148 case BGP_PMSI_TUNNEL_RSVP_P2MP:
2149 ND_TCHECK2(tptr[0], 8);
2150 ND_PRINT((ndo, "\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2151 ipaddr_string(ndo, tptr),
2152 EXTRACT_32BITS(tptr+4)));
2153 break;
2154 default:
2155 if (ndo->ndo_vflag <= 1) {
2156 print_unknown_data(ndo, tptr, "\n\t ", tlen);
2157 }
2158 }
2159 break;
2160 }
2161 case BGPTYPE_AIGP:
2162 {
2163 u_int8_t type;
2164 u_int16_t length;
2165
2166 ND_TCHECK2(tptr[0], 3);
2167
2168 tlen = len;
2169
2170 while (tlen >= 3) {
2171
2172 type = *tptr;
2173 length = EXTRACT_16BITS(tptr+1);
2174
2175 ND_PRINT((ndo, "\n\t %s TLV (%u), length %u",
2176 tok2str(bgp_aigp_values, "Unknown", type),
2177 type, length));
2178
2179
2180 /*
2181 * Check if we can read the TLV data.
2182 */
2183 ND_TCHECK2(tptr[3], length - 3);
2184
2185 switch (type) {
2186
2187 case BGP_AIGP_TLV:
2188 ND_TCHECK2(tptr[3], 8);
2189 ND_PRINT((ndo, ", metric %" PRIu64,
2190 EXTRACT_64BITS(tptr+3)));
2191 break;
2192
2193 default:
2194 if (ndo->ndo_vflag <= 1) {
2195 print_unknown_data(ndo, tptr+3,"\n\t ", length-3);
2196 }
2197 }
2198
2199 tptr += length;
2200 tlen -= length;
2201 }
2202 break;
2203 }
2204 case BGPTYPE_ATTR_SET:
2205 ND_TCHECK2(tptr[0], 4);
2206 if (len < 4)
2207 goto trunc;
2208 ND_PRINT((ndo, "\n\t Origin AS: %s",
2209 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr))));
2210 tptr+=4;
2211 len -=4;
2212
2213 while (len) {
2214 u_int aflags, alenlen, alen;
2215
2216 ND_TCHECK2(tptr[0], 2);
2217 if (len < 2)
2218 goto trunc;
2219 aflags = *tptr;
2220 atype = *(tptr + 1);
2221 tptr += 2;
2222 len -= 2;
2223 alenlen = bgp_attr_lenlen(aflags, tptr);
2224 ND_TCHECK2(tptr[0], alenlen);
2225 if (len < alenlen)
2226 goto trunc;
2227 alen = bgp_attr_len(aflags, tptr);
2228 tptr += alenlen;
2229 len -= alenlen;
2230
2231 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2232 tok2str(bgp_attr_values,
2233 "Unknown Attribute", atype),
2234 atype,
2235 alen));
2236
2237 if (aflags) {
2238 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2239 aflags & 0x80 ? "O" : "",
2240 aflags & 0x40 ? "T" : "",
2241 aflags & 0x20 ? "P" : "",
2242 aflags & 0x10 ? "E" : ""));
2243 if (aflags & 0xf)
2244 ND_PRINT((ndo, "+%x", aflags & 0xf));
2245 ND_PRINT((ndo, "]: "));
2246 }
2247 /* FIXME check for recursion */
2248 if (!bgp_attr_print(ndo, atype, tptr, alen))
2249 return 0;
2250 tptr += alen;
2251 len -= alen;
2252 }
2253 break;
2254
2255
2256 default:
2257 ND_TCHECK2(*pptr,len);
2258 ND_PRINT((ndo, "\n\t no Attribute %u decoder", atype)); /* we have no decoder for the attribute */
2259 if (ndo->ndo_vflag <= 1)
2260 print_unknown_data(ndo, pptr, "\n\t ", len);
2261 break;
2262 }
2263 if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
2264 ND_TCHECK2(*pptr,len);
2265 print_unknown_data(ndo, pptr, "\n\t ", len);
2266 }
2267 return 1;
2268
2269 trunc:
2270 return 0;
2271 }
2272
2273 static void
2274 bgp_capabilities_print(netdissect_options *ndo,
2275 const u_char *opt, int caps_len)
2276 {
2277 int cap_type, cap_len, tcap_len, cap_offset;
2278 int i = 0;
2279
2280 while (i < caps_len) {
2281 ND_TCHECK2(opt[i], BGP_CAP_HEADER_SIZE);
2282 cap_type=opt[i];
2283 cap_len=opt[i+1];
2284 tcap_len=cap_len;
2285 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2286 tok2str(bgp_capcode_values, "Unknown",
2287 cap_type),
2288 cap_type,
2289 cap_len));
2290 ND_TCHECK2(opt[i+2], cap_len);
2291 switch (cap_type) {
2292 case BGP_CAPCODE_MP:
2293 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u)",
2294 tok2str(af_values, "Unknown",
2295 EXTRACT_16BITS(opt+i+2)),
2296 EXTRACT_16BITS(opt+i+2),
2297 tok2str(bgp_safi_values, "Unknown",
2298 opt[i+5]),
2299 opt[i+5]));
2300 break;
2301 case BGP_CAPCODE_RESTART:
2302 ND_PRINT((ndo, "\n\t\tRestart Flags: [%s], Restart Time %us",
2303 ((opt[i+2])&0x80) ? "R" : "none",
2304 EXTRACT_16BITS(opt+i+2)&0xfff));
2305 tcap_len-=2;
2306 cap_offset=4;
2307 while(tcap_len>=4) {
2308 ND_PRINT((ndo, "\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2309 tok2str(af_values,"Unknown",
2310 EXTRACT_16BITS(opt+i+cap_offset)),
2311 EXTRACT_16BITS(opt+i+cap_offset),
2312 tok2str(bgp_safi_values,"Unknown",
2313 opt[i+cap_offset+2]),
2314 opt[i+cap_offset+2],
2315 ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ));
2316 tcap_len-=4;
2317 cap_offset+=4;
2318 }
2319 break;
2320 case BGP_CAPCODE_RR:
2321 case BGP_CAPCODE_RR_CISCO:
2322 break;
2323 case BGP_CAPCODE_AS_NEW:
2324
2325 /*
2326 * Extract the 4 byte AS number encoded.
2327 */
2328 if (cap_len == 4) {
2329 ND_PRINT((ndo, "\n\t\t 4 Byte AS %s",
2330 as_printf(ndo, astostr, sizeof(astostr),
2331 EXTRACT_32BITS(opt + i + 2))));
2332 }
2333 break;
2334 case BGP_CAPCODE_ADD_PATH:
2335 cap_offset=2;
2336 if (tcap_len == 0) {
2337 ND_PRINT((ndo, " (bogus)")); /* length */
2338 break;
2339 }
2340 while (tcap_len > 0) {
2341 if (tcap_len < 4) {
2342 ND_PRINT((ndo, "\n\t\t(invalid)"));
2343 break;
2344 }
2345 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2346 tok2str(af_values,"Unknown",EXTRACT_16BITS(opt+i+cap_offset)),
2347 EXTRACT_16BITS(opt+i+cap_offset),
2348 tok2str(bgp_safi_values,"Unknown",opt[i+cap_offset+2]),
2349 opt[i+cap_offset+2],
2350 tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",opt[i+cap_offset+3])
2351 ));
2352 tcap_len-=4;
2353 cap_offset+=4;
2354 }
2355 break;
2356 default:
2357 ND_PRINT((ndo, "\n\t\tno decoder for Capability %u",
2358 cap_type));
2359 if (ndo->ndo_vflag <= 1)
2360 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2361 break;
2362 }
2363 if (ndo->ndo_vflag > 1 && cap_len > 0) {
2364 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2365 }
2366 i += BGP_CAP_HEADER_SIZE + cap_len;
2367 }
2368 return;
2369
2370 trunc:
2371 ND_PRINT((ndo, "[|BGP]"));
2372 }
2373
2374 static void
2375 bgp_open_print(netdissect_options *ndo,
2376 const u_char *dat, int length)
2377 {
2378 struct bgp_open bgpo;
2379 struct bgp_opt bgpopt;
2380 const u_char *opt;
2381 int i;
2382
2383 ND_TCHECK2(dat[0], BGP_OPEN_SIZE);
2384 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
2385
2386 ND_PRINT((ndo, "\n\t Version %d, ", bgpo.bgpo_version));
2387 ND_PRINT((ndo, "my AS %s, ",
2388 as_printf(ndo, astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))));
2389 ND_PRINT((ndo, "Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)));
2390 ND_PRINT((ndo, "ID %s", getname(ndo, (u_char *)&bgpo.bgpo_id)));
2391 ND_PRINT((ndo, "\n\t Optional parameters, length: %u", bgpo.bgpo_optlen));
2392
2393 /* some little sanity checking */
2394 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
2395 return;
2396
2397 /* ugly! */
2398 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
2399 opt++;
2400
2401 i = 0;
2402 while (i < bgpo.bgpo_optlen) {
2403 ND_TCHECK2(opt[i], BGP_OPT_SIZE);
2404 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
2405 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
2406 ND_PRINT((ndo, "\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len));
2407 break;
2408 }
2409
2410 ND_PRINT((ndo, "\n\t Option %s (%u), length: %u",
2411 tok2str(bgp_opt_values,"Unknown",
2412 bgpopt.bgpopt_type),
2413 bgpopt.bgpopt_type,
2414 bgpopt.bgpopt_len));
2415
2416 /* now let's decode the options we know*/
2417 switch(bgpopt.bgpopt_type) {
2418
2419 case BGP_OPT_CAP:
2420 bgp_capabilities_print(ndo, &opt[i+BGP_OPT_SIZE],
2421 bgpopt.bgpopt_len);
2422 break;
2423
2424 case BGP_OPT_AUTH:
2425 default:
2426 ND_PRINT((ndo, "\n\t no decoder for option %u",
2427 bgpopt.bgpopt_type));
2428 break;
2429 }
2430 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
2431 }
2432 return;
2433 trunc:
2434 ND_PRINT((ndo, "[|BGP]"));
2435 }
2436
2437 static void
2438 bgp_update_print(netdissect_options *ndo,
2439 const u_char *dat, int length)
2440 {
2441 struct bgp bgp;
2442 const u_char *p;
2443 int withdrawn_routes_len;
2444 int len;
2445 int i;
2446 #ifndef INET6
2447 char buf[MAXHOSTNAMELEN + 100];
2448 int wpfx;
2449 #endif
2450
2451 ND_TCHECK2(dat[0], BGP_SIZE);
2452 if (length < BGP_SIZE)
2453 goto trunc;
2454 memcpy(&bgp, dat, BGP_SIZE);
2455 p = dat + BGP_SIZE; /*XXX*/
2456 length -= BGP_SIZE;
2457
2458 /* Unfeasible routes */
2459 ND_TCHECK2(p[0], 2);
2460 if (length < 2)
2461 goto trunc;
2462 withdrawn_routes_len = EXTRACT_16BITS(p);
2463 p += 2;
2464 length -= 2;
2465 if (withdrawn_routes_len) {
2466 /*
2467 * Without keeping state from the original NLRI message,
2468 * it's not possible to tell if this a v4 or v6 route,
2469 * so only try to decode it if we're not v6 enabled.
2470 */
2471 ND_TCHECK2(p[0], withdrawn_routes_len);
2472 if (length < withdrawn_routes_len)
2473 goto trunc;
2474 #ifdef INET6
2475 ND_PRINT((ndo, "\n\t Withdrawn routes: %d bytes", withdrawn_routes_len));
2476 p += withdrawn_routes_len;
2477 length -= withdrawn_routes_len;
2478 #else
2479 if (withdrawn_routes_len < 2)
2480 goto trunc;
2481 length -= 2;
2482 withdrawn_routes_len -= 2;
2483
2484
2485 ND_PRINT((ndo, "\n\t Withdrawn routes:"));
2486
2487 while(withdrawn_routes_len > 0) {
2488 wpfx = decode_prefix4(ndo, p, withdrawn_routes_len, buf, sizeof(buf));
2489 if (wpfx == -1) {
2490 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2491 break;
2492 } else if (wpfx == -2)
2493 goto trunc;
2494 else if (wpfx == -3)
2495 goto trunc; /* bytes left, but not enough */
2496 else {
2497 ND_PRINT((ndo, "\n\t %s", buf));
2498 p += wpfx;
2499 length -= wpfx;
2500 withdrawn_routes_len -= wpfx;
2501 }
2502 }
2503 #endif
2504 }
2505
2506 ND_TCHECK2(p[0], 2);
2507 if (length < 2)
2508 goto trunc;
2509 len = EXTRACT_16BITS(p);
2510 p += 2;
2511 length -= 2;
2512
2513 if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
2514 /* No withdrawn routes, no path attributes, no NLRI */
2515 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
2516 return;
2517 }
2518
2519 if (len) {
2520 /* do something more useful!*/
2521 while (len) {
2522 int aflags, atype, alenlen, alen;
2523
2524 ND_TCHECK2(p[0], 2);
2525 if (len < 2)
2526 goto trunc;
2527 if (length < 2)
2528 goto trunc;
2529 aflags = *p;
2530 atype = *(p + 1);
2531 p += 2;
2532 len -= 2;
2533 length -= 2;
2534 alenlen = bgp_attr_lenlen(aflags, p);
2535 ND_TCHECK2(p[0], alenlen);
2536 if (len < alenlen)
2537 goto trunc;
2538 if (length < alenlen)
2539 goto trunc;
2540 alen = bgp_attr_len(aflags, p);
2541 p += alenlen;
2542 len -= alenlen;
2543 length -= alenlen;
2544
2545 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2546 tok2str(bgp_attr_values, "Unknown Attribute",
2547 atype),
2548 atype,
2549 alen));
2550
2551 if (aflags) {
2552 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2553 aflags & 0x80 ? "O" : "",
2554 aflags & 0x40 ? "T" : "",
2555 aflags & 0x20 ? "P" : "",
2556 aflags & 0x10 ? "E" : ""));
2557 if (aflags & 0xf)
2558 ND_PRINT((ndo, "+%x", aflags & 0xf));
2559 ND_PRINT((ndo, "]: "));
2560 }
2561 if (len < alen)
2562 goto trunc;
2563 if (length < alen)
2564 goto trunc;
2565 if (!bgp_attr_print(ndo, atype, p, alen))
2566 goto trunc;
2567 p += alen;
2568 len -= alen;
2569 length -= alen;
2570 }
2571 }
2572
2573 if (length) {
2574 /*
2575 * XXX - what if they're using the "Advertisement of
2576 * Multiple Paths in BGP" feature:
2577 *
2578 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
2579 *
2580 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
2581 */
2582 ND_PRINT((ndo, "\n\t Updated routes:"));
2583 while (length) {
2584 char buf[MAXHOSTNAMELEN + 100];
2585 i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
2586 if (i == -1) {
2587 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2588 break;
2589 } else if (i == -2)
2590 goto trunc;
2591 else if (i == -3)
2592 goto trunc; /* bytes left, but not enough */
2593 else {
2594 ND_PRINT((ndo, "\n\t %s", buf));
2595 p += i;
2596 length -= i;
2597 }
2598 }
2599 }
2600 return;
2601 trunc:
2602 ND_PRINT((ndo, "[|BGP]"));
2603 }
2604
2605 static void
2606 bgp_notification_print(netdissect_options *ndo,
2607 const u_char *dat, int length)
2608 {
2609 struct bgp_notification bgpn;
2610 const u_char *tptr;
2611
2612 ND_TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
2613 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
2614
2615 /* some little sanity checking */
2616 if (length<BGP_NOTIFICATION_SIZE)
2617 return;
2618
2619 ND_PRINT((ndo, ", %s (%u)",
2620 tok2str(bgp_notify_major_values, "Unknown Error",
2621 bgpn.bgpn_major),
2622 bgpn.bgpn_major));
2623
2624 switch (bgpn.bgpn_major) {
2625
2626 case BGP_NOTIFY_MAJOR_MSG:
2627 ND_PRINT((ndo, ", subcode %s (%u)",
2628 tok2str(bgp_notify_minor_msg_values, "Unknown",
2629 bgpn.bgpn_minor),
2630 bgpn.bgpn_minor));
2631 break;
2632 case BGP_NOTIFY_MAJOR_OPEN:
2633 ND_PRINT((ndo, ", subcode %s (%u)",
2634 tok2str(bgp_notify_minor_open_values, "Unknown",
2635 bgpn.bgpn_minor),
2636 bgpn.bgpn_minor));
2637 break;
2638 case BGP_NOTIFY_MAJOR_UPDATE:
2639 ND_PRINT((ndo, ", subcode %s (%u)",
2640 tok2str(bgp_notify_minor_update_values, "Unknown",
2641 bgpn.bgpn_minor),
2642 bgpn.bgpn_minor));
2643 break;
2644 case BGP_NOTIFY_MAJOR_CAP:
2645 ND_PRINT((ndo, " subcode %s (%u)",
2646 tok2str(bgp_notify_minor_cap_values, "Unknown",
2647 bgpn.bgpn_minor),
2648 bgpn.bgpn_minor));
2649 case BGP_NOTIFY_MAJOR_CEASE:
2650 ND_PRINT((ndo, ", subcode %s (%u)",
2651 tok2str(bgp_notify_minor_cease_values, "Unknown",
2652 bgpn.bgpn_minor),
2653 bgpn.bgpn_minor));
2654
2655 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2656 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2657 */
2658 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
2659 tptr = dat + BGP_NOTIFICATION_SIZE;
2660 ND_TCHECK2(*tptr, 7);
2661 ND_PRINT((ndo, ", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2662 tok2str(af_values, "Unknown",
2663 EXTRACT_16BITS(tptr)),
2664 EXTRACT_16BITS(tptr),
2665 tok2str(bgp_safi_values, "Unknown", *(tptr+2)),
2666 *(tptr+2),
2667 EXTRACT_32BITS(tptr+3)));
2668 }
2669 break;
2670 default:
2671 break;
2672 }
2673
2674 return;
2675 trunc:
2676 ND_PRINT((ndo, "[|BGP]"));
2677 }
2678
2679 static void
2680 bgp_route_refresh_print(netdissect_options *ndo,
2681 const u_char *pptr, int len)
2682 {
2683 const struct bgp_route_refresh *bgp_route_refresh_header;
2684
2685 ND_TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
2686
2687 /* some little sanity checking */
2688 if (len<BGP_ROUTE_REFRESH_SIZE)
2689 return;
2690
2691 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
2692
2693 ND_PRINT((ndo, "\n\t AFI %s (%u), SAFI %s (%u)",
2694 tok2str(af_values,"Unknown",
2695 /* this stinks but the compiler pads the structure
2696 * weird */
2697 EXTRACT_16BITS(&bgp_route_refresh_header->afi)),
2698 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
2699 tok2str(bgp_safi_values,"Unknown",
2700 bgp_route_refresh_header->safi),
2701 bgp_route_refresh_header->safi));
2702
2703 if (ndo->ndo_vflag > 1) {
2704 ND_TCHECK2(*pptr, len);
2705 print_unknown_data(ndo, pptr, "\n\t ", len);
2706 }
2707
2708 return;
2709 trunc:
2710 ND_PRINT((ndo, "[|BGP]"));
2711 }
2712
2713 static int
2714 bgp_header_print(netdissect_options *ndo,
2715 const u_char *dat, int length)
2716 {
2717 struct bgp bgp;
2718
2719 ND_TCHECK2(dat[0], BGP_SIZE);
2720 memcpy(&bgp, dat, BGP_SIZE);
2721 ND_PRINT((ndo, "\n\t%s Message (%u), length: %u",
2722 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
2723 bgp.bgp_type,
2724 length));
2725
2726 switch (bgp.bgp_type) {
2727 case BGP_OPEN:
2728 bgp_open_print(ndo, dat, length);
2729 break;
2730 case BGP_UPDATE:
2731 bgp_update_print(ndo, dat, length);
2732 break;
2733 case BGP_NOTIFICATION:
2734 bgp_notification_print(ndo, dat, length);
2735 break;
2736 case BGP_KEEPALIVE:
2737 break;
2738 case BGP_ROUTE_REFRESH:
2739 bgp_route_refresh_print(ndo, dat, length);
2740 break;
2741 default:
2742 /* we have no decoder for the BGP message */
2743 ND_TCHECK2(*dat, length);
2744 ND_PRINT((ndo, "\n\t no Message %u decoder", bgp.bgp_type));
2745 print_unknown_data(ndo, dat, "\n\t ", length);
2746 break;
2747 }
2748 return 1;
2749 trunc:
2750 ND_PRINT((ndo, "[|BGP]"));
2751 return 0;
2752 }
2753
2754 void
2755 bgp_print(netdissect_options *ndo,
2756 const u_char *dat, int length)
2757 {
2758 const u_char *p;
2759 const u_char *ep;
2760 const u_char *start;
2761 const u_char marker[] = {
2762 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2763 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2764 };
2765 struct bgp bgp;
2766 uint16_t hlen;
2767
2768 ep = dat + length;
2769 if (ndo->ndo_snapend < dat + length)
2770 ep = ndo->ndo_snapend;
2771
2772 ND_PRINT((ndo, ": BGP"));
2773
2774 if (ndo->ndo_vflag < 1) /* lets be less chatty */
2775 return;
2776
2777 p = dat;
2778 start = p;
2779 while (p < ep) {
2780 if (!ND_TTEST2(p[0], 1))
2781 break;
2782 if (p[0] != 0xff) {
2783 p++;
2784 continue;
2785 }
2786
2787 if (!ND_TTEST2(p[0], sizeof(marker)))
2788 break;
2789 if (memcmp(p, marker, sizeof(marker)) != 0) {
2790 p++;
2791 continue;
2792 }
2793
2794 /* found BGP header */
2795 ND_TCHECK2(p[0], BGP_SIZE); /*XXX*/
2796 memcpy(&bgp, p, BGP_SIZE);
2797
2798 if (start != p)
2799 ND_PRINT((ndo, " [|BGP]"));
2800
2801 hlen = ntohs(bgp.bgp_len);
2802 if (hlen < BGP_SIZE) {
2803 ND_PRINT((ndo, "\n[|BGP Bogus header length %u < %u]", hlen,
2804 BGP_SIZE));
2805 break;
2806 }
2807
2808 if (ND_TTEST2(p[0], hlen)) {
2809 if (!bgp_header_print(ndo, p, hlen))
2810 return;
2811 p += hlen;
2812 start = p;
2813 } else {
2814 ND_PRINT((ndo, "\n[|BGP %s]",
2815 tok2str(bgp_msg_values,
2816 "Unknown Message Type",
2817 bgp.bgp_type)));
2818 break;
2819 }
2820 }
2821
2822 return;
2823
2824 trunc:
2825 ND_PRINT((ndo, " [|BGP]"));
2826 }
2827
2828 /*
2829 * Local Variables:
2830 * c-style: whitesmith
2831 * c-basic-offset: 4
2832 * End:
2833 */