net.c 44 KB

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