-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.c
423 lines (355 loc) · 8.07 KB
/
dev.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
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include "dev.h"
#include "str.h"
#include "sock.h"
#include "die.h"
#include "link.h"
#include "built_in.h"
int __device_ifindex(const char *ifname)
{
int ret, sock, index;
struct ifreq ifr;
if (!strncmp("any", ifname, strlen("any")))
return 0;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCGIFINDEX, &ifr);
if (ret)
index = -1;
else
index = ifr.ifr_ifindex;
close(sock);
return index;
}
int device_ifindex(const char *ifname)
{
int index = __device_ifindex(ifname);
if (unlikely(index < 0))
panic("Cannot get ifindex from device!\n");
return index;
}
int device_type(const char *ifname)
{
int ret, sock, type;
struct ifreq ifr;
if (!strncmp("any", ifname, strlen("any")))
return ARPHRD_ETHER;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCGIFHWADDR, &ifr);
if (unlikely(ret))
panic("Cannot get iftype from device!\n");
/* dev->type */
type = ifr.ifr_hwaddr.sa_family;
close(sock);
return type;
}
static int __device_address6(const char *ifname, struct sockaddr_storage *ss)
{
int ret, family, found = -EINVAL;
struct ifaddrs *ifaddr, *ifa;
ret = getifaddrs(&ifaddr);
if (unlikely(ret < 0))
panic("Cannot get device addresses for IPv6!\n");
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family != AF_INET6)
continue;
if (strcmp(ifa->ifa_name, ifname))
continue;
memcpy(ss, ifa->ifa_addr, sizeof(*ss));
found = 0;
break;
}
freeifaddrs(ifaddr);
return found;
}
int device_address(const char *ifname, int af, struct sockaddr_storage *ss)
{
int ret, sock;
struct ifreq ifr;
if (unlikely(!ss))
return -EINVAL;
if (!strncmp("any", ifname, strlen("any")))
return -EINVAL;
if (af == AF_INET6)
return __device_address6(ifname, ss);
sock = af_socket(af);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_addr.sa_family = af;
ret = ioctl(sock, SIOCGIFADDR, &ifr);
if (likely(!ret))
memcpy(ss, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
close(sock);
return ret;
}
int device_hw_address(const char *ifname, uint8_t *addr, size_t len)
{
int ret, sock;
struct ifreq ifr;
if (!addr)
return -EINVAL;
if (len < IFHWADDRLEN)
return -ENOSPC;
if (!strncmp("any", ifname, strlen("any")))
return -EINVAL;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCGIFHWADDR, &ifr);
if (!ret)
memcpy(addr, &ifr.ifr_hwaddr.sa_data[0], IFHWADDRLEN);
close(sock);
return ret;
}
size_t device_mtu(const char *ifname)
{
size_t mtu = 0;
int ret, sock;
struct ifreq ifr;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCGIFMTU, &ifr);
if (likely(!ret))
mtu = ifr.ifr_mtu;
close(sock);
return mtu;
}
short device_get_flags(const char *ifname)
{
short flags = 0;
int ret, sock;
struct ifreq ifr;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
if (likely(!ret))
flags = ifr.ifr_flags;
close(sock);
return flags;
}
void device_set_flags(const char *ifname, const short flags)
{
int ret, sock;
struct ifreq ifr;
sock = af_socket(AF_INET);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_flags = flags;
ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
if (unlikely(ret < 0))
panic("Cannot set NIC flags (%s)!\n", strerror(errno));
close(sock);
}
int device_up_and_running(const char *ifname)
{
if (!ifname)
return -EINVAL;
if (!strncmp("any", ifname, strlen("any")))
return 1;
return (device_get_flags(ifname) &
(IFF_UP | IFF_RUNNING)) ==
(IFF_UP | IFF_RUNNING);
}
uint32_t device_bitrate(const char *ifname)
{
uint32_t scopper;
scopper = ethtool_bitrate(ifname);
return scopper ? : wireless_bitrate(ifname);
}
short device_enter_promiscuous_mode(const char *ifname)
{
short ifflags;
if (!strncmp("any", ifname, strlen("any")))
return 0;
ifflags = device_get_flags(ifname);
device_set_flags(ifname, ifflags | IFF_PROMISC);
return ifflags;
}
void device_leave_promiscuous_mode(const char *ifname, short oldflags)
{
if (!strncmp("any", ifname, strlen("any")))
return;
device_set_flags(ifname, oldflags);
}
const char *device_type2str(uint16_t type)
{
switch (type) {
case ARPHRD_ETHER:
return "ether";
case ARPHRD_EETHER:
return "eether";
case ARPHRD_AX25:
return "ax25";
case ARPHRD_PRONET:
return "pronet";
case ARPHRD_CHAOS:
return "chaos";
case ARPHRD_IEEE802:
return "ieee802";
case ARPHRD_ARCNET:
return "arcnet";
case ARPHRD_APPLETLK:
return "appletlk";
case ARPHRD_DLCI:
return "dlci";
case ARPHRD_ATM:
return "atm";
case ARPHRD_METRICOM:
return "metricom";
case ARPHRD_IEEE1394:
return "ieee1394";
case ARPHRD_INFINIBAND:
return "infiniband";
case ARPHRD_SLIP:
return "slip";
case ARPHRD_CSLIP:
return "cslip";
case ARPHRD_SLIP6:
return "slip6";
case ARPHRD_CSLIP6:
return "cslip6";
case ARPHRD_RSRVD:
return "RSRVD";
case ARPHRD_ADAPT:
return "adapt";
case ARPHRD_ROSE:
return "rose";
case ARPHRD_X25:
return "x25";
case ARPHRD_HWX25:
return "hwx25";
case ARPHRD_CAN:
return "can";
case ARPHRD_PPP:
return "ppp";
case ARPHRD_HDLC:
return "hdlc";
case ARPHRD_LAPB:
return "lapb";
case ARPHRD_DDCMP:
return "ddcmp";
case ARPHRD_RAWHDLC:
return "rawhdlc";
case ARPHRD_TUNNEL:
return "tunnel";
case ARPHRD_TUNNEL6:
return "tunnel6";
case ARPHRD_FRAD:
return "frad";
case ARPHRD_SKIP:
return "skip";
case ARPHRD_LOOPBACK:
return "loopback";
case ARPHRD_LOCALTLK:
return "localtlk";
case ARPHRD_FDDI:
return "fddi";
case ARPHRD_BIF:
return "bif";
case ARPHRD_SIT:
return "sit";
case ARPHRD_IPDDP:
return "ipddp";
case ARPHRD_IPGRE:
return "ipgre";
case ARPHRD_PIMREG:
return "pimreg";
case ARPHRD_HIPPI:
return "hippi";
case ARPHRD_ASH:
return "ash";
case ARPHRD_ECONET:
return "econet";
case ARPHRD_IRDA:
return "irda";
case ARPHRD_FCPP:
return "fcpp";
case ARPHRD_FCAL:
return "fcal";
case ARPHRD_FCPL:
return "fcpl";
case ARPHRD_FCFABRIC:
return "fcfb0";
case ARPHRD_FCFABRIC + 1:
return "fcfb1";
case ARPHRD_FCFABRIC + 2:
return "fcfb2";
case ARPHRD_FCFABRIC + 3:
return "fcfb3";
case ARPHRD_FCFABRIC + 4:
return "fcfb4";
case ARPHRD_FCFABRIC + 5:
return "fcfb5";
case ARPHRD_FCFABRIC + 6:
return "fcfb6";
case ARPHRD_FCFABRIC + 7:
return "fcfb7";
case ARPHRD_FCFABRIC + 8:
return "fcfb8";
case ARPHRD_FCFABRIC + 9:
return "fcfb9";
case ARPHRD_FCFABRIC + 10:
return "fcfb10";
case ARPHRD_FCFABRIC + 11:
return "fcfb11";
case ARPHRD_FCFABRIC + 12:
return "fcfb12";
case ARPHRD_IEEE802_TR:
return "ieee802_tr";
case ARPHRD_IEEE80211:
return "ieee80211";
case ARPHRD_IEEE80211_PRISM:
return "ieee80211_prism";
case ARPHRD_IEEE80211_RADIOTAP:
return "ieee80211_radiotap";
case ARPHRD_IEEE802154:
return "ieee802154";
case ARPHRD_PHONET:
return "phonet";
case ARPHRD_PHONET_PIPE:
return "phonet_pipe";
case ARPHRD_CAIF:
return "caif";
case ARPHRD_IP6GRE:
return "ip6gre";
case ARPHRD_NETLINK:
return "netlink";
case ARPHRD_NONE:
return "none";
case ARPHRD_VOID:
return "void";
default:
return "Unknown";
}
}
/* Taken from iproute2 ll_addr_n2a func */
const char *device_addr2str(const unsigned char *addr, int alen, int type,
char *buf, int blen)
{
int i, l;
if (alen == 4 &&
(type == ARPHRD_TUNNEL || type == ARPHRD_SIT ||
type == ARPHRD_IPGRE))
return inet_ntop(AF_INET, addr, buf, blen);
if (alen == 16 && type == ARPHRD_TUNNEL6)
return inet_ntop(AF_INET6, addr, buf, blen);
snprintf(buf, blen, "%02x", addr[0]);
for (i = 1, l = 2; i < alen && l < blen; i++, l += 3)
snprintf(buf + l, blen - l, ":%02x", addr[i]);
return buf;
}