net.c 30 KB

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