net.c 36 KB

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