net.c 37 KB

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