net.c 44 KB

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