net.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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 <net.h>
  78. #include "bootp.h"
  79. #include "tftp.h"
  80. #include "rarp.h"
  81. #include "nfs.h"
  82. #ifdef CONFIG_STATUS_LED
  83. #include <status_led.h>
  84. #include <miiphy.h>
  85. #endif
  86. #if defined(CONFIG_CMD_SNTP)
  87. #include "sntp.h"
  88. #endif
  89. #if defined(CONFIG_CDP_VERSION)
  90. #include <timestamp.h>
  91. #endif
  92. #if defined(CONFIG_CMD_DNS)
  93. #include "dns.h"
  94. #endif
  95. DECLARE_GLOBAL_DATA_PTR;
  96. #ifndef CONFIG_ARP_TIMEOUT
  97. # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
  98. #else
  99. # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
  100. #endif
  101. #ifndef CONFIG_NET_RETRY_COUNT
  102. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  103. #else
  104. # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
  105. #endif
  106. /** BOOTP EXTENTIONS **/
  107. IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
  108. IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
  109. IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
  110. #if defined(CONFIG_BOOTP_DNS2)
  111. IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
  112. #endif
  113. char NetOurNISDomain[32]={0,}; /* Our NIS domain */
  114. char NetOurHostName[32]={0,}; /* Our hostname */
  115. char NetOurRootPath[64]={0,}; /* Our bootpath */
  116. ushort NetBootFileSize=0; /* Our bootfile size in blocks */
  117. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  118. IPaddr_t Mcast_addr;
  119. #endif
  120. /** END OF BOOTP EXTENTIONS **/
  121. ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
  122. uchar NetOurEther[6]; /* Our ethernet address */
  123. uchar NetServerEther[6] = /* Boot server enet address */
  124. { 0, 0, 0, 0, 0, 0 };
  125. IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  126. IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  127. volatile uchar *NetRxPacket; /* Current receive packet */
  128. int NetRxPacketLen; /* Current rx packet length */
  129. unsigned NetIPID; /* IP packet ID */
  130. uchar NetBcastAddr[6] = /* Ethernet bcast address */
  131. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  132. uchar NetEtherNullAddr[6] =
  133. { 0, 0, 0, 0, 0, 0 };
  134. #ifdef CONFIG_API
  135. void (*push_packet)(volatile void *, int len) = 0;
  136. #endif
  137. #if defined(CONFIG_CMD_CDP)
  138. uchar NetCDPAddr[6] = /* Ethernet bcast address */
  139. { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
  140. #endif
  141. int NetState; /* Network loop state */
  142. #ifdef CONFIG_NET_MULTI
  143. int NetRestartWrap = 0; /* Tried all network devices */
  144. static int NetRestarted = 0; /* Network loop restarted */
  145. static int NetDevExists = 0; /* At least one device configured */
  146. #endif
  147. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  148. ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
  149. ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
  150. char BootFile[128]; /* Boot File name */
  151. #if defined(CONFIG_CMD_PING)
  152. IPaddr_t NetPingIP; /* the ip address to ping */
  153. static void PingStart(void);
  154. #endif
  155. #if defined(CONFIG_CMD_CDP)
  156. static void CDPStart(void);
  157. #endif
  158. #if defined(CONFIG_CMD_SNTP)
  159. IPaddr_t NetNtpServerIP; /* NTP server IP address */
  160. int NetTimeOffset=0; /* offset time from UTC */
  161. #endif
  162. #ifdef CONFIG_NETCONSOLE
  163. void NcStart(void);
  164. int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
  165. #endif
  166. volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  167. volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
  168. static rxhand_f *packetHandler; /* Current RX packet handler */
  169. static thand_f *timeHandler; /* Current timeout handler */
  170. static ulong timeStart; /* Time base value */
  171. static ulong timeDelta; /* Current timeout value */
  172. volatile uchar *NetTxPacket = 0; /* THE transmit packet */
  173. static int net_check_prereq (proto_t protocol);
  174. static int NetTryCount;
  175. /**********************************************************************/
  176. IPaddr_t NetArpWaitPacketIP;
  177. IPaddr_t NetArpWaitReplyIP;
  178. uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
  179. uchar *NetArpWaitTxPacket; /* THE transmit packet */
  180. int NetArpWaitTxPacketSize;
  181. uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
  182. ulong NetArpWaitTimerStart;
  183. int NetArpWaitTry;
  184. void ArpRequest (void)
  185. {
  186. int i;
  187. volatile uchar *pkt;
  188. ARP_t *arp;
  189. debug("ARP broadcast %d\n", NetArpWaitTry);
  190. pkt = NetTxPacket;
  191. pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
  192. arp = (ARP_t *) pkt;
  193. arp->ar_hrd = htons (ARP_ETHER);
  194. arp->ar_pro = htons (PROT_IP);
  195. arp->ar_hln = 6;
  196. arp->ar_pln = 4;
  197. arp->ar_op = htons (ARPOP_REQUEST);
  198. memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
  199. NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
  200. for (i = 10; i < 16; ++i) {
  201. arp->ar_data[i] = 0; /* dest ET addr = 0 */
  202. }
  203. if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
  204. (NetOurIP & NetOurSubnetMask)) {
  205. if (NetOurGatewayIP == 0) {
  206. puts ("## Warning: gatewayip needed but not set\n");
  207. NetArpWaitReplyIP = NetArpWaitPacketIP;
  208. } else {
  209. NetArpWaitReplyIP = NetOurGatewayIP;
  210. }
  211. } else {
  212. NetArpWaitReplyIP = NetArpWaitPacketIP;
  213. }
  214. NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
  215. (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
  216. }
  217. void ArpTimeoutCheck(void)
  218. {
  219. ulong t;
  220. if (!NetArpWaitPacketIP)
  221. return;
  222. t = get_timer(0);
  223. /* check for arp timeout */
  224. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
  225. NetArpWaitTry++;
  226. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  227. puts ("\nARP Retry count exceeded; starting again\n");
  228. NetArpWaitTry = 0;
  229. NetStartAgain();
  230. } else {
  231. NetArpWaitTimerStart = t;
  232. ArpRequest();
  233. }
  234. }
  235. }
  236. static void
  237. NetInitLoop(proto_t protocol)
  238. {
  239. static int env_changed_id = 0;
  240. bd_t *bd = gd->bd;
  241. int env_id = get_env_id ();
  242. /* update only when the environment has changed */
  243. if (env_changed_id != env_id) {
  244. NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
  245. NetOurGatewayIP = getenv_IPaddr ("gatewayip");
  246. NetOurSubnetMask= getenv_IPaddr ("netmask");
  247. NetServerIP = getenv_IPaddr ("serverip");
  248. NetOurNativeVLAN = getenv_VLAN("nvlan");
  249. NetOurVLAN = getenv_VLAN("vlan");
  250. #if defined(CONFIG_CMD_DNS)
  251. NetOurDNSIP = getenv_IPaddr("dnsip");
  252. #endif
  253. env_changed_id = env_id;
  254. }
  255. return;
  256. }
  257. /**********************************************************************/
  258. /*
  259. * Main network processing loop.
  260. */
  261. int
  262. NetLoop(proto_t protocol)
  263. {
  264. bd_t *bd = gd->bd;
  265. #ifdef CONFIG_NET_MULTI
  266. NetRestarted = 0;
  267. NetDevExists = 0;
  268. #endif
  269. /* XXX problem with bss workaround */
  270. NetArpWaitPacketMAC = NULL;
  271. NetArpWaitTxPacket = NULL;
  272. NetArpWaitPacketIP = 0;
  273. NetArpWaitReplyIP = 0;
  274. NetArpWaitTxPacket = NULL;
  275. NetTxPacket = NULL;
  276. NetTryCount = 1;
  277. if (!NetTxPacket) {
  278. int i;
  279. /*
  280. * Setup packet buffers, aligned correctly.
  281. */
  282. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  283. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  284. for (i = 0; i < PKTBUFSRX; i++) {
  285. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  286. }
  287. }
  288. if (!NetArpWaitTxPacket) {
  289. NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
  290. NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
  291. NetArpWaitTxPacketSize = 0;
  292. }
  293. eth_halt();
  294. #ifdef CONFIG_NET_MULTI
  295. eth_set_current();
  296. #endif
  297. if (eth_init(bd) < 0) {
  298. eth_halt();
  299. return(-1);
  300. }
  301. restart:
  302. #ifdef CONFIG_NET_MULTI
  303. memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
  304. #else
  305. eth_getenv_enetaddr("ethaddr", NetOurEther);
  306. #endif
  307. NetState = NETLOOP_CONTINUE;
  308. /*
  309. * Start the ball rolling with the given start function. From
  310. * here on, this code is a state machine driven by received
  311. * packets and timer events.
  312. */
  313. NetInitLoop(protocol);
  314. switch (net_check_prereq (protocol)) {
  315. case 1:
  316. /* network not configured */
  317. eth_halt();
  318. return (-1);
  319. #ifdef CONFIG_NET_MULTI
  320. case 2:
  321. /* network device not configured */
  322. break;
  323. #endif /* CONFIG_NET_MULTI */
  324. case 0:
  325. #ifdef CONFIG_NET_MULTI
  326. NetDevExists = 1;
  327. #endif
  328. switch (protocol) {
  329. case TFTP:
  330. /* always use ARP to get server ethernet address */
  331. TftpStart();
  332. break;
  333. #if defined(CONFIG_CMD_DHCP)
  334. case DHCP:
  335. BootpTry = 0;
  336. NetOurIP = 0;
  337. DhcpRequest(); /* Basically same as BOOTP */
  338. break;
  339. #endif
  340. case BOOTP:
  341. BootpTry = 0;
  342. NetOurIP = 0;
  343. BootpRequest ();
  344. break;
  345. case RARP:
  346. RarpTry = 0;
  347. NetOurIP = 0;
  348. RarpRequest ();
  349. break;
  350. #if defined(CONFIG_CMD_PING)
  351. case PING:
  352. PingStart();
  353. break;
  354. #endif
  355. #if defined(CONFIG_CMD_NFS)
  356. case NFS:
  357. NfsStart();
  358. break;
  359. #endif
  360. #if defined(CONFIG_CMD_CDP)
  361. case CDP:
  362. CDPStart();
  363. break;
  364. #endif
  365. #ifdef CONFIG_NETCONSOLE
  366. case NETCONS:
  367. NcStart();
  368. break;
  369. #endif
  370. #if defined(CONFIG_CMD_SNTP)
  371. case SNTP:
  372. SntpStart();
  373. break;
  374. #endif
  375. #if defined(CONFIG_CMD_DNS)
  376. case DNS:
  377. DnsStart();
  378. break;
  379. #endif
  380. default:
  381. break;
  382. }
  383. NetBootFileXferSize = 0;
  384. break;
  385. }
  386. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  387. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  388. /*
  389. * Echo the inverted link state to the fault LED.
  390. */
  391. if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
  392. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  393. } else {
  394. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  395. }
  396. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  397. #endif /* CONFIG_MII, ... */
  398. /*
  399. * Main packet reception loop. Loop receiving packets until
  400. * someone sets `NetState' to a state that terminates.
  401. */
  402. for (;;) {
  403. WATCHDOG_RESET();
  404. #ifdef CONFIG_SHOW_ACTIVITY
  405. {
  406. extern void show_activity(int arg);
  407. show_activity(1);
  408. }
  409. #endif
  410. /*
  411. * Check the ethernet for a new packet. The ethernet
  412. * receive routine will process it.
  413. */
  414. eth_rx();
  415. /*
  416. * Abort if ctrl-c was pressed.
  417. */
  418. if (ctrlc()) {
  419. eth_halt();
  420. puts ("\nAbort\n");
  421. return (-1);
  422. }
  423. ArpTimeoutCheck();
  424. /*
  425. * Check for a timeout, and run the timeout handler
  426. * if we have one.
  427. */
  428. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  429. thand_f *x;
  430. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  431. # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  432. defined(CONFIG_STATUS_LED) && \
  433. defined(STATUS_LED_RED)
  434. /*
  435. * Echo the inverted link state to the fault LED.
  436. */
  437. if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
  438. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  439. } else {
  440. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  441. }
  442. # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  443. #endif /* CONFIG_MII, ... */
  444. x = timeHandler;
  445. timeHandler = (thand_f *)0;
  446. (*x)();
  447. }
  448. switch (NetState) {
  449. case NETLOOP_RESTART:
  450. #ifdef CONFIG_NET_MULTI
  451. NetRestarted = 1;
  452. #endif
  453. goto restart;
  454. case NETLOOP_SUCCESS:
  455. if (NetBootFileXferSize > 0) {
  456. char buf[20];
  457. printf("Bytes transferred = %ld (%lx hex)\n",
  458. NetBootFileXferSize,
  459. NetBootFileXferSize);
  460. sprintf(buf, "%lX", NetBootFileXferSize);
  461. setenv("filesize", buf);
  462. sprintf(buf, "%lX", (unsigned long)load_addr);
  463. setenv("fileaddr", buf);
  464. }
  465. eth_halt();
  466. return NetBootFileXferSize;
  467. case NETLOOP_FAIL:
  468. return (-1);
  469. }
  470. }
  471. }
  472. /**********************************************************************/
  473. static void
  474. startAgainTimeout(void)
  475. {
  476. NetState = NETLOOP_RESTART;
  477. }
  478. static void
  479. startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
  480. {
  481. /* Totally ignore the packet */
  482. }
  483. void NetStartAgain (void)
  484. {
  485. char *nretry;
  486. int retry_forever = 0;
  487. unsigned long retrycnt = 0;
  488. nretry = getenv("netretry");
  489. if (nretry) {
  490. if (!strcmp(nretry, "yes"))
  491. retry_forever = 1;
  492. else if (!strcmp(nretry, "no"))
  493. retrycnt = 0;
  494. else if (!strcmp(nretry, "once"))
  495. retrycnt = 1;
  496. else
  497. retrycnt = simple_strtoul(nretry, NULL, 0);
  498. } else
  499. retry_forever = 1;
  500. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  501. eth_halt();
  502. NetState = NETLOOP_FAIL;
  503. return;
  504. }
  505. NetTryCount++;
  506. #ifndef CONFIG_NET_MULTI
  507. NetSetTimeout (10000UL, startAgainTimeout);
  508. NetSetHandler (startAgainHandler);
  509. #else /* !CONFIG_NET_MULTI*/
  510. eth_halt ();
  511. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  512. eth_try_another (!NetRestarted);
  513. #endif
  514. eth_init (gd->bd);
  515. if (NetRestartWrap) {
  516. NetRestartWrap = 0;
  517. if (NetDevExists) {
  518. NetSetTimeout (10000UL, startAgainTimeout);
  519. NetSetHandler (startAgainHandler);
  520. } else {
  521. NetState = NETLOOP_FAIL;
  522. }
  523. } else {
  524. NetState = NETLOOP_RESTART;
  525. }
  526. #endif /* CONFIG_NET_MULTI */
  527. }
  528. /**********************************************************************/
  529. /*
  530. * Miscelaneous bits.
  531. */
  532. void
  533. NetSetHandler(rxhand_f * f)
  534. {
  535. packetHandler = f;
  536. }
  537. void
  538. NetSetTimeout(ulong iv, thand_f * f)
  539. {
  540. if (iv == 0) {
  541. timeHandler = (thand_f *)0;
  542. } else {
  543. timeHandler = f;
  544. timeStart = get_timer(0);
  545. timeDelta = iv;
  546. }
  547. }
  548. void
  549. NetSendPacket(volatile uchar * pkt, int len)
  550. {
  551. (void) eth_send(pkt, len);
  552. }
  553. int
  554. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  555. {
  556. uchar *pkt;
  557. /* convert to new style broadcast */
  558. if (dest == 0)
  559. dest = 0xFFFFFFFF;
  560. /* if broadcast, make the ether address a broadcast and don't do ARP */
  561. if (dest == 0xFFFFFFFF)
  562. ether = NetBcastAddr;
  563. /* if MAC address was not discovered yet, save the packet and do an ARP request */
  564. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  565. debug("sending ARP for %08lx\n", dest);
  566. NetArpWaitPacketIP = dest;
  567. NetArpWaitPacketMAC = ether;
  568. pkt = NetArpWaitTxPacket;
  569. pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
  570. NetSetIP (pkt, dest, dport, sport, len);
  571. memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
  572. /* size of the waiting packet */
  573. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
  574. /* and do the ARP request */
  575. NetArpWaitTry = 1;
  576. NetArpWaitTimerStart = get_timer(0);
  577. ArpRequest();
  578. return 1; /* waiting */
  579. }
  580. debug("sending UDP to %08lx/%pM\n", dest, ether);
  581. pkt = (uchar *)NetTxPacket;
  582. pkt += NetSetEther (pkt, ether, PROT_IP);
  583. NetSetIP (pkt, dest, dport, sport, len);
  584. (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
  585. return 0; /* transmitted */
  586. }
  587. #if defined(CONFIG_CMD_PING)
  588. static ushort PingSeqNo;
  589. int PingSend(void)
  590. {
  591. static uchar mac[6];
  592. volatile IP_t *ip;
  593. volatile ushort *s;
  594. uchar *pkt;
  595. /* XXX always send arp request */
  596. memcpy(mac, NetEtherNullAddr, 6);
  597. debug("sending ARP for %08lx\n", NetPingIP);
  598. NetArpWaitPacketIP = NetPingIP;
  599. NetArpWaitPacketMAC = mac;
  600. pkt = NetArpWaitTxPacket;
  601. pkt += NetSetEther(pkt, mac, PROT_IP);
  602. ip = (volatile IP_t *)pkt;
  603. /*
  604. * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
  605. */
  606. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  607. ip->ip_tos = 0;
  608. ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
  609. ip->ip_id = htons(NetIPID++);
  610. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  611. ip->ip_ttl = 255;
  612. ip->ip_p = 0x01; /* ICMP */
  613. ip->ip_sum = 0;
  614. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  615. NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
  616. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  617. s = &ip->udp_src; /* XXX ICMP starts here */
  618. s[0] = htons(0x0800); /* echo-request, code */
  619. s[1] = 0; /* checksum */
  620. s[2] = 0; /* identifier */
  621. s[3] = htons(PingSeqNo++); /* sequence number */
  622. s[1] = ~NetCksum((uchar *)s, 8/2);
  623. /* size of the waiting packet */
  624. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
  625. /* and do the ARP request */
  626. NetArpWaitTry = 1;
  627. NetArpWaitTimerStart = get_timer(0);
  628. ArpRequest();
  629. return 1; /* waiting */
  630. }
  631. static void
  632. PingTimeout (void)
  633. {
  634. eth_halt();
  635. NetState = NETLOOP_FAIL; /* we did not get the reply */
  636. }
  637. static void
  638. PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  639. {
  640. IPaddr_t tmp;
  641. volatile IP_t *ip = (volatile IP_t *)pkt;
  642. tmp = NetReadIP((void *)&ip->ip_src);
  643. if (tmp != NetPingIP)
  644. return;
  645. NetState = NETLOOP_SUCCESS;
  646. }
  647. static void PingStart(void)
  648. {
  649. #if defined(CONFIG_NET_MULTI)
  650. printf ("Using %s device\n", eth_get_name());
  651. #endif /* CONFIG_NET_MULTI */
  652. NetSetTimeout (10000UL, PingTimeout);
  653. NetSetHandler (PingHandler);
  654. PingSend();
  655. }
  656. #endif
  657. #if defined(CONFIG_CMD_CDP)
  658. #define CDP_DEVICE_ID_TLV 0x0001
  659. #define CDP_ADDRESS_TLV 0x0002
  660. #define CDP_PORT_ID_TLV 0x0003
  661. #define CDP_CAPABILITIES_TLV 0x0004
  662. #define CDP_VERSION_TLV 0x0005
  663. #define CDP_PLATFORM_TLV 0x0006
  664. #define CDP_NATIVE_VLAN_TLV 0x000a
  665. #define CDP_APPLIANCE_VLAN_TLV 0x000e
  666. #define CDP_TRIGGER_TLV 0x000f
  667. #define CDP_POWER_CONSUMPTION_TLV 0x0010
  668. #define CDP_SYSNAME_TLV 0x0014
  669. #define CDP_SYSOBJECT_TLV 0x0015
  670. #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
  671. #define CDP_TIMEOUT 250UL /* one packet every 250ms */
  672. static int CDPSeq;
  673. static int CDPOK;
  674. ushort CDPNativeVLAN;
  675. ushort CDPApplianceVLAN;
  676. static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
  677. static ushort CDP_compute_csum(const uchar *buff, ushort len)
  678. {
  679. ushort csum;
  680. int odd;
  681. ulong result = 0;
  682. ushort leftover;
  683. ushort *p;
  684. if (len > 0) {
  685. odd = 1 & (ulong)buff;
  686. if (odd) {
  687. result = *buff << 8;
  688. len--;
  689. buff++;
  690. }
  691. while (len > 1) {
  692. p = (ushort *)buff;
  693. result += *p++;
  694. buff = (uchar *)p;
  695. if (result & 0x80000000)
  696. result = (result & 0xFFFF) + (result >> 16);
  697. len -= 2;
  698. }
  699. if (len) {
  700. leftover = (signed short)(*(const signed char *)buff);
  701. /* CISCO SUCKS big time! (and blows too):
  702. * CDP uses the IP checksum algorithm with a twist;
  703. * for the last byte it *sign* extends and sums.
  704. */
  705. result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
  706. }
  707. while (result >> 16)
  708. result = (result & 0xFFFF) + (result >> 16);
  709. if (odd)
  710. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  711. }
  712. /* add up 16-bit and 17-bit words for 17+c bits */
  713. result = (result & 0xffff) + (result >> 16);
  714. /* add up 16-bit and 2-bit for 16+c bit */
  715. result = (result & 0xffff) + (result >> 16);
  716. /* add up carry.. */
  717. result = (result & 0xffff) + (result >> 16);
  718. /* negate */
  719. csum = ~(ushort)result;
  720. /* run time endian detection */
  721. if (csum != htons(csum)) /* little endian */
  722. csum = htons(csum);
  723. return csum;
  724. }
  725. int CDPSendTrigger(void)
  726. {
  727. volatile uchar *pkt;
  728. volatile ushort *s;
  729. volatile ushort *cp;
  730. Ethernet_t *et;
  731. int len;
  732. ushort chksum;
  733. #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
  734. defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
  735. char buf[32];
  736. #endif
  737. pkt = NetTxPacket;
  738. et = (Ethernet_t *)pkt;
  739. /* NOTE: trigger sent not on any VLAN */
  740. /* form ethernet header */
  741. memcpy(et->et_dest, NetCDPAddr, 6);
  742. memcpy(et->et_src, NetOurEther, 6);
  743. pkt += ETHER_HDR_SIZE;
  744. /* SNAP header */
  745. memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
  746. pkt += sizeof(CDP_SNAP_hdr);
  747. /* CDP header */
  748. *pkt++ = 0x02; /* CDP version 2 */
  749. *pkt++ = 180; /* TTL */
  750. s = (volatile ushort *)pkt;
  751. cp = s;
  752. *s++ = htons(0); /* checksum (0 for later calculation) */
  753. /* CDP fields */
  754. #ifdef CONFIG_CDP_DEVICE_ID
  755. *s++ = htons(CDP_DEVICE_ID_TLV);
  756. *s++ = htons(CONFIG_CDP_DEVICE_ID);
  757. sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
  758. memcpy((uchar *)s, buf, 16);
  759. s += 16 / 2;
  760. #endif
  761. #ifdef CONFIG_CDP_PORT_ID
  762. *s++ = htons(CDP_PORT_ID_TLV);
  763. memset(buf, 0, sizeof(buf));
  764. sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
  765. len = strlen(buf);
  766. if (len & 1) /* make it even */
  767. len++;
  768. *s++ = htons(len + 4);
  769. memcpy((uchar *)s, buf, len);
  770. s += len / 2;
  771. #endif
  772. #ifdef CONFIG_CDP_CAPABILITIES
  773. *s++ = htons(CDP_CAPABILITIES_TLV);
  774. *s++ = htons(8);
  775. *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
  776. s += 2;
  777. #endif
  778. #ifdef CONFIG_CDP_VERSION
  779. *s++ = htons(CDP_VERSION_TLV);
  780. memset(buf, 0, sizeof(buf));
  781. strcpy(buf, CONFIG_CDP_VERSION);
  782. len = strlen(buf);
  783. if (len & 1) /* make it even */
  784. len++;
  785. *s++ = htons(len + 4);
  786. memcpy((uchar *)s, buf, len);
  787. s += len / 2;
  788. #endif
  789. #ifdef CONFIG_CDP_PLATFORM
  790. *s++ = htons(CDP_PLATFORM_TLV);
  791. memset(buf, 0, sizeof(buf));
  792. strcpy(buf, CONFIG_CDP_PLATFORM);
  793. len = strlen(buf);
  794. if (len & 1) /* make it even */
  795. len++;
  796. *s++ = htons(len + 4);
  797. memcpy((uchar *)s, buf, len);
  798. s += len / 2;
  799. #endif
  800. #ifdef CONFIG_CDP_TRIGGER
  801. *s++ = htons(CDP_TRIGGER_TLV);
  802. *s++ = htons(8);
  803. *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
  804. s += 2;
  805. #endif
  806. #ifdef CONFIG_CDP_POWER_CONSUMPTION
  807. *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
  808. *s++ = htons(6);
  809. *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
  810. #endif
  811. /* length of ethernet packet */
  812. len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
  813. et->et_protlen = htons(len);
  814. len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
  815. chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
  816. if (chksum == 0)
  817. chksum = 0xFFFF;
  818. *cp = htons(chksum);
  819. (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
  820. return 0;
  821. }
  822. static void
  823. CDPTimeout (void)
  824. {
  825. CDPSeq++;
  826. if (CDPSeq < 3) {
  827. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  828. CDPSendTrigger();
  829. return;
  830. }
  831. /* if not OK try again */
  832. if (!CDPOK)
  833. NetStartAgain();
  834. else
  835. NetState = NETLOOP_SUCCESS;
  836. }
  837. static void
  838. CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  839. {
  840. /* nothing */
  841. }
  842. static void
  843. CDPHandler(const uchar * pkt, unsigned len)
  844. {
  845. const uchar *t;
  846. const ushort *ss;
  847. ushort type, tlen;
  848. uchar applid;
  849. ushort vlan, nvlan;
  850. /* minimum size? */
  851. if (len < sizeof(CDP_SNAP_hdr) + 4)
  852. goto pkt_short;
  853. /* check for valid CDP SNAP header */
  854. if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
  855. return;
  856. pkt += sizeof(CDP_SNAP_hdr);
  857. len -= sizeof(CDP_SNAP_hdr);
  858. /* Version of CDP protocol must be >= 2 and TTL != 0 */
  859. if (pkt[0] < 0x02 || pkt[1] == 0)
  860. return;
  861. /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
  862. if (pkt[0] != 0x02)
  863. printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
  864. pkt[0] & 0xff);
  865. if (CDP_compute_csum(pkt, len) != 0)
  866. return;
  867. pkt += 4;
  868. len -= 4;
  869. vlan = htons(-1);
  870. nvlan = htons(-1);
  871. while (len > 0) {
  872. if (len < 4)
  873. goto pkt_short;
  874. ss = (const ushort *)pkt;
  875. type = ntohs(ss[0]);
  876. tlen = ntohs(ss[1]);
  877. if (tlen > len) {
  878. goto pkt_short;
  879. }
  880. pkt += tlen;
  881. len -= tlen;
  882. ss += 2; /* point ss to the data of the TLV */
  883. tlen -= 4;
  884. switch (type) {
  885. case CDP_DEVICE_ID_TLV:
  886. break;
  887. case CDP_ADDRESS_TLV:
  888. break;
  889. case CDP_PORT_ID_TLV:
  890. break;
  891. case CDP_CAPABILITIES_TLV:
  892. break;
  893. case CDP_VERSION_TLV:
  894. break;
  895. case CDP_PLATFORM_TLV:
  896. break;
  897. case CDP_NATIVE_VLAN_TLV:
  898. nvlan = *ss;
  899. break;
  900. case CDP_APPLIANCE_VLAN_TLV:
  901. t = (const uchar *)ss;
  902. while (tlen > 0) {
  903. if (tlen < 3)
  904. goto pkt_short;
  905. applid = t[0];
  906. ss = (const ushort *)(t + 1);
  907. #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
  908. if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
  909. vlan = *ss;
  910. #else
  911. vlan = ntohs(*ss); /* XXX will this work; dunno */
  912. #endif
  913. t += 3; tlen -= 3;
  914. }
  915. break;
  916. case CDP_TRIGGER_TLV:
  917. break;
  918. case CDP_POWER_CONSUMPTION_TLV:
  919. break;
  920. case CDP_SYSNAME_TLV:
  921. break;
  922. case CDP_SYSOBJECT_TLV:
  923. break;
  924. case CDP_MANAGEMENT_ADDRESS_TLV:
  925. break;
  926. }
  927. }
  928. CDPApplianceVLAN = vlan;
  929. CDPNativeVLAN = nvlan;
  930. CDPOK = 1;
  931. return;
  932. pkt_short:
  933. printf("** CDP packet is too short\n");
  934. return;
  935. }
  936. static void CDPStart(void)
  937. {
  938. #if defined(CONFIG_NET_MULTI)
  939. printf ("Using %s device\n", eth_get_name());
  940. #endif
  941. CDPSeq = 0;
  942. CDPOK = 0;
  943. CDPNativeVLAN = htons(-1);
  944. CDPApplianceVLAN = htons(-1);
  945. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  946. NetSetHandler (CDPDummyHandler);
  947. CDPSendTrigger();
  948. }
  949. #endif
  950. #ifdef CONFIG_IP_DEFRAG
  951. /*
  952. * This function collects fragments in a single packet, according
  953. * to the algorithm in RFC815. It returns NULL or the pointer to
  954. * a complete packet, in static storage
  955. */
  956. #ifndef CONFIG_NET_MAXDEFRAG
  957. #define CONFIG_NET_MAXDEFRAG 16384
  958. #endif
  959. /*
  960. * MAXDEFRAG, above, is chosen in the config file and is real data
  961. * so we need to add the NFS overhead, which is more than TFTP.
  962. * To use sizeof in the internal unnamed structures, we need a real
  963. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  964. * The compiler doesn't complain nor allocates the actual structure
  965. */
  966. static struct rpc_t rpc_specimen;
  967. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  968. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
  969. /*
  970. * this is the packet being assembled, either data or frag control.
  971. * Fragments go by 8 bytes, so this union must be 8 bytes long
  972. */
  973. struct hole {
  974. /* first_byte is address of this structure */
  975. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  976. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  977. u16 prev_hole; /* index of prev, 0 == none */
  978. u16 unused;
  979. };
  980. static IP_t *__NetDefragment(IP_t *ip, int *lenp)
  981. {
  982. static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
  983. static u16 first_hole, total_len;
  984. struct hole *payload, *thisfrag, *h, *newh;
  985. IP_t *localip = (IP_t *)pkt_buff;
  986. uchar *indata = (uchar *)ip;
  987. int offset8, start, len, done = 0;
  988. u16 ip_off = ntohs(ip->ip_off);
  989. /* payload starts after IP header, this fragment is in there */
  990. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
  991. offset8 = (ip_off & IP_OFFS);
  992. thisfrag = payload + offset8;
  993. start = offset8 * 8;
  994. len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
  995. if (start + len > IP_MAXUDP) /* fragment extends too far */
  996. return NULL;
  997. if (!total_len || localip->ip_id != ip->ip_id) {
  998. /* new (or different) packet, reset structs */
  999. total_len = 0xffff;
  1000. payload[0].last_byte = ~0;
  1001. payload[0].next_hole = 0;
  1002. payload[0].prev_hole = 0;
  1003. first_hole = 0;
  1004. /* any IP header will work, copy the first we received */
  1005. memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
  1006. }
  1007. /*
  1008. * What follows is the reassembly algorithm. We use the payload
  1009. * array as a linked list of hole descriptors, as each hole starts
  1010. * at a multiple of 8 bytes. However, last byte can be whatever value,
  1011. * so it is represented as byte count, not as 8-byte blocks.
  1012. */
  1013. h = payload + first_hole;
  1014. while (h->last_byte < start) {
  1015. if (!h->next_hole) {
  1016. /* no hole that far away */
  1017. return NULL;
  1018. }
  1019. h = payload + h->next_hole;
  1020. }
  1021. if (offset8 + (len / 8) <= h - payload) {
  1022. /* no overlap with holes (dup fragment?) */
  1023. return NULL;
  1024. }
  1025. if (!(ip_off & IP_FLAGS_MFRAG)) {
  1026. /* no more fragmentss: truncate this (last) hole */
  1027. total_len = start + len;
  1028. h->last_byte = start + len;
  1029. }
  1030. /*
  1031. * There is some overlap: fix the hole list. This code doesn't
  1032. * deal with a fragment that overlaps with two different holes
  1033. * (thus being a superset of a previously-received fragment).
  1034. */
  1035. if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
  1036. /* complete overlap with hole: remove hole */
  1037. if (!h->prev_hole && !h->next_hole) {
  1038. /* last remaining hole */
  1039. done = 1;
  1040. } else if (!h->prev_hole) {
  1041. /* first hole */
  1042. first_hole = h->next_hole;
  1043. payload[h->next_hole].prev_hole = 0;
  1044. } else if (!h->next_hole) {
  1045. /* last hole */
  1046. payload[h->prev_hole].next_hole = 0;
  1047. } else {
  1048. /* in the middle of the list */
  1049. payload[h->next_hole].prev_hole = h->prev_hole;
  1050. payload[h->prev_hole].next_hole = h->next_hole;
  1051. }
  1052. } else if (h->last_byte <= start + len) {
  1053. /* overlaps with final part of the hole: shorten this hole */
  1054. h->last_byte = start;
  1055. } else if (h >= thisfrag) {
  1056. /* overlaps with initial part of the hole: move this hole */
  1057. newh = thisfrag + (len / 8);
  1058. *newh = *h;
  1059. h = newh;
  1060. if (h->next_hole)
  1061. payload[h->next_hole].prev_hole = (h - payload);
  1062. if (h->prev_hole)
  1063. payload[h->prev_hole].next_hole = (h - payload);
  1064. else
  1065. first_hole = (h - payload);
  1066. } else {
  1067. /* fragment sits in the middle: split the hole */
  1068. newh = thisfrag + (len / 8);
  1069. *newh = *h;
  1070. h->last_byte = start;
  1071. h->next_hole = (newh - payload);
  1072. newh->prev_hole = (h - payload);
  1073. if (newh->next_hole)
  1074. payload[newh->next_hole].prev_hole = (newh - payload);
  1075. }
  1076. /* finally copy this fragment and possibly return whole packet */
  1077. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
  1078. if (!done)
  1079. return NULL;
  1080. localip->ip_len = htons(total_len);
  1081. *lenp = total_len + IP_HDR_SIZE_NO_UDP;
  1082. return localip;
  1083. }
  1084. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  1085. {
  1086. u16 ip_off = ntohs(ip->ip_off);
  1087. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  1088. return ip; /* not a fragment */
  1089. return __NetDefragment(ip, lenp);
  1090. }
  1091. #else /* !CONFIG_IP_DEFRAG */
  1092. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  1093. {
  1094. u16 ip_off = ntohs(ip->ip_off);
  1095. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  1096. return ip; /* not a fragment */
  1097. return NULL;
  1098. }
  1099. #endif
  1100. void
  1101. NetReceive(volatile uchar * inpkt, int len)
  1102. {
  1103. Ethernet_t *et;
  1104. IP_t *ip;
  1105. ARP_t *arp;
  1106. IPaddr_t tmp;
  1107. int x;
  1108. uchar *pkt;
  1109. #if defined(CONFIG_CMD_CDP)
  1110. int iscdp;
  1111. #endif
  1112. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  1113. debug("packet received\n");
  1114. NetRxPacket = inpkt;
  1115. NetRxPacketLen = len;
  1116. et = (Ethernet_t *)inpkt;
  1117. /* too small packet? */
  1118. if (len < ETHER_HDR_SIZE)
  1119. return;
  1120. #ifdef CONFIG_API
  1121. if (push_packet) {
  1122. (*push_packet)(inpkt, len);
  1123. return;
  1124. }
  1125. #endif
  1126. #if defined(CONFIG_CMD_CDP)
  1127. /* keep track if packet is CDP */
  1128. iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
  1129. #endif
  1130. myvlanid = ntohs(NetOurVLAN);
  1131. if (myvlanid == (ushort)-1)
  1132. myvlanid = VLAN_NONE;
  1133. mynvlanid = ntohs(NetOurNativeVLAN);
  1134. if (mynvlanid == (ushort)-1)
  1135. mynvlanid = VLAN_NONE;
  1136. x = ntohs(et->et_protlen);
  1137. debug("packet received\n");
  1138. if (x < 1514) {
  1139. /*
  1140. * Got a 802 packet. Check the other protocol field.
  1141. */
  1142. x = ntohs(et->et_prot);
  1143. ip = (IP_t *)(inpkt + E802_HDR_SIZE);
  1144. len -= E802_HDR_SIZE;
  1145. } else if (x != PROT_VLAN) { /* normal packet */
  1146. ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
  1147. len -= ETHER_HDR_SIZE;
  1148. } else { /* VLAN packet */
  1149. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
  1150. debug("VLAN packet received\n");
  1151. /* too small packet? */
  1152. if (len < VLAN_ETHER_HDR_SIZE)
  1153. return;
  1154. /* if no VLAN active */
  1155. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  1156. #if defined(CONFIG_CMD_CDP)
  1157. && iscdp == 0
  1158. #endif
  1159. )
  1160. return;
  1161. cti = ntohs(vet->vet_tag);
  1162. vlanid = cti & VLAN_IDMASK;
  1163. x = ntohs(vet->vet_type);
  1164. ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
  1165. len -= VLAN_ETHER_HDR_SIZE;
  1166. }
  1167. debug("Receive from protocol 0x%x\n", x);
  1168. #if defined(CONFIG_CMD_CDP)
  1169. if (iscdp) {
  1170. CDPHandler((uchar *)ip, len);
  1171. return;
  1172. }
  1173. #endif
  1174. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  1175. if (vlanid == VLAN_NONE)
  1176. vlanid = (mynvlanid & VLAN_IDMASK);
  1177. /* not matched? */
  1178. if (vlanid != (myvlanid & VLAN_IDMASK))
  1179. return;
  1180. }
  1181. switch (x) {
  1182. case PROT_ARP:
  1183. /*
  1184. * We have to deal with two types of ARP packets:
  1185. * - REQUEST packets will be answered by sending our
  1186. * IP address - if we know it.
  1187. * - REPLY packates are expected only after we asked
  1188. * for the TFTP server's or the gateway's ethernet
  1189. * address; so if we receive such a packet, we set
  1190. * the server ethernet address
  1191. */
  1192. debug("Got ARP\n");
  1193. arp = (ARP_t *)ip;
  1194. if (len < ARP_HDR_SIZE) {
  1195. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1196. return;
  1197. }
  1198. if (ntohs(arp->ar_hrd) != ARP_ETHER) {
  1199. return;
  1200. }
  1201. if (ntohs(arp->ar_pro) != PROT_IP) {
  1202. return;
  1203. }
  1204. if (arp->ar_hln != 6) {
  1205. return;
  1206. }
  1207. if (arp->ar_pln != 4) {
  1208. return;
  1209. }
  1210. if (NetOurIP == 0) {
  1211. return;
  1212. }
  1213. if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
  1214. return;
  1215. }
  1216. switch (ntohs(arp->ar_op)) {
  1217. case ARPOP_REQUEST: /* reply with our IP address */
  1218. debug("Got ARP REQUEST, return our IP\n");
  1219. pkt = (uchar *)et;
  1220. pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
  1221. arp->ar_op = htons(ARPOP_REPLY);
  1222. memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
  1223. NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
  1224. memcpy (&arp->ar_data[ 0], NetOurEther, 6);
  1225. NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
  1226. (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
  1227. return;
  1228. case ARPOP_REPLY: /* arp reply */
  1229. /* are we waiting for a reply */
  1230. if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
  1231. break;
  1232. #ifdef CONFIG_KEEP_SERVERADDR
  1233. if (NetServerIP == NetArpWaitPacketIP) {
  1234. char buf[20];
  1235. sprintf(buf, "%pM", arp->ar_data);
  1236. setenv("serveraddr", buf);
  1237. }
  1238. #endif
  1239. debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
  1240. arp->ar_data);
  1241. tmp = NetReadIP(&arp->ar_data[6]);
  1242. /* matched waiting packet's address */
  1243. if (tmp == NetArpWaitReplyIP) {
  1244. debug("Got it\n");
  1245. /* save address for later use */
  1246. memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
  1247. #ifdef CONFIG_NETCONSOLE
  1248. (*packetHandler)(0,0,0,0);
  1249. #endif
  1250. /* modify header, and transmit it */
  1251. memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
  1252. (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
  1253. /* no arp request pending now */
  1254. NetArpWaitPacketIP = 0;
  1255. NetArpWaitTxPacketSize = 0;
  1256. NetArpWaitPacketMAC = NULL;
  1257. }
  1258. return;
  1259. default:
  1260. debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
  1261. return;
  1262. }
  1263. break;
  1264. case PROT_RARP:
  1265. debug("Got RARP\n");
  1266. arp = (ARP_t *)ip;
  1267. if (len < ARP_HDR_SIZE) {
  1268. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1269. return;
  1270. }
  1271. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  1272. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  1273. (ntohs(arp->ar_pro) != PROT_IP) ||
  1274. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  1275. puts ("invalid RARP header\n");
  1276. } else {
  1277. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  1278. if (NetServerIP == 0)
  1279. NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
  1280. memcpy (NetServerEther, &arp->ar_data[ 0], 6);
  1281. (*packetHandler)(0,0,0,0);
  1282. }
  1283. break;
  1284. case PROT_IP:
  1285. debug("Got IP\n");
  1286. /* Before we start poking the header, make sure it is there */
  1287. if (len < IP_HDR_SIZE) {
  1288. debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
  1289. return;
  1290. }
  1291. /* Check the packet length */
  1292. if (len < ntohs(ip->ip_len)) {
  1293. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  1294. return;
  1295. }
  1296. len = ntohs(ip->ip_len);
  1297. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  1298. /* Can't deal with anything except IPv4 */
  1299. if ((ip->ip_hl_v & 0xf0) != 0x40) {
  1300. return;
  1301. }
  1302. /* Can't deal with IP options (headers != 20 bytes) */
  1303. if ((ip->ip_hl_v & 0x0f) > 0x05) {
  1304. return;
  1305. }
  1306. /* Check the Checksum of the header */
  1307. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  1308. puts ("checksum bad\n");
  1309. return;
  1310. }
  1311. /* If it is not for us, ignore it */
  1312. tmp = NetReadIP(&ip->ip_dst);
  1313. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  1314. #ifdef CONFIG_MCAST_TFTP
  1315. if (Mcast_addr != tmp)
  1316. #endif
  1317. return;
  1318. }
  1319. /*
  1320. * The function returns the unchanged packet if it's not
  1321. * a fragment, and either the complete packet or NULL if
  1322. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  1323. */
  1324. if (!(ip = NetDefragment(ip, &len)))
  1325. return;
  1326. /*
  1327. * watch for ICMP host redirects
  1328. *
  1329. * There is no real handler code (yet). We just watch
  1330. * for ICMP host redirect messages. In case anybody
  1331. * sees these messages: please contact me
  1332. * (wd@denx.de), or - even better - send me the
  1333. * necessary fixes :-)
  1334. *
  1335. * Note: in all cases where I have seen this so far
  1336. * it was a problem with the router configuration,
  1337. * for instance when a router was configured in the
  1338. * BOOTP reply, but the TFTP server was on the same
  1339. * subnet. So this is probably a warning that your
  1340. * configuration might be wrong. But I'm not really
  1341. * sure if there aren't any other situations.
  1342. */
  1343. if (ip->ip_p == IPPROTO_ICMP) {
  1344. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  1345. switch (icmph->type) {
  1346. case ICMP_REDIRECT:
  1347. if (icmph->code != ICMP_REDIR_HOST)
  1348. return;
  1349. printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
  1350. return;
  1351. #if defined(CONFIG_CMD_PING)
  1352. case ICMP_ECHO_REPLY:
  1353. /*
  1354. * IP header OK. Pass the packet to the current handler.
  1355. */
  1356. /* XXX point to ip packet */
  1357. (*packetHandler)((uchar *)ip, 0, 0, 0);
  1358. return;
  1359. case ICMP_ECHO_REQUEST:
  1360. debug("Got ICMP ECHO REQUEST, return %d bytes \n",
  1361. ETHER_HDR_SIZE + len);
  1362. memcpy (&et->et_dest[0], &et->et_src[0], 6);
  1363. memcpy (&et->et_src[ 0], NetOurEther, 6);
  1364. ip->ip_sum = 0;
  1365. ip->ip_off = 0;
  1366. NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
  1367. NetCopyIP((void*)&ip->ip_src, &NetOurIP);
  1368. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
  1369. icmph->type = ICMP_ECHO_REPLY;
  1370. icmph->checksum = 0;
  1371. icmph->checksum = ~NetCksum((uchar *)icmph,
  1372. (len - IP_HDR_SIZE_NO_UDP) >> 1);
  1373. (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
  1374. return;
  1375. #endif
  1376. default:
  1377. return;
  1378. }
  1379. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1380. return;
  1381. }
  1382. #ifdef CONFIG_UDP_CHECKSUM
  1383. if (ip->udp_xsum != 0) {
  1384. ulong xsum;
  1385. ushort *sumptr;
  1386. ushort sumlen;
  1387. xsum = ip->ip_p;
  1388. xsum += (ntohs(ip->udp_len));
  1389. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  1390. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  1391. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  1392. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  1393. sumlen = ntohs(ip->udp_len);
  1394. sumptr = (ushort *) &(ip->udp_src);
  1395. while (sumlen > 1) {
  1396. ushort sumdata;
  1397. sumdata = *sumptr++;
  1398. xsum += ntohs(sumdata);
  1399. sumlen -= 2;
  1400. }
  1401. if (sumlen > 0) {
  1402. ushort sumdata;
  1403. sumdata = *(unsigned char *) sumptr;
  1404. sumdata = (sumdata << 8) & 0xff00;
  1405. xsum += sumdata;
  1406. }
  1407. while ((xsum >> 16) != 0) {
  1408. xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
  1409. }
  1410. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1411. printf(" UDP wrong checksum %08lx %08x\n",
  1412. xsum, ntohs(ip->udp_xsum));
  1413. return;
  1414. }
  1415. }
  1416. #endif
  1417. #ifdef CONFIG_NETCONSOLE
  1418. nc_input_packet((uchar *)ip +IP_HDR_SIZE,
  1419. ntohs(ip->udp_dst),
  1420. ntohs(ip->udp_src),
  1421. ntohs(ip->udp_len) - 8);
  1422. #endif
  1423. /*
  1424. * IP header OK. Pass the packet to the current handler.
  1425. */
  1426. (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
  1427. ntohs(ip->udp_dst),
  1428. ntohs(ip->udp_src),
  1429. ntohs(ip->udp_len) - 8);
  1430. break;
  1431. }
  1432. }
  1433. /**********************************************************************/
  1434. static int net_check_prereq (proto_t protocol)
  1435. {
  1436. switch (protocol) {
  1437. /* Fall through */
  1438. #if defined(CONFIG_CMD_PING)
  1439. case PING:
  1440. if (NetPingIP == 0) {
  1441. puts ("*** ERROR: ping address not given\n");
  1442. return (1);
  1443. }
  1444. goto common;
  1445. #endif
  1446. #if defined(CONFIG_CMD_SNTP)
  1447. case SNTP:
  1448. if (NetNtpServerIP == 0) {
  1449. puts ("*** ERROR: NTP server address not given\n");
  1450. return (1);
  1451. }
  1452. goto common;
  1453. #endif
  1454. #if defined(CONFIG_CMD_DNS)
  1455. case DNS:
  1456. if (NetOurDNSIP == 0) {
  1457. puts("*** ERROR: DNS server address not given\n");
  1458. return 1;
  1459. }
  1460. goto common;
  1461. #endif
  1462. #if defined(CONFIG_CMD_NFS)
  1463. case NFS:
  1464. #endif
  1465. case NETCONS:
  1466. case TFTP:
  1467. if (NetServerIP == 0) {
  1468. puts ("*** ERROR: `serverip' not set\n");
  1469. return (1);
  1470. }
  1471. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
  1472. common:
  1473. #endif
  1474. if (NetOurIP == 0) {
  1475. puts ("*** ERROR: `ipaddr' not set\n");
  1476. return (1);
  1477. }
  1478. /* Fall through */
  1479. case DHCP:
  1480. case RARP:
  1481. case BOOTP:
  1482. case CDP:
  1483. if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1484. #ifdef CONFIG_NET_MULTI
  1485. extern int eth_get_dev_index (void);
  1486. int num = eth_get_dev_index ();
  1487. switch (num) {
  1488. case -1:
  1489. puts ("*** ERROR: No ethernet found.\n");
  1490. return (1);
  1491. case 0:
  1492. puts ("*** ERROR: `ethaddr' not set\n");
  1493. break;
  1494. default:
  1495. printf ("*** ERROR: `eth%daddr' not set\n",
  1496. num);
  1497. break;
  1498. }
  1499. NetStartAgain ();
  1500. return (2);
  1501. #else
  1502. puts ("*** ERROR: `ethaddr' not set\n");
  1503. return (1);
  1504. #endif
  1505. }
  1506. /* Fall through */
  1507. default:
  1508. return (0);
  1509. }
  1510. return (0); /* OK */
  1511. }
  1512. /**********************************************************************/
  1513. int
  1514. NetCksumOk(uchar * ptr, int len)
  1515. {
  1516. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1517. }
  1518. unsigned
  1519. NetCksum(uchar * ptr, int len)
  1520. {
  1521. ulong xsum;
  1522. ushort *p = (ushort *)ptr;
  1523. xsum = 0;
  1524. while (len-- > 0)
  1525. xsum += *p++;
  1526. xsum = (xsum & 0xffff) + (xsum >> 16);
  1527. xsum = (xsum & 0xffff) + (xsum >> 16);
  1528. return (xsum & 0xffff);
  1529. }
  1530. int
  1531. NetEthHdrSize(void)
  1532. {
  1533. ushort myvlanid;
  1534. myvlanid = ntohs(NetOurVLAN);
  1535. if (myvlanid == (ushort)-1)
  1536. myvlanid = VLAN_NONE;
  1537. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
  1538. }
  1539. int
  1540. NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
  1541. {
  1542. Ethernet_t *et = (Ethernet_t *)xet;
  1543. ushort myvlanid;
  1544. myvlanid = ntohs(NetOurVLAN);
  1545. if (myvlanid == (ushort)-1)
  1546. myvlanid = VLAN_NONE;
  1547. memcpy (et->et_dest, addr, 6);
  1548. memcpy (et->et_src, NetOurEther, 6);
  1549. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1550. et->et_protlen = htons(prot);
  1551. return ETHER_HDR_SIZE;
  1552. } else {
  1553. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
  1554. vet->vet_vlan_type = htons(PROT_VLAN);
  1555. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1556. vet->vet_type = htons(prot);
  1557. return VLAN_ETHER_HDR_SIZE;
  1558. }
  1559. }
  1560. void
  1561. NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
  1562. {
  1563. IP_t *ip = (IP_t *)xip;
  1564. /*
  1565. * If the data is an odd number of bytes, zero the
  1566. * byte after the last byte so that the checksum
  1567. * will work.
  1568. */
  1569. if (len & 1)
  1570. xip[IP_HDR_SIZE + len] = 0;
  1571. /*
  1572. * Construct an IP and UDP header.
  1573. * (need to set no fragment bit - XXX)
  1574. */
  1575. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  1576. ip->ip_tos = 0;
  1577. ip->ip_len = htons(IP_HDR_SIZE + len);
  1578. ip->ip_id = htons(NetIPID++);
  1579. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1580. ip->ip_ttl = 255;
  1581. ip->ip_p = 17; /* UDP */
  1582. ip->ip_sum = 0;
  1583. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  1584. NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
  1585. ip->udp_src = htons(sport);
  1586. ip->udp_dst = htons(dport);
  1587. ip->udp_len = htons(8 + len);
  1588. ip->udp_xsum = 0;
  1589. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  1590. }
  1591. void copy_filename (char *dst, char *src, int size)
  1592. {
  1593. if (*src && (*src == '"')) {
  1594. ++src;
  1595. --size;
  1596. }
  1597. while ((--size > 0) && *src && (*src != '"')) {
  1598. *dst++ = *src++;
  1599. }
  1600. *dst = '\0';
  1601. }
  1602. #if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
  1603. /*
  1604. * make port a little random, but use something trivial to compute
  1605. */
  1606. unsigned int random_port(void)
  1607. {
  1608. return 1024 + (get_timer(0) % 0x8000);;
  1609. }
  1610. #endif
  1611. void ip_to_string (IPaddr_t x, char *s)
  1612. {
  1613. x = ntohl (x);
  1614. sprintf (s, "%d.%d.%d.%d",
  1615. (int) ((x >> 24) & 0xff),
  1616. (int) ((x >> 16) & 0xff),
  1617. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1618. );
  1619. }
  1620. IPaddr_t string_to_ip(char *s)
  1621. {
  1622. IPaddr_t addr;
  1623. char *e;
  1624. int i;
  1625. if (s == NULL)
  1626. return(0);
  1627. for (addr=0, i=0; i<4; ++i) {
  1628. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  1629. addr <<= 8;
  1630. addr |= (val & 0xFF);
  1631. if (s) {
  1632. s = (*e) ? e+1 : e;
  1633. }
  1634. }
  1635. return (htonl(addr));
  1636. }
  1637. void VLAN_to_string(ushort x, char *s)
  1638. {
  1639. x = ntohs(x);
  1640. if (x == (ushort)-1)
  1641. x = VLAN_NONE;
  1642. if (x == VLAN_NONE)
  1643. strcpy(s, "none");
  1644. else
  1645. sprintf(s, "%d", x & VLAN_IDMASK);
  1646. }
  1647. ushort string_to_VLAN(char *s)
  1648. {
  1649. ushort id;
  1650. if (s == NULL)
  1651. return htons(VLAN_NONE);
  1652. if (*s < '0' || *s > '9')
  1653. id = VLAN_NONE;
  1654. else
  1655. id = (ushort)simple_strtoul(s, NULL, 10);
  1656. return htons(id);
  1657. }
  1658. IPaddr_t getenv_IPaddr (char *var)
  1659. {
  1660. return (string_to_ip(getenv(var)));
  1661. }
  1662. ushort getenv_VLAN(char *var)
  1663. {
  1664. return (string_to_VLAN(getenv(var)));
  1665. }