]> git.sur5r.net Git - u-boot/blobdiff - net/net.c
mxc_fec: fix some erroneous PHY accesses.
[u-boot] / net / net.c
index 7ce947db3003b4d9de05e7f749983655f371150c..cab4b2dd885cde6b952a6ba8737e2f1913ca073f 100644 (file)
--- a/net/net.c
+++ b/net/net.c
@@ -113,10 +113,6 @@ DECLARE_GLOBAL_DATA_PTR;
 # define ARP_TIMEOUT_COUNT     CONFIG_NET_RETRY_COUNT
 #endif
 
-#if 0
-#define ET_DEBUG
-#endif
-
 /** BOOTP EXTENTIONS **/
 
 IPaddr_t       NetOurSubnetMask=0;             /* Our subnet mask (0=unknown)  */
@@ -218,9 +214,8 @@ void ArpRequest (void)
        volatile uchar *pkt;
        ARP_t *arp;
 
-#ifdef ET_DEBUG
-       printf ("ARP broadcast %d\n", NetArpWaitTry);
-#endif
+       debug("ARP broadcast %d\n", NetArpWaitTry);
+
        pkt = NetTxPacket;
 
        pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
@@ -644,9 +639,8 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
        /* if MAC address was not discovered yet, save the packet and do an ARP request */
        if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
 
-#ifdef ET_DEBUG
-               printf("sending ARP for %08lx\n", dest);
-#endif
+               debug("sending ARP for %08lx\n", dest);
+
                NetArpWaitPacketIP = dest;
                NetArpWaitPacketMAC = ether;
 
@@ -666,9 +660,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
                return 1;       /* waiting */
        }
 
-#ifdef ET_DEBUG
-       printf("sending UDP to %08lx/%pM\n", dest, ether);
-#endif
+       debug("sending UDP to %08lx/%pM\n", dest, ether);
 
        pkt = (uchar *)NetTxPacket;
        pkt += NetSetEther (pkt, ether, PROT_IP);
@@ -692,9 +684,7 @@ int PingSend(void)
 
        memcpy(mac, NetEtherNullAddr, 6);
 
-#ifdef ET_DEBUG
-       printf("sending ARP for %08lx\n", NetPingIP);
-#endif
+       debug("sending ARP for %08lx\n", NetPingIP);
 
        NetArpWaitPacketIP = NetPingIP;
        NetArpWaitPacketMAC = mac;
@@ -1117,6 +1107,176 @@ static void CDPStart(void)
 }
 #endif
 
+#ifdef CONFIG_IP_DEFRAG
+/*
+ * This function collects fragments in a single packet, according
+ * to the algorithm in RFC815. It returns NULL or the pointer to
+ * a complete packet, in static storage
+ */
+#ifndef CONFIG_NET_MAXDEFRAG
+#define CONFIG_NET_MAXDEFRAG 16384
+#endif
+/*
+ * MAXDEFRAG, above, is chosen in the config file and  is real data
+ * so we need to add the NFS overhead, which is more than TFTP.
+ * To use sizeof in the internal unnamed structures, we need a real
+ * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
+ * The compiler doesn't complain nor allocates the actual structure
+ */
+static struct rpc_t rpc_specimen;
+#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
+
+#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
+
+/*
+ * this is the packet being assembled, either data or frag control.
+ * Fragments go by 8 bytes, so this union must be 8 bytes long
+ */
+struct hole {
+       /* first_byte is address of this structure */
+       u16 last_byte;  /* last byte in this hole + 1 (begin of next hole) */
+       u16 next_hole;  /* index of next (in 8-b blocks), 0 == none */
+       u16 prev_hole;  /* index of prev, 0 == none */
+       u16 unused;
+};
+
+static IP_t *__NetDefragment(IP_t *ip, int *lenp)
+{
+       static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
+       static u16 first_hole, total_len;
+       struct hole *payload, *thisfrag, *h, *newh;
+       IP_t *localip = (IP_t *)pkt_buff;
+       uchar *indata = (uchar *)ip;
+       int offset8, start, len, done = 0;
+       u16 ip_off = ntohs(ip->ip_off);
+
+       /* payload starts after IP header, this fragment is in there */
+       payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
+       offset8 =  (ip_off & IP_OFFS);
+       thisfrag = payload + offset8;
+       start = offset8 * 8;
+       len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
+
+       if (start + len > IP_MAXUDP) /* fragment extends too far */
+               return NULL;
+
+       if (!total_len || localip->ip_id != ip->ip_id) {
+               /* new (or different) packet, reset structs */
+               total_len = 0xffff;
+               payload[0].last_byte = ~0;
+               payload[0].next_hole = 0;
+               payload[0].prev_hole = 0;
+               first_hole = 0;
+               /* any IP header will work, copy the first we received */
+               memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
+       }
+
+       /*
+        * What follows is the reassembly algorithm. We use the payload
+        * array as a linked list of hole descriptors, as each hole starts
+        * at a multiple of 8 bytes. However, last byte can be whatever value,
+        * so it is represented as byte count, not as 8-byte blocks.
+        */
+
+       h = payload + first_hole;
+       while (h->last_byte < start) {
+               if (!h->next_hole) {
+                       /* no hole that far away */
+                       return NULL;
+               }
+               h = payload + h->next_hole;
+       }
+
+       if (offset8 + (len / 8) <= h - payload) {
+               /* no overlap with holes (dup fragment?) */
+               return NULL;
+       }
+
+       if (!(ip_off & IP_FLAGS_MFRAG)) {
+               /* no more fragmentss: truncate this (last) hole */
+               total_len = start + len;
+               h->last_byte = start + len;
+       }
+
+       /*
+        * There is some overlap: fix the hole list. This code doesn't
+        * deal with a fragment that overlaps with two different holes
+        * (thus being a superset of a previously-received fragment).
+        */
+
+       if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
+               /* complete overlap with hole: remove hole */
+               if (!h->prev_hole && !h->next_hole) {
+                       /* last remaining hole */
+                       done = 1;
+               } else if (!h->prev_hole) {
+                       /* first hole */
+                       first_hole = h->next_hole;
+                       payload[h->next_hole].prev_hole = 0;
+               } else if (!h->next_hole) {
+                       /* last hole */
+                       payload[h->prev_hole].next_hole = 0;
+               } else {
+                       /* in the middle of the list */
+                       payload[h->next_hole].prev_hole = h->prev_hole;
+                       payload[h->prev_hole].next_hole = h->next_hole;
+               }
+
+       } else if (h->last_byte <= start + len) {
+               /* overlaps with final part of the hole: shorten this hole */
+               h->last_byte = start;
+
+       } else if (h >= thisfrag) {
+               /* overlaps with initial part of the hole: move this hole */
+               newh = thisfrag + (len / 8);
+               *newh = *h;
+               h = newh;
+               if (h->next_hole)
+                       payload[h->next_hole].prev_hole = (h - payload);
+               if (h->prev_hole)
+                       payload[h->prev_hole].next_hole = (h - payload);
+               else
+                       first_hole = (h - payload);
+
+       } else {
+               /* fragment sits in the middle: split the hole */
+               newh = thisfrag + (len / 8);
+               *newh = *h;
+               h->last_byte = start;
+               h->next_hole = (newh - payload);
+               newh->prev_hole = (h - payload);
+               if (newh->next_hole)
+                       payload[newh->next_hole].prev_hole = (newh - payload);
+       }
+
+       /* finally copy this fragment and possibly return whole packet */
+       memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
+       if (!done)
+               return NULL;
+
+       localip->ip_len = htons(total_len);
+       *lenp = total_len + IP_HDR_SIZE_NO_UDP;
+       return localip;
+}
+
+static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
+{
+       u16 ip_off = ntohs(ip->ip_off);
+       if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
+               return ip; /* not a fragment */
+       return __NetDefragment(ip, lenp);
+}
+
+#else /* !CONFIG_IP_DEFRAG */
+
+static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
+{
+       u16 ip_off = ntohs(ip->ip_off);
+       if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
+               return ip; /* not a fragment */
+       return NULL;
+}
+#endif
 
 void
 NetReceive(volatile uchar * inpkt, int len)
@@ -1132,9 +1292,7 @@ NetReceive(volatile uchar * inpkt, int len)
 #endif
        ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
 
-#ifdef ET_DEBUG
-       printf("packet received\n");
-#endif
+       debug("packet received\n");
 
        NetRxPacket = inpkt;
        NetRxPacketLen = len;
@@ -1165,9 +1323,7 @@ NetReceive(volatile uchar * inpkt, int len)
 
        x = ntohs(et->et_protlen);
 
-#ifdef ET_DEBUG
-       printf("packet received\n");
-#endif
+       debug("packet received\n");
 
        if (x < 1514) {
                /*
@@ -1185,9 +1341,8 @@ NetReceive(volatile uchar * inpkt, int len)
        } else {                        /* VLAN packet */
                VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
 
-#ifdef ET_DEBUG
-               printf("VLAN packet received\n");
-#endif
+               debug("VLAN packet received\n");
+
                /* too small packet? */
                if (len < VLAN_ETHER_HDR_SIZE)
                        return;
@@ -1208,9 +1363,7 @@ NetReceive(volatile uchar * inpkt, int len)
                len -= VLAN_ETHER_HDR_SIZE;
        }
 
-#ifdef ET_DEBUG
-       printf("Receive from protocol 0x%x\n", x);
-#endif
+       debug("Receive from protocol 0x%x\n", x);
 
 #if defined(CONFIG_CMD_CDP)
        if (iscdp) {
@@ -1239,9 +1392,8 @@ NetReceive(volatile uchar * inpkt, int len)
                 *   address; so if we receive such a packet, we set
                 *   the server ethernet address
                 */
-#ifdef ET_DEBUG
-               puts ("Got ARP\n");
-#endif
+               debug("Got ARP\n");
+
                arp = (ARP_t *)ip;
                if (len < ARP_HDR_SIZE) {
                        printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
@@ -1270,9 +1422,7 @@ NetReceive(volatile uchar * inpkt, int len)
 
                switch (ntohs(arp->ar_op)) {
                case ARPOP_REQUEST:             /* reply with our IP address    */
-#ifdef ET_DEBUG
-                       puts ("Got ARP REQUEST, return our IP\n");
-#endif
+                       debug("Got ARP REQUEST, return our IP\n");
                        pkt = (uchar *)et;
                        pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
                        arp->ar_op = htons(ARPOP_REPLY);
@@ -1287,18 +1437,23 @@ NetReceive(volatile uchar * inpkt, int len)
                        /* are we waiting for a reply */
                        if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
                                break;
-#ifdef ET_DEBUG
-                       printf("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
-                               arp->ar_data);
+
+#ifdef CONFIG_KEEP_SERVERADDR
+                       if (NetServerIP == NetArpWaitPacketIP) {
+                               char buf[20];
+                               sprintf(buf, "%pM", arp->ar_data);
+                               setenv("serveraddr", buf);
+                       }
 #endif
 
+                       debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
+                               arp->ar_data);
+
                        tmp = NetReadIP(&arp->ar_data[6]);
 
                        /* matched waiting packet's address */
                        if (tmp == NetArpWaitReplyIP) {
-#ifdef ET_DEBUG
-                               puts ("Got it\n");
-#endif
+                               debug("Got it\n");
                                /* save address for later use */
                                memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
 
@@ -1317,17 +1472,13 @@ NetReceive(volatile uchar * inpkt, int len)
                        }
                        return;
                default:
-#ifdef ET_DEBUG
-                       printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
-#endif
+                       debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
                        return;
                }
                break;
 
        case PROT_RARP:
-#ifdef ET_DEBUG
-               puts ("Got RARP\n");
-#endif
+               debug("Got RARP\n");
                arp = (ARP_t *)ip;
                if (len < ARP_HDR_SIZE) {
                        printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
@@ -1351,36 +1502,34 @@ NetReceive(volatile uchar * inpkt, int len)
                break;
 
        case PROT_IP:
-#ifdef ET_DEBUG
-               puts ("Got IP\n");
-#endif
+               debug("Got IP\n");
+               /* Before we start poking the header, make sure it is there */
                if (len < IP_HDR_SIZE) {
-                       debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
+                       debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
                        return;
                }
+               /* Check the packet length */
                if (len < ntohs(ip->ip_len)) {
                        printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
                        return;
                }
                len = ntohs(ip->ip_len);
-#ifdef ET_DEBUG
-               printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
-#endif
+               debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
+
+               /* Can't deal with anything except IPv4 */
                if ((ip->ip_hl_v & 0xf0) != 0x40) {
                        return;
                }
-               /* Can't deal with fragments */
-               if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
-                       return;
-               }
-               /* can't deal with headers > 20 bytes */
+               /* Can't deal with IP options (headers != 20 bytes) */
                if ((ip->ip_hl_v & 0x0f) > 0x05) {
                        return;
                }
+               /* Check the Checksum of the header */
                if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
                        puts ("checksum bad\n");
                        return;
                }
+               /* If it is not for us, ignore it */
                tmp = NetReadIP(&ip->ip_dst);
                if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
 #ifdef CONFIG_MCAST_TFTP
@@ -1388,6 +1537,13 @@ NetReceive(volatile uchar * inpkt, int len)
 #endif
                        return;
                }
+               /*
+                * The function returns the unchanged packet if it's not
+                * a fragment, and either the complete packet or NULL if
+                * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
+                */
+               if (!(ip = NetDefragment(ip, &len)))
+                       return;
                /*
                 * watch for ICMP host redirects
                 *
@@ -1423,10 +1579,9 @@ NetReceive(volatile uchar * inpkt, int len)
                                (*packetHandler)((uchar *)ip, 0, 0, 0);
                                return;
                        case ICMP_ECHO_REQUEST:
-#ifdef ET_DEBUG
-                               printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
+                               debug("Got ICMP ECHO REQUEST, return %d bytes \n",
                                        ETHER_HDR_SIZE + len);
-#endif
+
                                memcpy (&et->et_dest[0], &et->et_src[0], 6);
                                memcpy (&et->et_src[ 0], NetOurEther, 6);