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() */
18 #define EXTRACT_16BIT(x) ntohs(*((uint16_t *) (x)))
19 #define EXTRACT_32BIT(x) ntohl(*((uint32_t *) (x)))
21 #define LLDP_TLV_TYPE(tlv) (((tlv) & 0xFE00) >> 9)
22 #define LLDP_TLV_LENGTH(tlv) ((tlv) & 0x01FF)
27 #define LLDP_TLV_END 0
28 #define LLDP_TLV_CHASSIS_ID 1
29 #define LLDP_TLV_PORT_ID 2
30 #define LLDP_TLV_TTL 3
31 #define LLDP_TLV_PORT_DESC 4
32 #define LLDP_TLV_SYSTEM_NAME 5
33 #define LLDP_TLV_SYSTEM_DESC 6
34 #define LLDP_TLV_SYSTEM_CAP 7
35 #define LLDP_TLV_MGMT_ADDR 8
36 #define LLDP_TLV_ORG_SPECIFIC 127
41 #define LLDP_CHASSIS_SUBTYPE_CHASSIS 1
42 #define LLDP_CHASSIS_SUBTYPE_IF_ALIAS 2
43 #define LLDP_CHASSIS_SUBTYPE_PORT 3
44 #define LLDP_CHASSIS_SUBTYPE_MAC_ADDR 4
45 #define LLDP_CHASSIS_SUBTYPE_NET_ADDR 5
46 #define LLDP_CHASSIS_SUBTYPE_IF_NAME 6
47 #define LLDP_CHASSIS_SUBTYPE_LOCAL 7
52 #define LLDP_PORT_SUBTYPE_IF_ALIAS 1
53 #define LLDP_PORT_SUBTYPE_PORT_COMP 2
54 #define LLDP_PORT_SUBTYPE_MAC_ADDR 3
55 #define LLDP_PORT_SUBTYPE_NET_ADDR 4
56 #define LLDP_PORT_SUBTYPE_IF_NAME 5
57 #define LLDP_PORT_SUBTYPE_AGENT_CIRC_ID 6
58 #define LLDP_PORT_SUBTYPE_LOCAL 7
61 * System capabilits bit masks
63 #define LLDP_SYSTEM_CAP_OTHER (1 << 0)
64 #define LLDP_SYSTEM_CAP_REPEATER (1 << 1)
65 #define LLDP_SYSTEM_CAP_BRIDGE (1 << 2)
66 #define LLDP_SYSTEM_CAP_WLAN_AP (1 << 3)
67 #define LLDP_SYSTEM_CAP_ROUTER (1 << 4)
68 #define LLDP_SYSTEM_CAP_TELEPHONE (1 << 5)
69 #define LLDP_SYSTEM_CAP_DOCSIS (1 << 6)
70 #define LLDP_SYSTEM_CAP_STATION_ONLY (1 << 7)
73 * Interface number subtypes (for Management addres TLV)
75 #define LLDP_IFACE_NUM_SUBTYPE_UNKNOWN 1
76 #define LLDP_IFACE_NUM_SUBTYPE_IF_INDEX 2
77 #define LLDP_IFACE_NUM_SUBTYPE_SYS_PORT 3
80 * IANA address family numbers (only the ones we actually use)
81 * http://www.iana.org/assignments/address-family-numbers/address-family-numbers.txt
83 * TODO: Move these into own header if there are other users?
85 #define IANA_AF_IPV4 1
86 #define IANA_AF_IPV6 2
89 static int lldp_print_net_addr(const uint8_t *addr
, size_t addrlen
)
103 inet_ntop(AF_INET
, addr
, buf
, sizeof(buf
));
109 inet_ntop(AF_INET6
, addr
, buf
, sizeof(buf
));
115 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
116 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
119 tprintf("unknown address family");
126 static void lldp_print_cap(uint16_t cap
)
128 unsigned int prev
= 0;
130 if (cap
& LLDP_SYSTEM_CAP_OTHER
)
131 tprintf("%sOther", prev
++ ? ", " : "");
132 if (cap
& LLDP_SYSTEM_CAP_REPEATER
)
133 tprintf("%sRepeater", prev
++ ? ", " : "");
134 if (cap
& LLDP_SYSTEM_CAP_BRIDGE
)
135 tprintf("%sBridge", prev
++ ? ", " : "");
136 if (cap
& LLDP_SYSTEM_CAP_WLAN_AP
)
137 tprintf("%sWLAN AP", prev
++ ? ", " : "");
138 if (cap
& LLDP_SYSTEM_CAP_ROUTER
)
139 tprintf("%sRouter", prev
++ ? ", " : "");
140 if (cap
& LLDP_SYSTEM_CAP_TELEPHONE
)
141 tprintf("%sTelephone", prev
++ ? ", " : "");
142 if (cap
& LLDP_SYSTEM_CAP_DOCSIS
)
143 tprintf("%sDOCSIS", prev
++ ? ", " : "");
144 if (cap
& LLDP_SYSTEM_CAP_STATION_ONLY
)
145 tprintf("%sStation only", prev
++ ? ", " : "");
148 static void lldp(struct pkt_buff
*pkt
)
150 unsigned int n_tlv
= 0;
151 uint8_t subtype
, mgmt_alen
, mgmt_oidlen
;
153 unsigned int tlv_type
, tlv_len
;
155 uint8_t *tlv_info_str
;
156 uint16_t sys_cap
, en_cap
;
165 while (len
>= sizeof(tlv_hdr
)) {
166 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
170 tlv_hdr
= EXTRACT_16BIT(data
);
171 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
172 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
174 len
-= sizeof(tlv_hdr
);
176 if (tlv_type
== LLDP_TLV_END
&& tlv_len
== 0) {
177 /* Chassis ID, Port ID and TTL are mandatory */
187 case LLDP_TLV_CHASSIS_ID
:
189 * The mandatory chassis ID shall be the first TLV and
190 * shall appear exactly once.
195 tprintf("Chassis ID");
200 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
201 if (tlv_info_str
== NULL
)
204 subtype
= *tlv_info_str
++;
205 tprintf(" (Subtype %u => ", subtype
);
208 case LLDP_CHASSIS_SUBTYPE_MAC_ADDR
:
212 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
213 tlv_info_str
[0], tlv_info_str
[1],
214 tlv_info_str
[2], tlv_info_str
[3],
215 tlv_info_str
[4], tlv_info_str
[5]);
217 case LLDP_CHASSIS_SUBTYPE_NET_ADDR
:
218 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
221 case LLDP_CHASSIS_SUBTYPE_CHASSIS
:
222 case LLDP_CHASSIS_SUBTYPE_IF_ALIAS
:
223 case LLDP_CHASSIS_SUBTYPE_PORT
:
224 case LLDP_CHASSIS_SUBTYPE_IF_NAME
:
225 case LLDP_CHASSIS_SUBTYPE_LOCAL
:
226 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
235 case LLDP_TLV_PORT_ID
:
237 * The mandatory port ID shall be the second TLV and
238 * shall appear exactly once.
243 tprintf(", Port ID");
248 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
249 if (tlv_info_str
== NULL
)
252 subtype
= *tlv_info_str
++;
253 tprintf(" (Subtype %u => ", subtype
);
256 case LLDP_PORT_SUBTYPE_MAC_ADDR
:
260 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
261 tlv_info_str
[0], tlv_info_str
[1],
262 tlv_info_str
[2], tlv_info_str
[3],
263 tlv_info_str
[4], tlv_info_str
[5]);
265 case LLDP_PORT_SUBTYPE_NET_ADDR
:
266 if (lldp_print_net_addr(tlv_info_str
, tlv_len
))
269 case LLDP_PORT_SUBTYPE_IF_ALIAS
:
270 case LLDP_PORT_SUBTYPE_PORT_COMP
:
271 case LLDP_PORT_SUBTYPE_IF_NAME
:
272 case LLDP_PORT_SUBTYPE_AGENT_CIRC_ID
:
273 case LLDP_PORT_SUBTYPE_LOCAL
:
274 tputs_safe((const char *) tlv_info_str
, tlv_len
- 1);
285 * The mandatory TTL shall be the third TLV and
286 * shall appear exactly once.
296 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
297 if (tlv_info_str
== NULL
)
300 tprintf(" (%u)", EXTRACT_16BIT(tlv_info_str
));
302 case LLDP_TLV_PORT_DESC
:
303 tprintf(", Port desc (");
305 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
306 if (tlv_info_str
== NULL
)
309 tputs_safe((const char *) tlv_info_str
, tlv_len
);
313 case LLDP_TLV_SYSTEM_NAME
:
314 tprintf(", Sys name (");
316 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
317 if (tlv_info_str
== NULL
)
320 tputs_safe((const char *) tlv_info_str
, tlv_len
);
324 case LLDP_TLV_SYSTEM_DESC
:
325 tprintf(", Sys desc (");
327 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
328 if (tlv_info_str
== NULL
)
331 tputs_safe((const char *) tlv_info_str
, tlv_len
);
335 case LLDP_TLV_SYSTEM_CAP
:
336 tprintf(", Sys Cap");
341 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
342 if (tlv_info_str
== NULL
)
345 sys_cap
= EXTRACT_16BIT(tlv_info_str
);
346 tlv_info_str
+= sizeof(uint32_t);
347 en_cap
= EXTRACT_16BIT(tlv_info_str
);
350 lldp_print_cap(sys_cap
);
352 tprintf(" Ena Cap (");
353 lldp_print_cap(en_cap
);
356 case LLDP_TLV_MGMT_ADDR
:
357 tprintf(", Mgmt Addr (");
359 if (tlv_len
< 9 || tlv_len
> 167)
362 tlv_info_str
= pkt_pull(pkt
, tlv_len
);
363 if (tlv_info_str
== NULL
)
366 mgmt_alen
= *tlv_info_str
;
368 if (tlv_len
- 1 < mgmt_alen
)
371 if (lldp_print_net_addr(tlv_info_str
, mgmt_alen
))
373 tlv_info_str
+= mgmt_alen
;
375 tprintf(", Iface Subtype %d/", *tlv_info_str
);
376 switch (*tlv_info_str
) {
377 case LLDP_IFACE_NUM_SUBTYPE_IF_INDEX
:
380 case LLDP_IFACE_NUM_SUBTYPE_SYS_PORT
:
381 tprintf("System Port Number");
389 tprintf(", Iface Number %u", EXTRACT_32BIT(tlv_info_str
));
392 mgmt_oidlen
= *tlv_info_str
;
393 if (tlv_len
- mgmt_alen
- sizeof(uint32_t) - 3 < mgmt_oidlen
)
395 if (mgmt_oidlen
> 0) {
397 tputs_safe((const char *) tlv_info_str
+ 1, mgmt_oidlen
);
402 case LLDP_TLV_ORG_SPECIFIC
:
403 tprintf(", Org specific");
408 tlv_info_str
= pkt_pull(pkt
, 4);
409 if (tlv_info_str
== NULL
)
412 oui
= ntohl(*((uint32_t *) tlv_info_str
));
413 subtype
= oui
& 0xff;
415 tprintf(" (OUI %s, Subtype %u)", lookup_vendor_str(oui
),
418 /* Just eat it up, we don't know how to interpret it */
419 pkt_pull(pkt
, tlv_len
- 4);
422 tprintf(", Unknown TLV %u", tlv_type
);
423 pkt_pull(pkt
, tlv_len
);
436 tprintf(" %sINVALID%s ]\n", colorize_start_full(black
, red
),
440 static void lldp_less(struct pkt_buff
*pkt
)
442 unsigned int len
, n_tlv
= 0;
443 unsigned int tlv_type
, tlv_len
;
448 while (len
>= sizeof(tlv_hdr
)) {
449 uint8_t *data
= pkt_pull(pkt
, sizeof(tlv_hdr
));
453 tlv_hdr
= EXTRACT_16BIT(data
);
454 tlv_type
= LLDP_TLV_TYPE(tlv_hdr
);
455 tlv_len
= LLDP_TLV_LENGTH(tlv_hdr
);
458 len
-= sizeof(tlv_hdr
);
460 if (tlv_type
== LLDP_TLV_END
|| tlv_len
== 0)
465 pkt_pull(pkt
, tlv_len
);
470 tprintf(" %u TLV%s", n_tlv
, n_tlv
== 1 ? "" : "s");
473 struct protocol lldp_ops
= {
476 .print_less
= lldp_less
,