net.c 39 KB

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