devether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. #include "../port/error.h"
  9. #include "../port/netif.h"
  10. #include "etherif.h"
  11. static Ether *etherxx[MaxEther];
  12. Chan*
  13. etherattach(char* spec)
  14. {
  15. ulong ctlrno;
  16. char *p;
  17. Chan *chan;
  18. Ether *ether;
  19. ctlrno = 0;
  20. if(spec && *spec){
  21. ctlrno = strtoul(spec, &p, 0);
  22. if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther))
  23. error(Ebadarg);
  24. }
  25. if((ether = etherxx[ctlrno]) == 0)
  26. error(Enodev);
  27. rlock(ether);
  28. if(waserror()){
  29. runlock(ether);
  30. nexterror();
  31. }
  32. chan = devattach('l', spec);
  33. chan->dev = ctlrno;
  34. if(ether->attach)
  35. ether->attach(etherxx[ctlrno]);
  36. poperror();
  37. runlock(ether);
  38. return chan;
  39. }
  40. static void
  41. ethershutdown(void)
  42. {
  43. Ether *ether;
  44. int i;
  45. for(i=0; i<MaxEther; i++){
  46. ether = etherxx[i];
  47. if(ether != nil && ether->detach != nil)
  48. ether->detach(ether);
  49. }
  50. }
  51. static Walkqid*
  52. etherwalk(Chan* chan, Chan *nchan, char **name, int nname)
  53. {
  54. Walkqid *wq;
  55. Ether *ether;
  56. ether = etherxx[chan->dev];
  57. rlock(ether);
  58. if(waserror()) {
  59. runlock(ether);
  60. nexterror();
  61. }
  62. wq = netifwalk(etherxx[chan->dev], chan, nchan, name, nname);
  63. poperror();
  64. runlock(ether);
  65. return wq;
  66. }
  67. static int
  68. etherstat(Chan* chan, uchar* dp, int n)
  69. {
  70. int s;
  71. Ether *ether;
  72. ether = etherxx[chan->dev];
  73. rlock(ether);
  74. if(waserror()) {
  75. runlock(ether);
  76. nexterror();
  77. }
  78. s = netifstat(ether, chan, dp, n);
  79. poperror();
  80. runlock(ether);
  81. return s;
  82. }
  83. static Chan*
  84. etheropen(Chan* chan, int omode)
  85. {
  86. Chan *c;
  87. Ether *ether;
  88. ether = etherxx[chan->dev];
  89. rlock(ether);
  90. if(waserror()) {
  91. runlock(ether);
  92. nexterror();
  93. }
  94. c = netifopen(ether, chan, omode);
  95. poperror();
  96. runlock(ether);
  97. return c;
  98. }
  99. static void
  100. ethercreate(Chan*, char*, int, ulong)
  101. {
  102. }
  103. static void
  104. etherclose(Chan* chan)
  105. {
  106. Ether *ether;
  107. ether = etherxx[chan->dev];
  108. rlock(ether);
  109. if(waserror()) {
  110. runlock(ether);
  111. nexterror();
  112. }
  113. netifclose(ether, chan);
  114. poperror();
  115. runlock(ether);
  116. }
  117. static long
  118. etherread(Chan* chan, void* buf, long n, vlong off)
  119. {
  120. Ether *ether;
  121. ulong offset = off;
  122. long r;
  123. ether = etherxx[chan->dev];
  124. rlock(ether);
  125. if(waserror()) {
  126. runlock(ether);
  127. nexterror();
  128. }
  129. if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
  130. /*
  131. * With some controllers it is necessary to reach
  132. * into the chip to extract statistics.
  133. */
  134. if(NETTYPE(chan->qid.path) == Nifstatqid){
  135. r = ether->ifstat(ether, buf, n, offset);
  136. goto out;
  137. }
  138. if(NETTYPE(chan->qid.path) == Nstatqid)
  139. ether->ifstat(ether, buf, 0, offset);
  140. }
  141. r = netifread(ether, chan, buf, n, offset);
  142. out:
  143. poperror();
  144. runlock(ether);
  145. return r;
  146. }
  147. static Block*
  148. etherbread(Chan* chan, long n, ulong offset)
  149. {
  150. Block *b;
  151. Ether *ether;
  152. ether = etherxx[chan->dev];
  153. rlock(ether);
  154. if(waserror()) {
  155. runlock(ether);
  156. nexterror();
  157. }
  158. b = netifbread(ether, chan, n, offset);
  159. poperror();
  160. runlock(ether);
  161. return b;
  162. }
  163. static int
  164. etherwstat(Chan* chan, uchar* dp, int n)
  165. {
  166. Ether *ether;
  167. int r;
  168. ether = etherxx[chan->dev];
  169. rlock(ether);
  170. if(waserror()) {
  171. runlock(ether);
  172. nexterror();
  173. }
  174. r = netifwstat(ether, chan, dp, n);
  175. poperror();
  176. runlock(ether);
  177. return r;
  178. }
  179. static void
  180. etherrtrace(Netfile* f, Etherpkt* pkt, int len)
  181. {
  182. int i, n;
  183. Block *bp;
  184. if(qwindow(f->in) <= 0)
  185. return;
  186. if(len > 58)
  187. n = 58;
  188. else
  189. n = len;
  190. bp = iallocb(64);
  191. if(bp == nil)
  192. return;
  193. memmove(bp->wp, pkt->d, n);
  194. i = TK2MS(MACHP(0)->ticks);
  195. bp->wp[58] = len>>8;
  196. bp->wp[59] = len;
  197. bp->wp[60] = i>>24;
  198. bp->wp[61] = i>>16;
  199. bp->wp[62] = i>>8;
  200. bp->wp[63] = i;
  201. bp->wp += 64;
  202. qpass(f->in, bp);
  203. }
  204. Block*
  205. etheriq(Ether* ether, Block* bp, int fromwire)
  206. {
  207. Etherpkt *pkt;
  208. ushort type;
  209. int len, multi, tome, fromme;
  210. Netfile **ep, *f, **fp, *fx;
  211. Block *xbp;
  212. ether->inpackets++;
  213. pkt = (Etherpkt*)bp->rp;
  214. len = BLEN(bp);
  215. type = (pkt->type[0]<<8)|pkt->type[1];
  216. fx = 0;
  217. ep = &ether->f[Ntypes];
  218. multi = pkt->d[0] & 1;
  219. /* check for valid multcast addresses */
  220. if(multi && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) != 0 && ether->prom == 0){
  221. if(!activemulti(ether, pkt->d, sizeof(pkt->d))){
  222. if(fromwire){
  223. freeb(bp);
  224. bp = 0;
  225. }
  226. return bp;
  227. }
  228. }
  229. /* is it for me? */
  230. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  231. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  232. /*
  233. * Multiplex the packet to all the connections which want it.
  234. * If the packet is not to be used subsequently (fromwire != 0),
  235. * attempt to simply pass it into one of the connections, thereby
  236. * saving a copy of the data (usual case hopefully).
  237. */
  238. for(fp = ether->f; fp < ep; fp++){
  239. if((f = *fp) && (f->type == type || f->type < 0))
  240. if(tome || multi || f->prom){
  241. /* Don't want to hear bridged packets */
  242. if(f->bridge && !fromwire && !fromme)
  243. continue;
  244. if(!f->headersonly){
  245. if(fromwire && fx == 0)
  246. fx = f;
  247. else if(xbp = iallocb(len)){
  248. memmove(xbp->wp, pkt, len);
  249. xbp->wp += len;
  250. if(qpass(f->in, xbp) < 0)
  251. ether->soverflows++;
  252. }
  253. else
  254. ether->soverflows++;
  255. }
  256. else
  257. etherrtrace(f, pkt, len);
  258. }
  259. }
  260. if(fx){
  261. if(qpass(fx->in, bp) < 0)
  262. ether->soverflows++;
  263. return 0;
  264. }
  265. if(fromwire){
  266. freeb(bp);
  267. return 0;
  268. }
  269. return bp;
  270. }
  271. static int
  272. etheroq(Ether* ether, Block* bp)
  273. {
  274. int len, loopback, s;
  275. Etherpkt *pkt;
  276. ether->outpackets++;
  277. /*
  278. * Check if the packet has to be placed back onto the input queue,
  279. * i.e. if it's a loopback or broadcast packet or the interface is
  280. * in promiscuous mode.
  281. * If it's a loopback packet indicate to etheriq that the data isn't
  282. * needed and return, etheriq will pass-on or free the block.
  283. * To enable bridging to work, only packets that were originated
  284. * by this interface are fed back.
  285. */
  286. pkt = (Etherpkt*)bp->rp;
  287. len = BLEN(bp);
  288. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  289. if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom){
  290. s = splhi();
  291. etheriq(ether, bp, 0);
  292. splx(s);
  293. }
  294. if(!loopback){
  295. qbwrite(ether->oq, bp);
  296. if(ether->transmit != nil)
  297. ether->transmit(ether);
  298. }else
  299. freeb(bp);
  300. return len;
  301. }
  302. static long
  303. etherwrite(Chan* chan, void* buf, long n, vlong)
  304. {
  305. Ether *ether;
  306. Block *bp;
  307. int onoff;
  308. Cmdbuf *cb;
  309. long l;
  310. ether = etherxx[chan->dev];
  311. rlock(ether);
  312. if(waserror()) {
  313. runlock(ether);
  314. nexterror();
  315. }
  316. if(NETTYPE(chan->qid.path) != Ndataqid) {
  317. l = netifwrite(ether, chan, buf, n);
  318. if(l >= 0)
  319. goto out;
  320. cb = parsecmd(buf, n);
  321. if(strcmp(cb->f[0], "nonblocking") == 0){
  322. if(cb->nf <= 1)
  323. onoff = 1;
  324. else
  325. onoff = atoi(cb->f[1]);
  326. qnoblock(ether->oq, onoff);
  327. free(cb);
  328. goto out;
  329. }
  330. free(cb);
  331. if(ether->ctl!=nil){
  332. l = ether->ctl(ether,buf,n);
  333. goto out;
  334. }
  335. error(Ebadctl);
  336. }
  337. if(n > ether->maxmtu)
  338. error(Etoobig);
  339. if(n < ether->minmtu)
  340. error(Etoosmall);
  341. bp = allocb(n);
  342. if(waserror()){
  343. freeb(bp);
  344. nexterror();
  345. }
  346. memmove(bp->rp, buf, n);
  347. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  348. bp->wp += n;
  349. poperror();
  350. l = etheroq(ether, bp);
  351. out:
  352. poperror();
  353. runlock(ether);
  354. return l;
  355. }
  356. static long
  357. etherbwrite(Chan* chan, Block* bp, ulong)
  358. {
  359. Ether *ether;
  360. long n;
  361. n = BLEN(bp);
  362. if(NETTYPE(chan->qid.path) != Ndataqid){
  363. if(waserror()) {
  364. freeb(bp);
  365. nexterror();
  366. }
  367. n = etherwrite(chan, bp->rp, n, 0);
  368. poperror();
  369. freeb(bp);
  370. return n;
  371. }
  372. ether = etherxx[chan->dev];
  373. rlock(ether);
  374. if(waserror()) {
  375. runlock(ether);
  376. nexterror();
  377. }
  378. if(n > ether->maxmtu){
  379. freeb(bp);
  380. error(Etoobig);
  381. }
  382. if(n < ether->minmtu){
  383. freeb(bp);
  384. error(Etoosmall);
  385. }
  386. n = etheroq(ether, bp);
  387. poperror();
  388. runlock(ether);
  389. return n;
  390. }
  391. static struct {
  392. char* type;
  393. int (*reset)(Ether*);
  394. } cards[MaxEther+1];
  395. void
  396. addethercard(char* t, int (*r)(Ether*))
  397. {
  398. static int ncard;
  399. if(ncard == MaxEther)
  400. panic("too many ether cards");
  401. cards[ncard].type = t;
  402. cards[ncard].reset = r;
  403. ncard++;
  404. }
  405. int
  406. parseether(uchar *to, char *from)
  407. {
  408. char nip[4];
  409. char *p;
  410. int i;
  411. p = from;
  412. for(i = 0; i < Eaddrlen; i++){
  413. if(*p == 0)
  414. return -1;
  415. nip[0] = *p++;
  416. if(*p == 0)
  417. return -1;
  418. nip[1] = *p++;
  419. nip[2] = 0;
  420. to[i] = strtoul(nip, 0, 16);
  421. if(*p == ':')
  422. p++;
  423. }
  424. return 0;
  425. }
  426. static void
  427. etherreset(void)
  428. {
  429. Ether *ether;
  430. int i, n, ctlrno;
  431. char name[KNAMELEN], buf[128];
  432. for(ether = 0, ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  433. if(ether == 0)
  434. ether = malloc(sizeof(Ether));
  435. memset(ether, 0, sizeof(Ether));
  436. ether->ctlrno = ctlrno;
  437. ether->mbps = 10;
  438. ether->minmtu = ETHERMINTU;
  439. ether->maxmtu = ETHERMAXTU;
  440. ether->itype = -1;
  441. if(archether(ctlrno, ether) <= 0)
  442. continue;
  443. for(n = 0; cards[n].type; n++){
  444. if(cistrcmp(cards[n].type, ether->type))
  445. continue;
  446. for(i = 0; i < ether->nopt; i++){
  447. if(cistrncmp(ether->opt[i], "ea=", 3) == 0){
  448. if(parseether(ether->ea, &ether->opt[i][3]) == -1)
  449. memset(ether->ea, 0, Eaddrlen);
  450. }else if(cistrcmp(ether->opt[i], "fullduplex") == 0 ||
  451. cistrcmp(ether->opt[i], "10BASE-TFD") == 0)
  452. ether->fullduplex = 1;
  453. else if(cistrcmp(ether->opt[i], "100BASE-TXFD") == 0)
  454. ether->mbps = 100;
  455. }
  456. if(cards[n].reset(ether))
  457. break;
  458. snprint(name, sizeof(name), "ether%d", ctlrno);
  459. if(ether->interrupt != nil)
  460. intrenable(ether->itype, ether->irq, ether->interrupt, ether, name);
  461. i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX irq %lud",
  462. ctlrno, ether->type, ether->mbps, ether->port, ether->irq);
  463. if(ether->mem)
  464. i += sprint(buf+i, " addr 0x%luX", PADDR(ether->mem));
  465. if(ether->size)
  466. i += sprint(buf+i, " size 0x%luX", ether->size);
  467. i += sprint(buf+i, ": %2.2uX%2.2uX%2.2uX%2.2uX%2.2uX%2.2uX",
  468. ether->ea[0], ether->ea[1], ether->ea[2],
  469. ether->ea[3], ether->ea[4], ether->ea[5]);
  470. sprint(buf+i, "\n");
  471. iprint(buf);
  472. if(ether->mbps == 100){
  473. netifinit(ether, name, Ntypes, 256*1024);
  474. if(ether->oq == 0)
  475. ether->oq = qopen(256*1024, Qmsg, 0, 0);
  476. }
  477. else{
  478. netifinit(ether, name, Ntypes, 64*1024);
  479. if(ether->oq == 0)
  480. ether->oq = qopen(64*1024, Qmsg, 0, 0);
  481. }
  482. if(ether->oq == 0)
  483. panic("etherreset %s", name);
  484. ether->alen = Eaddrlen;
  485. memmove(ether->addr, ether->ea, Eaddrlen);
  486. memset(ether->bcast, 0xFF, Eaddrlen);
  487. etherxx[ctlrno] = ether;
  488. ether = 0;
  489. break;
  490. }
  491. }
  492. if(ether)
  493. free(ether);
  494. }
  495. static void
  496. etherpower(int on)
  497. {
  498. int i;
  499. Ether *ether;
  500. for(i = 0; i < MaxEther; i++){
  501. if((ether = etherxx[i]) == nil || ether->power == nil)
  502. continue;
  503. if(on){
  504. if(canrlock(ether))
  505. continue;
  506. if(ether->power != nil)
  507. ether->power(ether, on);
  508. wunlock(ether);
  509. }else{
  510. if(!canrlock(ether))
  511. continue;
  512. wlock(ether);
  513. if(ether->power != nil)
  514. ether->power(ether, on);
  515. /* Keep locked until power goes back on */
  516. }
  517. }
  518. }
  519. #define POLY 0xedb88320
  520. /* really slow 32 bit crc for ethers */
  521. ulong
  522. ethercrc(uchar *p, int len)
  523. {
  524. int i, j;
  525. ulong crc, b;
  526. crc = 0xffffffff;
  527. for(i = 0; i < len; i++){
  528. b = *p++;
  529. for(j = 0; j < 8; j++){
  530. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  531. b >>= 1;
  532. }
  533. }
  534. return crc;
  535. }
  536. Dev etherdevtab = {
  537. 'l',
  538. "ether",
  539. etherreset,
  540. devinit,
  541. ethershutdown,
  542. etherattach,
  543. etherwalk,
  544. etherstat,
  545. etheropen,
  546. ethercreate,
  547. etherclose,
  548. etherread,
  549. etherbread,
  550. etherwrite,
  551. etherbwrite,
  552. devremove,
  553. etherwstat,
  554. etherpower,
  555. };