xref: /netbsd-src/external/bsd/tcpdump/dist/print-lldp.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1 /*
2  * Copyright (c) 1998-2007 The TCPDUMP project
3  *
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.
14  *
15  * Original code by Hannes Gredler (hannes@gredler.at)
16  * IEEE and TIA extensions by Carles Kishimoto <carles.kishimoto@gmail.com>
17  * DCBX extensions by Kaladhar Musunuru <kaladharm@sourceforge.net>
18  */
19 
20 /* \summary: IEEE 802.1ab Link Layer Discovery Protocol (LLDP) printer */
21 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-lldp.c,v 1.9 2017/09/08 14:01:13 christos Exp $");
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <netdissect-stdinc.h>
32 
33 #include <stdio.h>
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 #include "addrtoname.h"
38 #include "af.h"
39 #include "oui.h"
40 
41 #define	LLDP_EXTRACT_TYPE(x) (((x)&0xfe00)>>9)
42 #define	LLDP_EXTRACT_LEN(x) ((x)&0x01ff)
43 
44 /*
45  * TLV type codes
46  */
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
57 
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" },
69     { 0, NULL}
70 };
71 
72 /*
73  * Chassis ID subtypes
74  */
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
82 
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"},
91     { 0, NULL}
92 };
93 
94 /*
95  * Port ID subtypes
96  */
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
104 
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"},
113     { 0, NULL}
114 };
115 
116 /*
117  * System Capabilities
118  */
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)
127 
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"},
137     { 0, NULL}
138 };
139 
140 #define LLDP_PRIVATE_8021_SUBTYPE_PORT_VLAN_ID		1
141 #define LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_VLAN_ID	2
142 #define LLDP_PRIVATE_8021_SUBTYPE_VLAN_NAME		3
143 #define LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_IDENTITY	4
144 #define LLDP_PRIVATE_8021_SUBTYPE_CONGESTION_NOTIFICATION 8
145 #define LLDP_PRIVATE_8021_SUBTYPE_ETS_CONFIGURATION       9
146 #define LLDP_PRIVATE_8021_SUBTYPE_ETS_RECOMMENDATION     10
147 #define LLDP_PRIVATE_8021_SUBTYPE_PFC_CONFIGURATION      11
148 #define LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY   12
149 #define LLDP_PRIVATE_8021_SUBTYPE_EVB                    13
150 #define LLDP_PRIVATE_8021_SUBTYPE_CDCP 			 14
151 
152 static const struct tok lldp_8021_subtype_values[] = {
153     { LLDP_PRIVATE_8021_SUBTYPE_PORT_VLAN_ID, "Port VLAN Id"},
154     { LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_VLAN_ID, "Port and Protocol VLAN ID"},
155     { LLDP_PRIVATE_8021_SUBTYPE_VLAN_NAME, "VLAN name"},
156     { LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_IDENTITY, "Protocol Identity"},
157     { LLDP_PRIVATE_8021_SUBTYPE_CONGESTION_NOTIFICATION, "Congestion Notification"},
158     { LLDP_PRIVATE_8021_SUBTYPE_ETS_CONFIGURATION, "ETS Configuration"},
159     { LLDP_PRIVATE_8021_SUBTYPE_ETS_RECOMMENDATION, "ETS Recommendation"},
160     { LLDP_PRIVATE_8021_SUBTYPE_PFC_CONFIGURATION, "Priority Flow Control Configuration"},
161     { LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY, "Application Priority"},
162     { LLDP_PRIVATE_8021_SUBTYPE_EVB, "EVB"},
163     { LLDP_PRIVATE_8021_SUBTYPE_CDCP,"CDCP"},
164     { 0, NULL}
165 };
166 
167 #define LLDP_8021_PORT_PROTOCOL_VLAN_SUPPORT       (1 <<  1)
168 #define LLDP_8021_PORT_PROTOCOL_VLAN_STATUS        (1 <<  2)
169 
170 static const struct tok lldp_8021_port_protocol_id_values[] = {
171     { LLDP_8021_PORT_PROTOCOL_VLAN_SUPPORT, "supported"},
172     { LLDP_8021_PORT_PROTOCOL_VLAN_STATUS, "enabled"},
173     { 0, NULL}
174 };
175 
176 #define LLDP_PRIVATE_8023_SUBTYPE_MACPHY        1
177 #define LLDP_PRIVATE_8023_SUBTYPE_MDIPOWER      2
178 #define LLDP_PRIVATE_8023_SUBTYPE_LINKAGGR      3
179 #define LLDP_PRIVATE_8023_SUBTYPE_MTU           4
180 
181 static const struct tok lldp_8023_subtype_values[] = {
182     { LLDP_PRIVATE_8023_SUBTYPE_MACPHY,	"MAC/PHY configuration/status"},
183     { LLDP_PRIVATE_8023_SUBTYPE_MDIPOWER, "Power via MDI"},
184     { LLDP_PRIVATE_8023_SUBTYPE_LINKAGGR, "Link aggregation"},
185     { LLDP_PRIVATE_8023_SUBTYPE_MTU, "Max frame size"},
186     { 0, NULL}
187 };
188 
189 #define LLDP_PRIVATE_TIA_SUBTYPE_CAPABILITIES                   1
190 #define LLDP_PRIVATE_TIA_SUBTYPE_NETWORK_POLICY                 2
191 #define LLDP_PRIVATE_TIA_SUBTYPE_LOCAL_ID                       3
192 #define LLDP_PRIVATE_TIA_SUBTYPE_EXTENDED_POWER_MDI             4
193 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_HARDWARE_REV         5
194 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_FIRMWARE_REV         6
195 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SOFTWARE_REV         7
196 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SERIAL_NUMBER        8
197 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MANUFACTURER_NAME    9
198 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MODEL_NAME           10
199 #define LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_ASSET_ID             11
200 
201 static const struct tok lldp_tia_subtype_values[] = {
202     { LLDP_PRIVATE_TIA_SUBTYPE_CAPABILITIES, "LLDP-MED Capabilities" },
203     { LLDP_PRIVATE_TIA_SUBTYPE_NETWORK_POLICY, "Network policy" },
204     { LLDP_PRIVATE_TIA_SUBTYPE_LOCAL_ID, "Location identification" },
205     { LLDP_PRIVATE_TIA_SUBTYPE_EXTENDED_POWER_MDI, "Extended power-via-MDI" },
206     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_HARDWARE_REV, "Inventory - hardware revision" },
207     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_FIRMWARE_REV, "Inventory - firmware revision" },
208     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SOFTWARE_REV, "Inventory - software revision" },
209     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SERIAL_NUMBER, "Inventory - serial number" },
210     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MANUFACTURER_NAME, "Inventory - manufacturer name" },
211     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MODEL_NAME, "Inventory - model name" },
212     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_ASSET_ID, "Inventory - asset ID" },
213     { 0, NULL}
214 };
215 
216 #define LLDP_PRIVATE_TIA_LOCATION_ALTITUDE_METERS       1
217 #define LLDP_PRIVATE_TIA_LOCATION_ALTITUDE_FLOORS       2
218 
219 static const struct tok lldp_tia_location_altitude_type_values[] = {
220     { LLDP_PRIVATE_TIA_LOCATION_ALTITUDE_METERS, "meters"},
221     { LLDP_PRIVATE_TIA_LOCATION_ALTITUDE_FLOORS, "floors"},
222     { 0, NULL}
223 };
224 
225 /* ANSI/TIA-1057 - Annex B */
226 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A1		1
227 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A2		2
228 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A3		3
229 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A4		4
230 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A5		5
231 #define LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A6		6
232 
233 static const struct tok lldp_tia_location_lci_catype_values[] = {
234     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A1, "national subdivisions (state,canton,region,province,prefecture)"},
235     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A2, "county, parish, gun, district"},
236     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A3, "city, township, shi"},
237     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A4, "city division, borough, city district, ward chou"},
238     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A5, "neighborhood, block"},
239     { LLDP_PRIVATE_TIA_LOCATION_LCI_CATYPE_A6, "street"},
240     { 0, NULL}
241 };
242 
243 static const struct tok lldp_tia_location_lci_what_values[] = {
244     { 0, "location of DHCP server"},
245     { 1, "location of the network element believed to be closest to the client"},
246     { 2, "location of the client"},
247     { 0, NULL}
248 };
249 
250 /*
251  * From RFC 3636 - dot3MauType
252  */
253 #define		LLDP_MAU_TYPE_UNKNOWN		0
254 #define		LLDP_MAU_TYPE_AUI		1
255 #define		LLDP_MAU_TYPE_10BASE_5		2
256 #define		LLDP_MAU_TYPE_FOIRL		3
257 #define		LLDP_MAU_TYPE_10BASE_2		4
258 #define		LLDP_MAU_TYPE_10BASE_T		5
259 #define		LLDP_MAU_TYPE_10BASE_FP		6
260 #define		LLDP_MAU_TYPE_10BASE_FB		7
261 #define		LLDP_MAU_TYPE_10BASE_FL		8
262 #define		LLDP_MAU_TYPE_10BROAD36		9
263 #define		LLDP_MAU_TYPE_10BASE_T_HD	10
264 #define		LLDP_MAU_TYPE_10BASE_T_FD	11
265 #define		LLDP_MAU_TYPE_10BASE_FL_HD	12
266 #define		LLDP_MAU_TYPE_10BASE_FL_FD	13
267 #define		LLDP_MAU_TYPE_100BASE_T4	14
268 #define		LLDP_MAU_TYPE_100BASE_TX_HD	15
269 #define		LLDP_MAU_TYPE_100BASE_TX_FD	16
270 #define		LLDP_MAU_TYPE_100BASE_FX_HD	17
271 #define		LLDP_MAU_TYPE_100BASE_FX_FD	18
272 #define		LLDP_MAU_TYPE_100BASE_T2_HD	19
273 #define		LLDP_MAU_TYPE_100BASE_T2_FD	20
274 #define		LLDP_MAU_TYPE_1000BASE_X_HD	21
275 #define		LLDP_MAU_TYPE_1000BASE_X_FD	22
276 #define		LLDP_MAU_TYPE_1000BASE_LX_HD	23
277 #define		LLDP_MAU_TYPE_1000BASE_LX_FD	24
278 #define		LLDP_MAU_TYPE_1000BASE_SX_HD	25
279 #define		LLDP_MAU_TYPE_1000BASE_SX_FD	26
280 #define		LLDP_MAU_TYPE_1000BASE_CX_HD	27
281 #define		LLDP_MAU_TYPE_1000BASE_CX_FD	28
282 #define		LLDP_MAU_TYPE_1000BASE_T_HD	29
283 #define		LLDP_MAU_TYPE_1000BASE_T_FD	30
284 #define		LLDP_MAU_TYPE_10GBASE_X		31
285 #define		LLDP_MAU_TYPE_10GBASE_LX4	32
286 #define		LLDP_MAU_TYPE_10GBASE_R		33
287 #define		LLDP_MAU_TYPE_10GBASE_ER	34
288 #define		LLDP_MAU_TYPE_10GBASE_LR	35
289 #define		LLDP_MAU_TYPE_10GBASE_SR	36
290 #define		LLDP_MAU_TYPE_10GBASE_W		37
291 #define		LLDP_MAU_TYPE_10GBASE_EW	38
292 #define		LLDP_MAU_TYPE_10GBASE_LW	39
293 #define		LLDP_MAU_TYPE_10GBASE_SW	40
294 
295 static const struct tok lldp_mau_types_values[] = {
296     { LLDP_MAU_TYPE_UNKNOWN,            "Unknown"},
297     { LLDP_MAU_TYPE_AUI,                "AUI"},
298     { LLDP_MAU_TYPE_10BASE_5,           "10BASE_5"},
299     { LLDP_MAU_TYPE_FOIRL,              "FOIRL"},
300     { LLDP_MAU_TYPE_10BASE_2,           "10BASE2"},
301     { LLDP_MAU_TYPE_10BASE_T,           "10BASET duplex mode unknown"},
302     { LLDP_MAU_TYPE_10BASE_FP,          "10BASEFP"},
303     { LLDP_MAU_TYPE_10BASE_FB,          "10BASEFB"},
304     { LLDP_MAU_TYPE_10BASE_FL,          "10BASEFL duplex mode unknown"},
305     { LLDP_MAU_TYPE_10BROAD36,          "10BROAD36"},
306     { LLDP_MAU_TYPE_10BASE_T_HD,        "10BASET hdx"},
307     { LLDP_MAU_TYPE_10BASE_T_FD,        "10BASET fdx"},
308     { LLDP_MAU_TYPE_10BASE_FL_HD,       "10BASEFL hdx"},
309     { LLDP_MAU_TYPE_10BASE_FL_FD,       "10BASEFL fdx"},
310     { LLDP_MAU_TYPE_100BASE_T4,         "100BASET4"},
311     { LLDP_MAU_TYPE_100BASE_TX_HD,      "100BASETX hdx"},
312     { LLDP_MAU_TYPE_100BASE_TX_FD,      "100BASETX fdx"},
313     { LLDP_MAU_TYPE_100BASE_FX_HD,      "100BASEFX hdx"},
314     { LLDP_MAU_TYPE_100BASE_FX_FD,      "100BASEFX fdx"},
315     { LLDP_MAU_TYPE_100BASE_T2_HD,      "100BASET2 hdx"},
316     { LLDP_MAU_TYPE_100BASE_T2_FD,      "100BASET2 fdx"},
317     { LLDP_MAU_TYPE_1000BASE_X_HD,      "1000BASEX hdx"},
318     { LLDP_MAU_TYPE_1000BASE_X_FD,      "1000BASEX fdx"},
319     { LLDP_MAU_TYPE_1000BASE_LX_HD,     "1000BASELX hdx"},
320     { LLDP_MAU_TYPE_1000BASE_LX_FD,     "1000BASELX fdx"},
321     { LLDP_MAU_TYPE_1000BASE_SX_HD,     "1000BASESX hdx"},
322     { LLDP_MAU_TYPE_1000BASE_SX_FD,     "1000BASESX fdx"},
323     { LLDP_MAU_TYPE_1000BASE_CX_HD,     "1000BASECX hdx"},
324     { LLDP_MAU_TYPE_1000BASE_CX_FD,     "1000BASECX fdx"},
325     { LLDP_MAU_TYPE_1000BASE_T_HD,      "1000BASET hdx"},
326     { LLDP_MAU_TYPE_1000BASE_T_FD,      "1000BASET fdx"},
327     { LLDP_MAU_TYPE_10GBASE_X,          "10GBASEX"},
328     { LLDP_MAU_TYPE_10GBASE_LX4,        "10GBASELX4"},
329     { LLDP_MAU_TYPE_10GBASE_R,          "10GBASER"},
330     { LLDP_MAU_TYPE_10GBASE_ER,         "10GBASEER"},
331     { LLDP_MAU_TYPE_10GBASE_LR,         "10GBASELR"},
332     { LLDP_MAU_TYPE_10GBASE_SR,         "10GBASESR"},
333     { LLDP_MAU_TYPE_10GBASE_W,          "10GBASEW"},
334     { LLDP_MAU_TYPE_10GBASE_EW,         "10GBASEEW"},
335     { LLDP_MAU_TYPE_10GBASE_LW,         "10GBASELW"},
336     { LLDP_MAU_TYPE_10GBASE_SW,         "10GBASESW"},
337     { 0, NULL}
338 };
339 
340 #define LLDP_8023_AUTONEGOTIATION_SUPPORT       (1 <<  0)
341 #define LLDP_8023_AUTONEGOTIATION_STATUS        (1 <<  1)
342 
343 static const struct tok lldp_8023_autonegotiation_values[] = {
344     { LLDP_8023_AUTONEGOTIATION_SUPPORT, "supported"},
345     { LLDP_8023_AUTONEGOTIATION_STATUS, "enabled"},
346     { 0, NULL}
347 };
348 
349 #define LLDP_TIA_CAPABILITY_MED                         (1 <<  0)
350 #define LLDP_TIA_CAPABILITY_NETWORK_POLICY              (1 <<  1)
351 #define LLDP_TIA_CAPABILITY_LOCATION_IDENTIFICATION     (1 <<  2)
352 #define LLDP_TIA_CAPABILITY_EXTENDED_POWER_MDI_PSE      (1 <<  3)
353 #define LLDP_TIA_CAPABILITY_EXTENDED_POWER_MDI_PD       (1 <<  4)
354 #define LLDP_TIA_CAPABILITY_INVENTORY                   (1 <<  5)
355 
356 static const struct tok lldp_tia_capabilities_values[] = {
357     { LLDP_TIA_CAPABILITY_MED, "LLDP-MED capabilities"},
358     { LLDP_TIA_CAPABILITY_NETWORK_POLICY, "network policy"},
359     { LLDP_TIA_CAPABILITY_LOCATION_IDENTIFICATION, "location identification"},
360     { LLDP_TIA_CAPABILITY_EXTENDED_POWER_MDI_PSE, "extended power via MDI-PSE"},
361     { LLDP_TIA_CAPABILITY_EXTENDED_POWER_MDI_PD, "extended power via MDI-PD"},
362     { LLDP_TIA_CAPABILITY_INVENTORY, "Inventory"},
363     { 0, NULL}
364 };
365 
366 #define LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_1           1
367 #define LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_2           2
368 #define LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_3           3
369 #define LLDP_TIA_DEVICE_TYPE_NETWORK_CONNECTIVITY       4
370 
371 static const struct tok lldp_tia_device_type_values[] = {
372     { LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_1, "endpoint class 1"},
373     { LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_2, "endpoint class 2"},
374     { LLDP_TIA_DEVICE_TYPE_ENDPOINT_CLASS_3, "endpoint class 3"},
375     { LLDP_TIA_DEVICE_TYPE_NETWORK_CONNECTIVITY, "network connectivity"},
376     { 0, NULL}
377 };
378 
379 #define LLDP_TIA_APPLICATION_TYPE_VOICE                 1
380 #define LLDP_TIA_APPLICATION_TYPE_VOICE_SIGNALING       2
381 #define LLDP_TIA_APPLICATION_TYPE_GUEST_VOICE           3
382 #define LLDP_TIA_APPLICATION_TYPE_GUEST_VOICE_SIGNALING 4
383 #define LLDP_TIA_APPLICATION_TYPE_SOFTPHONE_VOICE       5
384 #define LLDP_TIA_APPLICATION_TYPE_VIDEO_CONFERENCING    6
385 #define LLDP_TIA_APPLICATION_TYPE_STREAMING_VIDEO       7
386 #define LLDP_TIA_APPLICATION_TYPE_VIDEO_SIGNALING       8
387 
388 static const struct tok lldp_tia_application_type_values[] = {
389     { LLDP_TIA_APPLICATION_TYPE_VOICE, "voice"},
390     { LLDP_TIA_APPLICATION_TYPE_VOICE_SIGNALING, "voice signaling"},
391     { LLDP_TIA_APPLICATION_TYPE_GUEST_VOICE, "guest voice"},
392     { LLDP_TIA_APPLICATION_TYPE_GUEST_VOICE_SIGNALING, "guest voice signaling"},
393     { LLDP_TIA_APPLICATION_TYPE_SOFTPHONE_VOICE, "softphone voice"},
394     { LLDP_TIA_APPLICATION_TYPE_VIDEO_CONFERENCING, "video conferencing"},
395     { LLDP_TIA_APPLICATION_TYPE_STREAMING_VIDEO, "streaming video"},
396     { LLDP_TIA_APPLICATION_TYPE_VIDEO_SIGNALING, "video signaling"},
397     { 0, NULL}
398 };
399 
400 #define LLDP_TIA_NETWORK_POLICY_X_BIT           (1 << 5)
401 #define LLDP_TIA_NETWORK_POLICY_T_BIT           (1 << 6)
402 #define LLDP_TIA_NETWORK_POLICY_U_BIT           (1 << 7)
403 
404 static const struct tok lldp_tia_network_policy_bits_values[] = {
405     { LLDP_TIA_NETWORK_POLICY_U_BIT, "Unknown"},
406     { LLDP_TIA_NETWORK_POLICY_T_BIT, "Tagged"},
407     { LLDP_TIA_NETWORK_POLICY_X_BIT, "reserved"},
408     { 0, NULL}
409 };
410 
411 #define LLDP_EXTRACT_NETWORK_POLICY_VLAN(x)           (((x)&0x1ffe)>>1)
412 #define LLDP_EXTRACT_NETWORK_POLICY_L2_PRIORITY(x)    (((x)&0x01ff)>>6)
413 #define LLDP_EXTRACT_NETWORK_POLICY_DSCP(x)           ((x)&0x003f)
414 
415 #define LLDP_TIA_LOCATION_DATA_FORMAT_COORDINATE_BASED  1
416 #define LLDP_TIA_LOCATION_DATA_FORMAT_CIVIC_ADDRESS     2
417 #define LLDP_TIA_LOCATION_DATA_FORMAT_ECS_ELIN          3
418 
419 static const struct tok lldp_tia_location_data_format_values[] = {
420     { LLDP_TIA_LOCATION_DATA_FORMAT_COORDINATE_BASED, "coordinate-based LCI"},
421     { LLDP_TIA_LOCATION_DATA_FORMAT_CIVIC_ADDRESS, "civic address LCI"},
422     { LLDP_TIA_LOCATION_DATA_FORMAT_ECS_ELIN, "ECS ELIN"},
423     { 0, NULL}
424 };
425 
426 #define LLDP_TIA_LOCATION_DATUM_WGS_84          1
427 #define LLDP_TIA_LOCATION_DATUM_NAD_83_NAVD_88  2
428 #define LLDP_TIA_LOCATION_DATUM_NAD_83_MLLW     3
429 
430 static const struct tok lldp_tia_location_datum_type_values[] = {
431     { LLDP_TIA_LOCATION_DATUM_WGS_84, "World Geodesic System 1984"},
432     { LLDP_TIA_LOCATION_DATUM_NAD_83_NAVD_88, "North American Datum 1983 (NAVD88)"},
433     { LLDP_TIA_LOCATION_DATUM_NAD_83_MLLW, "North American Datum 1983 (MLLW)"},
434     { 0, NULL}
435 };
436 
437 #define LLDP_TIA_POWER_SOURCE_PSE               1
438 #define LLDP_TIA_POWER_SOURCE_LOCAL             2
439 #define LLDP_TIA_POWER_SOURCE_PSE_AND_LOCAL     3
440 
441 static const struct tok lldp_tia_power_source_values[] = {
442     { LLDP_TIA_POWER_SOURCE_PSE, "PSE - primary power source"},
443     { LLDP_TIA_POWER_SOURCE_LOCAL, "local - backup power source"},
444     { LLDP_TIA_POWER_SOURCE_PSE_AND_LOCAL, "PSE+local - reserved"},
445     { 0, NULL}
446 };
447 
448 #define LLDP_TIA_POWER_PRIORITY_CRITICAL        1
449 #define LLDP_TIA_POWER_PRIORITY_HIGH            2
450 #define LLDP_TIA_POWER_PRIORITY_LOW             3
451 
452 static const struct tok lldp_tia_power_priority_values[] = {
453     { LLDP_TIA_POWER_PRIORITY_CRITICAL, "critical"},
454     { LLDP_TIA_POWER_PRIORITY_HIGH, "high"},
455     { LLDP_TIA_POWER_PRIORITY_LOW, "low"},
456     { 0, NULL}
457 };
458 
459 #define LLDP_TIA_POWER_VAL_MAX               1024
460 
461 static const struct tok lldp_tia_inventory_values[] = {
462     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_HARDWARE_REV, "Hardware revision" },
463     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_FIRMWARE_REV, "Firmware revision" },
464     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SOFTWARE_REV, "Software revision" },
465     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SERIAL_NUMBER, "Serial number" },
466     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MANUFACTURER_NAME, "Manufacturer name" },
467     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MODEL_NAME, "Model name" },
468     { LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_ASSET_ID, "Asset ID" },
469     { 0, NULL}
470 };
471 
472 /*
473  * From RFC 3636 - ifMauAutoNegCapAdvertisedBits
474  */
475 #define	 LLDP_MAU_PMD_OTHER			(1 <<  15)
476 #define	 LLDP_MAU_PMD_10BASE_T			(1 <<  14)
477 #define	 LLDP_MAU_PMD_10BASE_T_FD		(1 <<  13)
478 #define	 LLDP_MAU_PMD_100BASE_T4		(1 <<  12)
479 #define	 LLDP_MAU_PMD_100BASE_TX		(1 <<  11)
480 #define	 LLDP_MAU_PMD_100BASE_TX_FD		(1 <<  10)
481 #define	 LLDP_MAU_PMD_100BASE_T2		(1 <<  9)
482 #define	 LLDP_MAU_PMD_100BASE_T2_FD		(1 <<  8)
483 #define	 LLDP_MAU_PMD_FDXPAUSE			(1 <<  7)
484 #define	 LLDP_MAU_PMD_FDXAPAUSE			(1 <<  6)
485 #define	 LLDP_MAU_PMD_FDXSPAUSE			(1 <<  5)
486 #define	 LLDP_MAU_PMD_FDXBPAUSE			(1 <<  4)
487 #define	 LLDP_MAU_PMD_1000BASE_X		(1 <<  3)
488 #define	 LLDP_MAU_PMD_1000BASE_X_FD		(1 <<  2)
489 #define	 LLDP_MAU_PMD_1000BASE_T		(1 <<  1)
490 #define	 LLDP_MAU_PMD_1000BASE_T_FD		(1 <<  0)
491 
492 static const struct tok lldp_pmd_capability_values[] = {
493     { LLDP_MAU_PMD_10BASE_T,		"10BASE-T hdx"},
494     { LLDP_MAU_PMD_10BASE_T_FD,	        "10BASE-T fdx"},
495     { LLDP_MAU_PMD_100BASE_T4,		"100BASE-T4"},
496     { LLDP_MAU_PMD_100BASE_TX,		"100BASE-TX hdx"},
497     { LLDP_MAU_PMD_100BASE_TX_FD,	"100BASE-TX fdx"},
498     { LLDP_MAU_PMD_100BASE_T2,		"100BASE-T2 hdx"},
499     { LLDP_MAU_PMD_100BASE_T2_FD,	"100BASE-T2 fdx"},
500     { LLDP_MAU_PMD_FDXPAUSE,		"Pause for fdx links"},
501     { LLDP_MAU_PMD_FDXAPAUSE,		"Asym PAUSE for fdx"},
502     { LLDP_MAU_PMD_FDXSPAUSE,		"Sym PAUSE for fdx"},
503     { LLDP_MAU_PMD_FDXBPAUSE,		"Asym and Sym PAUSE for fdx"},
504     { LLDP_MAU_PMD_1000BASE_X,		"1000BASE-{X LX SX CX} hdx"},
505     { LLDP_MAU_PMD_1000BASE_X_FD,	"1000BASE-{X LX SX CX} fdx"},
506     { LLDP_MAU_PMD_1000BASE_T,		"1000BASE-T hdx"},
507     { LLDP_MAU_PMD_1000BASE_T_FD,	"1000BASE-T fdx"},
508     { 0, NULL}
509 };
510 
511 #define	LLDP_MDI_PORT_CLASS			(1 <<  0)
512 #define	LLDP_MDI_POWER_SUPPORT			(1 <<  1)
513 #define LLDP_MDI_POWER_STATE			(1 <<  2)
514 #define LLDP_MDI_PAIR_CONTROL_ABILITY		(1 <<  3)
515 
516 static const struct tok lldp_mdi_values[] = {
517     { LLDP_MDI_PORT_CLASS, 		"PSE"},
518     { LLDP_MDI_POWER_SUPPORT, 		"supported"},
519     { LLDP_MDI_POWER_STATE, 		"enabled"},
520     { LLDP_MDI_PAIR_CONTROL_ABILITY, 	"can be controlled"},
521     { 0, NULL}
522 };
523 
524 #define LLDP_MDI_PSE_PORT_POWER_PAIRS_SIGNAL	1
525 #define LLDP_MDI_PSE_PORT_POWER_PAIRS_SPARE	2
526 
527 static const struct tok lldp_mdi_power_pairs_values[] = {
528     { LLDP_MDI_PSE_PORT_POWER_PAIRS_SIGNAL,	"signal"},
529     { LLDP_MDI_PSE_PORT_POWER_PAIRS_SPARE,	"spare"},
530     { 0, NULL}
531 };
532 
533 #define LLDP_MDI_POWER_CLASS0		1
534 #define LLDP_MDI_POWER_CLASS1		2
535 #define LLDP_MDI_POWER_CLASS2		3
536 #define LLDP_MDI_POWER_CLASS3		4
537 #define LLDP_MDI_POWER_CLASS4		5
538 
539 static const struct tok lldp_mdi_power_class_values[] = {
540     { LLDP_MDI_POWER_CLASS0,     "class0"},
541     { LLDP_MDI_POWER_CLASS1,     "class1"},
542     { LLDP_MDI_POWER_CLASS2,     "class2"},
543     { LLDP_MDI_POWER_CLASS3,     "class3"},
544     { LLDP_MDI_POWER_CLASS4,     "class4"},
545     { 0, NULL}
546 };
547 
548 #define LLDP_AGGREGATION_CAPABILTIY     (1 <<  0)
549 #define LLDP_AGGREGATION_STATUS         (1 <<  1)
550 
551 static const struct tok lldp_aggregation_values[] = {
552     { LLDP_AGGREGATION_CAPABILTIY, "supported"},
553     { LLDP_AGGREGATION_STATUS, "enabled"},
554     { 0, NULL}
555 };
556 
557 /*
558  * DCBX protocol subtypes.
559  */
560 #define LLDP_DCBX_SUBTYPE_1                1
561 #define LLDP_DCBX_SUBTYPE_2                2
562 
563 static const struct tok lldp_dcbx_subtype_values[] = {
564     { LLDP_DCBX_SUBTYPE_1, "DCB Capability Exchange Protocol Rev 1" },
565     { LLDP_DCBX_SUBTYPE_2, "DCB Capability Exchange Protocol Rev 1.01" },
566     { 0, NULL}
567 };
568 
569 #define LLDP_DCBX_CONTROL_TLV                1
570 #define LLDP_DCBX_PRIORITY_GROUPS_TLV        2
571 #define LLDP_DCBX_PRIORITY_FLOW_CONTROL_TLV  3
572 #define LLDP_DCBX_APPLICATION_TLV            4
573 
574 /*
575  * Interface numbering subtypes.
576  */
577 #define LLDP_INTF_NUMB_IFX_SUBTYPE         2
578 #define LLDP_INTF_NUMB_SYSPORT_SUBTYPE     3
579 
580 static const struct tok lldp_intf_numb_subtype_values[] = {
581     { LLDP_INTF_NUMB_IFX_SUBTYPE, "Interface Index" },
582     { LLDP_INTF_NUMB_SYSPORT_SUBTYPE, "System Port Number" },
583     { 0, NULL}
584 };
585 
586 #define LLDP_INTF_NUM_LEN                  5
587 
588 #define LLDP_EVB_MODE_NOT_SUPPORTED	0
589 #define LLDP_EVB_MODE_EVB_BRIDGE	1
590 #define LLDP_EVB_MODE_EVB_STATION	2
591 #define LLDP_EVB_MODE_RESERVED		3
592 
593 static const struct tok lldp_evb_mode_values[]={
594     { LLDP_EVB_MODE_NOT_SUPPORTED, "Not Supported"},
595     { LLDP_EVB_MODE_EVB_BRIDGE, "EVB Bridge"},
596     { LLDP_EVB_MODE_EVB_STATION, "EVB Staion"},
597     { LLDP_EVB_MODE_RESERVED, "Reserved for future Standardization"},
598     { 0, NULL},
599 };
600 
601 #define NO_OF_BITS 8
602 #define LLDP_PRIVATE_8021_SUBTYPE_CONGESTION_NOTIFICATION_LENGTH  6
603 #define LLDP_PRIVATE_8021_SUBTYPE_ETS_CONFIGURATION_LENGTH       25
604 #define LLDP_PRIVATE_8021_SUBTYPE_ETS_RECOMMENDATION_LENGTH      25
605 #define LLDP_PRIVATE_8021_SUBTYPE_PFC_CONFIGURATION_LENGTH        6
606 #define LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY_MIN_LENGTH 5
607 #define LLDP_PRIVATE_8021_SUBTYPE_EVB_LENGTH                      9
608 #define LLDP_PRIVATE_8021_SUBTYPE_CDCP_MIN_LENGTH                 8
609 
610 #define LLDP_IANA_SUBTYPE_MUDURL 1
611 
612 static const struct tok lldp_iana_subtype_values[] =   {
613     { LLDP_IANA_SUBTYPE_MUDURL, "MUD-URL" },
614     { 0, NULL }
615 };
616 
617 
618 static void
619 print_ets_priority_assignment_table(netdissect_options *ndo,
620                                     const u_char *ptr)
621 {
622     ND_PRINT((ndo, "\n\t    Priority Assignment Table"));
623     ND_PRINT((ndo, "\n\t     Priority : 0   1   2   3   4   5   6   7"));
624     ND_PRINT((ndo, "\n\t     Value    : %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d",
625             ptr[0]>>4,ptr[0]&0x0f,ptr[1]>>4,ptr[1]&0x0f,ptr[2]>>4,
626             ptr[2] & 0x0f, ptr[3] >> 4, ptr[3] & 0x0f));
627 }
628 
629 static void
630 print_tc_bandwidth_table(netdissect_options *ndo,
631                          const u_char *ptr)
632 {
633     ND_PRINT((ndo, "\n\t    TC Bandwidth Table"));
634     ND_PRINT((ndo, "\n\t     TC%%   : 0   1   2   3   4   5   6   7"));
635     ND_PRINT((ndo, "\n\t     Value : %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d",
636              ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]));
637 }
638 
639 static void
640 print_tsa_assignment_table(netdissect_options *ndo,
641                            const u_char *ptr)
642 {
643     ND_PRINT((ndo, "\n\t    TSA Assignment Table"));
644     ND_PRINT((ndo, "\n\t     Traffic Class: 0   1   2   3   4   5   6   7"));
645     ND_PRINT((ndo, "\n\t     Value        : %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d",
646              ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]));
647 }
648 
649 /*
650  * Print IEEE 802.1 private extensions. (802.1AB annex E)
651  */
652 static int
653 lldp_private_8021_print(netdissect_options *ndo,
654                         const u_char *tptr, u_int tlv_len)
655 {
656     int subtype, hexdump = FALSE;
657     u_int sublen;
658     u_int tval;
659     u_int i;
660 
661     if (tlv_len < 4) {
662         return hexdump;
663     }
664     subtype = *(tptr+3);
665 
666     ND_PRINT((ndo, "\n\t  %s Subtype (%u)",
667            tok2str(lldp_8021_subtype_values, "unknown", subtype),
668            subtype));
669 
670     switch (subtype) {
671     case LLDP_PRIVATE_8021_SUBTYPE_PORT_VLAN_ID:
672         if (tlv_len < 6) {
673             return hexdump;
674         }
675         ND_PRINT((ndo, "\n\t    port vlan id (PVID): %u",
676                EXTRACT_16BITS(tptr + 4)));
677         break;
678     case LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_VLAN_ID:
679         if (tlv_len < 7) {
680             return hexdump;
681         }
682         ND_PRINT((ndo, "\n\t    port and protocol vlan id (PPVID): %u, flags [%s] (0x%02x)",
683                EXTRACT_16BITS(tptr+5),
684 	       bittok2str(lldp_8021_port_protocol_id_values, "none", *(tptr+4)),
685 	       *(tptr + 4)));
686         break;
687     case LLDP_PRIVATE_8021_SUBTYPE_VLAN_NAME:
688         if (tlv_len < 6) {
689             return hexdump;
690         }
691         ND_PRINT((ndo, "\n\t    vlan id (VID): %u", EXTRACT_16BITS(tptr + 4)));
692         if (tlv_len < 7) {
693             return hexdump;
694         }
695         sublen = *(tptr+6);
696         if (tlv_len < 7+sublen) {
697             return hexdump;
698         }
699         ND_PRINT((ndo, "\n\t    vlan name: "));
700         safeputs(ndo, tptr + 7, sublen);
701         break;
702     case LLDP_PRIVATE_8021_SUBTYPE_PROTOCOL_IDENTITY:
703         if (tlv_len < 5) {
704             return hexdump;
705         }
706         sublen = *(tptr+4);
707         if (tlv_len < 5+sublen) {
708             return hexdump;
709         }
710         ND_PRINT((ndo, "\n\t    protocol identity: "));
711         safeputs(ndo, tptr + 5, sublen);
712         break;
713     case LLDP_PRIVATE_8021_SUBTYPE_CONGESTION_NOTIFICATION:
714         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_CONGESTION_NOTIFICATION_LENGTH){
715         	return hexdump;
716         }
717         tval=*(tptr+4);
718         ND_PRINT((ndo, "\n\t    Pre-Priority CNPV Indicator"));
719         ND_PRINT((ndo, "\n\t     Priority : 0  1  2  3  4  5  6  7"));
720         ND_PRINT((ndo, "\n\t     Value    : "));
721         for(i=0;i<NO_OF_BITS;i++)
722             ND_PRINT((ndo, "%-2d ", (tval >> i) & 0x01));
723         tval=*(tptr+5);
724         ND_PRINT((ndo, "\n\t    Pre-Priority Ready Indicator"));
725         ND_PRINT((ndo, "\n\t     Priority : 0  1  2  3  4  5  6  7"));
726         ND_PRINT((ndo, "\n\t     Value    : "));
727         for(i=0;i<NO_OF_BITS;i++)
728             ND_PRINT((ndo, "%-2d ", (tval >> i) & 0x01));
729         break;
730 
731     case LLDP_PRIVATE_8021_SUBTYPE_ETS_CONFIGURATION:
732         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_ETS_CONFIGURATION_LENGTH) {
733             return hexdump;
734         }
735         tval=*(tptr+4);
736         ND_PRINT((ndo, "\n\t    Willing:%d, CBS:%d, RES:%d, Max TCs:%d",
737         	tval >> 7, (tval >> 6) & 0x02, (tval >> 3) & 0x07, tval & 0x07));
738 
739         /*Print Priority Assignment Table*/
740         print_ets_priority_assignment_table(ndo, tptr + 5);
741 
742         /*Print TC Bandwidth Table*/
743         print_tc_bandwidth_table(ndo, tptr + 9);
744 
745         /* Print TSA Assignment Table */
746         print_tsa_assignment_table(ndo, tptr + 17);
747 
748         break;
749 
750     case LLDP_PRIVATE_8021_SUBTYPE_ETS_RECOMMENDATION:
751         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_ETS_RECOMMENDATION_LENGTH) {
752         	return hexdump;
753         }
754         ND_PRINT((ndo, "\n\t    RES: %d", *(tptr + 4)));
755         /*Print Priority Assignment Table */
756         print_ets_priority_assignment_table(ndo, tptr + 5);
757         /*Print TC Bandwidth Table */
758         print_tc_bandwidth_table(ndo, tptr + 9);
759         /* Print TSA Assignment Table */
760         print_tsa_assignment_table(ndo, tptr + 17);
761         break;
762 
763     case LLDP_PRIVATE_8021_SUBTYPE_PFC_CONFIGURATION:
764         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_PFC_CONFIGURATION_LENGTH) {
765             return hexdump;
766         }
767         tval=*(tptr+4);
768         ND_PRINT((ndo, "\n\t    Willing: %d, MBC: %d, RES: %d, PFC cap:%d ",
769         	tval >> 7, (tval >> 6) & 0x01, (tval >> 4) & 0x03, (tval & 0x0f)));
770         ND_PRINT((ndo, "\n\t    PFC Enable"));
771         tval=*(tptr+5);
772         ND_PRINT((ndo, "\n\t     Priority : 0  1  2  3  4  5  6  7"));
773         ND_PRINT((ndo, "\n\t     Value    : "));
774         for(i=0;i<NO_OF_BITS;i++)
775             ND_PRINT((ndo, "%-2d ", (tval >> i) & 0x01));
776         break;
777 
778     case LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY:
779         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY_MIN_LENGTH) {
780             return hexdump;
781         }
782         ND_PRINT((ndo, "\n\t    RES: %d", *(tptr + 4)));
783         if(tlv_len<=LLDP_PRIVATE_8021_SUBTYPE_APPLICATION_PRIORITY_MIN_LENGTH){
784         	return hexdump;
785         }
786         /*  Length of Application Priority Table */
787         sublen=tlv_len-5;
788         if(sublen%3!=0){
789         	return hexdump;
790         }
791         i=0;
792         ND_PRINT((ndo, "\n\t    Application Priority Table"));
793         while(i<sublen) {
794         	tval=*(tptr+i+5);
795         	ND_PRINT((ndo, "\n\t      Priority: %u, RES: %u, Sel: %u, Protocol ID: %u",
796         		 tval >> 5, (tval >> 3) & 0x03, (tval & 0x07),
797         		 EXTRACT_16BITS(tptr + i + 5)));
798         	i=i+3;
799         }
800         break;
801     case LLDP_PRIVATE_8021_SUBTYPE_EVB:
802         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_EVB_LENGTH){
803         	return hexdump;
804         }
805         ND_PRINT((ndo, "\n\t    EVB Bridge Status"));
806         tval=*(tptr+4);
807         ND_PRINT((ndo, "\n\t      RES: %d, BGID: %d, RRCAP: %d, RRCTR: %d",
808         	tval >> 3, (tval >> 2) & 0x01, (tval >> 1) & 0x01, tval & 0x01));
809         ND_PRINT((ndo, "\n\t    EVB Station Status"));
810         tval=*(tptr+5);
811         ND_PRINT((ndo, "\n\t      RES: %d, SGID: %d, RRREQ: %d,RRSTAT: %d",
812         	tval >> 4, (tval >> 3) & 0x01, (tval >> 2) & 0x01, tval & 0x03));
813         tval=*(tptr+6);
814         ND_PRINT((ndo, "\n\t    R: %d, RTE: %d, ",tval >> 5, tval & 0x1f));
815         tval=*(tptr+7);
816         ND_PRINT((ndo, "EVB Mode: %s [%d]",
817         	tok2str(lldp_evb_mode_values, "unknown", tval >> 6), tval >> 6));
818         ND_PRINT((ndo, "\n\t    ROL: %d, RWD: %d, ", (tval >> 5) & 0x01, tval & 0x1f));
819         tval=*(tptr+8);
820         ND_PRINT((ndo, "RES: %d, ROL: %d, RKA: %d", tval >> 6, (tval >> 5) & 0x01, tval & 0x1f));
821         break;
822 
823     case LLDP_PRIVATE_8021_SUBTYPE_CDCP:
824         if(tlv_len<LLDP_PRIVATE_8021_SUBTYPE_CDCP_MIN_LENGTH){
825         	return hexdump;
826         }
827         tval=*(tptr+4);
828         ND_PRINT((ndo, "\n\t    Role: %d, RES: %d, Scomp: %d ",
829         	tval >> 7, (tval >> 4) & 0x07, (tval >> 3) & 0x01));
830         ND_PRINT((ndo, "ChnCap: %d", EXTRACT_16BITS(tptr + 6) & 0x0fff));
831         sublen=tlv_len-8;
832         if(sublen%3!=0) {
833         	return hexdump;
834         }
835         i=0;
836         while(i<sublen) {
837         	tval=EXTRACT_24BITS(tptr+i+8);
838         	ND_PRINT((ndo, "\n\t    SCID: %d, SVID: %d",
839         		tval >> 12, tval & 0x000fff));
840         	i=i+3;
841         }
842         break;
843 
844     default:
845         hexdump = TRUE;
846         break;
847     }
848 
849     return hexdump;
850 }
851 
852 /*
853  * Print IEEE 802.3 private extensions. (802.3bc)
854  */
855 static int
856 lldp_private_8023_print(netdissect_options *ndo,
857                         const u_char *tptr, u_int tlv_len)
858 {
859     int subtype, hexdump = FALSE;
860 
861     if (tlv_len < 4) {
862         return hexdump;
863     }
864     subtype = *(tptr+3);
865 
866     ND_PRINT((ndo, "\n\t  %s Subtype (%u)",
867            tok2str(lldp_8023_subtype_values, "unknown", subtype),
868            subtype));
869 
870     switch (subtype) {
871     case LLDP_PRIVATE_8023_SUBTYPE_MACPHY:
872         if (tlv_len < 9) {
873             return hexdump;
874         }
875         ND_PRINT((ndo, "\n\t    autonegotiation [%s] (0x%02x)",
876                bittok2str(lldp_8023_autonegotiation_values, "none", *(tptr+4)),
877                *(tptr + 4)));
878         ND_PRINT((ndo, "\n\t    PMD autoneg capability [%s] (0x%04x)",
879                bittok2str(lldp_pmd_capability_values,"unknown", EXTRACT_16BITS(tptr+5)),
880                EXTRACT_16BITS(tptr + 5)));
881         ND_PRINT((ndo, "\n\t    MAU type %s (0x%04x)",
882                tok2str(lldp_mau_types_values, "unknown", EXTRACT_16BITS(tptr+7)),
883                EXTRACT_16BITS(tptr + 7)));
884         break;
885 
886     case LLDP_PRIVATE_8023_SUBTYPE_MDIPOWER:
887         if (tlv_len < 7) {
888             return hexdump;
889         }
890         ND_PRINT((ndo, "\n\t    MDI power support [%s], power pair %s, power class %s",
891                bittok2str(lldp_mdi_values, "none", *(tptr+4)),
892                tok2str(lldp_mdi_power_pairs_values, "unknown", *(tptr+5)),
893                tok2str(lldp_mdi_power_class_values, "unknown", *(tptr + 6))));
894         break;
895 
896     case LLDP_PRIVATE_8023_SUBTYPE_LINKAGGR:
897         if (tlv_len < 9) {
898             return hexdump;
899         }
900         ND_PRINT((ndo, "\n\t    aggregation status [%s], aggregation port ID %u",
901                bittok2str(lldp_aggregation_values, "none", *(tptr+4)),
902                EXTRACT_32BITS(tptr + 5)));
903         break;
904 
905     case LLDP_PRIVATE_8023_SUBTYPE_MTU:
906         if (tlv_len < 6) {
907             return hexdump;
908         }
909         ND_PRINT((ndo, "\n\t    MTU size %u", EXTRACT_16BITS(tptr + 4)));
910         break;
911 
912     default:
913         hexdump = TRUE;
914         break;
915     }
916 
917     return hexdump;
918 }
919 
920 /*
921  * Extract 34bits of latitude/longitude coordinates.
922  */
923 static uint64_t
924 lldp_extract_latlon(const u_char *tptr)
925 {
926     uint64_t latlon;
927 
928     latlon = *tptr & 0x3;
929     latlon = (latlon << 32) | EXTRACT_32BITS(tptr+1);
930 
931     return latlon;
932 }
933 
934 /* objects defined in IANA subtype 00 00 5e
935  * (right now there is only one)
936  */
937 
938 
939 static int
940 lldp_private_iana_print(netdissect_options *ndo,
941                         const u_char *tptr, u_int tlv_len)
942 {
943     int subtype, hexdump = FALSE;
944 
945     if (tlv_len < 8) {
946         return hexdump;
947     }
948     subtype = *(tptr+3);
949 
950     ND_PRINT((ndo, "\n\t  %s Subtype (%u)",
951            tok2str(lldp_iana_subtype_values, "unknown", subtype),
952            subtype));
953 
954     switch (subtype) {
955     case LLDP_IANA_SUBTYPE_MUDURL:
956         ND_PRINT((ndo, "\n\t  MUD-URL="));
957         (void)fn_printn(ndo, tptr+4, tlv_len-4, NULL);
958         break;
959     default:
960         hexdump=TRUE;
961     }
962 
963     return hexdump;
964 }
965 
966 
967 
968 /*
969  * Print private TIA extensions.
970  */
971 static int
972 lldp_private_tia_print(netdissect_options *ndo,
973                        const u_char *tptr, u_int tlv_len)
974 {
975     int subtype, hexdump = FALSE;
976     uint8_t location_format;
977     uint16_t power_val;
978     u_int lci_len;
979     uint8_t ca_type, ca_len;
980 
981     if (tlv_len < 4) {
982         return hexdump;
983     }
984     subtype = *(tptr+3);
985 
986     ND_PRINT((ndo, "\n\t  %s Subtype (%u)",
987            tok2str(lldp_tia_subtype_values, "unknown", subtype),
988            subtype));
989 
990     switch (subtype) {
991     case LLDP_PRIVATE_TIA_SUBTYPE_CAPABILITIES:
992         if (tlv_len < 7) {
993             return hexdump;
994         }
995         ND_PRINT((ndo, "\n\t    Media capabilities [%s] (0x%04x)",
996                bittok2str(lldp_tia_capabilities_values, "none",
997                           EXTRACT_16BITS(tptr + 4)), EXTRACT_16BITS(tptr + 4)));
998         ND_PRINT((ndo, "\n\t    Device type [%s] (0x%02x)",
999                tok2str(lldp_tia_device_type_values, "unknown", *(tptr+6)),
1000                *(tptr + 6)));
1001         break;
1002 
1003     case LLDP_PRIVATE_TIA_SUBTYPE_NETWORK_POLICY:
1004         if (tlv_len < 8) {
1005             return hexdump;
1006         }
1007         ND_PRINT((ndo, "\n\t    Application type [%s] (0x%02x)",
1008                tok2str(lldp_tia_application_type_values, "none", *(tptr+4)),
1009                *(tptr + 4)));
1010         ND_PRINT((ndo, ", Flags [%s]", bittok2str(
1011                    lldp_tia_network_policy_bits_values, "none", *(tptr + 5))));
1012         ND_PRINT((ndo, "\n\t    Vlan id %u",
1013                LLDP_EXTRACT_NETWORK_POLICY_VLAN(EXTRACT_16BITS(tptr + 5))));
1014         ND_PRINT((ndo, ", L2 priority %u",
1015                LLDP_EXTRACT_NETWORK_POLICY_L2_PRIORITY(EXTRACT_16BITS(tptr + 6))));
1016         ND_PRINT((ndo, ", DSCP value %u",
1017                LLDP_EXTRACT_NETWORK_POLICY_DSCP(EXTRACT_16BITS(tptr + 6))));
1018         break;
1019 
1020     case LLDP_PRIVATE_TIA_SUBTYPE_LOCAL_ID:
1021         if (tlv_len < 5) {
1022             return hexdump;
1023         }
1024         location_format = *(tptr+4);
1025         ND_PRINT((ndo, "\n\t    Location data format %s (0x%02x)",
1026                tok2str(lldp_tia_location_data_format_values, "unknown", location_format),
1027                location_format));
1028 
1029         switch (location_format) {
1030         case LLDP_TIA_LOCATION_DATA_FORMAT_COORDINATE_BASED:
1031             if (tlv_len < 21) {
1032                 return hexdump;
1033             }
1034             ND_PRINT((ndo, "\n\t    Latitude resolution %u, latitude value %" PRIu64,
1035                    (*(tptr + 5) >> 2), lldp_extract_latlon(tptr + 5)));
1036             ND_PRINT((ndo, "\n\t    Longitude resolution %u, longitude value %" PRIu64,
1037                    (*(tptr + 10) >> 2), lldp_extract_latlon(tptr + 10)));
1038             ND_PRINT((ndo, "\n\t    Altitude type %s (%u)",
1039                    tok2str(lldp_tia_location_altitude_type_values, "unknown",(*(tptr+15)>>4)),
1040                    (*(tptr + 15) >> 4)));
1041             ND_PRINT((ndo, "\n\t    Altitude resolution %u, altitude value 0x%x",
1042                    (EXTRACT_16BITS(tptr+15)>>6)&0x3f,
1043                    ((EXTRACT_32BITS(tptr + 16) & 0x3fffffff))));
1044             ND_PRINT((ndo, "\n\t    Datum %s (0x%02x)",
1045                    tok2str(lldp_tia_location_datum_type_values, "unknown", *(tptr+20)),
1046                    *(tptr + 20)));
1047             break;
1048 
1049         case LLDP_TIA_LOCATION_DATA_FORMAT_CIVIC_ADDRESS:
1050             if (tlv_len < 6) {
1051                 return hexdump;
1052             }
1053             lci_len = *(tptr+5);
1054             if (lci_len < 3) {
1055                 return hexdump;
1056             }
1057             if (tlv_len < 7+lci_len) {
1058                 return hexdump;
1059             }
1060             ND_PRINT((ndo, "\n\t    LCI length %u, LCI what %s (0x%02x), Country-code ",
1061                    lci_len,
1062                    tok2str(lldp_tia_location_lci_what_values, "unknown", *(tptr+6)),
1063                    *(tptr + 6)));
1064 
1065             /* Country code */
1066             safeputs(ndo, tptr + 7, 2);
1067 
1068             lci_len = lci_len-3;
1069             tptr = tptr + 9;
1070 
1071             /* Decode each civic address element */
1072             while (lci_len > 0) {
1073                 if (lci_len < 2) {
1074                     return hexdump;
1075                 }
1076 		ca_type = *(tptr);
1077                 ca_len = *(tptr+1);
1078 
1079 		tptr += 2;
1080                 lci_len -= 2;
1081 
1082                 ND_PRINT((ndo, "\n\t      CA type \'%s\' (%u), length %u: ",
1083                        tok2str(lldp_tia_location_lci_catype_values, "unknown", ca_type),
1084                        ca_type, ca_len));
1085 
1086 		/* basic sanity check */
1087 		if ( ca_type == 0 || ca_len == 0) {
1088                     return hexdump;
1089 		}
1090 		if (lci_len < ca_len) {
1091 		    return hexdump;
1092 		}
1093 
1094                 safeputs(ndo, tptr, ca_len);
1095                 tptr += ca_len;
1096                 lci_len -= ca_len;
1097             }
1098             break;
1099 
1100         case LLDP_TIA_LOCATION_DATA_FORMAT_ECS_ELIN:
1101             ND_PRINT((ndo, "\n\t    ECS ELIN id "));
1102             safeputs(ndo, tptr + 5, tlv_len - 5);
1103             break;
1104 
1105         default:
1106             ND_PRINT((ndo, "\n\t    Location ID "));
1107             print_unknown_data(ndo, tptr + 5, "\n\t      ", tlv_len - 5);
1108         }
1109         break;
1110 
1111     case LLDP_PRIVATE_TIA_SUBTYPE_EXTENDED_POWER_MDI:
1112         if (tlv_len < 7) {
1113             return hexdump;
1114         }
1115         ND_PRINT((ndo, "\n\t    Power type [%s]",
1116                (*(tptr + 4) & 0xC0 >> 6) ? "PD device" : "PSE device"));
1117         ND_PRINT((ndo, ", Power source [%s]",
1118                tok2str(lldp_tia_power_source_values, "none", (*(tptr + 4) & 0x30) >> 4)));
1119         ND_PRINT((ndo, "\n\t    Power priority [%s] (0x%02x)",
1120                tok2str(lldp_tia_power_priority_values, "none", *(tptr+4)&0x0f),
1121                *(tptr + 4) & 0x0f));
1122         power_val = EXTRACT_16BITS(tptr+5);
1123         if (power_val < LLDP_TIA_POWER_VAL_MAX) {
1124             ND_PRINT((ndo, ", Power %.1f Watts", ((float)power_val) / 10));
1125         } else {
1126             ND_PRINT((ndo, ", Power %u (Reserved)", power_val));
1127         }
1128         break;
1129 
1130     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_HARDWARE_REV:
1131     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_FIRMWARE_REV:
1132     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SOFTWARE_REV:
1133     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_SERIAL_NUMBER:
1134     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MANUFACTURER_NAME:
1135     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_MODEL_NAME:
1136     case LLDP_PRIVATE_TIA_SUBTYPE_INVENTORY_ASSET_ID:
1137         ND_PRINT((ndo, "\n\t  %s ",
1138                tok2str(lldp_tia_inventory_values, "unknown", subtype)));
1139         safeputs(ndo, tptr + 4, tlv_len - 4);
1140         break;
1141 
1142     default:
1143         hexdump = TRUE;
1144         break;
1145     }
1146 
1147     return hexdump;
1148 }
1149 
1150 /*
1151  * Print DCBX Protocol fields (V 1.01).
1152  */
1153 static int
1154 lldp_private_dcbx_print(netdissect_options *ndo,
1155                         const u_char *pptr, u_int len)
1156 {
1157     int subtype, hexdump = FALSE;
1158     uint8_t tval;
1159     uint16_t tlv;
1160     uint32_t i, pgval, uval;
1161     u_int tlen, tlv_type, tlv_len;
1162     const u_char *tptr, *mptr;
1163 
1164     if (len < 4) {
1165         return hexdump;
1166     }
1167     subtype = *(pptr+3);
1168 
1169     ND_PRINT((ndo, "\n\t  %s Subtype (%u)",
1170            tok2str(lldp_dcbx_subtype_values, "unknown", subtype),
1171            subtype));
1172 
1173     /* by passing old version */
1174     if (subtype == LLDP_DCBX_SUBTYPE_1)
1175 	return TRUE;
1176 
1177     tptr = pptr + 4;
1178     tlen = len - 4;
1179 
1180     while (tlen >= sizeof(tlv)) {
1181 
1182         ND_TCHECK2(*tptr, sizeof(tlv));
1183 
1184         tlv = EXTRACT_16BITS(tptr);
1185 
1186         tlv_type = LLDP_EXTRACT_TYPE(tlv);
1187         tlv_len = LLDP_EXTRACT_LEN(tlv);
1188         hexdump = FALSE;
1189 
1190         tlen -= sizeof(tlv);
1191         tptr += sizeof(tlv);
1192 
1193         /* loop check */
1194         if (!tlv_type || !tlv_len) {
1195             break;
1196         }
1197 
1198         ND_TCHECK2(*tptr, tlv_len);
1199         if (tlen < tlv_len) {
1200             goto trunc;
1201         }
1202 
1203 	/* decode every tlv */
1204         switch (tlv_type) {
1205         case LLDP_DCBX_CONTROL_TLV:
1206             if (tlv_len < 10) {
1207                 goto trunc;
1208             }
1209 	    ND_PRINT((ndo, "\n\t    Control - Protocol Control (type 0x%x, length %d)",
1210 		LLDP_DCBX_CONTROL_TLV, tlv_len));
1211 	    ND_PRINT((ndo, "\n\t      Oper_Version: %d", *tptr));
1212 	    ND_PRINT((ndo, "\n\t      Max_Version: %d", *(tptr + 1)));
1213 	    ND_PRINT((ndo, "\n\t      Sequence Number: %d", EXTRACT_32BITS(tptr + 2)));
1214 	    ND_PRINT((ndo, "\n\t      Acknowledgement Number: %d",
1215 					EXTRACT_32BITS(tptr + 6)));
1216 	    break;
1217         case LLDP_DCBX_PRIORITY_GROUPS_TLV:
1218             if (tlv_len < 17) {
1219                 goto trunc;
1220             }
1221 	    ND_PRINT((ndo, "\n\t    Feature - Priority Group (type 0x%x, length %d)",
1222 		LLDP_DCBX_PRIORITY_GROUPS_TLV, tlv_len));
1223 	    ND_PRINT((ndo, "\n\t      Oper_Version: %d", *tptr));
1224 	    ND_PRINT((ndo, "\n\t      Max_Version: %d", *(tptr + 1)));
1225 	    ND_PRINT((ndo, "\n\t      Info block(0x%02X): ", *(tptr + 2)));
1226 	    tval = *(tptr+2);
1227 	    ND_PRINT((ndo, "Enable bit: %d, Willing bit: %d, Error Bit: %d",
1228 		(tval &  0x80) ? 1 : 0, (tval &  0x40) ? 1 : 0,
1229 		(tval &  0x20) ? 1 : 0));
1230 	    ND_PRINT((ndo, "\n\t      SubType: %d", *(tptr + 3)));
1231 	    ND_PRINT((ndo, "\n\t      Priority Allocation"));
1232 
1233 	    /*
1234 	     * Array of 8 4-bit priority group ID values; we fetch all
1235 	     * 32 bits and extract each nibble.
1236 	     */
1237 	    pgval = EXTRACT_32BITS(tptr+4);
1238 	    for (i = 0; i <= 7; i++) {
1239 		ND_PRINT((ndo, "\n\t          PgId_%d: %d",
1240 			i, (pgval >> (28 - 4 * i)) & 0xF));
1241 	    }
1242 	    ND_PRINT((ndo, "\n\t      Priority Group Allocation"));
1243 	    for (i = 0; i <= 7; i++)
1244 		ND_PRINT((ndo, "\n\t          Pg percentage[%d]: %d", i, *(tptr + 8 + i)));
1245 	    ND_PRINT((ndo, "\n\t      NumTCsSupported: %d", *(tptr + 8 + 8)));
1246 	    break;
1247         case LLDP_DCBX_PRIORITY_FLOW_CONTROL_TLV:
1248             if (tlv_len < 6) {
1249                 goto trunc;
1250             }
1251 	    ND_PRINT((ndo, "\n\t    Feature - Priority Flow Control"));
1252 	    ND_PRINT((ndo, " (type 0x%x, length %d)",
1253 		LLDP_DCBX_PRIORITY_FLOW_CONTROL_TLV, tlv_len));
1254 	    ND_PRINT((ndo, "\n\t      Oper_Version: %d", *tptr));
1255 	    ND_PRINT((ndo, "\n\t      Max_Version: %d", *(tptr + 1)));
1256 	    ND_PRINT((ndo, "\n\t      Info block(0x%02X): ", *(tptr + 2)));
1257 	    tval = *(tptr+2);
1258 	    ND_PRINT((ndo, "Enable bit: %d, Willing bit: %d, Error Bit: %d",
1259 		(tval &  0x80) ? 1 : 0, (tval &  0x40) ? 1 : 0,
1260 		(tval &  0x20) ? 1 : 0));
1261 	    ND_PRINT((ndo, "\n\t      SubType: %d", *(tptr + 3)));
1262 	    tval = *(tptr+4);
1263 	    ND_PRINT((ndo, "\n\t      PFC Config (0x%02X)", *(tptr + 4)));
1264 	    for (i = 0; i <= 7; i++)
1265 		ND_PRINT((ndo, "\n\t          Priority Bit %d: %s",
1266 		    i, (tval & (1 << i)) ? "Enabled" : "Disabled"));
1267 	    ND_PRINT((ndo, "\n\t      NumTCPFCSupported: %d", *(tptr + 5)));
1268 	    break;
1269         case LLDP_DCBX_APPLICATION_TLV:
1270             if (tlv_len < 4) {
1271                 goto trunc;
1272             }
1273 	    ND_PRINT((ndo, "\n\t    Feature - Application (type 0x%x, length %d)",
1274 		LLDP_DCBX_APPLICATION_TLV, tlv_len));
1275 	    ND_PRINT((ndo, "\n\t      Oper_Version: %d", *tptr));
1276 	    ND_PRINT((ndo, "\n\t      Max_Version: %d", *(tptr + 1)));
1277 	    ND_PRINT((ndo, "\n\t      Info block(0x%02X): ", *(tptr + 2)));
1278 	    tval = *(tptr+2);
1279 	    ND_PRINT((ndo, "Enable bit: %d, Willing bit: %d, Error Bit: %d",
1280 		(tval &  0x80) ? 1 : 0, (tval &  0x40) ? 1 : 0,
1281 		(tval &  0x20) ? 1 : 0));
1282 	    ND_PRINT((ndo, "\n\t      SubType: %d", *(tptr + 3)));
1283 	    tval = tlv_len - 4;
1284 	    mptr = tptr + 4;
1285 	    while (tval >= 6) {
1286 		ND_PRINT((ndo, "\n\t      Application Value"));
1287 		ND_PRINT((ndo, "\n\t          Application Protocol ID: 0x%04x",
1288 			EXTRACT_16BITS(mptr)));
1289 		uval = EXTRACT_24BITS(mptr+2);
1290 		ND_PRINT((ndo, "\n\t          SF (0x%x) Application Protocol ID is %s",
1291 			(uval >> 22),
1292 			(uval >> 22) ? "Socket Number" : "L2 EtherType"));
1293 		ND_PRINT((ndo, "\n\t          OUI: 0x%06x", uval & 0x3fffff));
1294 		ND_PRINT((ndo, "\n\t          User Priority Map: 0x%02x", *(mptr + 5)));
1295 		tval = tval - 6;
1296 		mptr = mptr + 6;
1297 	    }
1298 	    break;
1299 	default:
1300 	    hexdump = TRUE;
1301 	    break;
1302 	}
1303 
1304         /* do we also want to see a hex dump ? */
1305         if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
1306 	    print_unknown_data(ndo, tptr, "\n\t    ", tlv_len);
1307         }
1308 
1309         tlen -= tlv_len;
1310         tptr += tlv_len;
1311     }
1312 
1313  trunc:
1314     return hexdump;
1315 }
1316 
1317 static char *
1318 lldp_network_addr_print(netdissect_options *ndo, const u_char *tptr, u_int len)
1319 {
1320     uint8_t af;
1321     static char buf[BUFSIZE];
1322     const char * (*pfunc)(netdissect_options *, const u_char *);
1323 
1324     if (len < 1)
1325       return NULL;
1326     len--;
1327     af = *tptr;
1328     switch (af) {
1329     case AFNUM_INET:
1330         if (len < 4)
1331           return NULL;
1332         /* This cannot be assigned to ipaddr_string(), which is a macro. */
1333         pfunc = getname;
1334         break;
1335     case AFNUM_INET6:
1336         if (len < 16)
1337           return NULL;
1338         /* This cannot be assigned to ip6addr_string(), which is a macro. */
1339         pfunc = getname6;
1340         break;
1341     case AFNUM_802:
1342         if (len < 6)
1343           return NULL;
1344         pfunc = etheraddr_string;
1345         break;
1346     default:
1347         pfunc = NULL;
1348         break;
1349     }
1350 
1351     if (!pfunc) {
1352         snprintf(buf, sizeof(buf), "AFI %s (%u), no AF printer !",
1353                  tok2str(af_values, "Unknown", af), af);
1354     } else {
1355         snprintf(buf, sizeof(buf), "AFI %s (%u): %s",
1356                  tok2str(af_values, "Unknown", af), af, (*pfunc)(ndo, tptr+1));
1357     }
1358 
1359     return buf;
1360 }
1361 
1362 static int
1363 lldp_mgmt_addr_tlv_print(netdissect_options *ndo,
1364                          const u_char *pptr, u_int len)
1365 {
1366     uint8_t mgmt_addr_len, intf_num_subtype, oid_len;
1367     const u_char *tptr;
1368     u_int tlen;
1369     char *mgmt_addr;
1370 
1371     tlen = len;
1372     tptr = pptr;
1373 
1374     if (tlen < 1) {
1375         return 0;
1376     }
1377     mgmt_addr_len = *tptr++;
1378     tlen--;
1379 
1380     if (tlen < mgmt_addr_len) {
1381         return 0;
1382     }
1383 
1384     mgmt_addr = lldp_network_addr_print(ndo, tptr, mgmt_addr_len);
1385     if (mgmt_addr == NULL) {
1386         return 0;
1387     }
1388     ND_PRINT((ndo, "\n\t  Management Address length %u, %s",
1389            mgmt_addr_len, mgmt_addr));
1390     tptr += mgmt_addr_len;
1391     tlen -= mgmt_addr_len;
1392 
1393     if (tlen < LLDP_INTF_NUM_LEN) {
1394         return 0;
1395     }
1396 
1397     intf_num_subtype = *tptr;
1398     ND_PRINT((ndo, "\n\t  %s Interface Numbering (%u): %u",
1399            tok2str(lldp_intf_numb_subtype_values, "Unknown", intf_num_subtype),
1400            intf_num_subtype,
1401            EXTRACT_32BITS(tptr + 1)));
1402 
1403     tptr += LLDP_INTF_NUM_LEN;
1404     tlen -= LLDP_INTF_NUM_LEN;
1405 
1406     /*
1407      * The OID is optional.
1408      */
1409     if (tlen) {
1410         oid_len = *tptr;
1411 
1412         if (tlen < 1U + oid_len) {
1413             return 0;
1414         }
1415         if (oid_len) {
1416             ND_PRINT((ndo, "\n\t  OID length %u", oid_len));
1417             safeputs(ndo, tptr + 1, oid_len);
1418         }
1419     }
1420 
1421     return 1;
1422 }
1423 
1424 void
1425 lldp_print(netdissect_options *ndo,
1426            register const u_char *pptr, register u_int len)
1427 {
1428     uint8_t subtype;
1429     uint16_t tlv, cap, ena_cap;
1430     u_int oui, tlen, hexdump, tlv_type, tlv_len;
1431     const u_char *tptr;
1432     char *network_addr;
1433 
1434     tptr = pptr;
1435     tlen = len;
1436 
1437     ND_PRINT((ndo, "LLDP, length %u", len));
1438 
1439     while (tlen >= sizeof(tlv)) {
1440 
1441         ND_TCHECK2(*tptr, sizeof(tlv));
1442 
1443         tlv = EXTRACT_16BITS(tptr);
1444 
1445         tlv_type = LLDP_EXTRACT_TYPE(tlv);
1446         tlv_len = LLDP_EXTRACT_LEN(tlv);
1447         hexdump = FALSE;
1448 
1449         tlen -= sizeof(tlv);
1450         tptr += sizeof(tlv);
1451 
1452         if (ndo->ndo_vflag) {
1453             ND_PRINT((ndo, "\n\t%s TLV (%u), length %u",
1454                    tok2str(lldp_tlv_values, "Unknown", tlv_type),
1455                    tlv_type, tlv_len));
1456         }
1457 
1458         /* infinite loop check */
1459         if (!tlv_type || !tlv_len) {
1460             break;
1461         }
1462 
1463         ND_TCHECK2(*tptr, tlv_len);
1464         if (tlen < tlv_len) {
1465             goto trunc;
1466         }
1467 
1468         switch (tlv_type) {
1469 
1470         case LLDP_CHASSIS_ID_TLV:
1471             if (ndo->ndo_vflag) {
1472                 if (tlv_len < 2) {
1473                     goto trunc;
1474                 }
1475                 subtype = *tptr;
1476                 ND_PRINT((ndo, "\n\t  Subtype %s (%u): ",
1477                        tok2str(lldp_chassis_subtype_values, "Unknown", subtype),
1478                        subtype));
1479 
1480                 switch (subtype) {
1481                 case LLDP_CHASSIS_MAC_ADDR_SUBTYPE:
1482                     if (tlv_len < 1+6) {
1483                         goto trunc;
1484                     }
1485                     ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr + 1)));
1486                     break;
1487 
1488                 case LLDP_CHASSIS_INTF_NAME_SUBTYPE: /* fall through */
1489                 case LLDP_CHASSIS_LOCAL_SUBTYPE:
1490                 case LLDP_CHASSIS_CHASSIS_COMP_SUBTYPE:
1491                 case LLDP_CHASSIS_INTF_ALIAS_SUBTYPE:
1492                 case LLDP_CHASSIS_PORT_COMP_SUBTYPE:
1493                     safeputs(ndo, tptr + 1, tlv_len - 1);
1494                     break;
1495 
1496                 case LLDP_CHASSIS_NETWORK_ADDR_SUBTYPE:
1497                     network_addr = lldp_network_addr_print(ndo, tptr+1, tlv_len-1);
1498                     if (network_addr == NULL) {
1499                         goto trunc;
1500                     }
1501                     ND_PRINT((ndo, "%s", network_addr));
1502                     break;
1503 
1504                 default:
1505                     hexdump = TRUE;
1506                     break;
1507                 }
1508             }
1509             break;
1510 
1511         case LLDP_PORT_ID_TLV:
1512             if (ndo->ndo_vflag) {
1513                 if (tlv_len < 2) {
1514                     goto trunc;
1515                 }
1516                 subtype = *tptr;
1517                 ND_PRINT((ndo, "\n\t  Subtype %s (%u): ",
1518                        tok2str(lldp_port_subtype_values, "Unknown", subtype),
1519                        subtype));
1520 
1521                 switch (subtype) {
1522                 case LLDP_PORT_MAC_ADDR_SUBTYPE:
1523                     if (tlv_len < 1+6) {
1524                         goto trunc;
1525                     }
1526                     ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr + 1)));
1527                     break;
1528 
1529                 case LLDP_PORT_INTF_NAME_SUBTYPE: /* fall through */
1530                 case LLDP_PORT_LOCAL_SUBTYPE:
1531                 case LLDP_PORT_AGENT_CIRC_ID_SUBTYPE:
1532                 case LLDP_PORT_INTF_ALIAS_SUBTYPE:
1533                 case LLDP_PORT_PORT_COMP_SUBTYPE:
1534                     safeputs(ndo, tptr + 1, tlv_len - 1);
1535                     break;
1536 
1537                 case LLDP_PORT_NETWORK_ADDR_SUBTYPE:
1538                     network_addr = lldp_network_addr_print(ndo, tptr+1, tlv_len-1);
1539                     if (network_addr == NULL) {
1540                         goto trunc;
1541                     }
1542                     ND_PRINT((ndo, "%s", network_addr));
1543                     break;
1544 
1545                 default:
1546                     hexdump = TRUE;
1547                     break;
1548                 }
1549             }
1550             break;
1551 
1552         case LLDP_TTL_TLV:
1553             if (ndo->ndo_vflag) {
1554                 if (tlv_len < 2) {
1555                     goto trunc;
1556                 }
1557                 ND_PRINT((ndo, ": TTL %us", EXTRACT_16BITS(tptr)));
1558             }
1559             break;
1560 
1561         case LLDP_PORT_DESCR_TLV:
1562             if (ndo->ndo_vflag) {
1563                 ND_PRINT((ndo, ": "));
1564                 safeputs(ndo, tptr, tlv_len);
1565             }
1566             break;
1567 
1568         case LLDP_SYSTEM_NAME_TLV:
1569             /*
1570              * The system name is also print in non-verbose mode
1571              * similar to the CDP printer.
1572              */
1573             ND_PRINT((ndo, ": "));
1574             safeputs(ndo, tptr, tlv_len);
1575             break;
1576 
1577         case LLDP_SYSTEM_DESCR_TLV:
1578             if (ndo->ndo_vflag) {
1579                 ND_PRINT((ndo, "\n\t  "));
1580                 safeputs(ndo, tptr, tlv_len);
1581             }
1582             break;
1583 
1584         case LLDP_SYSTEM_CAP_TLV:
1585             if (ndo->ndo_vflag) {
1586                 /*
1587                  * XXX - IEEE Std 802.1AB-2009 says the first octet
1588                  * if a chassis ID subtype, with the system
1589                  * capabilities and enabled capabilities following
1590                  * it.
1591                  */
1592                 if (tlv_len < 4) {
1593                     goto trunc;
1594                 }
1595                 cap = EXTRACT_16BITS(tptr);
1596                 ena_cap = EXTRACT_16BITS(tptr+2);
1597                 ND_PRINT((ndo, "\n\t  System  Capabilities [%s] (0x%04x)",
1598                        bittok2str(lldp_cap_values, "none", cap), cap));
1599                 ND_PRINT((ndo, "\n\t  Enabled Capabilities [%s] (0x%04x)",
1600                        bittok2str(lldp_cap_values, "none", ena_cap), ena_cap));
1601             }
1602             break;
1603 
1604         case LLDP_MGMT_ADDR_TLV:
1605             if (ndo->ndo_vflag) {
1606                 if (!lldp_mgmt_addr_tlv_print(ndo, tptr, tlv_len)) {
1607                     goto trunc;
1608                 }
1609             }
1610             break;
1611 
1612         case LLDP_PRIVATE_TLV:
1613             if (ndo->ndo_vflag) {
1614                 if (tlv_len < 3) {
1615                     goto trunc;
1616                 }
1617                 oui = EXTRACT_24BITS(tptr);
1618                 ND_PRINT((ndo, ": OUI %s (0x%06x)", tok2str(oui_values, "Unknown", oui), oui));
1619 
1620                 switch (oui) {
1621                 case OUI_IEEE_8021_PRIVATE:
1622                     hexdump = lldp_private_8021_print(ndo, tptr, tlv_len);
1623                     break;
1624                 case OUI_IEEE_8023_PRIVATE:
1625                     hexdump = lldp_private_8023_print(ndo, tptr, tlv_len);
1626                     break;
1627 		case OUI_IANA:
1628                     hexdump = lldp_private_iana_print(ndo, tptr, tlv_len);
1629                     break;
1630                 case OUI_TIA:
1631                     hexdump = lldp_private_tia_print(ndo, tptr, tlv_len);
1632                     break;
1633                 case OUI_DCBX:
1634                     hexdump = lldp_private_dcbx_print(ndo, tptr, tlv_len);
1635                     break;
1636                 default:
1637                     hexdump = TRUE;
1638                     break;
1639                 }
1640             }
1641             break;
1642 
1643         default:
1644             hexdump = TRUE;
1645             break;
1646         }
1647 
1648         /* do we also want to see a hex dump ? */
1649         if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
1650             print_unknown_data(ndo, tptr, "\n\t  ", tlv_len);
1651         }
1652 
1653         tlen -= tlv_len;
1654         tptr += tlv_len;
1655     }
1656     return;
1657  trunc:
1658     ND_PRINT((ndo, "\n\t[|LLDP]"));
1659 }
1660 
1661 /*
1662  * Local Variables:
1663  * c-style: whitesmith
1664  * c-basic-offset: 4
1665  * End:
1666  */
1667