net.c 37 KB

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