net.c 37 KB

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