net.c 44 KB

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