"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> |
| 7 | */ |
| 8 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include <netinet/if_ether.h> |
| 10 | #include <net/if_arp.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 13 | #include "dhcpd.h" |
| 14 | |
| 15 | |
| 16 | struct arpMsg { |
| 17 | /* Ethernet header */ |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame^] | 18 | uint8_t h_dest[6]; /* destination ether addr */ |
| 19 | uint8_t h_source[6]; /* source ether addr */ |
| 20 | uint16_t h_proto; /* packet type ID field */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 21 | |
| 22 | /* ARP packet */ |
| 23 | uint16_t htype; /* hardware type (must be ARPHRD_ETHER) */ |
| 24 | uint16_t ptype; /* protocol type (must be ETH_P_IP) */ |
| 25 | uint8_t hlen; /* hardware address length (must be 6) */ |
| 26 | uint8_t plen; /* protocol address length (must be 4) */ |
| 27 | uint16_t operation; /* ARP opcode */ |
| 28 | uint8_t sHaddr[6]; /* sender's hardware address */ |
| 29 | uint8_t sInaddr[4]; /* sender's IP address */ |
| 30 | uint8_t tHaddr[6]; /* target's hardware address */ |
| 31 | uint8_t tInaddr[4]; /* target's IP address */ |
| 32 | uint8_t pad[18]; /* pad for min. Ethernet payload (60 bytes) */ |
| 33 | } ATTRIBUTE_PACKED; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | |
| 35 | /* args: yiaddr - what IP to ping |
| 36 | * ip - our ip |
| 37 | * mac - our arp address |
| 38 | * interface - interface to use |
| 39 | * retn: 1 addr free |
| 40 | * 0 addr used |
| 41 | * -1 error |
| 42 | */ |
| 43 | |
| 44 | /* FIXME: match response against chaddr */ |
| 45 | int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) |
| 46 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | int timeout = 2; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 48 | int s; /* socket */ |
| 49 | int rv = 1; /* return value */ |
| 50 | struct sockaddr addr; /* for interface name */ |
| 51 | struct arpMsg arp; |
| 52 | fd_set fdset; |
| 53 | struct timeval tm; |
| 54 | time_t prevTime; |
| 55 | |
| 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 | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 64 | bb_perror_msg("cannot setsocketopt on raw socket"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 65 | close(s); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | /* send arp request */ |
| 70 | memset(&arp, 0, sizeof(arp)); |
| 71 | memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */ |
| 72 | memcpy(arp.h_source, mac, 6); /* MAC SA */ |
| 73 | arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */ |
| 74 | arp.htype = htons(ARPHRD_ETHER); /* hardware type */ |
| 75 | arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */ |
| 76 | arp.hlen = 6; /* hardware address length */ |
| 77 | arp.plen = 4; /* protocol address length */ |
| 78 | arp.operation = htons(ARPOP_REQUEST); /* ARP op code */ |
| 79 | memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */ |
| 80 | memcpy(arp.sHaddr, mac, 6); /* source hardware address */ |
| 81 | memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */ |
| 82 | |
| 83 | memset(&addr, 0, sizeof(addr)); |
| 84 | strcpy(addr.sa_data, interface); |
| 85 | if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) |
| 86 | rv = 0; |
| 87 | |
| 88 | /* wait arp reply, and check it */ |
| 89 | tm.tv_usec = 0; |
| 90 | prevTime = uptime(); |
| 91 | while (timeout > 0) { |
| 92 | FD_ZERO(&fdset); |
| 93 | FD_SET(s, &fdset); |
| 94 | tm.tv_sec = timeout; |
| 95 | if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 96 | bb_perror_msg("error on ARPING request"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 97 | if (errno != EINTR) rv = 0; |
| 98 | } else if (FD_ISSET(s, &fdset)) { |
| 99 | if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0; |
| 100 | if (arp.operation == htons(ARPOP_REPLY) && |
| 101 | memcmp(arp.tHaddr, mac, 6) == 0 && |
| 102 | *((uint32_t *) arp.sInaddr) == yiaddr) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 103 | DEBUG("Valid arp reply received for this address"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 104 | rv = 0; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | timeout -= uptime() - prevTime; |
| 109 | prevTime = uptime(); |
| 110 | } |
| 111 | close(s); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 112 | DEBUG("%salid arp replies for this address", rv ? "No v" : "V"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 113 | return rv; |
| 114 | } |