2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012, 2013 Tobias Klauser <tklauser@distanz.ch>
4 * Subject to the GPL, version 2.
8 #include <arpa/inet.h> /* for inet_ntop() */
9 #include <netinet/in.h> /* for ntohs()/ntohl() */
16 #define EXTRACT_16BIT(x) ntohs(*((uint16_t *) (x)))
17 #define EXTRACT_32BIT(x) ntohl(*((uint32_t *) (x)))
19 #define LLDP_TLV_TYPE(tlv) (((tlv) & 0xFE00) >> 9)
20 #define LLDP_TLV_LENGTH(tlv) ((tlv) & 0x01FF)
25 #define LLDP_TLV_END 0
26 #define LLDP_TLV_CHASSIS_ID 1
27 #define LLDP_TLV_PORT_ID 2
28 #define LLDP_TLV_TTL 3
29 #define LLDP_TLV_PORT_DESC 4
30 #define LLDP_TLV_SYSTEM_NAME 5
31 #define LLDP_TLV_SYSTEM_DESC 6
32 #define LLDP_TLV_SYSTEM_CAP 7
33 #define LLDP_TLV_MGMT_ADDR 8
34 #define LLDP_TLV_ORG_SPECIFIC 127
39 #define LLDP_CHASSIS_SUBTYPE_CHASSIS 1
40 #define LLDP_CHASSIS_SUBTYPE_IF_ALIAS 2
41 #define LLDP_CHASSIS_SUBTYPE_PORT 3
42 #define LLDP_CHASSIS_SUBTYPE_MAC_ADDR 4
43 #define LLDP_CHASSIS_SUBTYPE_NET_ADDR 5
44 #define LLDP_CHASSIS_SUBTYPE_IF_NAME 6
45 #define LLDP_CHASSIS_SUBTYPE_LOCAL 7
50 #define LLDP_PORT_SUBTYPE_IF_ALIAS 1
51 #define LLDP_PORT_SUBTYPE_PORT_COMP 2
52 #define LLDP_PORT_SUBTYPE_MAC_ADDR 3
53 #define LLDP_PORT_SUBTYPE_NET_ADDR 4
54 #define LLDP_PORT_SUBTYPE_IF_NAME 5
55 #define LLDP_PORT_SUBTYPE_AGENT_CIRC_ID 6
56 #define LLDP_PORT_SUBTYPE_LOCAL 7
59 * System capabilits bit masks
61 #define LLDP_SYSTEM_CAP_OTHER (1 << 0)
62 #define LLDP_SYSTEM_CAP_REPEATER (1 << 1)
63 #define LLDP_SYSTEM_CAP_BRIDGE (1 << 2)
64 #define LLDP_SYSTEM_CAP_WLAN_AP (1 << 3)
65 #define LLDP_SYSTEM_CAP_ROUTER (1 << 4)
66 #define LLDP_SYSTEM_CAP_TELEPHONE (1 << 5)
67 #define LLDP_SYSTEM_CAP_DOCSIS (1 << 6)
68 #define LLDP_SYSTEM_CAP_STATION_ONLY (1 << 7)
71 * Interface number subtypes (for Management addres TLV)
73 #define LLDP_IFACE_NUM_SUBTYPE_UNKNOWN 1
74 #define LLDP_IFACE_NUM_SUBTYPE_IF_INDEX 2
75 #define LLDP_IFACE_NUM_SUBTYPE_SYS_PORT 3
78 * IANA address family numbers (only the ones we actually use)
79 * http://www.iana.org/assignments/address-family-numbers/address-family-numbers.txt
81 * TODO: Move these into own header if there are other users?
83 #define IANA_AF_IPV4 1
84 #define IANA_AF_IPV6 2
87 static int lldp_print_net_addr(const uint8_t *addr
, size_t addrlen
)
101 inet_ntop(AF_INET
, addr
, buf
, sizeof(buf
));
107 inet_ntop(AF_INET6
, addr
, buf
, sizeof(buf
));
113 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
114 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
117 tprintf("unknown address family");
124 static inline void lldp_print_cap_one(const char *cap
, unsigned int *prev
)
126 tprintf("%s%s", *prev
? ", " : "", cap
);
130 static void lldp_print_cap(uint16_t cap
)
132 unsigned int prev
= 0;
134 if (cap
& LLDP_SYSTEM_CAP_OTHER
)
135 lldp_print_cap_one("Other", &prev
);
136 if (cap
& LLDP_SYSTEM_CAP_REPEATER
)
137 lldp_print_cap_one("Repeater", &prev
);
138 if (cap
& LLDP_SYSTEM_CAP_BRIDGE
)
139 lldp_print_cap_one("Bridge", &prev
);
140 if (cap
& LLDP_SYSTEM_CAP_WLAN_AP
)
141 lldp_print_cap_one("WLAN AP", &prev
);
142 if (cap
& LLDP_SYSTEM_CAP_ROUTER
)
143 lldp_print_cap_one("Router", &prev
);
144 if (cap
& LLDP_SYSTEM_CAP_TELEPHONE
)
145 lldp_print_cap_one("Telephone", &prev
);
146 if (cap
& LLDP_SYSTEM_CAP_DOCSIS
)
147 lldp_print_cap_one("DOCSIS", &prev
);
148 if (cap
& LLDP_SYSTEM_CAP_STATION_ONLY
)
149 lldp_print_cap_one("Station only", &prev
);
152 static void lldp(struct pkt_buff
*pkt
)
154 unsigned int n_tlv
= 0;
155 uint8_t subtype
, mgmt_alen
, mgmt_oidlen
;
157 unsigned int tlv_type
, tlv_len
;
159 uint8_t *tlv_info_str
;
160 uint16_t sys_cap
, en_cap
;
169 while (len
>= sizeof(tlv_hdr
)) {
170 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
174 tlv_hdr
= EXTRACT_16BIT(data
);
175 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
176 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
178 len
-= sizeof(tlv_hdr
);
180 if (tlv_type
== LLDP_TLV_END
&& tlv_len
== 0) {
181 /* Chassis ID, Port ID and TTL are mandatory */
191 case LLDP_TLV_CHASSIS_ID
:
193 * The mandatory chassis ID shall be the first TLV and
194 * shall appear exactly once.
199 tprintf("Chassis ID");
204 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
205 if (tlv_info_str
== NULL
)
208 subtype
= *tlv_info_str
++;
209 tprintf(" (Subtype %u => ", subtype
);
212 case LLDP_CHASSIS_SUBTYPE_MAC_ADDR
:
216 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
217 tlv_info_str
[0], tlv_info_str
[1],
218 tlv_info_str
[2], tlv_info_str
[3],
219 tlv_info_str
[4], tlv_info_str
[5]);
221 case LLDP_CHASSIS_SUBTYPE_NET_ADDR
:
222 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
225 case LLDP_CHASSIS_SUBTYPE_CHASSIS
:
226 case LLDP_CHASSIS_SUBTYPE_IF_ALIAS
:
227 case LLDP_CHASSIS_SUBTYPE_PORT
:
228 case LLDP_CHASSIS_SUBTYPE_IF_NAME
:
229 case LLDP_CHASSIS_SUBTYPE_LOCAL
:
230 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
239 case LLDP_TLV_PORT_ID
:
241 * The mandatory port ID shall be the second TLV and
242 * shall appear exactly once.
247 tprintf(", Port ID");
252 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
253 if (tlv_info_str
== NULL
)
256 subtype
= *tlv_info_str
++;
257 tprintf(" (Subtype %u => ", subtype
);
260 case LLDP_PORT_SUBTYPE_MAC_ADDR
:
264 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
265 tlv_info_str
[0], tlv_info_str
[1],
266 tlv_info_str
[2], tlv_info_str
[3],
267 tlv_info_str
[4], tlv_info_str
[5]);
269 case LLDP_PORT_SUBTYPE_NET_ADDR
:
270 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
273 case LLDP_PORT_SUBTYPE_IF_ALIAS
:
274 case LLDP_PORT_SUBTYPE_PORT_COMP
:
275 case LLDP_PORT_SUBTYPE_IF_NAME
:
276 case LLDP_PORT_SUBTYPE_AGENT_CIRC_ID
:
277 case LLDP_PORT_SUBTYPE_LOCAL
:
278 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
289 * The mandatory TTL shall be the third TLV and
290 * shall appear exactly once.
300 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
301 if (tlv_info_str
== NULL
)
304 tprintf(" (%u)", EXTRACT_16BIT(tlv_info_str
));
306 case LLDP_TLV_PORT_DESC
:
307 tprintf(", Port desc (");
309 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
310 if (tlv_info_str
== NULL
)
313 tputs_safe((const char *) tlv_info_str
, tlv_len
);
317 case LLDP_TLV_SYSTEM_NAME
:
318 tprintf(", Sys name (");
320 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
321 if (tlv_info_str
== NULL
)
324 tputs_safe((const char *) tlv_info_str
, tlv_len
);
328 case LLDP_TLV_SYSTEM_DESC
:
329 tprintf(", Sys desc (");
331 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
332 if (tlv_info_str
== NULL
)
335 tputs_safe((const char *) tlv_info_str
, tlv_len
);
339 case LLDP_TLV_SYSTEM_CAP
:
340 tprintf(", Sys Cap");
345 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
346 if (tlv_info_str
== NULL
)
349 sys_cap
= EXTRACT_16BIT(tlv_info_str
);
350 tlv_info_str
+= sizeof(uint32_t);
351 en_cap
= EXTRACT_16BIT(tlv_info_str
);
354 lldp_print_cap(sys_cap
);
356 tprintf(" Ena Cap (");
357 lldp_print_cap(en_cap
);
360 case LLDP_TLV_MGMT_ADDR
:
361 tprintf(", Mgmt Addr (");
363 if (tlv_len
< 9 || tlv_len
> 167)
366 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
367 if (tlv_info_str
== NULL
)
370 mgmt_alen
= *tlv_info_str
;
372 if (tlv_len
- 1 < mgmt_alen
)
375 if (lldp_print_net_addr(tlv_info_str
, mgmt_alen
))
377 tlv_info_str
+= mgmt_alen
;
379 tprintf(", Iface Subtype %d/", *tlv_info_str
);
380 switch (*tlv_info_str
) {
381 case LLDP_IFACE_NUM_SUBTYPE_IF_INDEX
:
384 case LLDP_IFACE_NUM_SUBTYPE_SYS_PORT
:
385 tprintf("System Port Number");
393 tprintf(", Iface Number %u", EXTRACT_32BIT(tlv_info_str
));
396 mgmt_oidlen
= *tlv_info_str
;
397 if (tlv_len
- mgmt_alen
- sizeof(uint32_t) - 3 < mgmt_oidlen
)
399 if (mgmt_oidlen
> 0) {
401 tputs_safe((const char *) tlv_info_str
+ 1, mgmt_oidlen
);
406 case LLDP_TLV_ORG_SPECIFIC
:
407 tprintf(", Org specific");
412 tlv_info_str
= pkt_pull(pkt
, 4);
413 if (tlv_info_str
== NULL
)
416 oui
= ntohl(*((uint32_t *) tlv_info_str
));
417 subtype
= oui
& 0xff;
419 tprintf(" (OUI %s, Subtype %u)", lookup_vendor_str(oui
),
422 /* Just eat it up, we don't know how to interpret it */
423 pkt_pull(pkt
, tlv_len
- 4);
426 tprintf(", Unknown TLV %u", tlv_type
);
427 pkt_pull(pkt
, tlv_len
);
440 tprintf(" %sINVALID%s ]\n", colorize_start_full(black
, red
),
444 static void lldp_less(struct pkt_buff
*pkt
)
446 unsigned int len
, n_tlv
= 0;
447 unsigned int tlv_type
, tlv_len
;
452 while (len
>= sizeof(tlv_hdr
)) {
453 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
457 tlv_hdr
= EXTRACT_16BIT(data
);
458 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
459 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
462 len
-= sizeof(tlv_hdr
);
464 if (tlv_type
== LLDP_TLV_END
|| tlv_len
== 0)
469 pkt_pull(pkt
, tlv_len
);
474 tprintf(" %u TLV%s", n_tlv
, n_tlv
== 1 ? "" : "s");
477 struct protocol lldp_ops
= {
480 .print_less
= lldp_less
,