net.c 33 KB

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