link_local.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  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. #include <common.h>
  14. #include <net.h>
  15. #include "arp.h"
  16. #include "net_rand.h"
  17. /* We don't need more than 32 bits of the counter */
  18. #define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ))
  19. enum {
  20. /* 169.254.0.0 */
  21. LINKLOCAL_ADDR = 0xa9fe0000,
  22. IN_CLASSB_NET = 0xffff0000,
  23. IN_CLASSB_HOST = 0x0000ffff,
  24. /* protocol timeout parameters, specified in seconds */
  25. PROBE_WAIT = 1,
  26. PROBE_MIN = 1,
  27. PROBE_MAX = 2,
  28. PROBE_NUM = 3,
  29. MAX_CONFLICTS = 10,
  30. RATE_LIMIT_INTERVAL = 60,
  31. ANNOUNCE_WAIT = 2,
  32. ANNOUNCE_NUM = 2,
  33. ANNOUNCE_INTERVAL = 2,
  34. DEFEND_INTERVAL = 10
  35. };
  36. /* States during the configuration process. */
  37. static enum ll_state_t {
  38. PROBE = 0,
  39. RATE_LIMIT_PROBE,
  40. ANNOUNCE,
  41. MONITOR,
  42. DEFEND,
  43. DISABLED
  44. } state = DISABLED;
  45. static struct in_addr ip;
  46. static int timeout_ms = -1;
  47. static unsigned deadline_ms;
  48. static unsigned conflicts;
  49. static unsigned nprobes;
  50. static unsigned nclaims;
  51. static int ready;
  52. static unsigned int seed;
  53. static void link_local_timeout(void);
  54. /**
  55. * Pick a random link local IP address on 169.254/16, except that
  56. * the first and last 256 addresses are reserved.
  57. */
  58. static struct in_addr pick(void)
  59. {
  60. unsigned tmp;
  61. struct in_addr ip;
  62. do {
  63. tmp = rand_r(&seed) & IN_CLASSB_HOST;
  64. } while (tmp > (IN_CLASSB_HOST - 0x0200));
  65. ip.s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
  66. return ip;
  67. }
  68. /**
  69. * Return milliseconds of random delay, up to "secs" seconds.
  70. */
  71. static inline unsigned random_delay_ms(unsigned secs)
  72. {
  73. return rand_r(&seed) % (secs * 1000);
  74. }
  75. static void configure_wait(void)
  76. {
  77. if (timeout_ms == -1)
  78. return;
  79. /* poll, being ready to adjust current timeout */
  80. if (!timeout_ms)
  81. timeout_ms = random_delay_ms(PROBE_WAIT);
  82. /* set deadline_ms to the point in time when we timeout */
  83. deadline_ms = MONOTONIC_MS() + timeout_ms;
  84. debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n",
  85. timeout_ms, eth_get_name(), nprobes, nclaims);
  86. net_set_timeout_handler(timeout_ms, link_local_timeout);
  87. }
  88. void link_local_start(void)
  89. {
  90. ip = getenv_ip("llipaddr");
  91. if (ip.s_addr != 0 &&
  92. (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
  93. puts("invalid link address");
  94. net_set_state(NETLOOP_FAIL);
  95. return;
  96. }
  97. net_netmask.s_addr = IN_CLASSB_NET;
  98. seed = seed_mac();
  99. if (ip.s_addr == 0)
  100. ip = pick();
  101. state = PROBE;
  102. timeout_ms = 0;
  103. conflicts = 0;
  104. nprobes = 0;
  105. nclaims = 0;
  106. ready = 0;
  107. configure_wait();
  108. }
  109. static void link_local_timeout(void)
  110. {
  111. switch (state) {
  112. case PROBE:
  113. /* timeouts in the PROBE state mean no conflicting ARP packets
  114. have been received, so we can progress through the states */
  115. if (nprobes < PROBE_NUM) {
  116. struct in_addr zero_ip = {.s_addr = 0};
  117. nprobes++;
  118. debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n",
  119. nprobes, eth_get_name(), &ip);
  120. arp_raw_request(zero_ip, net_null_ethaddr, ip);
  121. timeout_ms = PROBE_MIN * 1000;
  122. timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
  123. } else {
  124. /* Switch to announce state */
  125. state = ANNOUNCE;
  126. nclaims = 0;
  127. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  128. nclaims, eth_get_name(), &ip);
  129. arp_raw_request(ip, net_ethaddr, ip);
  130. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  131. }
  132. break;
  133. case RATE_LIMIT_PROBE:
  134. /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting
  135. ARP packets have been received, so we can move immediately
  136. to the announce state */
  137. state = ANNOUNCE;
  138. nclaims = 0;
  139. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  140. nclaims, eth_get_name(), &ip);
  141. arp_raw_request(ip, net_ethaddr, ip);
  142. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  143. break;
  144. case ANNOUNCE:
  145. /* timeouts in the ANNOUNCE state mean no conflicting ARP
  146. packets have been received, so we can progress through
  147. the states */
  148. if (nclaims < ANNOUNCE_NUM) {
  149. nclaims++;
  150. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  151. nclaims, eth_get_name(), &ip);
  152. arp_raw_request(ip, net_ethaddr, ip);
  153. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  154. } else {
  155. /* Switch to monitor state */
  156. state = MONITOR;
  157. printf("Successfully assigned %pI4\n", &ip);
  158. net_copy_ip(&net_ip, &ip);
  159. ready = 1;
  160. conflicts = 0;
  161. timeout_ms = -1;
  162. /* Never timeout in the monitor state */
  163. net_set_timeout_handler(0, NULL);
  164. /* NOTE: all other exit paths should deconfig ... */
  165. net_set_state(NETLOOP_SUCCESS);
  166. return;
  167. }
  168. break;
  169. case DEFEND:
  170. /* We won! No ARP replies, so just go back to monitor */
  171. state = MONITOR;
  172. timeout_ms = -1;
  173. conflicts = 0;
  174. break;
  175. default:
  176. /* Invalid, should never happen. Restart the whole protocol */
  177. state = PROBE;
  178. ip = pick();
  179. timeout_ms = 0;
  180. nprobes = 0;
  181. nclaims = 0;
  182. break;
  183. }
  184. configure_wait();
  185. }
  186. void link_local_receive_arp(struct arp_hdr *arp, int len)
  187. {
  188. int source_ip_conflict;
  189. int target_ip_conflict;
  190. struct in_addr null_ip = {.s_addr = 0};
  191. if (state == DISABLED)
  192. return;
  193. /* We need to adjust the timeout in case we didn't receive a
  194. conflicting packet. */
  195. if (timeout_ms > 0) {
  196. unsigned diff = deadline_ms - MONOTONIC_MS();
  197. if ((int)(diff) < 0) {
  198. /* Current time is greater than the expected timeout
  199. time. This should never happen */
  200. debug_cond(DEBUG_LL_STATE,
  201. "missed an expected timeout\n");
  202. timeout_ms = 0;
  203. } else {
  204. debug_cond(DEBUG_INT_STATE, "adjusting timeout\n");
  205. timeout_ms = diff | 1; /* never 0 */
  206. }
  207. }
  208. #if 0
  209. /* XXX Don't bother with ethernet link just yet */
  210. if ((fds[0].revents & POLLIN) == 0) {
  211. if (fds[0].revents & POLLERR) {
  212. /*
  213. * FIXME: links routinely go down;
  214. */
  215. bb_error_msg("iface %s is down", eth_get_name());
  216. if (ready)
  217. run(argv, "deconfig", &ip);
  218. return EXIT_FAILURE;
  219. }
  220. continue;
  221. }
  222. #endif
  223. debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n",
  224. eth_get_name(), ntohs(arp->ar_pro),
  225. ntohs(arp->ar_op));
  226. debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n",
  227. &arp->ar_sha,
  228. &arp->ar_spa);
  229. debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n",
  230. &arp->ar_tha,
  231. &arp->ar_tpa);
  232. if (arp->ar_op != htons(ARPOP_REQUEST) &&
  233. arp->ar_op != htons(ARPOP_REPLY)) {
  234. configure_wait();
  235. return;
  236. }
  237. source_ip_conflict = 0;
  238. target_ip_conflict = 0;
  239. if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0 &&
  240. memcmp(&arp->ar_sha, net_ethaddr, ARP_HLEN) != 0)
  241. source_ip_conflict = 1;
  242. /*
  243. * According to RFC 3927, section 2.2.1:
  244. * Check if packet is an ARP probe by checking for a null source IP
  245. * then check that target IP is equal to ours and source hw addr
  246. * is not equal to ours. This condition should cause a conflict only
  247. * during probe.
  248. */
  249. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  250. memcmp(&arp->ar_spa, &null_ip, ARP_PLEN) == 0 &&
  251. memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 &&
  252. memcmp(&arp->ar_sha, net_ethaddr, ARP_HLEN) != 0) {
  253. target_ip_conflict = 1;
  254. }
  255. debug_cond(DEBUG_NET_PKT,
  256. "state = %d, source ip conflict = %d, target ip conflict = "
  257. "%d\n", state, source_ip_conflict, target_ip_conflict);
  258. switch (state) {
  259. case PROBE:
  260. case ANNOUNCE:
  261. /* When probing or announcing, check for source IP conflicts
  262. and other hosts doing ARP probes (target IP conflicts). */
  263. if (source_ip_conflict || target_ip_conflict) {
  264. conflicts++;
  265. state = PROBE;
  266. if (conflicts >= MAX_CONFLICTS) {
  267. debug("%s ratelimit\n", eth_get_name());
  268. timeout_ms = RATE_LIMIT_INTERVAL * 1000;
  269. state = RATE_LIMIT_PROBE;
  270. }
  271. /* restart the whole protocol */
  272. ip = pick();
  273. timeout_ms = 0;
  274. nprobes = 0;
  275. nclaims = 0;
  276. }
  277. break;
  278. case MONITOR:
  279. /* If a conflict, we try to defend with a single ARP probe */
  280. if (source_ip_conflict) {
  281. debug("monitor conflict -- defending\n");
  282. state = DEFEND;
  283. timeout_ms = DEFEND_INTERVAL * 1000;
  284. arp_raw_request(ip, net_ethaddr, ip);
  285. }
  286. break;
  287. case DEFEND:
  288. /* Well, we tried. Start over (on conflict) */
  289. if (source_ip_conflict) {
  290. state = PROBE;
  291. debug("defend conflict -- starting over\n");
  292. ready = 0;
  293. net_ip.s_addr = 0;
  294. /* restart the whole protocol */
  295. ip = pick();
  296. timeout_ms = 0;
  297. nprobes = 0;
  298. nclaims = 0;
  299. }
  300. break;
  301. default:
  302. /* Invalid, should never happen. Restart the whole protocol */
  303. debug("invalid state -- starting over\n");
  304. state = PROBE;
  305. ip = pick();
  306. timeout_ms = 0;
  307. nprobes = 0;
  308. nclaims = 0;
  309. break;
  310. }
  311. configure_wait();
  312. }