link_local.c 7.6 KB

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