-
Notifications
You must be signed in to change notification settings - Fork 8
/
lldp_basman_clif.c
312 lines (274 loc) · 7.32 KB
/
lldp_basman_clif.c
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
304
305
306
307
308
309
310
311
312
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2010 Intel Corporation.
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <[email protected]>
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include "lldp_mod.h"
#include "lldptool.h"
#include "clif_msgs.h"
#include "lldp_basman.h"
#include "lldp_basman_clif.h"
void print_string_tlv(u16, char *info);
void print_capabilities(u16, char *info);
void print_mng_addr(u16, char *info);
u32 basman_lookup_tlv_name(char *tlvid_str);
int basman_print_help();
static const struct lldp_mod_ops basman_ops_clif = {
.lldp_mod_register = basman_cli_register,
.lldp_mod_unregister = basman_cli_unregister,
.print_tlv = basman_print_tlv,
.lookup_tlv_name = basman_lookup_tlv_name,
.print_help = basman_print_help,
};
struct type_name_info basman_tlv_names[] = {
{ .type = PORT_DESCRIPTION_TLV,
.name = "Port Description TLV", .key = "portDesc",
.print_info = print_string_tlv, },
{ .type = SYSTEM_NAME_TLV,
.name = "System Name TLV", .key = "sysName",
.print_info = print_string_tlv, },
{ .type = SYSTEM_DESCRIPTION_TLV,
.name = "System Description TLV", .key = "sysDesc",
.print_info = print_string_tlv, },
{ .type = SYSTEM_CAPABILITIES_TLV,
.name = "System Capabilities TLV", .key = "sysCap",
.print_info = print_capabilities, },
{ .type = MANAGEMENT_ADDRESS_TLV,
.name = "Management Address TLV", .key = "mngAddr",
.print_info = print_mng_addr, },
{ .type = INVALID_TLVID, }
};
int basman_print_help()
{
struct type_name_info *tn = &basman_tlv_names[0];
while (tn->type != INVALID_TLVID) {
if (tn->key && strlen(tn->key) && tn->name) {
printf(" %s", tn->key);
if (strlen(tn->key)+3 < 8)
printf("\t");
printf("\t: %s\n", tn->name);
}
tn++;
}
return 0;
}
struct lldp_module *basman_cli_register(void)
{
struct lldp_module *mod;
mod = malloc(sizeof(*mod));
if (!mod) {
fprintf(stderr, "failed to malloc module data\n");
goto out_err;
}
mod->id = LLDP_MOD_BASIC;
mod->ops = &basman_ops_clif;
return mod;
out_err:
return NULL;
}
void basman_cli_unregister(struct lldp_module *mod)
{
free(mod);
}
void print_string_tlv(u16 len, char *info)
{
int i;
for (i = 0; i < 2*len; i+=2)
printf("%c", hex2int(info+i));
printf("\n");
}
void print_capability_list(u16 capabilities)
{
int print_comma = 0;
if (capabilities & 0x01) {
printf("Other");
print_comma = 1;
}
if (capabilities & 0x02) {
printf("%sRepeater", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x04) {
printf("%sBridge", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x08) {
printf("%sWLAN Access Point", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x10) {
printf("%sRouter", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x20) {
printf("%sTelephone", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x40) {
printf("%sDOCSIS cable device", (print_comma)?", ":"");
print_comma = 1;
}
if (capabilities & 0x80) {
printf("%sStation Only", (print_comma)?", ":"");
print_comma = 1;
}
}
void print_capabilities(u16 len, char *info)
{
u16 system_cap;
u16 enabled_cap;
if (len != 4) {
printf("Bad System Capabilities TLV: %*.*s\n",
2*len, 2*len, info);
return;
}
hexstr2bin(info, (u8 *)&system_cap, sizeof(system_cap));
system_cap = ntohs(system_cap);
hexstr2bin(info+4, (u8 *)&enabled_cap, sizeof(enabled_cap));
enabled_cap = ntohs(enabled_cap);
printf("System capabilities: ");
print_capability_list(system_cap);
printf("\n");
printf("\tEnabled capabilities: ");
print_capability_list(enabled_cap);
printf("\n");
}
void print_mng_addr(u16 len, char *info)
{
u8 addrlen;
u8 addrnum;
u8 iftype;
u8 oidlen;
u32 ifnum;
u32 offset;
int i;
char buf[132];
if (len < 9 || len > 167) {
printf("Bad Management Address TLV: %*.*s\n",
2*len, 2*len, info);
return;
}
hexstr2bin(info, (u8 *)&addrlen, sizeof(addrlen));
hexstr2bin(info+2, (u8 *)&addrnum, sizeof(addrnum));
switch(addrnum) {
case MANADDR_ALL802:
if (addrlen != 1 + 6)
return;
printf("MAC: ");
for (i = 0; i < 12; i+=2) {
printf("%2.2s", info + 4 + i);
if (i < 10)
printf(":");
else
printf("\n");
}
break;
case MANADDR_IPV4:
if (addrlen == 5) {
struct in_addr addr;
hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
inet_ntop(AF_INET, (void *)&addr, buf,
sizeof(buf));
printf("IPv4: %s\n", buf);
} else {
printf("Bad IPv4: %*.*s\n",
2*(addrlen-2), 2*(addrlen-2), info+4);
}
break;
case MANADDR_IPV6:
if (addrlen == 17) {
struct in6_addr addr;
hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
memset(buf, 0, sizeof(buf));
inet_ntop(AF_INET6, (void *)&addr, buf,
sizeof(buf));
printf("IPv6: %s\n", buf);
} else {
printf("Bad IPv6: %*.*s\n",
2*(addrlen-2), 2*(addrlen-2), info+4);
}
break;
default:
printf("Network Address Type %d: %*.*s\n", addrnum,
2*(addrlen-1), 2*(addrlen-1), info+4);
break;
}
offset = 2*(1+addrlen);
hexstr2bin(info+offset, (u8 *)&iftype, sizeof(iftype));
offset += 2;
hexstr2bin(info+offset, (u8 *)&ifnum, sizeof(ifnum));
offset += 2*sizeof(u32);
ifnum = ntohl(ifnum);
switch (iftype) {
case IFNUM_UNKNOWN:
printf("\tUnknown interface subtype: ");
break;
case IFNUM_IFINDEX:
printf("\tIfindex: ");
break;
case IFNUM_SYS_PORT_NUM:
printf("\tSystem port number: ");
break;
default:
printf("\tBad interface numbering subtype: ");
break;
}
printf("%d\n", ifnum);
hexstr2bin(info+offset, (u8 *)&oidlen, sizeof(oidlen));
offset += 2;
if (oidlen && oidlen <= 128) {
memset(buf, 0, sizeof(buf));
if (hexstr2bin(info+offset, (u8 *)&buf, oidlen))
printf("\tOID: Error parsing OID\n");
else
printf("\tOID: %s\n", buf);
} else if (oidlen > 128) {
printf("\tOID: Invalid length = %d\n", oidlen);
}
}
/* return 1: if it printed the TLV
* 0: if it did not
*/
int basman_print_tlv(u32 tlvid, u16 len, char *info)
{
struct type_name_info *tn = &basman_tlv_names[0];
while (tn->type != INVALID_TLVID) {
if (tlvid == tn->type) {
printf("%s\n\t", tn->name);
if (tn->print_info)
tn->print_info(len, info);
return 1;
}
tn++;
}
return 0;
}
u32 basman_lookup_tlv_name(char *tlvid_str)
{
struct type_name_info *tn = &basman_tlv_names[0];
while (tn->type != INVALID_TLVID) {
if (!strcasecmp(tn->key, tlvid_str))
return tn->type;
tn++;
}
return INVALID_TLVID;
}