forked from jclehner/nmrpflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethsock.c
1225 lines (1027 loc) · 24 KB
/
ethsock.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
/**
* nmrpflash - Netgear Unbrick Utility
* Copyright (C) 2016 Joseph Lehner <[email protected]>
*
* nmrpflash is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nmrpflash is distributed in the hope that 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 nmrpflash. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <sys/types.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "nmrpd.h"
#if defined(NMRPFLASH_WINDOWS)
#include <iphlpapi.h>
#define WPCAP
#include <pcap.h>
#else
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <net/if.h>
#include <pcap.h>
#if defined(NMRPFLASH_LINUX)
#define NMRPFLASH_AF_PACKET AF_PACKET
#include <linux/if_packet.h>
#include <netlink/route/addr.h>
#include <netlink/route/neighbour.h>
#else
#define NMRPFLASH_AF_PACKET AF_LINK
#include <net/if_types.h>
#include <net/if_media.h>
#endif
#endif
struct ethsock
{
const char *intf;
pcap_t *pcap;
#ifndef NMRPFLASH_WINDOWS
int fd;
#ifdef NMRPFLASH_LINUX
bool stp;
// managed by NetworkManager
bool nm_managed;
#endif
#else
HANDLE handle;
DWORD index;
#endif
unsigned timeout;
uint8_t hwaddr[6];
};
struct ethsock_arp_undo
{
uint32_t ipaddr;
uint8_t hwaddr[6];
};
struct ethsock_ip_undo
{
uint32_t ip[2];
};
static int x_pcap_findalldevs(pcap_if_t **devs)
{
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(devs, errbuf) != 0) {
fprintf(stderr, "%s.\n", errbuf);
return -1;
}
return 0;
}
static bool intf_get_pcap_flags(const char *intf, bpf_u_int32 *flags)
{
pcap_if_t *devs, *dev;
if (x_pcap_findalldevs(&devs) == 0) {
for (dev = devs; dev; dev = dev->next) {
if (!strcmp(intf, dev->name)) {
*flags = dev->flags;
break;
}
}
pcap_freealldevs(devs);
return dev != NULL;
}
return false;
}
#ifndef NMRPFLASH_WINDOWS
static int systemf(const char *fmt, ...)
{
char cmd[1024];
int ret;
va_list va;
va_start(va, fmt);
ret = vsnprintf(cmd, sizeof(cmd) - 1, fmt, va);
if (ret >= sizeof(cmd) - 1) {
return -1;
}
ret = system(cmd);
va_end(va);
return ret;
}
#endif
#ifndef NMRPFLASH_WINDOWS
static inline bool sockaddr_get_hwaddr(struct sockaddr *sa, uint8_t *hwaddr)
{
void *src;
if (!sa || sa->sa_family != NMRPFLASH_AF_PACKET) {
return false;
}
#ifndef NMRPFLASH_LINUX
if (((struct sockaddr_dl*)sa)->sdl_type != IFT_ETHER) {
return false;
}
src = LLADDR((struct sockaddr_dl*)sa);
#else
src = ((struct sockaddr_ll*)sa)->sll_addr;
#endif
memcpy(hwaddr, src, 6);
return true;
}
#ifdef NMRPFLASH_LINUX
static int intf_sys_open(const char* intf, const char* file)
{
char name[256];
snprintf(name, sizeof(name), "/sys/class/net/%s/%s", intf, file);
return open(name, O_RDWR, 0644);
}
static bool intf_sys_read(const char* intf, const char* file, bool def)
{
char c;
int fd;
fd = intf_sys_open(intf, file);
if (fd == -1) {
return def;
}
c = 0;
read(fd, &c, 1);
close(fd);
return c ? (c == '1') : def;
}
static bool intf_stp_enable(const char *intf, bool enabled)
{
int fd;
ssize_t n;
fd = intf_sys_open(intf, "bridge/stp_state");
if (fd == -1) {
return false;
}
n = write(fd, enabled ? "1\n" : "0\n", 2);
close(fd);
return n == 2;
}
static struct nl_addr *build_ip(uint32_t ip)
{
struct nl_addr *na = nl_addr_build(AF_INET, &ip, 4);
if (!na) {
xperror("nl_addr_build");
}
return na;
}
static struct nl_sock *xnl_socket_route()
{
int err;
struct nl_sock *sk = nl_socket_alloc();
if (sk) {
if (!(err = nl_connect(sk, NETLINK_ROUTE))) {
return sk;
}
nl_socket_free(sk);
nl_perror(err, "nl_connect");
} else {
xperror("nl_socket_alloc");
}
return NULL;
}
static bool intf_add_del_ip(const char *intf, uint32_t ipaddr, uint32_t ipmask, bool add)
{
struct rtnl_addr *ra = NULL;
struct nl_sock *sk = NULL;
struct nl_addr *laddr = NULL;
struct nl_addr *bcast = NULL;
int err = 1;
if (!(sk = xnl_socket_route())) {
return false;
}
if (!(laddr = build_ip(ipaddr))) {
goto out;
}
nl_addr_set_prefixlen(laddr, bitcount(ipmask));
if (!(bcast = build_ip((ipaddr & ipmask) | ~ipmask))) {
goto out;
}
if (!(ra = rtnl_addr_alloc())) {
xperror("rtnl_addr_alloc");
goto out;
}
rtnl_addr_set_ifindex(ra, if_nametoindex(intf));
rtnl_addr_set_local(ra, laddr);
rtnl_addr_set_broadcast(ra, bcast);
if ((err = ((add ? rtnl_addr_add(sk, ra, 0) : rtnl_addr_delete(sk, ra, 0)) < 0))) {
if (add && err == -NLE_EXIST) {
err = 0;
} else if (add || verbosity > 1) {
nl_perror(err, add ? "rtnl_addr_add" : "rtnl_addr_delete");
}
}
out:
rtnl_addr_put(ra);
nl_addr_put(laddr);
nl_addr_put(bcast);
nl_socket_free(sk);
return !err;
}
static bool intf_add_del_arp(const char *intf, uint32_t ipaddr, uint8_t *hwaddr, bool add)
{
#if 0
struct arpreq arp;
memset(&arp, 0, sizeof(arp));
arp.arp_ha.sa_family = ARPHRD_ETHER;
memcpy(&arp.arp_ha.sa_data, hwaddr, 6);
arp.arp_flags = ATF_PERM | ATF_COM;
struct sockaddr_in *in = (struct sockaddr_in*)&req.arp_pa;
in->sin_addr.s_addr = htonl(ipaddr);
in->sin_family = AF_INET;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return false;
}
bool ret = true;
if (ioctl(fd, add ? SIOCSARP : SIOCDARP, &req) < 0) {
perror(add ? "ioctl(SIOCSARP)" : "ioctl(SIOCDARP");
ret = false;
}
close(fd);
return ret;
#else
struct nl_sock *sk;
struct rtnl_neigh *neigh;
struct nl_addr *mac, *ip;
int err = 1;
sk = NULL;
neigh = NULL;
mac = ip = NULL;
if (!(sk = xnl_socket_route())) {
goto out;
}
if (!(neigh = rtnl_neigh_alloc())) {
xperror("rtnl_neigh_alloc");
goto out;
}
if (!(mac = nl_addr_build(AF_PACKET, hwaddr, 6))) {
xperror("nl_addr_build");
goto out;
}
if (!(ip = nl_addr_build(AF_INET, &ipaddr, 4))) {
xperror("nl_addr_build");
goto out;
}
rtnl_neigh_set_ifindex(neigh, if_nametoindex(intf));
rtnl_neigh_set_dst(neigh, ip);
err = rtnl_neigh_delete(sk, neigh, 0);
if (add) {
rtnl_neigh_set_lladdr(neigh, mac);
rtnl_neigh_set_state(neigh, NUD_PERMANENT);
err = rtnl_neigh_add(sk, neigh, NLM_F_CREATE);
}
if (err && add) {
nl_perror(err, "rtnl_neigh_add");
}
out:
nl_addr_put(ip);
nl_addr_put(mac);
rtnl_neigh_put(neigh);
nl_socket_free(sk);
return !err;
#endif
}
#endif
static bool intf_get_info(const char *intf, uint8_t *hwaddr, bool *bridge)
{
struct ifaddrs *ifas, *ifa;
bool found;
if (getifaddrs(&ifas) != 0) {
xperror("getifaddrs");
return false;
}
found = false;
if (bridge) {
*bridge = false;
}
for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
if (!strcmp(ifa->ifa_name, intf)) {
if (sockaddr_get_hwaddr(ifa->ifa_addr, hwaddr)) {
#ifdef NMRPFLASH_BSD
if (bridge) {
*bridge = ((struct if_data*) ifa->ifa_data)->ifi_type == IFT_BRIDGE;
}
#endif
found = true;
break;
}
}
}
freeifaddrs(ifas);
return found;
}
#else
void win_perror2(const char *msg, DWORD err)
{
char *buf = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buf, 0, NULL);
if (buf) {
/* FormatMessageA terminates buf with CRLF! */
fprintf(stderr, "%s: %s", msg, buf);
LocalFree(buf);
} else {
fprintf(stderr, "%s: error %d\n", msg, (int)err);
}
}
static bool intf_get_if_row(NET_IFINDEX index, MIB_IF_ROW2* row)
{
DWORD err;
memset(row, 0, sizeof(*row));
row->InterfaceIndex = index;
err = GetIfEntry2(row);
if (err != NO_ERROR) {
if (verbosity > 1) {
win_perror2("GetIfEntry2", err);
}
return false;
}
return true;
}
static bool intf_get_info(const char *intf, uint8_t *hwaddr, DWORD *index)
{
PIP_ADAPTER_INFO adapters, adapter;
DWORD ret;
ULONG bufLen = 0;
bool found = false;
if ((ret = GetAdaptersInfo(NULL, &bufLen)) != ERROR_BUFFER_OVERFLOW) {
win_perror2("GetAdaptersInfo", ret);
return false;
}
adapters = malloc(bufLen);
if (!adapters) {
xperror("malloc");
return false;
}
if ((ret = GetAdaptersInfo(adapters, &bufLen) == NO_ERROR)) {
for (adapter = adapters; adapter; adapter = adapter->Next) {
if (adapter->Type != MIB_IF_TYPE_ETHERNET && adapter->Type != IF_TYPE_IEEE80211) {
continue;
}
/* Interface names from WinPcap are "\Device\NPF_{GUID}", while
* AdapterName from GetAdaptersInfo is just "{GUID}".*/
if (strstr(intf, adapter->AdapterName)) {
if (adapter->AddressLength == 6) {
memcpy(hwaddr, adapter->Address, 6);
if (index) {
*index = adapter->Index;
}
found = true;
break;
}
}
}
} else {
win_perror2("GetAdaptersInfo", ret);
}
free(adapters);
return found;
}
static const char *intf_name_to_wpcap(const char *intf)
{
static char buf[128];
if (intf[0] == '\\') {
return intf;
}
do {
NET_IFINDEX index;
DWORD err;
NET_LUID luid;
GUID guid;
if (sscanf(intf, "net%lu", &index) != 1) {
index = if_nametoindex(intf);
if (!index) {
break;
}
}
err = ConvertInterfaceIndexToLuid(index, &luid);
if (err != NO_ERROR) {
if (verbosity) {
win_perror2("ConvertInterfaceIndexToLuid", err);
}
break;
}
err = ConvertInterfaceLuidToGuid(&luid, &guid);
if (err != NO_ERROR) {
if (verbosity) {
win_perror2("ConvertInterfaceLuidToGuid", err);
}
break;
}
snprintf(buf, sizeof(buf),
"\\Device\\NPF_{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2],
guid.Data4[3], guid.Data4[4], guid.Data4[5],
guid.Data4[6], guid.Data4[7]);
return buf;
} while (false);
fprintf(stderr, "Invalid interface name.\n");
return NULL;
}
NET_IFINDEX intf_get_index(const char* intf)
{
const char* p;
GUID guid;
NET_LUID luid;
DWORD err;
NET_IFINDEX ret;
int n;
p = strstr(intf, "NPF_{");
if (!p) {
return 0;
}
sscanf(p + 5,
"%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%n",
&guid.Data1, &guid.Data2, &guid.Data3,
&guid.Data4[0], &guid.Data4[1], &guid.Data4[2],
&guid.Data4[3], &guid.Data4[4], &guid.Data4[5],
&guid.Data4[6], &guid.Data4[7], &n);
if (n != 36) {
return 0;
}
err = ConvertInterfaceGuidToLuid(&guid, &luid);
if (err) {
win_perror2("ConvertInterfaceGuidToLuid", err);
return 0;
}
err = ConvertInterfaceLuidToIndex(&luid, &ret);
if (err) {
win_perror2("ConvertInterfaceLuidToIndex", err);
return 0;
}
return ret;
}
#endif
inline uint8_t *ethsock_get_hwaddr(struct ethsock *sock)
{
return sock->hwaddr;
}
bool ethsock_is_wifi(struct ethsock *sock)
{
bpf_u_int32 flags;
if (!intf_get_pcap_flags(sock->intf, &flags)) {
return false;
}
return flags & PCAP_IF_WIRELESS;
}
bool ethsock_is_unplugged(struct ethsock *sock)
{
bpf_u_int32 flags;
if (!intf_get_pcap_flags(sock->intf, &flags)) {
return false;
}
return flags & PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
}
struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
{
char buf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
struct ethsock *sock;
bool is_bridge = false;
int err;
#ifdef NMRPFLASH_WINDOWS
intf = intf_name_to_wpcap(intf);
if (!intf) {
return NULL;
}
#endif
sock = malloc(sizeof(struct ethsock));
if (!sock) {
xperror("malloc");
return NULL;
}
buf[0] = '\0';
sock->intf = intf;
sock->pcap = pcap_open_live(sock->intf, BUFSIZ, 1, 1, buf);
if (!sock->pcap) {
fprintf(stderr, "%s.\n", buf);
goto cleanup;
}
if (*buf) {
fprintf(stderr, "Warning: %s.\n", buf);
}
if (pcap_datalink(sock->pcap) != DLT_EN10MB) {
fprintf(stderr, "%s is not an ethernet interface.\n",
intf);
goto cleanup;
}
#ifndef NMRPFLASH_WINDOWS
err = !intf_get_info(intf, sock->hwaddr, &is_bridge);
#else
err = !intf_get_info(intf, sock->hwaddr, &sock->index);
#endif
if (err) {
fprintf(stderr, "Failed to get interface info.\n");
goto cleanup;
}
#ifndef NMRPFLASH_WINDOWS
sock->fd = pcap_get_selectable_fd(sock->pcap);
if (sock->fd == -1) {
pcap_perror(sock->pcap, "pcap_get_selectable_fd");
goto cleanup;
}
#else
sock->handle = pcap_getevent(sock->pcap);
if (!sock->handle) {
pcap_perror(sock->pcap, "pcap_getevent");
goto cleanup;
}
err = pcap_setmintocopy(sock->pcap, 1);
if (err) {
pcap_perror(sock->pcap, "pcap_setmintocopy");
goto cleanup;
}
#endif
snprintf(buf, sizeof(buf), "ether proto 0x%04x and not ether src %s",
protocol, mac_to_str(sock->hwaddr));
err = pcap_compile(sock->pcap, &fp, buf, 0, 0);
if (err) {
pcap_perror(sock->pcap, "pcap_compile");
goto cleanup;
}
err = pcap_setfilter(sock->pcap, &fp);
pcap_freecode(&fp);
if (err) {
pcap_perror(sock->pcap, "pcap_setfilter");
goto cleanup;
}
#ifdef NMRPFLASH_LINUX
// nmrpflash does not work on bridge interfaces with STP enabled
if ((sock->stp = intf_sys_read(intf, "bridge/stp_state", false))) {
if (!intf_stp_enable(intf, false)) {
fprintf(stderr, "Warning: failed to disable STP on %s.\n", intf);
}
}
err = system("nmcli -v > /dev/null");
if (!err) {
err = systemf("nmcli -f GENERAL.STATE device show %s | grep -q unmanaged", sock->intf);
if (!err) {
sock->nm_managed = false;
} else {
sock->nm_managed = true;
err = systemf("nmcli device set ifname %s managed no", sock->intf);
if (err) {
printf("Warning: failed to temporarily disable NetworkManager\n");
} else if (verbosity > 1) {
printf("Temporarily disabling NetworkManager on interface.\n");
}
}
} else {
sock->nm_managed = false;
}
#else
if (is_bridge) {
fprintf(stderr, "Warning: bridge interfaces are not fully "
"supported on this platform.\n");
}
#endif
return sock;
cleanup:
ethsock_close(sock);
return NULL;
}
ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len)
{
struct pcap_pkthdr* hdr;
const u_char *capbuf;
int status;
#ifdef NMRPFLASH_WINDOWS
DWORD ret;
if (sock->timeout) {
ret = WaitForSingleObject(sock->handle, sock->timeout);
if (ret == WAIT_TIMEOUT) {
return 0;
} else if (ret != WAIT_OBJECT_0) {
win_perror2("WaitForSingleObject", ret);
return -1;
}
}
#else
if (sock->timeout) {
status = select_fd(sock->fd, sock->timeout);
if (status < 0) {
return -1;
} else if (status == 0) {
return 0;
}
}
#endif
status = pcap_next_ex(sock->pcap, &hdr, &capbuf);
switch (status) {
case 1:
memcpy(buf, capbuf, MIN(len, hdr->caplen));
return hdr->caplen;
case 0:
return 0;
case -1:
pcap_perror(sock->pcap, "pcap_next_ex");
return -1;
default:
fprintf(stderr, "pcap_next_ex: returned %d.\n", status);
return -1;
}
}
int ethsock_send(struct ethsock *sock, void *buf, size_t len)
{
#ifdef NMRPFLASH_WINDOWS
if (pcap_sendpacket(sock->pcap, buf, len) == 0) {
return 0;
} else {
pcap_perror(sock->pcap, "pcap_sendpacket");
return -1;
}
#else
if (pcap_inject(sock->pcap, buf, len) == len) {
return 0;
} else {
pcap_perror(sock->pcap, "pcap_inject");
return -1;
}
#endif
}
int ethsock_close(struct ethsock *sock)
{
if (!sock) {
return 0;
}
#ifdef NMRPFLASH_LINUX
if (sock->stp) {
intf_stp_enable(sock->intf, true);
}
if (sock->nm_managed) {
systemf("nmcli device set ifname %s managed yes", sock->intf);
}
#endif
if (sock->pcap) {
pcap_close(sock->pcap);
}
free(sock);
return 0;
}
inline int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
{
sock->timeout = msec;
return 0;
}
static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
{
#if defined(NMRPFLASH_UNIX) && !defined(NMRPFLASH_LINUX)
struct in_addr addr = { .s_addr = ipaddr };
#elif defined(NMRPFLASH_WINDOWS)
DWORD err;
MIB_IPNETROW arp = {
.dwIndex = sock->index,
.dwPhysAddrLen = 6,
.dwAddr = ipaddr,
.dwType = MIB_IPNET_TYPE_STATIC
};
memcpy(arp.bPhysAddr, hwaddr, 6);
#endif
if (undo) {
#if defined(NMRPFLASH_LINUX)
if (!intf_add_del_arp(sock->intf, ipaddr, hwaddr, true)) {
return -1;
}
#elif defined(NMRPFLASH_WINDOWS)
err = CreateIpNetEntry(&arp);
if (err != NO_ERROR) {
win_perror2("CreateIpNetEntry", err);
return -1;
}
#else
if (systemf("arp -s %s %s", inet_ntoa(addr), mac_to_str(hwaddr)) != 0) {
return -1;
}
#endif
*undo = malloc(sizeof(struct ethsock_arp_undo));
if (!*undo) {
xperror("malloc");
return -1;
}
(*undo)->ipaddr = ipaddr;
memcpy((*undo)->hwaddr, hwaddr, 6);
} else {
#if defined(NMRPFLASH_LINUX)
if (!intf_add_del_arp(sock->intf, ipaddr, hwaddr, false)) {
return -1;
}
#elif defined(NMRPFLASH_WINDOWS)
return DeleteIpNetEntry(&arp) ? 0 : -1;
#else
return systemf("arp -d %s", inet_ntoa(addr));
#endif
}
return 0;
}
int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
{
// remove any previous ARP entry
ethsock_arp(sock, hwaddr, ipaddr, NULL);
// add the new ARP entry
return undo ? ethsock_arp(sock, hwaddr, ipaddr, undo) : -1;
}
int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo)
{
if (!*undo) {
return 0;
}
int ret = ethsock_arp(sock, (*undo)->hwaddr, (*undo)->ipaddr, NULL);
free(*undo);
*undo = NULL;
return ret;
}
static bool get_hwaddr_from_pcap(const pcap_if_t *dev, uint8_t *hwaddr)
{
#ifndef NMRPFLASH_WINDOWS
pcap_addr_t *addr;
int i;
for (addr = dev->addresses; addr; addr = addr->next) {
if (verbosity > 1) {
printf("%s: sa_family=%d, sa_data={ ", dev->name,
addr->addr->sa_family);
for (i = 0; i != sizeof(addr->addr->sa_data); ++i) {
printf("%02x ", addr->addr->sa_data[i] & 0xff);
}
printf("}\n");
}
if (sockaddr_get_hwaddr(addr->addr, hwaddr)) {
return true;
}
}
#endif
return intf_get_info(dev->name, hwaddr, NULL);
}
int ethsock_list_all(void)
{
pcap_if_t *devs, *dev;
pcap_addr_t *addr;
uint8_t hwaddr[6];
unsigned dev_num = 0, dev_ok = 0;
#ifdef NMRPFLASH_WINDOWS
wchar_t *pretty = NULL;
NET_IFINDEX index;
MIB_IF_ROW2 row;
#endif
if (x_pcap_findalldevs(&devs) != 0) {
return -1;
}
memset(hwaddr, 0, 6);
for (dev = devs; dev; dev = dev->next, ++dev_num) {
if (dev->flags & PCAP_IF_LOOPBACK) {
if (verbosity) {
printf("%-15s (loopback device)\n", dev->name);
}
continue;
}
if (!get_hwaddr_from_pcap(dev, hwaddr)) {
if (verbosity) {
printf("%-15s (not an ethernet device)\n",
dev->name);
}
continue;
}
#ifndef NMRPFLASH_WINDOWS
printf("%-15s", dev->name);
#else
index = intf_get_index(dev->name);
if (intf_get_if_row(index, &row)) {
if (!row.InterfaceAndOperStatusFlags.HardwareInterface) {
if (verbosity) {
printf("%-15s (virtual interface)\n", dev->name);
}
continue;
}
if (row.Alias[0]) {
pretty = row.Alias;
}
}
if (!verbosity && index) {
printf("net%-2lu", index);
} else {
printf("%-15s", dev->name);
}
#endif
for (addr = dev->addresses; addr; addr = addr->next) {
if (addr->addr->sa_family == AF_INET) {
printf(" %-15s",
inet_ntoa(((struct sockaddr_in*)addr->addr)->sin_addr));
break;
}
}
if (!addr) {
printf(" %-15s", "0.0.0.0");
}
printf(" %s", mac_to_str(hwaddr));
#ifdef NMRPFLASH_WINDOWS
if (pretty) {
printf(" (%ls)", pretty);
} else if (dev->description) {
printf(" (%s)", dev->description);
}
#endif
printf("\n");
++dev_ok;
}
if (!dev_ok) {
printf("No suitable network interfaces found.\n");
}
return 0;
}