forked from intel/openlldp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldp_med.c
963 lines (867 loc) · 24.1 KB
/
lldp_med.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/*******************************************************************************
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 <lldp-devel@open-lldp.org>
*******************************************************************************/
#include <stdlib.h>
#include <errno.h>
#include <net/if.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <linux/if_bridge.h>
#include "lldp.h"
#include "lldp_med.h"
#include "messages.h"
#include "config.h"
#include "libconfig.h"
#include "lldp_mand_clif.h"
#include "lldp_med_cmds.h"
struct tlv_info_medcaps {
u8 oui[OUI_SIZE];
u8 subtype;
u16 medcaps;
u8 devtype;
} __attribute__ ((__packed__));
struct tlv_info_netpoli {
u8 oui[OUI_SIZE];
u8 subtype;
u8 apptype;
u16 unknown:1;
u16 tagged:1;
u16 reserved:1;
u16 vid:12;
u16 priority:3;
u16 dscp:6;
} __attribute__ ((__packed__));
struct tlv_info_extpvm {
u8 oui[OUI_SIZE];
u8 subtype;
u8 powtype:2;
u8 powsrc:2;
u8 powprio:4;
u16 powval;
} __attribute__ ((__packed__));
struct tlv_info_locid {
u8 oui[OUI_SIZE];
u8 subtype;
u8 format;
union {
u8 coord[16];
u8 civic[256];
u8 ecselin[25];
} lci;
} __attribute__ ((__packed__));
static const struct lldp_mod_ops med_ops = {
.lldp_mod_register = med_register,
.lldp_mod_unregister = med_unregister,
.lldp_mod_gettlv = med_gettlv,
.lldp_mod_ifup = med_ifup,
.lldp_mod_ifdown = med_ifdown,
.get_arg_handler = med_get_arg_handlers,
};
static struct med_data *med_data(const char *ifname, enum agent_type type)
{
struct med_user_data *mud;
struct med_data *md = NULL;
mud = find_module_user_data_by_id(&lldp_mod_head, LLDP_MOD_MED);
if (mud) {
LIST_FOREACH(md, &mud->head, entry) {
if (!strncmp(ifname, md->ifname, IFNAMSIZ) &&
(type == md->agenttype))
return md;
}
}
return NULL;
}
/* TODO : check config for optional caps */
static u16 med_get_caps(u8 devtype)
{
u16 medcaps = 0;
switch(devtype) {
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_I:
medcaps = (LLDP_MED_CAPABILITY_CAPAPILITIES |
LLDP_MED_CAPABILITY_INVENTORY);
break;
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_II:
medcaps = (LLDP_MED_CAPABILITY_CAPAPILITIES |
LLDP_MED_CAPABILITY_INVENTORY);
break;
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_III:
medcaps = (LLDP_MED_CAPABILITY_CAPAPILITIES |
LLDP_MED_CAPABILITY_INVENTORY |
LLDP_MED_CAPABILITY_LOCATION_ID);
break;
case LLDP_MED_DEVTYPE_NETWORK_CONNECTIVITY:
LLDPAD_DBG("%s:WARNING:NETWORK_CONNECTIVITY is"
"supported for bridge only\n", __func__);
medcaps = (LLDP_MED_CAPABILITY_CAPAPILITIES |
LLDP_MED_CAPABILITY_NETWORK_POLICY |
LLDP_MED_CAPABILITY_LOCATION_ID);
break;
case LLDP_MED_DEVTYPE_NOT_DEFINED:
default:
LLDPAD_DBG("%s:devtype %d not supported\n",
__func__, devtype);
break;
}
return medcaps;
}
/*
* med_bld_medcaps_tlv - build the LLDP-MED Capabilities TLV
* @md: the med data struct
*
* Try to load the med caps from the config if it exists, otherwise
* build the med caps tlv from the scratch
*
* Returns 0 on success
*/
static int med_bld_medcaps_tlv(struct med_data *md,
struct lldp_agent *agent)
{
int rc = EPERM;
struct tlv_info_medcaps medcaps;
struct unpacked_tlv *tlv = NULL;
/* free old one if it exists */
FREE_UNPKD_TLV(md, medcaps);
/* must be enabled */
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(LLDP_MED_CAPABILITIES))) {
LLDPAD_DBG("%s:%s:MED Caps is not enabled\n",
__func__, md->ifname);
rc = 0;
goto out_err;
}
/* load cap tlv info from config */
memset(&medcaps, 0, sizeof(medcaps));
if (get_config_tlvinfo_bin(md->ifname, agent->type,
TLVID_MED(LLDP_MED_CAPABILITIES),
&medcaps, sizeof(medcaps))) {
LLDPAD_DBG("%s:%s:Build MED Caps as Endpoint Class I\n",
__func__, md->ifname);
goto out_bld;
}
/* validate the data loaded */
if (LLDP_MED_DEVTYPE_DEFINED(medcaps.devtype) &&
(medcaps.devtype == get_med_devtype(md->ifname, agent->type))) {
LLDPAD_DBG("%s:%s:MED Caps loaded from config as type %d\n",
__func__, md->ifname, medcaps.devtype);
goto out_create;
}
LLDPAD_DBG("%s:%s:Load MED Caps is invalid\n",
__func__, md->ifname);
out_bld:
/* Not in config, build from scratch */
hton24(medcaps.oui, OUI_TIA_TR41);
medcaps.subtype = LLDP_MED_CAPABILITIES;
medcaps.devtype = get_med_devtype(md->ifname, agent->type);
medcaps.medcaps = htons(med_get_caps(medcaps.devtype));
out_create:
tlv = create_tlv();
if (!tlv) {
rc = ENOMEM;
goto out_err;
}
tlv->type = ORG_SPECIFIC_TLV;
tlv->length = sizeof(medcaps);
tlv->info = (u8 *)malloc(tlv->length);
if(!tlv->info) {
free(tlv);
tlv = NULL;
rc = ENOMEM;
goto out_err;
}
memcpy(tlv->info, &medcaps, tlv->length);
md->medcaps = tlv;
set_med_devtype(md->ifname, agent->type, medcaps.devtype);
set_config_tlvinfo_bin(md->ifname, agent->type,
TLVID_MED(LLDP_MED_CAPABILITIES),
&medcaps, sizeof(medcaps));
rc = 0;
out_err:
return rc;
}
#define SYSFS_INV_PATH "/sys/class/dmi/id"
#define PROC_INV_PATH "/proc/sys/kernel"
#define PATH_INV_HWREV SYSFS_INV_PATH "/board_version"
#define PATH_INV_FWREV SYSFS_INV_PATH "/bios_version"
#define PATH_INV_SWREV PROC_INV_PATH "/osrelease"
#define PATH_INV_MANUFACTURER SYSFS_INV_PATH "/sys_vendor"
#define PATH_INV_SERIAL SYSFS_INV_PATH "/product_uuid"
#define PATH_INV_MODELNAME SYSFS_INV_PATH "/product_name"
#define PATH_INV_ASSETID SYSFS_INV_PATH "/chassis_serial"
/*
* TODO: If supports IETF RFC 2737
*/
int med_read_inventory(u8 subtype, char *buf, size_t size)
{
char path[256];
struct utsname uts;
FILE *f = NULL;
memset(buf, 0, size);
memset(path, 0, sizeof(path));
switch (subtype) {
case LLDP_MED_INV_HWREV:
strncpy(path, PATH_INV_HWREV, sizeof(path) - 1);
break;
case LLDP_MED_INV_FWREV:
strncpy(path, PATH_INV_FWREV, sizeof(path) - 1);
break;
case LLDP_MED_INV_SWREV:
if (!uname(&uts)) {
strncpy(buf, uts.release, size - 1);
goto out_err;
}
LLDPAD_DBG("%s: uname() failed for %d, try"
" proc fs\n", __func__, subtype);
strncpy(path, PATH_INV_SWREV, sizeof(path) - 1);
break;
case LLDP_MED_INV_SERIAL:
strncpy(path, PATH_INV_SERIAL, sizeof(path) - 1);
break;
case LLDP_MED_INV_MANUFACTURER:
strncpy(path, PATH_INV_MANUFACTURER, sizeof(path) - 1);
break;
case LLDP_MED_INV_MODELNAME:
strncpy(path, PATH_INV_MODELNAME, sizeof(path) - 1);
break;
case LLDP_MED_INV_ASSETID:
strncpy(path, PATH_INV_ASSETID, sizeof(path) - 1);
break;
default:
LLDPAD_DBG("%s: unknown inventory subtype %d\n",
__func__, subtype);
goto out_err;
}
f = fopen(path, "r");
if (!f) {
LLDPAD_DBG("%s: fopen(%s) failed for type %d\n",
__func__, path, subtype);
goto out_err;
}
if (!fgets(buf, size, f)) {
LLDPAD_DBG("%s: fgets(%s) failed for type %d\n",
__func__, path, subtype);
memset(buf, 0, size);
}
fclose(f);
out_err:
return strlen(buf);
}
/*
* med_bld_invtlv - builds inventory tlv by subtype
* @md: the med data struct
* @subtype: LLDP-MED inventory tlv subtype
*/
static struct unpacked_tlv *med_bld_invtlv(struct med_data *md,
struct lldp_agent *agent,
u8 subtype)
{
int length;
u8 desc[33];
struct unpacked_tlv *tlv = NULL;
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(subtype))) {
LLDPAD_DBG("%s:%s:subtype %d tx disabled\n",
__func__, md->ifname, subtype);
goto out_err;
}
length = med_read_inventory(subtype, (char *)desc, sizeof(desc));
if (!length) {
LLDPAD_DBG("%s:%s:med_read_inventory(%d) failed\n",
__func__, md->ifname, subtype);
goto out_err;
}
if (desc[length - 1] == '\n')
length--;
tlv = create_tlv();
if (!tlv) {
LLDPAD_DBG("%s:%s:creat_tlv(%d) failed\n",
__func__, md->ifname, subtype);
goto out_err;
}
memset(tlv, 0, sizeof(struct unpacked_tlv));
tlv->type = ORG_SPECIFIC_TLV;
tlv->length = length + OUI_SUB_SIZE;
tlv->info = (u8 *) malloc(tlv->length);
if (!tlv->info) {
free(tlv);
tlv = NULL;
goto out_err;
}
hton24(tlv->info, OUI_TIA_TR41);
tlv->info[OUI_SIZE] = subtype;
memcpy(&tlv->info[OUI_SUB_SIZE], desc, length);
out_err:
return tlv;
}
/*
* med_bld_inventory_tlv - builds all inventory tlvs
* @md: the med data struct
*
*/
static int med_bld_inventory_tlv(struct med_data *md,
struct lldp_agent *agent)
{
FREE_UNPKD_TLV(md, inv_hwrev);
FREE_UNPKD_TLV(md, inv_fwrev);
FREE_UNPKD_TLV(md, inv_swrev);
FREE_UNPKD_TLV(md, inv_serial);
FREE_UNPKD_TLV(md, inv_manufacturer);
FREE_UNPKD_TLV(md, inv_modelname);
FREE_UNPKD_TLV(md, inv_assetid);
md->inv_hwrev = med_bld_invtlv(md, agent, LLDP_MED_INV_HWREV);
md->inv_fwrev = med_bld_invtlv(md, agent, LLDP_MED_INV_FWREV);
md->inv_swrev = med_bld_invtlv(md, agent, LLDP_MED_INV_SWREV);
md->inv_serial = med_bld_invtlv(md, agent, LLDP_MED_INV_SERIAL);
md->inv_manufacturer = med_bld_invtlv(md, agent, LLDP_MED_INV_MANUFACTURER);
md->inv_modelname = med_bld_invtlv(md, agent, LLDP_MED_INV_MODELNAME);
md->inv_assetid = med_bld_invtlv(md, agent, LLDP_MED_INV_ASSETID);
return 0;
}
static int med_is_pd(const char *ifname)
{
LLDPAD_DBG("%s:%s: TODO\n", __func__, ifname);
return 0;
}
static int med_is_pse(const char *ifname)
{
LLDPAD_DBG("%s:%s: TODO\n", __func__, ifname);
return 0;
}
/*
* med_bld_powvmdi_tlv - builds power via mdi tlv
* @port: the port associated
*
* Returns 0 for success or error code for failure
*
* TODO:
* When Extended Power via MDI TLV is enabled, it is recommended
* by the spec ANSI-TIA-1057 Clause 9.2.4 to to disable 802.3AB
* Optional Power via MDI TLV.
*
*/
static int med_bld_powvmdi_tlv(struct med_data *md,
struct lldp_agent *agent)
{
int rc = EINVAL;
int devtype;
int mandatory = 0;
struct tlv_info_extpvm extpvm;
struct unpacked_tlv *tlv = NULL;
/* free old one if it exists */
FREE_UNPKD_TLV(md, extpvm);
devtype = get_med_devtype(md->ifname, agent->type);
switch (devtype) {
case LLDP_MED_DEVTYPE_NETWORK_CONNECTIVITY:
mandatory = med_is_pse(md->ifname);
break;
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_I:
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_II:
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_III:
mandatory = med_is_pd(md->ifname);
break;
default:
LLDPAD_DBG("%s:%s: Unknown device type %d\n",
__func__, md->ifname, devtype);
rc = EINVAL;
goto out_err;
}
if (!mandatory) {
LLDPAD_DBG("%s:%s: No need to send Extended Power via MDI TLV\n",
__func__, md->ifname);
rc = 0;
goto out_err;
}
/* Mandatory */
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(LLDP_MED_EXTENDED_PVMDI))) {
LLDPAD_DBG("%s:%s: Must enable Extended Power via MDI TLV as it"
" mandatory for device type %d\n",
__func__, md->ifname, devtype);
rc = EPERM;
goto out_err;
}
/* Load from config */
if (get_config_tlvinfo_bin(md->ifname, agent->type,
TLVID_MED(LLDP_MED_NETWORK_POLICY),
&extpvm, sizeof(extpvm))) {
LLDPAD_DBG("%s:%s: Must configure Extended Power via MDI TLV as "
" currently it has to be manually configured\n",
__func__, md->ifname);
rc = EINVAL;
goto out_err;
}
/* disable Optional Power via MDI */
tlv_disabletx(md->ifname, agent->type,
TLVID_8023(LLDP_8023_POWER_VIA_MDI));
/* We should have a valid tlv_info_extpvm here */
if (extpvm.subtype != LLDP_MED_EXTENDED_PVMDI) {
LLDPAD_DBG("%s:%s: Wrong subtype %d: should be %d\n",
__func__, md->ifname, extpvm.subtype, LLDP_MED_EXTENDED_PVMDI);
rc = EINVAL;
goto out_err;
}
tlv = create_tlv();
if (!tlv) {
rc = ENOMEM;
goto out_err;
}
tlv->type = ORG_SPECIFIC_TLV;
tlv->length = sizeof(extpvm);
tlv->info = (u8 *)malloc(tlv->length);
if(!tlv->info) {
free(tlv);
rc = ENOMEM;
goto out_err;
}
memcpy(tlv->info, &extpvm, tlv->length);
md->extpvm = tlv;
rc = 0;
out_err:
return rc;
}
/*
* med_bld_locid_tlv - builds location id tlv
* @md: the med data struct
*
* Returns 0 for success or error code for failure
*
* TODO: currently only supports load from config, will not transmit if
* if it's not configured
*
*/
static int med_bld_locid_tlv(struct med_data *md,
struct lldp_agent *agent)
{
int rc = 0;
size_t length;
char *locstr = NULL;
struct tlv_info_locid locid;
struct unpacked_tlv *tlv = NULL;
/* free old one if it exists */
FREE_UNPKD_TLV(md, locid);
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(LLDP_MED_LOCATION_ID))) {
LLDPAD_DBG("%s:%s:Location Id TLV is not enabled\n",
__func__, md->ifname);
rc = 0;
goto out_err;
}
/* location data size varies from type, query as string w/ max length */
length = sizeof(struct tlv_info_locid) * 2 + 1;
locstr = malloc(length);
if (!locstr)
goto out_err;
memset(locstr, 0, length);
if (get_config_tlvinfo_str(md->ifname, agent->type,
TLVID_MED(LLDP_MED_LOCATION_ID),
locstr, length)) {
LLDPAD_DBG("%s:%s:Location Id TLV must be"
" administratively configured\n",
__func__, md->ifname);
goto out_err;
}
/* calculate the size in binary */
length = strlen(locstr) / 2;
if (hexstr2bin(locstr, (u8 *)&locid, length)) {
LLDPAD_DBG("%s:%s:Location Id TLV info corrupted: %s\n",
__func__, md->ifname, locstr);
goto out_err;
}
/* validate the loaded data */
if (LLDP_MED_LOCID_FORMAT_INVALID(locid.format)) {
LLDPAD_DBG("%s:%s:Location Id TLV info invalid: %s\n",
__func__, md->ifname, locstr);
goto out_err;
}
tlv = create_tlv();
if (!tlv)
goto out_err;
tlv->type = ORG_SPECIFIC_TLV;
tlv->length = length;
tlv->info = (u8 *)malloc(tlv->length);
if(!tlv->info) {
free(tlv);
goto out_err;
}
memcpy(tlv->info, &locid, tlv->length);
md->locid = tlv;
rc = 0;
out_err:
if (locstr)
free(locstr);
return rc;
}
/*
* med_get_netpoli - builds tlv_info_netploi struct
* @md: the med data struct
* @n: the tlv_info_netpoli data struct
*
* Returns 0 for success or error code for failure
*
* Either gets the netpoli from the config or build from scratch.
*
* TODO: currently only supports load from config, will fail if it's
* not configured
*/
static int med_get_netpoli(struct med_data *md,
struct lldp_agent *agent,
struct tlv_info_netpoli *n)
{
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(LLDP_MED_NETWORK_POLICY)))
return ENOENT;
if (!get_config_tlvinfo_bin(md->ifname, agent->type,
TLVID_MED(LLDP_MED_NETWORK_POLICY),
n, sizeof(*n)))
return 0;
return EPERM;
}
/*
* med_bld_netpoli_tlv - builds network policy tlv
* @port: the port associated
*
* Returns 0 for success or error code for failure
*/
static int med_bld_netpoli_tlv(struct med_data *md,
struct lldp_agent *agent)
{
int rc = EPERM;
int devtype;
struct tlv_info_netpoli netpoli;
struct unpacked_tlv *tlv = NULL;
/* free old one if it exists */
FREE_UNPKD_TLV(md, netpoli);
devtype = get_med_devtype(md->ifname, agent->type);
switch (devtype) {
case LLDP_MED_DEVTYPE_NETWORK_CONNECTIVITY:
/* Only transmit if it is administratively configured */
rc = med_get_netpoli(md, agent, &netpoli);
if (rc == ENOENT)
goto out_nobld;
if (rc == EPERM) {
LLDPAD_DBG("%s:%s: Must configure Network"
" Policy TLV if it is enabled for Network"
" Connectivity Devcie\n",
__func__, md->ifname);
goto out_err;
}
break;
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_I:
/* Optional: skip if failed to get it */
if (med_get_netpoli(md, agent, &netpoli)) {
LLDPAD_DBG("%s:%s: Skipping"
" Network Policy TLV for Class I Device\n",
__func__, md->ifname);
goto out_nobld;
}
break;
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_II:
case LLDP_MED_DEVTYPE_ENDPOINT_CLASS_III:
/* Mandatory */
if (med_get_netpoli(md, agent, &netpoli)) {
LLDPAD_DBG("%s:%s: Must enable and configure"
" Network Policy TLV for Class II/III Device\n",
__func__, md->ifname);
goto out_err;
}
break;
default:
LLDPAD_DBG("%s:%s: unknown dev type %d:\n",
__func__, md->ifname, devtype);
goto out_err;
}
/* We should have a valid tlv_info_netpoli */
if (netpoli.subtype != LLDP_MED_NETWORK_POLICY) {
LLDPAD_DBG("%s:%s: Wrong subtype %d: should be %d\n",
__func__, md->ifname, netpoli.subtype, LLDP_MED_NETWORK_POLICY);
rc = EINVAL;
goto out_err;
}
tlv = create_tlv();
if (!tlv) {
rc = ENOMEM;
goto out_err;
}
tlv->type = ORG_SPECIFIC_TLV;
tlv->length = sizeof(netpoli);
tlv->info = (u8 *)malloc(tlv->length);
if(!tlv->info) {
free(tlv);
rc = ENOMEM;
goto out_err;
}
memcpy(tlv->info, &netpoli, tlv->length);
md->netpoli = tlv;
out_nobld:
rc = 0;
out_err:
return rc;
}
/*
* med_bld_tlv - build all LLDP-MED TLVs
* @md: the med data struct
*
* Returns 0 for success or error code for failure
*/
static int med_bld_tlv(struct med_data *md, struct lldp_agent *agent)
{
if (!port_find_by_ifindex(get_ifidx(md->ifname)))
return -EEXIST;
/* no build if not enabled */
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_MED(LLDP_MED_RESERVED))) {
LLDPAD_DBG("%s:%s:LLDP-MED is not enabled\n",
__func__, md->ifname);
return 0;
}
/* no build if enabled no devtype is given */
if (!LLDP_MED_DEVTYPE_DEFINED(get_med_devtype(md->ifname, agent->type))) {
LLDPAD_DBG("%s:%s:LLDP-MED devtype is not defined\n",
__func__, md->ifname);
return -EPERM;
}
/* MED Cap is always mandatory for MED */
if (med_bld_medcaps_tlv(md, agent)) {
LLDPAD_DBG("%s:%s:MED Capabilities TLV is mandatory!\n",
__func__, md->ifname);
return -EPERM;
}
/* MAC PHY TLV is mandatory for MED */
if (!is_tlv_txenabled(md->ifname, agent->type,
TLVID_8023(LLDP_8023_MACPHY_CONFIG_STATUS))) {
LLDPAD_DBG("%s:%s MAC PHY Config is mandatory for MED\n",
__func__, md->ifname);
return -ENOTTY;
}
/* Build optional and conditional ones based on device type:
*
* LLDP-MED Endpoint Class I TLV Set: ANSI/TIA-1057-2006, 10.2.1.2
* - Capabilities: mandatory
* - Network Policy: optional
* - Extended Power-via-MDI: mandatory only for 802.3af PD
* - Inventory: optional
* LLDP-MED Endpoint Class II TLV Set: ANSI/TIA-1057-2006, 10.2.1.3
* - Capabilities: mandatory
* - Network Policy: mandatory
* - Extended Power-via-MDI: mandatory only for 802.3.af PD
* - Inventory: optional
* LLDP-MED Endpoint Class III TLV Set: ANSI/TIA-1057-2006, 10.2.1.4
* - Capabilities: mandatory
* - Network Policy: mandatory
* - Location Identification: optional
* - Extended Power-via-MDI: mandatory only for 802.3.af PD
* - Inventory: optional
* LLDP-MED Network Connectivity TLV Set: ANSI/TIA-1057-2006, 10.2.1.1
* - Capabilities: mandatory
* - Network Policy: exists only if administratively
* configured
* - Location Identification: exists only if
* administratively configured
* - Extended Power-via-MDI: mandatory only for
* 802.3.af PSE
* - Inventory: optional
*/
if (med_bld_netpoli_tlv(md, agent)) {
LLDPAD_DBG("%s:%s:med_bld_netpoli_tlv() failed\n",
__func__, md->ifname);
return -EPERM;
}
if (med_bld_locid_tlv(md, agent)) {
LLDPAD_DBG("%s:%s:med_bld_locid_tlv() failed\n",
__func__, md->ifname);
return -EPERM;
}
if (med_bld_powvmdi_tlv(md, agent)) {
LLDPAD_DBG("%s:%s:med_bld_powvmdi_tlv() failed\n",
__func__, md->ifname);
return -EPERM;
}
if (med_bld_inventory_tlv(md, agent)) {
LLDPAD_DBG("%s:%s:med_bld_inventory_tlv() failed\n",
__func__, md->ifname);
return -EPERM;
}
return 0;
}
/*
* med_free_tlv - free allocated tlvs
* @md: the private med data struct
*/
static void med_free_tlv(struct med_data *md)
{
if (md) {
FREE_UNPKD_TLV(md, medcaps);
FREE_UNPKD_TLV(md, netpoli);
FREE_UNPKD_TLV(md, locid);
FREE_UNPKD_TLV(md, extpvm);
FREE_UNPKD_TLV(md, inv_hwrev);
FREE_UNPKD_TLV(md, inv_fwrev);
FREE_UNPKD_TLV(md, inv_swrev);
FREE_UNPKD_TLV(md, inv_serial);
FREE_UNPKD_TLV(md, inv_manufacturer);
FREE_UNPKD_TLV(md, inv_modelname);
FREE_UNPKD_TLV(md, inv_assetid);
}
}
static void med_free_data(struct med_user_data *mud)
{
struct med_data *md;
if (mud) {
while (!LIST_EMPTY(&mud->head)) {
md = LIST_FIRST(&mud->head);
LIST_REMOVE(md, entry);
med_free_tlv(md);
free(md);
}
}
}
struct packed_tlv *med_gettlv(struct port *port,
struct lldp_agent *agent)
{
size_t size;
struct med_data *md;
struct packed_tlv *ptlv = NULL;
md = med_data(port->ifname, agent->type);
if (!md)
goto out_err;
med_free_tlv(md);
if (med_bld_tlv(md, agent)) {
goto out_err;
}
size = TLVSIZE(md->medcaps)
+ TLVSIZE(md->netpoli)
+ TLVSIZE(md->locid)
+ TLVSIZE(md->extpvm)
+ TLVSIZE(md->inv_hwrev)
+ TLVSIZE(md->inv_fwrev)
+ TLVSIZE(md->inv_swrev)
+ TLVSIZE(md->inv_serial)
+ TLVSIZE(md->inv_manufacturer)
+ TLVSIZE(md->inv_modelname)
+ TLVSIZE(md->inv_assetid);
if (!size)
goto out_err;
ptlv = create_ptlv();
if (!ptlv)
goto out_err;
ptlv->tlv = malloc(size);
if (!ptlv->tlv)
goto out_free;
/* Pack all previously-built tlvs, any failure in packing fails all */
ptlv->size = 0;
PACK_TLV_AFTER(md->medcaps, ptlv, size, out_free);
PACK_TLV_AFTER(md->netpoli, ptlv, size, out_free);
PACK_TLV_AFTER(md->locid, ptlv, size, out_free);
PACK_TLV_AFTER(md->extpvm, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_hwrev, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_fwrev, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_swrev, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_serial, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_manufacturer, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_modelname, ptlv, size, out_free);
PACK_TLV_AFTER(md->inv_assetid, ptlv, size, out_free);
return ptlv;
out_free:
free_pkd_tlv(ptlv);
out_err:
LLDPAD_DBG("%s:%s: failed\n", __func__, port->ifname);
return NULL;
}
void med_ifdown(char *ifname, struct lldp_agent *agent)
{
struct med_data *md;
md = med_data(ifname, agent->type);
if (!md)
goto out_err;
LIST_REMOVE(md, entry);
med_free_tlv(md);
free(md);
LLDPAD_INFO("%s:port %s removed\n", __func__, ifname);
return;
out_err:
LLDPAD_INFO("%s:port %s adding failed\n", __func__, ifname);
return;
}
void med_ifup(char *ifname, struct lldp_agent *agent)
{
struct med_data *md;
struct med_user_data *mud;
md = med_data(ifname, agent->type);
if (md) {
LLDPAD_DBG("%s:%s exists\n", __func__, ifname);
goto out_err;
}
/* not found, alloc/init per-port tlv data */
md = (struct med_data *) malloc(sizeof(*md));
if (!md) {
LLDPAD_DBG("%s:%s malloc %zu failed\n",
__func__, ifname, sizeof(*md));
goto out_err;
}
memset(md, 0, sizeof(struct med_data));
STRNCPY_TERMINATED(md->ifname, ifname, IFNAMSIZ);
md->agenttype = agent->type;
if (med_bld_tlv(md, agent)) {
LLDPAD_DBG("%s:%s med_bld_tlv failed\n",
__func__, ifname);
free(md);
goto out_err;
}
mud = find_module_user_data_by_id(&lldp_mod_head, LLDP_MOD_MED);
LIST_INSERT_HEAD(&mud->head, md, entry);
LLDPAD_INFO("%s:port %s added\n", __func__, ifname);
return;
out_err:
LLDPAD_INFO("%s:port %s adding failed\n", __func__, ifname);
return;
}
struct lldp_module *med_register(void)
{
struct lldp_module *mod;
struct med_user_data *mud;
mod = malloc(sizeof(*mod));
if (!mod) {
LLDPAD_ERR("failed to malloc LLDP-MED module data\n");
goto out_err;
}
mud = malloc(sizeof(struct med_user_data));
if (!mud) {
free(mod);
LLDPAD_ERR("failed to malloc LLDP-MED module user data\n");
goto out_err;
}
LIST_INIT(&mud->head);
mod->id = LLDP_MOD_MED;
mod->ops = &med_ops;
mod->data = mud;
LLDPAD_INFO("%s:done\n", __func__);
return mod;
out_err:
LLDPAD_INFO("%s:failed\n", __func__);
return NULL;
}
void med_unregister(struct lldp_module *mod)
{
if (mod->data) {
med_free_data((struct med_user_data *) mod->data);
free(mod->data);
}
free(mod);
LLDPAD_INFO("%s:done\n", __func__);
}