net.c 44 KB

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