net.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. * Copied from Linux Monitor (LiMon) - Networking.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. * Copyright 2000 Roland Borde
  7. * Copyright 2000 Paolo Scaffardi
  8. * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
  9. */
  10. /*
  11. * General Desription:
  12. *
  13. * The user interface supports commands for BOOTP, RARP, and TFTP.
  14. * Also, we support ARP internally. Depending on available data,
  15. * these interact as follows:
  16. *
  17. * BOOTP:
  18. *
  19. * Prerequisites: - own ethernet address
  20. * We want: - own IP address
  21. * - TFTP server IP address
  22. * - name of bootfile
  23. * Next step: ARP
  24. *
  25. * RARP:
  26. *
  27. * Prerequisites: - own ethernet address
  28. * We want: - own IP address
  29. * - TFTP server IP address
  30. * Next step: ARP
  31. *
  32. * ARP:
  33. *
  34. * Prerequisites: - own ethernet address
  35. * - own IP address
  36. * - TFTP server IP address
  37. * We want: - TFTP server ethernet address
  38. * Next step: TFTP
  39. *
  40. * DHCP:
  41. *
  42. * Prerequisites: - own ethernet address
  43. * We want: - IP, Netmask, ServerIP, Gateway IP
  44. * - bootfilename, lease time
  45. * Next step: - TFTP
  46. *
  47. * TFTP:
  48. *
  49. * Prerequisites: - own ethernet address
  50. * - own IP address
  51. * - TFTP server IP address
  52. * - TFTP server ethernet address
  53. * - name of bootfile (if unknown, we use a default name
  54. * derived from our own IP address)
  55. * We want: - load the boot file
  56. * Next step: none
  57. *
  58. * NFS:
  59. *
  60. * Prerequisites: - own ethernet address
  61. * - own IP address
  62. * - name of bootfile (if unknown, we use a default name
  63. * derived from our own IP address)
  64. * We want: - load the boot file
  65. * Next step: none
  66. *
  67. * SNTP:
  68. *
  69. * Prerequisites: - own ethernet address
  70. * - own IP address
  71. * We want: - network time
  72. * Next step: none
  73. */
  74. #include <common.h>
  75. #include <watchdog.h>
  76. #include <command.h>
  77. #include <linux/compiler.h>
  78. #include <net.h>
  79. #include "arp.h"
  80. #include "bootp.h"
  81. #include "tftp.h"
  82. #include "rarp.h"
  83. #include "nfs.h"
  84. #ifdef CONFIG_STATUS_LED
  85. #include <status_led.h>
  86. #include <miiphy.h>
  87. #endif
  88. #if defined(CONFIG_CMD_SNTP)
  89. #include "sntp.h"
  90. #endif
  91. #include "cdp.h"
  92. #if defined(CONFIG_CMD_DNS)
  93. #include "dns.h"
  94. #endif
  95. #include "ping.h"
  96. DECLARE_GLOBAL_DATA_PTR;
  97. /** BOOTP EXTENTIONS **/
  98. /* Our subnet mask (0=unknown) */
  99. IPaddr_t NetOurSubnetMask;
  100. /* Our gateways IP address */
  101. IPaddr_t NetOurGatewayIP;
  102. /* Our DNS IP address */
  103. IPaddr_t NetOurDNSIP;
  104. #if defined(CONFIG_BOOTP_DNS2)
  105. /* Our 2nd DNS IP address */
  106. IPaddr_t NetOurDNS2IP;
  107. #endif
  108. /* Our NIS domain */
  109. char NetOurNISDomain[32] = {0,};
  110. /* Our hostname */
  111. char NetOurHostName[32] = {0,};
  112. /* Our bootpath */
  113. char NetOurRootPath[64] = {0,};
  114. /* Our bootfile size in blocks */
  115. ushort NetBootFileSize;
  116. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  117. IPaddr_t Mcast_addr;
  118. #endif
  119. /** END OF BOOTP EXTENTIONS **/
  120. /* The actual transferred size of the bootfile (in bytes) */
  121. ulong NetBootFileXferSize;
  122. /* Our ethernet address */
  123. uchar NetOurEther[6];
  124. /* Boot server enet address */
  125. uchar NetServerEther[6];
  126. /* Our IP addr (0 = unknown) */
  127. IPaddr_t NetOurIP;
  128. /* Server IP addr (0 = unknown) */
  129. IPaddr_t NetServerIP;
  130. /* Current receive packet */
  131. uchar *NetRxPacket;
  132. /* Current rx packet length */
  133. int NetRxPacketLen;
  134. /* IP packet ID */
  135. unsigned NetIPID;
  136. /* Ethernet bcast address */
  137. uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  138. uchar NetEtherNullAddr[6];
  139. #ifdef CONFIG_API
  140. void (*push_packet)(void *, int len) = 0;
  141. #endif
  142. /* Network loop state */
  143. int NetState;
  144. /* Tried all network devices */
  145. int NetRestartWrap;
  146. /* Network loop restarted */
  147. static int NetRestarted;
  148. /* At least one device configured */
  149. static int NetDevExists;
  150. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  151. /* default is without VLAN */
  152. ushort NetOurVLAN = 0xFFFF;
  153. /* ditto */
  154. ushort NetOurNativeVLAN = 0xFFFF;
  155. /* Boot File name */
  156. char BootFile[128];
  157. #if defined(CONFIG_CMD_SNTP)
  158. /* NTP server IP address */
  159. IPaddr_t NetNtpServerIP;
  160. /* offset time from UTC */
  161. int NetTimeOffset;
  162. #endif
  163. uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  164. /* Receive packet */
  165. uchar *NetRxPackets[PKTBUFSRX];
  166. /* Current RX packet handler */
  167. static rxhand_f *packetHandler;
  168. #ifdef CONFIG_CMD_TFTPPUT
  169. static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
  170. #endif
  171. /* Current timeout handler */
  172. static thand_f *timeHandler;
  173. /* Time base value */
  174. static ulong timeStart;
  175. /* Current timeout value */
  176. static ulong timeDelta;
  177. /* THE transmit packet */
  178. uchar *NetTxPacket;
  179. static int net_check_prereq(enum proto_t protocol);
  180. static int NetTryCount;
  181. /**********************************************************************/
  182. /*
  183. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  184. * the boot file.
  185. */
  186. void net_auto_load(void)
  187. {
  188. const char *s = getenv("autoload");
  189. if (s != NULL) {
  190. if (*s == 'n') {
  191. /*
  192. * Just use BOOTP/RARP to configure system;
  193. * Do not use TFTP to load the bootfile.
  194. */
  195. NetState = NETLOOP_SUCCESS;
  196. return;
  197. }
  198. #if defined(CONFIG_CMD_NFS)
  199. if (strcmp(s, "NFS") == 0) {
  200. /*
  201. * Use NFS to load the bootfile.
  202. */
  203. NfsStart();
  204. return;
  205. }
  206. #endif
  207. }
  208. TftpStart(TFTPGET);
  209. }
  210. static void NetInitLoop(enum proto_t protocol)
  211. {
  212. static int env_changed_id;
  213. int env_id = get_env_id();
  214. /* update only when the environment has changed */
  215. if (env_changed_id != env_id) {
  216. NetOurIP = getenv_IPaddr("ipaddr");
  217. NetOurGatewayIP = getenv_IPaddr("gatewayip");
  218. NetOurSubnetMask = getenv_IPaddr("netmask");
  219. NetServerIP = getenv_IPaddr("serverip");
  220. NetOurNativeVLAN = getenv_VLAN("nvlan");
  221. NetOurVLAN = getenv_VLAN("vlan");
  222. #if defined(CONFIG_CMD_DNS)
  223. NetOurDNSIP = getenv_IPaddr("dnsip");
  224. #endif
  225. env_changed_id = env_id;
  226. }
  227. return;
  228. }
  229. /**********************************************************************/
  230. /*
  231. * Main network processing loop.
  232. */
  233. int NetLoop(enum proto_t protocol)
  234. {
  235. bd_t *bd = gd->bd;
  236. int ret = -1;
  237. NetRestarted = 0;
  238. NetDevExists = 0;
  239. NetTxPacket = NULL;
  240. NetTryCount = 1;
  241. ArpInit();
  242. if (!NetTxPacket) {
  243. int i;
  244. /*
  245. * Setup packet buffers, aligned correctly.
  246. */
  247. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  248. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  249. for (i = 0; i < PKTBUFSRX; i++)
  250. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  251. }
  252. bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
  253. eth_halt();
  254. eth_set_current();
  255. if (eth_init(bd) < 0) {
  256. eth_halt();
  257. return -1;
  258. }
  259. restart:
  260. memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
  261. NetState = NETLOOP_CONTINUE;
  262. /*
  263. * Start the ball rolling with the given start function. From
  264. * here on, this code is a state machine driven by received
  265. * packets and timer events.
  266. */
  267. NetInitLoop(protocol);
  268. switch (net_check_prereq(protocol)) {
  269. case 1:
  270. /* network not configured */
  271. eth_halt();
  272. return -1;
  273. case 2:
  274. /* network device not configured */
  275. break;
  276. case 0:
  277. NetDevExists = 1;
  278. NetBootFileXferSize = 0;
  279. switch (protocol) {
  280. case TFTPGET:
  281. #ifdef CONFIG_CMD_TFTPPUT
  282. case TFTPPUT:
  283. #endif
  284. /* always use ARP to get server ethernet address */
  285. TftpStart(protocol);
  286. break;
  287. #ifdef CONFIG_CMD_TFTPSRV
  288. case TFTPSRV:
  289. TftpStartServer();
  290. break;
  291. #endif
  292. #if defined(CONFIG_CMD_DHCP)
  293. case DHCP:
  294. BootpTry = 0;
  295. NetOurIP = 0;
  296. DhcpRequest(); /* Basically same as BOOTP */
  297. break;
  298. #endif
  299. case BOOTP:
  300. BootpTry = 0;
  301. NetOurIP = 0;
  302. BootpRequest();
  303. break;
  304. #if defined(CONFIG_CMD_RARP)
  305. case RARP:
  306. RarpTry = 0;
  307. NetOurIP = 0;
  308. RarpRequest();
  309. break;
  310. #endif
  311. #if defined(CONFIG_CMD_PING)
  312. case PING:
  313. ping_start();
  314. break;
  315. #endif
  316. #if defined(CONFIG_CMD_NFS)
  317. case NFS:
  318. NfsStart();
  319. break;
  320. #endif
  321. #if defined(CONFIG_CMD_CDP)
  322. case CDP:
  323. CDPStart();
  324. break;
  325. #endif
  326. #ifdef CONFIG_NETCONSOLE
  327. case NETCONS:
  328. NcStart();
  329. break;
  330. #endif
  331. #if defined(CONFIG_CMD_SNTP)
  332. case SNTP:
  333. SntpStart();
  334. break;
  335. #endif
  336. #if defined(CONFIG_CMD_DNS)
  337. case DNS:
  338. DnsStart();
  339. break;
  340. #endif
  341. default:
  342. break;
  343. }
  344. break;
  345. }
  346. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  347. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  348. defined(CONFIG_STATUS_LED) && \
  349. defined(STATUS_LED_RED)
  350. /*
  351. * Echo the inverted link state to the fault LED.
  352. */
  353. if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
  354. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  355. else
  356. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  357. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  358. #endif /* CONFIG_MII, ... */
  359. /*
  360. * Main packet reception loop. Loop receiving packets until
  361. * someone sets `NetState' to a state that terminates.
  362. */
  363. for (;;) {
  364. WATCHDOG_RESET();
  365. #ifdef CONFIG_SHOW_ACTIVITY
  366. show_activity(1);
  367. #endif
  368. /*
  369. * Check the ethernet for a new packet. The ethernet
  370. * receive routine will process it.
  371. */
  372. eth_rx();
  373. /*
  374. * Abort if ctrl-c was pressed.
  375. */
  376. if (ctrlc()) {
  377. eth_halt();
  378. puts("\nAbort\n");
  379. goto done;
  380. }
  381. ArpTimeoutCheck();
  382. /*
  383. * Check for a timeout, and run the timeout handler
  384. * if we have one.
  385. */
  386. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  387. thand_f *x;
  388. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  389. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  390. defined(CONFIG_STATUS_LED) && \
  391. defined(STATUS_LED_RED)
  392. /*
  393. * Echo the inverted link state to the fault LED.
  394. */
  395. if (miiphy_link(eth_get_dev()->name,
  396. CONFIG_SYS_FAULT_MII_ADDR)) {
  397. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  398. } else {
  399. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  400. }
  401. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  402. #endif /* CONFIG_MII, ... */
  403. x = timeHandler;
  404. timeHandler = (thand_f *)0;
  405. (*x)();
  406. }
  407. switch (NetState) {
  408. case NETLOOP_RESTART:
  409. NetRestarted = 1;
  410. goto restart;
  411. case NETLOOP_SUCCESS:
  412. if (NetBootFileXferSize > 0) {
  413. char buf[20];
  414. printf("Bytes transferred = %ld (%lx hex)\n",
  415. NetBootFileXferSize,
  416. NetBootFileXferSize);
  417. sprintf(buf, "%lX", NetBootFileXferSize);
  418. setenv("filesize", buf);
  419. sprintf(buf, "%lX", (unsigned long)load_addr);
  420. setenv("fileaddr", buf);
  421. }
  422. eth_halt();
  423. ret = NetBootFileXferSize;
  424. goto done;
  425. case NETLOOP_FAIL:
  426. goto done;
  427. }
  428. }
  429. done:
  430. #ifdef CONFIG_CMD_TFTPPUT
  431. /* Clear out the handlers */
  432. NetSetHandler(NULL);
  433. net_set_icmp_handler(NULL);
  434. #endif
  435. return ret;
  436. }
  437. /**********************************************************************/
  438. static void
  439. startAgainTimeout(void)
  440. {
  441. NetState = NETLOOP_RESTART;
  442. }
  443. static void
  444. startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
  445. unsigned src, unsigned len)
  446. {
  447. /* Totally ignore the packet */
  448. }
  449. void NetStartAgain(void)
  450. {
  451. char *nretry;
  452. int retry_forever = 0;
  453. unsigned long retrycnt = 0;
  454. nretry = getenv("netretry");
  455. if (nretry) {
  456. if (!strcmp(nretry, "yes"))
  457. retry_forever = 1;
  458. else if (!strcmp(nretry, "no"))
  459. retrycnt = 0;
  460. else if (!strcmp(nretry, "once"))
  461. retrycnt = 1;
  462. else
  463. retrycnt = simple_strtoul(nretry, NULL, 0);
  464. } else
  465. retry_forever = 1;
  466. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  467. eth_halt();
  468. NetState = NETLOOP_FAIL;
  469. return;
  470. }
  471. NetTryCount++;
  472. eth_halt();
  473. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  474. eth_try_another(!NetRestarted);
  475. #endif
  476. eth_init(gd->bd);
  477. if (NetRestartWrap) {
  478. NetRestartWrap = 0;
  479. if (NetDevExists) {
  480. NetSetTimeout(10000UL, startAgainTimeout);
  481. NetSetHandler(startAgainHandler);
  482. } else {
  483. NetState = NETLOOP_FAIL;
  484. }
  485. } else {
  486. NetState = NETLOOP_RESTART;
  487. }
  488. }
  489. /**********************************************************************/
  490. /*
  491. * Miscelaneous bits.
  492. */
  493. rxhand_f *
  494. NetGetHandler(void)
  495. {
  496. return packetHandler;
  497. }
  498. void
  499. NetSetHandler(rxhand_f *f)
  500. {
  501. packetHandler = f;
  502. }
  503. #ifdef CONFIG_CMD_TFTPPUT
  504. void net_set_icmp_handler(rxhand_icmp_f *f)
  505. {
  506. packet_icmp_handler = f;
  507. }
  508. #endif
  509. void
  510. NetSetTimeout(ulong iv, thand_f *f)
  511. {
  512. if (iv == 0) {
  513. timeHandler = (thand_f *)0;
  514. } else {
  515. timeHandler = f;
  516. timeStart = get_timer(0);
  517. timeDelta = iv;
  518. }
  519. }
  520. void
  521. NetSendPacket(uchar *pkt, int len)
  522. {
  523. (void) eth_send(pkt, len);
  524. }
  525. int
  526. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  527. {
  528. uchar *pkt;
  529. /* convert to new style broadcast */
  530. if (dest == 0)
  531. dest = 0xFFFFFFFF;
  532. /* if broadcast, make the ether address a broadcast and don't do ARP */
  533. if (dest == 0xFFFFFFFF)
  534. ether = NetBcastAddr;
  535. /*
  536. * if MAC address was not discovered yet, save the packet and do
  537. * an ARP request
  538. */
  539. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  540. debug("sending ARP for %08x\n", dest);
  541. NetArpWaitPacketIP = dest;
  542. NetArpWaitPacketMAC = ether;
  543. pkt = NetArpWaitTxPacket;
  544. pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
  545. NetSetIP(pkt, dest, dport, sport, len);
  546. memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
  547. (pkt - (uchar *)NetArpWaitTxPacket) +
  548. IP_UDP_HDR_SIZE, len);
  549. /* size of the waiting packet */
  550. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
  551. IP_UDP_HDR_SIZE + len;
  552. /* and do the ARP request */
  553. NetArpWaitTry = 1;
  554. NetArpWaitTimerStart = get_timer(0);
  555. ArpRequest();
  556. return 1; /* waiting */
  557. }
  558. debug("sending UDP to %08x/%pM\n", dest, ether);
  559. pkt = (uchar *)NetTxPacket;
  560. pkt += NetSetEther(pkt, ether, PROT_IP);
  561. NetSetIP(pkt, dest, dport, sport, len);
  562. eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
  563. return 0; /* transmitted */
  564. }
  565. #ifdef CONFIG_IP_DEFRAG
  566. /*
  567. * This function collects fragments in a single packet, according
  568. * to the algorithm in RFC815. It returns NULL or the pointer to
  569. * a complete packet, in static storage
  570. */
  571. #ifndef CONFIG_NET_MAXDEFRAG
  572. #define CONFIG_NET_MAXDEFRAG 16384
  573. #endif
  574. /*
  575. * MAXDEFRAG, above, is chosen in the config file and is real data
  576. * so we need to add the NFS overhead, which is more than TFTP.
  577. * To use sizeof in the internal unnamed structures, we need a real
  578. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  579. * The compiler doesn't complain nor allocates the actual structure
  580. */
  581. static struct rpc_t rpc_specimen;
  582. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  583. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
  584. /*
  585. * this is the packet being assembled, either data or frag control.
  586. * Fragments go by 8 bytes, so this union must be 8 bytes long
  587. */
  588. struct hole {
  589. /* first_byte is address of this structure */
  590. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  591. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  592. u16 prev_hole; /* index of prev, 0 == none */
  593. u16 unused;
  594. };
  595. static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  596. {
  597. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  598. static u16 first_hole, total_len;
  599. struct hole *payload, *thisfrag, *h, *newh;
  600. struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
  601. uchar *indata = (uchar *)ip;
  602. int offset8, start, len, done = 0;
  603. u16 ip_off = ntohs(ip->ip_off);
  604. /* payload starts after IP header, this fragment is in there */
  605. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
  606. offset8 = (ip_off & IP_OFFS);
  607. thisfrag = payload + offset8;
  608. start = offset8 * 8;
  609. len = ntohs(ip->ip_len) - IP_HDR_SIZE;
  610. if (start + len > IP_MAXUDP) /* fragment extends too far */
  611. return NULL;
  612. if (!total_len || localip->ip_id != ip->ip_id) {
  613. /* new (or different) packet, reset structs */
  614. total_len = 0xffff;
  615. payload[0].last_byte = ~0;
  616. payload[0].next_hole = 0;
  617. payload[0].prev_hole = 0;
  618. first_hole = 0;
  619. /* any IP header will work, copy the first we received */
  620. memcpy(localip, ip, IP_HDR_SIZE);
  621. }
  622. /*
  623. * What follows is the reassembly algorithm. We use the payload
  624. * array as a linked list of hole descriptors, as each hole starts
  625. * at a multiple of 8 bytes. However, last byte can be whatever value,
  626. * so it is represented as byte count, not as 8-byte blocks.
  627. */
  628. h = payload + first_hole;
  629. while (h->last_byte < start) {
  630. if (!h->next_hole) {
  631. /* no hole that far away */
  632. return NULL;
  633. }
  634. h = payload + h->next_hole;
  635. }
  636. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  637. if (offset8 + ((len + 7) / 8) <= h - payload) {
  638. /* no overlap with holes (dup fragment?) */
  639. return NULL;
  640. }
  641. if (!(ip_off & IP_FLAGS_MFRAG)) {
  642. /* no more fragmentss: truncate this (last) hole */
  643. total_len = start + len;
  644. h->last_byte = start + len;
  645. }
  646. /*
  647. * There is some overlap: fix the hole list. This code doesn't
  648. * deal with a fragment that overlaps with two different holes
  649. * (thus being a superset of a previously-received fragment).
  650. */
  651. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  652. /* complete overlap with hole: remove hole */
  653. if (!h->prev_hole && !h->next_hole) {
  654. /* last remaining hole */
  655. done = 1;
  656. } else if (!h->prev_hole) {
  657. /* first hole */
  658. first_hole = h->next_hole;
  659. payload[h->next_hole].prev_hole = 0;
  660. } else if (!h->next_hole) {
  661. /* last hole */
  662. payload[h->prev_hole].next_hole = 0;
  663. } else {
  664. /* in the middle of the list */
  665. payload[h->next_hole].prev_hole = h->prev_hole;
  666. payload[h->prev_hole].next_hole = h->next_hole;
  667. }
  668. } else if (h->last_byte <= start + len) {
  669. /* overlaps with final part of the hole: shorten this hole */
  670. h->last_byte = start;
  671. } else if (h >= thisfrag) {
  672. /* overlaps with initial part of the hole: move this hole */
  673. newh = thisfrag + (len / 8);
  674. *newh = *h;
  675. h = newh;
  676. if (h->next_hole)
  677. payload[h->next_hole].prev_hole = (h - payload);
  678. if (h->prev_hole)
  679. payload[h->prev_hole].next_hole = (h - payload);
  680. else
  681. first_hole = (h - payload);
  682. } else {
  683. /* fragment sits in the middle: split the hole */
  684. newh = thisfrag + (len / 8);
  685. *newh = *h;
  686. h->last_byte = start;
  687. h->next_hole = (newh - payload);
  688. newh->prev_hole = (h - payload);
  689. if (newh->next_hole)
  690. payload[newh->next_hole].prev_hole = (newh - payload);
  691. }
  692. /* finally copy this fragment and possibly return whole packet */
  693. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
  694. if (!done)
  695. return NULL;
  696. localip->ip_len = htons(total_len);
  697. *lenp = total_len + IP_HDR_SIZE;
  698. return localip;
  699. }
  700. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  701. {
  702. u16 ip_off = ntohs(ip->ip_off);
  703. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  704. return ip; /* not a fragment */
  705. return __NetDefragment(ip, lenp);
  706. }
  707. #else /* !CONFIG_IP_DEFRAG */
  708. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  709. {
  710. u16 ip_off = ntohs(ip->ip_off);
  711. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  712. return ip; /* not a fragment */
  713. return NULL;
  714. }
  715. #endif
  716. /**
  717. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  718. * drop others.
  719. *
  720. * @parma ip IP packet containing the ICMP
  721. */
  722. static void receive_icmp(struct ip_udp_hdr *ip, int len,
  723. IPaddr_t src_ip, struct ethernet_hdr *et)
  724. {
  725. ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
  726. switch (icmph->type) {
  727. case ICMP_REDIRECT:
  728. if (icmph->code != ICMP_REDIR_HOST)
  729. return;
  730. printf(" ICMP Host Redirect to %pI4 ",
  731. &icmph->un.gateway);
  732. break;
  733. default:
  734. #if defined(CONFIG_CMD_PING)
  735. ping_receive(et, ip, len);
  736. #endif
  737. #ifdef CONFIG_CMD_TFTPPUT
  738. if (packet_icmp_handler)
  739. packet_icmp_handler(icmph->type, icmph->code,
  740. ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
  741. icmph->un.data, ntohs(ip->udp_len));
  742. #endif
  743. break;
  744. }
  745. }
  746. void
  747. NetReceive(uchar *inpkt, int len)
  748. {
  749. struct ethernet_hdr *et;
  750. struct ip_udp_hdr *ip;
  751. IPaddr_t tmp;
  752. IPaddr_t src_ip;
  753. int x;
  754. #if defined(CONFIG_CMD_CDP)
  755. int iscdp;
  756. #endif
  757. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  758. debug("packet received\n");
  759. NetRxPacket = inpkt;
  760. NetRxPacketLen = len;
  761. et = (struct ethernet_hdr *)inpkt;
  762. /* too small packet? */
  763. if (len < ETHER_HDR_SIZE)
  764. return;
  765. #ifdef CONFIG_API
  766. if (push_packet) {
  767. (*push_packet)(inpkt, len);
  768. return;
  769. }
  770. #endif
  771. #if defined(CONFIG_CMD_CDP)
  772. /* keep track if packet is CDP */
  773. iscdp = is_cdp_packet(et->et_dest);
  774. #endif
  775. myvlanid = ntohs(NetOurVLAN);
  776. if (myvlanid == (ushort)-1)
  777. myvlanid = VLAN_NONE;
  778. mynvlanid = ntohs(NetOurNativeVLAN);
  779. if (mynvlanid == (ushort)-1)
  780. mynvlanid = VLAN_NONE;
  781. x = ntohs(et->et_protlen);
  782. debug("packet received\n");
  783. if (x < 1514) {
  784. struct e802_hdr *et802 = (struct e802_hdr *)et;
  785. /*
  786. * Got a 802 packet. Check the other protocol field.
  787. */
  788. x = ntohs(et802->et_prot);
  789. ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
  790. len -= E802_HDR_SIZE;
  791. } else if (x != PROT_VLAN) { /* normal packet */
  792. ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
  793. len -= ETHER_HDR_SIZE;
  794. } else { /* VLAN packet */
  795. struct vlan_ethernet_hdr *vet =
  796. (struct vlan_ethernet_hdr *)et;
  797. debug("VLAN packet received\n");
  798. /* too small packet? */
  799. if (len < VLAN_ETHER_HDR_SIZE)
  800. return;
  801. /* if no VLAN active */
  802. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  803. #if defined(CONFIG_CMD_CDP)
  804. && iscdp == 0
  805. #endif
  806. )
  807. return;
  808. cti = ntohs(vet->vet_tag);
  809. vlanid = cti & VLAN_IDMASK;
  810. x = ntohs(vet->vet_type);
  811. ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
  812. len -= VLAN_ETHER_HDR_SIZE;
  813. }
  814. debug("Receive from protocol 0x%x\n", x);
  815. #if defined(CONFIG_CMD_CDP)
  816. if (iscdp) {
  817. CDPHandler((uchar *)ip, len);
  818. return;
  819. }
  820. #endif
  821. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  822. if (vlanid == VLAN_NONE)
  823. vlanid = (mynvlanid & VLAN_IDMASK);
  824. /* not matched? */
  825. if (vlanid != (myvlanid & VLAN_IDMASK))
  826. return;
  827. }
  828. switch (x) {
  829. case PROT_ARP:
  830. ArpReceive(et, ip, len);
  831. break;
  832. #ifdef CONFIG_CMD_RARP
  833. case PROT_RARP:
  834. rarp_receive(ip, len);
  835. break;
  836. #endif
  837. case PROT_IP:
  838. debug("Got IP\n");
  839. /* Before we start poking the header, make sure it is there */
  840. if (len < IP_UDP_HDR_SIZE) {
  841. debug("len bad %d < %lu\n", len,
  842. (ulong)IP_UDP_HDR_SIZE);
  843. return;
  844. }
  845. /* Check the packet length */
  846. if (len < ntohs(ip->ip_len)) {
  847. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  848. return;
  849. }
  850. len = ntohs(ip->ip_len);
  851. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  852. /* Can't deal with anything except IPv4 */
  853. if ((ip->ip_hl_v & 0xf0) != 0x40)
  854. return;
  855. /* Can't deal with IP options (headers != 20 bytes) */
  856. if ((ip->ip_hl_v & 0x0f) > 0x05)
  857. return;
  858. /* Check the Checksum of the header */
  859. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
  860. puts("checksum bad\n");
  861. return;
  862. }
  863. /* If it is not for us, ignore it */
  864. tmp = NetReadIP(&ip->ip_dst);
  865. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  866. #ifdef CONFIG_MCAST_TFTP
  867. if (Mcast_addr != tmp)
  868. #endif
  869. return;
  870. }
  871. /* Read source IP address for later use */
  872. src_ip = NetReadIP(&ip->ip_src);
  873. /*
  874. * The function returns the unchanged packet if it's not
  875. * a fragment, and either the complete packet or NULL if
  876. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  877. */
  878. ip = NetDefragment(ip, &len);
  879. if (!ip)
  880. return;
  881. /*
  882. * watch for ICMP host redirects
  883. *
  884. * There is no real handler code (yet). We just watch
  885. * for ICMP host redirect messages. In case anybody
  886. * sees these messages: please contact me
  887. * (wd@denx.de), or - even better - send me the
  888. * necessary fixes :-)
  889. *
  890. * Note: in all cases where I have seen this so far
  891. * it was a problem with the router configuration,
  892. * for instance when a router was configured in the
  893. * BOOTP reply, but the TFTP server was on the same
  894. * subnet. So this is probably a warning that your
  895. * configuration might be wrong. But I'm not really
  896. * sure if there aren't any other situations.
  897. *
  898. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  899. * we send a tftp packet to a dead connection, or when
  900. * there is no server at the other end.
  901. */
  902. if (ip->ip_p == IPPROTO_ICMP) {
  903. receive_icmp(ip, len, src_ip, et);
  904. return;
  905. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  906. return;
  907. }
  908. #ifdef CONFIG_UDP_CHECKSUM
  909. if (ip->udp_xsum != 0) {
  910. ulong xsum;
  911. ushort *sumptr;
  912. ushort sumlen;
  913. xsum = ip->ip_p;
  914. xsum += (ntohs(ip->udp_len));
  915. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  916. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  917. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  918. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  919. sumlen = ntohs(ip->udp_len);
  920. sumptr = (ushort *) &(ip->udp_src);
  921. while (sumlen > 1) {
  922. ushort sumdata;
  923. sumdata = *sumptr++;
  924. xsum += ntohs(sumdata);
  925. sumlen -= 2;
  926. }
  927. if (sumlen > 0) {
  928. ushort sumdata;
  929. sumdata = *(unsigned char *) sumptr;
  930. sumdata = (sumdata << 8) & 0xff00;
  931. xsum += sumdata;
  932. }
  933. while ((xsum >> 16) != 0) {
  934. xsum = (xsum & 0x0000ffff) +
  935. ((xsum >> 16) & 0x0000ffff);
  936. }
  937. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  938. printf(" UDP wrong checksum %08lx %08x\n",
  939. xsum, ntohs(ip->udp_xsum));
  940. return;
  941. }
  942. }
  943. #endif
  944. #ifdef CONFIG_NETCONSOLE
  945. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  946. ntohs(ip->udp_dst),
  947. ntohs(ip->udp_src),
  948. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  949. #endif
  950. /*
  951. * IP header OK. Pass the packet to the current handler.
  952. */
  953. (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
  954. ntohs(ip->udp_dst),
  955. src_ip,
  956. ntohs(ip->udp_src),
  957. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  958. break;
  959. }
  960. }
  961. /**********************************************************************/
  962. static int net_check_prereq(enum proto_t protocol)
  963. {
  964. switch (protocol) {
  965. /* Fall through */
  966. #if defined(CONFIG_CMD_PING)
  967. case PING:
  968. if (NetPingIP == 0) {
  969. puts("*** ERROR: ping address not given\n");
  970. return 1;
  971. }
  972. goto common;
  973. #endif
  974. #if defined(CONFIG_CMD_SNTP)
  975. case SNTP:
  976. if (NetNtpServerIP == 0) {
  977. puts("*** ERROR: NTP server address not given\n");
  978. return 1;
  979. }
  980. goto common;
  981. #endif
  982. #if defined(CONFIG_CMD_DNS)
  983. case DNS:
  984. if (NetOurDNSIP == 0) {
  985. puts("*** ERROR: DNS server address not given\n");
  986. return 1;
  987. }
  988. goto common;
  989. #endif
  990. #if defined(CONFIG_CMD_NFS)
  991. case NFS:
  992. #endif
  993. case TFTPGET:
  994. case TFTPPUT:
  995. if (NetServerIP == 0) {
  996. puts("*** ERROR: `serverip' not set\n");
  997. return 1;
  998. }
  999. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1000. defined(CONFIG_CMD_DNS)
  1001. common:
  1002. #endif
  1003. /* Fall through */
  1004. case NETCONS:
  1005. case TFTPSRV:
  1006. if (NetOurIP == 0) {
  1007. puts("*** ERROR: `ipaddr' not set\n");
  1008. return 1;
  1009. }
  1010. /* Fall through */
  1011. #ifdef CONFIG_CMD_RARP
  1012. case RARP:
  1013. #endif
  1014. case BOOTP:
  1015. case CDP:
  1016. case DHCP:
  1017. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1018. int num = eth_get_dev_index();
  1019. switch (num) {
  1020. case -1:
  1021. puts("*** ERROR: No ethernet found.\n");
  1022. return 1;
  1023. case 0:
  1024. puts("*** ERROR: `ethaddr' not set\n");
  1025. break;
  1026. default:
  1027. printf("*** ERROR: `eth%daddr' not set\n",
  1028. num);
  1029. break;
  1030. }
  1031. NetStartAgain();
  1032. return 2;
  1033. }
  1034. /* Fall through */
  1035. default:
  1036. return 0;
  1037. }
  1038. return 0; /* OK */
  1039. }
  1040. /**********************************************************************/
  1041. int
  1042. NetCksumOk(uchar *ptr, int len)
  1043. {
  1044. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1045. }
  1046. unsigned
  1047. NetCksum(uchar *ptr, int len)
  1048. {
  1049. ulong xsum;
  1050. ushort *p = (ushort *)ptr;
  1051. xsum = 0;
  1052. while (len-- > 0)
  1053. xsum += *p++;
  1054. xsum = (xsum & 0xffff) + (xsum >> 16);
  1055. xsum = (xsum & 0xffff) + (xsum >> 16);
  1056. return xsum & 0xffff;
  1057. }
  1058. int
  1059. NetEthHdrSize(void)
  1060. {
  1061. ushort myvlanid;
  1062. myvlanid = ntohs(NetOurVLAN);
  1063. if (myvlanid == (ushort)-1)
  1064. myvlanid = VLAN_NONE;
  1065. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1066. VLAN_ETHER_HDR_SIZE;
  1067. }
  1068. int
  1069. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1070. {
  1071. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1072. ushort myvlanid;
  1073. myvlanid = ntohs(NetOurVLAN);
  1074. if (myvlanid == (ushort)-1)
  1075. myvlanid = VLAN_NONE;
  1076. memcpy(et->et_dest, addr, 6);
  1077. memcpy(et->et_src, NetOurEther, 6);
  1078. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1079. et->et_protlen = htons(prot);
  1080. return ETHER_HDR_SIZE;
  1081. } else {
  1082. struct vlan_ethernet_hdr *vet =
  1083. (struct vlan_ethernet_hdr *)xet;
  1084. vet->vet_vlan_type = htons(PROT_VLAN);
  1085. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1086. vet->vet_type = htons(prot);
  1087. return VLAN_ETHER_HDR_SIZE;
  1088. }
  1089. }
  1090. void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
  1091. {
  1092. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip;
  1093. /*
  1094. * If the data is an odd number of bytes, zero the
  1095. * byte after the last byte so that the checksum
  1096. * will work.
  1097. */
  1098. if (len & 1)
  1099. xip[IP_UDP_HDR_SIZE + len] = 0;
  1100. /*
  1101. * Construct an IP and UDP header.
  1102. * (need to set no fragment bit - XXX)
  1103. */
  1104. /* IP_HDR_SIZE / 4 (not including UDP) */
  1105. ip->ip_hl_v = 0x45;
  1106. ip->ip_tos = 0;
  1107. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1108. ip->ip_id = htons(NetIPID++);
  1109. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1110. ip->ip_ttl = 255;
  1111. ip->ip_p = 17; /* UDP */
  1112. ip->ip_sum = 0;
  1113. /* already in network byte order */
  1114. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  1115. /* - "" - */
  1116. NetCopyIP((void *)&ip->ip_dst, &dest);
  1117. ip->udp_src = htons(sport);
  1118. ip->udp_dst = htons(dport);
  1119. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1120. ip->udp_xsum = 0;
  1121. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
  1122. }
  1123. void copy_filename(char *dst, const char *src, int size)
  1124. {
  1125. if (*src && (*src == '"')) {
  1126. ++src;
  1127. --size;
  1128. }
  1129. while ((--size > 0) && *src && (*src != '"'))
  1130. *dst++ = *src++;
  1131. *dst = '\0';
  1132. }
  1133. #if defined(CONFIG_CMD_NFS) || \
  1134. defined(CONFIG_CMD_SNTP) || \
  1135. defined(CONFIG_CMD_DNS)
  1136. /*
  1137. * make port a little random (1024-17407)
  1138. * This keeps the math somewhat trivial to compute, and seems to work with
  1139. * all supported protocols/clients/servers
  1140. */
  1141. unsigned int random_port(void)
  1142. {
  1143. return 1024 + (get_timer(0) % 0x4000);
  1144. }
  1145. #endif
  1146. void ip_to_string(IPaddr_t x, char *s)
  1147. {
  1148. x = ntohl(x);
  1149. sprintf(s, "%d.%d.%d.%d",
  1150. (int) ((x >> 24) & 0xff),
  1151. (int) ((x >> 16) & 0xff),
  1152. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1153. );
  1154. }
  1155. void VLAN_to_string(ushort x, char *s)
  1156. {
  1157. x = ntohs(x);
  1158. if (x == (ushort)-1)
  1159. x = VLAN_NONE;
  1160. if (x == VLAN_NONE)
  1161. strcpy(s, "none");
  1162. else
  1163. sprintf(s, "%d", x & VLAN_IDMASK);
  1164. }
  1165. ushort string_to_VLAN(const char *s)
  1166. {
  1167. ushort id;
  1168. if (s == NULL)
  1169. return htons(VLAN_NONE);
  1170. if (*s < '0' || *s > '9')
  1171. id = VLAN_NONE;
  1172. else
  1173. id = (ushort)simple_strtoul(s, NULL, 10);
  1174. return htons(id);
  1175. }
  1176. ushort getenv_VLAN(char *var)
  1177. {
  1178. return string_to_VLAN(getenv(var));
  1179. }