net.c 43 KB

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