net.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. * General Desription:
  12. *
  13. * The user interface supports commands for BOOTP, RARP, and TFTP.
  14. * Also, we support ARP internally. Depending on available data,
  15. * these interact as follows:
  16. *
  17. * BOOTP:
  18. *
  19. * Prerequisites: - own ethernet address
  20. * We want: - own IP address
  21. * - TFTP server IP address
  22. * - name of bootfile
  23. * Next step: ARP
  24. *
  25. * RARP:
  26. *
  27. * Prerequisites: - own ethernet address
  28. * We want: - own IP address
  29. * - TFTP server IP address
  30. * Next step: ARP
  31. *
  32. * ARP:
  33. *
  34. * Prerequisites: - own ethernet address
  35. * - own IP address
  36. * - TFTP server IP address
  37. * We want: - TFTP server ethernet address
  38. * Next step: TFTP
  39. *
  40. * DHCP:
  41. *
  42. * Prerequisites: - own ethernet address
  43. * We want: - IP, Netmask, ServerIP, Gateway IP
  44. * - bootfilename, lease time
  45. * Next step: - TFTP
  46. *
  47. * TFTP:
  48. *
  49. * Prerequisites: - own ethernet address
  50. * - own IP address
  51. * - TFTP server IP address
  52. * - TFTP server ethernet address
  53. * - name of bootfile (if unknown, we use a default name
  54. * derived from our own IP address)
  55. * We want: - load the boot file
  56. * Next step: none
  57. *
  58. * NFS:
  59. *
  60. * Prerequisites: - own ethernet address
  61. * - own IP address
  62. * - name of bootfile (if unknown, we use a default name
  63. * derived from our own IP address)
  64. * We want: - load the boot file
  65. * Next step: none
  66. */
  67. #include <common.h>
  68. #include <watchdog.h>
  69. #include <command.h>
  70. #include <net.h>
  71. #include "bootp.h"
  72. #include "tftp.h"
  73. #include "rarp.h"
  74. #include "nfs.h"
  75. #ifdef CONFIG_STATUS_LED
  76. #include <status_led.h>
  77. #include <miiphy.h>
  78. #endif
  79. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  80. #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
  81. #ifndef CONFIG_NET_RETRY_COUNT
  82. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  83. #else
  84. # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  85. #endif
  86. #if 0
  87. #define ET_DEBUG
  88. #endif
  89. /** BOOTP EXTENTIONS **/
  90. IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
  91. IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
  92. IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
  93. #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
  94. IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
  95. #endif
  96. char NetOurNISDomain[32]={0,}; /* Our NIS domain */
  97. char NetOurHostName[32]={0,}; /* Our hostname */
  98. char NetOurRootPath[64]={0,}; /* Our bootpath */
  99. ushort NetBootFileSize=0; /* Our bootfile size in blocks */
  100. /** END OF BOOTP EXTENTIONS **/
  101. ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
  102. uchar NetOurEther[6]; /* Our ethernet address */
  103. uchar NetServerEther[6] = /* Boot server enet address */
  104. { 0, 0, 0, 0, 0, 0 };
  105. IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  106. IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
  107. volatile uchar *NetRxPkt; /* Current receive packet */
  108. int NetRxPktLen; /* Current rx packet length */
  109. unsigned NetIPID; /* IP packet ID */
  110. uchar NetBcastAddr[6] = /* Ethernet bcast address */
  111. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  112. uchar NetEtherNullAddr[6] =
  113. { 0, 0, 0, 0, 0, 0 };
  114. int NetState; /* Network loop state */
  115. #ifdef CONFIG_NET_MULTI
  116. int NetRestartWrap = 0; /* Tried all network devices */
  117. static int NetRestarted = 0; /* Network loop restarted */
  118. static int NetDevExists = 0; /* At least one device configured */
  119. #endif
  120. char BootFile[128]; /* Boot File name */
  121. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  122. IPaddr_t NetPingIP; /* the ip address to ping */
  123. static void PingStart(void);
  124. #endif
  125. volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  126. volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
  127. static rxhand_f *packetHandler; /* Current RX packet handler */
  128. static thand_f *timeHandler; /* Current timeout handler */
  129. static ulong timeStart; /* Time base value */
  130. static ulong timeDelta; /* Current timeout value */
  131. volatile uchar *NetTxPacket = 0; /* THE transmit packet */
  132. static int net_check_prereq (proto_t protocol);
  133. /**********************************************************************/
  134. IPaddr_t NetArpWaitPacketIP;
  135. IPaddr_t NetArpWaitReplyIP;
  136. uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
  137. uchar *NetArpWaitTxPacket; /* THE transmit packet */
  138. int NetArpWaitTxPacketSize;
  139. uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
  140. ulong NetArpWaitTimerStart;
  141. int NetArpWaitTry;
  142. void ArpRequest(void)
  143. {
  144. int i;
  145. volatile uchar *pkt;
  146. ARP_t * arp;
  147. #ifdef ET_DEBUG
  148. printf("ARP broadcast %d\n", NetArpWaitTry);
  149. #endif
  150. pkt = NetTxPacket;
  151. NetSetEther(pkt, NetBcastAddr, PROT_ARP);
  152. pkt += ETHER_HDR_SIZE;
  153. arp = (ARP_t *)pkt;
  154. arp->ar_hrd = htons(ARP_ETHER);
  155. arp->ar_pro = htons(PROT_IP);
  156. arp->ar_hln = 6;
  157. arp->ar_pln = 4;
  158. arp->ar_op = htons(ARPOP_REQUEST);
  159. memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
  160. NetWriteIP((uchar*)&arp->ar_data[6], NetOurIP); /* source IP addr */
  161. for (i=10; i<16; ++i) {
  162. arp->ar_data[i] = 0; /* dest ET addr = 0 */
  163. }
  164. if((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) {
  165. if (NetOurGatewayIP == 0) {
  166. puts ("## Warning: gatewayip needed but not set\n");
  167. }
  168. NetArpWaitReplyIP = NetOurGatewayIP;
  169. } else
  170. NetArpWaitReplyIP = NetArpWaitPacketIP;
  171. NetWriteIP((uchar*)&arp->ar_data[16], NetArpWaitReplyIP);
  172. (void) eth_send(NetTxPacket, ETHER_HDR_SIZE + ARP_HDR_SIZE);
  173. }
  174. void ArpTimeoutCheck(void)
  175. {
  176. ulong t;
  177. if (!NetArpWaitPacketIP)
  178. return;
  179. t = get_timer(0);
  180. /* check for arp timeout */
  181. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
  182. NetArpWaitTry++;
  183. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  184. puts ("\nARP Retry count exceeded; starting again\n");
  185. NetArpWaitTry = 0;
  186. NetStartAgain();
  187. } else {
  188. NetArpWaitTimerStart = t;
  189. ArpRequest();
  190. }
  191. }
  192. }
  193. /**********************************************************************/
  194. /*
  195. * Main network processing loop.
  196. */
  197. int
  198. NetLoop(proto_t protocol)
  199. {
  200. DECLARE_GLOBAL_DATA_PTR;
  201. bd_t *bd = gd->bd;
  202. #ifdef CONFIG_NET_MULTI
  203. NetRestarted = 0;
  204. NetDevExists = 0;
  205. #endif
  206. /* XXX problem with bss workaround */
  207. NetArpWaitPacketMAC = NULL;
  208. NetArpWaitTxPacket = NULL;
  209. NetArpWaitPacketIP = 0;
  210. NetArpWaitReplyIP = 0;
  211. NetArpWaitTxPacket = NULL;
  212. NetTxPacket = NULL;
  213. if (!NetTxPacket) {
  214. int i;
  215. /*
  216. * Setup packet buffers, aligned correctly.
  217. */
  218. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  219. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  220. for (i = 0; i < PKTBUFSRX; i++) {
  221. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  222. }
  223. }
  224. if (!NetArpWaitTxPacket) {
  225. NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
  226. NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
  227. NetArpWaitTxPacketSize = 0;
  228. }
  229. eth_halt();
  230. if(eth_init(bd) < 0)
  231. return(-1);
  232. restart:
  233. #ifdef CONFIG_NET_MULTI
  234. memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
  235. #else
  236. memcpy (NetOurEther, bd->bi_enetaddr, 6);
  237. #endif
  238. NetState = NETLOOP_CONTINUE;
  239. /*
  240. * Start the ball rolling with the given start function. From
  241. * here on, this code is a state machine driven by received
  242. * packets and timer events.
  243. */
  244. switch (protocol) {
  245. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  246. case NFS:
  247. #endif
  248. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  249. case PING:
  250. #endif
  251. case TFTP:
  252. NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
  253. NetOurGatewayIP = getenv_IPaddr ("gatewayip");
  254. NetOurSubnetMask= getenv_IPaddr ("netmask");
  255. switch (protocol) {
  256. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  257. case NFS:
  258. #endif
  259. case TFTP:
  260. NetServerIP = getenv_IPaddr ("serverip");
  261. break;
  262. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  263. case PING:
  264. /* nothing */
  265. break;
  266. #endif
  267. default:
  268. break;
  269. }
  270. break;
  271. case BOOTP:
  272. case RARP:
  273. /*
  274. * initialize our IP addr to 0 in order to accept ANY
  275. * IP addr assigned to us by the BOOTP / RARP server
  276. */
  277. NetOurIP = 0;
  278. NetServerIP = getenv_IPaddr ("serverip");
  279. break;
  280. default:
  281. break;
  282. }
  283. switch (net_check_prereq (protocol)) {
  284. case 1:
  285. /* network not configured */
  286. return (-1);
  287. #ifdef CONFIG_NET_MULTI
  288. case 2:
  289. /* network device not configured */
  290. break;
  291. #endif /* CONFIG_NET_MULTI */
  292. case 0:
  293. #ifdef CONFIG_NET_MULTI
  294. NetDevExists = 1;
  295. #endif
  296. switch (protocol) {
  297. case TFTP:
  298. /* always use ARP to get server ethernet address */
  299. TftpStart();
  300. break;
  301. #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
  302. case DHCP:
  303. /* Start with a clean slate... */
  304. NetOurIP = 0;
  305. NetServerIP = getenv_IPaddr ("serverip");
  306. DhcpRequest(); /* Basically same as BOOTP */
  307. break;
  308. #endif /* CFG_CMD_DHCP */
  309. case BOOTP:
  310. BootpTry = 0;
  311. BootpRequest ();
  312. break;
  313. case RARP:
  314. RarpTry = 0;
  315. RarpRequest ();
  316. break;
  317. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  318. case PING:
  319. PingStart();
  320. break;
  321. #endif
  322. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  323. case NFS:
  324. NfsStart();
  325. break;
  326. #endif
  327. default:
  328. break;
  329. }
  330. NetBootFileXferSize = 0;
  331. break;
  332. }
  333. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  334. #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  335. /*
  336. * Echo the inverted link state to the fault LED.
  337. */
  338. if(miiphy_link(CFG_FAULT_MII_ADDR)) {
  339. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  340. } else {
  341. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  342. }
  343. #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
  344. #endif /* CONFIG_MII, ... */
  345. /*
  346. * Main packet reception loop. Loop receiving packets until
  347. * someone sets `NetQuit'.
  348. */
  349. for (;;) {
  350. WATCHDOG_RESET();
  351. #ifdef CONFIG_SHOW_ACTIVITY
  352. {
  353. extern void show_activity(int arg);
  354. show_activity(1);
  355. }
  356. #endif
  357. /*
  358. * Check the ethernet for a new packet. The ethernet
  359. * receive routine will process it.
  360. */
  361. eth_rx();
  362. /*
  363. * Abort if ctrl-c was pressed.
  364. */
  365. if (ctrlc()) {
  366. eth_halt();
  367. puts ("\nAbort\n");
  368. return (-1);
  369. }
  370. ArpTimeoutCheck();
  371. /*
  372. * Check for a timeout, and run the timeout handler
  373. * if we have one.
  374. */
  375. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  376. thand_f *x;
  377. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  378. #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  379. /*
  380. * Echo the inverted link state to the fault LED.
  381. */
  382. if(miiphy_link(CFG_FAULT_MII_ADDR)) {
  383. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  384. } else {
  385. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  386. }
  387. #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
  388. #endif /* CONFIG_MII, ... */
  389. x = timeHandler;
  390. timeHandler = (thand_f *)0;
  391. (*x)();
  392. }
  393. switch (NetState) {
  394. case NETLOOP_RESTART:
  395. #ifdef CONFIG_NET_MULTI
  396. NetRestarted = 1;
  397. #endif
  398. goto restart;
  399. case NETLOOP_SUCCESS:
  400. if (NetBootFileXferSize > 0) {
  401. char buf[10];
  402. printf("Bytes transferred = %ld (%lx hex)\n",
  403. NetBootFileXferSize,
  404. NetBootFileXferSize);
  405. sprintf(buf, "%lx", NetBootFileXferSize);
  406. setenv("filesize", buf);
  407. }
  408. eth_halt();
  409. return NetBootFileXferSize;
  410. case NETLOOP_FAIL:
  411. return (-1);
  412. }
  413. }
  414. }
  415. /**********************************************************************/
  416. static void
  417. startAgainTimeout(void)
  418. {
  419. NetState = NETLOOP_RESTART;
  420. }
  421. static void
  422. startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
  423. {
  424. /* Totally ignore the packet */
  425. }
  426. void
  427. NetStartAgain(void)
  428. {
  429. #ifndef CONFIG_NET_MULTI
  430. NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
  431. NetSetHandler(startAgainHandler);
  432. #else
  433. DECLARE_GLOBAL_DATA_PTR;
  434. eth_halt();
  435. eth_try_another(!NetRestarted);
  436. eth_init(gd->bd);
  437. if (NetRestartWrap)
  438. {
  439. NetRestartWrap = 0;
  440. if (NetDevExists)
  441. {
  442. NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
  443. NetSetHandler(startAgainHandler);
  444. }
  445. else
  446. {
  447. NetState = NETLOOP_FAIL;
  448. }
  449. }
  450. else
  451. {
  452. NetState = NETLOOP_RESTART;
  453. }
  454. #endif
  455. }
  456. /**********************************************************************/
  457. /*
  458. * Miscelaneous bits.
  459. */
  460. void
  461. NetSetHandler(rxhand_f * f)
  462. {
  463. packetHandler = f;
  464. }
  465. void
  466. NetSetTimeout(int iv, thand_f * f)
  467. {
  468. if (iv == 0) {
  469. timeHandler = (thand_f *)0;
  470. } else {
  471. timeHandler = f;
  472. timeStart = get_timer(0);
  473. timeDelta = iv;
  474. }
  475. }
  476. void
  477. NetSendPacket(volatile uchar * pkt, int len)
  478. {
  479. (void) eth_send(pkt, len);
  480. }
  481. int
  482. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  483. {
  484. /* convert to new style broadcast */
  485. if (dest == 0)
  486. dest = 0xFFFFFFFF;
  487. /* if broadcast, make the ether address a broadcast and don't do ARP */
  488. if (dest == 0xFFFFFFFF)
  489. ether = NetBcastAddr;
  490. /* if MAC address was not discovered yet, save the packet and do an ARP request */
  491. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  492. #ifdef ET_DEBUG
  493. printf("sending ARP for %08lx\n", dest);
  494. #endif
  495. NetArpWaitPacketIP = dest;
  496. NetArpWaitPacketMAC = ether;
  497. NetSetEther (NetArpWaitTxPacket, NetArpWaitPacketMAC, PROT_IP);
  498. NetSetIP (NetArpWaitTxPacket + ETHER_HDR_SIZE, dest, dport, sport, len);
  499. memcpy(NetArpWaitTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE,
  500. (uchar *)NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE, len);
  501. /* size of the waiting packet */
  502. NetArpWaitTxPacketSize = ETHER_HDR_SIZE + IP_HDR_SIZE + len;
  503. /* and do the ARP request */
  504. NetArpWaitTry = 1;
  505. NetArpWaitTimerStart = get_timer(0);
  506. ArpRequest();
  507. return 1; /* waiting */
  508. }
  509. #ifdef ET_DEBUG
  510. printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
  511. dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
  512. #endif
  513. NetSetEther (NetTxPacket, ether, PROT_IP);
  514. NetSetIP (NetTxPacket + ETHER_HDR_SIZE, dest, dport, sport, len);
  515. (void) eth_send(NetTxPacket, ETHER_HDR_SIZE + IP_HDR_SIZE + len);
  516. return 0; /* transmited */
  517. }
  518. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  519. static ushort PingSeqNo;
  520. int PingSend(void)
  521. {
  522. static uchar mac[6];
  523. volatile IP_t *ip;
  524. volatile ushort *s;
  525. /* XXX always send arp request */
  526. memcpy(mac, NetEtherNullAddr, 6);
  527. #ifdef ET_DEBUG
  528. printf("sending ARP for %08lx\n", NetPingIP);
  529. #endif
  530. NetArpWaitPacketIP = NetPingIP;
  531. NetArpWaitPacketMAC = mac;
  532. NetSetEther(NetArpWaitTxPacket, mac, PROT_IP);
  533. ip = (volatile IP_t *)(NetArpWaitTxPacket + ETHER_HDR_SIZE);
  534. /*
  535. * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
  536. */
  537. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  538. ip->ip_tos = 0;
  539. ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
  540. ip->ip_id = htons(NetIPID++);
  541. ip->ip_off = htons(0x4000); /* No fragmentation */
  542. ip->ip_ttl = 255;
  543. ip->ip_p = 0x01; /* ICMP */
  544. ip->ip_sum = 0;
  545. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  546. NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
  547. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  548. s = &ip->udp_src; /* XXX ICMP starts here */
  549. s[0] = htons(0x0800); /* echo-request, code */
  550. s[1] = 0; /* checksum */
  551. s[2] = 0; /* identifier */
  552. s[3] = htons(PingSeqNo++); /* sequence number */
  553. s[1] = ~NetCksum((uchar *)s, 8/2);
  554. /* size of the waiting packet */
  555. NetArpWaitTxPacketSize = ETHER_HDR_SIZE + IP_HDR_SIZE_NO_UDP + 8;
  556. /* and do the ARP request */
  557. NetArpWaitTry = 1;
  558. NetArpWaitTimerStart = get_timer(0);
  559. ArpRequest();
  560. return 1; /* waiting */
  561. }
  562. static void
  563. PingTimeout (void)
  564. {
  565. eth_halt();
  566. NetState = NETLOOP_FAIL; /* we did not get the reply */
  567. }
  568. static void
  569. PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  570. {
  571. IPaddr_t tmp;
  572. volatile IP_t *ip = (volatile IP_t *)pkt;
  573. tmp = NetReadIP((void *)&ip->ip_src);
  574. if (tmp != NetPingIP)
  575. return;
  576. NetState = NETLOOP_SUCCESS;
  577. }
  578. static void PingStart(void)
  579. {
  580. NetSetTimeout (10 * CFG_HZ, PingTimeout);
  581. NetSetHandler (PingHandler);
  582. PingSend();
  583. }
  584. #endif
  585. void
  586. NetReceive(volatile uchar * pkt, int len)
  587. {
  588. Ethernet_t *et;
  589. IP_t *ip;
  590. ARP_t *arp;
  591. IPaddr_t tmp;
  592. int x;
  593. NetRxPkt = pkt;
  594. NetRxPktLen = len;
  595. et = (Ethernet_t *)pkt;
  596. x = ntohs(et->et_protlen);
  597. if (x < 1514) {
  598. /*
  599. * Got a 802 packet. Check the other protocol field.
  600. */
  601. x = ntohs(et->et_prot);
  602. ip = (IP_t *)(pkt + E802_HDR_SIZE);
  603. len -= E802_HDR_SIZE;
  604. } else {
  605. ip = (IP_t *)(pkt + ETHER_HDR_SIZE);
  606. len -= ETHER_HDR_SIZE;
  607. }
  608. #ifdef ET_DEBUG
  609. printf("Receive from protocol 0x%x\n", x);
  610. #endif
  611. switch (x) {
  612. case PROT_ARP:
  613. /*
  614. * We have to deal with two types of ARP packets:
  615. * - REQUEST packets will be answered by sending our
  616. * IP address - if we know it.
  617. * - REPLY packates are expected only after we asked
  618. * for the TFTP server's or the gateway's ethernet
  619. * address; so if we receive such a packet, we set
  620. * the server ethernet address
  621. */
  622. #ifdef ET_DEBUG
  623. puts ("Got ARP\n");
  624. #endif
  625. arp = (ARP_t *)ip;
  626. if (len < ARP_HDR_SIZE) {
  627. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  628. return;
  629. }
  630. if (ntohs(arp->ar_hrd) != ARP_ETHER) {
  631. return;
  632. }
  633. if (ntohs(arp->ar_pro) != PROT_IP) {
  634. return;
  635. }
  636. if (arp->ar_hln != 6) {
  637. return;
  638. }
  639. if (arp->ar_pln != 4) {
  640. return;
  641. }
  642. if (NetOurIP == 0) {
  643. return;
  644. }
  645. if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
  646. return;
  647. }
  648. switch (ntohs(arp->ar_op)) {
  649. case ARPOP_REQUEST: /* reply with our IP address */
  650. #ifdef ET_DEBUG
  651. puts ("Got ARP REQUEST, return our IP\n");
  652. #endif
  653. NetSetEther((uchar *)et, et->et_src, PROT_ARP);
  654. arp->ar_op = htons(ARPOP_REPLY);
  655. memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
  656. NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
  657. memcpy (&arp->ar_data[ 0], NetOurEther, 6);
  658. NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
  659. (void) eth_send((uchar *)et, ((uchar *)arp-pkt) + ARP_HDR_SIZE);
  660. return;
  661. case ARPOP_REPLY: /* arp reply */
  662. /* are we waiting for a reply */
  663. if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
  664. break;
  665. #ifdef ET_DEBUG
  666. printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
  667. arp->ar_data[0], arp->ar_data[1],
  668. arp->ar_data[2], arp->ar_data[3],
  669. arp->ar_data[4], arp->ar_data[5]);
  670. #endif
  671. tmp = NetReadIP(&arp->ar_data[6]);
  672. /* matched waiting packet's address */
  673. if (tmp == NetArpWaitReplyIP) {
  674. #ifdef ET_DEBUG
  675. puts ("Got it\n");
  676. #endif
  677. /* save address for later use */
  678. memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
  679. /* modify header, and transmit it */
  680. memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
  681. (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
  682. /* no arp request pending now */
  683. NetArpWaitPacketIP = 0;
  684. NetArpWaitTxPacketSize = 0;
  685. NetArpWaitPacketMAC = NULL;
  686. }
  687. return;
  688. default:
  689. #ifdef ET_DEBUG
  690. printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
  691. #endif
  692. return;
  693. }
  694. case PROT_RARP:
  695. #ifdef ET_DEBUG
  696. puts ("Got RARP\n");
  697. #endif
  698. arp = (ARP_t *)ip;
  699. if (len < ARP_HDR_SIZE) {
  700. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  701. return;
  702. }
  703. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  704. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  705. (ntohs(arp->ar_pro) != PROT_IP) ||
  706. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  707. puts ("invalid RARP header\n");
  708. } else {
  709. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  710. if (NetServerIP == 0)
  711. NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
  712. memcpy (NetServerEther, &arp->ar_data[ 0], 6);
  713. (*packetHandler)(0,0,0,0);
  714. }
  715. break;
  716. case PROT_IP:
  717. #ifdef ET_DEBUG
  718. puts ("Got IP\n");
  719. #endif
  720. if (len < IP_HDR_SIZE) {
  721. debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
  722. return;
  723. }
  724. if (len < ntohs(ip->ip_len)) {
  725. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  726. return;
  727. }
  728. len = ntohs(ip->ip_len);
  729. #ifdef ET_DEBUG
  730. printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  731. #endif
  732. if ((ip->ip_hl_v & 0xf0) != 0x40) {
  733. return;
  734. }
  735. if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
  736. return;
  737. }
  738. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  739. puts ("checksum bad\n");
  740. return;
  741. }
  742. tmp = NetReadIP(&ip->ip_dst);
  743. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  744. return;
  745. }
  746. /*
  747. * watch for ICMP host redirects
  748. *
  749. * There is no real handler code (yet). We just watch
  750. * for ICMP host redirect messages. In case anybody
  751. * sees these messages: please contact me
  752. * (wd@denx.de), or - even better - send me the
  753. * necessary fixes :-)
  754. *
  755. * Note: in all cases where I have seen this so far
  756. * it was a problem with the router configuration,
  757. * for instance when a router was configured in the
  758. * BOOTP reply, but the TFTP server was on the same
  759. * subnet. So this is probably a warning that your
  760. * configuration might be wrong. But I'm not really
  761. * sure if there aren't any other situations.
  762. */
  763. if (ip->ip_p == IPPROTO_ICMP) {
  764. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  765. switch (icmph->type) {
  766. case ICMP_REDIRECT:
  767. if (icmph->code != ICMP_REDIR_HOST)
  768. return;
  769. puts (" ICMP Host Redirect to ");
  770. print_IPaddr(icmph->un.gateway);
  771. putc(' ');
  772. break;
  773. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  774. case ICMP_ECHO_REPLY:
  775. /*
  776. * IP header OK. Pass the packet to the current handler.
  777. */
  778. /* XXX point to ip packet */
  779. (*packetHandler)((uchar *)ip, 0, 0, 0);
  780. break;
  781. #endif
  782. default:
  783. return;
  784. }
  785. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  786. return;
  787. }
  788. /*
  789. * IP header OK. Pass the packet to the current handler.
  790. */
  791. (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
  792. ntohs(ip->udp_dst),
  793. ntohs(ip->udp_src),
  794. ntohs(ip->udp_len) - 8);
  795. break;
  796. }
  797. }
  798. /**********************************************************************/
  799. static int net_check_prereq (proto_t protocol)
  800. {
  801. switch (protocol) {
  802. /* Fall through */
  803. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  804. case PING:
  805. if (NetPingIP == 0) {
  806. puts ("*** ERROR: ping address not given\n");
  807. return (1);
  808. }
  809. goto common;
  810. #endif
  811. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  812. case NFS:
  813. #endif
  814. case TFTP:
  815. if (NetServerIP == 0) {
  816. puts ("*** ERROR: `serverip' not set\n");
  817. return (1);
  818. }
  819. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  820. common:
  821. #endif
  822. if (NetOurIP == 0) {
  823. puts ("*** ERROR: `ipaddr' not set\n");
  824. return (1);
  825. }
  826. /* Fall through */
  827. case DHCP:
  828. case RARP:
  829. case BOOTP:
  830. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  831. #ifdef CONFIG_NET_MULTI
  832. extern int eth_get_dev_index (void);
  833. int num = eth_get_dev_index();
  834. switch (num) {
  835. case -1:
  836. puts ("*** ERROR: No ethernet found.\n");
  837. return (1);
  838. case 0:
  839. puts ("*** ERROR: `ethaddr' not set\n");
  840. break;
  841. default:
  842. printf ("*** ERROR: `eth%daddr' not set\n",
  843. num);
  844. break;
  845. }
  846. NetStartAgain ();
  847. return (2);
  848. #else
  849. puts ("*** ERROR: `ethaddr' not set\n");
  850. return (1);
  851. #endif
  852. }
  853. /* Fall through */
  854. default:
  855. return(0);
  856. }
  857. return (0); /* OK */
  858. }
  859. /**********************************************************************/
  860. int
  861. NetCksumOk(uchar * ptr, int len)
  862. {
  863. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  864. }
  865. unsigned
  866. NetCksum(uchar * ptr, int len)
  867. {
  868. ulong xsum;
  869. xsum = 0;
  870. while (len-- > 0)
  871. xsum += *((ushort *)ptr)++;
  872. xsum = (xsum & 0xffff) + (xsum >> 16);
  873. xsum = (xsum & 0xffff) + (xsum >> 16);
  874. return (xsum & 0xffff);
  875. }
  876. void
  877. NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
  878. {
  879. Ethernet_t *et = (Ethernet_t *)xet;
  880. memcpy (et->et_dest, addr, 6);
  881. memcpy (et->et_src, NetOurEther, 6);
  882. et->et_protlen = htons(prot);
  883. }
  884. void
  885. NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
  886. {
  887. volatile IP_t *ip = (IP_t *)xip;
  888. /*
  889. * If the data is an odd number of bytes, zero the
  890. * byte after the last byte so that the checksum
  891. * will work.
  892. */
  893. if (len & 1)
  894. xip[IP_HDR_SIZE + len] = 0;
  895. /*
  896. * Construct an IP and UDP header.
  897. (need to set no fragment bit - XXX)
  898. */
  899. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  900. ip->ip_tos = 0;
  901. ip->ip_len = htons(IP_HDR_SIZE + len);
  902. ip->ip_id = htons(NetIPID++);
  903. ip->ip_off = htons(0x4000); /* No fragmentation */
  904. ip->ip_ttl = 255;
  905. ip->ip_p = 17; /* UDP */
  906. ip->ip_sum = 0;
  907. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  908. NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
  909. ip->udp_src = htons(sport);
  910. ip->udp_dst = htons(dport);
  911. ip->udp_len = htons(8 + len);
  912. ip->udp_xsum = 0;
  913. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  914. }
  915. void copy_filename (uchar *dst, uchar *src, int size)
  916. {
  917. if (*src && (*src == '"')) {
  918. ++src;
  919. --size;
  920. }
  921. while ((--size > 0) && *src && (*src != '"')) {
  922. *dst++ = *src++;
  923. }
  924. *dst = '\0';
  925. }
  926. #endif /* CFG_CMD_NET */
  927. void ip_to_string (IPaddr_t x, char *s)
  928. {
  929. x = ntohl(x);
  930. sprintf (s,"%d.%d.%d.%d",
  931. (int)((x >> 24) & 0xff),
  932. (int)((x >> 16) & 0xff),
  933. (int)((x >> 8) & 0xff),
  934. (int)((x >> 0) & 0xff)
  935. );
  936. }
  937. IPaddr_t string_to_ip(char *s)
  938. {
  939. IPaddr_t addr;
  940. char *e;
  941. int i;
  942. if (s == NULL)
  943. return(0);
  944. for (addr=0, i=0; i<4; ++i) {
  945. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  946. addr <<= 8;
  947. addr |= (val & 0xFF);
  948. if (s) {
  949. s = (*e) ? e+1 : e;
  950. }
  951. }
  952. return (htonl(addr));
  953. }
  954. void print_IPaddr (IPaddr_t x)
  955. {
  956. char tmp[16];
  957. ip_to_string(x, tmp);
  958. puts(tmp);
  959. }
  960. IPaddr_t getenv_IPaddr (char *var)
  961. {
  962. return (string_to_ip(getenv(var)));
  963. }