net.c 36 KB

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