forked from intel/openlldp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldp_orgspec_clif.c
317 lines (264 loc) · 7.73 KB
/
lldp_orgspec_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
313
314
315
316
/*******************************************************************************
Implementation of Organisation Specific TLVs for LLDP
(c) Copyright SuSE Linux Products GmbH, 2011
Author(s): Hannes Reinecke <hare at suse dot de>
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".
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/un.h>
#include <sys/stat.h>
#include "lldp_mod.h"
#include "clif_msgs.h"
#include "lldp.h"
#include "lldp_orgspec_clif.h"
#define VNAME_SIZE 32
#define PROTOID_SIZE 256
static void orgspec_print_pvid_tlv(u16 len, char *info);
static void orgspec_print_ppvid_tlv(u16 len, char *info);
static void orgspec_print_vlan_name_tlv(u16 len, char *info);
static void orgspec_print_protoid_tlv(u16 len, char *info);
static void orgspec_print_vid_usage_tlv(u16 len, char *info);
static void orgspec_print_mgmt_vid_tlv(u16 len, char *info);
static void orgspec_print_link_aggr_tlv(u16 len, char *info);
static int orgspec_print_help();
u32 orgspec_lookup_tlv_name(char *tlvid_str);
static const struct lldp_mod_ops orgspec_ops_clif = {
.lldp_mod_register = orgspec_cli_register,
.lldp_mod_unregister = orgspec_cli_unregister,
.print_tlv = orgspec_print_tlv,
.lookup_tlv_name = orgspec_lookup_tlv_name,
.print_help = orgspec_print_help,
};
struct type_name_info orgspec_tlv_names[] = {
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_PVID,
.name = "Port VLAN ID TLV", .key = "PVID",
.print_info = orgspec_print_pvid_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_PPVID,
.name = "Port and Protocol VLAN ID TLV", .key = "PPVID",
.print_info = orgspec_print_ppvid_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_VLAN_NAME,
.name = "VLAN Name TLV", .key = "vlanName",
.print_info = orgspec_print_vlan_name_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_PROTO_ID,
.name = "Protocol Identity TLV", .key = "ProtoID",
.print_info = orgspec_print_protoid_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_VID_USAGE,
.name = "VID Usage Digest TLV", .key = "vidUsage",
.print_info = orgspec_print_vid_usage_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_MGMT_VID,
.name = "Management VID TLV", .key = "mgmtVID",
.print_info = orgspec_print_mgmt_vid_tlv },
{ .type = (OUI_IEEE_8021 << 8) | ORG_SPEC_LINK_AGGR,
.name = "Link Aggregation TLV", .key = "linkAggr",
.print_info = orgspec_print_link_aggr_tlv },
{ .type = INVALID_TLVID, }
};
static int orgspec_print_help()
{
struct type_name_info *tn = &orgspec_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 *orgspec_cli_register(void)
{
struct lldp_module *mod;
mod = malloc(sizeof(*mod));
if (!mod) {
fprintf(stderr, "failed to malloc module data\n");
return NULL;
}
mod->id = OUI_IEEE_8021;
mod->ops = &orgspec_ops_clif;
return mod;
}
void orgspec_cli_unregister(struct lldp_module *mod)
{
free(mod);
}
static void orgspec_print_pvid_tlv(u16 len, char *info)
{
u16 pvid;
if (len != 2) {
printf("Bad PVID TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&pvid, sizeof(pvid))) {
printf("Unable to decode PVID !\n");
return;
}
pvid = ntohs(pvid);
if (pvid == 0)
printf("PVID: unsupported");
else
printf("PVID: %d", pvid);
printf("\n");
}
static void orgspec_print_ppvid_tlv(u16 len, char *info)
{
u16 pvid;
u8 flags;
if (len != 3) {
printf("Bad PPVID TLV: %s\n", info);
return;
}
if (hexstr2bin(info, &flags, sizeof(flags))) {
printf("Unable to decode PVID flags !\n");
return;
}
if (flags & 2 && (flags & 1) != 0) {
printf("Error decoding ppvid, discard\n");
return;
}
if (hexstr2bin(info + 2, (u8 *)&pvid, sizeof(pvid))) {
printf("Unable to decode PVID !\n");
return;
}
printf("PVID: %x,%s supported,%s enabled",
ntohs(pvid), flags & 2 ? "" : " not",
flags & 1 ? "" : " not");
printf("\n");
}
static void orgspec_print_vlan_name_tlv(u16 len, char *info)
{
u16 vid;
u8 name_len;
unsigned char vlan_name[VNAME_SIZE] = {0};
if (len < 4) {
printf("Bad VLAN Name TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&vid, sizeof(vid))) {
printf("Unable to decode VID !\n");
return;
}
if (hexstr2bin(info + 4, &name_len, sizeof(name_len)))
name_len = 0;
if (!hexstr2bin(info + 6, vlan_name, name_len))
printf("VID %d: Name %s", ntohs(vid), vlan_name);
printf("\n");
}
static void orgspec_print_protoid_tlv(u16 len, char *info)
{
u8 protoid_len;
unsigned char protoid[PROTOID_SIZE] = {0};
int i;
if (len < 1) {
printf("Bad Protocol Identity TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&protoid_len, sizeof(protoid_len))) {
printf("Unable to decode VID !\n");
return;
}
if (!hexstr2bin(info + 2, protoid, protoid_len)) {
for (i = 0; i < protoid_len; i++)
printf("%02x.", protoid[i]);
}
printf("\n");
}
static void orgspec_print_vid_usage_tlv(u16 len, char *info)
{
u16 vid;
u8 name_len;
unsigned char vlan_name[VNAME_SIZE] = {0};
if (len < 4) {
printf("Bad VLAN Name TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&vid, sizeof(vid))) {
printf("Unable to decode VID !\n");
return;
}
if (hexstr2bin(info + 4, &name_len, sizeof(name_len)))
name_len = 0;
if (!hexstr2bin(info + 6, vlan_name, name_len))
printf("VID %d: Name %s", ntohs(vid), vlan_name);
printf("\n");
}
static void orgspec_print_mgmt_vid_tlv(u16 len, char *info)
{
u16 mgmt_vid;
if (len != 2) {
printf("Bad Mgmt VID TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&mgmt_vid, sizeof(mgmt_vid))) {
printf("Unable to decode PVID !\n");
return;
}
mgmt_vid = ntohs(mgmt_vid);
if (mgmt_vid == 0)
printf("Mgmt VID: unsupported\n");
else
printf("Mgmt VID: %d\n", mgmt_vid);
}
static void orgspec_print_link_aggr_tlv(u16 len, char *info)
{
u8 agg_status;
u32 agg_portid;
if (len != 5) {
printf("Bad Link Aggregation TLV: %s\n", info);
return;
}
if (hexstr2bin(info, (u8 *)&agg_status, sizeof(agg_status))) {
printf("Unable to decode Link Aggregation TLV !\n");
return;
}
if (!hexstr2bin(info + 2, (u8 *)&agg_portid, sizeof(agg_portid))) {
printf("Aggregation %scapable\n",
(agg_status & 0x01) ? "" : "not ");
printf("\tCurrently %saggregated\n",
(agg_status & 0x02) ? "" : "not ");
printf("\tAggregated Port ID: %d\n", ntohl(agg_portid));
}
}
/* return 1: if it printed the TLV
* 0: if it did not
*/
int orgspec_print_tlv(u32 tlvid, u16 len, char *info)
{
struct type_name_info *tn = &orgspec_tlv_names[0];
while (tn->type != INVALID_TLVID) {
if (tlvid == tn->type) {
printf("%s\n", tn->name);
if (tn->print_info) {
printf("\t");
tn->print_info(len-4, info);
}
return 1;
}
tn++;
}
return 0;
}
u32 orgspec_lookup_tlv_name(char *tlvid_str)
{
struct type_name_info *tn = &orgspec_tlv_names[0];
while (tn->type != INVALID_TLVID) {
if (!strcasecmp(tn->key, tlvid_str))
return tn->type;
tn++;
}
return INVALID_TLVID;
}