2
* RFC3927 ZeroConf IPv4 Link-Local addressing
3
* (see <http://www.zeroconf.org/>)
5
* Copied from BusyBox - networking/zcip.c
7
* Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
8
* Copyright (C) 2004 by David Brownell
9
* Copyright (C) 2010 by Joe Hershberger
11
* Licensed under the GPL v2 or later
19
/* We don't need more than 32 bits of the counter */
20
#define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ))
24
LINKLOCAL_ADDR = 0xa9fe0000,
26
IN_CLASSB_NET = 0xffff0000,
27
IN_CLASSB_HOST = 0x0000ffff,
29
/* protocol timeout parameters, specified in seconds */
35
RATE_LIMIT_INTERVAL = 60,
38
ANNOUNCE_INTERVAL = 2,
42
/* States during the configuration process. */
43
static enum ll_state_t {
53
static int timeout_ms = -1;
54
static unsigned deadline_ms;
55
static unsigned conflicts;
56
static unsigned nprobes;
57
static unsigned nclaims;
59
static unsigned int seed;
61
static void link_local_timeout(void);
64
* Pick a random link local IP address on 169.254/16, except that
65
* the first and last 256 addresses are reserved.
67
static IPaddr_t pick(void)
72
tmp = rand_r(&seed) & IN_CLASSB_HOST;
73
} while (tmp > (IN_CLASSB_HOST - 0x0200));
74
return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
78
* Return milliseconds of random delay, up to "secs" seconds.
80
static inline unsigned random_delay_ms(unsigned secs)
82
return rand_r(&seed) % (secs * 1000);
85
static void configure_wait(void)
90
/* poll, being ready to adjust current timeout */
92
timeout_ms = random_delay_ms(PROBE_WAIT);
94
/* set deadline_ms to the point in time when we timeout */
95
deadline_ms = MONOTONIC_MS() + timeout_ms;
97
debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n",
98
timeout_ms, eth_get_name(), nprobes, nclaims);
100
NetSetTimeout(timeout_ms, link_local_timeout);
103
void link_local_start(void)
105
ip = getenv_IPaddr("llipaddr");
106
if (ip != 0 && (ntohl(ip) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
107
puts("invalid link address");
108
net_set_state(NETLOOP_FAIL);
111
NetOurSubnetMask = IN_CLASSB_NET;
127
static void link_local_timeout(void)
131
/* timeouts in the PROBE state mean no conflicting ARP packets
132
have been received, so we can progress through the states */
133
if (nprobes < PROBE_NUM) {
135
debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n",
136
nprobes, eth_get_name(), &ip);
137
arp_raw_request(0, NetEtherNullAddr, ip);
138
timeout_ms = PROBE_MIN * 1000;
139
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
141
/* Switch to announce state */
144
debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
145
nclaims, eth_get_name(), &ip);
146
arp_raw_request(ip, NetOurEther, ip);
147
timeout_ms = ANNOUNCE_INTERVAL * 1000;
150
case RATE_LIMIT_PROBE:
151
/* timeouts in the RATE_LIMIT_PROBE state mean no conflicting
152
ARP packets have been received, so we can move immediately
153
to the announce state */
156
debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
157
nclaims, eth_get_name(), &ip);
158
arp_raw_request(ip, NetOurEther, ip);
159
timeout_ms = ANNOUNCE_INTERVAL * 1000;
162
/* timeouts in the ANNOUNCE state mean no conflicting ARP
163
packets have been received, so we can progress through
165
if (nclaims < ANNOUNCE_NUM) {
167
debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
168
nclaims, eth_get_name(), &ip);
169
arp_raw_request(ip, NetOurEther, ip);
170
timeout_ms = ANNOUNCE_INTERVAL * 1000;
172
/* Switch to monitor state */
174
printf("Successfully assigned %pI4\n", &ip);
175
NetCopyIP(&NetOurIP, &ip);
179
/* Never timeout in the monitor state */
180
NetSetTimeout(0, NULL);
182
/* NOTE: all other exit paths should deconfig ... */
183
net_set_state(NETLOOP_SUCCESS);
188
/* We won! No ARP replies, so just go back to monitor */
194
/* Invalid, should never happen. Restart the whole protocol */
205
void link_local_receive_arp(struct arp_hdr *arp, int len)
207
int source_ip_conflict;
208
int target_ip_conflict;
209
IPaddr_t null_ip = 0;
211
if (state == DISABLED)
214
/* We need to adjust the timeout in case we didn't receive a
215
conflicting packet. */
216
if (timeout_ms > 0) {
217
unsigned diff = deadline_ms - MONOTONIC_MS();
218
if ((int)(diff) < 0) {
219
/* Current time is greater than the expected timeout
220
time. This should never happen */
221
debug_cond(DEBUG_LL_STATE,
222
"missed an expected timeout\n");
225
debug_cond(DEBUG_INT_STATE, "adjusting timeout\n");
226
timeout_ms = diff | 1; /* never 0 */
230
/* XXX Don't bother with ethernet link just yet */
231
if ((fds[0].revents & POLLIN) == 0) {
232
if (fds[0].revents & POLLERR) {
234
* FIXME: links routinely go down;
236
bb_error_msg("iface %s is down", eth_get_name());
238
run(argv, "deconfig", &ip);
246
debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n",
247
eth_get_name(), ntohs(arp->ar_pro),
249
debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n",
252
debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n",
256
if (arp->ar_op != htons(ARPOP_REQUEST)
257
&& arp->ar_op != htons(ARPOP_REPLY)
263
source_ip_conflict = 0;
264
target_ip_conflict = 0;
266
if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0
267
&& memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0
269
source_ip_conflict = 1;
273
* According to RFC 3927, section 2.2.1:
274
* Check if packet is an ARP probe by checking for a null source IP
275
* then check that target IP is equal to ours and source hw addr
276
* is not equal to ours. This condition should cause a conflict only
279
if (arp->ar_op == htons(ARPOP_REQUEST) &&
280
memcmp(&arp->ar_spa, &null_ip, ARP_PLEN) == 0 &&
281
memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 &&
282
memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0) {
283
target_ip_conflict = 1;
286
debug_cond(DEBUG_NET_PKT,
287
"state = %d, source ip conflict = %d, target ip conflict = "
288
"%d\n", state, source_ip_conflict, target_ip_conflict);
292
/* When probing or announcing, check for source IP conflicts
293
and other hosts doing ARP probes (target IP conflicts). */
294
if (source_ip_conflict || target_ip_conflict) {
297
if (conflicts >= MAX_CONFLICTS) {
298
debug("%s ratelimit\n", eth_get_name());
299
timeout_ms = RATE_LIMIT_INTERVAL * 1000;
300
state = RATE_LIMIT_PROBE;
303
/* restart the whole protocol */
311
/* If a conflict, we try to defend with a single ARP probe */
312
if (source_ip_conflict) {
313
debug("monitor conflict -- defending\n");
315
timeout_ms = DEFEND_INTERVAL * 1000;
316
arp_raw_request(ip, NetOurEther, ip);
320
/* Well, we tried. Start over (on conflict) */
321
if (source_ip_conflict) {
323
debug("defend conflict -- starting over\n");
327
/* restart the whole protocol */
335
/* Invalid, should never happen. Restart the whole protocol */
336
debug("invalid state -- starting over\n");