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