net.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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 <linux/compiler.h>
  78. #include <net.h>
  79. #include "arp.h"
  80. #include "bootp.h"
  81. #include "tftp.h"
  82. #ifdef CONFIG_CMD_RARP
  83. #include "rarp.h"
  84. #endif
  85. #include "nfs.h"
  86. #ifdef CONFIG_STATUS_LED
  87. #include <status_led.h>
  88. #include <miiphy.h>
  89. #endif
  90. #if defined(CONFIG_CMD_SNTP)
  91. #include "sntp.h"
  92. #endif
  93. #include "cdp.h"
  94. #if defined(CONFIG_CMD_DNS)
  95. #include "dns.h"
  96. #endif
  97. DECLARE_GLOBAL_DATA_PTR;
  98. /** BOOTP EXTENTIONS **/
  99. /* Our subnet mask (0=unknown) */
  100. IPaddr_t NetOurSubnetMask;
  101. /* Our gateways IP address */
  102. IPaddr_t NetOurGatewayIP;
  103. /* Our DNS IP address */
  104. IPaddr_t NetOurDNSIP;
  105. #if defined(CONFIG_BOOTP_DNS2)
  106. /* Our 2nd DNS IP address */
  107. IPaddr_t NetOurDNS2IP;
  108. #endif
  109. /* Our NIS domain */
  110. char NetOurNISDomain[32] = {0,};
  111. /* Our hostname */
  112. char NetOurHostName[32] = {0,};
  113. /* Our bootpath */
  114. char NetOurRootPath[64] = {0,};
  115. /* Our bootfile size in blocks */
  116. ushort NetBootFileSize;
  117. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  118. IPaddr_t Mcast_addr;
  119. #endif
  120. /** END OF BOOTP EXTENTIONS **/
  121. /* The actual transferred size of the bootfile (in bytes) */
  122. ulong NetBootFileXferSize;
  123. /* Our ethernet address */
  124. uchar NetOurEther[6];
  125. /* Boot server enet address */
  126. uchar NetServerEther[6];
  127. /* Our IP addr (0 = unknown) */
  128. IPaddr_t NetOurIP;
  129. /* Server IP addr (0 = unknown) */
  130. IPaddr_t NetServerIP;
  131. /* Current receive packet */
  132. uchar *NetRxPacket;
  133. /* Current rx packet length */
  134. int NetRxPacketLen;
  135. /* IP packet ID */
  136. unsigned NetIPID;
  137. /* Ethernet bcast address */
  138. uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  139. uchar NetEtherNullAddr[6];
  140. #ifdef CONFIG_API
  141. void (*push_packet)(void *, int len) = 0;
  142. #endif
  143. /* Network loop state */
  144. int NetState;
  145. /* Tried all network devices */
  146. int NetRestartWrap;
  147. /* Network loop restarted */
  148. static int NetRestarted;
  149. /* At least one device configured */
  150. static int NetDevExists;
  151. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  152. /* default is without VLAN */
  153. ushort NetOurVLAN = 0xFFFF;
  154. /* ditto */
  155. ushort NetOurNativeVLAN = 0xFFFF;
  156. /* Boot File name */
  157. char BootFile[128];
  158. #if defined(CONFIG_CMD_PING)
  159. /* the ip address to ping */
  160. IPaddr_t NetPingIP;
  161. static void PingStart(void);
  162. #endif
  163. #if defined(CONFIG_CMD_SNTP)
  164. /* NTP server IP address */
  165. IPaddr_t NetNtpServerIP;
  166. /* offset time from UTC */
  167. int NetTimeOffset;
  168. #endif
  169. uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  170. /* Receive packet */
  171. uchar *NetRxPackets[PKTBUFSRX];
  172. /* Current RX packet handler */
  173. static rxhand_f *packetHandler;
  174. #ifdef CONFIG_CMD_TFTPPUT
  175. static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
  176. #endif
  177. /* Current timeout handler */
  178. static thand_f *timeHandler;
  179. /* Time base value */
  180. static ulong timeStart;
  181. /* Current timeout value */
  182. static ulong timeDelta;
  183. /* THE transmit packet */
  184. uchar *NetTxPacket;
  185. static int net_check_prereq(enum proto_t protocol);
  186. static int NetTryCount;
  187. /**********************************************************************/
  188. /*
  189. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  190. * the boot file.
  191. */
  192. void net_auto_load(void)
  193. {
  194. const char *s = getenv("autoload");
  195. if (s != NULL) {
  196. if (*s == 'n') {
  197. /*
  198. * Just use BOOTP/RARP to configure system;
  199. * Do not use TFTP to load the bootfile.
  200. */
  201. NetState = NETLOOP_SUCCESS;
  202. return;
  203. }
  204. #if defined(CONFIG_CMD_NFS)
  205. if (strcmp(s, "NFS") == 0) {
  206. /*
  207. * Use NFS to load the bootfile.
  208. */
  209. NfsStart();
  210. return;
  211. }
  212. #endif
  213. }
  214. TftpStart(TFTPGET);
  215. }
  216. static void NetInitLoop(enum proto_t protocol)
  217. {
  218. static int env_changed_id;
  219. int env_id = get_env_id();
  220. /* update only when the environment has changed */
  221. if (env_changed_id != env_id) {
  222. NetOurIP = getenv_IPaddr("ipaddr");
  223. NetOurGatewayIP = getenv_IPaddr("gatewayip");
  224. NetOurSubnetMask = getenv_IPaddr("netmask");
  225. NetServerIP = getenv_IPaddr("serverip");
  226. NetOurNativeVLAN = getenv_VLAN("nvlan");
  227. NetOurVLAN = getenv_VLAN("vlan");
  228. #if defined(CONFIG_CMD_DNS)
  229. NetOurDNSIP = getenv_IPaddr("dnsip");
  230. #endif
  231. env_changed_id = env_id;
  232. }
  233. return;
  234. }
  235. /**********************************************************************/
  236. /*
  237. * Main network processing loop.
  238. */
  239. int NetLoop(enum proto_t protocol)
  240. {
  241. bd_t *bd = gd->bd;
  242. int ret = -1;
  243. NetRestarted = 0;
  244. NetDevExists = 0;
  245. NetTxPacket = NULL;
  246. NetTryCount = 1;
  247. ArpInit();
  248. if (!NetTxPacket) {
  249. int i;
  250. /*
  251. * Setup packet buffers, aligned correctly.
  252. */
  253. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  254. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  255. for (i = 0; i < PKTBUFSRX; i++)
  256. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  257. }
  258. bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
  259. eth_halt();
  260. eth_set_current();
  261. if (eth_init(bd) < 0) {
  262. eth_halt();
  263. return -1;
  264. }
  265. restart:
  266. memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
  267. NetState = NETLOOP_CONTINUE;
  268. /*
  269. * Start the ball rolling with the given start function. From
  270. * here on, this code is a state machine driven by received
  271. * packets and timer events.
  272. */
  273. NetInitLoop(protocol);
  274. switch (net_check_prereq(protocol)) {
  275. case 1:
  276. /* network not configured */
  277. eth_halt();
  278. return -1;
  279. case 2:
  280. /* network device not configured */
  281. break;
  282. case 0:
  283. NetDevExists = 1;
  284. NetBootFileXferSize = 0;
  285. switch (protocol) {
  286. case TFTPGET:
  287. #ifdef CONFIG_CMD_TFTPPUT
  288. case TFTPPUT:
  289. #endif
  290. /* always use ARP to get server ethernet address */
  291. TftpStart(protocol);
  292. break;
  293. #ifdef CONFIG_CMD_TFTPSRV
  294. case TFTPSRV:
  295. TftpStartServer();
  296. break;
  297. #endif
  298. #if defined(CONFIG_CMD_DHCP)
  299. case DHCP:
  300. BootpTry = 0;
  301. NetOurIP = 0;
  302. DhcpRequest(); /* Basically same as BOOTP */
  303. break;
  304. #endif
  305. case BOOTP:
  306. BootpTry = 0;
  307. NetOurIP = 0;
  308. BootpRequest();
  309. break;
  310. #if defined(CONFIG_CMD_RARP)
  311. case RARP:
  312. RarpTry = 0;
  313. NetOurIP = 0;
  314. RarpRequest();
  315. break;
  316. #endif
  317. #if defined(CONFIG_CMD_PING)
  318. case PING:
  319. PingStart();
  320. break;
  321. #endif
  322. #if defined(CONFIG_CMD_NFS)
  323. case NFS:
  324. NfsStart();
  325. break;
  326. #endif
  327. #if defined(CONFIG_CMD_CDP)
  328. case CDP:
  329. CDPStart();
  330. break;
  331. #endif
  332. #ifdef CONFIG_NETCONSOLE
  333. case NETCONS:
  334. NcStart();
  335. break;
  336. #endif
  337. #if defined(CONFIG_CMD_SNTP)
  338. case SNTP:
  339. SntpStart();
  340. break;
  341. #endif
  342. #if defined(CONFIG_CMD_DNS)
  343. case DNS:
  344. DnsStart();
  345. break;
  346. #endif
  347. default:
  348. break;
  349. }
  350. break;
  351. }
  352. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  353. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  354. defined(CONFIG_STATUS_LED) && \
  355. defined(STATUS_LED_RED)
  356. /*
  357. * Echo the inverted link state to the fault LED.
  358. */
  359. if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
  360. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  361. else
  362. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  363. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  364. #endif /* CONFIG_MII, ... */
  365. /*
  366. * Main packet reception loop. Loop receiving packets until
  367. * someone sets `NetState' to a state that terminates.
  368. */
  369. for (;;) {
  370. WATCHDOG_RESET();
  371. #ifdef CONFIG_SHOW_ACTIVITY
  372. show_activity(1);
  373. #endif
  374. /*
  375. * Check the ethernet for a new packet. The ethernet
  376. * receive routine will process it.
  377. */
  378. eth_rx();
  379. /*
  380. * Abort if ctrl-c was pressed.
  381. */
  382. if (ctrlc()) {
  383. eth_halt();
  384. puts("\nAbort\n");
  385. goto done;
  386. }
  387. ArpTimeoutCheck();
  388. /*
  389. * Check for a timeout, and run the timeout handler
  390. * if we have one.
  391. */
  392. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  393. thand_f *x;
  394. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  395. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  396. defined(CONFIG_STATUS_LED) && \
  397. defined(STATUS_LED_RED)
  398. /*
  399. * Echo the inverted link state to the fault LED.
  400. */
  401. if (miiphy_link(eth_get_dev()->name,
  402. CONFIG_SYS_FAULT_MII_ADDR)) {
  403. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  404. } else {
  405. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  406. }
  407. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  408. #endif /* CONFIG_MII, ... */
  409. x = timeHandler;
  410. timeHandler = (thand_f *)0;
  411. (*x)();
  412. }
  413. switch (NetState) {
  414. case NETLOOP_RESTART:
  415. NetRestarted = 1;
  416. goto restart;
  417. case NETLOOP_SUCCESS:
  418. if (NetBootFileXferSize > 0) {
  419. char buf[20];
  420. printf("Bytes transferred = %ld (%lx hex)\n",
  421. NetBootFileXferSize,
  422. NetBootFileXferSize);
  423. sprintf(buf, "%lX", NetBootFileXferSize);
  424. setenv("filesize", buf);
  425. sprintf(buf, "%lX", (unsigned long)load_addr);
  426. setenv("fileaddr", buf);
  427. }
  428. eth_halt();
  429. ret = NetBootFileXferSize;
  430. goto done;
  431. case NETLOOP_FAIL:
  432. goto done;
  433. }
  434. }
  435. done:
  436. #ifdef CONFIG_CMD_TFTPPUT
  437. /* Clear out the handlers */
  438. NetSetHandler(NULL);
  439. net_set_icmp_handler(NULL);
  440. #endif
  441. return ret;
  442. }
  443. /**********************************************************************/
  444. static void
  445. startAgainTimeout(void)
  446. {
  447. NetState = NETLOOP_RESTART;
  448. }
  449. static void
  450. startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
  451. unsigned src, unsigned len)
  452. {
  453. /* Totally ignore the packet */
  454. }
  455. void NetStartAgain(void)
  456. {
  457. char *nretry;
  458. int retry_forever = 0;
  459. unsigned long retrycnt = 0;
  460. nretry = getenv("netretry");
  461. if (nretry) {
  462. if (!strcmp(nretry, "yes"))
  463. retry_forever = 1;
  464. else if (!strcmp(nretry, "no"))
  465. retrycnt = 0;
  466. else if (!strcmp(nretry, "once"))
  467. retrycnt = 1;
  468. else
  469. retrycnt = simple_strtoul(nretry, NULL, 0);
  470. } else
  471. retry_forever = 1;
  472. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  473. eth_halt();
  474. NetState = NETLOOP_FAIL;
  475. return;
  476. }
  477. NetTryCount++;
  478. eth_halt();
  479. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  480. eth_try_another(!NetRestarted);
  481. #endif
  482. eth_init(gd->bd);
  483. if (NetRestartWrap) {
  484. NetRestartWrap = 0;
  485. if (NetDevExists) {
  486. NetSetTimeout(10000UL, startAgainTimeout);
  487. NetSetHandler(startAgainHandler);
  488. } else {
  489. NetState = NETLOOP_FAIL;
  490. }
  491. } else {
  492. NetState = NETLOOP_RESTART;
  493. }
  494. }
  495. /**********************************************************************/
  496. /*
  497. * Miscelaneous bits.
  498. */
  499. rxhand_f *
  500. NetGetHandler(void)
  501. {
  502. return packetHandler;
  503. }
  504. void
  505. NetSetHandler(rxhand_f *f)
  506. {
  507. packetHandler = f;
  508. }
  509. #ifdef CONFIG_CMD_TFTPPUT
  510. void net_set_icmp_handler(rxhand_icmp_f *f)
  511. {
  512. packet_icmp_handler = f;
  513. }
  514. #endif
  515. void
  516. NetSetTimeout(ulong iv, thand_f *f)
  517. {
  518. if (iv == 0) {
  519. timeHandler = (thand_f *)0;
  520. } else {
  521. timeHandler = f;
  522. timeStart = get_timer(0);
  523. timeDelta = iv;
  524. }
  525. }
  526. void
  527. NetSendPacket(uchar *pkt, int len)
  528. {
  529. (void) eth_send(pkt, len);
  530. }
  531. int
  532. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  533. {
  534. uchar *pkt;
  535. /* convert to new style broadcast */
  536. if (dest == 0)
  537. dest = 0xFFFFFFFF;
  538. /* if broadcast, make the ether address a broadcast and don't do ARP */
  539. if (dest == 0xFFFFFFFF)
  540. ether = NetBcastAddr;
  541. /*
  542. * if MAC address was not discovered yet, save the packet and do
  543. * an ARP request
  544. */
  545. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  546. debug("sending ARP for %08x\n", dest);
  547. NetArpWaitPacketIP = dest;
  548. NetArpWaitPacketMAC = ether;
  549. pkt = NetArpWaitTxPacket;
  550. pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
  551. NetSetIP(pkt, dest, dport, sport, len);
  552. memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
  553. (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
  554. /* size of the waiting packet */
  555. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
  556. IP_HDR_SIZE + len;
  557. /* and do the ARP request */
  558. NetArpWaitTry = 1;
  559. NetArpWaitTimerStart = get_timer(0);
  560. ArpRequest();
  561. return 1; /* waiting */
  562. }
  563. debug("sending UDP to %08x/%pM\n", dest, ether);
  564. pkt = (uchar *)NetTxPacket;
  565. pkt += NetSetEther(pkt, ether, PROT_IP);
  566. NetSetIP(pkt, dest, dport, sport, len);
  567. (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
  568. return 0; /* transmitted */
  569. }
  570. #if defined(CONFIG_CMD_PING)
  571. static ushort PingSeqNo;
  572. int PingSend(void)
  573. {
  574. static uchar mac[6];
  575. IP_t *ip;
  576. ushort *s;
  577. uchar *pkt;
  578. /* XXX always send arp request */
  579. memcpy(mac, NetEtherNullAddr, 6);
  580. debug("sending ARP for %08x\n", NetPingIP);
  581. NetArpWaitPacketIP = NetPingIP;
  582. NetArpWaitPacketMAC = mac;
  583. pkt = NetArpWaitTxPacket;
  584. pkt += NetSetEther(pkt, mac, PROT_IP);
  585. ip = (IP_t *)pkt;
  586. /*
  587. * Construct an IP and ICMP header.
  588. * (need to set no fragment bit - XXX)
  589. */
  590. /* IP_HDR_SIZE / 4 (not including UDP) */
  591. ip->ip_hl_v = 0x45;
  592. ip->ip_tos = 0;
  593. ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
  594. ip->ip_id = htons(NetIPID++);
  595. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  596. ip->ip_ttl = 255;
  597. ip->ip_p = 0x01; /* ICMP */
  598. ip->ip_sum = 0;
  599. /* already in network byte order */
  600. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  601. /* - "" - */
  602. NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
  603. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  604. s = &ip->udp_src; /* XXX ICMP starts here */
  605. s[0] = htons(0x0800); /* echo-request, code */
  606. s[1] = 0; /* checksum */
  607. s[2] = 0; /* identifier */
  608. s[3] = htons(PingSeqNo++); /* sequence number */
  609. s[1] = ~NetCksum((uchar *)s, 8/2);
  610. /* size of the waiting packet */
  611. NetArpWaitTxPacketSize =
  612. (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
  613. /* and do the ARP request */
  614. NetArpWaitTry = 1;
  615. NetArpWaitTimerStart = get_timer(0);
  616. ArpRequest();
  617. return 1; /* waiting */
  618. }
  619. static void
  620. PingTimeout(void)
  621. {
  622. eth_halt();
  623. NetState = NETLOOP_FAIL; /* we did not get the reply */
  624. }
  625. static void
  626. PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  627. unsigned len)
  628. {
  629. if (sip != NetPingIP)
  630. return;
  631. NetState = NETLOOP_SUCCESS;
  632. }
  633. static void PingStart(void)
  634. {
  635. printf("Using %s device\n", eth_get_name());
  636. NetSetTimeout(10000UL, PingTimeout);
  637. NetSetHandler(PingHandler);
  638. PingSend();
  639. }
  640. #endif
  641. #ifdef CONFIG_IP_DEFRAG
  642. /*
  643. * This function collects fragments in a single packet, according
  644. * to the algorithm in RFC815. It returns NULL or the pointer to
  645. * a complete packet, in static storage
  646. */
  647. #ifndef CONFIG_NET_MAXDEFRAG
  648. #define CONFIG_NET_MAXDEFRAG 16384
  649. #endif
  650. /*
  651. * MAXDEFRAG, above, is chosen in the config file and is real data
  652. * so we need to add the NFS overhead, which is more than TFTP.
  653. * To use sizeof in the internal unnamed structures, we need a real
  654. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  655. * The compiler doesn't complain nor allocates the actual structure
  656. */
  657. static struct rpc_t rpc_specimen;
  658. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  659. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
  660. /*
  661. * this is the packet being assembled, either data or frag control.
  662. * Fragments go by 8 bytes, so this union must be 8 bytes long
  663. */
  664. struct hole {
  665. /* first_byte is address of this structure */
  666. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  667. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  668. u16 prev_hole; /* index of prev, 0 == none */
  669. u16 unused;
  670. };
  671. static IP_t *__NetDefragment(IP_t *ip, int *lenp)
  672. {
  673. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  674. static u16 first_hole, total_len;
  675. struct hole *payload, *thisfrag, *h, *newh;
  676. IP_t *localip = (IP_t *)pkt_buff;
  677. uchar *indata = (uchar *)ip;
  678. int offset8, start, len, done = 0;
  679. u16 ip_off = ntohs(ip->ip_off);
  680. /* payload starts after IP header, this fragment is in there */
  681. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
  682. offset8 = (ip_off & IP_OFFS);
  683. thisfrag = payload + offset8;
  684. start = offset8 * 8;
  685. len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
  686. if (start + len > IP_MAXUDP) /* fragment extends too far */
  687. return NULL;
  688. if (!total_len || localip->ip_id != ip->ip_id) {
  689. /* new (or different) packet, reset structs */
  690. total_len = 0xffff;
  691. payload[0].last_byte = ~0;
  692. payload[0].next_hole = 0;
  693. payload[0].prev_hole = 0;
  694. first_hole = 0;
  695. /* any IP header will work, copy the first we received */
  696. memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
  697. }
  698. /*
  699. * What follows is the reassembly algorithm. We use the payload
  700. * array as a linked list of hole descriptors, as each hole starts
  701. * at a multiple of 8 bytes. However, last byte can be whatever value,
  702. * so it is represented as byte count, not as 8-byte blocks.
  703. */
  704. h = payload + first_hole;
  705. while (h->last_byte < start) {
  706. if (!h->next_hole) {
  707. /* no hole that far away */
  708. return NULL;
  709. }
  710. h = payload + h->next_hole;
  711. }
  712. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  713. if (offset8 + ((len + 7) / 8) <= h - payload) {
  714. /* no overlap with holes (dup fragment?) */
  715. return NULL;
  716. }
  717. if (!(ip_off & IP_FLAGS_MFRAG)) {
  718. /* no more fragmentss: truncate this (last) hole */
  719. total_len = start + len;
  720. h->last_byte = start + len;
  721. }
  722. /*
  723. * There is some overlap: fix the hole list. This code doesn't
  724. * deal with a fragment that overlaps with two different holes
  725. * (thus being a superset of a previously-received fragment).
  726. */
  727. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  728. /* complete overlap with hole: remove hole */
  729. if (!h->prev_hole && !h->next_hole) {
  730. /* last remaining hole */
  731. done = 1;
  732. } else if (!h->prev_hole) {
  733. /* first hole */
  734. first_hole = h->next_hole;
  735. payload[h->next_hole].prev_hole = 0;
  736. } else if (!h->next_hole) {
  737. /* last hole */
  738. payload[h->prev_hole].next_hole = 0;
  739. } else {
  740. /* in the middle of the list */
  741. payload[h->next_hole].prev_hole = h->prev_hole;
  742. payload[h->prev_hole].next_hole = h->next_hole;
  743. }
  744. } else if (h->last_byte <= start + len) {
  745. /* overlaps with final part of the hole: shorten this hole */
  746. h->last_byte = start;
  747. } else if (h >= thisfrag) {
  748. /* overlaps with initial part of the hole: move this hole */
  749. newh = thisfrag + (len / 8);
  750. *newh = *h;
  751. h = newh;
  752. if (h->next_hole)
  753. payload[h->next_hole].prev_hole = (h - payload);
  754. if (h->prev_hole)
  755. payload[h->prev_hole].next_hole = (h - payload);
  756. else
  757. first_hole = (h - payload);
  758. } else {
  759. /* fragment sits in the middle: split the hole */
  760. newh = thisfrag + (len / 8);
  761. *newh = *h;
  762. h->last_byte = start;
  763. h->next_hole = (newh - payload);
  764. newh->prev_hole = (h - payload);
  765. if (newh->next_hole)
  766. payload[newh->next_hole].prev_hole = (newh - payload);
  767. }
  768. /* finally copy this fragment and possibly return whole packet */
  769. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
  770. if (!done)
  771. return NULL;
  772. localip->ip_len = htons(total_len);
  773. *lenp = total_len + IP_HDR_SIZE_NO_UDP;
  774. return localip;
  775. }
  776. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  777. {
  778. u16 ip_off = ntohs(ip->ip_off);
  779. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  780. return ip; /* not a fragment */
  781. return __NetDefragment(ip, lenp);
  782. }
  783. #else /* !CONFIG_IP_DEFRAG */
  784. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  785. {
  786. u16 ip_off = ntohs(ip->ip_off);
  787. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  788. return ip; /* not a fragment */
  789. return NULL;
  790. }
  791. #endif
  792. /**
  793. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  794. * drop others.
  795. *
  796. * @parma ip IP packet containing the ICMP
  797. */
  798. static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
  799. {
  800. ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
  801. switch (icmph->type) {
  802. case ICMP_REDIRECT:
  803. if (icmph->code != ICMP_REDIR_HOST)
  804. return;
  805. printf(" ICMP Host Redirect to %pI4 ",
  806. &icmph->un.gateway);
  807. break;
  808. #if defined(CONFIG_CMD_PING)
  809. case ICMP_ECHO_REPLY:
  810. /*
  811. * IP header OK. Pass the packet to the
  812. * current handler.
  813. */
  814. /*
  815. * XXX point to ip packet - should this use
  816. * packet_icmp_handler?
  817. */
  818. (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
  819. break;
  820. case ICMP_ECHO_REQUEST:
  821. debug("Got ICMP ECHO REQUEST, return %d bytes\n",
  822. ETHER_HDR_SIZE + len);
  823. memcpy(&et->et_dest[0], &et->et_src[0], 6);
  824. memcpy(&et->et_src[0], NetOurEther, 6);
  825. ip->ip_sum = 0;
  826. ip->ip_off = 0;
  827. NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
  828. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  829. ip->ip_sum = ~NetCksum((uchar *)ip,
  830. IP_HDR_SIZE_NO_UDP >> 1);
  831. icmph->type = ICMP_ECHO_REPLY;
  832. icmph->checksum = 0;
  833. icmph->checksum = ~NetCksum((uchar *)icmph,
  834. (len - IP_HDR_SIZE_NO_UDP) >> 1);
  835. (void) eth_send((uchar *)et,
  836. ETHER_HDR_SIZE + len);
  837. break;
  838. #endif
  839. default:
  840. #ifdef CONFIG_CMD_TFTPPUT
  841. if (packet_icmp_handler)
  842. packet_icmp_handler(icmph->type, icmph->code,
  843. ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
  844. icmph->un.data, ntohs(ip->udp_len));
  845. #endif
  846. break;
  847. }
  848. }
  849. void
  850. NetReceive(uchar *inpkt, int len)
  851. {
  852. Ethernet_t *et;
  853. IP_t *ip;
  854. #ifdef CONFIG_CMD_RARP
  855. ARP_t *arp;
  856. #endif
  857. IPaddr_t tmp;
  858. IPaddr_t src_ip;
  859. int x;
  860. #if defined(CONFIG_CMD_CDP)
  861. int iscdp;
  862. #endif
  863. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  864. debug("packet received\n");
  865. NetRxPacket = inpkt;
  866. NetRxPacketLen = len;
  867. et = (Ethernet_t *)inpkt;
  868. /* too small packet? */
  869. if (len < ETHER_HDR_SIZE)
  870. return;
  871. #ifdef CONFIG_API
  872. if (push_packet) {
  873. (*push_packet)(inpkt, len);
  874. return;
  875. }
  876. #endif
  877. #if defined(CONFIG_CMD_CDP)
  878. /* keep track if packet is CDP */
  879. iscdp = is_cdp_packet(et->et_dest);
  880. #endif
  881. myvlanid = ntohs(NetOurVLAN);
  882. if (myvlanid == (ushort)-1)
  883. myvlanid = VLAN_NONE;
  884. mynvlanid = ntohs(NetOurNativeVLAN);
  885. if (mynvlanid == (ushort)-1)
  886. mynvlanid = VLAN_NONE;
  887. x = ntohs(et->et_protlen);
  888. debug("packet received\n");
  889. if (x < 1514) {
  890. /*
  891. * Got a 802 packet. Check the other protocol field.
  892. */
  893. x = ntohs(et->et_prot);
  894. ip = (IP_t *)(inpkt + E802_HDR_SIZE);
  895. len -= E802_HDR_SIZE;
  896. } else if (x != PROT_VLAN) { /* normal packet */
  897. ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
  898. len -= ETHER_HDR_SIZE;
  899. } else { /* VLAN packet */
  900. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
  901. debug("VLAN packet received\n");
  902. /* too small packet? */
  903. if (len < VLAN_ETHER_HDR_SIZE)
  904. return;
  905. /* if no VLAN active */
  906. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  907. #if defined(CONFIG_CMD_CDP)
  908. && iscdp == 0
  909. #endif
  910. )
  911. return;
  912. cti = ntohs(vet->vet_tag);
  913. vlanid = cti & VLAN_IDMASK;
  914. x = ntohs(vet->vet_type);
  915. ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
  916. len -= VLAN_ETHER_HDR_SIZE;
  917. }
  918. debug("Receive from protocol 0x%x\n", x);
  919. #if defined(CONFIG_CMD_CDP)
  920. if (iscdp) {
  921. CDPHandler((uchar *)ip, len);
  922. return;
  923. }
  924. #endif
  925. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  926. if (vlanid == VLAN_NONE)
  927. vlanid = (mynvlanid & VLAN_IDMASK);
  928. /* not matched? */
  929. if (vlanid != (myvlanid & VLAN_IDMASK))
  930. return;
  931. }
  932. switch (x) {
  933. case PROT_ARP:
  934. ArpReceive(et, ip, len);
  935. break;
  936. #ifdef CONFIG_CMD_RARP
  937. case PROT_RARP:
  938. debug("Got RARP\n");
  939. arp = (ARP_t *)ip;
  940. if (len < ARP_HDR_SIZE) {
  941. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  942. return;
  943. }
  944. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  945. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  946. (ntohs(arp->ar_pro) != PROT_IP) ||
  947. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  948. puts("invalid RARP header\n");
  949. } else {
  950. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  951. if (NetServerIP == 0)
  952. NetCopyIP(&NetServerIP, &arp->ar_data[6]);
  953. memcpy(NetServerEther, &arp->ar_data[0], 6);
  954. (*packetHandler)(0, 0, 0, 0, 0);
  955. }
  956. break;
  957. #endif
  958. case PROT_IP:
  959. debug("Got IP\n");
  960. /* Before we start poking the header, make sure it is there */
  961. if (len < IP_HDR_SIZE) {
  962. debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
  963. return;
  964. }
  965. /* Check the packet length */
  966. if (len < ntohs(ip->ip_len)) {
  967. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  968. return;
  969. }
  970. len = ntohs(ip->ip_len);
  971. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  972. /* Can't deal with anything except IPv4 */
  973. if ((ip->ip_hl_v & 0xf0) != 0x40)
  974. return;
  975. /* Can't deal with IP options (headers != 20 bytes) */
  976. if ((ip->ip_hl_v & 0x0f) > 0x05)
  977. return;
  978. /* Check the Checksum of the header */
  979. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  980. puts("checksum bad\n");
  981. return;
  982. }
  983. /* If it is not for us, ignore it */
  984. tmp = NetReadIP(&ip->ip_dst);
  985. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  986. #ifdef CONFIG_MCAST_TFTP
  987. if (Mcast_addr != tmp)
  988. #endif
  989. return;
  990. }
  991. /* Read source IP address for later use */
  992. src_ip = NetReadIP(&ip->ip_src);
  993. /*
  994. * The function returns the unchanged packet if it's not
  995. * a fragment, and either the complete packet or NULL if
  996. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  997. */
  998. ip = NetDefragment(ip, &len);
  999. if (!ip)
  1000. return;
  1001. /*
  1002. * watch for ICMP host redirects
  1003. *
  1004. * There is no real handler code (yet). We just watch
  1005. * for ICMP host redirect messages. In case anybody
  1006. * sees these messages: please contact me
  1007. * (wd@denx.de), or - even better - send me the
  1008. * necessary fixes :-)
  1009. *
  1010. * Note: in all cases where I have seen this so far
  1011. * it was a problem with the router configuration,
  1012. * for instance when a router was configured in the
  1013. * BOOTP reply, but the TFTP server was on the same
  1014. * subnet. So this is probably a warning that your
  1015. * configuration might be wrong. But I'm not really
  1016. * sure if there aren't any other situations.
  1017. *
  1018. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  1019. * we send a tftp packet to a dead connection, or when
  1020. * there is no server at the other end.
  1021. */
  1022. if (ip->ip_p == IPPROTO_ICMP) {
  1023. receive_icmp(ip, len, src_ip, et);
  1024. return;
  1025. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1026. return;
  1027. }
  1028. #ifdef CONFIG_UDP_CHECKSUM
  1029. if (ip->udp_xsum != 0) {
  1030. ulong xsum;
  1031. ushort *sumptr;
  1032. ushort sumlen;
  1033. xsum = ip->ip_p;
  1034. xsum += (ntohs(ip->udp_len));
  1035. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  1036. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  1037. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  1038. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  1039. sumlen = ntohs(ip->udp_len);
  1040. sumptr = (ushort *) &(ip->udp_src);
  1041. while (sumlen > 1) {
  1042. ushort sumdata;
  1043. sumdata = *sumptr++;
  1044. xsum += ntohs(sumdata);
  1045. sumlen -= 2;
  1046. }
  1047. if (sumlen > 0) {
  1048. ushort sumdata;
  1049. sumdata = *(unsigned char *) sumptr;
  1050. sumdata = (sumdata << 8) & 0xff00;
  1051. xsum += sumdata;
  1052. }
  1053. while ((xsum >> 16) != 0) {
  1054. xsum = (xsum & 0x0000ffff) +
  1055. ((xsum >> 16) & 0x0000ffff);
  1056. }
  1057. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1058. printf(" UDP wrong checksum %08lx %08x\n",
  1059. xsum, ntohs(ip->udp_xsum));
  1060. return;
  1061. }
  1062. }
  1063. #endif
  1064. #ifdef CONFIG_NETCONSOLE
  1065. nc_input_packet((uchar *)ip + IP_HDR_SIZE,
  1066. ntohs(ip->udp_dst),
  1067. ntohs(ip->udp_src),
  1068. ntohs(ip->udp_len) - 8);
  1069. #endif
  1070. /*
  1071. * IP header OK. Pass the packet to the current handler.
  1072. */
  1073. (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
  1074. ntohs(ip->udp_dst),
  1075. src_ip,
  1076. ntohs(ip->udp_src),
  1077. ntohs(ip->udp_len) - 8);
  1078. break;
  1079. }
  1080. }
  1081. /**********************************************************************/
  1082. static int net_check_prereq(enum proto_t protocol)
  1083. {
  1084. switch (protocol) {
  1085. /* Fall through */
  1086. #if defined(CONFIG_CMD_PING)
  1087. case PING:
  1088. if (NetPingIP == 0) {
  1089. puts("*** ERROR: ping address not given\n");
  1090. return 1;
  1091. }
  1092. goto common;
  1093. #endif
  1094. #if defined(CONFIG_CMD_SNTP)
  1095. case SNTP:
  1096. if (NetNtpServerIP == 0) {
  1097. puts("*** ERROR: NTP server address not given\n");
  1098. return 1;
  1099. }
  1100. goto common;
  1101. #endif
  1102. #if defined(CONFIG_CMD_DNS)
  1103. case DNS:
  1104. if (NetOurDNSIP == 0) {
  1105. puts("*** ERROR: DNS server address not given\n");
  1106. return 1;
  1107. }
  1108. goto common;
  1109. #endif
  1110. #if defined(CONFIG_CMD_NFS)
  1111. case NFS:
  1112. #endif
  1113. case TFTPGET:
  1114. case TFTPPUT:
  1115. if (NetServerIP == 0) {
  1116. puts("*** ERROR: `serverip' not set\n");
  1117. return 1;
  1118. }
  1119. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1120. defined(CONFIG_CMD_DNS)
  1121. common:
  1122. #endif
  1123. /* Fall through */
  1124. case NETCONS:
  1125. case TFTPSRV:
  1126. if (NetOurIP == 0) {
  1127. puts("*** ERROR: `ipaddr' not set\n");
  1128. return 1;
  1129. }
  1130. /* Fall through */
  1131. #ifdef CONFIG_CMD_RARP
  1132. case RARP:
  1133. #endif
  1134. case BOOTP:
  1135. case CDP:
  1136. case DHCP:
  1137. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1138. int num = eth_get_dev_index();
  1139. switch (num) {
  1140. case -1:
  1141. puts("*** ERROR: No ethernet found.\n");
  1142. return 1;
  1143. case 0:
  1144. puts("*** ERROR: `ethaddr' not set\n");
  1145. break;
  1146. default:
  1147. printf("*** ERROR: `eth%daddr' not set\n",
  1148. num);
  1149. break;
  1150. }
  1151. NetStartAgain();
  1152. return 2;
  1153. }
  1154. /* Fall through */
  1155. default:
  1156. return 0;
  1157. }
  1158. return 0; /* OK */
  1159. }
  1160. /**********************************************************************/
  1161. int
  1162. NetCksumOk(uchar *ptr, int len)
  1163. {
  1164. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1165. }
  1166. unsigned
  1167. NetCksum(uchar *ptr, int len)
  1168. {
  1169. ulong xsum;
  1170. ushort *p = (ushort *)ptr;
  1171. xsum = 0;
  1172. while (len-- > 0)
  1173. xsum += *p++;
  1174. xsum = (xsum & 0xffff) + (xsum >> 16);
  1175. xsum = (xsum & 0xffff) + (xsum >> 16);
  1176. return xsum & 0xffff;
  1177. }
  1178. int
  1179. NetEthHdrSize(void)
  1180. {
  1181. ushort myvlanid;
  1182. myvlanid = ntohs(NetOurVLAN);
  1183. if (myvlanid == (ushort)-1)
  1184. myvlanid = VLAN_NONE;
  1185. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1186. VLAN_ETHER_HDR_SIZE;
  1187. }
  1188. int
  1189. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1190. {
  1191. Ethernet_t *et = (Ethernet_t *)xet;
  1192. ushort myvlanid;
  1193. myvlanid = ntohs(NetOurVLAN);
  1194. if (myvlanid == (ushort)-1)
  1195. myvlanid = VLAN_NONE;
  1196. memcpy(et->et_dest, addr, 6);
  1197. memcpy(et->et_src, NetOurEther, 6);
  1198. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1199. et->et_protlen = htons(prot);
  1200. return ETHER_HDR_SIZE;
  1201. } else {
  1202. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
  1203. vet->vet_vlan_type = htons(PROT_VLAN);
  1204. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1205. vet->vet_type = htons(prot);
  1206. return VLAN_ETHER_HDR_SIZE;
  1207. }
  1208. }
  1209. void
  1210. NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
  1211. {
  1212. IP_t *ip = (IP_t *)xip;
  1213. /*
  1214. * If the data is an odd number of bytes, zero the
  1215. * byte after the last byte so that the checksum
  1216. * will work.
  1217. */
  1218. if (len & 1)
  1219. xip[IP_HDR_SIZE + len] = 0;
  1220. /*
  1221. * Construct an IP and UDP header.
  1222. * (need to set no fragment bit - XXX)
  1223. */
  1224. /* IP_HDR_SIZE / 4 (not including UDP) */
  1225. ip->ip_hl_v = 0x45;
  1226. ip->ip_tos = 0;
  1227. ip->ip_len = htons(IP_HDR_SIZE + len);
  1228. ip->ip_id = htons(NetIPID++);
  1229. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1230. ip->ip_ttl = 255;
  1231. ip->ip_p = 17; /* UDP */
  1232. ip->ip_sum = 0;
  1233. /* already in network byte order */
  1234. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  1235. /* - "" - */
  1236. NetCopyIP((void *)&ip->ip_dst, &dest);
  1237. ip->udp_src = htons(sport);
  1238. ip->udp_dst = htons(dport);
  1239. ip->udp_len = htons(8 + len);
  1240. ip->udp_xsum = 0;
  1241. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  1242. }
  1243. void copy_filename(char *dst, const char *src, int size)
  1244. {
  1245. if (*src && (*src == '"')) {
  1246. ++src;
  1247. --size;
  1248. }
  1249. while ((--size > 0) && *src && (*src != '"'))
  1250. *dst++ = *src++;
  1251. *dst = '\0';
  1252. }
  1253. #if defined(CONFIG_CMD_NFS) || \
  1254. defined(CONFIG_CMD_SNTP) || \
  1255. defined(CONFIG_CMD_DNS)
  1256. /*
  1257. * make port a little random (1024-17407)
  1258. * This keeps the math somewhat trivial to compute, and seems to work with
  1259. * all supported protocols/clients/servers
  1260. */
  1261. unsigned int random_port(void)
  1262. {
  1263. return 1024 + (get_timer(0) % 0x4000);
  1264. }
  1265. #endif
  1266. void ip_to_string(IPaddr_t x, char *s)
  1267. {
  1268. x = ntohl(x);
  1269. sprintf(s, "%d.%d.%d.%d",
  1270. (int) ((x >> 24) & 0xff),
  1271. (int) ((x >> 16) & 0xff),
  1272. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1273. );
  1274. }
  1275. void VLAN_to_string(ushort x, char *s)
  1276. {
  1277. x = ntohs(x);
  1278. if (x == (ushort)-1)
  1279. x = VLAN_NONE;
  1280. if (x == VLAN_NONE)
  1281. strcpy(s, "none");
  1282. else
  1283. sprintf(s, "%d", x & VLAN_IDMASK);
  1284. }
  1285. ushort string_to_VLAN(const char *s)
  1286. {
  1287. ushort id;
  1288. if (s == NULL)
  1289. return htons(VLAN_NONE);
  1290. if (*s < '0' || *s > '9')
  1291. id = VLAN_NONE;
  1292. else
  1293. id = (ushort)simple_strtoul(s, NULL, 10);
  1294. return htons(id);
  1295. }
  1296. ushort getenv_VLAN(char *var)
  1297. {
  1298. return string_to_VLAN(getenv(var));
  1299. }