net.c 33 KB

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