net.c 33 KB

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