net.c 44 KB

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