net.c 39 KB

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