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