X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/9bccf70c0258c7cac2dcb80011b2a964d884c552..060df5ea7c632b1ac8cc8aac1fb59758165c2084:/bsd/netinet6/scope6.c?ds=sidebyside diff --git a/bsd/netinet6/scope6.c b/bsd/netinet6/scope6.c index 2a9e9ce0f..70e90dfa9 100644 --- a/bsd/netinet6/scope6.c +++ b/bsd/netinet6/scope6.c @@ -1,4 +1,4 @@ -/* $FreeBSD: src/sys/netinet6/scope6.c,v 1.1.2.2 2001/07/03 11:01:55 ume Exp $ */ +/* $FreeBSD: src/sys/netinet6/scope6.c,v 1.3 2002/03/25 10:12:51 ume Exp $ */ /* $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $ */ /* @@ -45,6 +45,8 @@ #include #include +extern lck_mtx_t *scope6_mutex; + struct scope6_id { /* * 16 is correspondent to 4bit multicast scope field. @@ -52,30 +54,35 @@ struct scope6_id { */ u_int32_t s6id_list[16]; }; -static size_t if_indexlim = 8; +static size_t if_scope_indexlim = 8; struct scope6_id *scope6_ids = NULL; -void -scope6_ifattach(ifp) - struct ifnet *ifp; +int +scope6_ifattach( + struct ifnet *ifp) { - int s = splnet(); - /* * We have some arrays that should be indexed by if_index. * since if_index will grow dynamically, they should grow too. */ - if (scope6_ids == NULL || if_index >= if_indexlim) { + lck_mtx_lock(scope6_mutex); + if (scope6_ids == NULL || if_index >= if_scope_indexlim) { size_t n; caddr_t q; + int newlim = if_scope_indexlim; - while (if_index >= if_indexlim) - if_indexlim <<= 1; + while (if_index >= newlim) + newlim <<= 1; /* grow scope index array */ - n = if_indexlim * sizeof(struct scope6_id); + n = newlim * sizeof(struct scope6_id); /* XXX: need new malloc type? */ q = (caddr_t)_MALLOC(n, M_IFADDR, M_WAITOK); + if (q == NULL) { + lck_mtx_unlock(scope6_mutex); + return ENOBUFS; + } + if_scope_indexlim = newlim; bzero(q, n); if (scope6_ids) { bcopy((caddr_t)scope6_ids, q, n/2); @@ -88,8 +95,8 @@ scope6_ifattach(ifp) /* don't initialize if called twice */ if (SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]) { - splx(s); - return; + lck_mtx_unlock(scope6_mutex); + return 0; } /* @@ -103,16 +110,17 @@ scope6_ifattach(ifp) SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1; #endif #undef SID + lck_mtx_unlock(scope6_mutex); - splx(s); + return 0; } int -scope6_set(ifp, idlist) - struct ifnet *ifp; - u_int32_t *idlist; +scope6_set( + struct ifnet *ifp, + u_int32_t *idlist) { - int i, s; + int i; int error = 0; if (scope6_ids == NULL) /* paranoid? */ @@ -128,8 +136,7 @@ scope6_set(ifp, idlist) * interface addresses, routing table entries, PCB entries... */ - s = splnet(); - + lck_mtx_lock(scope6_mutex); for (i = 0; i < 16; i++) { if (idlist[i] && idlist[i] != scope6_ids[ifp->if_index].s6id_list[i]) { @@ -141,7 +148,7 @@ scope6_set(ifp, idlist) * IDs, but we check the consistency for * safety in later use. */ - splx(s); + lck_mtx_unlock(scope6_mutex); return(EINVAL); } @@ -153,21 +160,23 @@ scope6_set(ifp, idlist) scope6_ids[ifp->if_index].s6id_list[i] = idlist[i]; } } - splx(s); + lck_mtx_unlock(scope6_mutex); return(error); } int -scope6_get(ifp, idlist) - struct ifnet *ifp; - u_int32_t *idlist; +scope6_get( + struct ifnet *ifp, + u_int32_t *idlist) { if (scope6_ids == NULL) /* paranoid? */ return(EINVAL); + lck_mtx_lock(scope6_mutex); bcopy(scope6_ids[ifp->if_index].s6id_list, idlist, sizeof(scope6_ids[ifp->if_index].s6id_list)); + lck_mtx_unlock(scope6_mutex); return(0); } @@ -222,7 +231,7 @@ struct in6_addr *addr; } } - if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) { + if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) { if (addr->s6_addr8[15] == 1) /* loopback */ return IPV6_ADDR_SCOPE_NODELOCAL; if (addr->s6_addr8[15] == 0) /* unspecified */ @@ -233,40 +242,49 @@ struct in6_addr *addr; } int -in6_addr2scopeid(ifp, addr) - struct ifnet *ifp; /* must not be NULL */ - struct in6_addr *addr; /* must not be NULL */ +in6_addr2scopeid( + struct ifnet *ifp, /* must not be NULL */ + struct in6_addr *addr) /* must not be NULL */ { int scope = in6_addrscope(addr); + int index = ifp->if_index; + int retid = 0; if (scope6_ids == NULL) /* paranoid? */ return(0); /* XXX */ - if (ifp->if_index >= if_indexlim) + + lck_mtx_lock(scope6_mutex); + if (index >= if_scope_indexlim) { + lck_mtx_unlock(scope6_mutex); return(0); /* XXX */ + } -#define SID scope6_ids[ifp->if_index] +#define SID scope6_ids[index] switch(scope) { case IPV6_ADDR_SCOPE_NODELOCAL: - return(-1); /* XXX: is this an appropriate value? */ - + retid = -1; /* XXX: is this an appropriate value? */ + break; case IPV6_ADDR_SCOPE_LINKLOCAL: - return(SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]); - + retid=SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]; + break; case IPV6_ADDR_SCOPE_SITELOCAL: - return(SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL]); - + retid=SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL]; + break; case IPV6_ADDR_SCOPE_ORGLOCAL: - return(SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL]); - + retid=SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL]; + break; default: - return(0); /* XXX: treat as global. */ + break; /* XXX: value 0, treat as global. */ } #undef SID + + lck_mtx_unlock(scope6_mutex); + return retid; } void -scope6_setdefault(ifp) - struct ifnet *ifp; /* note that this might be NULL */ +scope6_setdefault( + struct ifnet *ifp) /* note that this might be NULL */ { /* * Currently, this function just set the default "link" according to @@ -274,30 +292,39 @@ scope6_setdefault(ifp) * We might eventually have to separate the notion of "link" from * "interface" and provide a user interface to set the default. */ + lck_mtx_lock(scope6_mutex); if (ifp) { scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index; } else scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0; + lck_mtx_unlock(scope6_mutex); } int -scope6_get_default(idlist) - u_int32_t *idlist; +scope6_get_default( + u_int32_t *idlist) { if (scope6_ids == NULL) /* paranoid? */ return(EINVAL); + lck_mtx_lock(scope6_mutex); bcopy(scope6_ids[0].s6id_list, idlist, sizeof(scope6_ids[0].s6id_list)); + lck_mtx_unlock(scope6_mutex); return(0); } u_int32_t -scope6_addr2default(addr) - struct in6_addr *addr; +scope6_addr2default( + struct in6_addr *addr) { - return(scope6_ids[0].s6id_list[in6_addrscope(addr)]); + u_int32_t id = 0; + int index = in6_addrscope(addr); + lck_mtx_lock(scope6_mutex); + id = scope6_ids[0].s6id_list[index]; + lck_mtx_unlock(scope6_mutex); + return (id); }