net.c 33 KB

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