net.c 37 KB

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