net.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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. int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
  712. int payload_len)
  713. {
  714. uchar *pkt;
  715. int eth_hdr_size;
  716. int pkt_hdr_size;
  717. /* make sure the net_tx_packet is initialized (net_init() was called) */
  718. assert(net_tx_packet != NULL);
  719. if (net_tx_packet == NULL)
  720. return -1;
  721. /* convert to new style broadcast */
  722. if (dest.s_addr == 0)
  723. dest.s_addr = 0xFFFFFFFF;
  724. /* if broadcast, make the ether address a broadcast and don't do ARP */
  725. if (dest.s_addr == 0xFFFFFFFF)
  726. ether = (uchar *)net_bcast_ethaddr;
  727. pkt = (uchar *)net_tx_packet;
  728. eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
  729. pkt += eth_hdr_size;
  730. net_set_udp_header(pkt, dest, dport, sport, payload_len);
  731. pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
  732. /* if MAC address was not discovered yet, do an ARP request */
  733. if (memcmp(ether, net_null_ethaddr, 6) == 0) {
  734. debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
  735. /* save the ip and eth addr for the packet to send after arp */
  736. net_arp_wait_packet_ip = dest;
  737. arp_wait_packet_ethaddr = ether;
  738. /* size of the waiting packet */
  739. arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
  740. /* and do the ARP request */
  741. arp_wait_try = 1;
  742. arp_wait_timer_start = get_timer(0);
  743. arp_request();
  744. return 1; /* waiting */
  745. } else {
  746. debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
  747. &dest, ether);
  748. net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
  749. return 0; /* transmitted */
  750. }
  751. }
  752. #ifdef CONFIG_IP_DEFRAG
  753. /*
  754. * This function collects fragments in a single packet, according
  755. * to the algorithm in RFC815. It returns NULL or the pointer to
  756. * a complete packet, in static storage
  757. */
  758. #ifndef CONFIG_NET_MAXDEFRAG
  759. #define CONFIG_NET_MAXDEFRAG 16384
  760. #endif
  761. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
  762. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
  763. /*
  764. * this is the packet being assembled, either data or frag control.
  765. * Fragments go by 8 bytes, so this union must be 8 bytes long
  766. */
  767. struct hole {
  768. /* first_byte is address of this structure */
  769. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  770. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  771. u16 prev_hole; /* index of prev, 0 == none */
  772. u16 unused;
  773. };
  774. static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
  775. {
  776. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  777. static u16 first_hole, total_len;
  778. struct hole *payload, *thisfrag, *h, *newh;
  779. struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
  780. uchar *indata = (uchar *)ip;
  781. int offset8, start, len, done = 0;
  782. u16 ip_off = ntohs(ip->ip_off);
  783. /* payload starts after IP header, this fragment is in there */
  784. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
  785. offset8 = (ip_off & IP_OFFS);
  786. thisfrag = payload + offset8;
  787. start = offset8 * 8;
  788. len = ntohs(ip->ip_len) - IP_HDR_SIZE;
  789. if (start + len > IP_MAXUDP) /* fragment extends too far */
  790. return NULL;
  791. if (!total_len || localip->ip_id != ip->ip_id) {
  792. /* new (or different) packet, reset structs */
  793. total_len = 0xffff;
  794. payload[0].last_byte = ~0;
  795. payload[0].next_hole = 0;
  796. payload[0].prev_hole = 0;
  797. first_hole = 0;
  798. /* any IP header will work, copy the first we received */
  799. memcpy(localip, ip, IP_HDR_SIZE);
  800. }
  801. /*
  802. * What follows is the reassembly algorithm. We use the payload
  803. * array as a linked list of hole descriptors, as each hole starts
  804. * at a multiple of 8 bytes. However, last byte can be whatever value,
  805. * so it is represented as byte count, not as 8-byte blocks.
  806. */
  807. h = payload + first_hole;
  808. while (h->last_byte < start) {
  809. if (!h->next_hole) {
  810. /* no hole that far away */
  811. return NULL;
  812. }
  813. h = payload + h->next_hole;
  814. }
  815. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  816. if (offset8 + ((len + 7) / 8) <= h - payload) {
  817. /* no overlap with holes (dup fragment?) */
  818. return NULL;
  819. }
  820. if (!(ip_off & IP_FLAGS_MFRAG)) {
  821. /* no more fragmentss: truncate this (last) hole */
  822. total_len = start + len;
  823. h->last_byte = start + len;
  824. }
  825. /*
  826. * There is some overlap: fix the hole list. This code doesn't
  827. * deal with a fragment that overlaps with two different holes
  828. * (thus being a superset of a previously-received fragment).
  829. */
  830. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  831. /* complete overlap with hole: remove hole */
  832. if (!h->prev_hole && !h->next_hole) {
  833. /* last remaining hole */
  834. done = 1;
  835. } else if (!h->prev_hole) {
  836. /* first hole */
  837. first_hole = h->next_hole;
  838. payload[h->next_hole].prev_hole = 0;
  839. } else if (!h->next_hole) {
  840. /* last hole */
  841. payload[h->prev_hole].next_hole = 0;
  842. } else {
  843. /* in the middle of the list */
  844. payload[h->next_hole].prev_hole = h->prev_hole;
  845. payload[h->prev_hole].next_hole = h->next_hole;
  846. }
  847. } else if (h->last_byte <= start + len) {
  848. /* overlaps with final part of the hole: shorten this hole */
  849. h->last_byte = start;
  850. } else if (h >= thisfrag) {
  851. /* overlaps with initial part of the hole: move this hole */
  852. newh = thisfrag + (len / 8);
  853. *newh = *h;
  854. h = newh;
  855. if (h->next_hole)
  856. payload[h->next_hole].prev_hole = (h - payload);
  857. if (h->prev_hole)
  858. payload[h->prev_hole].next_hole = (h - payload);
  859. else
  860. first_hole = (h - payload);
  861. } else {
  862. /* fragment sits in the middle: split the hole */
  863. newh = thisfrag + (len / 8);
  864. *newh = *h;
  865. h->last_byte = start;
  866. h->next_hole = (newh - payload);
  867. newh->prev_hole = (h - payload);
  868. if (newh->next_hole)
  869. payload[newh->next_hole].prev_hole = (newh - payload);
  870. }
  871. /* finally copy this fragment and possibly return whole packet */
  872. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
  873. if (!done)
  874. return NULL;
  875. localip->ip_len = htons(total_len);
  876. *lenp = total_len + IP_HDR_SIZE;
  877. return localip;
  878. }
  879. static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
  880. int *lenp)
  881. {
  882. u16 ip_off = ntohs(ip->ip_off);
  883. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  884. return ip; /* not a fragment */
  885. return __net_defragment(ip, lenp);
  886. }
  887. #else /* !CONFIG_IP_DEFRAG */
  888. static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
  889. int *lenp)
  890. {
  891. u16 ip_off = ntohs(ip->ip_off);
  892. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  893. return ip; /* not a fragment */
  894. return NULL;
  895. }
  896. #endif
  897. /**
  898. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  899. * drop others.
  900. *
  901. * @parma ip IP packet containing the ICMP
  902. */
  903. static void receive_icmp(struct ip_udp_hdr *ip, int len,
  904. struct in_addr src_ip, struct ethernet_hdr *et)
  905. {
  906. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  907. switch (icmph->type) {
  908. case ICMP_REDIRECT:
  909. if (icmph->code != ICMP_REDIR_HOST)
  910. return;
  911. printf(" ICMP Host Redirect to %pI4 ",
  912. &icmph->un.gateway);
  913. break;
  914. default:
  915. #if defined(CONFIG_CMD_PING)
  916. ping_receive(et, ip, len);
  917. #endif
  918. #ifdef CONFIG_CMD_TFTPPUT
  919. if (packet_icmp_handler)
  920. packet_icmp_handler(icmph->type, icmph->code,
  921. ntohs(ip->udp_dst), src_ip,
  922. ntohs(ip->udp_src), icmph->un.data,
  923. ntohs(ip->udp_len));
  924. #endif
  925. break;
  926. }
  927. }
  928. void net_process_received_packet(uchar *in_packet, int len)
  929. {
  930. struct ethernet_hdr *et;
  931. struct ip_udp_hdr *ip;
  932. struct in_addr dst_ip;
  933. struct in_addr src_ip;
  934. int eth_proto;
  935. #if defined(CONFIG_CMD_CDP)
  936. int iscdp;
  937. #endif
  938. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  939. debug_cond(DEBUG_NET_PKT, "packet received\n");
  940. net_rx_packet = in_packet;
  941. net_rx_packet_len = len;
  942. et = (struct ethernet_hdr *)in_packet;
  943. /* too small packet? */
  944. if (len < ETHER_HDR_SIZE)
  945. return;
  946. #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
  947. if (push_packet) {
  948. (*push_packet)(in_packet, len);
  949. return;
  950. }
  951. #endif
  952. #if defined(CONFIG_CMD_CDP)
  953. /* keep track if packet is CDP */
  954. iscdp = is_cdp_packet(et->et_dest);
  955. #endif
  956. myvlanid = ntohs(net_our_vlan);
  957. if (myvlanid == (ushort)-1)
  958. myvlanid = VLAN_NONE;
  959. mynvlanid = ntohs(net_native_vlan);
  960. if (mynvlanid == (ushort)-1)
  961. mynvlanid = VLAN_NONE;
  962. eth_proto = ntohs(et->et_protlen);
  963. if (eth_proto < 1514) {
  964. struct e802_hdr *et802 = (struct e802_hdr *)et;
  965. /*
  966. * Got a 802.2 packet. Check the other protocol field.
  967. * XXX VLAN over 802.2+SNAP not implemented!
  968. */
  969. eth_proto = ntohs(et802->et_prot);
  970. ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
  971. len -= E802_HDR_SIZE;
  972. } else if (eth_proto != PROT_VLAN) { /* normal packet */
  973. ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
  974. len -= ETHER_HDR_SIZE;
  975. } else { /* VLAN packet */
  976. struct vlan_ethernet_hdr *vet =
  977. (struct vlan_ethernet_hdr *)et;
  978. debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
  979. /* too small packet? */
  980. if (len < VLAN_ETHER_HDR_SIZE)
  981. return;
  982. /* if no VLAN active */
  983. if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
  984. #if defined(CONFIG_CMD_CDP)
  985. && iscdp == 0
  986. #endif
  987. )
  988. return;
  989. cti = ntohs(vet->vet_tag);
  990. vlanid = cti & VLAN_IDMASK;
  991. eth_proto = ntohs(vet->vet_type);
  992. ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
  993. len -= VLAN_ETHER_HDR_SIZE;
  994. }
  995. debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
  996. #if defined(CONFIG_CMD_CDP)
  997. if (iscdp) {
  998. cdp_receive((uchar *)ip, len);
  999. return;
  1000. }
  1001. #endif
  1002. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  1003. if (vlanid == VLAN_NONE)
  1004. vlanid = (mynvlanid & VLAN_IDMASK);
  1005. /* not matched? */
  1006. if (vlanid != (myvlanid & VLAN_IDMASK))
  1007. return;
  1008. }
  1009. switch (eth_proto) {
  1010. case PROT_ARP:
  1011. arp_receive(et, ip, len);
  1012. break;
  1013. #ifdef CONFIG_CMD_RARP
  1014. case PROT_RARP:
  1015. rarp_receive(ip, len);
  1016. break;
  1017. #endif
  1018. case PROT_IP:
  1019. debug_cond(DEBUG_NET_PKT, "Got IP\n");
  1020. /* Before we start poking the header, make sure it is there */
  1021. if (len < IP_UDP_HDR_SIZE) {
  1022. debug("len bad %d < %lu\n", len,
  1023. (ulong)IP_UDP_HDR_SIZE);
  1024. return;
  1025. }
  1026. /* Check the packet length */
  1027. if (len < ntohs(ip->ip_len)) {
  1028. debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
  1029. return;
  1030. }
  1031. len = ntohs(ip->ip_len);
  1032. debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
  1033. len, ip->ip_hl_v & 0xff);
  1034. /* Can't deal with anything except IPv4 */
  1035. if ((ip->ip_hl_v & 0xf0) != 0x40)
  1036. return;
  1037. /* Can't deal with IP options (headers != 20 bytes) */
  1038. if ((ip->ip_hl_v & 0x0f) > 0x05)
  1039. return;
  1040. /* Check the Checksum of the header */
  1041. if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
  1042. debug("checksum bad\n");
  1043. return;
  1044. }
  1045. /* If it is not for us, ignore it */
  1046. dst_ip = net_read_ip(&ip->ip_dst);
  1047. if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
  1048. dst_ip.s_addr != 0xFFFFFFFF) {
  1049. #ifdef CONFIG_MCAST_TFTP
  1050. if (net_mcast_addr != dst_ip)
  1051. #endif
  1052. return;
  1053. }
  1054. /* Read source IP address for later use */
  1055. src_ip = net_read_ip(&ip->ip_src);
  1056. /*
  1057. * The function returns the unchanged packet if it's not
  1058. * a fragment, and either the complete packet or NULL if
  1059. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  1060. */
  1061. ip = net_defragment(ip, &len);
  1062. if (!ip)
  1063. return;
  1064. /*
  1065. * watch for ICMP host redirects
  1066. *
  1067. * There is no real handler code (yet). We just watch
  1068. * for ICMP host redirect messages. In case anybody
  1069. * sees these messages: please contact me
  1070. * (wd@denx.de), or - even better - send me the
  1071. * necessary fixes :-)
  1072. *
  1073. * Note: in all cases where I have seen this so far
  1074. * it was a problem with the router configuration,
  1075. * for instance when a router was configured in the
  1076. * BOOTP reply, but the TFTP server was on the same
  1077. * subnet. So this is probably a warning that your
  1078. * configuration might be wrong. But I'm not really
  1079. * sure if there aren't any other situations.
  1080. *
  1081. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  1082. * we send a tftp packet to a dead connection, or when
  1083. * there is no server at the other end.
  1084. */
  1085. if (ip->ip_p == IPPROTO_ICMP) {
  1086. receive_icmp(ip, len, src_ip, et);
  1087. return;
  1088. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1089. return;
  1090. }
  1091. debug_cond(DEBUG_DEV_PKT,
  1092. "received UDP (to=%pI4, from=%pI4, len=%d)\n",
  1093. &dst_ip, &src_ip, len);
  1094. #ifdef CONFIG_UDP_CHECKSUM
  1095. if (ip->udp_xsum != 0) {
  1096. ulong xsum;
  1097. ushort *sumptr;
  1098. ushort sumlen;
  1099. xsum = ip->ip_p;
  1100. xsum += (ntohs(ip->udp_len));
  1101. xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
  1102. xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
  1103. xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
  1104. xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
  1105. sumlen = ntohs(ip->udp_len);
  1106. sumptr = (ushort *)&(ip->udp_src);
  1107. while (sumlen > 1) {
  1108. ushort sumdata;
  1109. sumdata = *sumptr++;
  1110. xsum += ntohs(sumdata);
  1111. sumlen -= 2;
  1112. }
  1113. if (sumlen > 0) {
  1114. ushort sumdata;
  1115. sumdata = *(unsigned char *)sumptr;
  1116. sumdata = (sumdata << 8) & 0xff00;
  1117. xsum += sumdata;
  1118. }
  1119. while ((xsum >> 16) != 0) {
  1120. xsum = (xsum & 0x0000ffff) +
  1121. ((xsum >> 16) & 0x0000ffff);
  1122. }
  1123. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1124. printf(" UDP wrong checksum %08lx %08x\n",
  1125. xsum, ntohs(ip->udp_xsum));
  1126. return;
  1127. }
  1128. }
  1129. #endif
  1130. #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
  1131. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  1132. src_ip,
  1133. ntohs(ip->udp_dst),
  1134. ntohs(ip->udp_src),
  1135. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1136. #endif
  1137. /*
  1138. * IP header OK. Pass the packet to the current handler.
  1139. */
  1140. (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
  1141. ntohs(ip->udp_dst),
  1142. src_ip,
  1143. ntohs(ip->udp_src),
  1144. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1145. break;
  1146. #ifdef CONFIG_CMD_WOL
  1147. case PROT_WOL:
  1148. wol_receive(ip, len);
  1149. break;
  1150. #endif
  1151. }
  1152. }
  1153. /**********************************************************************/
  1154. static int net_check_prereq(enum proto_t protocol)
  1155. {
  1156. switch (protocol) {
  1157. /* Fall through */
  1158. #if defined(CONFIG_CMD_PING)
  1159. case PING:
  1160. if (net_ping_ip.s_addr == 0) {
  1161. puts("*** ERROR: ping address not given\n");
  1162. return 1;
  1163. }
  1164. goto common;
  1165. #endif
  1166. #if defined(CONFIG_CMD_SNTP)
  1167. case SNTP:
  1168. if (net_ntp_server.s_addr == 0) {
  1169. puts("*** ERROR: NTP server address not given\n");
  1170. return 1;
  1171. }
  1172. goto common;
  1173. #endif
  1174. #if defined(CONFIG_CMD_DNS)
  1175. case DNS:
  1176. if (net_dns_server.s_addr == 0) {
  1177. puts("*** ERROR: DNS server address not given\n");
  1178. return 1;
  1179. }
  1180. goto common;
  1181. #endif
  1182. #if defined(CONFIG_CMD_NFS)
  1183. case NFS:
  1184. #endif
  1185. /* Fall through */
  1186. case TFTPGET:
  1187. case TFTPPUT:
  1188. if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
  1189. puts("*** ERROR: `serverip' not set\n");
  1190. return 1;
  1191. }
  1192. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1193. defined(CONFIG_CMD_DNS)
  1194. common:
  1195. #endif
  1196. /* Fall through */
  1197. case NETCONS:
  1198. case FASTBOOT:
  1199. case TFTPSRV:
  1200. if (net_ip.s_addr == 0) {
  1201. puts("*** ERROR: `ipaddr' not set\n");
  1202. return 1;
  1203. }
  1204. /* Fall through */
  1205. #ifdef CONFIG_CMD_RARP
  1206. case RARP:
  1207. #endif
  1208. case BOOTP:
  1209. case CDP:
  1210. case DHCP:
  1211. case LINKLOCAL:
  1212. if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
  1213. int num = eth_get_dev_index();
  1214. switch (num) {
  1215. case -1:
  1216. puts("*** ERROR: No ethernet found.\n");
  1217. return 1;
  1218. case 0:
  1219. puts("*** ERROR: `ethaddr' not set\n");
  1220. break;
  1221. default:
  1222. printf("*** ERROR: `eth%daddr' not set\n",
  1223. num);
  1224. break;
  1225. }
  1226. net_start_again();
  1227. return 2;
  1228. }
  1229. /* Fall through */
  1230. default:
  1231. return 0;
  1232. }
  1233. return 0; /* OK */
  1234. }
  1235. /**********************************************************************/
  1236. int
  1237. net_eth_hdr_size(void)
  1238. {
  1239. ushort myvlanid;
  1240. myvlanid = ntohs(net_our_vlan);
  1241. if (myvlanid == (ushort)-1)
  1242. myvlanid = VLAN_NONE;
  1243. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1244. VLAN_ETHER_HDR_SIZE;
  1245. }
  1246. int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
  1247. {
  1248. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1249. ushort myvlanid;
  1250. myvlanid = ntohs(net_our_vlan);
  1251. if (myvlanid == (ushort)-1)
  1252. myvlanid = VLAN_NONE;
  1253. memcpy(et->et_dest, dest_ethaddr, 6);
  1254. memcpy(et->et_src, net_ethaddr, 6);
  1255. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1256. et->et_protlen = htons(prot);
  1257. return ETHER_HDR_SIZE;
  1258. } else {
  1259. struct vlan_ethernet_hdr *vet =
  1260. (struct vlan_ethernet_hdr *)xet;
  1261. vet->vet_vlan_type = htons(PROT_VLAN);
  1262. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1263. vet->vet_type = htons(prot);
  1264. return VLAN_ETHER_HDR_SIZE;
  1265. }
  1266. }
  1267. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
  1268. {
  1269. ushort protlen;
  1270. memcpy(et->et_dest, addr, 6);
  1271. memcpy(et->et_src, net_ethaddr, 6);
  1272. protlen = ntohs(et->et_protlen);
  1273. if (protlen == PROT_VLAN) {
  1274. struct vlan_ethernet_hdr *vet =
  1275. (struct vlan_ethernet_hdr *)et;
  1276. vet->vet_type = htons(prot);
  1277. return VLAN_ETHER_HDR_SIZE;
  1278. } else if (protlen > 1514) {
  1279. et->et_protlen = htons(prot);
  1280. return ETHER_HDR_SIZE;
  1281. } else {
  1282. /* 802.2 + SNAP */
  1283. struct e802_hdr *et802 = (struct e802_hdr *)et;
  1284. et802->et_prot = htons(prot);
  1285. return E802_HDR_SIZE;
  1286. }
  1287. }
  1288. void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
  1289. {
  1290. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1291. /*
  1292. * Construct an IP header.
  1293. */
  1294. /* IP_HDR_SIZE / 4 (not including UDP) */
  1295. ip->ip_hl_v = 0x45;
  1296. ip->ip_tos = 0;
  1297. ip->ip_len = htons(IP_HDR_SIZE);
  1298. ip->ip_id = htons(net_ip_id++);
  1299. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1300. ip->ip_ttl = 255;
  1301. ip->ip_sum = 0;
  1302. /* already in network byte order */
  1303. net_copy_ip((void *)&ip->ip_src, &source);
  1304. /* already in network byte order */
  1305. net_copy_ip((void *)&ip->ip_dst, &dest);
  1306. }
  1307. void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
  1308. int len)
  1309. {
  1310. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1311. /*
  1312. * If the data is an odd number of bytes, zero the
  1313. * byte after the last byte so that the checksum
  1314. * will work.
  1315. */
  1316. if (len & 1)
  1317. pkt[IP_UDP_HDR_SIZE + len] = 0;
  1318. net_set_ip_header(pkt, dest, net_ip);
  1319. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1320. ip->ip_p = IPPROTO_UDP;
  1321. ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
  1322. ip->udp_src = htons(sport);
  1323. ip->udp_dst = htons(dport);
  1324. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1325. ip->udp_xsum = 0;
  1326. }
  1327. void copy_filename(char *dst, const char *src, int size)
  1328. {
  1329. if (src && *src && (*src == '"')) {
  1330. ++src;
  1331. --size;
  1332. }
  1333. while ((--size > 0) && src && *src && (*src != '"'))
  1334. *dst++ = *src++;
  1335. *dst = '\0';
  1336. }
  1337. int is_serverip_in_cmd(void)
  1338. {
  1339. return !!strchr(net_boot_file_name, ':');
  1340. }
  1341. int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
  1342. {
  1343. char *colon;
  1344. if (net_boot_file_name[0] == '\0')
  1345. return 0;
  1346. colon = strchr(net_boot_file_name, ':');
  1347. if (colon) {
  1348. if (ipaddr)
  1349. *ipaddr = string_to_ip(net_boot_file_name);
  1350. strncpy(filename, colon + 1, max_len);
  1351. } else {
  1352. strncpy(filename, net_boot_file_name, max_len);
  1353. }
  1354. filename[max_len - 1] = '\0';
  1355. return 1;
  1356. }
  1357. #if defined(CONFIG_CMD_NFS) || \
  1358. defined(CONFIG_CMD_SNTP) || \
  1359. defined(CONFIG_CMD_DNS)
  1360. /*
  1361. * make port a little random (1024-17407)
  1362. * This keeps the math somewhat trivial to compute, and seems to work with
  1363. * all supported protocols/clients/servers
  1364. */
  1365. unsigned int random_port(void)
  1366. {
  1367. return 1024 + (get_timer(0) % 0x4000);
  1368. }
  1369. #endif
  1370. void ip_to_string(struct in_addr x, char *s)
  1371. {
  1372. x.s_addr = ntohl(x.s_addr);
  1373. sprintf(s, "%d.%d.%d.%d",
  1374. (int) ((x.s_addr >> 24) & 0xff),
  1375. (int) ((x.s_addr >> 16) & 0xff),
  1376. (int) ((x.s_addr >> 8) & 0xff),
  1377. (int) ((x.s_addr >> 0) & 0xff)
  1378. );
  1379. }
  1380. void vlan_to_string(ushort x, char *s)
  1381. {
  1382. x = ntohs(x);
  1383. if (x == (ushort)-1)
  1384. x = VLAN_NONE;
  1385. if (x == VLAN_NONE)
  1386. strcpy(s, "none");
  1387. else
  1388. sprintf(s, "%d", x & VLAN_IDMASK);
  1389. }
  1390. ushort string_to_vlan(const char *s)
  1391. {
  1392. ushort id;
  1393. if (s == NULL)
  1394. return htons(VLAN_NONE);
  1395. if (*s < '0' || *s > '9')
  1396. id = VLAN_NONE;
  1397. else
  1398. id = (ushort)simple_strtoul(s, NULL, 10);
  1399. return htons(id);
  1400. }
  1401. ushort env_get_vlan(char *var)
  1402. {
  1403. return string_to_vlan(env_get(var));
  1404. }