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