0eb05c560bd19af51f7b803e12d7a5baae3b0957
[akaros.git] / kern / src / net / netif.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 static int netown(struct netfile *, char *unused_char_p_t, int);
17 static int openfile(struct netif *, int);
18 static char *matchtoken(char *unused_char_p_t, char *);
19 static char *netmulti(struct netif *, struct netfile *,
20                                           uint8_t * unused_uint8_p_t, int);
21 static int parseaddr(uint8_t * unused_uint8_p_t, char *unused_char_p_t, int);
22
23 /*
24  *  set up a new network interface
25  */
26 void netifinit(struct netif *nif, char *name, int nfile, uint32_t limit)
27 {
28         qlock_init(&nif->qlock);
29         strncpy(nif->name, name, KNAMELEN - 1);
30         nif->name[KNAMELEN - 1] = 0;
31         nif->nfile = nfile;
32         nif->f = kzmalloc(nfile * sizeof(struct netfile *), 0);
33         if (nif->f)
34                 memset(nif->f, 0, nfile * sizeof(struct netfile *));
35         else
36                 nif->nfile = 0;
37         nif->limit = limit;
38 }
39
40 /*
41  *  generate a 3 level directory
42  */
43 static int
44 netifgen(struct chan *c, char *unused_char_p_t, struct dirtab *vp,
45                  int unused_int, int i, struct dir *dp)
46 {
47         struct qid q;
48         struct netif *nif = (struct netif *)vp;
49         struct netfile *f;
50         int perm;
51         char *o;
52
53         q.type = QTFILE;
54         q.vers = 0;
55
56         /* top level directory contains the name of the network */
57         if (c->qid.path == 0) {
58                 switch (i) {
59                         case DEVDOTDOT:
60                                 q.path = 0;
61                                 q.type = QTDIR;
62                                 devdir(c, q, ".", 0, eve, 0555, dp);
63                                 break;
64                         case 0:
65                                 q.path = N2ndqid;
66                                 q.type = QTDIR;
67                                 strncpy(get_cur_genbuf(), nif->name, GENBUF_SZ);
68                                 devdir(c, q, get_cur_genbuf(), 0, eve, 0555, dp);
69                                 break;
70                         default:
71                                 return -1;
72                 }
73                 return 1;
74         }
75
76         /* second level contains clone plus all the conversations.
77          *
78          * This ancient comment is from plan9.  Inferno and nxm both had issues
79          * here.  You couldn't ls /net/ether0/ when it didn't have any convs.  There
80          * were also issues with nxm where you couldn't stat ether0/x/stats
81          * properly.
82          *
83          * The issue is that if we handle things like Nstatqid, then we will never
84          * pass it down to the third level. And since we just set the path ==
85          * Nstatqid, we won't have the NETID muxed in. If someone isn't trying to
86          * generate a chan, but instead is looking it up (devwalk generates, devstat
87          * already has the chan), then they are also looking for a devdir with path
88          * containing ID << 5. So if you stat ether0/1/ifstats, devstat is looking
89          * for path 41, but we return path 9 (41 = 32 + 9). (these numbers are
90          * before we tracked NETID + 1).
91          *
92          * We (akaros and plan9) had a big if here, that would catch things that do
93          * not exist in the subdirs of a netif. Things like clone make sense here.
94          * I guess addr too, though that seems to be added since the original
95          * comment. You can see what the 3rd level was expecting to parse by looking
96          * farther down in the code.
97          *
98          * The root of the problem was that the old code couldn't tell the
99          * difference between no netid and netid 0. Now, we determine if we're at
100          * the second level by the lack of a netid, instead of trying to enumerate
101          * the qid types that the second level could have. The latter approach
102          * allowed for something like ether0/1/stats, but we couldn't actually
103          * devstat ether0/stats directly. It's worth noting that there is no
104          * difference to the content of ether0/stats and ether0/x/stats (when you
105          * read), but they have different chan qids.
106          *
107          * Here's the old if block:
108          t = NETTYPE(c->qid.path);
109          if (t == N2ndqid || t == Ncloneqid || t == Naddrqid) {
110          */
111         if (NETID(c->qid.path) == -1) {
112                 switch (i) {
113                         case DEVDOTDOT:
114                                 q.type = QTDIR;
115                                 q.path = 0;
116                                 devdir(c, q, ".", 0, eve, DMDIR | 0555, dp);
117                                 break;
118                         case 0:
119                                 q.path = Ncloneqid;
120                                 devdir(c, q, "clone", 0, eve, 0666, dp);
121                                 break;
122                         case 1:
123                                 q.path = Naddrqid;
124                                 devdir(c, q, "addr", 0, eve, 0666, dp);
125                                 break;
126                         case 2:
127                                 q.path = Nstatqid;
128                                 devdir(c, q, "stats", 0, eve, 0444, dp);
129                                 break;
130                         case 3:
131                                 q.path = Nifstatqid;
132                                 devdir(c, q, "ifstats", 0, eve, 0444, dp);
133                                 break;
134                         default:
135                                 i -= 4;
136                                 if (i >= nif->nfile)
137                                         return -1;
138                                 if (nif->f[i] == 0)
139                                         return 0;
140                                 q.type = QTDIR;
141                                 q.path = NETQID(i, N3rdqid);
142                                 snprintf(get_cur_genbuf(), GENBUF_SZ, "%d", i);
143                                 devdir(c, q, get_cur_genbuf(), 0, eve, DMDIR | 0555, dp);
144                                 break;
145                 }
146                 return 1;
147         }
148
149         /* third level */
150         f = nif->f[NETID(c->qid.path)];
151         if (f == 0)
152                 return 0;
153         if (*f->owner) {
154                 o = f->owner;
155                 perm = f->mode;
156         } else {
157                 o = eve;
158                 perm = 0666;
159         }
160         switch (i) {
161                 case DEVDOTDOT:
162                         q.type = QTDIR;
163                         q.path = N2ndqid;
164                         strncpy(get_cur_genbuf(), nif->name, GENBUF_SZ);
165                         devdir(c, q, get_cur_genbuf(), 0, eve, DMDIR | 0555, dp);
166                         break;
167                 case 0:
168                         q.path = NETQID(NETID(c->qid.path), Ndataqid);
169                         devdir(c, q, "data", 0, o, perm, dp);
170                         break;
171                 case 1:
172                         q.path = NETQID(NETID(c->qid.path), Nctlqid);
173                         devdir(c, q, "ctl", 0, o, perm, dp);
174                         break;
175                 case 2:
176                         q.path = NETQID(NETID(c->qid.path), Nstatqid);
177                         devdir(c, q, "stats", 0, eve, 0444, dp);
178                         break;
179                 case 3:
180                         q.path = NETQID(NETID(c->qid.path), Ntypeqid);
181                         devdir(c, q, "type", 0, eve, 0444, dp);
182                         break;
183                 case 4:
184                         q.path = NETQID(NETID(c->qid.path), Nifstatqid);
185                         devdir(c, q, "ifstats", 0, eve, 0444, dp);
186                         break;
187                 default:
188                         return -1;
189         }
190         return 1;
191 }
192
193 struct walkqid *netifwalk(struct netif *nif, struct chan *c, struct chan *nc,
194                                                   char **name, int nname)
195 {
196         return devwalk(c, nc, name, nname, (struct dirtab *)nif, 0, netifgen);
197 }
198
199 struct chan *netifopen(struct netif *nif, struct chan *c, int omode)
200 {
201         int id;
202         struct netfile *f;
203
204         id = 0;
205         if (c->qid.type & QTDIR) {
206                 if (!IS_RDONLY(omode))
207                         error(Eperm);
208         } else {
209                 switch (NETTYPE(c->qid.path)) {
210                         case Ndataqid:
211                         case Nctlqid:
212                                 id = NETID(c->qid.path);
213                                 openfile(nif, id);
214                                 break;
215                         case Ncloneqid:
216                                 id = openfile(nif, -1);
217                                 c->qid.path = NETQID(id, Nctlqid);
218                                 break;
219                         default:
220                                 if (!IS_RDONLY(omode))
221                                         error(Ebadarg);
222                 }
223                 switch (NETTYPE(c->qid.path)) {
224                         case Ndataqid:
225                         case Nctlqid:
226                                 f = nif->f[id];
227                                 if (netown(f, current->user, omode & 7) < 0)
228                                         error(Eperm);
229                                 break;
230                 }
231         }
232         c->mode = openmode(omode);
233         c->flag |= COPEN;
234         c->offset = 0;
235         c->iounit = qiomaxatomic;
236         return c;
237 }
238
239 long
240 netifread(struct netif *nif, struct chan *c, void *a, long n, uint32_t offset)
241 {
242         int i, j;
243         struct netfile *f;
244         char *p;
245
246         if (c->qid.type & QTDIR)
247                 return devdirread(c, a, n, (struct dirtab *)nif, 0, netifgen);
248
249         switch (NETTYPE(c->qid.path)) {
250                 case Ndataqid:
251                         f = nif->f[NETID(c->qid.path)];
252                         return qread(f->in, a, n);
253                 case Nctlqid:
254                         return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
255                 case Nstatqid:
256                         p = kzmalloc(READSTR, 0);
257                         if (p == NULL)
258                                 return 0;
259                         j = snprintf(p, READSTR, "in: %d\n", nif->inpackets);
260                         j += snprintf(p + j, READSTR - j, "link: %d\n", nif->link);
261                         j += snprintf(p + j, READSTR - j, "out: %d\n", nif->outpackets);
262                         j += snprintf(p + j, READSTR - j, "crc errs: %d\n", nif->crcs);
263                         j += snprintf(p + j, READSTR - j, "overflows: %d\n",
264                                                   nif->overflows);
265                         j += snprintf(p + j, READSTR - j, "soft overflows: %d\n",
266                                                   nif->soverflows);
267                         j += snprintf(p + j, READSTR - j, "framing errs: %d\n",
268                                                   nif->frames);
269                         j += snprintf(p + j, READSTR - j, "buffer errs: %d\n", nif->buffs);
270                         j += snprintf(p + j, READSTR - j, "output errs: %d\n", nif->oerrs);
271                         j += snprintf(p + j, READSTR - j, "prom: %d\n", nif->prom);
272                         j += snprintf(p + j, READSTR - j, "mbps: %d\n", nif->mbps);
273                         j += snprintf(p + j, READSTR - j, "addr: ");
274                         for (i = 0; i < nif->alen; i++)
275                                 j += snprintf(p + j, READSTR - j, "%02.2x", nif->addr[i]);
276                         snprintf(p + j, READSTR - j, "\n");
277                         n = readstr(offset, a, n, p);
278                         kfree(p);
279                         return n;
280                 case Naddrqid:
281                         p = kzmalloc(READSTR, 0);
282                         if (p == NULL)
283                                 return 0;
284                         j = 0;
285                         for (i = 0; i < nif->alen; i++)
286                                 j += snprintf(p + j, READSTR - j, "%02.2x", nif->addr[i]);
287                         n = readstr(offset, a, n, p);
288                         kfree(p);
289                         return n;
290                 case Ntypeqid:
291                         f = nif->f[NETID(c->qid.path)];
292                         return readnum(offset, a, n, f->type, NUMSIZE);
293                 case Nifstatqid:
294                         return 0;
295         }
296         error(Ebadarg);
297         return -1;      /* not reached */
298 }
299
300 struct block *netifbread(struct netif *nif, struct chan *c, long n,
301                                                  uint32_t offset)
302 {
303         if ((c->qid.type & QTDIR) || NETTYPE(c->qid.path) != Ndataqid)
304                 return devbread(c, n, offset);
305
306         return qbread(nif->f[NETID(c->qid.path)]->in, n);
307 }
308
309 /*
310  *  make sure this type isn't already in use on this device
311  */
312 static int typeinuse(struct netif *nif, int type)
313 {
314         struct netfile *f, **fp, **efp;
315
316         if (type <= 0)
317                 return 0;
318
319         efp = &nif->f[nif->nfile];
320         for (fp = nif->f; fp < efp; fp++) {
321                 f = *fp;
322                 if (f == 0)
323                         continue;
324                 if (f->type == type)
325                         return 1;
326         }
327         return 0;
328 }
329
330 /*
331  *  the devxxx.c that calls us handles writing data, it knows best
332  */
333 long netifwrite(struct netif *nif, struct chan *c, void *a, long n)
334 {
335         ERRSTACK(1);
336         struct netfile *f;
337         int type;
338         char *p, buf[64];
339         uint8_t binaddr[Nmaxaddr];
340
341         if (NETTYPE(c->qid.path) != Nctlqid)
342                 error(Eperm);
343
344         if (n >= sizeof(buf))
345                 n = sizeof(buf) - 1;
346         memmove(buf, a, n);
347         buf[n] = 0;
348
349         if (waserror()) {
350                 qunlock(&nif->qlock);
351                 nexterror();
352         }
353
354         qlock(&nif->qlock);
355         f = nif->f[NETID(c->qid.path)];
356         if ((p = matchtoken(buf, "connect")) != 0) {
357                 type = strtol(p, 0, 0); /* allows any base, though usually hex */
358                 if (typeinuse(nif, type))
359                         error(Einuse);
360                 f->type = type;
361                 if (f->type < 0)
362                         nif->all++;
363         } else if (matchtoken(buf, "promiscuous")) {
364                 if (f->prom == 0) {
365                         if (nif->prom == 0 && nif->promiscuous != NULL)
366                                 nif->promiscuous(nif->arg, 1);
367                         f->prom = 1;
368                         nif->prom++;
369                 }
370         } else if ((p = matchtoken(buf, "scanbs")) != 0) {
371                 /* scan for base stations */
372                 if (f->scan == 0) {
373                         type = strtol(p, 0, 0); /* allows any base, though usually hex */
374                         if (type < 5)
375                                 type = 5;
376                         if (nif->scanbs != NULL)
377                                 nif->scanbs(nif->arg, type);
378                         f->scan = type;
379                         nif->scan++;
380                 }
381         } else if (matchtoken(buf, "bridge")) {
382                 f->bridge = 1;
383         } else if (matchtoken(buf, "headersonly")) {
384                 f->headersonly = 1;
385         } else if ((p = matchtoken(buf, "addmulti")) != 0) {
386                 if (parseaddr(binaddr, p, nif->alen) < 0)
387                         error("bad address");
388                 p = netmulti(nif, f, binaddr, 1);
389                 if (p)
390                         error(p);
391         } else if ((p = matchtoken(buf, "remmulti")) != 0) {
392                 if (parseaddr(binaddr, p, nif->alen) < 0)
393                         error("bad address");
394                 p = netmulti(nif, f, binaddr, 0);
395                 if (p)
396                         error(p);
397         } else
398                 n = -1;
399         qunlock(&nif->qlock);
400         poperror();
401         return n;
402 }
403
404 int netifwstat(struct netif *nif, struct chan *c, uint8_t * db, int n)
405 {
406         struct dir *dir;
407         struct netfile *f;
408         int m;
409
410         f = nif->f[NETID(c->qid.path)];
411         if (f == 0) {
412                 set_errno(ENOENT);
413                 error(Enonexist);
414         }
415
416         if (netown(f, current->user, OWRITE) < 0)
417                 error(Eperm);
418
419         dir = kzmalloc(sizeof(struct dir) + n, 0);
420         m = convM2D(db, n, &dir[0], (char *)&dir[1]);
421         if (m == 0) {
422                 kfree(dir);
423                 error(Eshortstat);
424         }
425         if (!emptystr(dir[0].uid))
426                 strncpy(f->owner, dir[0].uid, KNAMELEN);
427         if (dir[0].mode != ~0UL)
428                 f->mode = dir[0].mode;
429         kfree(dir);
430         return m;
431 }
432
433 int netifstat(struct netif *nif, struct chan *c, uint8_t * db, int n)
434 {
435         return devstat(c, db, n, (struct dirtab *)nif, 0, netifgen);
436 }
437
438 void netifclose(struct netif *nif, struct chan *c)
439 {
440         struct netfile *f;
441         int t;
442         struct netaddr *ap;
443
444         if ((c->flag & COPEN) == 0)
445                 return;
446
447         t = NETTYPE(c->qid.path);
448         if (t != Ndataqid && t != Nctlqid)
449                 return;
450
451         f = nif->f[NETID(c->qid.path)];
452         qlock(&f->qlock);
453         if (--(f->inuse) == 0) {
454                 if (f->prom) {
455                         qlock(&nif->qlock);
456                         if (--(nif->prom) == 0 && nif->promiscuous != NULL)
457                                 nif->promiscuous(nif->arg, 0);
458                         qunlock(&nif->qlock);
459                         f->prom = 0;
460                 }
461                 if (f->scan) {
462                         qlock(&nif->qlock);
463                         if (--(nif->scan) == 0 && nif->scanbs != NULL)
464                                 nif->scanbs(nif->arg, 0);
465                         qunlock(&nif->qlock);
466                         f->prom = 0;
467                         f->scan = 0;
468                 }
469                 if (f->nmaddr) {
470                         qlock(&nif->qlock);
471                         t = 0;
472                         for (ap = nif->maddr; ap; ap = ap->next) {
473                                 if (f->maddr[t / 8] & (1 << (t % 8)))
474                                         netmulti(nif, f, ap->addr, 0);
475                         }
476                         qunlock(&nif->qlock);
477                         f->nmaddr = 0;
478                 }
479                 if (f->type < 0) {
480                         qlock(&nif->qlock);
481                         --(nif->all);
482                         qunlock(&nif->qlock);
483                 }
484                 f->owner[0] = 0;
485                 f->type = 0;
486                 f->bridge = 0;
487                 f->headersonly = 0;
488                 qclose(f->in);
489         }
490         qunlock(&f->qlock);
491 }
492
493 spinlock_t netlock = SPINLOCK_INITIALIZER;
494
495 static int netown(struct netfile *p, char *o, int omode)
496 {
497         static int access[] = { 0400, 0200, 0600, 0100 };
498         int mode;
499         int t;
500
501         spin_lock(&netlock);
502         if (*p->owner) {
503                 if (strncmp(o, p->owner, KNAMELEN) == 0)        /* User */
504                         mode = p->mode;
505                 else if (strncmp(o, eve, KNAMELEN) == 0)        /* Bootes is group */
506                         mode = p->mode << 3;
507                 else
508                         mode = p->mode << 6;    /* Other */
509
510                 t = access[omode & 3];
511                 if ((t & mode) == t) {
512                         spin_unlock(&netlock);
513                         return 0;
514                 } else {
515                         spin_unlock(&netlock);
516                         return -1;
517                 }
518         }
519         strncpy(p->owner, o, KNAMELEN);
520         p->mode = 0660;
521         spin_unlock(&netlock);
522         return 0;
523 }
524
525 /*
526  *  Increment the reference count of a network device.
527  *  If id < 0, return an unused ether device.
528  */
529 static int openfile(struct netif *nif, int id)
530 {
531         ERRSTACK(1);
532         struct netfile *f, **fp, **efp;
533
534         if (id >= 0) {
535                 f = nif->f[id];
536                 if (f == 0)
537                         error(Enodev);
538                 qlock(&f->qlock);
539                 qreopen(f->in);
540                 f->inuse++;
541                 qunlock(&f->qlock);
542                 return id;
543         }
544
545         qlock(&nif->qlock);
546         if (waserror()) {
547                 qunlock(&nif->qlock);
548                 nexterror();
549         }
550         efp = &nif->f[nif->nfile];
551         for (fp = nif->f; fp < efp; fp++) {
552                 f = *fp;
553                 if (f == 0) {
554                         f = kzmalloc(sizeof(struct netfile), 0);
555                         if (f == 0)
556                                 exhausted("memory");
557                         /* since we lock before netifinit (if we ever call that...) */
558                         qlock_init(&f->qlock);
559                         f->in = qopen(nif->limit, Qmsg, 0, 0);
560                         if (f->in == NULL) {
561                                 kfree(f);
562                                 exhausted("memory");
563                         }
564                         *fp = f;
565                         qlock(&f->qlock);
566                 } else {
567                         qlock(&f->qlock);
568                         if (f->inuse) {
569                                 qunlock(&f->qlock);
570                                 continue;
571                         }
572                 }
573                 f->inuse = 1;
574                 qreopen(f->in);
575                 netown(f, current->user, 0);
576                 qunlock(&f->qlock);
577                 qunlock(&nif->qlock);
578                 poperror();
579                 return fp - nif->f;
580         }
581         error(Enodev);
582         return -1;      /* not reached */
583 }
584
585 /*
586  *  look for a token starting a string,
587  *  return a pointer to first non-space char after it
588  */
589 static char *matchtoken(char *p, char *token)
590 {
591         int n;
592
593         n = strlen(token);
594         if (strncmp(p, token, n))
595                 return 0;
596         p += n;
597         if (*p == 0)
598                 return p;
599         if (*p != ' ' && *p != '\t' && *p != '\n')
600                 return 0;
601         while (*p == ' ' || *p == '\t' || *p == '\n')
602                 p++;
603         return p;
604 }
605
606 static uint32_t hash(uint8_t * a, int len)
607 {
608         uint32_t sum = 0;
609
610         while (len-- > 0)
611                 sum = (sum << 1) + *a++;
612         return sum % Nmhash;
613 }
614
615 int activemulti(struct netif *nif, uint8_t * addr, int alen)
616 {
617         struct netaddr *hp;
618
619         for (hp = nif->mhash[hash(addr, alen)]; hp; hp = hp->hnext)
620                 if (memcmp(addr, hp->addr, alen) == 0) {
621                         if (hp->ref)
622                                 return 1;
623                         else
624                                 break;
625                 }
626         return 0;
627 }
628
629 static int parseaddr(uint8_t * to, char *from, int alen)
630 {
631         char nip[4];
632         char *p;
633         int i;
634
635         p = from;
636         for (i = 0; i < alen; i++) {
637                 if (*p == 0)
638                         return -1;
639                 nip[0] = *p++;
640                 if (*p == 0)
641                         return -1;
642                 nip[1] = *p++;
643                 nip[2] = 0;
644                 to[i] = strtoul(nip, 0, 16);
645                 if (*p == ':')
646                         p++;
647         }
648         return 0;
649 }
650
651 /*
652  *  keep track of multicast addresses
653  */
654 static char *netmulti(struct netif *nif, struct netfile *f, uint8_t * addr,
655                                           int add)
656 {
657         struct netaddr **l, *ap;
658         int i;
659         uint32_t h;
660
661         if (nif->multicast == NULL)
662                 return "interface does not support multicast";
663
664         l = &nif->maddr;
665         i = 0;
666         for (ap = *l; ap; ap = *l) {
667                 if (memcmp(addr, ap->addr, nif->alen) == 0)
668                         break;
669                 i++;
670                 l = &ap->next;
671         }
672
673         if (add) {
674                 if (ap == 0) {
675                         /* TODO: AFAIK, this never gets freed.  if we fix that, we can use a
676                          * kref too (instead of int ap->ref). */
677                         *l = ap = kzmalloc(sizeof(*ap), 0);
678                         memmove(ap->addr, addr, nif->alen);
679                         ap->next = 0;
680                         ap->ref = 1;
681                         h = hash(addr, nif->alen);
682                         ap->hnext = nif->mhash[h];
683                         nif->mhash[h] = ap;
684                 } else {
685                         ap->ref++;
686                 }
687                 if (ap->ref == 1) {
688                         nif->nmaddr++;
689                         nif->multicast(nif->arg, addr, 1);
690                 }
691                 if (i < 8 * sizeof(f->maddr)) {
692                         if ((f->maddr[i / 8] & (1 << (i % 8))) == 0)
693                                 f->nmaddr++;
694                         f->maddr[i / 8] |= 1 << (i % 8);
695                 }
696         } else {
697                 if (ap == 0 || ap->ref == 0)
698                         return 0;
699                 ap->ref--;
700                 if (ap->ref == 0) {
701                         nif->nmaddr--;
702                         nif->multicast(nif->arg, addr, 0);
703                 }
704                 if (i < 8 * sizeof(f->maddr)) {
705                         if ((f->maddr[i / 8] & (1 << (i % 8))) != 0)
706                                 f->nmaddr--;
707                         f->maddr[i / 8] &= ~(1 << (i % 8));
708                 }
709         }
710         return 0;
711 }