net.c 35 KB

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