net.c 36 KB

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