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