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() */
17 #define EXTRACT_16BIT(x) ntohs(*((uint16_t *) (x)))
18 #define EXTRACT_32BIT(x) ntohl(*((uint32_t *) (x)))
20 #define LLDP_TLV_TYPE(tlv) (((tlv) & 0xFE00) >> 9)
21 #define LLDP_TLV_LENGTH(tlv) ((tlv) & 0x01FF)
26 #define LLDP_TLV_END 0
27 #define LLDP_TLV_CHASSIS_ID 1
28 #define LLDP_TLV_PORT_ID 2
29 #define LLDP_TLV_TTL 3
30 #define LLDP_TLV_PORT_DESC 4
31 #define LLDP_TLV_SYSTEM_NAME 5
32 #define LLDP_TLV_SYSTEM_DESC 6
33 #define LLDP_TLV_SYSTEM_CAP 7
34 #define LLDP_TLV_MGMT_ADDR 8
35 #define LLDP_TLV_ORG_SPECIFIC 127
40 #define LLDP_CHASSIS_SUBTYPE_CHASSIS 1
41 #define LLDP_CHASSIS_SUBTYPE_IF_ALIAS 2
42 #define LLDP_CHASSIS_SUBTYPE_PORT 3
43 #define LLDP_CHASSIS_SUBTYPE_MAC_ADDR 4
44 #define LLDP_CHASSIS_SUBTYPE_NET_ADDR 5
45 #define LLDP_CHASSIS_SUBTYPE_IF_NAME 6
46 #define LLDP_CHASSIS_SUBTYPE_LOCAL 7
51 #define LLDP_PORT_SUBTYPE_IF_ALIAS 1
52 #define LLDP_PORT_SUBTYPE_PORT_COMP 2
53 #define LLDP_PORT_SUBTYPE_MAC_ADDR 3
54 #define LLDP_PORT_SUBTYPE_NET_ADDR 4
55 #define LLDP_PORT_SUBTYPE_IF_NAME 5
56 #define LLDP_PORT_SUBTYPE_AGENT_CIRC_ID 6
57 #define LLDP_PORT_SUBTYPE_LOCAL 7
60 * System capabilits bit masks
62 #define LLDP_SYSTEM_CAP_OTHER (1 << 0)
63 #define LLDP_SYSTEM_CAP_REPEATER (1 << 1)
64 #define LLDP_SYSTEM_CAP_BRIDGE (1 << 2)
65 #define LLDP_SYSTEM_CAP_WLAN_AP (1 << 3)
66 #define LLDP_SYSTEM_CAP_ROUTER (1 << 4)
67 #define LLDP_SYSTEM_CAP_TELEPHONE (1 << 5)
68 #define LLDP_SYSTEM_CAP_DOCSIS (1 << 6)
69 #define LLDP_SYSTEM_CAP_STATION_ONLY (1 << 7)
72 * Interface number subtypes (for Management addres TLV)
74 #define LLDP_IFACE_NUM_SUBTYPE_UNKNOWN 1
75 #define LLDP_IFACE_NUM_SUBTYPE_IF_INDEX 2
76 #define LLDP_IFACE_NUM_SUBTYPE_SYS_PORT 3
79 * IANA address family numbers (only the ones we actually use)
80 * http://www.iana.org/assignments/address-family-numbers/address-family-numbers.txt
82 * TODO: Move these into own header if there are other users?
84 #define IANA_AF_IPV4 1
85 #define IANA_AF_IPV6 2
88 static int lldp_print_net_addr(const uint8_t *addr
, size_t addrlen
)
102 inet_ntop(AF_INET
, addr
, buf
, sizeof(buf
));
108 inet_ntop(AF_INET6
, addr
, buf
, sizeof(buf
));
114 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
115 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
118 tprintf("unknown address family");
125 static inline void lldp_print_cap_one(const char *cap
, unsigned int *prev
)
127 tprintf("%s%s", *prev
? ", " : "", cap
);
131 static void lldp_print_cap(uint16_t cap
)
133 unsigned int prev
= 0;
135 if (cap
& LLDP_SYSTEM_CAP_OTHER
)
136 lldp_print_cap_one("Other", &prev
);
137 if (cap
& LLDP_SYSTEM_CAP_REPEATER
)
138 lldp_print_cap_one("Repeater", &prev
);
139 if (cap
& LLDP_SYSTEM_CAP_BRIDGE
)
140 lldp_print_cap_one("Bridge", &prev
);
141 if (cap
& LLDP_SYSTEM_CAP_WLAN_AP
)
142 lldp_print_cap_one("WLAN AP", &prev
);
143 if (cap
& LLDP_SYSTEM_CAP_ROUTER
)
144 lldp_print_cap_one("Router", &prev
);
145 if (cap
& LLDP_SYSTEM_CAP_TELEPHONE
)
146 lldp_print_cap_one("Telephone", &prev
);
147 if (cap
& LLDP_SYSTEM_CAP_DOCSIS
)
148 lldp_print_cap_one("DOCSIS", &prev
);
149 if (cap
& LLDP_SYSTEM_CAP_STATION_ONLY
)
150 lldp_print_cap_one("Station only", &prev
);
153 static void lldp(struct pkt_buff
*pkt
)
155 unsigned int n_tlv
= 0;
156 uint8_t subtype
, mgmt_alen
, mgmt_oidlen
;
158 unsigned int tlv_type
, tlv_len
;
160 uint8_t *tlv_info_str
;
161 uint16_t sys_cap
, en_cap
;
170 while (len
>= sizeof(tlv_hdr
)) {
171 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
175 tlv_hdr
= EXTRACT_16BIT(data
);
176 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
177 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
179 len
-= sizeof(tlv_hdr
);
181 if (tlv_type
== LLDP_TLV_END
&& tlv_len
== 0) {
182 /* Chassis ID, Port ID and TTL are mandatory */
192 case LLDP_TLV_CHASSIS_ID
:
194 * The mandatory chassis ID shall be the first TLV and
195 * shall appear exactly once.
200 tprintf("Chassis ID");
205 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
206 if (tlv_info_str
== NULL
)
209 subtype
= *tlv_info_str
++;
210 tprintf(" (Subtype %u => ", subtype
);
213 case LLDP_CHASSIS_SUBTYPE_MAC_ADDR
:
217 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
218 tlv_info_str
[0], tlv_info_str
[1],
219 tlv_info_str
[2], tlv_info_str
[3],
220 tlv_info_str
[4], tlv_info_str
[5]);
222 case LLDP_CHASSIS_SUBTYPE_NET_ADDR
:
223 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
226 case LLDP_CHASSIS_SUBTYPE_CHASSIS
:
227 case LLDP_CHASSIS_SUBTYPE_IF_ALIAS
:
228 case LLDP_CHASSIS_SUBTYPE_PORT
:
229 case LLDP_CHASSIS_SUBTYPE_IF_NAME
:
230 case LLDP_CHASSIS_SUBTYPE_LOCAL
:
231 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
240 case LLDP_TLV_PORT_ID
:
242 * The mandatory port ID shall be the second TLV and
243 * shall appear exactly once.
248 tprintf(", Port ID");
253 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
254 if (tlv_info_str
== NULL
)
257 subtype
= *tlv_info_str
++;
258 tprintf(" (Subtype %u => ", subtype
);
261 case LLDP_PORT_SUBTYPE_MAC_ADDR
:
265 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
266 tlv_info_str
[0], tlv_info_str
[1],
267 tlv_info_str
[2], tlv_info_str
[3],
268 tlv_info_str
[4], tlv_info_str
[5]);
270 case LLDP_PORT_SUBTYPE_NET_ADDR
:
271 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
274 case LLDP_PORT_SUBTYPE_IF_ALIAS
:
275 case LLDP_PORT_SUBTYPE_PORT_COMP
:
276 case LLDP_PORT_SUBTYPE_IF_NAME
:
277 case LLDP_PORT_SUBTYPE_AGENT_CIRC_ID
:
278 case LLDP_PORT_SUBTYPE_LOCAL
:
279 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
290 * The mandatory TTL shall be the third TLV and
291 * shall appear exactly once.
301 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
302 if (tlv_info_str
== NULL
)
305 tprintf(" (%u)", EXTRACT_16BIT(tlv_info_str
));
307 case LLDP_TLV_PORT_DESC
:
308 tprintf(", Port desc (");
310 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
311 if (tlv_info_str
== NULL
)
314 tputs_safe((const char *) tlv_info_str
, tlv_len
);
318 case LLDP_TLV_SYSTEM_NAME
:
319 tprintf(", Sys name (");
321 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
322 if (tlv_info_str
== NULL
)
325 tputs_safe((const char *) tlv_info_str
, tlv_len
);
329 case LLDP_TLV_SYSTEM_DESC
:
330 tprintf(", Sys desc (");
332 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
333 if (tlv_info_str
== NULL
)
336 tputs_safe((const char *) tlv_info_str
, tlv_len
);
340 case LLDP_TLV_SYSTEM_CAP
:
341 tprintf(", Sys Cap");
346 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
347 if (tlv_info_str
== NULL
)
350 sys_cap
= EXTRACT_16BIT(tlv_info_str
);
351 tlv_info_str
+= sizeof(uint32_t);
352 en_cap
= EXTRACT_16BIT(tlv_info_str
);
355 lldp_print_cap(sys_cap
);
357 tprintf(" Ena Cap (");
358 lldp_print_cap(en_cap
);
361 case LLDP_TLV_MGMT_ADDR
:
362 tprintf(", Mgmt Addr (");
364 if (tlv_len
< 9 || tlv_len
> 167)
367 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
368 if (tlv_info_str
== NULL
)
371 mgmt_alen
= *tlv_info_str
;
373 if (tlv_len
- 1 < mgmt_alen
)
376 if (lldp_print_net_addr(tlv_info_str
, mgmt_alen
))
378 tlv_info_str
+= mgmt_alen
;
380 tprintf(", Iface Subtype %d/", *tlv_info_str
);
381 switch (*tlv_info_str
) {
382 case LLDP_IFACE_NUM_SUBTYPE_IF_INDEX
:
385 case LLDP_IFACE_NUM_SUBTYPE_SYS_PORT
:
386 tprintf("System Port Number");
394 tprintf(", Iface Number %u", EXTRACT_32BIT(tlv_info_str
));
397 mgmt_oidlen
= *tlv_info_str
;
398 if (tlv_len
- mgmt_alen
- sizeof(uint32_t) - 3 < mgmt_oidlen
)
400 if (mgmt_oidlen
> 0) {
402 tputs_safe((const char *) tlv_info_str
+ 1, mgmt_oidlen
);
407 case LLDP_TLV_ORG_SPECIFIC
:
408 tprintf(", Org specific");
413 tlv_info_str
= pkt_pull(pkt
, 4);
414 if (tlv_info_str
== NULL
)
417 oui
= ntohl(*((uint32_t *) tlv_info_str
));
418 subtype
= oui
& 0xff;
420 tprintf(" (OUI %s, Subtype %u)", lookup_vendor_str(oui
),
423 /* Just eat it up, we don't know how to interpret it */
424 pkt_pull(pkt
, tlv_len
- 4);
427 tprintf(", Unknown TLV %u", tlv_type
);
428 pkt_pull(pkt
, tlv_len
);
441 tprintf(" %sINVALID%s ]\n", colorize_start_full(black
, red
),
445 static void lldp_less(struct pkt_buff
*pkt
)
447 unsigned int len
, n_tlv
= 0;
448 unsigned int tlv_type
, tlv_len
;
453 while (len
>= sizeof(tlv_hdr
)) {
454 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
458 tlv_hdr
= EXTRACT_16BIT(data
);
459 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
460 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
463 len
-= sizeof(tlv_hdr
);
465 if (tlv_type
== LLDP_TLV_END
|| tlv_len
== 0)
470 pkt_pull(pkt
, tlv_len
);
475 tprintf(" %u TLV%s", n_tlv
, n_tlv
== 1 ? "" : "s");
478 struct protocol lldp_ops
= {
481 .print_less
= lldp_less
,