net.c 45 KB

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