net.c 44 KB

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