net.c 45 KB

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