2 * Copyright (c) 1998-2007 The TCPDUMP project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
15 * support for the IEEE Link Discovery Protocol as per 802.1ab
17 * Original code by Hannes Gredler (hannes@juniper.net)
21 static const char rcsid
[] _U_
=
22 "@(#) $Header: /tcpdump/master/tcpdump/print-lldp.c,v 1.3 2007-08-09 18:43:44 hannes Exp $";
29 #include <tcpdump-stdinc.h>
35 #include "interface.h"
37 #include "addrtoname.h"
41 #define LLDP_EXTRACT_TYPE(x) (((x)&0xfe00)>>9)
42 #define LLDP_EXTRACT_LEN(x) ((x)&0x01ff)
47 #define LLDP_END_TLV 0
48 #define LLDP_CHASSIS_ID_TLV 1
49 #define LLDP_PORT_ID_TLV 2
50 #define LLDP_TTL_TLV 3
51 #define LLDP_PORT_DESCR_TLV 4
52 #define LLDP_SYSTEM_NAME_TLV 5
53 #define LLDP_SYSTEM_DESCR_TLV 6
54 #define LLDP_SYSTEM_CAP_TLV 7
55 #define LLDP_MGMT_ADDR_TLV 8
56 #define LLDP_PRIVATE_TLV 127
58 static const struct tok lldp_tlv_values
[] = {
59 { LLDP_END_TLV
, "End" },
60 { LLDP_CHASSIS_ID_TLV
, "Chassis ID" },
61 { LLDP_PORT_ID_TLV
, "Port ID" },
62 { LLDP_TTL_TLV
, "Time to Live" },
63 { LLDP_PORT_DESCR_TLV
, "Port Description" },
64 { LLDP_SYSTEM_NAME_TLV
, "System Name" },
65 { LLDP_SYSTEM_DESCR_TLV
, "System Description" },
66 { LLDP_SYSTEM_CAP_TLV
, "System Capabilities" },
67 { LLDP_MGMT_ADDR_TLV
, "Management Address" },
68 { LLDP_PRIVATE_TLV
, "Organization specific" },
75 #define LLDP_CHASSIS_CHASSIS_COMP_SUBTYPE 1
76 #define LLDP_CHASSIS_INTF_ALIAS_SUBTYPE 2
77 #define LLDP_CHASSIS_PORT_COMP_SUBTYPE 3
78 #define LLDP_CHASSIS_MAC_ADDR_SUBTYPE 4
79 #define LLDP_CHASSIS_NETWORK_ADDR_SUBTYPE 5
80 #define LLDP_CHASSIS_INTF_NAME_SUBTYPE 6
81 #define LLDP_CHASSIS_LOCAL_SUBTYPE 7
83 static const struct tok lldp_chassis_subtype_values
[] = {
84 { LLDP_CHASSIS_CHASSIS_COMP_SUBTYPE
, "Chassis component"},
85 { LLDP_CHASSIS_INTF_ALIAS_SUBTYPE
, "Interface alias"},
86 { LLDP_CHASSIS_PORT_COMP_SUBTYPE
, "Port component"},
87 { LLDP_CHASSIS_MAC_ADDR_SUBTYPE
, "MAC address"},
88 { LLDP_CHASSIS_NETWORK_ADDR_SUBTYPE
, "Network address"},
89 { LLDP_CHASSIS_INTF_NAME_SUBTYPE
, "Interface name"},
90 { LLDP_CHASSIS_LOCAL_SUBTYPE
, "Local"},
97 #define LLDP_PORT_INTF_ALIAS_SUBTYPE 1
98 #define LLDP_PORT_PORT_COMP_SUBTYPE 2
99 #define LLDP_PORT_MAC_ADDR_SUBTYPE 3
100 #define LLDP_PORT_NETWORK_ADDR_SUBTYPE 4
101 #define LLDP_PORT_INTF_NAME_SUBTYPE 5
102 #define LLDP_PORT_AGENT_CIRC_ID_SUBTYPE 6
103 #define LLDP_PORT_LOCAL_SUBTYPE 7
105 static const struct tok lldp_port_subtype_values
[] = {
106 { LLDP_PORT_INTF_ALIAS_SUBTYPE
, "Interface alias"},
107 { LLDP_PORT_PORT_COMP_SUBTYPE
, "Port component"},
108 { LLDP_PORT_MAC_ADDR_SUBTYPE
, "MAC address"},
109 { LLDP_PORT_NETWORK_ADDR_SUBTYPE
, "Network Address"},
110 { LLDP_PORT_INTF_NAME_SUBTYPE
, "Interface Name"},
111 { LLDP_PORT_AGENT_CIRC_ID_SUBTYPE
, "Agent circuit ID"},
112 { LLDP_PORT_LOCAL_SUBTYPE
, "Local"},
117 * System Capabilities
119 #define LLDP_CAP_OTHER (1 << 0)
120 #define LLDP_CAP_REPEATER (1 << 1)
121 #define LLDP_CAP_BRIDGE (1 << 2)
122 #define LLDP_CAP_WLAN_AP (1 << 3)
123 #define LLDP_CAP_ROUTER (1 << 4)
124 #define LLDP_CAP_PHONE (1 << 5)
125 #define LLDP_CAP_DOCSIS (1 << 6)
126 #define LLDP_CAP_STATION_ONLY (1 << 7)
128 static const struct tok lldp_cap_values
[] = {
129 { LLDP_CAP_OTHER
, "Other"},
130 { LLDP_CAP_REPEATER
, "Repeater"},
131 { LLDP_CAP_BRIDGE
, "Bridge"},
132 { LLDP_CAP_WLAN_AP
, "WLAN AP"},
133 { LLDP_CAP_ROUTER
, "Router"},
134 { LLDP_CAP_PHONE
, "Telephone"},
135 { LLDP_CAP_DOCSIS
, "Docsis"},
136 { LLDP_CAP_STATION_ONLY
, "Station Only"},
141 * Interface numbering subtypes.
143 #define LLDP_INTF_NUMB_IFX_SUBTYPE 2
144 #define LLDP_INTF_NUMB_SYSPORT_SUBTYPE 3
146 static const struct tok lldp_intf_numb_subtype_values
[] = {
147 { LLDP_INTF_NUMB_IFX_SUBTYPE
, "Interface Index" },
148 { LLDP_INTF_NUMB_SYSPORT_SUBTYPE
, "System Port Number" },
152 #define LLDP_INTF_NUM_LEN 5
156 lldp_network_addr_print(const u_char
*tptr
) {
159 static char buf
[BUFSIZE
];
160 const char * (*pfunc
)(const u_char
*);
173 pfunc
= etheraddr_string
;
181 snprintf(buf
, sizeof(buf
), "AFI %s (%u) XXX",
182 tok2str(af_values
, "Unknown", af
), af
);
184 snprintf(buf
, sizeof(buf
), "AFI %s (%u): %s",
185 tok2str(af_values
, "Unknown", af
), af
, (*pfunc
)(tptr
+1));
192 lldp_mgmt_addr_tlv_print(const u_char
*pptr
, u_int len
) {
194 u_int8_t mgmt_addr_len
, intf_num_subtype
, oid_len
;
201 mgmt_addr_len
= *tptr
++;
204 if (tlen
< mgmt_addr_len
) {
208 printf("\n\t Management Address length %u, %s",
210 lldp_network_addr_print(tptr
));
211 tptr
+= mgmt_addr_len
;
212 tlen
-= mgmt_addr_len
;
214 if (tlen
< LLDP_INTF_NUM_LEN
) {
218 intf_num_subtype
= *tptr
;
219 printf("\n\t %s Interface Numbering (%u): %u",
220 tok2str(lldp_intf_numb_subtype_values
, "Unknown", intf_num_subtype
),
222 EXTRACT_32BITS(tptr
+1));
224 tptr
+= LLDP_INTF_NUM_LEN
;
225 tlen
-= LLDP_INTF_NUM_LEN
;
228 * The OID is optional.
234 printf("\n\t OID length %u", oid_len
);
235 safeputs((const char *)tptr
+1, oid_len
);
243 lldp_print(register const u_char
*pptr
, register u_int len
) {
246 u_int16_t tlv
, cap
, ena_cap
;
247 u_int oui
, tlen
, hexdump
, tlv_type
, tlv_len
;
254 printf("LLDP, length %u", len
);
257 while (tlen
>= sizeof(tlv
)) {
259 TCHECK2(*tptr
, sizeof(tlv
));
261 tlv
= EXTRACT_16BITS(tptr
);
263 tlv_type
= LLDP_EXTRACT_TYPE(tlv
);
264 tlv_len
= LLDP_EXTRACT_LEN(tlv
);
271 printf("\n\t%s TLV (%u), length %u",
272 tok2str(lldp_tlv_values
, "Unknown", tlv_type
),
276 /* infinite loop check */
277 if (!tlv_type
|| !tlv_len
) {
281 TCHECK2(*tptr
, tlv_len
);
286 printf(": TTL %us", EXTRACT_16BITS(tptr
));
290 case LLDP_SYSTEM_NAME_TLV
:
293 * The system name is also print in non-verbose mode
294 * similar to the CDP printer.
298 safeputs((const char *)tptr
, tlv_len
);
300 printf("LLDP, name ");
301 safeputs((const char *)tptr
, tlv_len
);
302 printf(", length %u", len
);
306 case LLDP_PORT_DESCR_TLV
:
309 safeputs((const char *)tptr
, tlv_len
);
313 case LLDP_SYSTEM_DESCR_TLV
:
316 safeputs((const char *)tptr
, tlv_len
);
321 case LLDP_CHASSIS_ID_TLV
:
324 printf("\n\t Subtype %s (%u): ",
325 tok2str(lldp_chassis_subtype_values
, "Unknown", subtype
),
329 case LLDP_CHASSIS_MAC_ADDR_SUBTYPE
:
330 printf("%s", etheraddr_string(tptr
+1));
333 case LLDP_CHASSIS_INTF_NAME_SUBTYPE
: /* fall through */
334 case LLDP_CHASSIS_LOCAL_SUBTYPE
:
335 case LLDP_CHASSIS_CHASSIS_COMP_SUBTYPE
:
336 case LLDP_CHASSIS_INTF_ALIAS_SUBTYPE
:
337 case LLDP_CHASSIS_PORT_COMP_SUBTYPE
:
338 safeputs((const char *)tptr
+1, tlv_len
-1);
341 case LLDP_CHASSIS_NETWORK_ADDR_SUBTYPE
:
342 printf("%s", lldp_network_addr_print(tptr
+1));
352 case LLDP_PORT_ID_TLV
:
355 printf("\n\t Subtype %s (%u): ",
356 tok2str(lldp_port_subtype_values
, "Unknown", subtype
),
360 case LLDP_PORT_MAC_ADDR_SUBTYPE
:
361 printf("%s", etheraddr_string(tptr
+1));
364 case LLDP_PORT_INTF_NAME_SUBTYPE
: /* fall through */
365 case LLDP_PORT_LOCAL_SUBTYPE
:
366 case LLDP_PORT_AGENT_CIRC_ID_SUBTYPE
:
367 case LLDP_PORT_INTF_ALIAS_SUBTYPE
:
368 case LLDP_PORT_PORT_COMP_SUBTYPE
:
369 safeputs((const char *)tptr
+1, tlv_len
-1);
372 case LLDP_PORT_NETWORK_ADDR_SUBTYPE
:
373 printf("%s", lldp_network_addr_print(tptr
+1));
383 case LLDP_PRIVATE_TLV
:
385 oui
= EXTRACT_24BITS(tptr
);
386 printf(": OUI %s (0x%06x)", tok2str(oui_values
, "Unknown", oui
), oui
);
391 case LLDP_SYSTEM_CAP_TLV
:
393 cap
= EXTRACT_16BITS(tptr
);
394 ena_cap
= EXTRACT_16BITS(tptr
+2);
395 printf("\n\t System Capabilities [%s] (0x%04x)",
396 bittok2str(lldp_cap_values
, "none", cap
), cap
);
397 printf("\n\t Enabled Capabilities [%s] (0x%04x)",
398 bittok2str(lldp_cap_values
, "none", ena_cap
), ena_cap
);
402 case LLDP_MGMT_ADDR_TLV
:
404 if (!lldp_mgmt_addr_tlv_print(tptr
, tlen
)) {
415 /* do we also want to see a hex dump ? */
416 if (vflag
> 1 || (vflag
&& hexdump
)) {
417 print_unknown_data(tptr
,"\n\t ", tlv_len
);
425 printf("\n\t[|LLDP]");
430 * c-style: whitesmith