]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - net/link_local.c
dm: Adjust lists_bind_fdt() to return the bound device
[karo-tx-uboot.git] / net / link_local.c
index dde96aec3ff32b6518074afdf5b25d2b4872bbf6..4152fae5bacba34c051bf2f877a11d435630325e 100644 (file)
@@ -56,6 +56,7 @@ static unsigned conflicts;
 static unsigned nprobes;
 static unsigned nclaims;
 static int ready;
+static unsigned int seed;
 
 static void link_local_timeout(void);
 
@@ -68,7 +69,7 @@ static IPaddr_t pick(void)
        unsigned tmp;
 
        do {
-               tmp = rand() & IN_CLASSB_HOST;
+               tmp = rand_r(&seed) & IN_CLASSB_HOST;
        } while (tmp > (IN_CLASSB_HOST - 0x0200));
        return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
 }
@@ -78,7 +79,7 @@ static IPaddr_t pick(void)
  */
 static inline unsigned random_delay_ms(unsigned secs)
 {
-       return rand() % (secs * 1000);
+       return rand_r(&seed) % (secs * 1000);
 }
 
 static void configure_wait(void)
@@ -93,7 +94,7 @@ static void configure_wait(void)
        /* set deadline_ms to the point in time when we timeout */
        deadline_ms = MONOTONIC_MS() + timeout_ms;
 
-       debug("...wait %d %s nprobes=%u, nclaims=%u\n",
+       debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n",
                        timeout_ms, eth_get_name(), nprobes, nclaims);
 
        NetSetTimeout(timeout_ms, link_local_timeout);
@@ -102,14 +103,14 @@ static void configure_wait(void)
 void link_local_start(void)
 {
        ip = getenv_IPaddr("llipaddr");
-       if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
+       if (ip != 0 && (ntohl(ip) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
                puts("invalid link address");
                net_set_state(NETLOOP_FAIL);
                return;
        }
        NetOurSubnetMask = IN_CLASSB_NET;
 
-       srand_mac();
+       seed = seed_mac();
        if (ip == 0)
                ip = pick();
 
@@ -131,7 +132,7 @@ static void link_local_timeout(void)
                   have been received, so we can progress through the states */
                if (nprobes < PROBE_NUM) {
                        nprobes++;
-                       debug("probe/%u %s@%pI4\n",
+                       debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n",
                                        nprobes, eth_get_name(), &ip);
                        arp_raw_request(0, NetEtherNullAddr, ip);
                        timeout_ms = PROBE_MIN * 1000;
@@ -140,7 +141,7 @@ static void link_local_timeout(void)
                        /* Switch to announce state */
                        state = ANNOUNCE;
                        nclaims = 0;
-                       debug("announce/%u %s@%pI4\n",
+                       debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
                                        nclaims, eth_get_name(), &ip);
                        arp_raw_request(ip, NetOurEther, ip);
                        timeout_ms = ANNOUNCE_INTERVAL * 1000;
@@ -152,7 +153,7 @@ static void link_local_timeout(void)
                   to the announce state */
                state = ANNOUNCE;
                nclaims = 0;
-               debug("announce/%u %s@%pI4\n",
+               debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
                                nclaims, eth_get_name(), &ip);
                arp_raw_request(ip, NetOurEther, ip);
                timeout_ms = ANNOUNCE_INTERVAL * 1000;
@@ -163,7 +164,7 @@ static void link_local_timeout(void)
                   the states */
                if (nclaims < ANNOUNCE_NUM) {
                        nclaims++;
-                       debug("announce/%u %s@%pI4\n",
+                       debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n",
                                        nclaims, eth_get_name(), &ip);
                        arp_raw_request(ip, NetOurEther, ip);
                        timeout_ms = ANNOUNCE_INTERVAL * 1000;
@@ -205,6 +206,7 @@ void link_local_receive_arp(struct arp_hdr *arp, int len)
 {
        int source_ip_conflict;
        int target_ip_conflict;
+       IPaddr_t null_ip = 0;
 
        if (state == DISABLED)
                return;
@@ -216,19 +218,21 @@ void link_local_receive_arp(struct arp_hdr *arp, int len)
                if ((int)(diff) < 0) {
                        /* Current time is greater than the expected timeout
                           time. This should never happen */
-                       debug("missed an expected timeout\n");
+                       debug_cond(DEBUG_LL_STATE,
+                               "missed an expected timeout\n");
                        timeout_ms = 0;
                } else {
-                       debug("adjusting timeout\n");
+                       debug_cond(DEBUG_INT_STATE, "adjusting timeout\n");
                        timeout_ms = diff | 1; /* never 0 */
                }
        }
-/*
- * XXX Don't bother with ethernet link just yet
+#if 0
+ /* XXX Don't bother with ethernet link just yet */
        if ((fds[0].revents & POLLIN) == 0) {
                if (fds[0].revents & POLLERR) {
-                       // FIXME: links routinely go down;
-                       // this shouldn't necessarily exit.
+                       /*
+                        * FIXME: links routinely go down;
+                        */
                        bb_error_msg("iface %s is down", eth_get_name());
                        if (ready) {
                                run(argv, "deconfig", &ip);
@@ -237,15 +241,15 @@ void link_local_receive_arp(struct arp_hdr *arp, int len)
                }
                continue;
        }
-*/
+#endif
 
-       debug("%s recv arp type=%d, op=%d,\n",
+       debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n",
                eth_get_name(), ntohs(arp->ar_pro),
                ntohs(arp->ar_op));
-       debug("\tsource=%pM %pI4\n",
+       debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n",
                &arp->ar_sha,
                &arp->ar_spa);
-       debug("\ttarget=%pM %pI4\n",
+       debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n",
                &arp->ar_tha,
                &arp->ar_tpa);
 
@@ -264,15 +268,24 @@ void link_local_receive_arp(struct arp_hdr *arp, int len)
        ) {
                source_ip_conflict = 1;
        }
-       if (arp->ar_op == htons(ARPOP_REQUEST)
-        && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0
-        && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0
-       ) {
+
+       /*
+        * According to RFC 3927, section 2.2.1:
+        * Check if packet is an ARP probe by checking for a null source IP
+        * then check that target IP is equal to ours and source hw addr
+        * is not equal to ours. This condition should cause a conflict only
+        * during probe.
+        */
+       if (arp->ar_op == htons(ARPOP_REQUEST) &&
+           memcmp(&arp->ar_spa, &null_ip, ARP_PLEN) == 0 &&
+           memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 &&
+           memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0) {
                target_ip_conflict = 1;
        }
 
-       debug("state = %d, source ip conflict = %d, target ip conflict = %d\n",
-               state, source_ip_conflict, target_ip_conflict);
+       debug_cond(DEBUG_NET_PKT,
+               "state = %d, source ip conflict = %d, target ip conflict = "
+               "%d\n", state, source_ip_conflict, target_ip_conflict);
        switch (state) {
        case PROBE:
        case ANNOUNCE: