net.c 39 KB

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