]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - net/link_local.c
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
22 /* We don't need more than 32 bits of the counter */
23 #define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ))
27 LINKLOCAL_ADDR
= 0xa9fe0000,
29 IN_CLASSB_NET
= 0xffff0000,
30 IN_CLASSB_HOST
= 0x0000ffff,
32 /* protocol timeout parameters, specified in seconds */
38 RATE_LIMIT_INTERVAL
= 60,
41 ANNOUNCE_INTERVAL
= 2,
45 /* States during the configuration process. */
46 static enum ll_state_t
{
55 static struct in_addr ip
;
56 static int timeout_ms
= -1;
57 static unsigned deadline_ms
;
58 static unsigned conflicts
;
59 static unsigned nprobes
;
60 static unsigned nclaims
;
62 static unsigned int seed
;
64 static void link_local_timeout(void);
67 * Pick a random link local IP address on 169.254/16, except that
68 * the first and last 256 addresses are reserved.
70 static struct in_addr
pick(void)
76 tmp
= rand_r(&seed
) & IN_CLASSB_HOST
;
77 } while (tmp
> (IN_CLASSB_HOST
- 0x0200));
78 ip
.s_addr
= htonl((LINKLOCAL_ADDR
+ 0x0100) + tmp
);
83 * Return milliseconds of random delay, up to "secs" seconds.
85 static inline unsigned random_delay_ms(unsigned secs
)
87 return rand_r(&seed
) % (secs
* 1000);
90 static void configure_wait(void)
95 /* poll, being ready to adjust current timeout */
97 timeout_ms
= random_delay_ms(PROBE_WAIT
);
99 /* set deadline_ms to the point in time when we timeout */
100 deadline_ms
= MONOTONIC_MS() + timeout_ms
;
102 debug_cond(DEBUG_DEV_PKT
, "...wait %d %s nprobes=%u, nclaims=%u\n",
103 timeout_ms
, eth_get_name(), nprobes
, nclaims
);
105 net_set_timeout_handler(timeout_ms
, link_local_timeout
);
108 void link_local_start(void)
110 ip
= env_get_ip("llipaddr");
111 if (ip
.s_addr
!= 0 &&
112 (ntohl(ip
.s_addr
) & IN_CLASSB_NET
) != LINKLOCAL_ADDR
) {
113 puts("invalid link address");
114 net_set_state(NETLOOP_FAIL
);
117 net_netmask
.s_addr
= htonl(IN_CLASSB_NET
);
133 static void link_local_timeout(void)
137 /* timeouts in the PROBE state mean no conflicting ARP packets
138 have been received, so we can progress through the states */
139 if (nprobes
< PROBE_NUM
) {
140 struct in_addr zero_ip
= {.s_addr
= 0};
143 debug_cond(DEBUG_LL_STATE
, "probe/%u %s@%pI4\n",
144 nprobes
, eth_get_name(), &ip
);
145 arp_raw_request(zero_ip
, net_null_ethaddr
, ip
);
146 timeout_ms
= PROBE_MIN
* 1000;
147 timeout_ms
+= random_delay_ms(PROBE_MAX
- PROBE_MIN
);
149 /* Switch to announce state */
152 debug_cond(DEBUG_LL_STATE
, "announce/%u %s@%pI4\n",
153 nclaims
, eth_get_name(), &ip
);
154 arp_raw_request(ip
, net_ethaddr
, ip
);
155 timeout_ms
= ANNOUNCE_INTERVAL
* 1000;
158 case RATE_LIMIT_PROBE
:
159 /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting
160 ARP packets have been received, so we can move immediately
161 to the announce state */
164 debug_cond(DEBUG_LL_STATE
, "announce/%u %s@%pI4\n",
165 nclaims
, eth_get_name(), &ip
);
166 arp_raw_request(ip
, net_ethaddr
, ip
);
167 timeout_ms
= ANNOUNCE_INTERVAL
* 1000;
170 /* timeouts in the ANNOUNCE state mean no conflicting ARP
171 packets have been received, so we can progress through
173 if (nclaims
< ANNOUNCE_NUM
) {
175 debug_cond(DEBUG_LL_STATE
, "announce/%u %s@%pI4\n",
176 nclaims
, eth_get_name(), &ip
);
177 arp_raw_request(ip
, net_ethaddr
, ip
);
178 timeout_ms
= ANNOUNCE_INTERVAL
* 1000;
180 /* Switch to monitor state */
182 printf("Successfully assigned %pI4\n", &ip
);
183 net_copy_ip(&net_ip
, &ip
);
187 /* Never timeout in the monitor state */
188 net_set_timeout_handler(0, NULL
);
190 /* NOTE: all other exit paths should deconfig ... */
191 net_set_state(NETLOOP_SUCCESS
);
196 /* We won! No ARP replies, so just go back to monitor */
202 /* Invalid, should never happen. Restart the whole protocol */
213 void link_local_receive_arp(struct arp_hdr
*arp
, int len
)
215 int source_ip_conflict
;
216 int target_ip_conflict
;
217 struct in_addr null_ip
= {.s_addr
= 0};
219 if (state
== DISABLED
)
222 /* We need to adjust the timeout in case we didn't receive a
223 conflicting packet. */
224 if (timeout_ms
> 0) {
225 unsigned diff
= deadline_ms
- MONOTONIC_MS();
226 if ((int)(diff
) < 0) {
227 /* Current time is greater than the expected timeout
228 time. This should never happen */
229 debug_cond(DEBUG_LL_STATE
,
230 "missed an expected timeout\n");
233 debug_cond(DEBUG_INT_STATE
, "adjusting timeout\n");
234 timeout_ms
= diff
| 1; /* never 0 */
238 /* XXX Don't bother with ethernet link just yet */
239 if ((fds
[0].revents
& POLLIN
) == 0) {
240 if (fds
[0].revents
& POLLERR
) {
242 * FIXME: links routinely go down;
244 bb_error_msg("iface %s is down", eth_get_name());
246 run(argv
, "deconfig", &ip
);
253 debug_cond(DEBUG_INT_STATE
, "%s recv arp type=%d, op=%d,\n",
254 eth_get_name(), ntohs(arp
->ar_pro
),
256 debug_cond(DEBUG_INT_STATE
, "\tsource=%pM %pI4\n",
259 debug_cond(DEBUG_INT_STATE
, "\ttarget=%pM %pI4\n",
263 if (arp
->ar_op
!= htons(ARPOP_REQUEST
) &&
264 arp
->ar_op
!= htons(ARPOP_REPLY
)) {
269 source_ip_conflict
= 0;
270 target_ip_conflict
= 0;
272 if (memcmp(&arp
->ar_spa
, &ip
, ARP_PLEN
) == 0 &&
273 memcmp(&arp
->ar_sha
, net_ethaddr
, ARP_HLEN
) != 0)
274 source_ip_conflict
= 1;
277 * According to RFC 3927, section 2.2.1:
278 * Check if packet is an ARP probe by checking for a null source IP
279 * then check that target IP is equal to ours and source hw addr
280 * is not equal to ours. This condition should cause a conflict only
283 if (arp
->ar_op
== htons(ARPOP_REQUEST
) &&
284 memcmp(&arp
->ar_spa
, &null_ip
, ARP_PLEN
) == 0 &&
285 memcmp(&arp
->ar_tpa
, &ip
, ARP_PLEN
) == 0 &&
286 memcmp(&arp
->ar_sha
, net_ethaddr
, ARP_HLEN
) != 0) {
287 target_ip_conflict
= 1;
290 debug_cond(DEBUG_NET_PKT
,
291 "state = %d, source ip conflict = %d, target ip conflict = "
292 "%d\n", state
, source_ip_conflict
, target_ip_conflict
);
296 /* When probing or announcing, check for source IP conflicts
297 and other hosts doing ARP probes (target IP conflicts). */
298 if (source_ip_conflict
|| target_ip_conflict
) {
301 if (conflicts
>= MAX_CONFLICTS
) {
302 debug("%s ratelimit\n", eth_get_name());
303 timeout_ms
= RATE_LIMIT_INTERVAL
* 1000;
304 state
= RATE_LIMIT_PROBE
;
307 /* restart the whole protocol */
315 /* If a conflict, we try to defend with a single ARP probe */
316 if (source_ip_conflict
) {
317 debug("monitor conflict -- defending\n");
319 timeout_ms
= DEFEND_INTERVAL
* 1000;
320 arp_raw_request(ip
, net_ethaddr
, ip
);
324 /* Well, we tried. Start over (on conflict) */
325 if (source_ip_conflict
) {
327 debug("defend conflict -- starting over\n");
331 /* restart the whole protocol */
339 /* Invalid, should never happen. Restart the whole protocol */
340 debug("invalid state -- starting over\n");