"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 3 | * Mostly stolen from: dhcpcd - DHCP client daemon |
| 4 | * by Yoichi Hariguchi <yoichi@fore.com> |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | #include <netinet/if_ether.h> |
| 9 | #include <net/if_arp.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 13 | struct arpMsg { |
| 14 | /* Ethernet header */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 15 | uint8_t h_dest[6]; /* 00 destination ether addr */ |
| 16 | uint8_t h_source[6]; /* 06 source ether addr */ |
| 17 | uint16_t h_proto; /* 0c packet type ID field */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 18 | |
| 19 | /* ARP packet */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 20 | uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */ |
| 21 | uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */ |
| 22 | uint8_t hlen; /* 12 hardware address length (must be 6) */ |
| 23 | uint8_t plen; /* 13 protocol address length (must be 4) */ |
| 24 | uint16_t operation; /* 14 ARP opcode */ |
| 25 | uint8_t sHaddr[6]; /* 16 sender's hardware address */ |
| 26 | uint8_t sInaddr[4]; /* 1c sender's IP address */ |
| 27 | uint8_t tHaddr[6]; /* 20 target's hardware address */ |
| 28 | uint8_t tInaddr[4]; /* 26 target's IP address */ |
| 29 | uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 30 | } PACKED; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 32 | enum { |
| 33 | ARP_MSG_SIZE = 0x2a |
| 34 | }; |
| 35 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 36 | /* Returns 1 if no reply received */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 37 | int FAST_FUNC arpping(uint32_t test_nip, |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 38 | const uint8_t *safe_mac, |
| 39 | uint32_t from_ip, |
| 40 | uint8_t *from_mac, |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 41 | const char *interface, |
| 42 | unsigned timeo) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | { |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 44 | int timeout_ms; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 45 | struct pollfd pfd[1]; |
| 46 | #define s (pfd[0].fd) /* socket */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 47 | int rv = 1; /* "no reply received" yet */ |
| 48 | struct sockaddr addr; /* for interface name */ |
| 49 | struct arpMsg arp; |
Denys Vlasenko | 0d15d5b | 2021-06-03 11:32:40 +0200 | [diff] [blame] | 50 | const char *msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 51 | |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 52 | if (!timeo) |
| 53 | return 1; |
| 54 | |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 55 | s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); |
| 56 | if (s == -1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 57 | bb_simple_perror_msg(bb_msg_can_not_create_raw_socket); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | return -1; |
| 59 | } |
| 60 | |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 61 | if (setsockopt_broadcast(s) == -1) { |
Denys Vlasenko | 0d15d5b | 2021-06-03 11:32:40 +0200 | [diff] [blame] | 62 | bb_simple_perror_msg("can't enable bcast on ARP socket"); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 63 | goto ret; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* send arp request */ |
| 67 | memset(&arp, 0, sizeof(arp)); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 68 | memset(arp.h_dest, 0xff, 6); /* MAC DA */ |
| 69 | memcpy(arp.h_source, from_mac, 6); /* MAC SA */ |
| 70 | arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */ |
| 71 | arp.htype = htons(ARPHRD_ETHER); /* hardware type */ |
| 72 | arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */ |
| 73 | arp.hlen = 6; /* hardware address length */ |
| 74 | arp.plen = 4; /* protocol address length */ |
| 75 | arp.operation = htons(ARPOP_REQUEST); /* ARP op code */ |
| 76 | memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */ |
| 77 | memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */ |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 78 | /* tHaddr is zero-filled */ /* target hardware address */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 79 | memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 80 | |
| 81 | memset(&addr, 0, sizeof(addr)); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 82 | safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data)); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 83 | if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) { |
| 84 | // TODO: error message? caller didn't expect us to fail, |
| 85 | // just returning 1 "no reply received" misleads it. |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 86 | goto ret; |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 87 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 89 | /* wait for arp reply, and check it */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 90 | timeout_ms = (int)timeo; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 91 | do { |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 92 | typedef uint32_t aliased_uint32_t FIX_ALIASING; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 93 | int r; |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 94 | unsigned prevTime = monotonic_ms(); |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 95 | |
| 96 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 97 | r = safe_poll(pfd, 1, timeout_ms); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 98 | if (r < 0) |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 99 | break; |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 100 | if (r) { |
Denys Vlasenko | cb23d93 | 2010-01-12 12:29:55 +0100 | [diff] [blame] | 101 | r = safe_read(s, &arp, sizeof(arp)); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 102 | if (r < 0) |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 103 | break; |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 104 | |
Denys Vlasenko | dc36a72 | 2009-06-17 13:56:50 +0200 | [diff] [blame] | 105 | //log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x", |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 106 | // arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2], |
| 107 | // arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]); |
| 108 | |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 109 | if (r >= ARP_MSG_SIZE |
| 110 | && arp.operation == htons(ARPOP_REPLY) |
| 111 | /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */ |
| 112 | /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 113 | && *(aliased_uint32_t*)arp.sInaddr == test_nip |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 114 | ) { |
Denys Vlasenko | dc36a72 | 2009-06-17 13:56:50 +0200 | [diff] [blame] | 115 | /* if ARP source MAC matches safe_mac |
| 116 | * (which is client's MAC), then it's not a conflict |
| 117 | * (client simply already has this IP and replies to ARPs!) |
| 118 | */ |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 119 | if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0) |
| 120 | rv = 0; |
Denys Vlasenko | dc36a72 | 2009-06-17 13:56:50 +0200 | [diff] [blame] | 121 | //else log2("sHaddr == safe_mac"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | } |
Denys Vlasenko | 6500d12 | 2012-04-17 17:10:31 +0200 | [diff] [blame] | 125 | timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1; |
| 126 | |
| 127 | /* We used to check "timeout_ms > 0", but |
| 128 | * this is more under/overflow-resistant |
| 129 | * (people did see overflows here when system time jumps): |
| 130 | */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 131 | } while ((unsigned)timeout_ms <= timeo); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 132 | |
| 133 | ret: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 134 | close(s); |
Denys Vlasenko | 0d15d5b | 2021-06-03 11:32:40 +0200 | [diff] [blame] | 135 | msg = "no ARP reply received for this address"; |
| 136 | if (rv == 0) |
| 137 | msg += 3; |
| 138 | log1s(msg); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 139 | return rv; |
| 140 | } |