devether.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. ether->ifstat(ether, buf, 0, offset);
  86. }
  87. return netifread(&ether->Netif, chan, buf, n, offset);
  88. }
  89. static Block*
  90. etherbread(Chan* chan, int32_t n, int64_t offset)
  91. {
  92. return netifbread(&etherxx[chan->devno]->Netif, chan, n, offset);
  93. }
  94. static int32_t
  95. etherwstat(Chan* chan, uint8_t* dp, int32_t n)
  96. {
  97. return netifwstat(&etherxx[chan->devno]->Netif, chan, dp, n);
  98. }
  99. static void
  100. etherrtrace(Netfile* f, Etherpkt* pkt, int len)
  101. {
  102. int i, n;
  103. Block *bp;
  104. if(qwindow(f->iq) <= 0)
  105. return;
  106. if(len > 58)
  107. n = 58;
  108. else
  109. n = len;
  110. bp = iallocb(64);
  111. if(bp == nil)
  112. return;
  113. memmove(bp->wp, pkt->d, n);
  114. i = TK2MS(sys->ticks);
  115. bp->wp[58] = len>>8;
  116. bp->wp[59] = len;
  117. bp->wp[60] = i>>24;
  118. bp->wp[61] = i>>16;
  119. bp->wp[62] = i>>8;
  120. bp->wp[63] = i;
  121. bp->wp += 64;
  122. qpass(f->iq, bp);
  123. }
  124. Block*
  125. etheriq(Ether* ether, Block* bp, int fromwire)
  126. {
  127. Etherpkt *pkt;
  128. uint16_t type;
  129. int multi, tome, fromme;
  130. Netfile **ep, *f, **fp, *fx;
  131. Block *xbp;
  132. size_t len;
  133. ether->Netif.inpackets++;
  134. pkt = (Etherpkt*)bp->rp;
  135. len = BLEN(bp);
  136. type = (pkt->type[0]<<8)|pkt->type[1];
  137. fx = 0;
  138. ep = &ether->Netif.f[Ntypes];
  139. multi = pkt->d[0] & 1;
  140. /* check for valid multicast addresses */
  141. if(multi && memcmp(pkt->d, ether->Netif.bcast, sizeof(pkt->d)) != 0 && ether->Netif.prom == 0){
  142. if(!activemulti(&ether->Netif, pkt->d, sizeof(pkt->d))){
  143. if(fromwire){
  144. freeb(bp);
  145. bp = 0;
  146. }
  147. return bp;
  148. }
  149. }
  150. /* is it for me? */
  151. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  152. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  153. /*
  154. * Multiplex the packet to all the connections which want it.
  155. * If the packet is not to be used subsequently (fromwire != 0),
  156. * attempt to simply pass it into one of the connections, thereby
  157. * saving a copy of the data (usual case hopefully).
  158. */
  159. for(fp = ether->Netif.f; fp < ep; fp++){
  160. if(f = *fp)
  161. if(f->type == type || f->type < 0)
  162. if(tome || multi || f->prom || f->bridge & 2){
  163. /* Don't want to hear bridged packets */
  164. if(f->bridge && !fromwire && !fromme)
  165. continue;
  166. if(!f->headersonly){
  167. if(fromwire && fx == 0)
  168. fx = f;
  169. else if(xbp = iallocb(len)){
  170. memmove(xbp->wp, pkt, len);
  171. xbp->wp += len;
  172. if(qpass(f->iq, xbp) < 0)
  173. ether->Netif.soverflows++;
  174. }
  175. else
  176. ether->Netif.soverflows++;
  177. }
  178. else
  179. etherrtrace(f, pkt, len);
  180. }
  181. }
  182. if(fx){
  183. if(qpass(fx->iq, bp) < 0)
  184. ether->Netif.soverflows++;
  185. return 0;
  186. }
  187. if(fromwire){
  188. freeb(bp);
  189. return 0;
  190. }
  191. return bp;
  192. }
  193. static int
  194. etheroq(Ether* ether, Block* bp)
  195. {
  196. int loopback, s;
  197. Etherpkt *pkt;
  198. size_t len;
  199. ether->Netif.outpackets++;
  200. /*
  201. * Check if the packet has to be placed back onto the input queue,
  202. * i.e. if it's a loopback or broadcast packet or the interface is
  203. * in promiscuous mode.
  204. * If it's a loopback packet indicate to etheriq that the data isn't
  205. * needed and return, etheriq will pass-on or free the block.
  206. * To enable bridging to work, only packets that were originated
  207. * by this interface are fed back.
  208. */
  209. pkt = (Etherpkt*)bp->rp;
  210. len = BLEN(bp);
  211. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  212. if(loopback || memcmp(pkt->d, ether->Netif.bcast, sizeof(pkt->d)) == 0 || ether->Netif.prom){
  213. s = splhi();
  214. etheriq(ether, bp, 0);
  215. splx(s);
  216. }
  217. if(!loopback){
  218. qbwrite(ether->oq, bp);
  219. if(ether->transmit != nil)
  220. ether->transmit(ether);
  221. } else
  222. freeb(bp);
  223. return len;
  224. }
  225. static int32_t
  226. etherwrite(Chan* chan, void* buf, int32_t n, int64_t mm)
  227. {
  228. Proc *up = externup();
  229. Ether *ether;
  230. Block *bp;
  231. int nn, onoff;
  232. Cmdbuf *cb;
  233. ether = etherxx[chan->devno];
  234. if(NETTYPE(chan->qid.path) != Ndataqid) {
  235. nn = netifwrite(&ether->Netif, chan, buf, n);
  236. if(nn >= 0)
  237. return nn;
  238. cb = parsecmd(buf, n);
  239. if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
  240. if(cb->nf <= 1)
  241. onoff = 1;
  242. else
  243. onoff = atoi(cb->f[1]);
  244. qnoblock(ether->oq, onoff);
  245. free(cb);
  246. return n;
  247. }
  248. free(cb);
  249. if(ether->ctl != nil)
  250. return ether->ctl(ether, buf, n);
  251. error(Ebadctl);
  252. }
  253. if(n > ether->Netif.mtu)
  254. error(Etoobig);
  255. if(n < ether->Netif.minmtu)
  256. error(Etoosmall);
  257. bp = allocb(n);
  258. if(waserror()){
  259. freeb(bp);
  260. nexterror();
  261. }
  262. memmove(bp->rp, buf, n);
  263. if((ether->Netif.f[NETID(chan->qid.path)]->bridge & 2) == 0)
  264. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  265. poperror();
  266. bp->wp += n;
  267. return etheroq(ether, bp);
  268. }
  269. static int32_t
  270. etherbwrite(Chan* chan, Block* bp, int64_t mm)
  271. {
  272. Proc *up = externup();
  273. Ether *ether;
  274. size_t n;
  275. n = BLEN(bp);
  276. if(NETTYPE(chan->qid.path) != Ndataqid){
  277. if(waserror()) {
  278. freeb(bp);
  279. nexterror();
  280. }
  281. n = etherwrite(chan, bp->rp, n, 0);
  282. poperror();
  283. freeb(bp);
  284. return n;
  285. }
  286. ether = etherxx[chan->devno];
  287. if(n > ether->Netif.mtu){
  288. freeb(bp);
  289. error(Etoobig);
  290. }
  291. if(n < ether->Netif.minmtu){
  292. freeb(bp);
  293. error(Etoosmall);
  294. }
  295. return etheroq(ether, bp);
  296. }
  297. static struct {
  298. char* type;
  299. int (*reset)(Ether*);
  300. } cards[MaxEther+1];
  301. void
  302. addethercard(char* t, int (*r)(Ether*))
  303. {
  304. static int ncard;
  305. if(ncard == MaxEther)
  306. panic("too many ether cards");
  307. cards[ncard].type = t;
  308. cards[ncard].reset = r;
  309. ncard++;
  310. }
  311. int
  312. parseether(uint8_t *to, char *from)
  313. {
  314. char nip[4];
  315. char *p;
  316. int i;
  317. p = from;
  318. for(i = 0; i < Eaddrlen; i++){
  319. if(*p == 0)
  320. return -1;
  321. nip[0] = *p++;
  322. if(*p == 0)
  323. return -1;
  324. nip[1] = *p++;
  325. nip[2] = 0;
  326. to[i] = strtoul(nip, 0, 16);
  327. if(*p == ':')
  328. p++;
  329. }
  330. return 0;
  331. }
  332. static Ether*
  333. etherprobe(int cardno, int ctlrno)
  334. {
  335. if(cardno >= MaxEther)
  336. return nil;
  337. if(cards[cardno].type == nil || cards[cardno].reset == nil)
  338. return nil;
  339. return archetherprobe(ctlrno, cards[cardno].type, cards[cardno].reset);
  340. }
  341. static void
  342. etherreset(void)
  343. {
  344. Ether *ether;
  345. int cardno, ctlrno;
  346. cardno = ctlrno = 0;
  347. while(cards[cardno].type != nil && ctlrno < MaxEther){
  348. if(etherxx[ctlrno] != nil){
  349. ctlrno++;
  350. continue;
  351. }
  352. if((ether = etherprobe(cardno, ctlrno)) == nil){
  353. cardno++;
  354. continue;
  355. }
  356. etherxx[ctlrno] = ether;
  357. ctlrno++;
  358. }
  359. }
  360. static void
  361. ethershutdown(void)
  362. {
  363. char name[32];
  364. int i;
  365. Ether *ether;
  366. for(i = 0; i < MaxEther; i++){
  367. ether = etherxx[i];
  368. if(ether == nil)
  369. continue;
  370. if(ether->shutdown == nil) {
  371. print("#l%d: no shutdown function\n", i);
  372. continue;
  373. }
  374. snprint(name, sizeof(name), "ether%d", i);
  375. archethershutdown(ether);
  376. (*ether->shutdown)(ether);
  377. }
  378. }
  379. #define POLY 0xedb88320
  380. /* really slow 32 bit crc for ethers */
  381. uint32_t
  382. ethercrc(uint8_t *p, int len)
  383. {
  384. int i, j;
  385. uint32_t crc, b;
  386. crc = 0xffffffff;
  387. for(i = 0; i < len; i++){
  388. b = *p++;
  389. for(j = 0; j < 8; j++){
  390. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  391. b >>= 1;
  392. }
  393. }
  394. return crc;
  395. }
  396. Dev etherdevtab = {
  397. .dc = 'l',
  398. .name = "ether",
  399. .reset = etherreset,
  400. .init = devinit,
  401. .shutdown = ethershutdown,
  402. .attach = etherattach,
  403. .walk = etherwalk,
  404. .stat = etherstat,
  405. .open = etheropen,
  406. .create = ethercreate,
  407. .close = etherclose,
  408. .read = etherread,
  409. .bread = etherbread,
  410. .write = etherwrite,
  411. .bwrite = etherbwrite,
  412. .remove = devremove,
  413. .wstat = etherwstat,
  414. };