net.c 31 KB

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