netsniff-ng: mlock: only lock current and future pages when root
[netsniff-ng.git] / proto_lldp.c
blob22b7684800dcc5ba5b84bd9c9c031c8c3aa184ac
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012, 2013 Tobias Klauser <tklauser@distanz.ch>
4 * Subject to the GPL, version 2.
5 */
7 #include <stdint.h>
8 #include <arpa/inet.h> /* for inet_ntop() */
9 #include <netinet/in.h> /* for ntohs()/ntohl() */
11 #include "built_in.h"
12 #include "oui.h"
13 #include "pkt_buff.h"
14 #include "proto.h"
15 #include "protos.h"
16 #include "xutils.h"
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)
25 * LLDP TLV types
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
39 * Chassis ID subtypes
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
50 * Port ID subtypes
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
87 #define IANA_AF_802 6
89 static int lldp_print_net_addr(const uint8_t *addr, size_t addrlen)
91 uint8_t af;
92 char buf[64];
94 if (addrlen < 1)
95 return -EINVAL;
97 af = *addr++;
98 addrlen--;
99 switch (af) {
100 case IANA_AF_IPV4:
101 if (addrlen < 4)
102 return -EINVAL;
103 inet_ntop(AF_INET, addr, buf, sizeof(buf));
104 tprintf("%s", buf);
105 break;
106 case IANA_AF_IPV6:
107 if (addrlen < 16)
108 return -EINVAL;
109 inet_ntop(AF_INET6, addr, buf, sizeof(buf));
110 tprintf("%s", buf);
111 break;
112 case IANA_AF_802:
113 if (addrlen < 6)
114 return -EINVAL;
115 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
116 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
117 break;
118 default:
119 tprintf("unknown address family");
120 break;
123 return 0;
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;
152 uint16_t tlv_hdr;
153 unsigned int tlv_type, tlv_len;
154 unsigned int len;
155 uint8_t *tlv_info_str;
156 uint16_t sys_cap, en_cap;
157 uint32_t oui;
159 len = pkt_len(pkt);
160 if (len == 0)
161 return;
163 tprintf(" [ LLDP ");
165 while (len >= sizeof(tlv_hdr)) {
166 tlv_hdr = EXTRACT_16BIT(pkt_pull(pkt, sizeof(tlv_hdr)));
167 tlv_type = LLDP_TLV_TYPE(tlv_hdr);
168 tlv_len = LLDP_TLV_LENGTH(tlv_hdr);
170 len -= sizeof(tlv_hdr);
172 if (tlv_type == LLDP_TLV_END && tlv_len == 0) {
173 /* Chassis ID, Port ID and TTL are mandatory */
174 if (n_tlv < 3)
175 goto out_invalid;
176 else
177 break;
179 if (len < tlv_len)
180 goto out_invalid;
182 switch (tlv_type) {
183 case LLDP_TLV_CHASSIS_ID:
185 * The mandatory chassis ID shall be the first TLV and
186 * shall appear exactly once.
188 if (n_tlv != 0)
189 goto out_invalid;
191 tprintf("Chassis ID");
193 if (tlv_len < 2)
194 goto out_invalid;
196 tlv_info_str = pkt_pull(pkt, tlv_len);
197 if (tlv_info_str == NULL)
198 goto out_invalid;
200 subtype = *tlv_info_str++;
201 tprintf(" (Subtype %u => ", subtype);
203 switch (subtype) {
204 case LLDP_CHASSIS_SUBTYPE_MAC_ADDR:
205 if (tlv_len < 7)
206 goto out_invalid;
208 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
209 tlv_info_str[0], tlv_info_str[1],
210 tlv_info_str[2], tlv_info_str[3],
211 tlv_info_str[4], tlv_info_str[5]);
212 break;
213 case LLDP_CHASSIS_SUBTYPE_NET_ADDR:
214 if (lldp_print_net_addr(tlv_info_str, tlv_len))
215 goto out_invalid;
216 break;
217 case LLDP_CHASSIS_SUBTYPE_CHASSIS:
218 case LLDP_CHASSIS_SUBTYPE_IF_ALIAS:
219 case LLDP_CHASSIS_SUBTYPE_PORT:
220 case LLDP_CHASSIS_SUBTYPE_IF_NAME:
221 case LLDP_CHASSIS_SUBTYPE_LOCAL:
222 tputs_safe((const char *) tlv_info_str, tlv_len - 1);
223 break;
224 default:
225 tprintf("Reserved");
226 break;
229 tprintf(")");
230 break;
231 case LLDP_TLV_PORT_ID:
233 * The mandatory port ID shall be the second TLV and
234 * shall appear exactly once.
236 if (n_tlv != 1)
237 goto out_invalid;
239 tprintf(", Port ID");
241 if (tlv_len < 2)
242 goto out_invalid;
244 tlv_info_str = pkt_pull(pkt, tlv_len);
245 if (tlv_info_str == NULL)
246 goto out_invalid;
248 subtype = *tlv_info_str++;
249 tprintf(" (Subtype %u => ", subtype);
251 switch (subtype) {
252 case LLDP_PORT_SUBTYPE_MAC_ADDR:
253 if (tlv_len < 7)
254 goto out_invalid;
256 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
257 tlv_info_str[0], tlv_info_str[1],
258 tlv_info_str[2], tlv_info_str[3],
259 tlv_info_str[4], tlv_info_str[5]);
260 break;
261 case LLDP_PORT_SUBTYPE_NET_ADDR:
262 if (lldp_print_net_addr(tlv_info_str, tlv_len))
263 goto out_invalid;
264 break;
265 case LLDP_PORT_SUBTYPE_IF_ALIAS:
266 case LLDP_PORT_SUBTYPE_PORT_COMP:
267 case LLDP_PORT_SUBTYPE_IF_NAME:
268 case LLDP_PORT_SUBTYPE_AGENT_CIRC_ID:
269 case LLDP_PORT_SUBTYPE_LOCAL:
270 tputs_safe((const char *) tlv_info_str, tlv_len - 1);
271 break;
272 default:
273 tprintf("Reserved");
274 break;
277 tprintf(")");
278 break;
279 case LLDP_TLV_TTL:
281 * The mandatory TTL shall be the third TLV and
282 * shall appear exactly once.
284 if (n_tlv != 2)
285 goto out_invalid;
287 tprintf(", TTL");
289 if (tlv_len != 2)
290 goto out_invalid;
292 tlv_info_str = pkt_pull(pkt, tlv_len);
293 if (tlv_info_str == NULL)
294 goto out_invalid;
296 tprintf(" (%u)", EXTRACT_16BIT(tlv_info_str));
297 break;
298 case LLDP_TLV_PORT_DESC:
299 tprintf(", Port desc (");
301 tlv_info_str = pkt_pull(pkt, tlv_len);
302 if (tlv_info_str == NULL)
303 tprintf("none");
304 else
305 tputs_safe((const char *) tlv_info_str, tlv_len);
307 tprintf(")");
308 break;
309 case LLDP_TLV_SYSTEM_NAME:
310 tprintf(", Sys name (");
312 tlv_info_str = pkt_pull(pkt, tlv_len);
313 if (tlv_info_str == NULL)
314 tprintf("none");
315 else
316 tputs_safe((const char *) tlv_info_str, tlv_len);
318 tprintf(")");
319 break;
320 case LLDP_TLV_SYSTEM_DESC:
321 tprintf(", Sys desc (");
323 tlv_info_str = pkt_pull(pkt, tlv_len);
324 if (tlv_info_str == NULL)
325 tprintf("none");
326 else
327 tputs_safe((const char *) tlv_info_str, tlv_len);
329 tprintf(")");
330 break;
331 case LLDP_TLV_SYSTEM_CAP:
332 tprintf(", Sys Cap");
334 if (tlv_len != 4)
335 goto out_invalid;
337 tlv_info_str = pkt_pull(pkt, tlv_len);
338 if (tlv_info_str == NULL)
339 goto out_invalid;
341 sys_cap = EXTRACT_16BIT(tlv_info_str);
342 tlv_info_str += sizeof(uint32_t);
343 en_cap = EXTRACT_16BIT(tlv_info_str);
345 tprintf(" (");
346 lldp_print_cap(sys_cap);
347 tprintf(")");
348 tprintf(" Ena Cap (");
349 lldp_print_cap(en_cap);
350 tprintf(")");
351 break;
352 case LLDP_TLV_MGMT_ADDR:
353 tprintf(", Mgmt Addr (");
355 if (tlv_len < 9 || tlv_len > 167)
356 goto out_invalid;
358 tlv_info_str = pkt_pull(pkt, tlv_len);
359 if (tlv_info_str == NULL)
360 goto out_invalid;
362 mgmt_alen = *tlv_info_str;
363 tlv_info_str++;
364 if (tlv_len - 1 < mgmt_alen)
365 goto out_invalid;
367 if (lldp_print_net_addr(tlv_info_str, mgmt_alen))
368 goto out_invalid;
369 tlv_info_str += mgmt_alen;
371 tprintf(", Iface Subtype %d/", *tlv_info_str);
372 switch (*tlv_info_str) {
373 case LLDP_IFACE_NUM_SUBTYPE_IF_INDEX:
374 tprintf("ifIndex");
375 break;
376 case LLDP_IFACE_NUM_SUBTYPE_SYS_PORT:
377 tprintf("System Port Number");
378 break;
379 default:
380 tprintf("Unknown");
381 break;
384 tlv_info_str++;
385 tprintf(", Iface Number %u", EXTRACT_32BIT(tlv_info_str));
387 tlv_info_str += 4;
388 mgmt_oidlen = *tlv_info_str;
389 if (tlv_len - mgmt_alen - sizeof(uint32_t) - 3 < mgmt_oidlen)
390 goto out_invalid;
391 if (mgmt_oidlen > 0) {
392 tprintf(", OID ");
393 tputs_safe((const char *) tlv_info_str + 1, mgmt_oidlen);
396 tprintf(")");
397 break;
398 case LLDP_TLV_ORG_SPECIFIC:
399 tprintf(", Org specific");
401 if (tlv_len < 4)
402 goto out_invalid;
404 tlv_info_str = pkt_pull(pkt, 4);
405 if (tlv_info_str == NULL)
406 goto out_invalid;
408 oui = ntohl(*((uint32_t *) tlv_info_str));
409 subtype = oui & 0xff;
410 oui >>= 8;
411 tprintf(" (OUI %s, Subtype %u)", lookup_vendor_str(oui),
412 subtype);
414 /* Just eat it up, we don't know how to interpret it */
415 pkt_pull(pkt, tlv_len - 4);
416 break;
417 default:
418 tprintf(", Unknown TLV %u", tlv_type);
419 pkt_pull(pkt, tlv_len);
420 break;
423 n_tlv++;
426 len -= tlv_len;
428 tprintf(" ]\n");
429 return;
431 out_invalid:
432 tprintf(" %sINVALID%s ]\n", colorize_start_full(black, red),
433 colorize_end());
436 static void lldp_less(struct pkt_buff *pkt)
438 unsigned int len, n_tlv = 0;
439 unsigned int tlv_type, tlv_len;
440 uint16_t tlv_hdr;
442 len = pkt_len(pkt);
444 while (len >= sizeof(tlv_hdr)) {
445 tlv_hdr = EXTRACT_16BIT(pkt_pull(pkt, sizeof(tlv_hdr)));
446 tlv_type = LLDP_TLV_TYPE(tlv_hdr);
447 tlv_len = LLDP_TLV_LENGTH(tlv_hdr);
449 n_tlv++;
450 len -= sizeof(tlv_hdr);
452 if (tlv_type == LLDP_TLV_END || tlv_len == 0)
453 break;
454 if (len < tlv_len)
455 break;
457 pkt_pull(pkt, tlv_len);
459 len -= tlv_len;
462 tprintf(" %u TLV%s", n_tlv, n_tlv == 1 ? "" : "s");
465 struct protocol lldp_ops = {
466 .key = 0x88cc,
467 .print_full = lldp,
468 .print_less = lldp_less,