1 | #include <sys/types.h> |
---|
2 | #include <string.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <sys/types.h> |
---|
5 | #include <sys/ioctl.h> |
---|
6 | #include <sys/stat.h> |
---|
7 | #include <stdio.h> |
---|
8 | #include <string.h> |
---|
9 | #include <errno.h> |
---|
10 | #include <net/if.h> |
---|
11 | |
---|
12 | #include <linux/sockios.h> |
---|
13 | |
---|
14 | #define ETHTOOL_GSET 0x00000001 |
---|
15 | #define ETHTOOL_GWOL 0x00000005 |
---|
16 | #define ETHTOOL_GMSGLVL 0x00000007 |
---|
17 | #define ETHTOOL_GLINK 0x0000000a |
---|
18 | #define SOPASS_MAX 6 |
---|
19 | |
---|
20 | typedef __uint32_t __u32; |
---|
21 | typedef __uint16_t __u16; |
---|
22 | typedef __uint8_t __u8; |
---|
23 | |
---|
24 | static char *devname = NULL; |
---|
25 | |
---|
26 | /* This should work for both 32 and 64 bit userland. */ |
---|
27 | struct ethtool_cmd { |
---|
28 | __u32 cmd; |
---|
29 | __u32 supported; /* Features this interface supports */ |
---|
30 | __u32 advertising; /* Features this interface advertises */ |
---|
31 | __u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */ |
---|
32 | __u8 duplex; /* Duplex, half or full */ |
---|
33 | __u8 port; /* Which connector port */ |
---|
34 | __u8 phy_address; |
---|
35 | __u8 transceiver; /* Which transceiver to use */ |
---|
36 | __u8 autoneg; /* Enable or disable autonegotiation */ |
---|
37 | __u32 maxtxpkt; /* Tx pkts before generating tx int */ |
---|
38 | __u32 maxrxpkt; /* Rx pkts before generating rx int */ |
---|
39 | __u32 reserved[4]; |
---|
40 | }; |
---|
41 | |
---|
42 | /* wake-on-lan settings */ |
---|
43 | struct ethtool_wolinfo { |
---|
44 | __u32 cmd; |
---|
45 | __u32 supported; |
---|
46 | __u32 wolopts; |
---|
47 | __u8 sopass[SOPASS_MAX]; /* SecureOn(tm) password */ |
---|
48 | }; |
---|
49 | |
---|
50 | /* for passing single values */ |
---|
51 | struct ethtool_value { |
---|
52 | __u32 cmd; |
---|
53 | __u32 data; |
---|
54 | }; |
---|
55 | |
---|
56 | static int do_gset(int fd, struct ifreq *ifr) |
---|
57 | { |
---|
58 | int err; |
---|
59 | struct ethtool_cmd ecmd; |
---|
60 | struct ethtool_wolinfo wolinfo; |
---|
61 | struct ethtool_value edata; |
---|
62 | int allfail = 1; |
---|
63 | |
---|
64 | fprintf(stdout, "Settings for %s:\n", devname); |
---|
65 | |
---|
66 | edata.cmd = ETHTOOL_GMSGLVL; |
---|
67 | ifr->ifr_data = (caddr_t)&edata; |
---|
68 | err = ioctl(fd, SIOCETHTOOL, ifr); |
---|
69 | if (err == 0) { |
---|
70 | fprintf(stdout, " Current message level: 0x%08x (%d)\n", |
---|
71 | edata.data, edata.data); |
---|
72 | allfail = 0; |
---|
73 | } else if (errno != EOPNOTSUPP) { |
---|
74 | perror("Cannot get message level"); |
---|
75 | } |
---|
76 | |
---|
77 | edata.cmd = ETHTOOL_GLINK; |
---|
78 | ifr->ifr_data = (caddr_t)&edata; |
---|
79 | err = ioctl(fd, SIOCETHTOOL, ifr); |
---|
80 | if (err == 0) { |
---|
81 | fprintf(stdout, " Link detected: %s\n", |
---|
82 | edata.data ? "yes":"no"); |
---|
83 | allfail = 0; |
---|
84 | } else if (errno != EOPNOTSUPP) { |
---|
85 | perror("Cannot get link status"); |
---|
86 | } |
---|
87 | |
---|
88 | if (allfail) { |
---|
89 | fprintf(stdout, "No data available\n"); |
---|
90 | return 75; |
---|
91 | } |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | main(int argc, char **argv) |
---|
96 | { |
---|
97 | int fd; |
---|
98 | struct ifreq ifr; |
---|
99 | |
---|
100 | if(argc != 2) { |
---|
101 | printf("usage: %s ethdevice\n", argv[0]); |
---|
102 | exit(0); |
---|
103 | } |
---|
104 | |
---|
105 | devname = argv[1]; |
---|
106 | |
---|
107 | /* Setup our control structures. */ |
---|
108 | memset(&ifr, 0, sizeof(ifr)); |
---|
109 | strcpy(ifr.ifr_name, devname); |
---|
110 | |
---|
111 | /* Open control socket. */ |
---|
112 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
---|
113 | if (fd < 0) { |
---|
114 | perror("Cannot get control socket"); |
---|
115 | return 70; |
---|
116 | } |
---|
117 | |
---|
118 | do_gset(fd, &ifr); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|