summaryrefslogtreecommitdiff
path: root/usr.sbin/tcpdump/print-lldp.c
blob: 6bae94709702e9fc8f8de4b1604a5418cd0e9ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*	$OpenBSD: print-lldp.c,v 1.9 2016/11/28 17:47:15 jca Exp $	*/

/*
 * Copyright (c) 2006 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/time.h>
#include <sys/socket.h>

#include <net/if.h>

#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "addrtoname.h"
#include "extract.h"
#include "interface.h"
#include "afnum.h"

enum {
	LLDP_TLV_END			= 0,
	LLDP_TLV_CHASSIS_ID		= 1,
	LLDP_TLV_PORT_ID		= 2,
	LLDP_TLV_TTL			= 3,
	LLDP_TLV_PORT_DESCR		= 4,
	LLDP_TLV_SYSTEM_NAME		= 5,
	LLDP_TLV_SYSTEM_DESCR		= 6,
	LLDP_TLV_SYSTEM_CAP		= 7,
	LLDP_TLV_MANAGEMENT_ADDR	= 8,
	LLDP_TLV_ORG			= 127
};

enum {
	LLDP_CHASSISID_SUBTYPE_CHASSIS	= 1,
	LLDP_CHASSISID_SUBTYPE_IFALIAS	= 2,
	LLDP_CHASSISID_SUBTYPE_PORT	= 3,
	LLDP_CHASSISID_SUBTYPE_LLADDR	= 4,
	LLDP_CHASSISID_SUBTYPE_ADDR	= 5,
	LLDP_CHASSISID_SUBTYPE_IFNAME	= 6,
	LLDP_CHASSISID_SUBTYPE_LOCAL	= 7
};

enum {
	LLDP_PORTID_SUBTYPE_IFALIAS	= 1,
	LLDP_PORTID_SUBTYPE_PORT	= 2,
	LLDP_PORTID_SUBTYPE_LLADDR	= 3,
	LLDP_PORTID_SUBTYPE_ADDR	= 4,
	LLDP_PORTID_SUBTYPE_IFNAME	= 5,
	LLDP_PORTID_SUBTYPE_AGENTCID	= 6,
	LLDP_PORTID_SUBTYPE_LOCAL	= 7
};

#define LLDP_CAP_OTHER          0x01
#define LLDP_CAP_REPEATER       0x02
#define LLDP_CAP_BRIDGE         0x04
#define LLDP_CAP_WLAN           0x08
#define LLDP_CAP_ROUTER         0x10
#define LLDP_CAP_TELEPHONE      0x20
#define LLDP_CAP_DOCSIS         0x40
#define LLDP_CAP_STATION        0x80
#define LLDP_CAP_BITS								\
	"\20\01OTHER\02REPEATER\03BRIDGE\04WLAN\05ROUTER\06TELEPHONE"		\
	"\07DOCSIS\10STATION"

enum {
	LLDP_MGMT_IFACE_UNKNOWN	= 1,
	LLDP_MGMT_IFACE_IFINDEX	= 2,
	LLDP_MGMT_IFACE_SYSPORT	= 3
};

static const char *afnumber[] = AFNUM_NAME_STR;

void		 lldp_print_str(u_int8_t *, int);
const char	*lldp_print_addr(int, const void *);
void		 lldp_print_id(int, u_int8_t *, int);

void
lldp_print_str(u_int8_t *str, int len)
{
	int i;
	printf("\"");
	for (i = 0; i < len; i++)
		printf("%c", isprint(str[i]) ? str[i] : '.');
	printf("\"");
}

const char *
lldp_print_addr(int af, const void *addr)
{
	static char buf[48];
	if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
		return ("?");
	return (buf);
}

void
lldp_print_id(int type, u_int8_t *ptr, int len)
{
	u_int8_t id;
	u_int8_t *data;

	id = *(u_int8_t *)ptr;
	len -= sizeof(u_int8_t);
	data = ptr + sizeof(u_int8_t);
	if (len <= 0)
		return;

	if (type == LLDP_TLV_CHASSIS_ID) {
		switch (id) {
		case LLDP_CHASSISID_SUBTYPE_CHASSIS:
			printf("chassis ");
			lldp_print_str(data, len);
			break;
		case LLDP_CHASSISID_SUBTYPE_IFALIAS:
			printf("ifalias");
			break;
		case LLDP_CHASSISID_SUBTYPE_PORT:
			printf("port");
			break;
		case LLDP_CHASSISID_SUBTYPE_LLADDR:
			printf("lladdr %s", etheraddr_string(data));
			break;
		case LLDP_CHASSISID_SUBTYPE_ADDR:
			printf("addr");
			break;
		case LLDP_CHASSISID_SUBTYPE_IFNAME:
			printf("ifname ");
			lldp_print_str(data, len);
			break;
		case LLDP_CHASSISID_SUBTYPE_LOCAL:
			printf("local ");
			lldp_print_str(data, len);
			break;
		default:
			printf("unknown 0x%02x", id);
			break;
		}

	} else if (type == LLDP_TLV_PORT_ID) {
		switch (id) {
		case LLDP_PORTID_SUBTYPE_IFALIAS:
			printf("ifalias");
			break;
		case LLDP_PORTID_SUBTYPE_PORT:
			printf("port");
			break;
		case LLDP_PORTID_SUBTYPE_LLADDR:
			printf("lladdr %s", etheraddr_string(data));
			break;
		case LLDP_PORTID_SUBTYPE_ADDR:
			printf("addr");
			break;
		case LLDP_PORTID_SUBTYPE_IFNAME:
			printf("ifname ");
			lldp_print_str(data, len);
			break;
		case LLDP_PORTID_SUBTYPE_AGENTCID:
			printf("agentcid");
			break;
		case LLDP_PORTID_SUBTYPE_LOCAL:
			printf("local ");
			lldp_print_str(data, len);
			break;
		default:
			printf("unknown 0x%02x", id);
			break;
		}
	}
}

void
lldp_print(const u_char *p, u_int len)
{
	u_int16_t tlv;
	u_int8_t *ptr = (u_int8_t *)p, v = 0;
	int n, type, vlen, alen;

	printf("LLDP");

#define _ptrinc(_v)	ptr += (_v); vlen -= (_v);

	for (n = 0; n < len;) {
		TCHECK2(*ptr, sizeof(tlv));

		tlv = EXTRACT_16BITS(ptr);
		type = (tlv & 0xfe00) >> 9;
		vlen = tlv & 0x1ff;
		n += vlen;

		ptr += sizeof(tlv);
		TCHECK2(*ptr, vlen);

		switch (type) {
		case LLDP_TLV_END:
			goto done;
			break;

		case LLDP_TLV_CHASSIS_ID:
			printf(", ChassisId: ");
			lldp_print_id(type, ptr, vlen);
			break;

		case LLDP_TLV_PORT_ID:
			printf(", PortId: ");
			lldp_print_id(type, ptr, vlen);
			break;

		case LLDP_TLV_TTL:
			printf(", TTL: ");
			TCHECK2(*ptr, 2);
			printf("%ds", EXTRACT_16BITS(ptr));
			break;

		case LLDP_TLV_PORT_DESCR:
			printf(", PortDescr: ");
			lldp_print_str(ptr, vlen);
			break;

		case LLDP_TLV_SYSTEM_NAME:
			printf(", SysName: ");
			lldp_print_str(ptr, vlen);
			break;

		case LLDP_TLV_SYSTEM_DESCR:
			printf(", SysDescr: ");
			lldp_print_str(ptr, vlen);
			break;

		case LLDP_TLV_SYSTEM_CAP:
			printf(", CAP:");
			TCHECK2(*ptr, 4);
			printb(" available", EXTRACT_16BITS(ptr),
			    LLDP_CAP_BITS);
			_ptrinc(sizeof(u_int16_t));
			printb(" enabled", EXTRACT_16BITS(ptr),
			    LLDP_CAP_BITS);
			break;

		case LLDP_TLV_MANAGEMENT_ADDR:
			printf(", MgmtAddr:");
			TCHECK2(*ptr, 2);
			alen = *ptr - sizeof(u_int8_t);
			_ptrinc(sizeof(u_int8_t));
			v = *ptr;
			_ptrinc(sizeof(u_int8_t));
			if (v < AFNUM_MAX)
				printf(" %s", afnumber[v]);
			else
				printf(" type %d", v);
			TCHECK2(*ptr, alen);
			switch (v) {
			case AFNUM_INET:
				if (alen != sizeof(struct in_addr))
					goto trunc;
				printf(" %s",
				    lldp_print_addr(AF_INET, ptr));
				break;
			case AFNUM_INET6:
				if (alen != sizeof(struct in6_addr))
					goto trunc;
				printf(" %s",
				    lldp_print_addr(AF_INET6, ptr));
				break;
			}
			_ptrinc(alen);
			v = *(u_int8_t *)ptr;
			break;

		case LLDP_TLV_ORG:
			printf(", Org");
			break;

		default:
			printf(", type %d length %d", type, vlen);
			break;
		}
		ptr += vlen;
	}

 done:
	return;

 trunc:
	printf(" [|LLDP]");
}