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
|
/*
* et driver ioctl swiss army knife command.
*
* Copyright 2007, Broadcom Corporation
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
* the contents of this file may not be disclosed to third parties, copied
* or duplicated in any form, in whole or in part, without the prior
* written permission of Broadcom Corporation.
*
* $Id: et.c,v 1.1.1.1 2008/07/21 09:14:16 james26_jang Exp $
*/
#include <stdio.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/in.h>
#include <typedefs.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <etioctl.h>
#include <proto/ethernet.h>
typedef u_int64_t u64;
typedef u_int32_t u32;
typedef u_int16_t u16;
typedef u_int8_t u8;
#include <linux/sockios.h>
#include <linux/ethtool.h>
static void usage(char *av0);
static void syserr(char *s);
static void et_find(int s, struct ifreq *ifr);
static int et_check(int s, struct ifreq *ifr);
char buf[16 * 1024];
#define VECLEN 2
static int optind;
int
main(int ac, char *av[])
{
char *interface = NULL;
struct ifreq ifr;
char *endptr;
int arg;
int vecarg[VECLEN];
int s;
if (ac < 2)
usage(av[0]);
optind = 1;
if (av[1][0] == '-') {
if ((av[1][1] != 'a') && (av[1][1] != 'i'))
usage(av[0]);
if (ac < 4)
usage(av[0]);
interface = av[2];
optind += 2;
}
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
syserr("socket");
if (interface)
strncpy(ifr.ifr_name, interface, sizeof (ifr.ifr_name));
else
et_find(s, &ifr);
if (!*ifr.ifr_name) {
fprintf(stderr, "et interface not found\n");
exit(1);
}
if (strcmp(av[optind], "up") == 0) {
if (ioctl(s, SIOCSETCUP, (caddr_t)&ifr) < 0)
syserr("etcup");
} else if (strcmp(av[optind], "down") == 0) {
if (ioctl(s, SIOCSETCDOWN, (caddr_t)&ifr) < 0)
syserr("etcdown");
} else if (strcmp(av[optind], "loop") == 0) {
if (optind >= (ac -1))
usage(av[0]);
arg = atoi(av[optind + 1]);
ifr.ifr_data = (caddr_t) &arg;
if (ioctl(s, SIOCSETCLOOP, (caddr_t)&ifr) < 0)
syserr("etcloop");
} else if (strcmp(av[optind], "dump") == 0) {
ifr.ifr_data = buf;
if (ioctl(s, SIOCGETCDUMP, (caddr_t)&ifr) < 0)
syserr("etcdump");
printf("%s\n", buf);
} else if (strcmp(av[optind], "msglevel") == 0) {
if (optind >= (ac -1))
usage(av[0]);
arg = strtol(av[optind + 1], &endptr, 0);
ifr.ifr_data = (caddr_t) &arg;
if (ioctl(s, SIOCSETCSETMSGLEVEL, (caddr_t)&ifr) < 0)
syserr("etcsetmsglevel");
} else if (strcmp(av[optind], "promisc") == 0) {
if (optind >= (ac -1))
usage(av[0]);
arg = atoi(av[optind + 1]);
ifr.ifr_data = (caddr_t) &arg;
if (ioctl(s, SIOCSETCPROMISC, (caddr_t)&ifr) < 0)
syserr("etcpromisc");
} else if (strcmp(av[optind], "qos") == 0) {
if (optind >= (ac -1))
usage(av[0]);
arg = atoi(av[optind + 1]);
ifr.ifr_data = (caddr_t) &arg;
if (ioctl(s, SIOCSETCQOS, (caddr_t)&ifr) < 0)
syserr("etcqos");
} else if (strcmp(av[optind], "txdown") == 0) {
if (ioctl(s, SIOCSETCTXDOWN, (caddr_t)&ifr) < 0)
syserr("etctxdown");
} else if (strcmp(av[optind], "speed") == 0) {
if (optind >= (ac -1))
usage(av[0]);
if (strcmp(av[optind+1], "auto") == 0)
arg = -1;
else if (strcmp(av[optind+1], "10half") == 0)
arg = 0;
else if (strcmp(av[optind+1], "10full") == 0)
arg = 1;
else if (strcmp(av[optind+1], "100half") == 0)
arg = 2;
else if (strcmp(av[optind+1], "100full") == 0)
arg = 3;
else
usage(av[0]);
ifr.ifr_data = (caddr_t) &arg;
if (ioctl(s, SIOCSETCSPEED, (caddr_t)&ifr) < 0)
syserr("etcspeed");
}
else if (strcmp(av[optind], "phyrd") == 0) {
int cmd = -1;
if ((ac < (optind + 2)) || (ac > (optind + 3))) {
usage(av[0]);
} else if (ac == (optind + 3)) {
/* PHY address provided */
vecarg[0] = strtoul(av[optind + 1], NULL, 0) << 16;;
vecarg[0] |= strtoul(av[optind + 2], NULL, 0) & 0xffff;
cmd = SIOCGETCPHYRD2;
} else {
/* "My" PHY address implied */
vecarg[0] = strtoul(av[optind + 1], NULL, 0);
cmd = SIOCGETCPHYRD;
}
ifr.ifr_data = (caddr_t) vecarg;
if (ioctl(s, cmd, (caddr_t)&ifr) < 0)
syserr("etcphyrd");
printf("0x%04x\n", vecarg[1]);
} else if (strcmp(av[optind], "phywr") == 0) {
int cmd = -1;
if ((ac < (optind + 3)) || (ac > (optind + 4))) {
usage(av[0]);
} else if (ac == (optind + 4)) {
vecarg[0] = strtoul(av[optind + 1], NULL, 0) << 16;;
vecarg[0] |= strtoul(av[optind + 2], NULL, 0) & 0xffff;
vecarg[1] = strtoul(av[optind + 3], NULL, 0);
cmd = SIOCSETCPHYWR2;
} else {
vecarg[0] = strtoul(av[optind + 1], NULL, 0);
vecarg[1] = strtoul(av[optind + 2], NULL, 0);
cmd = SIOCSETCPHYWR;
}
ifr.ifr_data = (caddr_t) vecarg;
if (ioctl(s, cmd, (caddr_t)&ifr) < 0)
syserr("etcphywr");
} else if (strcmp(av[optind], "robord") == 0) {
if (ac != (optind + 3))
usage(av[0]);
vecarg[0] = strtoul(av[optind + 1], NULL, 0) << 16;;
vecarg[0] |= strtoul(av[optind + 2], NULL, 0) & 0xffff;
ifr.ifr_data = (caddr_t) vecarg;
if (ioctl(s, SIOCGETCROBORD, (caddr_t)&ifr) < 0)
syserr("etcrobord");
printf("0x%04x\n", vecarg[1]);
} else if (strcmp(av[optind], "robowr") == 0) {
if (ac != (optind + 4))
usage(av[0]);
vecarg[0] = strtoul(av[optind + 1], NULL, 0) << 16;;
vecarg[0] |= strtoul(av[optind + 2], NULL, 0) & 0xffff;
vecarg[1] = strtoul(av[optind + 3], NULL, 0);
ifr.ifr_data = (caddr_t) vecarg;
if (ioctl(s, SIOCSETCROBOWR, (caddr_t)&ifr) < 0)
syserr("etcrobowr");
} else
usage(av[0]);
return (0);
}
static void
usage(char *av0)
{
fprintf(stderr, "usage: %s [ [ -a | -i ] interface ] and one of:\n"
"\tup\n"
"\tdown\n"
"\tloop <0 or 1>\n"
"\tdump\n"
"\tmsglevel <bitvec> (error=1, trace=2, prhdr=4, prpkt=8)\n"
"\tpromisc <0 or 1>\n"
"\tqos <0 or 1>\n"
"\ttxdown\n"
"\tspeed <auto, 10half, 10full, 100half, 100full>\n"
"\tphyrd [<phyaddr>] <reg>\n"
"\tphywr [<phyaddr>] <reg> <val>\n"
"\trobord <page> <reg>\n"
"\trobowr <page> <reg> <val>\n"
,
av0);
exit(1);
}
static void
et_find(int s, struct ifreq *ifr)
{
char proc_net_dev[] = "/proc/net/dev";
FILE *fp;
char buf[512], *c, *name;
ifr->ifr_name[0] = '\0';
/* eat first two lines */
if (!(fp = fopen(proc_net_dev, "r")) ||
!fgets(buf, sizeof(buf), fp) ||
!fgets(buf, sizeof(buf), fp))
return;
while (fgets(buf, sizeof(buf), fp)) {
c = buf;
while (isspace(*c))
c++;
if (!(name = strsep(&c, ":")))
continue;
strncpy(ifr->ifr_name, name, IFNAMSIZ);
if (et_check(s, ifr) == 0)
break;
ifr->ifr_name[0] = '\0';
}
fclose(fp);
}
static int
et_check(int s, struct ifreq *ifr)
{
struct ethtool_drvinfo info;
memset(&info, 0, sizeof(info));
info.cmd = ETHTOOL_GDRVINFO;
ifr->ifr_data = (caddr_t)&info;
if (ioctl(s, SIOCETHTOOL, (caddr_t)ifr) < 0) {
/* print a good diagnostic if not superuser */
if (errno == EPERM)
syserr("siocethtool");
return (-1);
}
if (!strncmp(info.driver, "et", 2))
return (0);
else if (!strncmp(info.driver, "bcm57", 5))
return (0);
return (-1);
}
static void
syserr(char *s)
{
perror(s);
exit(1);
}
|