net.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 <command.h>
  76. #include <net.h>
  77. #if defined(CONFIG_STATUS_LED)
  78. #include <miiphy.h>
  79. #include <status_led.h>
  80. #endif
  81. #include <watchdog.h>
  82. #include <linux/compiler.h>
  83. #include "arp.h"
  84. #include "bootp.h"
  85. #include "cdp.h"
  86. #if defined(CONFIG_CMD_DNS)
  87. #include "dns.h"
  88. #endif
  89. #include "nfs.h"
  90. #include "ping.h"
  91. #include "rarp.h"
  92. #if defined(CONFIG_CMD_SNTP)
  93. #include "sntp.h"
  94. #endif
  95. #include "tftp.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 dst_ip;
  753. IPaddr_t src_ip;
  754. int eth_proto;
  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. eth_proto = ntohs(et->et_protlen);
  783. debug("packet received\n");
  784. if (eth_proto < 1514) {
  785. struct e802_hdr *et802 = (struct e802_hdr *)et;
  786. /*
  787. * Got a 802.2 packet. Check the other protocol field.
  788. * XXX VLAN over 802.2+SNAP not implemented!
  789. */
  790. eth_proto = ntohs(et802->et_prot);
  791. ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
  792. len -= E802_HDR_SIZE;
  793. } else if (eth_proto != PROT_VLAN) { /* normal packet */
  794. ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
  795. len -= ETHER_HDR_SIZE;
  796. } else { /* VLAN packet */
  797. struct vlan_ethernet_hdr *vet =
  798. (struct vlan_ethernet_hdr *)et;
  799. debug("VLAN packet received\n");
  800. /* too small packet? */
  801. if (len < VLAN_ETHER_HDR_SIZE)
  802. return;
  803. /* if no VLAN active */
  804. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  805. #if defined(CONFIG_CMD_CDP)
  806. && iscdp == 0
  807. #endif
  808. )
  809. return;
  810. cti = ntohs(vet->vet_tag);
  811. vlanid = cti & VLAN_IDMASK;
  812. eth_proto = ntohs(vet->vet_type);
  813. ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
  814. len -= VLAN_ETHER_HDR_SIZE;
  815. }
  816. debug("Receive from protocol 0x%x\n", eth_proto);
  817. #if defined(CONFIG_CMD_CDP)
  818. if (iscdp) {
  819. cdp_receive((uchar *)ip, len);
  820. return;
  821. }
  822. #endif
  823. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  824. if (vlanid == VLAN_NONE)
  825. vlanid = (mynvlanid & VLAN_IDMASK);
  826. /* not matched? */
  827. if (vlanid != (myvlanid & VLAN_IDMASK))
  828. return;
  829. }
  830. switch (eth_proto) {
  831. case PROT_ARP:
  832. ArpReceive(et, ip, len);
  833. break;
  834. #ifdef CONFIG_CMD_RARP
  835. case PROT_RARP:
  836. rarp_receive(ip, len);
  837. break;
  838. #endif
  839. case PROT_IP:
  840. debug("Got IP\n");
  841. /* Before we start poking the header, make sure it is there */
  842. if (len < IP_UDP_HDR_SIZE) {
  843. debug("len bad %d < %lu\n", len,
  844. (ulong)IP_UDP_HDR_SIZE);
  845. return;
  846. }
  847. /* Check the packet length */
  848. if (len < ntohs(ip->ip_len)) {
  849. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  850. return;
  851. }
  852. len = ntohs(ip->ip_len);
  853. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  854. /* Can't deal with anything except IPv4 */
  855. if ((ip->ip_hl_v & 0xf0) != 0x40)
  856. return;
  857. /* Can't deal with IP options (headers != 20 bytes) */
  858. if ((ip->ip_hl_v & 0x0f) > 0x05)
  859. return;
  860. /* Check the Checksum of the header */
  861. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
  862. puts("checksum bad\n");
  863. return;
  864. }
  865. /* If it is not for us, ignore it */
  866. dst_ip = NetReadIP(&ip->ip_dst);
  867. if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
  868. #ifdef CONFIG_MCAST_TFTP
  869. if (Mcast_addr != dst_ip)
  870. #endif
  871. return;
  872. }
  873. /* Read source IP address for later use */
  874. src_ip = NetReadIP(&ip->ip_src);
  875. /*
  876. * The function returns the unchanged packet if it's not
  877. * a fragment, and either the complete packet or NULL if
  878. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  879. */
  880. ip = NetDefragment(ip, &len);
  881. if (!ip)
  882. return;
  883. /*
  884. * watch for ICMP host redirects
  885. *
  886. * There is no real handler code (yet). We just watch
  887. * for ICMP host redirect messages. In case anybody
  888. * sees these messages: please contact me
  889. * (wd@denx.de), or - even better - send me the
  890. * necessary fixes :-)
  891. *
  892. * Note: in all cases where I have seen this so far
  893. * it was a problem with the router configuration,
  894. * for instance when a router was configured in the
  895. * BOOTP reply, but the TFTP server was on the same
  896. * subnet. So this is probably a warning that your
  897. * configuration might be wrong. But I'm not really
  898. * sure if there aren't any other situations.
  899. *
  900. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  901. * we send a tftp packet to a dead connection, or when
  902. * there is no server at the other end.
  903. */
  904. if (ip->ip_p == IPPROTO_ICMP) {
  905. receive_icmp(ip, len, src_ip, et);
  906. return;
  907. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  908. return;
  909. }
  910. #ifdef CONFIG_UDP_CHECKSUM
  911. if (ip->udp_xsum != 0) {
  912. ulong xsum;
  913. ushort *sumptr;
  914. ushort sumlen;
  915. xsum = ip->ip_p;
  916. xsum += (ntohs(ip->udp_len));
  917. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  918. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  919. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  920. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  921. sumlen = ntohs(ip->udp_len);
  922. sumptr = (ushort *) &(ip->udp_src);
  923. while (sumlen > 1) {
  924. ushort sumdata;
  925. sumdata = *sumptr++;
  926. xsum += ntohs(sumdata);
  927. sumlen -= 2;
  928. }
  929. if (sumlen > 0) {
  930. ushort sumdata;
  931. sumdata = *(unsigned char *) sumptr;
  932. sumdata = (sumdata << 8) & 0xff00;
  933. xsum += sumdata;
  934. }
  935. while ((xsum >> 16) != 0) {
  936. xsum = (xsum & 0x0000ffff) +
  937. ((xsum >> 16) & 0x0000ffff);
  938. }
  939. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  940. printf(" UDP wrong checksum %08lx %08x\n",
  941. xsum, ntohs(ip->udp_xsum));
  942. return;
  943. }
  944. }
  945. #endif
  946. #ifdef CONFIG_NETCONSOLE
  947. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  948. ntohs(ip->udp_dst),
  949. ntohs(ip->udp_src),
  950. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  951. #endif
  952. /*
  953. * IP header OK. Pass the packet to the current handler.
  954. */
  955. (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
  956. ntohs(ip->udp_dst),
  957. src_ip,
  958. ntohs(ip->udp_src),
  959. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  960. break;
  961. }
  962. }
  963. /**********************************************************************/
  964. static int net_check_prereq(enum proto_t protocol)
  965. {
  966. switch (protocol) {
  967. /* Fall through */
  968. #if defined(CONFIG_CMD_PING)
  969. case PING:
  970. if (NetPingIP == 0) {
  971. puts("*** ERROR: ping address not given\n");
  972. return 1;
  973. }
  974. goto common;
  975. #endif
  976. #if defined(CONFIG_CMD_SNTP)
  977. case SNTP:
  978. if (NetNtpServerIP == 0) {
  979. puts("*** ERROR: NTP server address not given\n");
  980. return 1;
  981. }
  982. goto common;
  983. #endif
  984. #if defined(CONFIG_CMD_DNS)
  985. case DNS:
  986. if (NetOurDNSIP == 0) {
  987. puts("*** ERROR: DNS server address not given\n");
  988. return 1;
  989. }
  990. goto common;
  991. #endif
  992. #if defined(CONFIG_CMD_NFS)
  993. case NFS:
  994. #endif
  995. case TFTPGET:
  996. case TFTPPUT:
  997. if (NetServerIP == 0) {
  998. puts("*** ERROR: `serverip' not set\n");
  999. return 1;
  1000. }
  1001. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1002. defined(CONFIG_CMD_DNS)
  1003. common:
  1004. #endif
  1005. /* Fall through */
  1006. case NETCONS:
  1007. case TFTPSRV:
  1008. if (NetOurIP == 0) {
  1009. puts("*** ERROR: `ipaddr' not set\n");
  1010. return 1;
  1011. }
  1012. /* Fall through */
  1013. #ifdef CONFIG_CMD_RARP
  1014. case RARP:
  1015. #endif
  1016. case BOOTP:
  1017. case CDP:
  1018. case DHCP:
  1019. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1020. int num = eth_get_dev_index();
  1021. switch (num) {
  1022. case -1:
  1023. puts("*** ERROR: No ethernet found.\n");
  1024. return 1;
  1025. case 0:
  1026. puts("*** ERROR: `ethaddr' not set\n");
  1027. break;
  1028. default:
  1029. printf("*** ERROR: `eth%daddr' not set\n",
  1030. num);
  1031. break;
  1032. }
  1033. NetStartAgain();
  1034. return 2;
  1035. }
  1036. /* Fall through */
  1037. default:
  1038. return 0;
  1039. }
  1040. return 0; /* OK */
  1041. }
  1042. /**********************************************************************/
  1043. int
  1044. NetCksumOk(uchar *ptr, int len)
  1045. {
  1046. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1047. }
  1048. unsigned
  1049. NetCksum(uchar *ptr, int len)
  1050. {
  1051. ulong xsum;
  1052. ushort *p = (ushort *)ptr;
  1053. xsum = 0;
  1054. while (len-- > 0)
  1055. xsum += *p++;
  1056. xsum = (xsum & 0xffff) + (xsum >> 16);
  1057. xsum = (xsum & 0xffff) + (xsum >> 16);
  1058. return xsum & 0xffff;
  1059. }
  1060. int
  1061. NetEthHdrSize(void)
  1062. {
  1063. ushort myvlanid;
  1064. myvlanid = ntohs(NetOurVLAN);
  1065. if (myvlanid == (ushort)-1)
  1066. myvlanid = VLAN_NONE;
  1067. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1068. VLAN_ETHER_HDR_SIZE;
  1069. }
  1070. int
  1071. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1072. {
  1073. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1074. ushort myvlanid;
  1075. myvlanid = ntohs(NetOurVLAN);
  1076. if (myvlanid == (ushort)-1)
  1077. myvlanid = VLAN_NONE;
  1078. memcpy(et->et_dest, addr, 6);
  1079. memcpy(et->et_src, NetOurEther, 6);
  1080. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1081. et->et_protlen = htons(prot);
  1082. return ETHER_HDR_SIZE;
  1083. } else {
  1084. struct vlan_ethernet_hdr *vet =
  1085. (struct vlan_ethernet_hdr *)xet;
  1086. vet->vet_vlan_type = htons(PROT_VLAN);
  1087. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1088. vet->vet_type = htons(prot);
  1089. return VLAN_ETHER_HDR_SIZE;
  1090. }
  1091. }
  1092. void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
  1093. {
  1094. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip;
  1095. /*
  1096. * If the data is an odd number of bytes, zero the
  1097. * byte after the last byte so that the checksum
  1098. * will work.
  1099. */
  1100. if (len & 1)
  1101. xip[IP_UDP_HDR_SIZE + len] = 0;
  1102. /*
  1103. * Construct an IP and UDP header.
  1104. * (need to set no fragment bit - XXX)
  1105. */
  1106. /* IP_HDR_SIZE / 4 (not including UDP) */
  1107. ip->ip_hl_v = 0x45;
  1108. ip->ip_tos = 0;
  1109. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1110. ip->ip_id = htons(NetIPID++);
  1111. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1112. ip->ip_ttl = 255;
  1113. ip->ip_p = 17; /* UDP */
  1114. ip->ip_sum = 0;
  1115. /* already in network byte order */
  1116. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  1117. /* - "" - */
  1118. NetCopyIP((void *)&ip->ip_dst, &dest);
  1119. ip->udp_src = htons(sport);
  1120. ip->udp_dst = htons(dport);
  1121. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1122. ip->udp_xsum = 0;
  1123. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
  1124. }
  1125. void copy_filename(char *dst, const char *src, int size)
  1126. {
  1127. if (*src && (*src == '"')) {
  1128. ++src;
  1129. --size;
  1130. }
  1131. while ((--size > 0) && *src && (*src != '"'))
  1132. *dst++ = *src++;
  1133. *dst = '\0';
  1134. }
  1135. #if defined(CONFIG_CMD_NFS) || \
  1136. defined(CONFIG_CMD_SNTP) || \
  1137. defined(CONFIG_CMD_DNS)
  1138. /*
  1139. * make port a little random (1024-17407)
  1140. * This keeps the math somewhat trivial to compute, and seems to work with
  1141. * all supported protocols/clients/servers
  1142. */
  1143. unsigned int random_port(void)
  1144. {
  1145. return 1024 + (get_timer(0) % 0x4000);
  1146. }
  1147. #endif
  1148. void ip_to_string(IPaddr_t x, char *s)
  1149. {
  1150. x = ntohl(x);
  1151. sprintf(s, "%d.%d.%d.%d",
  1152. (int) ((x >> 24) & 0xff),
  1153. (int) ((x >> 16) & 0xff),
  1154. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1155. );
  1156. }
  1157. void VLAN_to_string(ushort x, char *s)
  1158. {
  1159. x = ntohs(x);
  1160. if (x == (ushort)-1)
  1161. x = VLAN_NONE;
  1162. if (x == VLAN_NONE)
  1163. strcpy(s, "none");
  1164. else
  1165. sprintf(s, "%d", x & VLAN_IDMASK);
  1166. }
  1167. ushort string_to_VLAN(const char *s)
  1168. {
  1169. ushort id;
  1170. if (s == NULL)
  1171. return htons(VLAN_NONE);
  1172. if (*s < '0' || *s > '9')
  1173. id = VLAN_NONE;
  1174. else
  1175. id = (ushort)simple_strtoul(s, NULL, 10);
  1176. return htons(id);
  1177. }
  1178. ushort getenv_VLAN(char *var)
  1179. {
  1180. return string_to_VLAN(getenv(var));
  1181. }