ethsock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <sys/types.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include "nmrpd.h"
  26. #if defined(NMRPFLASH_WINDOWS)
  27. #define NMRPFLASH_NETALIAS_PREFIX "net"
  28. #define WPCAP
  29. #include <pcap.h>
  30. #else
  31. #include <sys/ioctl.h>
  32. #include <ifaddrs.h>
  33. #include <unistd.h>
  34. #include <net/if.h>
  35. #include <pcap.h>
  36. #if defined(NMRPFLASH_LINUX)
  37. #define NMRPFLASH_AF_PACKET AF_PACKET
  38. #include <linux/if_packet.h>
  39. #else
  40. #define NMRPFLASH_AF_PACKET AF_LINK
  41. #include <net/if_types.h>
  42. #include <net/if_media.h>
  43. #endif
  44. #endif
  45. struct ethsock
  46. {
  47. const char *intf;
  48. pcap_t *pcap;
  49. #ifndef NMRPFLASH_WINDOWS
  50. int fd;
  51. #ifdef NMRPFLASH_LINUX
  52. bool stp;
  53. #endif
  54. #else
  55. HANDLE handle;
  56. DWORD index;
  57. #endif
  58. unsigned timeout;
  59. uint8_t hwaddr[6];
  60. };
  61. struct ethsock_arp_undo
  62. {
  63. uint32_t ipaddr;
  64. uint8_t hwaddr[6];
  65. };
  66. struct ethsock_ip_undo
  67. {
  68. #ifndef NMRPFLASH_WINDOWS
  69. uint32_t ip[2];
  70. #else
  71. ULONG context;
  72. #endif
  73. };
  74. const char *mac_to_str(uint8_t *mac)
  75. {
  76. static char buf[18];
  77. snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
  78. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  79. return buf;
  80. }
  81. static int x_pcap_findalldevs(pcap_if_t **devs)
  82. {
  83. char errbuf[PCAP_ERRBUF_SIZE];
  84. if (pcap_findalldevs(devs, errbuf) != 0) {
  85. fprintf(stderr, "%s.\n", errbuf);
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. #ifndef NMRPFLASH_WINDOWS
  91. static inline bool sockaddr_get_hwaddr(struct sockaddr *sa, uint8_t *hwaddr)
  92. {
  93. void *src;
  94. if (sa->sa_family != NMRPFLASH_AF_PACKET) {
  95. return false;
  96. }
  97. #ifndef NMRPFLASH_LINUX
  98. if (((struct sockaddr_dl*)sa)->sdl_type != IFT_ETHER) {
  99. return false;
  100. }
  101. src = LLADDR((struct sockaddr_dl*)sa);
  102. #else
  103. src = ((struct sockaddr_ll*)sa)->sll_addr;
  104. #endif
  105. memcpy(hwaddr, src, 6);
  106. return true;
  107. }
  108. #ifdef NMRPFLASH_LINUX
  109. static int bridge_stp_state(const char *intf)
  110. {
  111. char name[256];
  112. snprintf(name, sizeof(name), "/sys/class/net/%s/bridge/stp_state", intf);
  113. return open(name, O_RDWR, 0644);
  114. }
  115. static bool bridge_stp_enabled(const char *intf)
  116. {
  117. char c;
  118. int fd = bridge_stp_state(intf);
  119. if (fd == -1) {
  120. return false;
  121. }
  122. if (read(fd, &c, 1) != 1) {
  123. c = '0';
  124. }
  125. close(fd);
  126. return c == '1';
  127. }
  128. static bool bridge_stp(const char *intf, bool enabled)
  129. {
  130. bool ret;
  131. const char *s = enabled ? "1\n" : "0\n";
  132. int fd = bridge_stp_state(intf);
  133. if (fd == -1) {
  134. return false;
  135. }
  136. ret = (write(fd, s, 2) == 2);
  137. close(fd);
  138. return ret;
  139. }
  140. #endif
  141. static bool intf_get_info(const char *intf, uint8_t *hwaddr, bool *bridge)
  142. {
  143. struct ifaddrs *ifas, *ifa;
  144. bool found;
  145. if (getifaddrs(&ifas) != 0) {
  146. xperror("getifaddrs");
  147. return false;
  148. }
  149. found = false;
  150. if (bridge) {
  151. *bridge = false;
  152. }
  153. for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
  154. if (!strcmp(ifa->ifa_name, intf)) {
  155. if (sockaddr_get_hwaddr(ifa->ifa_addr, hwaddr)) {
  156. #ifdef NMRPFLASH_BSD
  157. if (bridge) {
  158. *bridge = ((struct if_data*) ifa->ifa_data)->ifi_type == IFT_BRIDGE;
  159. }
  160. #endif
  161. found = true;
  162. break;
  163. }
  164. }
  165. }
  166. freeifaddrs(ifas);
  167. return found;
  168. }
  169. #else
  170. void win_perror2(const char *msg, DWORD err)
  171. {
  172. char *buf = NULL;
  173. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  174. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  175. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  176. (LPTSTR)&buf, 0, NULL);
  177. if (buf) {
  178. /* FormatMessageA terminates buf with CRLF! */
  179. fprintf(stderr, "%s: %s", msg, buf);
  180. LocalFree(buf);
  181. } else {
  182. fprintf(stderr, "%s: error %d\n", msg, (int)err);
  183. }
  184. }
  185. static bool intf_get_info(const char *intf, uint8_t *hwaddr, DWORD *index)
  186. {
  187. PIP_ADAPTER_INFO adapters, adapter;
  188. DWORD ret;
  189. ULONG i, bufLen = 0;
  190. bool found = false;
  191. if ((ret = GetAdaptersInfo(NULL, &bufLen)) != ERROR_BUFFER_OVERFLOW) {
  192. win_perror2("GetAdaptersInfo", ret);
  193. return false;
  194. }
  195. adapters = malloc(bufLen);
  196. if (!adapters) {
  197. xperror("malloc");
  198. return false;
  199. }
  200. if ((ret = GetAdaptersInfo(adapters, &bufLen) == NO_ERROR)) {
  201. for (adapter = adapters; adapter; adapter = adapter->Next) {
  202. if (adapter->Type != MIB_IF_TYPE_ETHERNET && adapter->Type != IF_TYPE_IEEE80211) {
  203. continue;
  204. }
  205. /* Interface names from WinPcap are "\Device\NPF_{GUID}", while
  206. * AdapterName from GetAdaptersInfo is just "{GUID}".*/
  207. if (strstr(intf, adapter->AdapterName)) {
  208. if (adapter->AddressLength == 6) {
  209. memcpy(hwaddr, adapter->Address, 6);
  210. if (index) {
  211. *index = adapter->Index;
  212. }
  213. found = true;
  214. break;
  215. }
  216. }
  217. }
  218. } else {
  219. win_perror2("GetAdaptersInfo", ret);
  220. }
  221. free(adapters);
  222. return found;
  223. }
  224. static const char *intf_alias_to_wpcap(const char *intf)
  225. {
  226. static char buf[128];
  227. pcap_if_t *devs, *dev;
  228. unsigned i = 0, dev_num = 0;
  229. if (intf[0] == '\\') {
  230. return intf;
  231. } else if (sscanf(intf, NMRPFLASH_NETALIAS_PREFIX "%u", &dev_num) != 1) {
  232. fprintf(stderr, "Invalid interface alias.\n");
  233. return NULL;
  234. }
  235. if (x_pcap_findalldevs(&devs) != 0) {
  236. return NULL;
  237. }
  238. for (dev = devs; dev; dev = dev->next, ++i) {
  239. if (i == dev_num) {
  240. if (verbosity) {
  241. printf("%s%u: %s\n", NMRPFLASH_NETALIAS_PREFIX, i, dev->name);
  242. }
  243. strncpy(buf, dev->name, sizeof(buf) - 1);
  244. buf[sizeof(buf) - 1] = '\0';
  245. break;
  246. }
  247. }
  248. pcap_freealldevs(devs);
  249. if (!dev) {
  250. fprintf(stderr, "Interface alias not found.\n");
  251. return NULL;
  252. }
  253. return buf;
  254. }
  255. static const char *intf_get_pretty_name(const char *intf)
  256. {
  257. static char buf[512];
  258. char *guid;
  259. HKEY hkey;
  260. LONG err;
  261. DWORD len;
  262. guid = strstr(intf, "NPF_{");
  263. if (!guid) {
  264. return NULL;
  265. }
  266. guid += 4;
  267. snprintf(buf, sizeof(buf),
  268. "System\\CurrentControlSet\\Control\\Network\\"
  269. "{4D36E972-E325-11CE-BFC1-08002BE10318}\\"
  270. "%s\\Connection", guid);
  271. err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, buf, 0, KEY_READ, &hkey);
  272. if (err != ERROR_SUCCESS) {
  273. if (verbosity > 1) {
  274. win_perror2("RegOpenKeyExA", err);
  275. }
  276. return NULL;
  277. }
  278. len = sizeof(buf);
  279. err = RegQueryValueExA(hkey, "Name", NULL, NULL, (LPBYTE)buf, &len);
  280. if (err == ERROR_SUCCESS) {
  281. intf = buf;
  282. } else {
  283. if (verbosity > 1) {
  284. win_perror2("RegQueryValueExA", err);
  285. }
  286. intf = NULL;
  287. }
  288. RegCloseKey(hkey);
  289. return intf;
  290. }
  291. #endif
  292. inline uint8_t *ethsock_get_hwaddr(struct ethsock *sock)
  293. {
  294. return sock->hwaddr;
  295. }
  296. struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
  297. {
  298. char buf[PCAP_ERRBUF_SIZE];
  299. struct bpf_program fp;
  300. struct ethsock *sock;
  301. bool is_bridge;
  302. int err;
  303. #ifdef NMRPFLASH_WINDOWS
  304. intf = intf_alias_to_wpcap(intf);
  305. if (!intf) {
  306. return NULL;
  307. }
  308. #endif
  309. sock = malloc(sizeof(struct ethsock));
  310. if (!sock) {
  311. xperror("malloc");
  312. return NULL;
  313. }
  314. buf[0] = '\0';
  315. sock->intf = intf;
  316. sock->pcap = pcap_open_live(sock->intf, BUFSIZ, 1, 1, buf);
  317. if (!sock->pcap) {
  318. fprintf(stderr, "%s.\n", buf);
  319. goto cleanup;
  320. }
  321. if (*buf) {
  322. fprintf(stderr, "Warning: %s.\n", buf);
  323. }
  324. if (pcap_datalink(sock->pcap) != DLT_EN10MB) {
  325. fprintf(stderr, "%s is not an ethernet interface.\n",
  326. intf);
  327. goto cleanup;
  328. }
  329. #ifndef NMRPFLASH_WINDOWS
  330. err = !intf_get_info(intf, sock->hwaddr, &is_bridge);
  331. #else
  332. err = !intf_get_info(intf, sock->hwaddr, &sock->index);
  333. #endif
  334. if (err) {
  335. fprintf(stderr, "Failed to get interface info.\n");
  336. goto cleanup;
  337. }
  338. #ifndef NMRPFLASH_WINDOWS
  339. sock->fd = pcap_get_selectable_fd(sock->pcap);
  340. if (sock->fd == -1) {
  341. pcap_perror(sock->pcap, "pcap_get_selectable_fd");
  342. goto cleanup;
  343. }
  344. #else
  345. sock->handle = pcap_getevent(sock->pcap);
  346. if (!sock->handle) {
  347. pcap_perror(sock->pcap, "pcap_getevent");
  348. goto cleanup;
  349. }
  350. err = pcap_setmintocopy(sock->pcap, 1);
  351. if (err) {
  352. pcap_perror(sock->pcap, "pcap_setmintocopy");
  353. goto cleanup;
  354. }
  355. #endif
  356. snprintf(buf, sizeof(buf), "ether proto 0x%04x and not ether src %s",
  357. protocol, mac_to_str(sock->hwaddr));
  358. err = pcap_compile(sock->pcap, &fp, buf, 0, 0);
  359. if (err) {
  360. pcap_perror(sock->pcap, "pcap_compile");
  361. goto cleanup;
  362. }
  363. err = pcap_setfilter(sock->pcap, &fp);
  364. pcap_freecode(&fp);
  365. if (err) {
  366. pcap_perror(sock->pcap, "pcap_setfilter");
  367. goto cleanup;
  368. }
  369. #ifdef NMRPFLASH_LINUX
  370. // nmrpflash does not work on bridge interfaces with STP enabled
  371. if ((sock->stp = bridge_stp_enabled(intf))) {
  372. if (!bridge_stp(intf, false)) {
  373. fprintf(stderr, "Warning: failed to disable STP on %s.\n", intf);
  374. }
  375. }
  376. #else
  377. if (is_bridge) {
  378. fprintf(stderr, "Warning: bridge interfaces are not fully "
  379. "supported on this platform.\n");
  380. }
  381. #endif
  382. return sock;
  383. cleanup:
  384. ethsock_close(sock);
  385. return NULL;
  386. }
  387. int select_fd(int fd, unsigned timeout)
  388. {
  389. struct timeval tv;
  390. int status;
  391. fd_set fds;
  392. FD_ZERO(&fds);
  393. FD_SET(fd, &fds);
  394. tv.tv_sec = timeout / 1000;
  395. tv.tv_usec = 1000 * (timeout % 1000);
  396. status = select(fd + 1, &fds, NULL, NULL, &tv);
  397. if (status < 0) {
  398. sock_perror("select");
  399. }
  400. return status;
  401. }
  402. ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len)
  403. {
  404. struct pcap_pkthdr* hdr;
  405. const u_char *capbuf;
  406. int status;
  407. #ifdef NMRPFLASH_WINDOWS
  408. DWORD ret;
  409. if (sock->timeout) {
  410. ret = WaitForSingleObject(sock->handle, sock->timeout);
  411. if (ret == WAIT_TIMEOUT) {
  412. return 0;
  413. } else if (ret != WAIT_OBJECT_0) {
  414. win_perror2("WaitForSingleObject", ret);
  415. return -1;
  416. }
  417. }
  418. #else
  419. if (sock->timeout) {
  420. status = select_fd(sock->fd, sock->timeout);
  421. if (status < 0) {
  422. return -1;
  423. } else if (status == 0) {
  424. return 0;
  425. }
  426. }
  427. #endif
  428. status = pcap_next_ex(sock->pcap, &hdr, &capbuf);
  429. switch (status) {
  430. case 1:
  431. memcpy(buf, capbuf, MIN(len, hdr->caplen));
  432. return hdr->caplen;
  433. case 0:
  434. return 0;
  435. case -1:
  436. pcap_perror(sock->pcap, "pcap_next_ex");
  437. return -1;
  438. default:
  439. fprintf(stderr, "pcap_next_ex: returned %d.\n", status);
  440. return -1;
  441. }
  442. }
  443. int ethsock_send(struct ethsock *sock, void *buf, size_t len)
  444. {
  445. #ifdef NMRPFLASH_WINDOWS
  446. if (pcap_sendpacket(sock->pcap, buf, len) == 0) {
  447. return 0;
  448. } else {
  449. pcap_perror(sock->pcap, "pcap_sendpacket");
  450. return -1;
  451. }
  452. #else
  453. if (pcap_inject(sock->pcap, buf, len) == len) {
  454. return 0;
  455. } else {
  456. pcap_perror(sock->pcap, "pcap_inject");
  457. return -1;
  458. }
  459. #endif
  460. }
  461. int ethsock_close(struct ethsock *sock)
  462. {
  463. if (!sock) {
  464. return 0;
  465. }
  466. #ifdef NMRPFLASH_LINUX
  467. if (sock->stp) {
  468. bridge_stp(sock->intf, true);
  469. }
  470. #endif
  471. if (sock->pcap) {
  472. pcap_close(sock->pcap);
  473. }
  474. free(sock);
  475. return 0;
  476. }
  477. inline int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
  478. {
  479. sock->timeout = msec;
  480. return 0;
  481. }
  482. #ifndef NMRPFLASH_WINDOWS
  483. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
  484. {
  485. return 0;
  486. }
  487. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo)
  488. {
  489. return 0;
  490. }
  491. #else
  492. static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
  493. {
  494. DWORD ret;
  495. MIB_IPNETROW arp = {
  496. .dwIndex = sock->index,
  497. .dwPhysAddrLen = 6,
  498. .dwAddr = ipaddr,
  499. .dwType = MIB_IPNET_TYPE_STATIC
  500. };
  501. memcpy(arp.bPhysAddr, hwaddr, 6);
  502. if (undo) {
  503. ret = CreateIpNetEntry(&arp);
  504. if (ret != NO_ERROR) {
  505. win_perror2("CreateIpNetEntry", ret);
  506. return -1;
  507. }
  508. *undo = malloc(sizeof(struct ethsock_arp_undo));
  509. if (!*undo) {
  510. xperror("malloc");
  511. return -1;
  512. }
  513. (*undo)->ipaddr = ipaddr;
  514. memcpy((*undo)->hwaddr, hwaddr, 6);
  515. } else {
  516. DeleteIpNetEntry(&arp);
  517. }
  518. return 0;
  519. }
  520. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
  521. {
  522. ethsock_arp(sock, hwaddr, ipaddr, NULL);
  523. return undo ? ethsock_arp(sock, hwaddr, ipaddr, undo) : -1;
  524. }
  525. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo)
  526. {
  527. if (!*undo) {
  528. return 0;
  529. }
  530. int ret = ethsock_arp(sock, (*undo)->hwaddr, (*undo)->ipaddr, NULL);
  531. free(*undo);
  532. *undo = NULL;
  533. return ret;
  534. }
  535. #endif
  536. static bool get_hwaddr_from_pcap(const pcap_if_t *dev, uint8_t *hwaddr)
  537. {
  538. #ifndef NMRPFLASH_WINDOWS
  539. pcap_addr_t *addr;
  540. int i;
  541. for (addr = dev->addresses; addr; addr = addr->next) {
  542. if (verbosity > 1) {
  543. printf("%s: sa_family=%d, sa_data={ ", dev->name,
  544. addr->addr->sa_family);
  545. for (i = 0; i != sizeof(addr->addr->sa_data); ++i) {
  546. printf("%02x ", addr->addr->sa_data[i] & 0xff);
  547. }
  548. printf("}\n");
  549. }
  550. if (sockaddr_get_hwaddr(addr->addr, hwaddr)) {
  551. return true;
  552. }
  553. }
  554. #endif
  555. return intf_get_info(dev->name, hwaddr, NULL);
  556. }
  557. int ethsock_list_all(void)
  558. {
  559. pcap_if_t *devs, *dev;
  560. pcap_addr_t *addr;
  561. uint8_t hwaddr[6];
  562. unsigned dev_num = 0, dev_ok = 0;
  563. #ifdef NMRPFLASH_WINDOWS
  564. const char *pretty;
  565. #endif
  566. if (x_pcap_findalldevs(&devs) != 0) {
  567. return -1;
  568. }
  569. memset(hwaddr, 0, 6);
  570. for (dev = devs; dev; dev = dev->next, ++dev_num) {
  571. if (dev->flags & PCAP_IF_LOOPBACK) {
  572. if (verbosity) {
  573. printf("%-15s (loopback device)\n", dev->name);
  574. }
  575. continue;
  576. }
  577. if (!get_hwaddr_from_pcap(dev, hwaddr)) {
  578. if (verbosity) {
  579. printf("%-15s (not an ethernet device)\n",
  580. dev->name);
  581. }
  582. continue;
  583. }
  584. #ifndef NMRPFLASH_WINDOWS
  585. printf("%-15s", dev->name);
  586. #else
  587. /* Call this here so *_perror() calls don't happen within a line */
  588. pretty = intf_get_pretty_name(dev->name);
  589. if (!verbosity) {
  590. printf("%s%-2u", NMRPFLASH_NETALIAS_PREFIX, dev_num);
  591. } else {
  592. printf("%s", dev->name);
  593. }
  594. #endif
  595. for (addr = dev->addresses; addr; addr = addr->next) {
  596. if (addr->addr->sa_family == AF_INET) {
  597. printf(" %-15s",
  598. inet_ntoa(((struct sockaddr_in*)addr->addr)->sin_addr));
  599. break;
  600. }
  601. }
  602. if (!addr) {
  603. printf(" %-15s", "0.0.0.0");
  604. }
  605. printf(" %s", mac_to_str(hwaddr));
  606. #ifdef NMRPFLASH_WINDOWS
  607. if (pretty) {
  608. printf(" (%s)", pretty);
  609. } else if (dev->description) {
  610. printf(" (%s)", dev->description);
  611. }
  612. #endif
  613. printf("\n");
  614. ++dev_ok;
  615. }
  616. if (!dev_ok) {
  617. printf("No suitable network interfaces found.\n");
  618. }
  619. return 0;
  620. }
  621. int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
  622. void *arg)
  623. {
  624. struct ethsock_ip_callback_args args;
  625. pcap_if_t *devs, *dev;
  626. pcap_addr_t *addr;
  627. int status = 0;
  628. if (x_pcap_findalldevs(&devs) != 0) {
  629. return -1;
  630. }
  631. args.arg = arg;
  632. for (dev = devs; dev; dev = dev->next) {
  633. if (strcmp(sock->intf, dev->name)) {
  634. continue;
  635. }
  636. for (addr = dev->addresses; addr; addr = addr->next) {
  637. if (addr->addr->sa_family == AF_INET) {
  638. args.ipaddr = &((struct sockaddr_in*)addr->addr)->sin_addr;
  639. args.ipmask = &((struct sockaddr_in*)addr->netmask)->sin_addr;
  640. status = callback(&args);
  641. if (status <= 0) {
  642. break;
  643. }
  644. }
  645. }
  646. break;
  647. }
  648. pcap_freealldevs(devs);
  649. return status <= 0 ? status : 0;
  650. }
  651. static inline void set_addr(void *p, uint32_t addr)
  652. {
  653. struct sockaddr_in* sin = p;
  654. sin->sin_family = AF_INET;
  655. sin->sin_addr.s_addr = addr;
  656. #ifdef NMRPFLASH_BSD
  657. ((struct sockaddr*)p)->sa_len = sizeof(struct sockaddr_in);
  658. #endif
  659. }
  660. #ifndef NMRPFLASH_WINDOWS
  661. static bool intf_up(int fd, const char *intf, bool up)
  662. {
  663. struct ifreq ifr;
  664. strncpy(ifr.ifr_name, intf, IFNAMSIZ);
  665. if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
  666. if (up) {
  667. xperror("ioctl(SIOCGIFFLAGS)");
  668. }
  669. return false;
  670. }
  671. if (!up) {
  672. ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
  673. } else {
  674. ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  675. }
  676. if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
  677. if (up) {
  678. xperror("ioctl(SIOCSIFFLAGS)");
  679. }
  680. return false;
  681. }
  682. return true;
  683. }
  684. #endif
  685. int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo)
  686. {
  687. if (undo && !(*undo = malloc(sizeof(struct ethsock_ip_undo)))) {
  688. xperror("malloc");
  689. return -1;
  690. }
  691. int ret = -1;
  692. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  693. if (fd < 0) {
  694. sock_perror("socket");
  695. goto out;
  696. }
  697. #ifndef NMRPFLASH_WINDOWS
  698. // XXX: undo is non-zero only if we're adding an IP
  699. bool add = undo;
  700. #ifdef NMRPFLASH_LINUX
  701. struct ifreq ifr;
  702. strncpy(ifr.ifr_name, sock->intf, IFNAMSIZ);
  703. // FIXME: automatically determine the next free alias
  704. strcat(ifr.ifr_name, ":42");
  705. if (add) {
  706. set_addr(&ifr.ifr_addr, ipaddr);
  707. if (ioctl(fd, SIOCSIFADDR, &ifr) != 0) {
  708. xperror("ioctl(SIOSIFADDR)");
  709. goto out;
  710. }
  711. set_addr(&ifr.ifr_netmask, ipmask);
  712. if (ioctl(fd, SIOCSIFNETMASK, &ifr) != 0) {
  713. xperror("ioctl(SIOCSIFNETMASK)");
  714. goto out;
  715. }
  716. (*undo)->ip[0] = ipaddr;
  717. (*undo)->ip[1] = ipmask;
  718. }
  719. if (!intf_up(fd, ifr.ifr_name, add)) {
  720. goto out;
  721. }
  722. #else // NMRPFLASH_OSX (or any other BSD)
  723. struct ifaliasreq ifra;
  724. memset(&ifra, 0, sizeof(ifra));
  725. strncpy(ifra.ifra_name, sock->intf, IFNAMSIZ);
  726. set_addr(&ifra.ifra_addr, ipaddr);
  727. set_addr(&ifra.ifra_mask, ipmask);
  728. //set_addr(&ifra.ifra_broadaddr, (ipaddr & ipmask) | ~ipmask);
  729. if (ioctl(fd, add ? SIOCAIFADDR : SIOCDIFADDR, &ifra) != 0) {
  730. if (add) {
  731. xperror("ioctl(SIOCAIFADDR");
  732. }
  733. goto out;
  734. }
  735. if (add) {
  736. (*undo)->ip[0] = ipaddr;
  737. (*undo)->ip[1] = ipmask;
  738. intf_up(fd, ifra.ifra_name, true);
  739. }
  740. #endif
  741. #else // NMRPFLASH_WINDOWS
  742. struct sockaddr_in sin;
  743. ULONG instance;
  744. (*undo)->context = 0;
  745. DWORD err = AddIPAddress(ipaddr, ipmask, sock->index, &(*undo)->context, &instance);
  746. if (err != NO_ERROR && err != ERROR_DUP_DOMAINNAME && err != ERROR_OBJECT_ALREADY_EXISTS) {
  747. win_perror2("AddIPAddress", err);
  748. goto out;
  749. }
  750. set_addr(&sin, ipaddr);
  751. time_t beg = time_monotonic();
  752. /* Wait until the new IP has actually been added */
  753. while (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
  754. if ((time_monotonic() - beg) >= 5) {
  755. fprintf(stderr, "Failed to bind after 5 seconds: ");
  756. sock_perror("bind");
  757. DeleteIPAddress((*undo)->context);
  758. goto out;
  759. }
  760. }
  761. #endif
  762. ret = 0;
  763. out:
  764. #ifndef NMRPFLASH_WINDOWS
  765. close(fd);
  766. #else
  767. closesocket(fd);
  768. #endif
  769. if (ret != 0 && undo) {
  770. free(*undo);
  771. *undo = NULL;
  772. }
  773. return ret;
  774. }
  775. int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
  776. {
  777. if (!*undo) {
  778. return 0;
  779. }
  780. int ret;
  781. #ifndef NMRPFLASH_WINDOWS
  782. if ((*undo)->ip[0] != INADDR_NONE) {
  783. ret = ethsock_ip_add(sock, (*undo)->ip[0], (*undo)->ip[1], NULL);
  784. } else {
  785. ret = 0;
  786. }
  787. #else
  788. DeleteIPAddress((*undo)->context);
  789. ret = 0;
  790. #endif
  791. free(*undo);
  792. *undo = NULL;
  793. return ret;
  794. }