net.c 44 KB

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