net.c 34 KB

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