link_local.c 8.5 KB

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