net.c 44 KB

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