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