blob: 8e7665f09073588dfde0e136bf38d4c71227c87c [file] [log] [blame]
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001/*
2 * RFC3927 ZeroConf IPv4 Link-Local addressing
3 * (see <http://www.zeroconf.org/>)
4 *
5 * Copied from BusyBox - networking/zcip.c
6 *
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
10 *
11 * Licensed under the GPL v2 or later
12 */
13
14#include <common.h>
15#include <net.h>
16#include "arp.h"
17#include "net_rand.h"
18
19/* We don't need more than 32 bits of the counter */
20#define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ))
21
22enum {
23/* 169.254.0.0 */
24 LINKLOCAL_ADDR = 0xa9fe0000,
25
26 IN_CLASSB_NET = 0xffff0000,
27 IN_CLASSB_HOST = 0x0000ffff,
28
29/* protocol timeout parameters, specified in seconds */
30 PROBE_WAIT = 1,
31 PROBE_MIN = 1,
32 PROBE_MAX = 2,
33 PROBE_NUM = 3,
34 MAX_CONFLICTS = 10,
35 RATE_LIMIT_INTERVAL = 60,
36 ANNOUNCE_WAIT = 2,
37 ANNOUNCE_NUM = 2,
38 ANNOUNCE_INTERVAL = 2,
39 DEFEND_INTERVAL = 10
40};
41
42/* States during the configuration process. */
43static enum ll_state_t {
44 PROBE = 0,
45 RATE_LIMIT_PROBE,
46 ANNOUNCE,
47 MONITOR,
48 DEFEND,
49 DISABLED
50} state = DISABLED;
51
52static IPaddr_t ip;
53static int timeout_ms = -1;
54static unsigned deadline_ms;
55static unsigned conflicts;
56static unsigned nprobes;
57static unsigned nclaims;
58static int ready;
Michael Walle99e139d2012-06-05 11:33:15 +000059static unsigned int seed;
Joe Hershbergerd22c3382012-05-23 08:00:12 +000060
61static void link_local_timeout(void);
62
63/**
64 * Pick a random link local IP address on 169.254/16, except that
65 * the first and last 256 addresses are reserved.
66 */
67static IPaddr_t pick(void)
68{
69 unsigned tmp;
70
71 do {
Michael Walle99e139d2012-06-05 11:33:15 +000072 tmp = rand_r(&seed) & IN_CLASSB_HOST;
Joe Hershbergerd22c3382012-05-23 08:00:12 +000073 } while (tmp > (IN_CLASSB_HOST - 0x0200));
74 return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
75}
76
77/**
78 * Return milliseconds of random delay, up to "secs" seconds.
79 */
80static inline unsigned random_delay_ms(unsigned secs)
81{
Michael Walle99e139d2012-06-05 11:33:15 +000082 return rand_r(&seed) % (secs * 1000);
Joe Hershbergerd22c3382012-05-23 08:00:12 +000083}
84
85static void configure_wait(void)
86{
87 if (timeout_ms == -1)
88 return;
89
90 /* poll, being ready to adjust current timeout */
91 if (!timeout_ms)
92 timeout_ms = random_delay_ms(PROBE_WAIT);
93
94 /* set deadline_ms to the point in time when we timeout */
95 deadline_ms = MONOTONIC_MS() + timeout_ms;
96
Joe Hershberger4ef8d532012-05-23 08:01:04 +000097 debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +000098 timeout_ms, eth_get_name(), nprobes, nclaims);
99
100 NetSetTimeout(timeout_ms, link_local_timeout);
101}
102
103void link_local_start(void)
104{
105 ip = getenv_IPaddr("llipaddr");
106 if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
107 puts("invalid link address");
108 net_set_state(NETLOOP_FAIL);
109 return;
110 }
111 NetOurSubnetMask = IN_CLASSB_NET;
112
Michael Walle99e139d2012-06-05 11:33:15 +0000113 seed = seed_mac();
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000114 if (ip == 0)
115 ip = pick();
116
117 state = PROBE;
118 timeout_ms = 0;
119 conflicts = 0;
120 nprobes = 0;
121 nclaims = 0;
122 ready = 0;
123
124 configure_wait();
125}
126
127static void link_local_timeout(void)
128{
129 switch (state) {
130 case PROBE:
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) {
134 nprobes++;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000135 debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000136 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);
140 } else {
141 /* Switch to announce state */
142 state = ANNOUNCE;
143 nclaims = 0;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000144 debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000145 nclaims, eth_get_name(), &ip);
146 arp_raw_request(ip, NetOurEther, ip);
147 timeout_ms = ANNOUNCE_INTERVAL * 1000;
148 }
149 break;
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 */
154 state = ANNOUNCE;
155 nclaims = 0;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000156 debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000157 nclaims, eth_get_name(), &ip);
158 arp_raw_request(ip, NetOurEther, ip);
159 timeout_ms = ANNOUNCE_INTERVAL * 1000;
160 break;
161 case ANNOUNCE:
162 /* timeouts in the ANNOUNCE state mean no conflicting ARP
163 packets have been received, so we can progress through
164 the states */
165 if (nclaims < ANNOUNCE_NUM) {
166 nclaims++;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000167 debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000168 nclaims, eth_get_name(), &ip);
169 arp_raw_request(ip, NetOurEther, ip);
170 timeout_ms = ANNOUNCE_INTERVAL * 1000;
171 } else {
172 /* Switch to monitor state */
173 state = MONITOR;
174 printf("Successfully assigned %pI4\n", &ip);
175 NetCopyIP(&NetOurIP, &ip);
176 ready = 1;
177 conflicts = 0;
178 timeout_ms = -1;
179 /* Never timeout in the monitor state */
180 NetSetTimeout(0, NULL);
181
182 /* NOTE: all other exit paths should deconfig ... */
183 net_set_state(NETLOOP_SUCCESS);
184 return;
185 }
186 break;
187 case DEFEND:
188 /* We won! No ARP replies, so just go back to monitor */
189 state = MONITOR;
190 timeout_ms = -1;
191 conflicts = 0;
192 break;
193 default:
194 /* Invalid, should never happen. Restart the whole protocol */
195 state = PROBE;
196 ip = pick();
197 timeout_ms = 0;
198 nprobes = 0;
199 nclaims = 0;
200 break;
201 }
202 configure_wait();
203}
204
205void link_local_receive_arp(struct arp_hdr *arp, int len)
206{
207 int source_ip_conflict;
208 int target_ip_conflict;
209
210 if (state == DISABLED)
211 return;
212
213 /* We need to adjust the timeout in case we didn't receive a
214 conflicting packet. */
215 if (timeout_ms > 0) {
216 unsigned diff = deadline_ms - MONOTONIC_MS();
217 if ((int)(diff) < 0) {
218 /* Current time is greater than the expected timeout
219 time. This should never happen */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000220 debug_cond(DEBUG_LL_STATE,
221 "missed an expected timeout\n");
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000222 timeout_ms = 0;
223 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000224 debug_cond(DEBUG_INT_STATE, "adjusting timeout\n");
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000225 timeout_ms = diff | 1; /* never 0 */
226 }
227 }
228/*
229 * XXX Don't bother with ethernet link just yet
230 if ((fds[0].revents & POLLIN) == 0) {
231 if (fds[0].revents & POLLERR) {
Wolfgang Denk3fe63832012-07-10 09:18:33 +0200232 /*
233 * FIXME: links routinely go down;
234 */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000235 bb_error_msg("iface %s is down", eth_get_name());
236 if (ready) {
237 run(argv, "deconfig", &ip);
238 }
239 return EXIT_FAILURE;
240 }
241 continue;
242 }
243*/
244
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000245 debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000246 eth_get_name(), ntohs(arp->ar_pro),
247 ntohs(arp->ar_op));
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000248 debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000249 &arp->ar_sha,
250 &arp->ar_spa);
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000251 debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n",
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000252 &arp->ar_tha,
253 &arp->ar_tpa);
254
255 if (arp->ar_op != htons(ARPOP_REQUEST)
256 && arp->ar_op != htons(ARPOP_REPLY)
257 ) {
258 configure_wait();
259 return;
260 }
261
262 source_ip_conflict = 0;
263 target_ip_conflict = 0;
264
265 if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0
266 && memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0
267 ) {
268 source_ip_conflict = 1;
269 }
270 if (arp->ar_op == htons(ARPOP_REQUEST)
271 && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0
272 && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0
273 ) {
274 target_ip_conflict = 1;
275 }
276
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000277 debug_cond(DEBUG_NET_PKT,
278 "state = %d, source ip conflict = %d, target ip conflict = "
279 "%d\n", state, source_ip_conflict, target_ip_conflict);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000280 switch (state) {
281 case PROBE:
282 case ANNOUNCE:
283 /* When probing or announcing, check for source IP conflicts
284 and other hosts doing ARP probes (target IP conflicts). */
285 if (source_ip_conflict || target_ip_conflict) {
286 conflicts++;
287 state = PROBE;
288 if (conflicts >= MAX_CONFLICTS) {
289 debug("%s ratelimit\n", eth_get_name());
290 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
291 state = RATE_LIMIT_PROBE;
292 }
293
294 /* restart the whole protocol */
295 ip = pick();
296 timeout_ms = 0;
297 nprobes = 0;
298 nclaims = 0;
299 }
300 break;
301 case MONITOR:
302 /* If a conflict, we try to defend with a single ARP probe */
303 if (source_ip_conflict) {
304 debug("monitor conflict -- defending\n");
305 state = DEFEND;
306 timeout_ms = DEFEND_INTERVAL * 1000;
307 arp_raw_request(ip, NetOurEther, ip);
308 }
309 break;
310 case DEFEND:
311 /* Well, we tried. Start over (on conflict) */
312 if (source_ip_conflict) {
313 state = PROBE;
314 debug("defend conflict -- starting over\n");
315 ready = 0;
316 NetOurIP = 0;
317
318 /* restart the whole protocol */
319 ip = pick();
320 timeout_ms = 0;
321 nprobes = 0;
322 nclaims = 0;
323 }
324 break;
325 default:
326 /* Invalid, should never happen. Restart the whole protocol */
327 debug("invalid state -- starting over\n");
328 state = PROBE;
329 ip = pick();
330 timeout_ms = 0;
331 nprobes = 0;
332 nclaims = 0;
333 break;
334 }
335 configure_wait();
336}