net.c 33 KB

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