-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmac.c
460 lines (400 loc) · 10.5 KB
/
pmac.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
/* 1) choose MAC address type
* 2) generate random
* 3) set to specified interface
*
* 1) MAC address type
* - locally administered (default off)
* - same vendor in auto mode (interface specified)
* - specified vendor
* - random known vendor
* - randomize vendor part
* 2) generate random
* 3) print or set to specified interface
*/
#define pmac_version "0.1"
/* source of numbers */
#define random_device "/dev/urandom"
/* length of HW address */
#define addr_len 6
/* length of vendor specific part of HW address */
#define vendor_len 3
/* number of random bytes read too - used internally */
#define extra_bytes 1
/* default choice */
#define default_choice "any"
struct vendor_mac {
char *name;
unsigned char mac[3];
};
/* configuration read from command line */
int print, force, local, multi;
char *vendor, *interface, *manually;
/* buffer for storing random values */
unsigned char buf[addr_len + extra_bytes];
/* AF_INET socket for ioctl() calls.*/
int skfd = -1;
struct vendor_mac vendors[] = {
{ "intel", "\x05\x04\x03" },
};
int read_random_buf(void) {
FILE *random_file = fopen(random_device, "r");
size_t num;
if (!random_file)
return 1;
num = fread(buf, sizeof(char), addr_len, random_file);
if (num < addr_len)
printf("Cannot read %d bytes from %s. Read only %ld bytes.\n", addr_len, random_device, num);
fclose(random_file);
return num = addr_len;
}
void generate_device_part(unsigned char *addr) {
int i;
for (i = vendor_len; i < addr_len; ++i)
addr[i] = buf[i];
}
int init_socket(void) {
if((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return 1;
}
return 0;
}
int get_address_from_interface(char *interface, unsigned char *addr) {
struct ifreq ifr;
strncpy(ifr.ifr_name, interface, strlen(interface) + 1);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) {
printf("Cannot read HW address from interface:\n");
perror("ioctl(SIOCGIFHWADDR)");
return 0;
}
memcpy(addr, ifr.ifr_hwaddr.sa_data, vendor_len);
return 1;
}
int set_address_to_interface(char *interface, unsigned char *addr) {
struct ifreq ifr;
char ifup = 0;
strncpy(ifr.ifr_name, interface, strlen(interface) + 1);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
printf("Cannot read flags from interface:\n");
perror("ioctl(SIOCGIFFLAGS)");
return 0;
}
/* check if interface is up, bring it down when forced */
if (ifr.ifr_flags & IFF_UP) {
if (!force) {
printf("Interface is up, cannot change HW address\n");
return 0;
}
ifup = 1;
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
printf("Cannot set flags on interface:\n");
perror("ioctl(SIOCSIFFLAGS)");
return 0;
}
}
memcpy(&(ifr.ifr_hwaddr.sa_data), addr, sizeof(struct sockaddr));
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
printf("Setting address to interface\n");
if (ioctl(skfd, SIOCSIFHWADDR, &ifr) < 0) {
printf("Cannot assign HW address to interface:\n");
perror("ioctl(SIOCSIFHWADDR)");
return 0;
}
if (ifup) {
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
printf("Cannot read flags from interface:\n");
perror("ioctl(SIOCGIFFLAGS)");
return 0;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
printf("Cannot set flags to interface:\n");
perror("ioctl(SIOCSIFFLAGS)");
return 0;
}
}
return 1;
}
void list_vendors(void) {
int i,j;
for (i = 0; i < sizeof(vendors) / sizeof(struct vendor_mac); ++i) {
printf("%-20s", vendors[i].name);
for (j = 0; j < vendor_len; ++j)
printf("%2.02x:", vendors[i].mac[j]);
printf("...\n");
}
printf(
"%-20sselect any vendor mentioned above\n"
"%-20suse the same vendor as interface specified with -i\n"
"%-20sgenerate random vendor\n",
"any", "same", "random");
}
int select_vendor(unsigned char *addr, char *vendor) {
int i;
int len = strlen(vendor);
int num = sizeof(vendors) / sizeof(struct vendor_mac);
if (len == 0) {
printf("Vendor name is empty.\n");
return 0;
}
if (!strncmp("same", vendor, len))
return get_address_from_interface(interface, addr);
if (!strncmp("random", vendor, len)) {
memcpy(buf, addr, vendor_len);
return 1;
}
if (!strncmp("any", vendor, len)) {
i = buf[addr_len + 1] % num;
}
else {
/* find it */
for (i = 0; i < num; ++i)
if (strcmp(vendors[i].name, vendor) == 0)
break;
if (i == num) {
printf("Vendor '%s' not found.\n", vendor);
return 0;
}
}
memcpy(addr, vendors[i].mac, vendor_len);
return 1;
}
inline int hexa_digit(char digit) {
if ((digit >= '0') && (digit <= '9'))
return digit - '0';
if ((digit >= 'a') && (digit <= 'f'))
return digit - 'a' + 10;
if ((digit >= 'A') && (digit <= 'F'))
return digit - 'A' + 10;
return -1;
}
int parse_manual_address(char *input, unsigned char *addr) {
char *ptr;
char separator = 0;
enum state_type {
first,
second,
separ
} state = first;
int tmp;
if ((strlen(input) != addr_len * 2) && (strlen(input) != addr_len * 3 - 1)) {
printf("Unexpected length of manually set address.\n");
return 0;
}
if ((hexa_digit(input[2]) == -1) || (strlen(input) == addr_len * 3 - 1))
/* there is separator or input is invalid */
separator = input[2];
for (ptr = input; ptr - input < strlen(input); ++ptr) {
tmp = hexa_digit(*ptr);
switch(state) {
case first:
/* we're reading first digit */
if (tmp >= 0)
addr[separator ? (ptr - input)/3 : (ptr - input)/2] = tmp << 4;
else {
printf("Unexpected character '%c' in the positition %ld of MAC address (expected first digit\n", *ptr, ptr - input + 1);
return 0;
}
state = second;
break;
case second:
/* we're reading second digit */
if (tmp >= 0)
addr[separator ? (ptr - input)/3 : (ptr - input)/2] |= tmp;
else {
printf("Unexpected character '%c' in the positition %ld of MAC address (expected second digit)\n", *ptr, ptr - input + 1);
return 0;
}
if (separator)
state = separ;
else
state = first;
break;
case separ:
/* we're reading separator */
if (*ptr != separator) {
printf("Unexpected separator '%c' in the position %ld of MAC address\n", *ptr, ptr - input + 1);
return 0;
}
state = first;
break;
}
}
return 1;
}
void print_address(unsigned char *addr) {
int i;
for (i = 0; i < addr_len - 1; ++i)
printf("%02x:", addr[i]);
printf("%02x\n", addr[addr_len - 1]);
}
void cleanup(void) {
if (vendor)
free(vendor);
if (interface)
free(interface);
if (manually)
free(manually);
if (skfd != -1)
close(skfd);
}
void print_help(void) {
printf(
"pmac v%s - proper MAC generator\n"
"generate random MAC address and set to interface\n"
"\n"
"Usage:\n"
"\tpmac [ -p | --print ] [ -i | --interface int ] [ -v | --vendor name ]\n"
"\t [ -m | --manually addr ] [ -h | --help ]\n"
"\n"
"-p --print instead of setting MAC address just print it\n"
"-i --interface int interface to read MAC address and/or to write MAC to\n"
"-v --vendor name vendor name to be used for vendor part of MAC address\n"
"-m --manually addr do not generate random, manually set address to addr\n"
"-f --force if interface is up, bring it down to change the address\n"
"-l --list list supported vendor names\n"
"-L --local set 'locally administered' bit in result MAC address, default no\n"
"-M --multicast set 'multicast' bit in result MAC address, default no\n"
"-h --help print this screen\n"
"\n", pmac_version);
}
int main(int argc, char *argv[]) {
unsigned char addr[] = { 0, 1, 2, 3, 4, 5 };
int c;
struct option longopts[] = {
{"vendor", 1, 0, 'v'},
{"interface", 1, 0, 'i'},
{"print", 0, 0, 'p'},
{"manually", 1, 0, 'm'},
{"force", 0, 0, 'f'},
{"vendor", 0, 0, 'l'},
{"local", 0, 0, 'L'},
{"multicast", 0, 0, 'M'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0},
};
/* command line parsing */
while (1) {
int idx;
c = getopt_long(argc, argv, "flhpv:i:m:", longopts, &idx);
if (c == -1)
break;
switch(c) {
case 'v':
if(optarg) {
printf("vendor: %s\n", optarg);
vendor = malloc(strlen(optarg) + 1);
strncpy(vendor, optarg, strlen(optarg) + 1);
}
break;
case 'i':
if(optarg) {
printf("interface: %s\n", optarg);
interface = malloc(strlen(optarg) + 1);
strncpy(interface, optarg, strlen(optarg) + 1);
}
break;
case 'p':
print = 1;
break;
case 'f':
force = 1;
break;
case 'L':
local = 1;
break;
case 'M':
multi = 1;
break;
case 'l':
list_vendors();
cleanup();
return 0;
break;
case 'h':
print_help();
cleanup();
return 0;
break;
case 'm':
if(optarg) {
manually = malloc(strlen(optarg) + 1);
printf("manually: %s\n", optarg);
strncpy(manually, optarg, strlen(optarg) + 1);
}
break;
case '?':
cleanup();
return 1;
break;
}
}
/* read all needed random numbers in one step */
if (!read_random_buf()) {
cleanup();
return 1;
}
/* open socket for getting/setting HW address from/to interface */
init_socket();
if (!manually) {
/* if address is not set manually, check for specified vendor */
if (!vendor) {
/* if vendor is not specified, use default */
printf("Vendor not set, using '%s' as default\n", default_choice);
vendor = malloc(strlen(default_choice + 1));
strncpy(vendor, default_choice, strlen(default_choice) + 1);
}
/* prepare vendor part of address */
if (!select_vendor(addr, vendor)) {
cleanup();
return 1;
}
/* prepare device part of address */
generate_device_part(addr);
/* if result address should have 'locally administered' bit set, do so */
if (local)
addr[0] |= 0x2;
else
addr[0] &= ~0x2;
/* if result address should be multicast one, do so */
if (multi)
addr[0] |= 0x1;
else
addr[0] &= ~0x1;
}
else {
/* address set manually - read it */
if (!parse_manual_address(manually, addr))
return 1;
}
if (interface)
/* interface defined */
if (print)
/* but address should be printed */
print_address(addr);
else {
/* set address to interface */
if (!set_address_to_interface(interface, addr)) {
cleanup();
return 1;
}
}
else {
/* interface not set - I can only print result */
if (!print)
printf("Interface not set, printing...\n");
print_address(addr);
}
/* free all buffers and socket */
cleanup();
return EXIT_SUCCESS;
}