net.c 38 KB

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