net.c 34 KB

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