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