net.c 36 KB

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