Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/networking/udhcp/arpping.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (show annotations) (download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 4225 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 /* vi: set sw=4 ts=4: */
2 /*
3 * arpping.c
4 *
5 * Mostly stolen from: dhcpcd - DHCP client daemon
6 * by Yoichi Hariguchi <yoichi@fore.com>
7 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 */
10 #include <netinet/if_ether.h>
11 #include <net/if_arp.h>
12
13 #include "common.h"
14 #include "dhcpd.h"
15
16 struct arpMsg {
17 /* Ethernet header */
18 uint8_t h_dest[6]; /* 00 destination ether addr */
19 uint8_t h_source[6]; /* 06 source ether addr */
20 uint16_t h_proto; /* 0c packet type ID field */
21
22 /* ARP packet */
23 uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
24 uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
25 uint8_t hlen; /* 12 hardware address length (must be 6) */
26 uint8_t plen; /* 13 protocol address length (must be 4) */
27 uint16_t operation; /* 14 ARP opcode */
28 uint8_t sHaddr[6]; /* 16 sender's hardware address */
29 uint8_t sInaddr[4]; /* 1c sender's IP address */
30 uint8_t tHaddr[6]; /* 20 target's hardware address */
31 uint8_t tInaddr[4]; /* 26 target's IP address */
32 uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
33 } PACKED;
34
35 enum {
36 ARP_MSG_SIZE = 0x2a
37 };
38
39 /* Returns 1 if no reply received */
40 int FAST_FUNC arpping(uint32_t test_nip,
41 const uint8_t *safe_mac,
42 uint32_t from_ip,
43 uint8_t *from_mac,
44 const char *interface)
45 {
46 int timeout_ms;
47 struct pollfd pfd[1];
48 #define s (pfd[0].fd) /* socket */
49 int rv = 1; /* "no reply received" yet */
50 struct sockaddr addr; /* for interface name */
51 struct arpMsg arp;
52
53 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
54 if (s == -1) {
55 bb_perror_msg(bb_msg_can_not_create_raw_socket);
56 return -1;
57 }
58
59 if (setsockopt_broadcast(s) == -1) {
60 bb_perror_msg("can't enable bcast on raw socket");
61 goto ret;
62 }
63
64 /* send arp request */
65 memset(&arp, 0, sizeof(arp));
66 memset(arp.h_dest, 0xff, 6); /* MAC DA */
67 memcpy(arp.h_source, from_mac, 6); /* MAC SA */
68 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
69 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
70 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
71 arp.hlen = 6; /* hardware address length */
72 arp.plen = 4; /* protocol address length */
73 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
74 memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
75 memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
76 /* tHaddr is zero-filled */ /* target hardware address */
77 memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
78
79 memset(&addr, 0, sizeof(addr));
80 safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
81 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
82 // TODO: error message? caller didn't expect us to fail,
83 // just returning 1 "no reply received" misleads it.
84 goto ret;
85 }
86
87 /* wait for arp reply, and check it */
88 timeout_ms = 2000;
89 do {
90 int r;
91 unsigned prevTime = monotonic_ms();
92
93 pfd[0].events = POLLIN;
94 r = safe_poll(pfd, 1, timeout_ms);
95 if (r < 0)
96 break;
97 if (r) {
98 r = safe_read(s, &arp, sizeof(arp));
99 if (r < 0)
100 break;
101
102 //log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
103 // arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
104 // arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
105
106 if (r >= ARP_MSG_SIZE
107 && arp.operation == htons(ARPOP_REPLY)
108 /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
109 /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
110 && *((uint32_t *) arp.sInaddr) == test_nip
111 ) {
112 /* if ARP source MAC matches safe_mac
113 * (which is client's MAC), then it's not a conflict
114 * (client simply already has this IP and replies to ARPs!)
115 */
116 if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
117 rv = 0;
118 //else log2("sHaddr == safe_mac");
119 break;
120 }
121 }
122 timeout_ms -= (unsigned)monotonic_ms() - prevTime;
123 } while (timeout_ms > 0);
124
125 ret:
126 close(s);
127 log1("%srp reply received for this address", rv ? "No a" : "A");
128 return rv;
129 }