net.c 45 KB

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