link_local.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 IPaddr_t 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 IPaddr_t pick(void)
  59. {
  60. unsigned tmp;
  61. do {
  62. tmp = rand_r(&seed) & IN_CLASSB_HOST;
  63. } while (tmp > (IN_CLASSB_HOST - 0x0200));
  64. return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
  65. }
  66. /**
  67. * Return milliseconds of random delay, up to "secs" seconds.
  68. */
  69. static inline unsigned random_delay_ms(unsigned secs)
  70. {
  71. return rand_r(&seed) % (secs * 1000);
  72. }
  73. static void configure_wait(void)
  74. {
  75. if (timeout_ms == -1)
  76. return;
  77. /* poll, being ready to adjust current timeout */
  78. if (!timeout_ms)
  79. timeout_ms = random_delay_ms(PROBE_WAIT);
  80. /* set deadline_ms to the point in time when we timeout */
  81. deadline_ms = MONOTONIC_MS() + timeout_ms;
  82. debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n",
  83. timeout_ms, eth_get_name(), nprobes, nclaims);
  84. NetSetTimeout(timeout_ms, link_local_timeout);
  85. }
  86. void link_local_start(void)
  87. {
  88. ip = getenv_IPaddr("llipaddr");
  89. if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
  90. puts("invalid link address");
  91. net_set_state(NETLOOP_FAIL);
  92. return;
  93. }
  94. NetOurSubnetMask = IN_CLASSB_NET;
  95. seed = seed_mac();
  96. if (ip == 0)
  97. ip = pick();
  98. state = PROBE;
  99. timeout_ms = 0;
  100. conflicts = 0;
  101. nprobes = 0;
  102. nclaims = 0;
  103. ready = 0;
  104. configure_wait();
  105. }
  106. static void link_local_timeout(void)
  107. {
  108. switch (state) {
  109. case PROBE:
  110. /* timeouts in the PROBE state mean no conflicting ARP packets
  111. have been received, so we can progress through the states */
  112. if (nprobes < PROBE_NUM) {
  113. nprobes++;
  114. debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n",
  115. nprobes, eth_get_name(), &ip);
  116. arp_raw_request(0, NetEtherNullAddr, ip);
  117. timeout_ms = PROBE_MIN * 1000;
  118. timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
  119. } else {
  120. /* Switch to announce state */
  121. state = ANNOUNCE;
  122. nclaims = 0;
  123. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  124. nclaims, eth_get_name(), &ip);
  125. arp_raw_request(ip, NetOurEther, ip);
  126. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  127. }
  128. break;
  129. case RATE_LIMIT_PROBE:
  130. /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting
  131. ARP packets have been received, so we can move immediately
  132. to the announce state */
  133. state = ANNOUNCE;
  134. nclaims = 0;
  135. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  136. nclaims, eth_get_name(), &ip);
  137. arp_raw_request(ip, NetOurEther, ip);
  138. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  139. break;
  140. case ANNOUNCE:
  141. /* timeouts in the ANNOUNCE state mean no conflicting ARP
  142. packets have been received, so we can progress through
  143. the states */
  144. if (nclaims < ANNOUNCE_NUM) {
  145. nclaims++;
  146. debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
  147. nclaims, eth_get_name(), &ip);
  148. arp_raw_request(ip, NetOurEther, ip);
  149. timeout_ms = ANNOUNCE_INTERVAL * 1000;
  150. } else {
  151. /* Switch to monitor state */
  152. state = MONITOR;
  153. printf("Successfully assigned %pI4\n", &ip);
  154. NetCopyIP(&NetOurIP, &ip);
  155. ready = 1;
  156. conflicts = 0;
  157. timeout_ms = -1;
  158. /* Never timeout in the monitor state */
  159. NetSetTimeout(0, NULL);
  160. /* NOTE: all other exit paths should deconfig ... */
  161. net_set_state(NETLOOP_SUCCESS);
  162. return;
  163. }
  164. break;
  165. case DEFEND:
  166. /* We won! No ARP replies, so just go back to monitor */
  167. state = MONITOR;
  168. timeout_ms = -1;
  169. conflicts = 0;
  170. break;
  171. default:
  172. /* Invalid, should never happen. Restart the whole protocol */
  173. state = PROBE;
  174. ip = pick();
  175. timeout_ms = 0;
  176. nprobes = 0;
  177. nclaims = 0;
  178. break;
  179. }
  180. configure_wait();
  181. }
  182. void link_local_receive_arp(struct arp_hdr *arp, int len)
  183. {
  184. int source_ip_conflict;
  185. int target_ip_conflict;
  186. if (state == DISABLED)
  187. return;
  188. /* We need to adjust the timeout in case we didn't receive a
  189. conflicting packet. */
  190. if (timeout_ms > 0) {
  191. unsigned diff = deadline_ms - MONOTONIC_MS();
  192. if ((int)(diff) < 0) {
  193. /* Current time is greater than the expected timeout
  194. time. This should never happen */
  195. debug_cond(DEBUG_LL_STATE,
  196. "missed an expected timeout\n");
  197. timeout_ms = 0;
  198. } else {
  199. debug_cond(DEBUG_INT_STATE, "adjusting timeout\n");
  200. timeout_ms = diff | 1; /* never 0 */
  201. }
  202. }
  203. #if 0
  204. /* XXX Don't bother with ethernet link just yet */
  205. if ((fds[0].revents & POLLIN) == 0) {
  206. if (fds[0].revents & POLLERR) {
  207. /*
  208. * FIXME: links routinely go down;
  209. */
  210. bb_error_msg("iface %s is down", eth_get_name());
  211. if (ready) {
  212. run(argv, "deconfig", &ip);
  213. }
  214. return EXIT_FAILURE;
  215. }
  216. continue;
  217. }
  218. #endif
  219. debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n",
  220. eth_get_name(), ntohs(arp->ar_pro),
  221. ntohs(arp->ar_op));
  222. debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n",
  223. &arp->ar_sha,
  224. &arp->ar_spa);
  225. debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n",
  226. &arp->ar_tha,
  227. &arp->ar_tpa);
  228. if (arp->ar_op != htons(ARPOP_REQUEST)
  229. && arp->ar_op != htons(ARPOP_REPLY)
  230. ) {
  231. configure_wait();
  232. return;
  233. }
  234. source_ip_conflict = 0;
  235. target_ip_conflict = 0;
  236. if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0
  237. && memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0
  238. ) {
  239. source_ip_conflict = 1;
  240. }
  241. if (arp->ar_op == htons(ARPOP_REQUEST)
  242. && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0
  243. && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0
  244. ) {
  245. target_ip_conflict = 1;
  246. }
  247. debug_cond(DEBUG_NET_PKT,
  248. "state = %d, source ip conflict = %d, target ip conflict = "
  249. "%d\n", state, source_ip_conflict, target_ip_conflict);
  250. switch (state) {
  251. case PROBE:
  252. case ANNOUNCE:
  253. /* When probing or announcing, check for source IP conflicts
  254. and other hosts doing ARP probes (target IP conflicts). */
  255. if (source_ip_conflict || target_ip_conflict) {
  256. conflicts++;
  257. state = PROBE;
  258. if (conflicts >= MAX_CONFLICTS) {
  259. debug("%s ratelimit\n", eth_get_name());
  260. timeout_ms = RATE_LIMIT_INTERVAL * 1000;
  261. state = RATE_LIMIT_PROBE;
  262. }
  263. /* restart the whole protocol */
  264. ip = pick();
  265. timeout_ms = 0;
  266. nprobes = 0;
  267. nclaims = 0;
  268. }
  269. break;
  270. case MONITOR:
  271. /* If a conflict, we try to defend with a single ARP probe */
  272. if (source_ip_conflict) {
  273. debug("monitor conflict -- defending\n");
  274. state = DEFEND;
  275. timeout_ms = DEFEND_INTERVAL * 1000;
  276. arp_raw_request(ip, NetOurEther, ip);
  277. }
  278. break;
  279. case DEFEND:
  280. /* Well, we tried. Start over (on conflict) */
  281. if (source_ip_conflict) {
  282. state = PROBE;
  283. debug("defend conflict -- starting over\n");
  284. ready = 0;
  285. NetOurIP = 0;
  286. /* restart the whole protocol */
  287. ip = pick();
  288. timeout_ms = 0;
  289. nprobes = 0;
  290. nclaims = 0;
  291. }
  292. break;
  293. default:
  294. /* Invalid, should never happen. Restart the whole protocol */
  295. debug("invalid state -- starting over\n");
  296. state = PROBE;
  297. ip = pick();
  298. timeout_ms = 0;
  299. nprobes = 0;
  300. nclaims = 0;
  301. break;
  302. }
  303. configure_wait();
  304. }