net: add LRO feature
[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                         j += snprintf(p + j, READSTR - j, "\n");
277                         j += snprintf(p + j, READSTR - j, "feat: ");
278                         if (nif->feat & NETF_IPCK)
279                                 j += snprintf(p + j, READSTR - j, "ipck ");
280                         if (nif->feat & NETF_UDPCK)
281                                 j += snprintf(p + j, READSTR - j, "udpck ");
282                         if (nif->feat & NETF_TCPCK)
283                                 j += snprintf(p + j, READSTR - j, "tcppck ");
284                         if (nif->feat & NETF_PADMIN)
285                                 j += snprintf(p + j, READSTR - j, "padmin ");
286                         if (nif->feat & NETF_SG)
287                                 j += snprintf(p + j, READSTR - j, "sg ");
288                         if (nif->feat & NETF_TSO)
289                                 j += snprintf(p + j, READSTR - j, "tso ");
290                         if (nif->feat & NETF_LRO)
291                                 j += snprintf(p + j, READSTR - j, "lro ");
292                         snprintf(p + j, READSTR - j, "\n");
293                         n = readstr(offset, a, n, p);
294                         kfree(p);
295                         return n;
296                 case Naddrqid:
297                         p = kzmalloc(READSTR, 0);
298                         if (p == NULL)
299                                 return 0;
300                         j = 0;
301                         for (i = 0; i < nif->alen; i++)
302                                 j += snprintf(p + j, READSTR - j, "%02.2x", nif->addr[i]);
303                         n = readstr(offset, a, n, p);
304                         kfree(p);
305                         return n;
306                 case Ntypeqid:
307                         f = nif->f[NETID(c->qid.path)];
308                         return readnum(offset, a, n, f->type, NUMSIZE);
309                 case Nifstatqid:
310                         return 0;
311         }
312         error(Ebadarg);
313         return -1;      /* not reached */
314 }
315
316 struct block *netifbread(struct netif *nif, struct chan *c, long n,
317                                                  uint32_t offset)
318 {
319         if ((c->qid.type & QTDIR) || NETTYPE(c->qid.path) != Ndataqid)
320                 return devbread(c, n, offset);
321
322         return qbread(nif->f[NETID(c->qid.path)]->in, n);
323 }
324
325 /*
326  *  make sure this type isn't already in use on this device
327  */
328 static int typeinuse(struct netif *nif, int type)
329 {
330         struct netfile *f, **fp, **efp;
331
332         if (type <= 0)
333                 return 0;
334
335         efp = &nif->f[nif->nfile];
336         for (fp = nif->f; fp < efp; fp++) {
337                 f = *fp;
338                 if (f == 0)
339                         continue;
340                 if (f->type == type)
341                         return 1;
342         }
343         return 0;
344 }
345
346 /*
347  *  the devxxx.c that calls us handles writing data, it knows best
348  */
349 long netifwrite(struct netif *nif, struct chan *c, void *a, long n)
350 {
351         ERRSTACK(1);
352         struct netfile *f;
353         int type;
354         char *p, buf[64];
355         uint8_t binaddr[Nmaxaddr];
356
357         if (NETTYPE(c->qid.path) != Nctlqid)
358                 error(Eperm);
359
360         if (n >= sizeof(buf))
361                 n = sizeof(buf) - 1;
362         memmove(buf, a, n);
363         buf[n] = 0;
364
365         if (waserror()) {
366                 qunlock(&nif->qlock);
367                 nexterror();
368         }
369
370         qlock(&nif->qlock);
371         f = nif->f[NETID(c->qid.path)];
372         if ((p = matchtoken(buf, "connect")) != 0) {
373                 type = strtol(p, 0, 0); /* allows any base, though usually hex */
374                 if (typeinuse(nif, type))
375                         error(Einuse);
376                 f->type = type;
377                 if (f->type < 0)
378                         nif->all++;
379         } else if (matchtoken(buf, "promiscuous")) {
380                 if (f->prom == 0) {
381                         if (nif->prom == 0 && nif->promiscuous != NULL)
382                                 nif->promiscuous(nif->arg, 1);
383                         f->prom = 1;
384                         nif->prom++;
385                 }
386         } else if ((p = matchtoken(buf, "scanbs")) != 0) {
387                 /* scan for base stations */
388                 if (f->scan == 0) {
389                         type = strtol(p, 0, 0); /* allows any base, though usually hex */
390                         if (type < 5)
391                                 type = 5;
392                         if (nif->scanbs != NULL)
393                                 nif->scanbs(nif->arg, type);
394                         f->scan = type;
395                         nif->scan++;
396                 }
397         } else if (matchtoken(buf, "bridge")) {
398                 f->bridge = 1;
399         } else if (matchtoken(buf, "headersonly")) {
400                 f->headersonly = 1;
401         } else if ((p = matchtoken(buf, "addmulti")) != 0) {
402                 if (parseaddr(binaddr, p, nif->alen) < 0)
403                         error("bad address");
404                 p = netmulti(nif, f, binaddr, 1);
405                 if (p)
406                         error(p);
407         } else if ((p = matchtoken(buf, "remmulti")) != 0) {
408                 if (parseaddr(binaddr, p, nif->alen) < 0)
409                         error("bad address");
410                 p = netmulti(nif, f, binaddr, 0);
411                 if (p)
412                         error(p);
413         } else
414                 n = -1;
415         qunlock(&nif->qlock);
416         poperror();
417         return n;
418 }
419
420 int netifwstat(struct netif *nif, struct chan *c, uint8_t * db, int n)
421 {
422         struct dir *dir;
423         struct netfile *f;
424         int m;
425
426         f = nif->f[NETID(c->qid.path)];
427         if (f == 0) {
428                 set_errno(ENOENT);
429                 error(Enonexist);
430         }
431
432         if (netown(f, current->user, OWRITE) < 0)
433                 error(Eperm);
434
435         dir = kzmalloc(sizeof(struct dir) + n, 0);
436         m = convM2D(db, n, &dir[0], (char *)&dir[1]);
437         if (m == 0) {
438                 kfree(dir);
439                 error(Eshortstat);
440         }
441         if (!emptystr(dir[0].uid))
442                 strncpy(f->owner, dir[0].uid, KNAMELEN);
443         if (dir[0].mode != ~0UL)
444                 f->mode = dir[0].mode;
445         kfree(dir);
446         return m;
447 }
448
449 int netifstat(struct netif *nif, struct chan *c, uint8_t * db, int n)
450 {
451         return devstat(c, db, n, (struct dirtab *)nif, 0, netifgen);
452 }
453
454 void netifclose(struct netif *nif, struct chan *c)
455 {
456         struct netfile *f;
457         int t;
458         struct netaddr *ap;
459
460         if ((c->flag & COPEN) == 0)
461                 return;
462
463         t = NETTYPE(c->qid.path);
464         if (t != Ndataqid && t != Nctlqid)
465                 return;
466
467         f = nif->f[NETID(c->qid.path)];
468         qlock(&f->qlock);
469         if (--(f->inuse) == 0) {
470                 if (f->prom) {
471                         qlock(&nif->qlock);
472                         if (--(nif->prom) == 0 && nif->promiscuous != NULL)
473                                 nif->promiscuous(nif->arg, 0);
474                         qunlock(&nif->qlock);
475                         f->prom = 0;
476                 }
477                 if (f->scan) {
478                         qlock(&nif->qlock);
479                         if (--(nif->scan) == 0 && nif->scanbs != NULL)
480                                 nif->scanbs(nif->arg, 0);
481                         qunlock(&nif->qlock);
482                         f->prom = 0;
483                         f->scan = 0;
484                 }
485                 if (f->nmaddr) {
486                         qlock(&nif->qlock);
487                         t = 0;
488                         for (ap = nif->maddr; ap; ap = ap->next) {
489                                 if (f->maddr[t / 8] & (1 << (t % 8)))
490                                         netmulti(nif, f, ap->addr, 0);
491                         }
492                         qunlock(&nif->qlock);
493                         f->nmaddr = 0;
494                 }
495                 if (f->type < 0) {
496                         qlock(&nif->qlock);
497                         --(nif->all);
498                         qunlock(&nif->qlock);
499                 }
500                 f->owner[0] = 0;
501                 f->type = 0;
502                 f->bridge = 0;
503                 f->headersonly = 0;
504                 qclose(f->in);
505         }
506         qunlock(&f->qlock);
507 }
508
509 spinlock_t netlock = SPINLOCK_INITIALIZER;
510
511 static int netown(struct netfile *p, char *o, int omode)
512 {
513         static int access[] = { 0400, 0200, 0600, 0100 };
514         int mode;
515         int t;
516
517         spin_lock(&netlock);
518         if (*p->owner) {
519                 if (strncmp(o, p->owner, KNAMELEN) == 0)        /* User */
520                         mode = p->mode;
521                 else if (strncmp(o, eve, KNAMELEN) == 0)        /* Bootes is group */
522                         mode = p->mode << 3;
523                 else
524                         mode = p->mode << 6;    /* Other */
525
526                 t = access[omode & 3];
527                 if ((t & mode) == t) {
528                         spin_unlock(&netlock);
529                         return 0;
530                 } else {
531                         spin_unlock(&netlock);
532                         return -1;
533                 }
534         }
535         strncpy(p->owner, o, KNAMELEN);
536         p->mode = 0660;
537         spin_unlock(&netlock);
538         return 0;
539 }
540
541 /*
542  *  Increment the reference count of a network device.
543  *  If id < 0, return an unused ether device.
544  */
545 static int openfile(struct netif *nif, int id)
546 {
547         ERRSTACK(1);
548         struct netfile *f, **fp, **efp;
549
550         if (id >= 0) {
551                 f = nif->f[id];
552                 if (f == 0)
553                         error(Enodev);
554                 qlock(&f->qlock);
555                 qreopen(f->in);
556                 f->inuse++;
557                 qunlock(&f->qlock);
558                 return id;
559         }
560
561         qlock(&nif->qlock);
562         if (waserror()) {
563                 qunlock(&nif->qlock);
564                 nexterror();
565         }
566         efp = &nif->f[nif->nfile];
567         for (fp = nif->f; fp < efp; fp++) {
568                 f = *fp;
569                 if (f == 0) {
570                         f = kzmalloc(sizeof(struct netfile), 0);
571                         if (f == 0)
572                                 exhausted("memory");
573                         /* since we lock before netifinit (if we ever call that...) */
574                         qlock_init(&f->qlock);
575                         f->in = qopen(nif->limit, Qmsg, 0, 0);
576                         if (f->in == NULL) {
577                                 kfree(f);
578                                 exhausted("memory");
579                         }
580                         *fp = f;
581                         qlock(&f->qlock);
582                 } else {
583                         qlock(&f->qlock);
584                         if (f->inuse) {
585                                 qunlock(&f->qlock);
586                                 continue;
587                         }
588                 }
589                 f->inuse = 1;
590                 qreopen(f->in);
591                 netown(f, current->user, 0);
592                 qunlock(&f->qlock);
593                 qunlock(&nif->qlock);
594                 poperror();
595                 return fp - nif->f;
596         }
597         error(Enodev);
598         return -1;      /* not reached */
599 }
600
601 /*
602  *  look for a token starting a string,
603  *  return a pointer to first non-space char after it
604  */
605 static char *matchtoken(char *p, char *token)
606 {
607         int n;
608
609         n = strlen(token);
610         if (strncmp(p, token, n))
611                 return 0;
612         p += n;
613         if (*p == 0)
614                 return p;
615         if (*p != ' ' && *p != '\t' && *p != '\n')
616                 return 0;
617         while (*p == ' ' || *p == '\t' || *p == '\n')
618                 p++;
619         return p;
620 }
621
622 static uint32_t hash(uint8_t * a, int len)
623 {
624         uint32_t sum = 0;
625
626         while (len-- > 0)
627                 sum = (sum << 1) + *a++;
628         return sum % Nmhash;
629 }
630
631 int activemulti(struct netif *nif, uint8_t * addr, int alen)
632 {
633         struct netaddr *hp;
634
635         for (hp = nif->mhash[hash(addr, alen)]; hp; hp = hp->hnext)
636                 if (memcmp(addr, hp->addr, alen) == 0) {
637                         if (hp->ref)
638                                 return 1;
639                         else
640                                 break;
641                 }
642         return 0;
643 }
644
645 static int parseaddr(uint8_t * to, char *from, int alen)
646 {
647         char nip[4];
648         char *p;
649         int i;
650
651         p = from;
652         for (i = 0; i < alen; i++) {
653                 if (*p == 0)
654                         return -1;
655                 nip[0] = *p++;
656                 if (*p == 0)
657                         return -1;
658                 nip[1] = *p++;
659                 nip[2] = 0;
660                 to[i] = strtoul(nip, 0, 16);
661                 if (*p == ':')
662                         p++;
663         }
664         return 0;
665 }
666
667 /*
668  *  keep track of multicast addresses
669  */
670 static char *netmulti(struct netif *nif, struct netfile *f, uint8_t * addr,
671                                           int add)
672 {
673         struct netaddr **l, *ap;
674         int i;
675         uint32_t h;
676
677         if (nif->multicast == NULL)
678                 return "interface does not support multicast";
679
680         l = &nif->maddr;
681         i = 0;
682         for (ap = *l; ap; ap = *l) {
683                 if (memcmp(addr, ap->addr, nif->alen) == 0)
684                         break;
685                 i++;
686                 l = &ap->next;
687         }
688
689         if (add) {
690                 if (ap == 0) {
691                         /* TODO: AFAIK, this never gets freed.  if we fix that, we can use a
692                          * kref too (instead of int ap->ref). */
693                         *l = ap = kzmalloc(sizeof(*ap), 0);
694                         memmove(ap->addr, addr, nif->alen);
695                         ap->next = 0;
696                         ap->ref = 1;
697                         h = hash(addr, nif->alen);
698                         ap->hnext = nif->mhash[h];
699                         nif->mhash[h] = ap;
700                 } else {
701                         ap->ref++;
702                 }
703                 if (ap->ref == 1) {
704                         nif->nmaddr++;
705                         nif->multicast(nif->arg, addr, 1);
706                 }
707                 if (i < 8 * sizeof(f->maddr)) {
708                         if ((f->maddr[i / 8] & (1 << (i % 8))) == 0)
709                                 f->nmaddr++;
710                         f->maddr[i / 8] |= 1 << (i % 8);
711                 }
712         } else {
713                 if (ap == 0 || ap->ref == 0)
714                         return 0;
715                 ap->ref--;
716                 if (ap->ref == 0) {
717                         nif->nmaddr--;
718                         nif->multicast(nif->arg, addr, 0);
719                 }
720                 if (i < 8 * sizeof(f->maddr)) {
721                         if ((f->maddr[i / 8] & (1 << (i % 8))) != 0)
722                                 f->nmaddr--;
723                         f->maddr[i / 8] &= ~(1 << (i % 8));
724                 }
725         }
726         return 0;
727 }