devether.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "../port/error.h"
  16. #include "../port/netif.h"
  17. #include "etherif.h"
  18. static Ether *etherxx[MaxEther];
  19. extern Ether *archetherprobe(int ctlrno, char *type, int (*reset)(Ether *));
  20. extern void archethershutdown(Ether *ether);
  21. Chan*
  22. etherattach(char* spec)
  23. {
  24. Proc *up = externup();
  25. int ctlrno;
  26. char *p;
  27. Chan *chan;
  28. ctlrno = 0;
  29. if(spec && *spec){
  30. ctlrno = strtoul(spec, &p, 0);
  31. if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther))
  32. error(Ebadarg);
  33. }
  34. if(etherxx[ctlrno] == 0)
  35. error(Enodev);
  36. chan = devattach('l', spec);
  37. if(waserror()){
  38. chanfree(chan);
  39. nexterror();
  40. }
  41. chan->devno = ctlrno;
  42. if(etherxx[ctlrno]->attach)
  43. etherxx[ctlrno]->attach(etherxx[ctlrno]);
  44. poperror();
  45. return chan;
  46. }
  47. static Walkqid*
  48. etherwalk(Chan* chan, Chan* nchan, char** name, int nname)
  49. {
  50. return netifwalk(&etherxx[chan->devno]->Netif, chan, nchan, name, nname);
  51. }
  52. static int32_t
  53. etherstat(Chan* chan, uint8_t* dp, int32_t n)
  54. {
  55. return netifstat(&etherxx[chan->devno]->Netif, chan, dp, n);
  56. }
  57. static Chan*
  58. etheropen(Chan* chan, int omode)
  59. {
  60. return netifopen(&etherxx[chan->devno]->Netif, chan, omode);
  61. }
  62. static void
  63. ethercreate(Chan* c, char* d, int i, int n)
  64. {
  65. }
  66. static void
  67. etherclose(Chan* chan)
  68. {
  69. netifclose(&etherxx[chan->devno]->Netif, chan);
  70. }
  71. static int32_t
  72. etherread(Chan* chan, void* buf, int32_t n, int64_t off)
  73. {
  74. Ether *ether;
  75. uint32_t offset = off;
  76. ether = etherxx[chan->devno];
  77. if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
  78. /*
  79. * With some controllers it is necessary to reach
  80. * into the chip to extract statistics.
  81. */
  82. if(NETTYPE(chan->qid.path) == Nifstatqid)
  83. return ether->ifstat(ether, buf, n, offset);
  84. else if(NETTYPE(chan->qid.path) == Nstatqid)
  85. // XXX: This forces some drivers to accumulate
  86. // statistics that are then read out by the
  87. // netifread() call below. Note that this means
  88. // there is an implicit contract between the
  89. // devether layer and the drivers: drivers must
  90. // treat an ifstat() call with a zero length
  91. // argument specially and use it as a call to
  92. // syncronize the stats in ether->Netif.
  93. ether->ifstat(ether, buf, 0, offset);
  94. }
  95. return netifread(&ether->Netif, chan, buf, n, offset);
  96. }
  97. static Block*
  98. etherbread(Chan* chan, int32_t n, int64_t offset)
  99. {
  100. return netifbread(&etherxx[chan->devno]->Netif, chan, n, offset);
  101. }
  102. static int32_t
  103. etherwstat(Chan* chan, uint8_t* dp, int32_t n)
  104. {
  105. return netifwstat(&etherxx[chan->devno]->Netif, chan, dp, n);
  106. }
  107. static void
  108. etherrtrace(Netfile* f, Etherpkt* pkt, int len)
  109. {
  110. int i, n;
  111. Block *bp;
  112. if(qwindow(f->iq) <= 0)
  113. return;
  114. if(len > 58)
  115. n = 58;
  116. else
  117. n = len;
  118. bp = iallocb(64);
  119. if(bp == nil)
  120. return;
  121. memmove(bp->wp, pkt->d, n);
  122. i = TK2MS(sys->ticks);
  123. bp->wp[58] = len>>8;
  124. bp->wp[59] = len;
  125. bp->wp[60] = i>>24;
  126. bp->wp[61] = i>>16;
  127. bp->wp[62] = i>>8;
  128. bp->wp[63] = i;
  129. bp->wp += 64;
  130. qpass(f->iq, bp);
  131. }
  132. Block*
  133. etheriq(Ether* ether, Block* bp, int fromwire)
  134. {
  135. Etherpkt *pkt;
  136. uint16_t type;
  137. int multi, tome, fromme;
  138. Netfile **ep, *f, **fp, *fx;
  139. Block *xbp;
  140. size_t len;
  141. ether->Netif.inpackets++;
  142. pkt = (Etherpkt*)bp->rp;
  143. len = BLEN(bp);
  144. type = (pkt->type[0]<<8)|pkt->type[1];
  145. fx = 0;
  146. ep = &ether->Netif.f[Ntypes];
  147. multi = pkt->d[0] & 1;
  148. /* check for valid multicast addresses */
  149. if(multi && memcmp(pkt->d, ether->Netif.bcast, sizeof(pkt->d)) != 0 && ether->Netif.prom == 0){
  150. if(!activemulti(&ether->Netif, pkt->d, sizeof(pkt->d))){
  151. if(fromwire){
  152. freeb(bp);
  153. bp = nil;
  154. }
  155. return bp;
  156. }
  157. }
  158. /* is it for me? */
  159. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  160. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  161. /*
  162. * Multiplex the packet to all the connections that want it.
  163. * If the packet is not to be used subsequently (fromwire != 0),
  164. * attempt to simply pass it into one of the connections, thereby
  165. * saving a copy of the data (usual case hopefully).
  166. */
  167. for(fp = ether->Netif.f; fp < ep; fp++){
  168. f = *fp;
  169. if(f != nil)
  170. if(f->type == type || f->type < 0)
  171. if(tome || multi || f->prom || (f->bridge & 0x02)){
  172. /* Don't want to hear bridged packets */
  173. if(f->bridge && !fromwire && !fromme)
  174. continue;
  175. if(f->headersonly){
  176. etherrtrace(f, pkt, len);
  177. continue;
  178. }
  179. if(fromwire && fx == 0){
  180. fx = f;
  181. continue;
  182. }
  183. xbp = iallocb(len);
  184. if(xbp == nil){
  185. ether->Netif.soverflows++;
  186. continue;
  187. }
  188. memmove(xbp->wp, pkt, len);
  189. xbp->wp += len;
  190. if(qpass(f->iq, xbp) < 0)
  191. ether->Netif.soverflows++;
  192. }
  193. }
  194. if(fx != nil){
  195. if(qpass(fx->iq, bp) < 0)
  196. ether->Netif.soverflows++;
  197. return 0;
  198. }
  199. if(fromwire){
  200. freeb(bp);
  201. return 0;
  202. }
  203. return bp;
  204. }
  205. static int
  206. etheroq(Ether* ether, Block* bp)
  207. {
  208. int loopback, s;
  209. Etherpkt *pkt;
  210. size_t len;
  211. ether->Netif.outpackets++;
  212. /*
  213. * Check if the packet has to be placed back onto the input queue,
  214. * i.e. if it's a loopback or broadcast packet or the interface is
  215. * in promiscuous mode.
  216. * If it's a loopback packet indicate to etheriq that the data isn't
  217. * needed and return, etheriq will pass-on or free the block.
  218. * To enable bridging to work, only packets that were originated
  219. * by this interface are fed back.
  220. */
  221. pkt = (Etherpkt*)bp->rp;
  222. len = BLEN(bp);
  223. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  224. if(loopback || memcmp(pkt->d, ether->Netif.bcast, sizeof(pkt->d)) == 0 || ether->Netif.prom){
  225. s = splhi();
  226. etheriq(ether, bp, 0);
  227. splx(s);
  228. }
  229. if(loopback){
  230. freeb(bp);
  231. return len;
  232. }
  233. qbwrite(ether->oq, bp);
  234. if(ether->transmit != nil)
  235. ether->transmit(ether);
  236. return len;
  237. }
  238. static int32_t
  239. etherwrite(Chan* chan, void* buf, int32_t n, int64_t mm)
  240. {
  241. Proc *up = externup();
  242. Ether *ether;
  243. Block *bp;
  244. int nn, onoff;
  245. Cmdbuf *cb;
  246. ether = etherxx[chan->devno];
  247. if(NETTYPE(chan->qid.path) != Ndataqid) {
  248. nn = netifwrite(&ether->Netif, chan, buf, n);
  249. if(nn >= 0)
  250. return nn;
  251. cb = parsecmd(buf, n);
  252. if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
  253. onoff = 1;
  254. if(cb->nf > 1)
  255. onoff = atoi(cb->f[1]);
  256. qnoblock(ether->oq, onoff);
  257. free(cb);
  258. return n;
  259. }
  260. free(cb);
  261. if(ether->ctl == nil)
  262. error(Ebadctl);
  263. return ether->ctl(ether, buf, n);
  264. }
  265. if(n > ether->Netif.mtu)
  266. error(Etoobig);
  267. if(n < ether->Netif.minmtu)
  268. error(Etoosmall);
  269. bp = allocb(n);
  270. if(waserror()){
  271. freeb(bp);
  272. nexterror();
  273. }
  274. memmove(bp->rp, buf, n);
  275. if((ether->Netif.f[NETID(chan->qid.path)]->bridge & 2) == 0)
  276. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  277. poperror();
  278. bp->wp += n;
  279. return etheroq(ether, bp);
  280. }
  281. static int32_t
  282. etherbwrite(Chan* chan, Block* bp, int64_t mm)
  283. {
  284. Proc *up = externup();
  285. Ether *ether;
  286. size_t n;
  287. n = BLEN(bp);
  288. if(NETTYPE(chan->qid.path) != Ndataqid){
  289. if(waserror()) {
  290. freeb(bp);
  291. nexterror();
  292. }
  293. n = etherwrite(chan, bp->rp, n, 0);
  294. poperror();
  295. freeb(bp);
  296. return n;
  297. }
  298. ether = etherxx[chan->devno];
  299. if(n > ether->Netif.mtu){
  300. freeb(bp);
  301. error(Etoobig);
  302. }
  303. if(n < ether->Netif.minmtu){
  304. freeb(bp);
  305. error(Etoosmall);
  306. }
  307. return etheroq(ether, bp);
  308. }
  309. static struct {
  310. char* type;
  311. int (*reset)(Ether*);
  312. } cards[MaxEther+1];
  313. void
  314. addethercard(char* t, int (*r)(Ether*))
  315. {
  316. static int ncard;
  317. if(ncard == MaxEther)
  318. panic("too many ether cards");
  319. cards[ncard].type = t;
  320. cards[ncard].reset = r;
  321. ncard++;
  322. }
  323. int
  324. parseether(uint8_t *to, char *from)
  325. {
  326. char nip[4];
  327. char *p;
  328. int i;
  329. p = from;
  330. for(i = 0; i < Eaddrlen; i++){
  331. if(*p == 0)
  332. return -1;
  333. nip[0] = *p++;
  334. if(*p == 0)
  335. return -1;
  336. nip[1] = *p++;
  337. nip[2] = 0;
  338. to[i] = strtoul(nip, 0, 16);
  339. if(*p == ':')
  340. p++;
  341. }
  342. return 0;
  343. }
  344. static Ether*
  345. etherprobe(int cardno, int ctlrno)
  346. {
  347. if(cardno >= MaxEther)
  348. return nil;
  349. if(cards[cardno].type == nil || cards[cardno].reset == nil)
  350. return nil;
  351. return archetherprobe(ctlrno, cards[cardno].type, cards[cardno].reset);
  352. }
  353. static void
  354. etherreset(void)
  355. {
  356. Ether *ether;
  357. int cardno, ctlrno;
  358. cardno = ctlrno = 0;
  359. while(cards[cardno].type != nil && ctlrno < MaxEther){
  360. if(etherxx[ctlrno] != nil){
  361. ctlrno++;
  362. continue;
  363. }
  364. if((ether = etherprobe(cardno, ctlrno)) == nil){
  365. cardno++;
  366. continue;
  367. }
  368. etherxx[ctlrno] = ether;
  369. ctlrno++;
  370. }
  371. }
  372. static void
  373. ethershutdown(void)
  374. {
  375. char name[32];
  376. int i;
  377. Ether *ether;
  378. for(i = 0; i < MaxEther; i++){
  379. ether = etherxx[i];
  380. if(ether == nil)
  381. continue;
  382. if(ether->shutdown == nil) {
  383. print("#l%d: no shutdown function\n", i);
  384. continue;
  385. }
  386. snprint(name, sizeof(name), "ether%d", i);
  387. archethershutdown(ether);
  388. (*ether->shutdown)(ether);
  389. }
  390. }
  391. #define POLY 0xedb88320
  392. /* really slow 32 bit crc for ethers */
  393. uint32_t
  394. ethercrc(uint8_t *p, int len)
  395. {
  396. int i, j;
  397. uint32_t crc, b;
  398. crc = 0xffffffff;
  399. for(i = 0; i < len; i++){
  400. b = *p++;
  401. for(j = 0; j < 8; j++){
  402. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  403. b >>= 1;
  404. }
  405. }
  406. return crc;
  407. }
  408. Dev etherdevtab = {
  409. .dc = 'l',
  410. .name = "ether",
  411. .reset = etherreset,
  412. .init = devinit,
  413. .shutdown = ethershutdown,
  414. .attach = etherattach,
  415. .walk = etherwalk,
  416. .stat = etherstat,
  417. .open = etheropen,
  418. .create = ethercreate,
  419. .close = etherclose,
  420. .read = etherread,
  421. .bread = etherbread,
  422. .write = etherwrite,
  423. .bwrite = etherbwrite,
  424. .remove = devremove,
  425. .wstat = etherwstat,
  426. };