forked from intel/openlldp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldp_util.c
1394 lines (1200 loc) · 29.8 KB
/
lldp_util.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2012 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 <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <netlink/msg.h>
#include <arpa/inet.h>
#include <linux/wireless.h>
#include <linux/sockios.h>
#include <dirent.h>
#include "linux/if_bonding.h"
#include "linux/if_bridge.h"
#include "linux/ethtool.h"
#include "linux/rtnetlink.h"
#include "linux/if_vlan.h"
#include "linux/if.h"
#include "lldp.h"
#include "lldp_util.h"
#include "messages.h"
#include "lldp_dcbx_nl.h"
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static int hex2byte(const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
/**
* bin2hexstr - Convert binary data to ASCII string
* @hex: ASCII hex string (e.g., "01ab")
* @buf: Buffer for the binary data
* @len: Length of the text to convert in bytes (of buf); hex will be double
* this size
* Returns: 0 on success, -1 on failure (invalid hex string)
*/
#define BYTE2CHAR(b) (((b) > 9) ? ((b) - 0xa + 'A') : ((b) + '0'))
int bin2hexstr(const u8 *hex, size_t hexlen, char *buf, size_t buflen)
{
u8 b;
size_t i, j;
for (i = j = 0; (i < hexlen) && (j < buflen); i++, j +=2) {
b = (hex[i] & 0xf0) >> 4;
buf[j] = BYTE2CHAR(b);
b = hex[i] & 0x0f;
buf[j + 1] = BYTE2CHAR(b);
}
return 0;
}
/**
* hexstr2bin - Convert ASCII hex string into binary data
* @hex: ASCII hex string (e.g., "01ab")
* @buf: Buffer for the binary data
* @len: Length of the text to convert in bytes (of buf); hex will be double
* this size
* Returns: 0 on success, -1 on failure (invalid hex string)
*/
int hexstr2bin(const char *hex, u8 *buf, size_t len)
{
size_t i;
int a;
const char *ipos = hex;
u8 *opos = buf;
for (i = 0; i < len; i++) {
a = hex2byte(ipos);
if (a < 0)
return -1;
*opos++ = a;
ipos += 2;
}
return 0;
}
/* assumes input is pointer to two hex digits */
/* returns -1 on error */
int hex2int(char *b)
{
int i;
int n=0;
int m;
for (i=0,m=1; i<2; i++,m--) {
if (isxdigit(*(b+i))) {
if (*(b+i) <= '9')
n |= (*(b+i) & 0x0f) << (4*m);
else
n |= ((*(b+i) & 0x0f) + 9) << (4*m);
}
else {
return -1;
}
}
return n;
}
char *print_mac(char *mac, char *buf)
{
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)*(mac + 0),
(unsigned char)*(mac + 1),
(unsigned char)*(mac + 2),
(unsigned char)*(mac + 3),
(unsigned char)*(mac + 4),
(unsigned char)*(mac + 5));
return buf;
}
static int get_ioctl_socket(void)
{
static int ioctl_socket = -1;
if (ioctl_socket >= 0)
return ioctl_socket;
ioctl_socket = socket(PF_INET, SOCK_DGRAM, 0);
if (ioctl_socket < 0) {
int err = errno;
perror("socket create failed\n");
errno = err;
}
return ioctl_socket;
}
int is_valid_lldp_device(const char *device_name)
{
if (is_loopback(device_name))
return 0;
if (is_vlan(device_name))
return 0;
if (is_bridge(device_name))
return 0;
if (is_macvtap(device_name))
return 0;
return 1;
}
/**
* is_bond - check if interface is a bond interface
* @ifname: name of the interface
*
* Returns 0 if ifname is not a bond, 1 if it is a bond.
*/
int is_bond(const char *ifname)
{
int fd;
int rc = 0;
struct ifreq ifr;
ifbond ifb;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
memset(&ifb, 0, sizeof(ifb));
ifr.ifr_data = (caddr_t)&ifb;
if (ioctl(fd, SIOCBONDINFOQUERY, &ifr) == 0)
rc = 1;
}
return rc;
}
/**
* get_src_mac_from_bond - select a source MAC to use for slave
* @bond_port: pointer to port structure for a bond interface
* @ifname: interface name of the slave port
* @addr: address of buffer in which to return the selected MAC address
*
* Checks to see if ifname is a slave of the bond port. If it is,
* then a
* Returns 0 if a source MAC from the bond could not be found. 1 is
* returned if the slave was found in the bond. addr is updated with
* the source MAC that should be used.
*/
int get_src_mac_from_bond(struct port *bond_port, char *ifname, u8 *addr)
{
int fd;
struct ifreq ifr;
ifbond ifb;
ifslave ifs;
char act_ifname[IFNAMSIZ];
unsigned char bond_mac[ETH_ALEN], san_mac[ETH_ALEN];
int found = 0;
int i;
fd = get_ioctl_socket();
if (fd < 0)
return 0;
memset(bond_mac, 0, sizeof(bond_mac));
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, bond_port->ifname);
memset(&ifb, 0, sizeof(ifb));
ifr.ifr_data = (caddr_t)&ifb;
if (ioctl(fd,SIOCBONDINFOQUERY, &ifr) == 0) {
/* get the MAC address for the current bond port */
if (ioctl(fd,SIOCGIFHWADDR, &ifr) == 0)
memcpy(bond_mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
else
perror("error getting bond MAC address");
/* scan the bond's slave ports and looking for the
* current port and the active slave port.
*/
memset(act_ifname, 0, sizeof(act_ifname));
for (i = 0; i < ifb.num_slaves; i++) {
memset(&ifs, 0, sizeof(ifs));
ifs.slave_id = i;
ifr.ifr_data = (caddr_t)&ifs;
if (ioctl(fd,SIOCBONDSLAVEINFOQUERY, &ifr) == 0) {
if (!strncmp(ifs.slave_name, ifname,
IFNAMSIZ))
found = 1;
if (ifs.state == BOND_STATE_ACTIVE)
strncpy(act_ifname, ifs.slave_name,
IFNAMSIZ);
}
}
}
/* current port is not a slave of the bond */
if (!found)
return 0;
/* Get slave port's current perm MAC address
* This will be the default return value
*/
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
if (ioctl(fd,SIOCGIFHWADDR, &ifr) == 0) {
memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
}
else {
perror("error getting slave MAC address");
return 0;
}
switch (ifb.bond_mode) {
case BOND_MODE_ACTIVEBACKUP:
/* If current port is not the active slave, then
* if the bond MAC is equal to the port's
* permanent MAC, then find and return
* the permanent MAC of the active
* slave port. Otherwise, return the
* permanent MAC of the port.
*/
if (strncmp(ifname, act_ifname, IFNAMSIZ))
if (get_perm_hwaddr(ifname, addr, san_mac) == 0)
if (!memcmp(bond_mac, addr, ETH_ALEN))
get_perm_hwaddr(act_ifname, addr,
san_mac);
break;
default:
/* Use the current MAC of the port */
break;
}
return 1;
}
/*
* Return true if the mac address is valid (non-zero and no hardware
* broadcast address)
*/
int is_valid_mac(const u8 *mac)
{
static const u8 zero_mac[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static const u8 ff_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static const u8 iana_mcast[ETH_ALEN] = {0x01, 0x00, 0x5E};
static const u8 ipv6_mcast[ETH_ALEN] = {0x33, 0x33};
if(memcmp(mac, zero_mac, ETH_ALEN) == 0 ||
memcmp(mac, ff_mac, ETH_ALEN) == 0)
return 0;
/* IANA multicast and ipv6 multicast mac address
* For reference check document:
* https://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml
*/
if(memcmp(mac, iana_mcast, 3) == 0 ||
memcmp(mac, ipv6_mcast, 2) == 0)
return 0;
return 1;
}
int read_int(const char *path)
{
int rc = -1;
char buf[256];
FILE *f = fopen(path, "r");
if (f) {
if (fgets(buf, sizeof(buf), f))
rc = atoi(buf);
fclose(f);
}
return rc;
}
int read_bool(const char *path)
{
return read_int(path) > 0;
}
int get_ifflags(const char *ifname)
{
int fd;
int flags = 0;
struct ifreq ifr;
/* use ioctl */
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
flags = ifr.ifr_flags;
}
return flags;
}
int get_ifname(int ifindex, char *ifname)
{
int fd;
int rc;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
fd = get_ioctl_socket();
if (fd < 0)
return -1;
ifr.ifr_ifindex = ifindex;
rc = ioctl(fd, SIOCGIFNAME, &ifr);
if (rc >= 0)
memcpy(ifname, ifr.ifr_name, IFNAMSIZ);
return rc;
}
int get_ifpflags(const char *ifname)
{
int fd;
int flags = 0;
struct ifreq ifr;
/* use ioctl */
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFPFLAGS, &ifr) == 0)
flags = ifr.ifr_flags;
}
return flags;
}
int get_iftype(const char *ifname)
{
char path[256];
snprintf(path, sizeof(path), "/sys/class/net/%s/type", ifname);
return read_int(path);
}
int get_iffeatures(const char *ifname)
{
char path[256];
snprintf(path, sizeof(path), "/sys/class/net/%s/features", ifname);
return read_int(path);
}
int get_iflink(const char *ifname)
{
char path[256];
snprintf(path, sizeof(path), "/sys/class/net/%s/iflink", ifname);
return read_int(path);
}
int is_ether(const char *ifname)
{
/* check for bridge in sysfs */
int type = get_iftype(ifname);
return (type == ARPHRD_ETHER) || (type == ARPHRD_EETHER);
}
int is_loopback(const char *ifname)
{
return get_ifflags(ifname) & IFF_LOOPBACK;
}
int is_p2p(const char *ifname)
{
return get_ifflags(ifname) & IFF_POINTOPOINT;
}
int is_noarp(const char *ifname)
{
return get_ifflags(ifname) & IFF_NOARP;
}
int is_mbond(const char *ifname)
{
return get_ifflags(ifname) & IFF_MASTER;
}
int is_sbond(const char *ifname)
{
return get_ifflags(ifname) & IFF_SLAVE;
}
int is_slave(const char *ifmaster, const char *ifslave)
{
int i;
int rc = 0;
int fd;
struct ifreq ifr;
struct ifbond ifb;
struct ifslave ifs;
if (!is_mbond(ifmaster))
goto out_done;
fd = get_ioctl_socket();
if (fd < 0)
goto out_done;
memset(&ifr, 0, sizeof(ifr));
memset(&ifb, 0, sizeof(ifb));
STRNCPY_TERMINATED(ifr.ifr_name, ifmaster, IFNAMSIZ);
ifr.ifr_data = (caddr_t)&ifb;
if (ioctl(fd, SIOCBONDINFOQUERY, &ifr))
goto out_done;
for (i = 0; i < ifb.num_slaves; i++) {
memset(&ifs, 0, sizeof(ifs));
ifs.slave_id = i;
ifr.ifr_data = (caddr_t)&ifs;
if (ioctl(fd, SIOCBONDSLAVEINFOQUERY, &ifr) == 0) {
if (!strncmp(ifs.slave_name, ifslave, IFNAMSIZ)) {
rc = 1;
break;
}
}
}
out_done:
return rc;
}
int get_ifidx(const char *ifname)
{
int fd;
int idx = 0;
struct ifreq ifreq;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifreq, 0, sizeof(ifreq));
STRNCPY_TERMINATED(ifreq.ifr_name, ifname, IFNAMSIZ);
if (ioctl(fd, SIOCGIFINDEX, &ifreq) == 0)
idx = ifreq.ifr_ifindex;
}
return idx;
}
int get_master(const char *ifname)
{
int i;
int idx = 0;
int fd;
int cnt;
struct ifreq *ifr = NULL;
struct ifconf ifc;
char ifcbuf[sizeof(struct ifreq) * 32];
/* if it's a master bond, return its own index */
if (is_mbond(ifname))
return get_ifidx(ifname);
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifc, 0, sizeof(ifc));
memset(ifcbuf, 0, sizeof(ifcbuf));
ifc.ifc_buf = ifcbuf;
ifc.ifc_len = sizeof(ifcbuf);
if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) == 0) {
ifr = ifc.ifc_req;
cnt = ifc.ifc_len/sizeof(struct ifreq);
for (i = 0; i < cnt; i++, ifr++) {
if (!strncmp(ifr->ifr_name, ifname, IFNAMSIZ))
continue;
if (!is_mbond(ifr->ifr_name))
continue;
if (!is_slave(ifr->ifr_name, ifname))
continue;
if (ioctl(fd, SIOCGIFINDEX, ifr) == 0)
idx = ifr->ifr_ifindex;
break;
}
}
}
return idx;
}
int is_bridge(const char *ifname)
{
int fd;
int rc = 0;
char path[256];
DIR *dirp;
if (!is_ether(ifname)) {
return 0;
}
/* check for bridge in sysfs */
snprintf(path, sizeof(path), "/sys/class/net/%s/bridge", ifname);
dirp = opendir(path);
if (dirp) {
closedir(dirp);
rc = 1;
} else {
/* use ioctl */
fd = get_ioctl_socket();
if (fd >= 0) {
struct ifreq ifr;
struct __bridge_info bi;
unsigned long args[4] = { BRCTL_GET_BRIDGE_INFO,
(unsigned long) &bi, 0, 0 };
ifr.ifr_data = (char *)args;
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCDEVPRIVATE, &ifr) == 0)
rc = 1;
}
}
return rc;
}
int is_bridge_port(const char *ifname)
{
int rc = 0;
char path[256];
DIR *dirp;
if (!is_ether(ifname)) {
return 0;
}
/* check if the given ifname is a bridge port in sysfs */
snprintf(path, sizeof(path), "/sys/class/net/%s/brport/", ifname);
dirp = opendir(path);
if (dirp) {
closedir(dirp);
rc = 1;
}
return rc;
}
int is_vlan(const char *ifname)
{
int fd;
int rc = 0;
struct vlan_ioctl_args ifv;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifv, 0, sizeof(ifv));
ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
STRNCPY_TERMINATED(ifv.device1, ifname, sizeof(ifv.device1));
if (ioctl(fd, SIOCGIFVLAN, &ifv) == 0)
rc = 1;
}
return rc;
}
int is_vlan_capable(const char *ifname)
{
int features = get_iffeatures(ifname);
#ifndef NETIF_F_VLAN_CHALLENGED
#define NETIF_F_VLAN_CHALLENGED 1024
#endif
return !(features & NETIF_F_VLAN_CHALLENGED);
}
int is_wlan(const char *ifname)
{
int fd;
int rc = 0;
struct iwreq iwreq;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&iwreq, 0, sizeof(iwreq));
STRNCPY_TERMINATED(iwreq.ifr_name, ifname, sizeof(iwreq.ifr_name));
if (ioctl(fd, SIOCGIWNAME, &iwreq) == 0)
rc = 1;
}
return rc;
}
#define NLMSG_SIZE 1024
static struct nla_policy ifla_info_policy[IFLA_INFO_MAX + 1] =
{
[IFLA_INFO_KIND] = { .type = NLA_STRING},
[IFLA_INFO_DATA] = { .type = NLA_NESTED },
[IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING},
[IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
};
int is_macvtap(const char *ifname)
{
int ret, s;
struct nlmsghdr *nlh;
struct ifinfomsg *ifinfo;
struct nlattr *tb[IFLA_MAX+1],
*tb2[IFLA_INFO_MAX+1];
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0) {
return false;
}
nlh = malloc(NLMSG_SIZE);
if (!nlh) {
goto out;
}
memset(nlh, 0, NLMSG_SIZE);
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlh->nlmsg_type = RTM_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST;
ifinfo = NLMSG_DATA(nlh);
ifinfo->ifi_family = AF_UNSPEC;
ifinfo->ifi_index = get_ifidx(ifname);
ret = send(s, nlh, nlh->nlmsg_len, 0);
if (ret < 0) {
goto out_free;
}
memset(nlh, 0, NLMSG_SIZE);
do {
ret = recv(s, (void *) nlh, NLMSG_SIZE, MSG_DONTWAIT);
} while ((ret < 0) && errno == EINTR);
if (nlmsg_parse(nlh, sizeof(struct ifinfomsg),
(struct nlattr **)&tb, IFLA_MAX, NULL)) {
goto out_free;
}
if (tb[IFLA_IFNAME]) {
ifname = (char *)RTA_DATA(tb[IFLA_IFNAME]);
} else {
ifinfo = (struct ifinfomsg *)NLMSG_DATA(nlh);
}
if (tb[IFLA_LINKINFO]) {
if (nla_parse_nested(tb2, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
ifla_info_policy)) {
goto out_free;
}
if (tb2[IFLA_INFO_KIND]) {
char *kind = (char*)(RTA_DATA(tb2[IFLA_INFO_KIND]));
if (!(strcmp("macvtap", kind) && strcmp("macvlan", kind))) {
free(nlh);
close(s);
return true;
}
}
} else {
goto out_free;
}
out_free:
free(nlh);
out:
close(s);
return false;
}
static int is_router(void)
{
int rc = 0;
char path[256];
snprintf(path, sizeof(path), "/proc/sys/net/ipv4/conf/all/forwarding");
rc = read_bool(path);
snprintf(path, sizeof(path), "/proc/sys/net/ipv6/conf/all/forwarding");
rc |= read_bool(path);
return rc;
}
int is_active(const char *ifname)
{
int fd;
int rc = 0;
struct ifreq ifr;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
if (ifr.ifr_flags & IFF_UP)
rc = 1;
}
return rc;
}
int is_autoneg_supported(const char *ifname)
{
int rc = 0;
int fd;
struct ifreq ifr;
struct ethtool_cmd cmd;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = &cmd;
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
if (cmd.supported & SUPPORTED_Autoneg)
rc = 1;
}
return rc;
}
int is_autoneg_enabled(const char *ifname)
{
int rc = 0;
int fd;
struct ifreq ifr;
struct ethtool_cmd cmd;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = &cmd;
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
rc = cmd.autoneg;
}
return rc;
}
/* IETF RFC 3636 dot3MauType: http://www.rfc-editor.org/rfc/rfc3636.txt */
#define MAUCAPADV_bOther (1 << 0) /* other or unknown */
#define MAUCAPADV_b10baseT (1 << 1) /* 10BASE-T half duplex mode */
#define MAUCAPADV_b10baseTFD (1 << 2) /* 10BASE-T full duplex mode */
#define MAUCAPADV_b100baseT4 (1 << 3) /* 100BASE-T4 */
#define MAUCAPADV_b100baseTX (1 << 4) /* 100BASE-TX half duplex mode */
#define MAUCAPADV_b100baseTXFD (1 << 5) /* 100BASE-TX full duplex mode */
#define MAUCAPADV_b100baseT2 (1 << 6) /* 100BASE-T2 half duplex mode */
#define MAUCAPADV_b100baseT2FD (1 << 7) /* 100BASE-T2 full duplex mode */
#define MAUCAPADV_bFdxPause (1 << 8) /* PAUSE for full-duplex links */
#define MAUCAPADV_bFdxAPause (1 << 9) /* Asymmetric PAUSE for full-duplex links */
#define MAUCAPADV_bFdxSPause (1 << 10) /* Symmetric PAUSE for full-duplex links */
#define MAUCAPADV_bFdxBPause (1 << 11) /* Asymmetric and Symmetric PAUSE for full-duplex links */
#define MAUCAPADV_b1000baseX (1 << 12) /* 1000BASE-X, -LX, -SX, -CX half duplex mode */
#define MAUCAPADV_b1000baseXFD (1 << 13) /* 1000BASE-X, -LX, -SX, -CX full duplex mode */
#define MAUCAPADV_b1000baseT (1 << 14) /* 1000BASE-T half duplex mode */
#define MAUCAPADV_b1000baseTFD (1 << 15) /* 1000BASE-T full duplex mode */
int get_maucaps(const char *ifname)
{
int fd;
u16 caps = MAUCAPADV_bOther;
struct ifreq ifr;
struct ethtool_cmd cmd;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = &cmd;
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCETHTOOL, &ifr) == 0) {
if (cmd.advertising & ADVERTISED_10baseT_Half)
caps |= MAUCAPADV_b10baseT;
if (cmd.advertising & ADVERTISED_10baseT_Full)
caps |= MAUCAPADV_b10baseTFD;
if (cmd.advertising & ADVERTISED_100baseT_Half)
caps |= MAUCAPADV_b100baseTX;
if (cmd.advertising & ADVERTISED_100baseT_Full)
caps |= MAUCAPADV_b100baseTXFD;
if (cmd.advertising & ADVERTISED_1000baseT_Half)
caps |= MAUCAPADV_b1000baseT;
if (cmd.advertising & ADVERTISED_1000baseT_Full)
caps |= MAUCAPADV_b1000baseTFD;
if (cmd.advertising & ADVERTISED_Pause)
caps |= (MAUCAPADV_bFdxPause | MAUCAPADV_bFdxSPause);
if (cmd.advertising & ADVERTISED_Asym_Pause)
caps |= MAUCAPADV_bFdxAPause;
if (cmd.advertising & (ADVERTISED_Asym_Pause | ADVERTISED_Pause))
caps |= MAUCAPADV_bFdxBPause;
}
}
return caps;
}
int get_mautype(const char *ifname)
{
int rc = 0;
int fd;
struct ifreq ifr;
struct ethtool_cmd cmd;
u32 speed;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = &cmd;
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCETHTOOL, &ifr) == 0) {
/* TODO: too many dot3MauTypes,
* should check duplex, speed, and port */
speed = (cmd.speed_hi << 16) | cmd.speed;
if (cmd.port == PORT_AUI)
rc = DOT3MAUTYPE_AUI;
else if (speed == SPEED_10)
rc = DOT3MAUTYPE_10BaseT;
else if (speed == SPEED_100)
rc = DOT3MAUTYPE_100BaseTXFD;
else if (speed == SPEED_1000)
rc = DOT3MAUTYPE_1000BaseTFD;
}
}
return rc;
}
int get_mtu(const char *ifname)
{
int fd;
int rc = 0;
struct ifreq ifr;
fd = get_ioctl_socket();
if (fd >= 0) {
memset(&ifr, 0, sizeof(ifr));
STRNCPY_TERMINATED(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFMTU, &ifr) == 0)
rc = ifr.ifr_mtu;
}
return rc;
}
int get_mfs(const char *ifname)
{
int mfs = get_mtu(ifname);
#ifndef VLAN_HLEN
#define VLAN_HLEN 4
#endif
if (mfs) {
mfs += ETH_HLEN + ETH_FCS_LEN;
if (is_vlan_capable(ifname))
mfs += VLAN_HLEN;
}
return mfs;
}
int get_mac(const char *ifname, u8 mac[], bool perm_mac)
{
int ret, s;
struct nlmsghdr *nlh;
struct ifinfomsg *ifinfo;
struct nlattr *tb[IFLA_MAX+1],
*tb2[IFLA_INFO_MAX+1];
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0) {
goto out;
}
nlh = malloc(NLMSG_SIZE);
if (!nlh) {
goto out;
}
memset(nlh, 0, NLMSG_SIZE);
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlh->nlmsg_type = RTM_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST;
ifinfo = NLMSG_DATA(nlh);
ifinfo->ifi_family = AF_UNSPEC;
ifinfo->ifi_index = get_ifidx(ifname);
ret = send(s, nlh, nlh->nlmsg_len, 0);
if (ret < 0) {
goto out_free;
}
memset(nlh, 0, NLMSG_SIZE);
do {
ret = recv(s, (void *) nlh, NLMSG_SIZE, MSG_DONTWAIT);
} while ((ret < 0) && errno == EINTR);
if (nlmsg_parse(nlh, sizeof(struct ifinfomsg),
(struct nlattr **)&tb, IFLA_MAX, NULL)) {
goto out_free;
}
if (tb[IFLA_ADDRESS])
memcpy(mac, (char*)(RTA_DATA(tb[IFLA_ADDRESS])), 6);
/* Check for the permanent mac address on bonding slaves */
if (perm_mac && tb[IFLA_LINKINFO]) {
char *kind;
struct nlattr *tb3;
int off = 0;
if (nla_parse_nested(tb2, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
ifla_info_policy)) {
goto out_free;