wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copied from Linux Monitor (LiMon) - Networking. |
| 3 | * |
| 4 | * Copyright 1994 - 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2002 Wolfgang Denk, wd@denx.de |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * General Desription: |
| 13 | * |
| 14 | * The user interface supports commands for BOOTP, RARP, and TFTP. |
| 15 | * Also, we support ARP internally. Depending on available data, |
| 16 | * these interact as follows: |
| 17 | * |
| 18 | * BOOTP: |
| 19 | * |
| 20 | * Prerequisites: - own ethernet address |
| 21 | * We want: - own IP address |
| 22 | * - TFTP server IP address |
| 23 | * - name of bootfile |
| 24 | * Next step: ARP |
| 25 | * |
| 26 | * RARP: |
| 27 | * |
| 28 | * Prerequisites: - own ethernet address |
| 29 | * We want: - own IP address |
| 30 | * - TFTP server IP address |
| 31 | * Next step: ARP |
| 32 | * |
| 33 | * ARP: |
| 34 | * |
| 35 | * Prerequisites: - own ethernet address |
| 36 | * - own IP address |
| 37 | * - TFTP server IP address |
| 38 | * We want: - TFTP server ethernet address |
| 39 | * Next step: TFTP |
| 40 | * |
| 41 | * DHCP: |
| 42 | * |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 43 | * Prerequisites: - own ethernet address |
| 44 | * We want: - IP, Netmask, ServerIP, Gateway IP |
| 45 | * - bootfilename, lease time |
| 46 | * Next step: - TFTP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 47 | * |
| 48 | * TFTP: |
| 49 | * |
| 50 | * Prerequisites: - own ethernet address |
| 51 | * - own IP address |
| 52 | * - TFTP server IP address |
| 53 | * - TFTP server ethernet address |
| 54 | * - name of bootfile (if unknown, we use a default name |
| 55 | * derived from our own IP address) |
| 56 | * We want: - load the boot file |
| 57 | * Next step: none |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 58 | * |
| 59 | * NFS: |
| 60 | * |
| 61 | * Prerequisites: - own ethernet address |
| 62 | * - own IP address |
| 63 | * - name of bootfile (if unknown, we use a default name |
| 64 | * derived from our own IP address) |
| 65 | * We want: - load the boot file |
| 66 | * Next step: none |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 67 | * |
| 68 | * SNTP: |
| 69 | * |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 70 | * Prerequisites: - own ethernet address |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 71 | * - own IP address |
| 72 | * We want: - network time |
| 73 | * Next step: none |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | |
| 77 | #include <common.h> |
| 78 | #include <watchdog.h> |
| 79 | #include <command.h> |
Joe Hershberger | 48522bb | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 80 | #include <linux/compiler.h> |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 81 | #include <net.h> |
| 82 | #include "bootp.h" |
| 83 | #include "tftp.h" |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 84 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 85 | #include "rarp.h" |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 86 | #endif |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 87 | #include "nfs.h" |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 88 | #ifdef CONFIG_STATUS_LED |
| 89 | #include <status_led.h> |
| 90 | #include <miiphy.h> |
| 91 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 92 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 93 | #include "sntp.h" |
| 94 | #endif |
Joe Hershberger | f575ae1 | 2012-05-23 07:57:59 +0000 | [diff] [blame^] | 95 | #include "cdp.h" |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 96 | #if defined(CONFIG_CMD_DNS) |
| 97 | #include "dns.h" |
| 98 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 99 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 100 | DECLARE_GLOBAL_DATA_PTR; |
| 101 | |
Guennadi Liakhovetski | 40cb90e | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 102 | #ifndef CONFIG_ARP_TIMEOUT |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 103 | /* Milliseconds before trying ARP again */ |
| 104 | # define ARP_TIMEOUT 5000UL |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 105 | #else |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 106 | # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT |
Guennadi Liakhovetski | 40cb90e | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | |
| 110 | #ifndef CONFIG_NET_RETRY_COUNT |
| 111 | # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 112 | #else |
| 113 | # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 114 | #endif |
| 115 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 116 | /** BOOTP EXTENTIONS **/ |
| 117 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 118 | /* Our subnet mask (0=unknown) */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 119 | IPaddr_t NetOurSubnetMask; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 120 | /* Our gateways IP address */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 121 | IPaddr_t NetOurGatewayIP; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 122 | /* Our DNS IP address */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 123 | IPaddr_t NetOurDNSIP; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 124 | #if defined(CONFIG_BOOTP_DNS2) |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 125 | /* Our 2nd DNS IP address */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 126 | IPaddr_t NetOurDNS2IP; |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 127 | #endif |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 128 | /* Our NIS domain */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 129 | char NetOurNISDomain[32] = {0,}; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 130 | /* Our hostname */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 131 | char NetOurHostName[32] = {0,}; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 132 | /* Our bootpath */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 133 | char NetOurRootPath[64] = {0,}; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 134 | /* Our bootfile size in blocks */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 135 | ushort NetBootFileSize; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 136 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 137 | #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */ |
| 138 | IPaddr_t Mcast_addr; |
| 139 | #endif |
| 140 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 141 | /** END OF BOOTP EXTENTIONS **/ |
| 142 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 143 | /* The actual transferred size of the bootfile (in bytes) */ |
| 144 | ulong NetBootFileXferSize; |
| 145 | /* Our ethernet address */ |
| 146 | uchar NetOurEther[6]; |
| 147 | /* Boot server enet address */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 148 | uchar NetServerEther[6]; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 149 | /* Our IP addr (0 = unknown) */ |
| 150 | IPaddr_t NetOurIP; |
| 151 | /* Server IP addr (0 = unknown) */ |
| 152 | IPaddr_t NetServerIP; |
| 153 | /* Current receive packet */ |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 154 | uchar *NetRxPacket; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 155 | /* Current rx packet length */ |
| 156 | int NetRxPacketLen; |
| 157 | /* IP packet ID */ |
| 158 | unsigned NetIPID; |
| 159 | /* Ethernet bcast address */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 160 | uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 161 | uchar NetEtherNullAddr[6]; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 162 | #ifdef CONFIG_API |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 163 | void (*push_packet)(void *, int len) = 0; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 164 | #endif |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 165 | /* Network loop state */ |
| 166 | int NetState; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 167 | /* Tried all network devices */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 168 | int NetRestartWrap; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 169 | /* Network loop restarted */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 170 | static int NetRestarted; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 171 | /* At least one device configured */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 172 | static int NetDevExists; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 173 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 174 | /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 175 | /* default is without VLAN */ |
| 176 | ushort NetOurVLAN = 0xFFFF; |
| 177 | /* ditto */ |
| 178 | ushort NetOurNativeVLAN = 0xFFFF; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 179 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 180 | /* Boot File name */ |
| 181 | char BootFile[128]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 182 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 183 | #if defined(CONFIG_CMD_PING) |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 184 | /* the ip address to ping */ |
| 185 | IPaddr_t NetPingIP; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 186 | |
| 187 | static void PingStart(void); |
| 188 | #endif |
| 189 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 190 | #if defined(CONFIG_CMD_SNTP) |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 191 | /* NTP server IP address */ |
| 192 | IPaddr_t NetNtpServerIP; |
| 193 | /* offset time from UTC */ |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 194 | int NetTimeOffset; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 197 | uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 198 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 199 | /* Receive packet */ |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 200 | uchar *NetRxPackets[PKTBUFSRX]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 201 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 202 | /* Current RX packet handler */ |
| 203 | static rxhand_f *packetHandler; |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 204 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 205 | static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */ |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 206 | #endif |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 207 | /* Current timeout handler */ |
| 208 | static thand_f *timeHandler; |
| 209 | /* Time base value */ |
| 210 | static ulong timeStart; |
| 211 | /* Current timeout value */ |
| 212 | static ulong timeDelta; |
| 213 | /* THE transmit packet */ |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 214 | uchar *NetTxPacket; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 215 | |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 216 | static int net_check_prereq(enum proto_t protocol); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 217 | |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 218 | static int NetTryCount; |
| 219 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 220 | /**********************************************************************/ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 221 | |
| 222 | IPaddr_t NetArpWaitPacketIP; |
| 223 | IPaddr_t NetArpWaitReplyIP; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 224 | /* MAC address of waiting packet's destination */ |
| 225 | uchar *NetArpWaitPacketMAC; |
| 226 | /* THE transmit packet */ |
| 227 | uchar *NetArpWaitTxPacket; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 228 | int NetArpWaitTxPacketSize; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 229 | uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 230 | ulong NetArpWaitTimerStart; |
| 231 | int NetArpWaitTry; |
| 232 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 233 | void ArpRequest(void) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 234 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 235 | uchar *pkt; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 236 | ARP_t *arp; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 237 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 238 | debug("ARP broadcast %d\n", NetArpWaitTry); |
| 239 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 240 | pkt = NetTxPacket; |
| 241 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 242 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 243 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 244 | arp = (ARP_t *) pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 245 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 246 | arp->ar_hrd = htons(ARP_ETHER); |
| 247 | arp->ar_pro = htons(PROT_IP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 248 | arp->ar_hln = 6; |
| 249 | arp->ar_pln = 4; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 250 | arp->ar_op = htons(ARPOP_REQUEST); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 251 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 252 | /* source ET addr */ |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 253 | memcpy(&arp->ar_data[0], NetOurEther, 6); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 254 | /* source IP addr */ |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 255 | NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); |
Simon Glass | ed1ada7 | 2011-10-26 14:18:39 +0000 | [diff] [blame] | 256 | /* dest ET addr = 0 */ |
| 257 | memset(&arp->ar_data[10], '\0', 6); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 258 | if ((NetArpWaitPacketIP & NetOurSubnetMask) != |
| 259 | (NetOurIP & NetOurSubnetMask)) { |
| 260 | if (NetOurGatewayIP == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 261 | puts("## Warning: gatewayip needed but not set\n"); |
Wolfgang Denk | d509b81 | 2006-03-12 01:13:30 +0100 | [diff] [blame] | 262 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 263 | } else { |
| 264 | NetArpWaitReplyIP = NetOurGatewayIP; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 265 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 266 | } else { |
| 267 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 268 | } |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 269 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 270 | NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); |
| 271 | (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void ArpTimeoutCheck(void) |
| 275 | { |
| 276 | ulong t; |
| 277 | |
| 278 | if (!NetArpWaitPacketIP) |
| 279 | return; |
| 280 | |
| 281 | t = get_timer(0); |
| 282 | |
| 283 | /* check for arp timeout */ |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 284 | if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 285 | NetArpWaitTry++; |
| 286 | |
| 287 | if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 288 | puts("\nARP Retry count exceeded; starting again\n"); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 289 | NetArpWaitTry = 0; |
| 290 | NetStartAgain(); |
| 291 | } else { |
| 292 | NetArpWaitTimerStart = t; |
| 293 | ArpRequest(); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Simon Glass | e4a3d57 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 298 | /* |
| 299 | * Check if autoload is enabled. If so, use either NFS or TFTP to download |
| 300 | * the boot file. |
| 301 | */ |
| 302 | void net_auto_load(void) |
| 303 | { |
| 304 | const char *s = getenv("autoload"); |
| 305 | |
| 306 | if (s != NULL) { |
| 307 | if (*s == 'n') { |
| 308 | /* |
| 309 | * Just use BOOTP/RARP to configure system; |
| 310 | * Do not use TFTP to load the bootfile. |
| 311 | */ |
| 312 | NetState = NETLOOP_SUCCESS; |
| 313 | return; |
| 314 | } |
| 315 | #if defined(CONFIG_CMD_NFS) |
| 316 | if (strcmp(s, "NFS") == 0) { |
| 317 | /* |
| 318 | * Use NFS to load the bootfile. |
| 319 | */ |
| 320 | NfsStart(); |
| 321 | return; |
| 322 | } |
| 323 | #endif |
| 324 | } |
| 325 | TftpStart(TFTPGET); |
| 326 | } |
| 327 | |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 328 | static void NetInitLoop(enum proto_t protocol) |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 329 | { |
Luca Ceresoli | c586ce6 | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 330 | static int env_changed_id; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 331 | int env_id = get_env_id(); |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 332 | |
| 333 | /* update only when the environment has changed */ |
Michael Zaidman | 3c172c4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 334 | if (env_changed_id != env_id) { |
Enric Balletbo i Serra | 23a70bf | 2011-05-31 21:01:47 +0000 | [diff] [blame] | 335 | NetOurIP = getenv_IPaddr("ipaddr"); |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 336 | NetOurGatewayIP = getenv_IPaddr("gatewayip"); |
| 337 | NetOurSubnetMask = getenv_IPaddr("netmask"); |
| 338 | NetServerIP = getenv_IPaddr("serverip"); |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 339 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
Michael Zaidman | 3c172c4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 340 | NetOurVLAN = getenv_VLAN("vlan"); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 341 | #if defined(CONFIG_CMD_DNS) |
| 342 | NetOurDNSIP = getenv_IPaddr("dnsip"); |
| 343 | #endif |
Michael Zaidman | 3c172c4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 344 | env_changed_id = env_id; |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 345 | } |
Michael Zaidman | 3c172c4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 346 | |
Heiko Schocher | da95427 | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 347 | return; |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 348 | } |
| 349 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 350 | /**********************************************************************/ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 351 | /* |
| 352 | * Main network processing loop. |
| 353 | */ |
| 354 | |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 355 | int NetLoop(enum proto_t protocol) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 356 | { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 357 | bd_t *bd = gd->bd; |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 358 | int ret = -1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 359 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 360 | NetRestarted = 0; |
| 361 | NetDevExists = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 362 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 363 | /* XXX problem with bss workaround */ |
| 364 | NetArpWaitPacketMAC = NULL; |
| 365 | NetArpWaitTxPacket = NULL; |
| 366 | NetArpWaitPacketIP = 0; |
| 367 | NetArpWaitReplyIP = 0; |
| 368 | NetArpWaitTxPacket = NULL; |
| 369 | NetTxPacket = NULL; |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 370 | NetTryCount = 1; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 371 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 372 | if (!NetTxPacket) { |
| 373 | int i; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 374 | /* |
| 375 | * Setup packet buffers, aligned correctly. |
| 376 | */ |
| 377 | NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); |
| 378 | NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 379 | for (i = 0; i < PKTBUFSRX; i++) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 380 | NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if (!NetArpWaitTxPacket) { |
| 384 | NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); |
| 385 | NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; |
| 386 | NetArpWaitTxPacketSize = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Simon Glass | 573f14f | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 389 | bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 390 | eth_halt(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 391 | eth_set_current(); |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 392 | if (eth_init(bd) < 0) { |
| 393 | eth_halt(); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 394 | return -1; |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 395 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 396 | |
| 397 | restart: |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 398 | memcpy(NetOurEther, eth_get_dev()->enetaddr, 6); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 399 | |
| 400 | NetState = NETLOOP_CONTINUE; |
| 401 | |
| 402 | /* |
| 403 | * Start the ball rolling with the given start function. From |
| 404 | * here on, this code is a state machine driven by received |
| 405 | * packets and timer events. |
| 406 | */ |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 407 | NetInitLoop(protocol); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 408 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 409 | switch (net_check_prereq(protocol)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 410 | case 1: |
| 411 | /* network not configured */ |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 412 | eth_halt(); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 413 | return -1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 414 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 415 | case 2: |
| 416 | /* network device not configured */ |
| 417 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 418 | |
| 419 | case 0: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 420 | NetDevExists = 1; |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 421 | NetBootFileXferSize = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 422 | switch (protocol) { |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 423 | case TFTPGET: |
Simon Glass | 1fb7cd4 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 424 | #ifdef CONFIG_CMD_TFTPPUT |
| 425 | case TFTPPUT: |
| 426 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 427 | /* always use ARP to get server ethernet address */ |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 428 | TftpStart(protocol); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 429 | break; |
Luca Ceresoli | 7a83af0 | 2011-05-17 00:03:40 +0000 | [diff] [blame] | 430 | #ifdef CONFIG_CMD_TFTPSRV |
| 431 | case TFTPSRV: |
| 432 | TftpStartServer(); |
| 433 | break; |
| 434 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 435 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 436 | case DHCP: |
wdenk | d407bf5 | 2004-10-11 22:51:13 +0000 | [diff] [blame] | 437 | BootpTry = 0; |
Michael Zaidman | 09133f8 | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 438 | NetOurIP = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 439 | DhcpRequest(); /* Basically same as BOOTP */ |
| 440 | break; |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 441 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 442 | |
| 443 | case BOOTP: |
| 444 | BootpTry = 0; |
Michael Zaidman | 09133f8 | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 445 | NetOurIP = 0; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 446 | BootpRequest(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 447 | break; |
| 448 | |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 449 | #if defined(CONFIG_CMD_RARP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 450 | case RARP: |
| 451 | RarpTry = 0; |
Michael Zaidman | 09133f8 | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 452 | NetOurIP = 0; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 453 | RarpRequest(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 454 | break; |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 455 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 456 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 457 | case PING: |
| 458 | PingStart(); |
| 459 | break; |
| 460 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 461 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 462 | case NFS: |
| 463 | NfsStart(); |
| 464 | break; |
| 465 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 466 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 467 | case CDP: |
| 468 | CDPStart(); |
| 469 | break; |
| 470 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 471 | #ifdef CONFIG_NETCONSOLE |
| 472 | case NETCONS: |
| 473 | NcStart(); |
| 474 | break; |
| 475 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 476 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 477 | case SNTP: |
| 478 | SntpStart(); |
| 479 | break; |
| 480 | #endif |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 481 | #if defined(CONFIG_CMD_DNS) |
| 482 | case DNS: |
| 483 | DnsStart(); |
| 484 | break; |
| 485 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 486 | default: |
| 487 | break; |
| 488 | } |
| 489 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 493 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 494 | #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ |
| 495 | defined(CONFIG_STATUS_LED) && \ |
| 496 | defined(STATUS_LED_RED) |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 497 | /* |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 498 | * Echo the inverted link state to the fault LED. |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 499 | */ |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 500 | if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 501 | status_led_set(STATUS_LED_RED, STATUS_LED_OFF); |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 502 | else |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 503 | status_led_set(STATUS_LED_RED, STATUS_LED_ON); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 504 | #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 505 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * Main packet reception loop. Loop receiving packets until |
wdenk | 59acc29 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 509 | * someone sets `NetState' to a state that terminates. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 510 | */ |
| 511 | for (;;) { |
| 512 | WATCHDOG_RESET(); |
| 513 | #ifdef CONFIG_SHOW_ACTIVITY |
Joe Hershberger | 48522bb | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 514 | show_activity(1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 515 | #endif |
| 516 | /* |
| 517 | * Check the ethernet for a new packet. The ethernet |
| 518 | * receive routine will process it. |
| 519 | */ |
Guennadi Liakhovetski | 40cb90e | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 520 | eth_rx(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * Abort if ctrl-c was pressed. |
| 524 | */ |
| 525 | if (ctrlc()) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 526 | eth_halt(); |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 527 | puts("\nAbort\n"); |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 528 | goto done; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 529 | } |
| 530 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 531 | ArpTimeoutCheck(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 532 | |
| 533 | /* |
| 534 | * Check for a timeout, and run the timeout handler |
| 535 | * if we have one. |
| 536 | */ |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 537 | if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 538 | thand_f *x; |
| 539 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 540 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 541 | #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ |
| 542 | defined(CONFIG_STATUS_LED) && \ |
| 543 | defined(STATUS_LED_RED) |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 544 | /* |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 545 | * Echo the inverted link state to the fault LED. |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 546 | */ |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 547 | if (miiphy_link(eth_get_dev()->name, |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 548 | CONFIG_SYS_FAULT_MII_ADDR)) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 549 | status_led_set(STATUS_LED_RED, STATUS_LED_OFF); |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 550 | } else { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 551 | status_led_set(STATUS_LED_RED, STATUS_LED_ON); |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 552 | } |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 553 | #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 554 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 555 | x = timeHandler; |
| 556 | timeHandler = (thand_f *)0; |
| 557 | (*x)(); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | switch (NetState) { |
| 562 | |
| 563 | case NETLOOP_RESTART: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 564 | NetRestarted = 1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 565 | goto restart; |
| 566 | |
| 567 | case NETLOOP_SUCCESS: |
| 568 | if (NetBootFileXferSize > 0) { |
Wolfgang Denk | f34024d | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 569 | char buf[20]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 570 | printf("Bytes transferred = %ld (%lx hex)\n", |
| 571 | NetBootFileXferSize, |
| 572 | NetBootFileXferSize); |
Wolfgang Denk | f34024d | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 573 | sprintf(buf, "%lX", NetBootFileXferSize); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 574 | setenv("filesize", buf); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 575 | |
| 576 | sprintf(buf, "%lX", (unsigned long)load_addr); |
| 577 | setenv("fileaddr", buf); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 578 | } |
| 579 | eth_halt(); |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 580 | ret = NetBootFileXferSize; |
| 581 | goto done; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 582 | |
| 583 | case NETLOOP_FAIL: |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 584 | goto done; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 587 | |
| 588 | done: |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 589 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 590 | /* Clear out the handlers */ |
| 591 | NetSetHandler(NULL); |
| 592 | net_set_icmp_handler(NULL); |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 593 | #endif |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 594 | return ret; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /**********************************************************************/ |
| 598 | |
| 599 | static void |
| 600 | startAgainTimeout(void) |
| 601 | { |
| 602 | NetState = NETLOOP_RESTART; |
| 603 | } |
| 604 | |
| 605 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 606 | startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, |
| 607 | unsigned src, unsigned len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 608 | { |
| 609 | /* Totally ignore the packet */ |
| 610 | } |
| 611 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 612 | void NetStartAgain(void) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 613 | { |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 614 | char *nretry; |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 615 | int retry_forever = 0; |
| 616 | unsigned long retrycnt = 0; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 617 | |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 618 | nretry = getenv("netretry"); |
| 619 | if (nretry) { |
| 620 | if (!strcmp(nretry, "yes")) |
| 621 | retry_forever = 1; |
| 622 | else if (!strcmp(nretry, "no")) |
| 623 | retrycnt = 0; |
| 624 | else if (!strcmp(nretry, "once")) |
| 625 | retrycnt = 1; |
| 626 | else |
| 627 | retrycnt = simple_strtoul(nretry, NULL, 0); |
| 628 | } else |
| 629 | retry_forever = 1; |
| 630 | |
| 631 | if ((!retry_forever) && (NetTryCount >= retrycnt)) { |
| 632 | eth_halt(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 633 | NetState = NETLOOP_FAIL; |
| 634 | return; |
| 635 | } |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 636 | |
| 637 | NetTryCount++; |
| 638 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 639 | eth_halt(); |
Matthias Fuchs | 8b0c5c1 | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 640 | #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER) |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 641 | eth_try_another(!NetRestarted); |
Matthias Fuchs | 8b0c5c1 | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 642 | #endif |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 643 | eth_init(gd->bd); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 644 | if (NetRestartWrap) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 645 | NetRestartWrap = 0; |
Remy Bohmer | 67b96e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 646 | if (NetDevExists) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 647 | NetSetTimeout(10000UL, startAgainTimeout); |
| 648 | NetSetHandler(startAgainHandler); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 649 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 650 | NetState = NETLOOP_FAIL; |
| 651 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 652 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 653 | NetState = NETLOOP_RESTART; |
| 654 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /**********************************************************************/ |
| 658 | /* |
| 659 | * Miscelaneous bits. |
| 660 | */ |
| 661 | |
| 662 | void |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 663 | NetSetHandler(rxhand_f *f) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 664 | { |
| 665 | packetHandler = f; |
| 666 | } |
| 667 | |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 668 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 669 | void net_set_icmp_handler(rxhand_icmp_f *f) |
| 670 | { |
| 671 | packet_icmp_handler = f; |
| 672 | } |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 673 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 674 | |
| 675 | void |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 676 | NetSetTimeout(ulong iv, thand_f *f) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 677 | { |
| 678 | if (iv == 0) { |
| 679 | timeHandler = (thand_f *)0; |
| 680 | } else { |
| 681 | timeHandler = f; |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 682 | timeStart = get_timer(0); |
| 683 | timeDelta = iv; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | |
| 688 | void |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 689 | NetSendPacket(uchar *pkt, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 690 | { |
| 691 | (void) eth_send(pkt, len); |
| 692 | } |
| 693 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 694 | int |
| 695 | NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) |
| 696 | { |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 697 | uchar *pkt; |
| 698 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 699 | /* convert to new style broadcast */ |
| 700 | if (dest == 0) |
| 701 | dest = 0xFFFFFFFF; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 702 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 703 | /* if broadcast, make the ether address a broadcast and don't do ARP */ |
| 704 | if (dest == 0xFFFFFFFF) |
| 705 | ether = NetBcastAddr; |
| 706 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 707 | /* |
| 708 | * if MAC address was not discovered yet, save the packet and do |
| 709 | * an ARP request |
| 710 | */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 711 | if (memcmp(ether, NetEtherNullAddr, 6) == 0) { |
| 712 | |
Matthias Weisser | ea45cb0 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 713 | debug("sending ARP for %08x\n", dest); |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 714 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 715 | NetArpWaitPacketIP = dest; |
| 716 | NetArpWaitPacketMAC = ether; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 717 | |
| 718 | pkt = NetArpWaitTxPacket; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 719 | pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 720 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 721 | NetSetIP(pkt, dest, dport, sport, len); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 722 | memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + |
| 723 | (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 724 | |
| 725 | /* size of the waiting packet */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 726 | NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + |
| 727 | IP_HDR_SIZE + len; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 728 | |
| 729 | /* and do the ARP request */ |
| 730 | NetArpWaitTry = 1; |
| 731 | NetArpWaitTimerStart = get_timer(0); |
| 732 | ArpRequest(); |
| 733 | return 1; /* waiting */ |
| 734 | } |
| 735 | |
Matthias Weisser | ea45cb0 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 736 | debug("sending UDP to %08x/%pM\n", dest, ether); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 737 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 738 | pkt = (uchar *)NetTxPacket; |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 739 | pkt += NetSetEther(pkt, ether, PROT_IP); |
| 740 | NetSetIP(pkt, dest, dport, sport, len); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 741 | (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 742 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 743 | return 0; /* transmitted */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 746 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 747 | static ushort PingSeqNo; |
| 748 | |
| 749 | int PingSend(void) |
| 750 | { |
| 751 | static uchar mac[6]; |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 752 | IP_t *ip; |
| 753 | ushort *s; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 754 | uchar *pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 755 | |
| 756 | /* XXX always send arp request */ |
| 757 | |
| 758 | memcpy(mac, NetEtherNullAddr, 6); |
| 759 | |
Matthias Weisser | ea45cb0 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 760 | debug("sending ARP for %08x\n", NetPingIP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 761 | |
| 762 | NetArpWaitPacketIP = NetPingIP; |
| 763 | NetArpWaitPacketMAC = mac; |
| 764 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 765 | pkt = NetArpWaitTxPacket; |
| 766 | pkt += NetSetEther(pkt, mac, PROT_IP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 767 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 768 | ip = (IP_t *)pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 769 | |
| 770 | /* |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 771 | * Construct an IP and ICMP header. |
| 772 | * (need to set no fragment bit - XXX) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 773 | */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 774 | /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 775 | ip->ip_hl_v = 0x45; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 776 | ip->ip_tos = 0; |
| 777 | ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); |
| 778 | ip->ip_id = htons(NetIPID++); |
Peter Tyser | e0c07b8 | 2008-12-01 16:26:20 -0600 | [diff] [blame] | 779 | ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 780 | ip->ip_ttl = 255; |
| 781 | ip->ip_p = 0x01; /* ICMP */ |
| 782 | ip->ip_sum = 0; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 783 | /* already in network byte order */ |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 784 | NetCopyIP((void *)&ip->ip_src, &NetOurIP); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 785 | /* - "" - */ |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 786 | NetCopyIP((void *)&ip->ip_dst, &NetPingIP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 787 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 788 | |
| 789 | s = &ip->udp_src; /* XXX ICMP starts here */ |
| 790 | s[0] = htons(0x0800); /* echo-request, code */ |
| 791 | s[1] = 0; /* checksum */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 792 | s[2] = 0; /* identifier */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 793 | s[3] = htons(PingSeqNo++); /* sequence number */ |
| 794 | s[1] = ~NetCksum((uchar *)s, 8/2); |
| 795 | |
| 796 | /* size of the waiting packet */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 797 | NetArpWaitTxPacketSize = |
| 798 | (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 799 | |
| 800 | /* and do the ARP request */ |
| 801 | NetArpWaitTry = 1; |
| 802 | NetArpWaitTimerStart = get_timer(0); |
| 803 | ArpRequest(); |
| 804 | return 1; /* waiting */ |
| 805 | } |
| 806 | |
| 807 | static void |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 808 | PingTimeout(void) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 809 | { |
| 810 | eth_halt(); |
| 811 | NetState = NETLOOP_FAIL; /* we did not get the reply */ |
| 812 | } |
| 813 | |
| 814 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 815 | PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 816 | unsigned len) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 817 | { |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 818 | if (sip != NetPingIP) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 819 | return; |
| 820 | |
| 821 | NetState = NETLOOP_SUCCESS; |
| 822 | } |
| 823 | |
| 824 | static void PingStart(void) |
| 825 | { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 826 | printf("Using %s device\n", eth_get_name()); |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 827 | NetSetTimeout(10000UL, PingTimeout); |
| 828 | NetSetHandler(PingHandler); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 829 | |
| 830 | PingSend(); |
| 831 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 832 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 833 | |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 834 | #ifdef CONFIG_IP_DEFRAG |
| 835 | /* |
| 836 | * This function collects fragments in a single packet, according |
| 837 | * to the algorithm in RFC815. It returns NULL or the pointer to |
| 838 | * a complete packet, in static storage |
| 839 | */ |
| 840 | #ifndef CONFIG_NET_MAXDEFRAG |
| 841 | #define CONFIG_NET_MAXDEFRAG 16384 |
| 842 | #endif |
| 843 | /* |
| 844 | * MAXDEFRAG, above, is chosen in the config file and is real data |
| 845 | * so we need to add the NFS overhead, which is more than TFTP. |
| 846 | * To use sizeof in the internal unnamed structures, we need a real |
| 847 | * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately). |
| 848 | * The compiler doesn't complain nor allocates the actual structure |
| 849 | */ |
| 850 | static struct rpc_t rpc_specimen; |
| 851 | #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply)) |
| 852 | |
| 853 | #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) |
| 854 | |
| 855 | /* |
| 856 | * this is the packet being assembled, either data or frag control. |
| 857 | * Fragments go by 8 bytes, so this union must be 8 bytes long |
| 858 | */ |
| 859 | struct hole { |
| 860 | /* first_byte is address of this structure */ |
| 861 | u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */ |
| 862 | u16 next_hole; /* index of next (in 8-b blocks), 0 == none */ |
| 863 | u16 prev_hole; /* index of prev, 0 == none */ |
| 864 | u16 unused; |
| 865 | }; |
| 866 | |
| 867 | static IP_t *__NetDefragment(IP_t *ip, int *lenp) |
| 868 | { |
Joe Hershberger | 48522bb | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 869 | static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 870 | static u16 first_hole, total_len; |
| 871 | struct hole *payload, *thisfrag, *h, *newh; |
| 872 | IP_t *localip = (IP_t *)pkt_buff; |
| 873 | uchar *indata = (uchar *)ip; |
| 874 | int offset8, start, len, done = 0; |
| 875 | u16 ip_off = ntohs(ip->ip_off); |
| 876 | |
| 877 | /* payload starts after IP header, this fragment is in there */ |
| 878 | payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); |
| 879 | offset8 = (ip_off & IP_OFFS); |
| 880 | thisfrag = payload + offset8; |
| 881 | start = offset8 * 8; |
| 882 | len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; |
| 883 | |
| 884 | if (start + len > IP_MAXUDP) /* fragment extends too far */ |
| 885 | return NULL; |
| 886 | |
| 887 | if (!total_len || localip->ip_id != ip->ip_id) { |
| 888 | /* new (or different) packet, reset structs */ |
| 889 | total_len = 0xffff; |
| 890 | payload[0].last_byte = ~0; |
| 891 | payload[0].next_hole = 0; |
| 892 | payload[0].prev_hole = 0; |
| 893 | first_hole = 0; |
| 894 | /* any IP header will work, copy the first we received */ |
| 895 | memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * What follows is the reassembly algorithm. We use the payload |
| 900 | * array as a linked list of hole descriptors, as each hole starts |
| 901 | * at a multiple of 8 bytes. However, last byte can be whatever value, |
| 902 | * so it is represented as byte count, not as 8-byte blocks. |
| 903 | */ |
| 904 | |
| 905 | h = payload + first_hole; |
| 906 | while (h->last_byte < start) { |
| 907 | if (!h->next_hole) { |
| 908 | /* no hole that far away */ |
| 909 | return NULL; |
| 910 | } |
| 911 | h = payload + h->next_hole; |
| 912 | } |
| 913 | |
Fillod Stephane | e397e59 | 2010-06-11 19:26:43 +0200 | [diff] [blame] | 914 | /* last fragment may be 1..7 bytes, the "+7" forces acceptance */ |
| 915 | if (offset8 + ((len + 7) / 8) <= h - payload) { |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 916 | /* no overlap with holes (dup fragment?) */ |
| 917 | return NULL; |
| 918 | } |
| 919 | |
| 920 | if (!(ip_off & IP_FLAGS_MFRAG)) { |
| 921 | /* no more fragmentss: truncate this (last) hole */ |
| 922 | total_len = start + len; |
| 923 | h->last_byte = start + len; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * There is some overlap: fix the hole list. This code doesn't |
| 928 | * deal with a fragment that overlaps with two different holes |
| 929 | * (thus being a superset of a previously-received fragment). |
| 930 | */ |
| 931 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 932 | if ((h >= thisfrag) && (h->last_byte <= start + len)) { |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 933 | /* complete overlap with hole: remove hole */ |
| 934 | if (!h->prev_hole && !h->next_hole) { |
| 935 | /* last remaining hole */ |
| 936 | done = 1; |
| 937 | } else if (!h->prev_hole) { |
| 938 | /* first hole */ |
| 939 | first_hole = h->next_hole; |
| 940 | payload[h->next_hole].prev_hole = 0; |
| 941 | } else if (!h->next_hole) { |
| 942 | /* last hole */ |
| 943 | payload[h->prev_hole].next_hole = 0; |
| 944 | } else { |
| 945 | /* in the middle of the list */ |
| 946 | payload[h->next_hole].prev_hole = h->prev_hole; |
| 947 | payload[h->prev_hole].next_hole = h->next_hole; |
| 948 | } |
| 949 | |
| 950 | } else if (h->last_byte <= start + len) { |
| 951 | /* overlaps with final part of the hole: shorten this hole */ |
| 952 | h->last_byte = start; |
| 953 | |
| 954 | } else if (h >= thisfrag) { |
| 955 | /* overlaps with initial part of the hole: move this hole */ |
| 956 | newh = thisfrag + (len / 8); |
| 957 | *newh = *h; |
| 958 | h = newh; |
| 959 | if (h->next_hole) |
| 960 | payload[h->next_hole].prev_hole = (h - payload); |
| 961 | if (h->prev_hole) |
| 962 | payload[h->prev_hole].next_hole = (h - payload); |
| 963 | else |
| 964 | first_hole = (h - payload); |
| 965 | |
| 966 | } else { |
| 967 | /* fragment sits in the middle: split the hole */ |
| 968 | newh = thisfrag + (len / 8); |
| 969 | *newh = *h; |
| 970 | h->last_byte = start; |
| 971 | h->next_hole = (newh - payload); |
| 972 | newh->prev_hole = (h - payload); |
| 973 | if (newh->next_hole) |
| 974 | payload[newh->next_hole].prev_hole = (newh - payload); |
| 975 | } |
| 976 | |
| 977 | /* finally copy this fragment and possibly return whole packet */ |
| 978 | memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); |
| 979 | if (!done) |
| 980 | return NULL; |
| 981 | |
| 982 | localip->ip_len = htons(total_len); |
| 983 | *lenp = total_len + IP_HDR_SIZE_NO_UDP; |
| 984 | return localip; |
| 985 | } |
| 986 | |
| 987 | static inline IP_t *NetDefragment(IP_t *ip, int *lenp) |
| 988 | { |
| 989 | u16 ip_off = ntohs(ip->ip_off); |
| 990 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 991 | return ip; /* not a fragment */ |
| 992 | return __NetDefragment(ip, lenp); |
| 993 | } |
| 994 | |
| 995 | #else /* !CONFIG_IP_DEFRAG */ |
| 996 | |
| 997 | static inline IP_t *NetDefragment(IP_t *ip, int *lenp) |
| 998 | { |
| 999 | u16 ip_off = ntohs(ip->ip_off); |
| 1000 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 1001 | return ip; /* not a fragment */ |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | #endif |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1005 | |
Simon Glass | 8f79bb1 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 1006 | /** |
| 1007 | * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently |
| 1008 | * drop others. |
| 1009 | * |
| 1010 | * @parma ip IP packet containing the ICMP |
| 1011 | */ |
| 1012 | static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) |
| 1013 | { |
| 1014 | ICMP_t *icmph = (ICMP_t *)&ip->udp_src; |
| 1015 | |
| 1016 | switch (icmph->type) { |
| 1017 | case ICMP_REDIRECT: |
| 1018 | if (icmph->code != ICMP_REDIR_HOST) |
| 1019 | return; |
| 1020 | printf(" ICMP Host Redirect to %pI4 ", |
| 1021 | &icmph->un.gateway); |
| 1022 | break; |
| 1023 | #if defined(CONFIG_CMD_PING) |
| 1024 | case ICMP_ECHO_REPLY: |
| 1025 | /* |
| 1026 | * IP header OK. Pass the packet to the |
| 1027 | * current handler. |
| 1028 | */ |
| 1029 | /* |
| 1030 | * XXX point to ip packet - should this use |
| 1031 | * packet_icmp_handler? |
| 1032 | */ |
| 1033 | (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); |
| 1034 | break; |
| 1035 | case ICMP_ECHO_REQUEST: |
| 1036 | debug("Got ICMP ECHO REQUEST, return %d bytes\n", |
| 1037 | ETHER_HDR_SIZE + len); |
| 1038 | |
| 1039 | memcpy(&et->et_dest[0], &et->et_src[0], 6); |
| 1040 | memcpy(&et->et_src[0], NetOurEther, 6); |
| 1041 | |
| 1042 | ip->ip_sum = 0; |
| 1043 | ip->ip_off = 0; |
| 1044 | NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); |
| 1045 | NetCopyIP((void *)&ip->ip_src, &NetOurIP); |
| 1046 | ip->ip_sum = ~NetCksum((uchar *)ip, |
| 1047 | IP_HDR_SIZE_NO_UDP >> 1); |
| 1048 | |
| 1049 | icmph->type = ICMP_ECHO_REPLY; |
| 1050 | icmph->checksum = 0; |
| 1051 | icmph->checksum = ~NetCksum((uchar *)icmph, |
| 1052 | (len - IP_HDR_SIZE_NO_UDP) >> 1); |
| 1053 | (void) eth_send((uchar *)et, |
| 1054 | ETHER_HDR_SIZE + len); |
| 1055 | break; |
| 1056 | #endif |
| 1057 | default: |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 1058 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 1059 | if (packet_icmp_handler) |
| 1060 | packet_icmp_handler(icmph->type, icmph->code, |
| 1061 | ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src), |
| 1062 | icmph->un.data, ntohs(ip->udp_len)); |
Simon Glass | 39bccd2 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 1063 | #endif |
Simon Glass | 8f79bb1 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1068 | void |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1069 | NetReceive(uchar *inpkt, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1070 | { |
| 1071 | Ethernet_t *et; |
| 1072 | IP_t *ip; |
| 1073 | ARP_t *arp; |
| 1074 | IPaddr_t tmp; |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1075 | IPaddr_t src_ip; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1076 | int x; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1077 | uchar *pkt; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1078 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1079 | int iscdp; |
| 1080 | #endif |
| 1081 | ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1082 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1083 | debug("packet received\n"); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1084 | |
Mike Frysinger | d9bec9f | 2009-07-18 21:04:08 -0400 | [diff] [blame] | 1085 | NetRxPacket = inpkt; |
| 1086 | NetRxPacketLen = len; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1087 | et = (Ethernet_t *)inpkt; |
| 1088 | |
| 1089 | /* too small packet? */ |
| 1090 | if (len < ETHER_HDR_SIZE) |
| 1091 | return; |
| 1092 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1093 | #ifdef CONFIG_API |
| 1094 | if (push_packet) { |
| 1095 | (*push_packet)(inpkt, len); |
| 1096 | return; |
| 1097 | } |
| 1098 | #endif |
| 1099 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1100 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1101 | /* keep track if packet is CDP */ |
| 1102 | iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; |
| 1103 | #endif |
| 1104 | |
| 1105 | myvlanid = ntohs(NetOurVLAN); |
| 1106 | if (myvlanid == (ushort)-1) |
| 1107 | myvlanid = VLAN_NONE; |
| 1108 | mynvlanid = ntohs(NetOurNativeVLAN); |
| 1109 | if (mynvlanid == (ushort)-1) |
| 1110 | mynvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1111 | |
| 1112 | x = ntohs(et->et_protlen); |
| 1113 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1114 | debug("packet received\n"); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1115 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1116 | if (x < 1514) { |
| 1117 | /* |
| 1118 | * Got a 802 packet. Check the other protocol field. |
| 1119 | */ |
| 1120 | x = ntohs(et->et_prot); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1121 | |
| 1122 | ip = (IP_t *)(inpkt + E802_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1123 | len -= E802_HDR_SIZE; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1124 | |
| 1125 | } else if (x != PROT_VLAN) { /* normal packet */ |
| 1126 | ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1127 | len -= ETHER_HDR_SIZE; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1128 | |
| 1129 | } else { /* VLAN packet */ |
| 1130 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; |
| 1131 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1132 | debug("VLAN packet received\n"); |
| 1133 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1134 | /* too small packet? */ |
| 1135 | if (len < VLAN_ETHER_HDR_SIZE) |
| 1136 | return; |
| 1137 | |
| 1138 | /* if no VLAN active */ |
| 1139 | if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1140 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1141 | && iscdp == 0 |
| 1142 | #endif |
| 1143 | ) |
| 1144 | return; |
| 1145 | |
| 1146 | cti = ntohs(vet->vet_tag); |
| 1147 | vlanid = cti & VLAN_IDMASK; |
| 1148 | x = ntohs(vet->vet_type); |
| 1149 | |
| 1150 | ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); |
| 1151 | len -= VLAN_ETHER_HDR_SIZE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1154 | debug("Receive from protocol 0x%x\n", x); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1155 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1156 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1157 | if (iscdp) { |
| 1158 | CDPHandler((uchar *)ip, len); |
| 1159 | return; |
| 1160 | } |
| 1161 | #endif |
| 1162 | |
| 1163 | if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { |
| 1164 | if (vlanid == VLAN_NONE) |
| 1165 | vlanid = (mynvlanid & VLAN_IDMASK); |
| 1166 | /* not matched? */ |
| 1167 | if (vlanid != (myvlanid & VLAN_IDMASK)) |
| 1168 | return; |
| 1169 | } |
| 1170 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1171 | switch (x) { |
| 1172 | |
| 1173 | case PROT_ARP: |
| 1174 | /* |
| 1175 | * We have to deal with two types of ARP packets: |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1176 | * - REQUEST packets will be answered by sending our |
| 1177 | * IP address - if we know it. |
| 1178 | * - REPLY packates are expected only after we asked |
| 1179 | * for the TFTP server's or the gateway's ethernet |
| 1180 | * address; so if we receive such a packet, we set |
| 1181 | * the server ethernet address |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1182 | */ |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1183 | debug("Got ARP\n"); |
| 1184 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1185 | arp = (ARP_t *)ip; |
| 1186 | if (len < ARP_HDR_SIZE) { |
| 1187 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1188 | return; |
| 1189 | } |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1190 | if (ntohs(arp->ar_hrd) != ARP_ETHER) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1191 | return; |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1192 | if (ntohs(arp->ar_pro) != PROT_IP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1193 | return; |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1194 | if (arp->ar_hln != 6) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1195 | return; |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1196 | if (arp->ar_pln != 4) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1197 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1198 | |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1199 | if (NetOurIP == 0) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1200 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1201 | |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1202 | if (NetReadIP(&arp->ar_data[16]) != NetOurIP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1203 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1204 | |
| 1205 | switch (ntohs(arp->ar_op)) { |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1206 | case ARPOP_REQUEST: |
| 1207 | /* reply with our IP address */ |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1208 | debug("Got ARP REQUEST, return our IP\n"); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1209 | pkt = (uchar *)et; |
| 1210 | pkt += NetSetEther(pkt, et->et_src, PROT_ARP); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1211 | arp->ar_op = htons(ARPOP_REPLY); |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1212 | memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1213 | NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1214 | memcpy(&arp->ar_data[0], NetOurEther, 6); |
| 1215 | NetCopyIP(&arp->ar_data[6], &NetOurIP); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1216 | (void) eth_send((uchar *)et, |
| 1217 | (pkt - (uchar *)et) + ARP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1218 | return; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1219 | |
| 1220 | case ARPOP_REPLY: /* arp reply */ |
| 1221 | /* are we waiting for a reply */ |
| 1222 | if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) |
| 1223 | break; |
Robin Getz | 97cfe86 | 2009-07-21 12:15:28 -0400 | [diff] [blame] | 1224 | |
| 1225 | #ifdef CONFIG_KEEP_SERVERADDR |
| 1226 | if (NetServerIP == NetArpWaitPacketIP) { |
| 1227 | char buf[20]; |
| 1228 | sprintf(buf, "%pM", arp->ar_data); |
| 1229 | setenv("serveraddr", buf); |
| 1230 | } |
| 1231 | #endif |
| 1232 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1233 | debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", |
Mike Frysinger | 95823ca | 2009-02-11 18:23:48 -0500 | [diff] [blame] | 1234 | arp->ar_data); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1235 | |
| 1236 | tmp = NetReadIP(&arp->ar_data[6]); |
| 1237 | |
| 1238 | /* matched waiting packet's address */ |
| 1239 | if (tmp == NetArpWaitReplyIP) { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1240 | debug("Got it\n"); |
Joe Hershberger | 48522bb | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 1241 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1242 | /* save address for later use */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1243 | memcpy(NetArpWaitPacketMAC, |
| 1244 | &arp->ar_data[0], 6); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1245 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1246 | #ifdef CONFIG_NETCONSOLE |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1247 | (*packetHandler)(0, 0, 0, 0, 0); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1248 | #endif |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1249 | /* modify header, and transmit it */ |
Joe Hershberger | 48522bb | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 1250 | memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> |
| 1251 | et_dest, NetArpWaitPacketMAC, 6); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1252 | (void) eth_send(NetArpWaitTxPacket, |
| 1253 | NetArpWaitTxPacketSize); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* no arp request pending now */ |
| 1256 | NetArpWaitPacketIP = 0; |
| 1257 | NetArpWaitTxPacketSize = 0; |
| 1258 | NetArpWaitPacketMAC = NULL; |
| 1259 | |
| 1260 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | default: |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1263 | debug("Unexpected ARP opcode 0x%x\n", |
| 1264 | ntohs(arp->ar_op)); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
wdenk | 289f932 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 1267 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1268 | |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1269 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1270 | case PROT_RARP: |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1271 | debug("Got RARP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1272 | arp = (ARP_t *)ip; |
| 1273 | if (len < ARP_HDR_SIZE) { |
| 1274 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | if ((ntohs(arp->ar_op) != RARPOP_REPLY) || |
| 1279 | (ntohs(arp->ar_hrd) != ARP_ETHER) || |
| 1280 | (ntohs(arp->ar_pro) != PROT_IP) || |
| 1281 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { |
| 1282 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1283 | puts("invalid RARP header\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1284 | } else { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1285 | NetCopyIP(&NetOurIP, &arp->ar_data[16]); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 1286 | if (NetServerIP == 0) |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1287 | NetCopyIP(&NetServerIP, &arp->ar_data[6]); |
| 1288 | memcpy(NetServerEther, &arp->ar_data[0], 6); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1289 | |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1290 | (*packetHandler)(0, 0, 0, 0, 0); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1291 | } |
| 1292 | break; |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1293 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1294 | case PROT_IP: |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1295 | debug("Got IP\n"); |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1296 | /* Before we start poking the header, make sure it is there */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1297 | if (len < IP_HDR_SIZE) { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1298 | debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1299 | return; |
| 1300 | } |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1301 | /* Check the packet length */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1302 | if (len < ntohs(ip->ip_len)) { |
| 1303 | printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); |
| 1304 | return; |
| 1305 | } |
| 1306 | len = ntohs(ip->ip_len); |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1307 | debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); |
| 1308 | |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1309 | /* Can't deal with anything except IPv4 */ |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1310 | if ((ip->ip_hl_v & 0xf0) != 0x40) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1311 | return; |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1312 | /* Can't deal with IP options (headers != 20 bytes) */ |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1313 | if ((ip->ip_hl_v & 0x0f) > 0x05) |
Remy Bohmer | 6b52cfe | 2008-06-03 15:48:17 +0200 | [diff] [blame] | 1314 | return; |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1315 | /* Check the Checksum of the header */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1316 | if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1317 | puts("checksum bad\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1318 | return; |
| 1319 | } |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1320 | /* If it is not for us, ignore it */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1321 | tmp = NetReadIP(&ip->ip_dst); |
| 1322 | if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1323 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 1324 | if (Mcast_addr != tmp) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1325 | #endif |
Luca Ceresoli | c819abe | 2011-05-04 02:40:46 +0000 | [diff] [blame] | 1326 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1327 | } |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1328 | /* Read source IP address for later use */ |
| 1329 | src_ip = NetReadIP(&ip->ip_src); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1330 | /* |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1331 | * The function returns the unchanged packet if it's not |
| 1332 | * a fragment, and either the complete packet or NULL if |
| 1333 | * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL) |
| 1334 | */ |
Luca Ceresoli | ccb9ebe | 2011-05-04 02:40:47 +0000 | [diff] [blame] | 1335 | ip = NetDefragment(ip, &len); |
| 1336 | if (!ip) |
Alessandro Rubini | 5cfaa4e | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1337 | return; |
| 1338 | /* |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1339 | * watch for ICMP host redirects |
| 1340 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1341 | * There is no real handler code (yet). We just watch |
| 1342 | * for ICMP host redirect messages. In case anybody |
| 1343 | * sees these messages: please contact me |
| 1344 | * (wd@denx.de), or - even better - send me the |
| 1345 | * necessary fixes :-) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1346 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1347 | * Note: in all cases where I have seen this so far |
| 1348 | * it was a problem with the router configuration, |
| 1349 | * for instance when a router was configured in the |
| 1350 | * BOOTP reply, but the TFTP server was on the same |
| 1351 | * subnet. So this is probably a warning that your |
| 1352 | * configuration might be wrong. But I'm not really |
| 1353 | * sure if there aren't any other situations. |
Simon Glass | 4793ee6 | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 1354 | * |
| 1355 | * Simon Glass <sjg@chromium.org>: We get an ICMP when |
| 1356 | * we send a tftp packet to a dead connection, or when |
| 1357 | * there is no server at the other end. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1358 | */ |
| 1359 | if (ip->ip_p == IPPROTO_ICMP) { |
Simon Glass | 8f79bb1 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 1360 | receive_icmp(ip, len, src_ip, et); |
| 1361 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1362 | } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ |
| 1363 | return; |
| 1364 | } |
| 1365 | |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1366 | #ifdef CONFIG_UDP_CHECKSUM |
| 1367 | if (ip->udp_xsum != 0) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1368 | ulong xsum; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1369 | ushort *sumptr; |
| 1370 | ushort sumlen; |
| 1371 | |
| 1372 | xsum = ip->ip_p; |
| 1373 | xsum += (ntohs(ip->udp_len)); |
| 1374 | xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; |
| 1375 | xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; |
| 1376 | xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; |
| 1377 | xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; |
| 1378 | |
| 1379 | sumlen = ntohs(ip->udp_len); |
| 1380 | sumptr = (ushort *) &(ip->udp_src); |
| 1381 | |
| 1382 | while (sumlen > 1) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1383 | ushort sumdata; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1384 | |
| 1385 | sumdata = *sumptr++; |
| 1386 | xsum += ntohs(sumdata); |
| 1387 | sumlen -= 2; |
| 1388 | } |
| 1389 | if (sumlen > 0) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1390 | ushort sumdata; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1391 | |
| 1392 | sumdata = *(unsigned char *) sumptr; |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1393 | sumdata = (sumdata << 8) & 0xff00; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1394 | xsum += sumdata; |
| 1395 | } |
| 1396 | while ((xsum >> 16) != 0) { |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1397 | xsum = (xsum & 0x0000ffff) + |
| 1398 | ((xsum >> 16) & 0x0000ffff); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1399 | } |
| 1400 | if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 1401 | printf(" UDP wrong checksum %08lx %08x\n", |
| 1402 | xsum, ntohs(ip->udp_xsum)); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | } |
| 1406 | #endif |
| 1407 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1408 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1409 | #ifdef CONFIG_NETCONSOLE |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1410 | nc_input_packet((uchar *)ip + IP_HDR_SIZE, |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1411 | ntohs(ip->udp_dst), |
| 1412 | ntohs(ip->udp_src), |
| 1413 | ntohs(ip->udp_len) - 8); |
| 1414 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1415 | /* |
| 1416 | * IP header OK. Pass the packet to the current handler. |
| 1417 | */ |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1418 | (*packetHandler)((uchar *)ip + IP_HDR_SIZE, |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1419 | ntohs(ip->udp_dst), |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1420 | src_ip, |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1421 | ntohs(ip->udp_src), |
| 1422 | ntohs(ip->udp_len) - 8); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | /**********************************************************************/ |
| 1429 | |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 1430 | static int net_check_prereq(enum proto_t protocol) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1431 | { |
| 1432 | switch (protocol) { |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1433 | /* Fall through */ |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1434 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1435 | case PING: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1436 | if (NetPingIP == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1437 | puts("*** ERROR: ping address not given\n"); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1438 | return 1; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1439 | } |
| 1440 | goto common; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1441 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1442 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1443 | case SNTP: |
| 1444 | if (NetNtpServerIP == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1445 | puts("*** ERROR: NTP server address not given\n"); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1446 | return 1; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1447 | } |
| 1448 | goto common; |
| 1449 | #endif |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1450 | #if defined(CONFIG_CMD_DNS) |
| 1451 | case DNS: |
| 1452 | if (NetOurDNSIP == 0) { |
| 1453 | puts("*** ERROR: DNS server address not given\n"); |
| 1454 | return 1; |
| 1455 | } |
| 1456 | goto common; |
| 1457 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1458 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1459 | case NFS: |
| 1460 | #endif |
Simon Glass | e4bf0c5 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 1461 | case TFTPGET: |
Simon Glass | 1fb7cd4 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 1462 | case TFTPPUT: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1463 | if (NetServerIP == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1464 | puts("*** ERROR: `serverip' not set\n"); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1465 | return 1; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1466 | } |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1467 | #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \ |
| 1468 | defined(CONFIG_CMD_DNS) |
| 1469 | common: |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1470 | #endif |
Simon Guinot | 8b6bbe1 | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1471 | /* Fall through */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1472 | |
Simon Guinot | 8b6bbe1 | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1473 | case NETCONS: |
Luca Ceresoli | 7a83af0 | 2011-05-17 00:03:40 +0000 | [diff] [blame] | 1474 | case TFTPSRV: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1475 | if (NetOurIP == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1476 | puts("*** ERROR: `ipaddr' not set\n"); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1477 | return 1; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1478 | } |
| 1479 | /* Fall through */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1480 | |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1481 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1482 | case RARP: |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1483 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1484 | case BOOTP: |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1485 | case CDP: |
Peter Tyser | bf6cb24 | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1486 | case DHCP: |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1487 | if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1488 | int num = eth_get_dev_index(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1489 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1490 | switch (num) { |
| 1491 | case -1: |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1492 | puts("*** ERROR: No ethernet found.\n"); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1493 | return 1; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1494 | case 0: |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1495 | puts("*** ERROR: `ethaddr' not set\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1496 | break; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1497 | default: |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1498 | printf("*** ERROR: `eth%daddr' not set\n", |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1499 | num); |
| 1500 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1501 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1502 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1503 | NetStartAgain(); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1504 | return 2; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1505 | } |
| 1506 | /* Fall through */ |
| 1507 | default: |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1508 | return 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1509 | } |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1510 | return 0; /* OK */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | /**********************************************************************/ |
| 1513 | |
| 1514 | int |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1515 | NetCksumOk(uchar *ptr, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1516 | { |
| 1517 | return !((NetCksum(ptr, len) + 1) & 0xfffe); |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | unsigned |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1522 | NetCksum(uchar *ptr, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1523 | { |
| 1524 | ulong xsum; |
Stefan Roese | 9d2a873 | 2005-08-31 12:55:50 +0200 | [diff] [blame] | 1525 | ushort *p = (ushort *)ptr; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1526 | |
| 1527 | xsum = 0; |
| 1528 | while (len-- > 0) |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 1529 | xsum += *p++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1530 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1531 | xsum = (xsum & 0xffff) + (xsum >> 16); |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1532 | return xsum & 0xffff; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1535 | int |
| 1536 | NetEthHdrSize(void) |
| 1537 | { |
| 1538 | ushort myvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1539 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1540 | myvlanid = ntohs(NetOurVLAN); |
| 1541 | if (myvlanid == (ushort)-1) |
| 1542 | myvlanid = VLAN_NONE; |
| 1543 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1544 | return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : |
| 1545 | VLAN_ETHER_HDR_SIZE; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | int |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1549 | NetSetEther(uchar *xet, uchar * addr, uint prot) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1550 | { |
| 1551 | Ethernet_t *et = (Ethernet_t *)xet; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1552 | ushort myvlanid; |
| 1553 | |
| 1554 | myvlanid = ntohs(NetOurVLAN); |
| 1555 | if (myvlanid == (ushort)-1) |
| 1556 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1557 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1558 | memcpy(et->et_dest, addr, 6); |
| 1559 | memcpy(et->et_src, NetOurEther, 6); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1560 | if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { |
Luca Ceresoli | c819abe | 2011-05-04 02:40:46 +0000 | [diff] [blame] | 1561 | et->et_protlen = htons(prot); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1562 | return ETHER_HDR_SIZE; |
| 1563 | } else { |
| 1564 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1565 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1566 | vet->vet_vlan_type = htons(PROT_VLAN); |
| 1567 | vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); |
| 1568 | vet->vet_type = htons(prot); |
| 1569 | return VLAN_ETHER_HDR_SIZE; |
| 1570 | } |
| 1571 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1572 | |
| 1573 | void |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1574 | NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1575 | { |
Olav Morken | af8626e | 2009-01-23 12:56:26 +0100 | [diff] [blame] | 1576 | IP_t *ip = (IP_t *)xip; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1577 | |
| 1578 | /* |
| 1579 | * If the data is an odd number of bytes, zero the |
| 1580 | * byte after the last byte so that the checksum |
| 1581 | * will work. |
| 1582 | */ |
| 1583 | if (len & 1) |
| 1584 | xip[IP_HDR_SIZE + len] = 0; |
| 1585 | |
| 1586 | /* |
| 1587 | * Construct an IP and UDP header. |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1588 | * (need to set no fragment bit - XXX) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1589 | */ |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1590 | /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 1591 | ip->ip_hl_v = 0x45; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1592 | ip->ip_tos = 0; |
| 1593 | ip->ip_len = htons(IP_HDR_SIZE + len); |
| 1594 | ip->ip_id = htons(NetIPID++); |
Peter Tyser | e0c07b8 | 2008-12-01 16:26:20 -0600 | [diff] [blame] | 1595 | ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1596 | ip->ip_ttl = 255; |
| 1597 | ip->ip_p = 17; /* UDP */ |
| 1598 | ip->ip_sum = 0; |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1599 | /* already in network byte order */ |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1600 | NetCopyIP((void *)&ip->ip_src, &NetOurIP); |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1601 | /* - "" - */ |
Luca Ceresoli | 6b147d1 | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1602 | NetCopyIP((void *)&ip->ip_dst, &dest); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1603 | ip->udp_src = htons(sport); |
| 1604 | ip->udp_dst = htons(dport); |
| 1605 | ip->udp_len = htons(8 + len); |
| 1606 | ip->udp_xsum = 0; |
| 1607 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 1608 | } |
| 1609 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1610 | void copy_filename(char *dst, const char *src, int size) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1611 | { |
| 1612 | if (*src && (*src == '"')) { |
| 1613 | ++src; |
| 1614 | --size; |
| 1615 | } |
| 1616 | |
Luca Ceresoli | d3c65b0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1617 | while ((--size > 0) && *src && (*src != '"')) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1618 | *dst++ = *src++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1619 | *dst = '\0'; |
| 1620 | } |
| 1621 | |
Luca Ceresoli | 3e38e42 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1622 | #if defined(CONFIG_CMD_NFS) || \ |
| 1623 | defined(CONFIG_CMD_SNTP) || \ |
| 1624 | defined(CONFIG_CMD_DNS) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1625 | /* |
Robin Getz | 9739946 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1626 | * make port a little random (1024-17407) |
| 1627 | * This keeps the math somewhat trivial to compute, and seems to work with |
| 1628 | * all supported protocols/clients/servers |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1629 | */ |
| 1630 | unsigned int random_port(void) |
| 1631 | { |
Robin Getz | 9739946 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1632 | return 1024 + (get_timer(0) % 0x4000); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1633 | } |
| 1634 | #endif |
| 1635 | |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1636 | void ip_to_string(IPaddr_t x, char *s) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1637 | { |
Luca Ceresoli | 4f63acd | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1638 | x = ntohl(x); |
| 1639 | sprintf(s, "%d.%d.%d.%d", |
| 1640 | (int) ((x >> 24) & 0xff), |
| 1641 | (int) ((x >> 16) & 0xff), |
| 1642 | (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1643 | ); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1646 | void VLAN_to_string(ushort x, char *s) |
| 1647 | { |
| 1648 | x = ntohs(x); |
| 1649 | |
| 1650 | if (x == (ushort)-1) |
| 1651 | x = VLAN_NONE; |
| 1652 | |
| 1653 | if (x == VLAN_NONE) |
| 1654 | strcpy(s, "none"); |
| 1655 | else |
| 1656 | sprintf(s, "%d", x & VLAN_IDMASK); |
| 1657 | } |
| 1658 | |
Mike Frysinger | 2e3ef6e | 2010-10-20 07:16:48 -0400 | [diff] [blame] | 1659 | ushort string_to_VLAN(const char *s) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1660 | { |
| 1661 | ushort id; |
| 1662 | |
| 1663 | if (s == NULL) |
wdenk | b9711de | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1664 | return htons(VLAN_NONE); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (*s < '0' || *s > '9') |
| 1667 | id = VLAN_NONE; |
| 1668 | else |
| 1669 | id = (ushort)simple_strtoul(s, NULL, 10); |
| 1670 | |
wdenk | b9711de | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1671 | return htons(id); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1674 | ushort getenv_VLAN(char *var) |
| 1675 | { |
Luca Ceresoli | 92895de | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1676 | return string_to_VLAN(getenv(var)); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1677 | } |