Getting more of the IP stack to compile
[akaros.git] / kern / src / net / ipifc.c
index a044a57..02e681f 100644 (file)
@@ -1,12 +1,17 @@
-#include "u.h"
-#include "../port/lib.h"
-#include "mem.h"
-#include "dat.h"
-#include "fns.h"
-#include "../port/error.h"
-
-#include "ip.h"
-#include "ipv6.h"
+// INFERNO
+#include <vfs.h>
+#include <kfs.h>
+#include <slab.h>
+#include <kmalloc.h>
+#include <kref.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <error.h>
+#include <cpio.h>
+#include <pmap.h>
+#include <smp.h>
+#include <ip.h>
 
 #define DPRINT if(0)print
 
@@ -18,7 +23,7 @@ enum {
        QMAX = 64*1024-1,
 };
 
-Medium *media[Maxmedia] =
+struct medium *media[Maxmedia] =
 {
        0
 };
@@ -28,21 +33,21 @@ Medium *media[Maxmedia] =
  */
 struct Ipself
 {
-       uchar   a[IPaddrlen];
-       Ipself  *hnext;         /* next address in the hash table */
-       Iplink  *link;          /* binding twixt Ipself and Ipifc */
-       ulong   expire;
-       uchar   type;           /* type of address */
+       uint8_t a[IPaddrlen];
+       struct Ipself   *hnext;         /* next address in the hash table */
+       struct Iplink   *link;          /* binding twixt Ipself and Ipifc */
+       uint32_t        expire;
+       uint8_t type;           /* type of address */
        int     ref;
-       Ipself  *next;          /* free list */
+       struct Ipself   *next;          /* free list */
 };
 
 struct Ipselftab
 {
-       QLock;
+       qlock_t qlock;
        int     inited;
        int     acceptall;      /* true if an interface has the null address */
-       Ipself  *hash[NHASH];   /* hash chains */
+       struct Ipself   *hash[NHASH];   /* hash chains */
 };
 
 /*
@@ -53,8 +58,8 @@ typedef struct Ipmcast Ipmcast;
 struct Ipmcast
 {
        Ipmcast *next;
-       uchar   ma[IPaddrlen];  /* multicast address */
-       uchar   ia[IPaddrlen];  /* interface address */
+       uint8_t ma[IPaddrlen];  /* multicast address */
+       uint8_t ia[IPaddrlen];  /* interface address */
 };
 
 /* quick hash for ip addresses */
@@ -62,23 +67,26 @@ struct Ipmcast
 
 static char tifc[] = "ifc ";
 
-static void    addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type);
-static void    remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a);
-static char*   ipifcjoinmulti(Ipifc *ifc, char **argv, int argc);
-static char*   ipifcleavemulti(Ipifc *ifc, char **argv, int argc);
-static void    ipifcregisterproxy(Fs*, Ipifc*, uchar*);
-static char*   ipifcremlifc(Ipifc*, Iplifc*);
+static void    addselfcache(struct Fs *f, struct Ipifc *ifc, struct Iplifc *lifc,
+                               uint8_t *a, int type);
+static void    remselfcache(struct Fs *f,
+                               struct Ipifc *ifc,
+                               struct Iplifc *lifc, uint8_t *a);
+static char*   ipifcjoinmulti(struct Ipifc *ifc, char **argv, int argc);
+static char*   ipifcleavemulti(struct Ipifc *ifc, char **argv, int argc);
+static void    ipifcregisterproxy(struct Fs*, struct Ipifc*, uint8_t *unused_uint8_p_t);
+static char*   ipifcremlifc(struct Ipifc*, struct Iplifc*);
 
 /*
  *  link in a new medium
  */
 void
-addipmedium(Medium *med)
+addipmedium(struct medium *med)
 {
        int i;
 
-       for(i = 0; i < nelem(media)-1; i++)
-               if(media[i] == nil){
+       for(i = 0; i < ARRAY_SIZE(media)-1; i++)
+               if(media[i] == NULL){
                        media[i] = med;
                        break;
                }
@@ -87,12 +95,12 @@ addipmedium(Medium *med)
 /*
  *  find the medium with this name
  */
-Medium*
+struct medium*
 ipfindmedium(char *name)
 {
-       Medium **mp;
+       struct medium **mp;
 
-       for(mp = media; *mp != nil; mp++)
+       for(mp = media; *mp != NULL; mp++)
                if(strcmp((*mp)->name, name) == 0)
                        break;
        return *mp;
@@ -103,28 +111,29 @@ ipfindmedium(char *name)
  *  called with c locked
  */
 static char*
-ipifcbind(Conv *c, char **argv, int argc)
+ipifcbind(struct conv *c, char **argv, int argc)
 {
-       Ipifc *ifc;
-       Medium *m;
+       ERRSTACK(2);
+       struct Ipifc *ifc;
+       struct medium *m;
 
        if(argc < 2)
                return Ebadarg;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
 
        /* bind the device to the interface */
        m = ipfindmedium(argv[1]);
-       if(m == nil)
+       if(m == NULL)
                return "unknown interface type";
 
-       wlock(ifc);
-       if(ifc->m != nil){
-               wunlock(ifc);
+       wlock(&ifc->rwlock);
+       if(ifc->m != NULL){
+               wunlock(&ifc->rwlock);
                return "interface already bound";       
        }
        if(waserror()){
-               wunlock(ifc);
+               wunlock(&ifc->rwlock);
                nexterror();
        }
 
@@ -135,7 +144,7 @@ ipifcbind(Conv *c, char **argv, int argc)
        if(argc > 2)
                strncpy(ifc->dev, argv[2], sizeof(ifc->dev));
        else
-               snprint(ifc->dev, sizeof ifc->dev, "%s%d", m->name, c->x);
+               snprintf(ifc->dev, sizeof ifc->dev, "%s%d", m->name, c->x);
        ifc->dev[sizeof(ifc->dev)-1] = 0;
 
        /* set up parameters */
@@ -162,10 +171,10 @@ ipifcbind(Conv *c, char **argv, int argc)
        qreopen(c->eq);
        qreopen(c->sq);
 
-       wunlock(ifc);
+       wunlock(&ifc->rwlock);
        poperror();
 
-       return nil;
+       return NULL;
 }
 
 /*
@@ -173,26 +182,27 @@ ipifcbind(Conv *c, char **argv, int argc)
  *  called with ifc->conv closed
  */
 static char*
-ipifcunbind(Ipifc *ifc)
+ipifcunbind(struct Ipifc *ifc)
 {
+       ERRSTACK(2);
        char *err;
 
        if(waserror()){
-               wunlock(ifc);
+               wunlock(&ifc->rwlock);
                nexterror();
        }
-       wlock(ifc);
+       wlock(&ifc->rwlock);
 
        /* dissociate routes */
-       if(ifc->m != nil && ifc->m->unbindonclose == 0)
+       if(ifc->m != NULL && ifc->m->unbindonclose == 0)
                ifc->conv->inuse--;
        ifc->ifcid++;
 
        /* disassociate device */
-       if(ifc->m != nil && ifc->m->unbind)
+       if(ifc->m != NULL && ifc->m->unbind)
                (*ifc->m->unbind)(ifc);
        memset(ifc->dev, 0, sizeof(ifc->dev));
-       ifc->arg = nil;
+       ifc->arg = NULL;
        ifc->reassemble = 0;
 
        /* close queues to stop queuing of packets */
@@ -207,10 +217,10 @@ ipifcunbind(Ipifc *ifc)
                        error(err);
        }
 
-       ifc->m = nil;
-       wunlock(ifc);
+       ifc->m = NULL;
+       wunlock(&ifc->rwlock);
        poperror();
-       return nil;
+       return NULL;
 }
 
 
@@ -221,62 +231,62 @@ char slineformat[] = "    %-40I %-10M %-40I %-12lud %-12lud\n";
 
 
 static int
-ipifcstate(Conv *c, char *state, int n)
+ipifcstate(struct conv *c, char *state, int n)
 {
-       Ipifc *ifc;
-       Iplifc *lifc;
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
        int m;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
 
-       m = snprint(state, n, sfixedformat,
+       m = snprintf(state, n, sfixedformat,
                ifc->dev, ifc->maxtu, ifc->sendra6, ifc->recvra6,
-               ifc->rp.mflag, ifc->rp.oflag, ifc->rp.maxraint,
-               ifc->rp.minraint, ifc->rp.linkmtu, ifc->rp.reachtime,
-               ifc->rp.rxmitra, ifc->rp.ttl, ifc->rp.routerlt,
-               ifc->in, ifc->out, ifc->inerr, ifc->outerr);
+                    ifc->rp.mflag, ifc->rp.oflag, ifc->rp.maxraint,
+                    ifc->rp.minraint, ifc->rp.linkmtu, ifc->rp.reachtime,
+                    ifc->rp.rxmitra, ifc->rp.ttl, ifc->rp.routerlt,
+                    ifc->in, ifc->out, ifc->inerr, ifc->outerr);
 
-       rlock(ifc);
+       rlock(&ifc->rwlock);
        for(lifc = ifc->lifc; lifc && n > m; lifc = lifc->next)
-               m += snprint(state+m, n - m, slineformat,
+               m += snprintf(state+m, n - m, slineformat,
                        lifc->local, lifc->mask, lifc->remote,
                        lifc->validlt, lifc->preflt);
-       if(ifc->lifc == nil)
-               m += snprint(state+m, n - m, "\n");
-       runlock(ifc);
+       if(ifc->lifc == NULL)
+               m += snprintf(state+m, n - m, "\n");
+       runlock(&ifc->rwlock);
        return m;
 }
 
 static int
-ipifclocal(Conv *c, char *state, int n)
+ipifclocal(struct conv *c, char *state, int n)
 {
-       Ipifc *ifc;
-       Iplifc *lifc;
-       Iplink *link;
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
+       struct Iplink *link;
        int m;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
 
        m = 0;
 
-       rlock(ifc);
+       rlock(&ifc->rwlock);
        for(lifc = ifc->lifc; lifc; lifc = lifc->next){
-               m += snprint(state+m, n - m, "%-40.40I ->", lifc->local);
+               m += snprintf(state+m, n - m, "%-40.40I ->", lifc->local);
                for(link = lifc->link; link; link = link->lifclink)
-                       m += snprint(state+m, n - m, " %-40.40I", link->self->a);
-               m += snprint(state+m, n - m, "\n");
+                       m += snprintf(state+m, n - m, " %-40.40I", link->self->a);
+               m += snprintf(state+m, n - m, "\n");
        }
-       runlock(ifc);
+       runlock(&ifc->rwlock);
        return m;
 }
 
 static int
-ipifcinuse(Conv *c)
+ipifcinuse(struct conv *c)
 {
-       Ipifc *ifc;
+       struct Ipifc *ifc;
 
-       ifc = (Ipifc*)c->ptcl;
-       return ifc->m != nil;
+       ifc = (struct Ipifc*)c->ptcl;
+       return ifc->m != NULL;
 }
 
 /*
@@ -285,28 +295,29 @@ ipifcinuse(Conv *c)
 static void
 ipifckick(void *x)
 {
-       Conv *c = x;
-       Block *bp;
-       Ipifc *ifc;
+       ERRSTACK(2);
+       struct conv *c = x;
+       struct block *bp;
+       struct Ipifc *ifc;
 
        bp = qget(c->wq);
-       if(bp == nil)
+       if(bp == NULL)
                return;
 
-       ifc = (Ipifc*)c->ptcl;
-       if(!canrlock(ifc)){
+       ifc = (struct Ipifc*)c->ptcl;
+       if(!canrlock(&ifc->rwlock)){
                freeb(bp);
                return;
        }
        if(waserror()){
-               runlock(ifc);
+               runlock(&ifc->rwlock);
                nexterror();
        }
-       if(ifc->m == nil || ifc->m->pktin == nil)
+       if(ifc->m == NULL || ifc->m->pktin == NULL)
                freeb(bp);
        else
                (*ifc->m->pktin)(c->p->f, ifc, bp);
-       runlock(ifc);
+       runlock(&ifc->rwlock);
        poperror();
 }
 
@@ -314,17 +325,17 @@ ipifckick(void *x)
  *  called when a new ipifc structure is created
  */
 static void
-ipifccreate(Conv *c)
+ipifccreate(struct conv *c)
 {
-       Ipifc *ifc;
+       struct Ipifc *ifc;
 
        c->rq = qopen(QMAX, 0, 0, 0);
        c->sq = qopen(2*QMAX, 0, 0, 0);
        c->wq = qopen(QMAX, Qkick, ipifckick, c);
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
        ifc->conv = c;
        ifc->unbinding = 0;
-       ifc->m = nil;
+       ifc->m = NULL;
        ifc->reassemble = 0;
 }
 
@@ -333,14 +344,14 @@ ipifccreate(Conv *c)
  *  called with c locked, we must unlock
  */
 static void
-ipifcclose(Conv *c)
+ipifcclose(struct conv *c)
 {
-       Ipifc *ifc;
-       Medium *m;
+       struct Ipifc *ifc;
+       struct medium *m;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
        m = ifc->m;
-       if(m != nil && m->unbindonclose)
+       if(m != NULL && m->unbindonclose)
                ipifcunbind(ifc);
 }
 
@@ -348,35 +359,36 @@ ipifcclose(Conv *c)
  *  change an interface's mtu
  */
 char*
-ipifcsetmtu(Ipifc *ifc, char **argv, int argc)
+ipifcsetmtu(struct Ipifc *ifc, char **argv, int argc)
 {
        int mtu;
 
        if(argc < 2)
                return Ebadarg;
-       if(ifc->m == nil)
+       if(ifc->m == NULL)
                return Ebadarg;
        mtu = strtoul(argv[1], 0, 0);
        if(mtu < ifc->m->mintu || mtu > ifc->m->maxtu)
                return Ebadarg;
        ifc->maxtu = mtu;
-       return nil;
+       return NULL;
 }
 
 /*
  *  add an address to an interface.
  */
 char*
-ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
+ipifcadd(struct Ipifc *ifc, char **argv, int argc, int tentative,
+        struct Iplifc *lifcp)
 {
-       uchar ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
-       uchar bcast[IPaddrlen], net[IPaddrlen];
-       Iplifc *lifc, **l;
+       uint8_t ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
+       uint8_t bcast[IPaddrlen], net[IPaddrlen];
+       struct Iplifc *lifc, **l;
        int i, type, mtu;
-       Fs *f;
+       struct Fs *f;
        int sendnbrdisc = 0;
 
-       if(ifc->m == nil)
+       if(ifc->m == NULL)
                return "ipifc not yet bound to device";
 
        f = ifc->conv->p->f;
@@ -419,14 +431,14 @@ ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
        }
        if(isv4(ip))
                tentative = 0;
-       wlock(ifc);
+       wlock(&ifc->rwlock);
 
        /* ignore if this is already a local address for this ifc */
        for(lifc = ifc->lifc; lifc; lifc = lifc->next) {
                if(ipcmp(lifc->local, ip) == 0) {
                        if(lifc->tentative != tentative)
                                lifc->tentative = tentative;
-                       if(lifcp != nil) {
+                       if(lifcp != NULL) {
                                lifc->onlink = lifcp->onlink;
                                lifc->autoflag = lifcp->autoflag;
                                lifc->validlt = lifcp->validlt;
@@ -438,13 +450,13 @@ ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
        }
 
        /* add the address to the list of logical ifc's for this ifc */
-       lifc = smalloc(sizeof(Iplifc));
+       lifc = kzmalloc(sizeof(struct Iplifc), 0);
        ipmove(lifc->local, ip);
        ipmove(lifc->mask, mask);
        ipmove(lifc->remote, rem);
        ipmove(lifc->net, net);
        lifc->tentative = tentative;
-       if(lifcp != nil) {
+       if(lifcp != NULL) {
                lifc->onlink = lifcp->onlink;
                lifc->autoflag = lifcp->autoflag;
                lifc->validlt = lifcp->validlt;
@@ -458,7 +470,7 @@ ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
                lifc->preflt = 0xffffffff;
                lifc->origint = NOW / 10^3;
        }
-       lifc->next = nil;
+       lifc->next = NULL;
 
        for(l = &ifc->lifc; *l; l = &(*l)->next)
                ;
@@ -530,14 +542,14 @@ ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
        }
 
        /* register the address on this network for address resolution */
-       if(isv4(ip) && ifc->m->areg != nil)
+       if(isv4(ip) && ifc->m->areg != NULL)
                (*ifc->m->areg)(ifc, ip);
 
 out:
-       wunlock(ifc);
+       wunlock(&ifc->rwlock);
        if(tentative && sendnbrdisc)
                icmpns(f, 0, SRC_UNSPEC, ip, TARG_MULTI, ifc->mac);
-       return nil;
+       return NULL;
 }
 
 /*
@@ -545,10 +557,10 @@ out:
  *  always called with ifc wlock'd
  */
 static char*
-ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
+ipifcremlifc(struct Ipifc *ifc, struct Iplifc *lifc)
 {
-       Iplifc **l;
-       Fs *f;
+       struct Iplifc **l;
+       struct Fs *f;
 
        f = ifc->conv->p->f;
 
@@ -557,9 +569,9 @@ ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
         *  for pt to pt we actually specify the remote address as the
         *  addresss to remove.
         */
-       for(l = &ifc->lifc; *l != nil && *l != lifc; l = &(*l)->next)
+       for(l = &ifc->lifc; *l != NULL && *l != lifc; l = &(*l)->next)
                ;
-       if(*l == nil)
+       if(*l == NULL)
                return "address not on this interface";
        *l = lifc->next;
 
@@ -580,8 +592,8 @@ ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
                        v6delroute(f, v6allnodesL, v6allnodesLmask, 1);
        }
 
-       free(lifc);
-       return nil;
+       kfree(lifc);
+       return NULL;
 
 }
 
@@ -590,12 +602,12 @@ ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
  *  called with c locked
  */
 char*
-ipifcrem(Ipifc *ifc, char **argv, int argc)
+ipifcrem(struct Ipifc *ifc, char **argv, int argc)
 {
-       uchar ip[IPaddrlen];
-       uchar mask[IPaddrlen];
-       uchar rem[IPaddrlen];
-       Iplifc *lifc;
+       uint8_t ip[IPaddrlen];
+       uint8_t mask[IPaddrlen];
+       uint8_t rem[IPaddrlen];
+       struct Iplifc *lifc;
        char *rv;
 
        if(argc < 3)
@@ -608,14 +620,14 @@ ipifcrem(Ipifc *ifc, char **argv, int argc)
        else
                parseip(rem, argv[3]);
 
-       wlock(ifc);
+       wlock(&ifc->rwlock);
 
        /*
         *  find address on this interface and remove from chain.
         *  for pt to pt we actually specify the remote address as the
         *  addresss to remove.
         */
-       for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next) {
+       for(lifc = ifc->lifc; lifc != NULL; lifc = lifc->next) {
                if (memcmp(ip, lifc->local, IPaddrlen) == 0
                && memcmp(mask, lifc->mask, IPaddrlen) == 0
                && memcmp(rem, lifc->remote, IPaddrlen) == 0)
@@ -623,7 +635,7 @@ ipifcrem(Ipifc *ifc, char **argv, int argc)
        }
 
        rv = ipifcremlifc(ifc, lifc);
-       wunlock(ifc);
+       wunlock(&ifc->rwlock);
        return rv;
 }
 
@@ -632,40 +644,41 @@ ipifcrem(Ipifc *ifc, char **argv, int argc)
  * TRIP linecards
  */
 void
-ipifcaddroute(Fs *f, int vers, uchar *addr, uchar *mask, uchar *gate, int type)
+ipifcaddroute(struct Fs *f, int vers, uint8_t *addr, uint8_t *mask,
+             uint8_t *gate, int type)
 {
-       Medium *m;
-       Conv **cp, **e;
-       Ipifc *ifc;
+       struct medium *m;
+       struct conv **cp, **e;
+       struct Ipifc *ifc;
 
        e = &f->ipifc->conv[f->ipifc->nc];
        for(cp = f->ipifc->conv; cp < e; cp++){
-               if(*cp != nil) {
-                       ifc = (Ipifc*)(*cp)->ptcl;
+               if(*cp != NULL) {
+                       ifc = (struct Ipifc*)(*cp)->ptcl;
                        m = ifc->m;
-                       if(m == nil)
+                       if(m == NULL)
                                continue;
-                       if(m->addroute != nil)
+                       if(m->addroute != NULL)
                                m->addroute(ifc, vers, addr, mask, gate, type);
                }
        }
 }
 
 void
-ipifcremroute(Fs *f, int vers, uchar *addr, uchar *mask)
+ipifcremroute(struct Fs *f, int vers, uint8_t *addr, uint8_t *mask)
 {
-       Medium *m;
-       Conv **cp, **e;
-       Ipifc *ifc;
+       struct medium *m;
+       struct conv **cp, **e;
+       struct Ipifc *ifc;
 
        e = &f->ipifc->conv[f->ipifc->nc];
        for(cp = f->ipifc->conv; cp < e; cp++){
-               if(*cp != nil) {
-                       ifc = (Ipifc*)(*cp)->ptcl;
+               if(*cp != NULL) {
+                       ifc = (struct Ipifc*)(*cp)->ptcl;
                        m = ifc->m;
-                       if(m == nil)
+                       if(m == NULL)
                                continue;
-                       if(m->remroute != nil)
+                       if(m->remroute != NULL)
                                m->remroute(ifc, vers, addr, mask);
                }
        }
@@ -677,40 +690,41 @@ ipifcremroute(Fs *f, int vers, uchar *addr, uchar *mask)
  *  and add a new one.
  */
 static char*
-ipifcconnect(Conv* c, char **argv, int argc)
+ipifcconnect(struct conv* c, char **argv, int argc)
 {
+       ERRSTACK(2);
        char *err;
-       Ipifc *ifc;
+       struct Ipifc *ifc;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
 
-       if(ifc->m == nil)
+       if(ifc->m == NULL)
                 return "ipifc not yet bound to device";
 
        if(waserror()){
-               wunlock(ifc);
+               wunlock(&ifc->rwlock);
                nexterror();
        }
-       wlock(ifc);
+       wlock(&ifc->rwlock);
        while(ifc->lifc){
                err = ipifcremlifc(ifc, ifc->lifc);
                if(err)
                        error(err);
        }
-       wunlock(ifc);
+       wunlock(&ifc->rwlock);
        poperror();
 
-       err = ipifcadd(ifc, argv, argc, 0, nil);
+       err = ipifcadd(ifc, argv, argc, 0, NULL);
        if(err)
                return err;
 
-       Fsconnected(c, nil);
+       Fsconnected(c, NULL);
 
-       return nil;
+       return NULL;
 }
 
 char*
-ipifcsetpar6(Ipifc *ifc, char **argv, int argc)
+ipifcsetpar6(struct Ipifc *ifc, char **argv, int argc)
 {
        int i, argsleft, vmax = ifc->rp.maxraint, vmin = ifc->rp.minraint;
 
@@ -757,11 +771,11 @@ ipifcsetpar6(Ipifc *ifc, char **argv, int argc)
                return Ebadarg;
        }
 
-       return nil;
+       return NULL;
 }
 
 char*
-ipifcsendra6(Ipifc *ifc, char **argv, int argc)
+ipifcsendra6(struct Ipifc *ifc, char **argv, int argc)
 {
        int i;
        
@@ -769,11 +783,11 @@ ipifcsendra6(Ipifc *ifc, char **argv, int argc)
        if(argc > 1)
                i = atoi(argv[1]);
        ifc->sendra6 = (i!=0);
-       return nil;
+       return NULL;
 }
 
 char*
-ipifcrecvra6(Ipifc *ifc, char **argv, int argc)
+ipifcrecvra6(struct Ipifc *ifc, char **argv, int argc)
 {
        int i;
        
@@ -781,7 +795,7 @@ ipifcrecvra6(Ipifc *ifc, char **argv, int argc)
        if(argc > 1)
                i = atoi(argv[1]);
        ifc->recvra6 = (i!=0);  
-       return nil;
+       return NULL;
 }
 
 /*
@@ -789,18 +803,18 @@ ipifcrecvra6(Ipifc *ifc, char **argv, int argc)
  *  called with c locked.
  */
 static char*
-ipifcctl(Conv* c, char**argv, int argc)
+ipifcctl(struct conv* c, char**argv, int argc)
 {
-       Ipifc *ifc;
+       struct Ipifc *ifc;
        int i;
 
-       ifc = (Ipifc*)c->ptcl;
+       ifc = (struct Ipifc*)c->ptcl;
        if(strcmp(argv[0], "add") == 0)
-               return ipifcadd(ifc, argv, argc, 0, nil);
+               return ipifcadd(ifc, argv, argc, 0, NULL);
        else if(strcmp(argv[0], "bootp") == 0)
                return bootp(ifc);
        else if(strcmp(argv[0], "try") == 0)
-               return ipifcadd(ifc, argv, argc, 1, nil);
+               return ipifcadd(ifc, argv, argc, 1, NULL);
        else if(strcmp(argv[0], "remove") == 0)
                return ipifcrem(ifc, argv, argc);
        else if(strcmp(argv[0], "unbind") == 0)
@@ -813,14 +827,14 @@ ipifcctl(Conv* c, char**argv, int argc)
                return ipifcsetmtu(ifc, argv, argc);
        else if(strcmp(argv[0], "reassemble") == 0){
                ifc->reassemble = 1;
-               return nil;
+               return NULL;
        }
        else if(strcmp(argv[0], "iprouting") == 0){
                i = 1;
                if(argc > 1)
                        i = atoi(argv[1]);
                iprouting(c->p->f, i);
-               return nil;
+               return NULL;
        }
        else if(strcmp(argv[0], "addpref6") == 0)
                return ipifcaddpref6(ifc, argv, argc);
@@ -833,36 +847,36 @@ ipifcctl(Conv* c, char**argv, int argc)
        return "unsupported ctl";
 }
 
-ipifcstats(Proto *ipifc, char *buf, int len)
+int ipifcstats(struct Proto *ipifc, char *buf, int len)
 {
        return ipstats(ipifc->f, buf, len);
 }
 
 void
-ipifcinit(Fs *f)
+ipifcinit(struct Fs *f)
 {
-       Proto *ipifc;
+       struct Proto *ipifc;
 
-       ipifc = smalloc(sizeof(Proto));
+       ipifc = kzmalloc(sizeof(struct Proto), 0);
        ipifc->name = "ipifc";
        ipifc->connect = ipifcconnect;
-       ipifc->announce = nil;
+       ipifc->announce = NULL;
        ipifc->bind = ipifcbind;
        ipifc->state = ipifcstate;
        ipifc->create = ipifccreate;
        ipifc->close = ipifcclose;
-       ipifc->rcv = nil;
+       ipifc->rcv = NULL;
        ipifc->ctl = ipifcctl;
-       ipifc->advise = nil;
+       ipifc->advise = NULL;
        ipifc->stats = ipifcstats;
        ipifc->inuse = ipifcinuse;
        ipifc->local = ipifclocal;
        ipifc->ipproto = -1;
        ipifc->nc = Maxmedia;
-       ipifc->ptclsize = sizeof(Ipifc);
+       ipifc->ptclsize = sizeof(struct Ipifc);
 
        f->ipifc = ipifc;                       /* hack for ipifcremroute, findipifc, ... */
-       f->self = smalloc(sizeof(Ipselftab));   /* hack for ipforme */
+       f->self = kzmalloc(sizeof(struct Ipselftab), 0);        /* hack for ipforme */
 
        Fsproto(f, ipifc);
 }
@@ -872,13 +886,14 @@ ipifcinit(Fs *f)
  *     called with c locked
  */
 static void
-addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
+addselfcache(struct Fs *f, struct Ipifc *ifc,
+            struct Iplifc *lifc, uint8_t *a, int type)
 {
-       Ipself *p;
-       Iplink *lp;
+       struct Ipself *p;
+       struct Iplink *lp;
        int h;
 
-       qlock(f->self);
+       qlock(&f->self->qlock);
 
        /* see if the address already exists */
        h = hashipa(a);
@@ -887,8 +902,8 @@ addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
                        break;
 
        /* allocate a local address and add to hash chain */
-       if(p == nil){
-               p = smalloc(sizeof(*p));
+       if(p == NULL){
+               p = kzmalloc(sizeof(*p), 0);
                ipmove(p->a, a);
                p->type = type;
                p->next = f->self->hash[h];
@@ -905,9 +920,9 @@ addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
                        break;
 
        /* allocate a lifc-to-local link and link to both */
-       if(lp == nil){
-               lp = smalloc(sizeof(*lp));
-               lp->ref = 1;
+       if(lp == NULL){
+               lp = kzmalloc(sizeof(*lp), 0);
+               kref_init(&lp->ref, fake_release, 1);
                lp->lifc = lifc;
                lp->self = p;
                lp->selflink = p->link;
@@ -921,13 +936,13 @@ addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
                else
                        v6addroute(f, tifc, a, IPallbits, a, type);
 
-               if((type & Rmulti) && ifc->m->addmulti != nil)
+               if((type & Rmulti) && ifc->m->addmulti != NULL)
                        (*ifc->m->addmulti)(ifc, a, lifc->local);
        } else {
-               lp->ref++;
+               kref_get(&lp->ref, 1);
        }
 
-       qunlock(f->self);
+       qunlock(&f->self->qlock);
 }
 
 /*
@@ -936,45 +951,45 @@ addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
  *  just put them aside for a while before freeing them.
  *     called with f->self locked
  */
-static Iplink *freeiplink;
-static Ipself *freeipself;
+static struct Iplink *freeiplink;
+static struct Ipself *freeipself;
 
 static void
-iplinkfree(Iplink *p)
+iplinkfree(struct Iplink *p)
 {
-       Iplink **l, *np;
-       ulong now = NOW;
+       struct Iplink **l, *np;
+       uint32_t now = NOW;
 
        l = &freeiplink;
        for(np = *l; np; np = *l){
                if(np->expire > now){
                        *l = np->next;
-                       free(np);
+                       kfree(np);
                        continue;
                }
                l = &np->next;
        }
        p->expire = now + 5000;         /* give other threads 5 secs to get out */
-       p->next = nil;
+       p->next = NULL;
        *l = p;
 }
 static void
-ipselffree(Ipself *p)
+ipselffree(struct Ipself *p)
 {
-       Ipself **l, *np;
-       ulong now = NOW;
+       struct Ipself **l, *np;
+       uint32_t now = NOW;
 
        l = &freeipself;
        for(np = *l; np; np = *l){
                if(np->expire > now){
                        *l = np->next;
-                       free(np);
+                       kfree(np);
                        continue;
                }
                l = &np->next;
        }
        p->expire = now + 5000;         /* give other threads 5 secs to get out */
-       p->next = nil;
+       p->next = NULL;
        *l = p;
 }
 
@@ -984,12 +999,12 @@ ipselffree(Ipself *p)
  *     called with c locked
  */
 static void
-remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
+remselfcache(struct Fs *f, struct Ipifc *ifc, struct Iplifc *lifc, uint8_t *a)
 {
-       Ipself *p, **l;
-       Iplink *link, **l_self, **l_lifc;
+       struct Ipself *p, **l;
+       struct Iplink *link, **l_self, **l_lifc;
 
-       qlock(f->self);
+       qlock(&f->self->qlock);
 
        /* find the unique selftab entry */
        l = &f->self->hash[hashipa(a)];
@@ -999,7 +1014,7 @@ remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
                l = &p->next;
        }
 
-       if(p == nil)
+       if(p == NULL)
                goto out;
 
        /*
@@ -1013,7 +1028,7 @@ remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
                l_lifc = &link->lifclink;
        }
 
-       if(link == nil)
+       if(link == NULL)
                goto out;
 
        /*
@@ -1027,13 +1042,13 @@ remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
                l_self = &link->selflink;
        }
 
-       if(link == nil)
+       if(link == NULL)
                panic("remselfcache");
 
-       if(--(link->ref) != 0)
+       if(kref_refcnt(&link->ref) > 1)
                goto out;
 
-       if((p->type & Rmulti) && ifc->m->remmulti != nil)
+       if((p->type & Rmulti) && ifc->m->remmulti != NULL)
                (*ifc->m->remmulti)(ifc, a, lifc->local);
 
        /* ref == 0, remove from both chains and free the link */
@@ -1041,7 +1056,7 @@ remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
        *l_self = link->selflink;
        iplinkfree(link);
 
-       if(p->link != nil)
+       if(p->link != NULL)
                goto out;
 
        /* remove from routing table */
@@ -1059,7 +1074,7 @@ remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
                f->self->acceptall = 0;
 
 out:
-       qunlock(f->self);
+       qunlock(&f->self->qlock);
 }
 
 static char *stformat = "%-44.44I %2.2d %4.4s\n";
@@ -1069,37 +1084,37 @@ enum
 };
 
 long
-ipselftabread(Fs *f, char *cp, ulong offset, int n)
+ipselftabread(struct Fs *f, char *cp, uint32_t offset, int n)
 {
        int i, m, nifc, off;
-       Ipself *p;
-       Iplink *link;
+       struct Ipself *p;
+       struct Iplink *link;
        char state[8];
 
        m = 0;
        off = offset;
-       qlock(f->self);
+       qlock(&f->self->qlock);
        for(i = 0; i < NHASH && m < n; i++){
-               for(p = f->self->hash[i]; p != nil && m < n; p = p->next){
+               for(p = f->self->hash[i]; p != NULL && m < n; p = p->next){
                        nifc = 0;
                        for(link = p->link; link; link = link->selflink)
                                nifc++;
                        routetype(p->type, state);
-                       m += snprint(cp + m, n - m, stformat, p->a, nifc, state);
+                       m += snprintf(cp + m, n - m, stformat, p->a, nifc, state);
                        if(off > 0){
                                off -= m;
                                m = 0;
                        }
                }
        }
-       qunlock(f->self);
+       qunlock(&f->self->qlock);
        return m;
 }
 
 int
-iptentative(Fs *f, uchar *addr)
+iptentative(struct Fs *f, uint8_t *addr)
 {
-       Ipself *p;
+       struct Ipself *p;
 
        p = f->self->hash[hashipa(addr)];
        for(; p; p = p->next){
@@ -1118,9 +1133,9 @@ iptentative(Fs *f, uchar *addr)
  *     Rmcast
  */
 int
-ipforme(Fs *f, uchar *addr)
+ipforme(struct Fs *f, uint8_t *addr)
 {
-       Ipself *p;
+       struct Ipself *p;
 
        p = f->self->hash[hashipa(addr)];
        for(; p; p = p->next){
@@ -1137,18 +1152,18 @@ ipforme(Fs *f, uchar *addr)
 
 /*
  *  find the ifc on same net as the remote system.  If none,
- *  return nil.
+ *  return NULL.
  */
-Ipifc*
-findipifc(Fs *f, uchar *remote, int type)
+struct Ipifc*
+findipifc(struct Fs *f, uint8_t *remote, int type)
 {
-       Ipifc *ifc, *x;
-       Iplifc *lifc;
-       Conv **cp, **e;
-       uchar gnet[IPaddrlen];
-       uchar xmask[IPaddrlen];
+       struct Ipifc *ifc, *x;
+       struct Iplifc *lifc;
+       struct conv **cp, **e;
+       uint8_t gnet[IPaddrlen];
+       uint8_t xmask[IPaddrlen];
 
-       x = nil; memset(xmask, 0, IPaddrlen);
+       x = NULL; memset(xmask, 0, IPaddrlen);
 
        /* find most specific match */
        e = &f->ipifc->conv[f->ipifc->nc];
@@ -1156,19 +1171,19 @@ findipifc(Fs *f, uchar *remote, int type)
                if(*cp == 0)
                        continue;
 
-               ifc = (Ipifc*)(*cp)->ptcl;
+               ifc = (struct Ipifc*)(*cp)->ptcl;
 
                for(lifc = ifc->lifc; lifc; lifc = lifc->next){
                        maskip(remote, lifc->mask, gnet);
                        if(ipcmp(gnet, lifc->net) == 0){
-                               if(x == nil || ipcmp(lifc->mask, xmask) > 0){
+                               if(x == NULL || ipcmp(lifc->mask, xmask) > 0){
                                        x = ifc;
                                        ipmove(xmask, lifc->mask);
                                }
                        }
                }
        }
-       if(x != nil)
+       if(x != NULL)
                return x;
 
        /* for now for broadcast and multicast, just use first interface */
@@ -1176,13 +1191,13 @@ findipifc(Fs *f, uchar *remote, int type)
                for(cp = f->ipifc->conv; cp < e; cp++){
                        if(*cp == 0)
                                continue;
-                       ifc = (Ipifc*)(*cp)->ptcl;
-                       if(ifc->lifc != nil)
+                       ifc = (struct Ipifc*)(*cp)->ptcl;
+                       if(ifc->lifc != NULL)
                                return ifc;
                }
        }
                
-       return nil;
+       return NULL;
 }
 
 enum {
@@ -1195,7 +1210,7 @@ enum {
 };
 
 int
-v6addrtype(uchar *addr)
+v6addrtype(uint8_t *addr)
 {
        if(isv6global(addr))
                return globalv6;
@@ -1211,11 +1226,11 @@ v6addrtype(uchar *addr)
 #define v6addrcurr(lifc) (( (lifc)->origint + (lifc)->preflt >= (NOW/10^3) ) || ( (lifc)->preflt == 0xffffffff ))
 
 static void
-findprimaryipv6(Fs *f, uchar *local)
+findprimaryipv6(struct Fs *f, uint8_t *local)
 {
-       Conv **cp, **e;
-       Ipifc *ifc;
-       Iplifc *lifc;
+       struct conv **cp, **e;
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
        int atype, atypel;
 
        ipmove(local, v6Unspecified);
@@ -1228,7 +1243,7 @@ findprimaryipv6(Fs *f, uchar *local)
        for(cp = f->ipifc->conv; cp < e; cp++){
                if(*cp == 0)
                        continue;
-               ifc = (Ipifc*)(*cp)->ptcl;
+               ifc = (struct Ipifc*)(*cp)->ptcl;
                for(lifc = ifc->lifc; lifc; lifc = lifc->next){
                        atypel = v6addrtype(lifc->local);
                        if(atypel > atype)
@@ -1246,19 +1261,19 @@ findprimaryipv6(Fs *f, uchar *local)
  *  returns first ip address configured
  */
 static void
-findprimaryipv4(Fs *f, uchar *local)
+findprimaryipv4(struct Fs *f, uint8_t *local)
 {
-       Conv **cp, **e;
-       Ipifc *ifc;
-       Iplifc *lifc;
+       struct conv **cp, **e;
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
 
        /* find first ifc local address */
        e = &f->ipifc->conv[f->ipifc->nc];
        for(cp = f->ipifc->conv; cp < e; cp++){
                if(*cp == 0)
                        continue;
-               ifc = (Ipifc*)(*cp)->ptcl;
-               if((lifc = ifc->lifc) != nil){
+               ifc = (struct Ipifc*)(*cp)->ptcl;
+               if((lifc = ifc->lifc) != NULL){
                        ipmove(local, lifc->local);
                        return;
                }
@@ -1270,25 +1285,23 @@ findprimaryipv4(Fs *f, uchar *local)
  *  local and return the ifc for that address
  */
 void
-findlocalip(Fs *f, uchar *local, uchar *remote)
+findlocalip(struct Fs *f, uint8_t *local, uint8_t *remote)
 {
-       Ipifc *ifc;
-       Iplifc *lifc;
-       Route *r;
-       uchar gate[IPaddrlen];
-       uchar gnet[IPaddrlen];
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
+       struct route *r;
+       uint8_t gate[IPaddrlen];
+       uint8_t gnet[IPaddrlen];
        int version;
        int atype = unspecifiedv6, atypel = unknownv6;
 
-       USED(atype);
-       USED(atypel);
-       qlock(f->ipifc);
-       r = v6lookup(f, remote, nil);
+       qlock(&f->ipifc->qlock);
+       r = v6lookup(f, remote, NULL);
        version = (memcmp(remote, v4prefix, IPv4off) == 0) ? V4 : V6;
        
-       if(r != nil){
-               ifc = r->ifc;
-               if(r->type & Rv4)
+       if(r != NULL){
+               ifc = r->rt.ifc;
+               if(r->rt.type & Rv4)
                        v4tov6(gate, r->v4.gate);
                else {
                        ipmove(gate, r->v6.gate);
@@ -1339,16 +1352,16 @@ findlocalip(Fs *f, uchar *local, uchar *remote)
        }
 
 out:
-       qunlock(f->ipifc);
+       qunlock(&f->ipifc->qlock);
 }
 
 /*
  *  return first v4 address associated with an interface
  */
 int
-ipv4local(Ipifc *ifc, uchar *addr)
+ipv4local(struct Ipifc *ifc, uint8_t *addr)
 {
-       Iplifc *lifc;
+       struct Iplifc *lifc;
 
        for(lifc = ifc->lifc; lifc; lifc = lifc->next){
                if(isv4(lifc->local)){
@@ -1363,9 +1376,9 @@ ipv4local(Ipifc *ifc, uchar *addr)
  *  return first v6 address associated with an interface
  */
 int
-ipv6local(Ipifc *ifc, uchar *addr)
+ipv6local(struct Ipifc *ifc, uint8_t *addr)
 {
-       Iplifc *lifc;
+       struct Iplifc *lifc;
 
        for(lifc = ifc->lifc; lifc; lifc = lifc->next){
                if(!isv4(lifc->local) && !(lifc->tentative)){
@@ -1377,9 +1390,9 @@ ipv6local(Ipifc *ifc, uchar *addr)
 }
 
 int
-ipv6anylocal(Ipifc *ifc, uchar *addr)
+ipv6anylocal(struct Ipifc *ifc, uint8_t *addr)
 {
-       Iplifc *lifc;
+       struct Iplifc *lifc;
 
        for(lifc = ifc->lifc; lifc; lifc = lifc->next){
                if(!isv4(lifc->local)){
@@ -1393,15 +1406,15 @@ ipv6anylocal(Ipifc *ifc, uchar *addr)
 /*
  *  see if this address is bound to the interface
  */
-Iplifc*
-iplocalonifc(Ipifc *ifc, uchar *ip)
+struct Iplifc*
+iplocalonifc(struct Ipifc *ifc, uint8_t *ip)
 {
-       Iplifc *lifc;
+       struct Iplifc *lifc;
 
        for(lifc = ifc->lifc; lifc; lifc = lifc->next)
                if(ipcmp(ip, lifc->local) == 0)
                        return lifc;
-       return nil;
+       return NULL;
 }
 
 
@@ -1409,17 +1422,17 @@ iplocalonifc(Ipifc *ifc, uchar *ip)
  *  See if we're proxying for this address on this interface
  */
 int
-ipproxyifc(Fs *f, Ipifc *ifc, uchar *ip)
+ipproxyifc(struct Fs *f, struct Ipifc *ifc, uint8_t *ip)
 {
-       Route *r;
-       uchar net[IPaddrlen];
-       Iplifc *lifc;
+       struct route *r;
+       uint8_t net[IPaddrlen];
+       struct Iplifc *lifc;
 
        /* see if this is a direct connected pt to pt address */
-       r = v6lookup(f, ip, nil);
-       if(r == nil)
+       r = v6lookup(f, ip, NULL);
+       if(r == NULL)
                return 0;
-       if((r->type & (Rifc|Rproxy)) != (Rifc|Rproxy))
+       if((r->rt.type & (Rifc|Rproxy)) != (Rifc|Rproxy))
                return 0;
 
        /* see if this is on the right interface */
@@ -1436,7 +1449,7 @@ ipproxyifc(Fs *f, Ipifc *ifc, uchar *ip)
  *  return multicast version if any
  */
 int
-ipismulticast(uchar *ip)
+ipismulticast(uint8_t *ip)
 {
        if(isv4(ip)){
                if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
@@ -1449,7 +1462,7 @@ ipismulticast(uchar *ip)
 }
 
 int
-ipisbm(uchar *ip)
+ipisbm(uint8_t *ip)
 {
        if(isv4(ip)){
                if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
@@ -1468,13 +1481,14 @@ ipisbm(uchar *ip)
  *  add a multicast address to an interface, called with c locked
  */
 void
-ipifcaddmulti(Conv *c, uchar *ma, uchar *ia)
+ipifcaddmulti(struct conv *c, uint8_t *ma, uint8_t *ia)
 {
-       Ipifc *ifc;
-       Iplifc *lifc;
-       Conv **p;
-       Ipmulti *multi, **l;
-       Fs *f;
+       ERRSTACK(2);
+       struct Ipifc *ifc;
+       struct Iplifc *lifc;
+       struct conv **p;
+       struct Ipmulti *multi, **l;
+       struct Fs *f;
 
        f = c->p->f;
        
@@ -1483,24 +1497,24 @@ ipifcaddmulti(Conv *c, uchar *ma, uchar *ia)
                if(ipcmp(ia, (*l)->ia) == 0)
                        return;         /* it's already there */
 
-       multi = *l = smalloc(sizeof(*multi));
+       multi = *l = kzmalloc(sizeof(*multi), 0);
        ipmove(multi->ma, ma);
        ipmove(multi->ia, ia);
-       multi->next = nil;
+       multi->next = NULL;
 
        for(p = f->ipifc->conv; *p; p++){
                if((*p)->inuse == 0)
                        continue;
-               ifc = (Ipifc*)(*p)->ptcl;
+               ifc = (struct Ipifc*)(*p)->ptcl;
                if(waserror()){
-                       wunlock(ifc);
+                       wunlock(&ifc->rwlock);
                        nexterror();
                }
-               wlock(ifc);
+               wlock(&ifc->rwlock);
                for(lifc = ifc->lifc; lifc; lifc = lifc->next)
                        if(ipcmp(ia, lifc->local) == 0)
                                addselfcache(f, ifc, lifc, ma, Rmulti);
-               wunlock(ifc);
+               wunlock(&ifc->rwlock);
                poperror();
        }
 }
@@ -1510,13 +1524,14 @@ ipifcaddmulti(Conv *c, uchar *ma, uchar *ia)
  *  remove a multicast address from an interface, called with c locked
  */
 void
-ipifcremmulti(Conv *c, uchar *ma, uchar *ia)
+ipifcremmulti(struct conv *c, uint8_t *ma, uint8_t *ia)
 {
-       Ipmulti *multi, **l;
-       Iplifc *lifc;
-       Conv **p;
-       Ipifc *ifc;
-       Fs *f;
+       ERRSTACK(2);
+       struct Ipmulti *multi, **l;
+       struct Iplifc *lifc;
+       struct conv **p;
+       struct Ipifc *ifc;
+       struct Fs *f;
 
        f = c->p->f;
        
@@ -1526,7 +1541,7 @@ ipifcremmulti(Conv *c, uchar *ma, uchar *ia)
                        break;
 
        multi = *l;
-       if(multi == nil)
+       if(multi == NULL)
                return;         /* we don't have it open */
 
        *l = multi->next;
@@ -1535,63 +1550,61 @@ ipifcremmulti(Conv *c, uchar *ma, uchar *ia)
                if((*p)->inuse == 0)
                        continue;
 
-               ifc = (Ipifc*)(*p)->ptcl;
+               ifc = (struct Ipifc*)(*p)->ptcl;
                if(waserror()){
-                       wunlock(ifc);
+                       wunlock(&ifc->rwlock);
                        nexterror();
                }
-               wlock(ifc);
+               wlock(&ifc->rwlock);
                for(lifc = ifc->lifc; lifc; lifc = lifc->next)
                        if(ipcmp(ia, lifc->local) == 0)
                                remselfcache(f, ifc, lifc, ma);
-               wunlock(ifc);
+               wunlock(&ifc->rwlock);
                poperror();
        }
 
-       free(multi);
+       kfree(multi);
 }
 
 /*
  *  make lifc's join and leave multicast groups
  */
 static char*
-ipifcjoinmulti(Ipifc *ifc, char **argv, int argc)
+ipifcjoinmulti(struct Ipifc *ifc, char **argv, int argc)
 {
-       USED(ifc, argv, argc);
-       return nil;
+       return NULL;
 }
 
 static char*
-ipifcleavemulti(Ipifc *ifc, char **argv, int argc)
+ipifcleavemulti(struct Ipifc *ifc, char **argv, int argc)
 {
-       USED(ifc, argv, argc);
-       return nil;
+       return NULL;
 }
 
 static void
-ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
+ipifcregisterproxy(struct Fs *f, struct Ipifc *ifc, uint8_t *ip)
 {
-       Conv **cp, **e;
-       Ipifc *nifc;
-       Iplifc *lifc;
-       Medium *m;
-       uchar net[IPaddrlen];
+       struct conv **cp, **e;
+       struct Ipifc *nifc;
+       struct Iplifc *lifc;
+       struct medium *m;
+       uint8_t net[IPaddrlen];
 
        /* register the address on any network that will proxy for us */
        e = &f->ipifc->conv[f->ipifc->nc];
 
        if(!isv4(ip)) { // V6
                for(cp = f->ipifc->conv; cp < e; cp++){
-                       if(*cp == nil)
+                       if(*cp == NULL)
                                continue;
-                       nifc = (Ipifc*)(*cp)->ptcl;
+                       nifc = (struct Ipifc*)(*cp)->ptcl;
                        if(nifc == ifc)
                                continue;
        
-                       rlock(nifc);
+                       rlock(&nifc->rwlock);
                        m = nifc->m;
-                       if(m == nil || m->addmulti == nil) {
-                               runlock(nifc);
+                       if(m == NULL || m->addmulti == NULL) {
+                               runlock(&nifc->rwlock);
                                continue;
                        }
                        for(lifc = nifc->lifc; lifc; lifc = lifc->next){
@@ -1604,22 +1617,22 @@ ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
                                        break;
                                }
                        }
-                       runlock(nifc);
+                       runlock(&nifc->rwlock);
                }
                return;
        }
        else { // V4
                for(cp = f->ipifc->conv; cp < e; cp++){
-                       if(*cp == nil)
+                       if(*cp == NULL)
                                continue;
-                       nifc = (Ipifc*)(*cp)->ptcl;
+                       nifc = (struct Ipifc*)(*cp)->ptcl;
                        if(nifc == ifc)
                                continue;
        
-                       rlock(nifc);
+                       rlock(&nifc->rwlock);
                        m = nifc->m;
-                       if(m == nil || m->areg == nil){
-                               runlock(nifc);
+                       if(m == NULL || m->areg == NULL){
+                               runlock(&nifc->rwlock);
                                continue;
                        }
                        for(lifc = nifc->lifc; lifc; lifc = lifc->next){
@@ -1629,7 +1642,7 @@ ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
                                        break;
                                }
                        }
-                       runlock(nifc);
+                       runlock(&nifc->rwlock);
                }
        }
 }
@@ -1637,13 +1650,13 @@ ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
 
 // added for new v6 mesg types
 static void
-adddefroute6(Fs *f, uchar *gate, int force)
+adddefroute6(struct Fs *f, uint8_t *gate, int force)
 {
-       Route *r;
+       struct route *r;
 
-       r = v6lookup(f, v6Unspecified, nil);
-       if(r!=nil)
-       if(!(force) && (strcmp(r->tag,"ra")!=0))        // route entries generated
+       r = v6lookup(f, v6Unspecified, NULL);
+       if(r!=NULL)
+       if(!(force) && (strcmp(r->rt.tag,"ra")!=0))     // route entries generated
                return;                 // by all other means take
                                        // precedence over router annc
 
@@ -1657,16 +1670,16 @@ enum
 };
 
 char*
-ipifcaddpref6(Ipifc *ifc, char**argv, int argc)
+ipifcaddpref6(struct Ipifc *ifc, char**argv, int argc)
 {
-       uchar   onlink = 1;
-       uchar   autoflag = 1;
+       uint8_t onlink = 1;
+       uint8_t autoflag = 1;
        long    validlt = 0xffffffff;
        long    preflt = 0xffffffff;
        long    origint = NOW / 10^3;
-       uchar   prefix[IPaddrlen];
+       uint8_t prefix[IPaddrlen];
        int     plen = 64;
-       Iplifc  *lifc;
+       struct Iplifc   *lifc;
        char    addr[40], preflen[6];
        char    *params[3];
 
@@ -1698,20 +1711,20 @@ ipifcaddpref6(Ipifc *ifc, char**argv, int argc)
        )
                return Ebadarg;
 
-       lifc = smalloc(sizeof(Iplifc));
+       lifc = kzmalloc(sizeof(struct Iplifc), 0);
        lifc->onlink = (onlink!=0);
        lifc->autoflag = (autoflag!=0);
        lifc->validlt = validlt;
        lifc->preflt = preflt;
        lifc->origint = origint;
 
-       if(ifc->m->pref2addr!=nil)
+       if(ifc->m->pref2addr!=NULL)
                ifc->m->pref2addr(prefix, ifc->mac);
        else
                return Ebadarg;
        
-       sprint(addr, "%I", prefix);
-       sprint(preflen, "/%d", plen);
+       snprintf(addr, sizeof(addr), "%I", prefix);
+       snprintf(preflen, sizeof(preflen), "/%d", plen);
        params[0] = "add";
        params[1] = addr;
        params[2] = preflen;