net.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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. *
  75. * WOL:
  76. *
  77. * Prerequisites: - own ethernet address
  78. * We want: - magic packet or timeout
  79. * Next step: none
  80. */
  81. #include <common.h>
  82. #include <bootstage.h>
  83. #include <command.h>
  84. #include <console.h>
  85. #include <env.h>
  86. #include <env_internal.h>
  87. #include <errno.h>
  88. #include <image.h>
  89. #include <log.h>
  90. #include <net.h>
  91. #include <net/fastboot.h>
  92. #include <net/tftp.h>
  93. #if defined(CONFIG_CMD_PCAP)
  94. #include <net/pcap.h>
  95. #endif
  96. #include <net/udp.h>
  97. #if defined(CONFIG_LED_STATUS)
  98. #include <miiphy.h>
  99. #include <status_led.h>
  100. #endif
  101. #include <watchdog.h>
  102. #include <linux/compiler.h>
  103. #include "arp.h"
  104. #include "bootp.h"
  105. #include "cdp.h"
  106. #if defined(CONFIG_CMD_DNS)
  107. #include "dns.h"
  108. #endif
  109. #include "link_local.h"
  110. #include "nfs.h"
  111. #include "ping.h"
  112. #include "rarp.h"
  113. #if defined(CONFIG_CMD_WOL)
  114. #include "wol.h"
  115. #endif
  116. /** BOOTP EXTENTIONS **/
  117. /* Our subnet mask (0=unknown) */
  118. struct in_addr net_netmask;
  119. /* Our gateways IP address */
  120. struct in_addr net_gateway;
  121. /* Our DNS IP address */
  122. struct in_addr net_dns_server;
  123. #if defined(CONFIG_BOOTP_DNS2)
  124. /* Our 2nd DNS IP address */
  125. struct in_addr net_dns_server2;
  126. #endif
  127. /** END OF BOOTP EXTENTIONS **/
  128. /* Our ethernet address */
  129. u8 net_ethaddr[6];
  130. /* Boot server enet address */
  131. u8 net_server_ethaddr[6];
  132. /* Our IP addr (0 = unknown) */
  133. struct in_addr net_ip;
  134. /* Server IP addr (0 = unknown) */
  135. struct in_addr net_server_ip;
  136. /* Current receive packet */
  137. uchar *net_rx_packet;
  138. /* Current rx packet length */
  139. int net_rx_packet_len;
  140. /* IP packet ID */
  141. static unsigned net_ip_id;
  142. /* Ethernet bcast address */
  143. const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  144. const u8 net_null_ethaddr[6];
  145. #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
  146. void (*push_packet)(void *, int len) = 0;
  147. #endif
  148. /* Network loop state */
  149. enum net_loop_state net_state;
  150. /* Tried all network devices */
  151. int net_restart_wrap;
  152. /* Network loop restarted */
  153. static int net_restarted;
  154. /* At least one device configured */
  155. static int net_dev_exists;
  156. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  157. /* default is without VLAN */
  158. ushort net_our_vlan = 0xFFFF;
  159. /* ditto */
  160. ushort net_native_vlan = 0xFFFF;
  161. /* Boot File name */
  162. char net_boot_file_name[1024];
  163. /* Indicates whether the file name was specified on the command line */
  164. bool net_boot_file_name_explicit;
  165. /* The actual transferred size of the bootfile (in bytes) */
  166. u32 net_boot_file_size;
  167. /* Boot file size in blocks as reported by the DHCP server */
  168. u32 net_boot_file_expected_size_in_blocks;
  169. static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  170. /* Receive packets */
  171. uchar *net_rx_packets[PKTBUFSRX];
  172. /* Current UDP RX packet handler */
  173. static rxhand_f *udp_packet_handler;
  174. /* Current ARP RX packet handler */
  175. static rxhand_f *arp_packet_handler;
  176. #ifdef CONFIG_CMD_TFTPPUT
  177. /* Current ICMP rx handler */
  178. static rxhand_icmp_f *packet_icmp_handler;
  179. #endif
  180. /* Current timeout handler */
  181. static thand_f *time_handler;
  182. /* Time base value */
  183. static ulong time_start;
  184. /* Current timeout value */
  185. static ulong time_delta;
  186. /* THE transmit packet */
  187. uchar *net_tx_packet;
  188. static int net_check_prereq(enum proto_t protocol);
  189. static int net_try_count;
  190. int __maybe_unused net_busy_flag;
  191. /**********************************************************************/
  192. static int on_ipaddr(const char *name, const char *value, enum env_op op,
  193. int flags)
  194. {
  195. if (flags & H_PROGRAMMATIC)
  196. return 0;
  197. net_ip = string_to_ip(value);
  198. return 0;
  199. }
  200. U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
  201. static int on_gatewayip(const char *name, const char *value, enum env_op op,
  202. int flags)
  203. {
  204. if (flags & H_PROGRAMMATIC)
  205. return 0;
  206. net_gateway = string_to_ip(value);
  207. return 0;
  208. }
  209. U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
  210. static int on_netmask(const char *name, const char *value, enum env_op op,
  211. int flags)
  212. {
  213. if (flags & H_PROGRAMMATIC)
  214. return 0;
  215. net_netmask = string_to_ip(value);
  216. return 0;
  217. }
  218. U_BOOT_ENV_CALLBACK(netmask, on_netmask);
  219. static int on_serverip(const char *name, const char *value, enum env_op op,
  220. int flags)
  221. {
  222. if (flags & H_PROGRAMMATIC)
  223. return 0;
  224. net_server_ip = string_to_ip(value);
  225. return 0;
  226. }
  227. U_BOOT_ENV_CALLBACK(serverip, on_serverip);
  228. static int on_nvlan(const char *name, const char *value, enum env_op op,
  229. int flags)
  230. {
  231. if (flags & H_PROGRAMMATIC)
  232. return 0;
  233. net_native_vlan = string_to_vlan(value);
  234. return 0;
  235. }
  236. U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
  237. static int on_vlan(const char *name, const char *value, enum env_op op,
  238. int flags)
  239. {
  240. if (flags & H_PROGRAMMATIC)
  241. return 0;
  242. net_our_vlan = string_to_vlan(value);
  243. return 0;
  244. }
  245. U_BOOT_ENV_CALLBACK(vlan, on_vlan);
  246. #if defined(CONFIG_CMD_DNS)
  247. static int on_dnsip(const char *name, const char *value, enum env_op op,
  248. int flags)
  249. {
  250. if (flags & H_PROGRAMMATIC)
  251. return 0;
  252. net_dns_server = string_to_ip(value);
  253. return 0;
  254. }
  255. U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
  256. #endif
  257. /*
  258. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  259. * the boot file.
  260. */
  261. void net_auto_load(void)
  262. {
  263. #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
  264. const char *s = env_get("autoload");
  265. if (s != NULL && strcmp(s, "NFS") == 0) {
  266. if (net_check_prereq(NFS)) {
  267. /* We aren't expecting to get a serverip, so just accept the assigned IP */
  268. if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
  269. net_set_state(NETLOOP_SUCCESS);
  270. } else {
  271. printf("Cannot autoload with NFS\n");
  272. net_set_state(NETLOOP_FAIL);
  273. }
  274. return;
  275. }
  276. /*
  277. * Use NFS to load the bootfile.
  278. */
  279. nfs_start();
  280. return;
  281. }
  282. #endif
  283. if (env_get_yesno("autoload") == 0) {
  284. /*
  285. * Just use BOOTP/RARP to configure system;
  286. * Do not use TFTP to load the bootfile.
  287. */
  288. net_set_state(NETLOOP_SUCCESS);
  289. return;
  290. }
  291. if (net_check_prereq(TFTPGET)) {
  292. /* We aren't expecting to get a serverip, so just accept the assigned IP */
  293. if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
  294. net_set_state(NETLOOP_SUCCESS);
  295. } else {
  296. printf("Cannot autoload with TFTPGET\n");
  297. net_set_state(NETLOOP_FAIL);
  298. }
  299. return;
  300. }
  301. tftp_start(TFTPGET);
  302. }
  303. static int net_init_loop(void)
  304. {
  305. if (eth_get_dev())
  306. memcpy(net_ethaddr, eth_get_ethaddr(), 6);
  307. else
  308. /*
  309. * Not ideal, but there's no way to get the actual error, and I
  310. * don't feel like fixing all the users of eth_get_dev to deal
  311. * with errors.
  312. */
  313. return -ENONET;
  314. return 0;
  315. }
  316. static void net_clear_handlers(void)
  317. {
  318. net_set_udp_handler(NULL);
  319. net_set_arp_handler(NULL);
  320. net_set_timeout_handler(0, NULL);
  321. }
  322. static void net_cleanup_loop(void)
  323. {
  324. net_clear_handlers();
  325. }
  326. int net_init(void)
  327. {
  328. static int first_call = 1;
  329. if (first_call) {
  330. /*
  331. * Setup packet buffers, aligned correctly.
  332. */
  333. int i;
  334. net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
  335. net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
  336. for (i = 0; i < PKTBUFSRX; i++) {
  337. net_rx_packets[i] = net_tx_packet +
  338. (i + 1) * PKTSIZE_ALIGN;
  339. }
  340. arp_init();
  341. net_clear_handlers();
  342. /* Only need to setup buffer pointers once. */
  343. first_call = 0;
  344. }
  345. return net_init_loop();
  346. }
  347. /**********************************************************************/
  348. /*
  349. * Main network processing loop.
  350. */
  351. int net_loop(enum proto_t protocol)
  352. {
  353. int ret = -EINVAL;
  354. enum net_loop_state prev_net_state = net_state;
  355. #if defined(CONFIG_CMD_PING)
  356. if (protocol != PING)
  357. net_ping_ip.s_addr = 0;
  358. #endif
  359. net_restarted = 0;
  360. net_dev_exists = 0;
  361. net_try_count = 1;
  362. debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
  363. bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
  364. net_init();
  365. if (eth_is_on_demand_init()) {
  366. eth_halt();
  367. eth_set_current();
  368. ret = eth_init();
  369. if (ret < 0) {
  370. eth_halt();
  371. return ret;
  372. }
  373. } else {
  374. eth_init_state_only();
  375. }
  376. restart:
  377. #ifdef CONFIG_USB_KEYBOARD
  378. net_busy_flag = 0;
  379. #endif
  380. net_set_state(NETLOOP_CONTINUE);
  381. /*
  382. * Start the ball rolling with the given start function. From
  383. * here on, this code is a state machine driven by received
  384. * packets and timer events.
  385. */
  386. debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
  387. net_init_loop();
  388. switch (net_check_prereq(protocol)) {
  389. case 1:
  390. /* network not configured */
  391. eth_halt();
  392. net_set_state(prev_net_state);
  393. return -ENODEV;
  394. case 2:
  395. /* network device not configured */
  396. break;
  397. case 0:
  398. net_dev_exists = 1;
  399. net_boot_file_size = 0;
  400. switch (protocol) {
  401. #ifdef CONFIG_CMD_TFTPBOOT
  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. #endif
  410. #ifdef CONFIG_CMD_TFTPSRV
  411. case TFTPSRV:
  412. tftp_start_server();
  413. break;
  414. #endif
  415. #ifdef CONFIG_UDP_FUNCTION_FASTBOOT
  416. case FASTBOOT:
  417. fastboot_start_server();
  418. break;
  419. #endif
  420. #if defined(CONFIG_CMD_DHCP)
  421. case DHCP:
  422. bootp_reset();
  423. net_ip.s_addr = 0;
  424. dhcp_request(); /* Basically same as BOOTP */
  425. break;
  426. #endif
  427. #if defined(CONFIG_CMD_BOOTP)
  428. case BOOTP:
  429. bootp_reset();
  430. net_ip.s_addr = 0;
  431. bootp_request();
  432. break;
  433. #endif
  434. #if defined(CONFIG_CMD_RARP)
  435. case RARP:
  436. rarp_try = 0;
  437. net_ip.s_addr = 0;
  438. rarp_request();
  439. break;
  440. #endif
  441. #if defined(CONFIG_CMD_PING)
  442. case PING:
  443. ping_start();
  444. break;
  445. #endif
  446. #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
  447. case NFS:
  448. nfs_start();
  449. break;
  450. #endif
  451. #if defined(CONFIG_CMD_CDP)
  452. case CDP:
  453. cdp_start();
  454. break;
  455. #endif
  456. #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
  457. case NETCONS:
  458. nc_start();
  459. break;
  460. #endif
  461. #if defined(CONFIG_CMD_DNS)
  462. case DNS:
  463. dns_start();
  464. break;
  465. #endif
  466. #if defined(CONFIG_CMD_LINK_LOCAL)
  467. case LINKLOCAL:
  468. link_local_start();
  469. break;
  470. #endif
  471. #if defined(CONFIG_CMD_WOL)
  472. case WOL:
  473. wol_start();
  474. break;
  475. #endif
  476. default:
  477. break;
  478. }
  479. if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
  480. udp_start();
  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. if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
  1118. ulong xsum;
  1119. u8 *sumptr;
  1120. ushort sumlen;
  1121. xsum = ip->ip_p;
  1122. xsum += (ntohs(ip->udp_len));
  1123. xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
  1124. xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
  1125. xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
  1126. xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
  1127. sumlen = ntohs(ip->udp_len);
  1128. sumptr = (u8 *)&ip->udp_src;
  1129. while (sumlen > 1) {
  1130. /* inlined ntohs() to avoid alignment errors */
  1131. xsum += (sumptr[0] << 8) + sumptr[1];
  1132. sumptr += 2;
  1133. sumlen -= 2;
  1134. }
  1135. if (sumlen > 0)
  1136. xsum += (sumptr[0] << 8) + sumptr[0];
  1137. while ((xsum >> 16) != 0) {
  1138. xsum = (xsum & 0x0000ffff) +
  1139. ((xsum >> 16) & 0x0000ffff);
  1140. }
  1141. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1142. printf(" UDP wrong checksum %08lx %08x\n",
  1143. xsum, ntohs(ip->udp_xsum));
  1144. return;
  1145. }
  1146. }
  1147. #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
  1148. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  1149. src_ip,
  1150. ntohs(ip->udp_dst),
  1151. ntohs(ip->udp_src),
  1152. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1153. #endif
  1154. /*
  1155. * IP header OK. Pass the packet to the current handler.
  1156. */
  1157. (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
  1158. ntohs(ip->udp_dst),
  1159. src_ip,
  1160. ntohs(ip->udp_src),
  1161. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1162. break;
  1163. #ifdef CONFIG_CMD_WOL
  1164. case PROT_WOL:
  1165. wol_receive(ip, len);
  1166. break;
  1167. #endif
  1168. }
  1169. }
  1170. /**********************************************************************/
  1171. static int net_check_prereq(enum proto_t protocol)
  1172. {
  1173. switch (protocol) {
  1174. /* Fall through */
  1175. #if defined(CONFIG_CMD_PING)
  1176. case PING:
  1177. if (net_ping_ip.s_addr == 0) {
  1178. puts("*** ERROR: ping address not given\n");
  1179. return 1;
  1180. }
  1181. goto common;
  1182. #endif
  1183. #if defined(CONFIG_CMD_DNS)
  1184. case DNS:
  1185. if (net_dns_server.s_addr == 0) {
  1186. puts("*** ERROR: DNS server address not given\n");
  1187. return 1;
  1188. }
  1189. goto common;
  1190. #endif
  1191. #if defined(CONFIG_PROT_UDP)
  1192. case UDP:
  1193. if (udp_prereq())
  1194. return 1;
  1195. goto common;
  1196. #endif
  1197. #if defined(CONFIG_CMD_NFS)
  1198. case NFS:
  1199. #endif
  1200. /* Fall through */
  1201. case TFTPGET:
  1202. case TFTPPUT:
  1203. if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
  1204. puts("*** ERROR: `serverip' not set\n");
  1205. return 1;
  1206. }
  1207. #if defined(CONFIG_CMD_PING) || \
  1208. defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
  1209. common:
  1210. #endif
  1211. /* Fall through */
  1212. case NETCONS:
  1213. case FASTBOOT:
  1214. case TFTPSRV:
  1215. if (net_ip.s_addr == 0) {
  1216. puts("*** ERROR: `ipaddr' not set\n");
  1217. return 1;
  1218. }
  1219. /* Fall through */
  1220. #ifdef CONFIG_CMD_RARP
  1221. case RARP:
  1222. #endif
  1223. case BOOTP:
  1224. case CDP:
  1225. case DHCP:
  1226. case LINKLOCAL:
  1227. if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
  1228. int num = eth_get_dev_index();
  1229. switch (num) {
  1230. case -1:
  1231. puts("*** ERROR: No ethernet found.\n");
  1232. return 1;
  1233. case 0:
  1234. puts("*** ERROR: `ethaddr' not set\n");
  1235. break;
  1236. default:
  1237. printf("*** ERROR: `eth%daddr' not set\n",
  1238. num);
  1239. break;
  1240. }
  1241. net_start_again();
  1242. return 2;
  1243. }
  1244. /* Fall through */
  1245. default:
  1246. return 0;
  1247. }
  1248. return 0; /* OK */
  1249. }
  1250. /**********************************************************************/
  1251. int
  1252. net_eth_hdr_size(void)
  1253. {
  1254. ushort myvlanid;
  1255. myvlanid = ntohs(net_our_vlan);
  1256. if (myvlanid == (ushort)-1)
  1257. myvlanid = VLAN_NONE;
  1258. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1259. VLAN_ETHER_HDR_SIZE;
  1260. }
  1261. int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
  1262. {
  1263. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1264. ushort myvlanid;
  1265. myvlanid = ntohs(net_our_vlan);
  1266. if (myvlanid == (ushort)-1)
  1267. myvlanid = VLAN_NONE;
  1268. memcpy(et->et_dest, dest_ethaddr, 6);
  1269. memcpy(et->et_src, net_ethaddr, 6);
  1270. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1271. et->et_protlen = htons(prot);
  1272. return ETHER_HDR_SIZE;
  1273. } else {
  1274. struct vlan_ethernet_hdr *vet =
  1275. (struct vlan_ethernet_hdr *)xet;
  1276. vet->vet_vlan_type = htons(PROT_VLAN);
  1277. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1278. vet->vet_type = htons(prot);
  1279. return VLAN_ETHER_HDR_SIZE;
  1280. }
  1281. }
  1282. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
  1283. {
  1284. ushort protlen;
  1285. memcpy(et->et_dest, addr, 6);
  1286. memcpy(et->et_src, net_ethaddr, 6);
  1287. protlen = ntohs(et->et_protlen);
  1288. if (protlen == PROT_VLAN) {
  1289. struct vlan_ethernet_hdr *vet =
  1290. (struct vlan_ethernet_hdr *)et;
  1291. vet->vet_type = htons(prot);
  1292. return VLAN_ETHER_HDR_SIZE;
  1293. } else if (protlen > 1514) {
  1294. et->et_protlen = htons(prot);
  1295. return ETHER_HDR_SIZE;
  1296. } else {
  1297. /* 802.2 + SNAP */
  1298. struct e802_hdr *et802 = (struct e802_hdr *)et;
  1299. et802->et_prot = htons(prot);
  1300. return E802_HDR_SIZE;
  1301. }
  1302. }
  1303. void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
  1304. u16 pkt_len, u8 proto)
  1305. {
  1306. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1307. /*
  1308. * Construct an IP header.
  1309. */
  1310. /* IP_HDR_SIZE / 4 (not including UDP) */
  1311. ip->ip_hl_v = 0x45;
  1312. ip->ip_tos = 0;
  1313. ip->ip_len = htons(pkt_len);
  1314. ip->ip_p = proto;
  1315. ip->ip_id = htons(net_ip_id++);
  1316. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1317. ip->ip_ttl = 255;
  1318. ip->ip_sum = 0;
  1319. /* already in network byte order */
  1320. net_copy_ip((void *)&ip->ip_src, &source);
  1321. /* already in network byte order */
  1322. net_copy_ip((void *)&ip->ip_dst, &dest);
  1323. ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
  1324. }
  1325. void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
  1326. int len)
  1327. {
  1328. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1329. /*
  1330. * If the data is an odd number of bytes, zero the
  1331. * byte after the last byte so that the checksum
  1332. * will work.
  1333. */
  1334. if (len & 1)
  1335. pkt[IP_UDP_HDR_SIZE + len] = 0;
  1336. net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
  1337. IPPROTO_UDP);
  1338. ip->udp_src = htons(sport);
  1339. ip->udp_dst = htons(dport);
  1340. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1341. ip->udp_xsum = 0;
  1342. }
  1343. void copy_filename(char *dst, const char *src, int size)
  1344. {
  1345. if (src && *src && (*src == '"')) {
  1346. ++src;
  1347. --size;
  1348. }
  1349. while ((--size > 0) && src && *src && (*src != '"'))
  1350. *dst++ = *src++;
  1351. *dst = '\0';
  1352. }
  1353. int is_serverip_in_cmd(void)
  1354. {
  1355. return !!strchr(net_boot_file_name, ':');
  1356. }
  1357. int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
  1358. {
  1359. char *colon;
  1360. if (net_boot_file_name[0] == '\0')
  1361. return 0;
  1362. colon = strchr(net_boot_file_name, ':');
  1363. if (colon) {
  1364. if (ipaddr)
  1365. *ipaddr = string_to_ip(net_boot_file_name);
  1366. strncpy(filename, colon + 1, max_len);
  1367. } else {
  1368. strncpy(filename, net_boot_file_name, max_len);
  1369. }
  1370. filename[max_len - 1] = '\0';
  1371. return 1;
  1372. }
  1373. void ip_to_string(struct in_addr x, char *s)
  1374. {
  1375. x.s_addr = ntohl(x.s_addr);
  1376. sprintf(s, "%d.%d.%d.%d",
  1377. (int) ((x.s_addr >> 24) & 0xff),
  1378. (int) ((x.s_addr >> 16) & 0xff),
  1379. (int) ((x.s_addr >> 8) & 0xff),
  1380. (int) ((x.s_addr >> 0) & 0xff)
  1381. );
  1382. }
  1383. void vlan_to_string(ushort x, char *s)
  1384. {
  1385. x = ntohs(x);
  1386. if (x == (ushort)-1)
  1387. x = VLAN_NONE;
  1388. if (x == VLAN_NONE)
  1389. strcpy(s, "none");
  1390. else
  1391. sprintf(s, "%d", x & VLAN_IDMASK);
  1392. }
  1393. ushort string_to_vlan(const char *s)
  1394. {
  1395. ushort id;
  1396. if (s == NULL)
  1397. return htons(VLAN_NONE);
  1398. if (*s < '0' || *s > '9')
  1399. id = VLAN_NONE;
  1400. else
  1401. id = (ushort)dectoul(s, NULL);
  1402. return htons(id);
  1403. }
  1404. ushort env_get_vlan(char *var)
  1405. {
  1406. return string_to_vlan(env_get(var));
  1407. }