net.c 39 KB

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