net.c 44 KB

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