#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/syslog.h>
#include <sys/mcache.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet6/in6_var.h>
#include <netinet6/scope6_var.h>
extern lck_mtx_t *scope6_mutex;
#ifdef ENABLE_DEFAULT_SCOPE
int ip6_use_defzone = 1;
#else
int ip6_use_defzone = 0;
#endif
static size_t if_scope_indexlim = 8;
struct scope6_id *scope6_ids = NULL;
int
scope6_ifattach(
struct ifnet *ifp)
{
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 >= newlim)
newlim <<= 1;
n = newlim * sizeof(struct scope6_id);
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);
FREE((caddr_t)scope6_ids, M_IFADDR);
}
scope6_ids = (struct scope6_id *)q;
}
#define SID scope6_ids[ifp->if_index]
if (SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]) {
lck_mtx_unlock(scope6_mutex);
return 0;
}
SID.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
#if MULTI_SCOPE
SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
#endif
#undef SID
lck_mtx_unlock(scope6_mutex);
return 0;
}
int
scope6_set(
struct ifnet *ifp,
u_int32_t *idlist)
{
int i;
int error = 0;
if (scope6_ids == NULL)
return(EINVAL);
lck_mtx_lock(scope6_mutex);
for (i = 0; i < 16; i++) {
if (idlist[i] &&
idlist[i] != scope6_ids[ifp->if_index].s6id_list[i]) {
if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
idlist[i] > if_index) {
lck_mtx_unlock(scope6_mutex);
return(EINVAL);
}
scope6_ids[ifp->if_index].s6id_list[i] = idlist[i];
}
}
lck_mtx_unlock(scope6_mutex);
return(error);
}
int
scope6_get(
struct ifnet *ifp,
u_int32_t *idlist)
{
if (scope6_ids == NULL)
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);
}
int
in6_addrscope(addr)
struct in6_addr *addr;
{
int scope;
if (addr->s6_addr8[0] == 0xfe) {
scope = addr->s6_addr8[1] & 0xc0;
switch (scope) {
case 0x80:
return IPV6_ADDR_SCOPE_LINKLOCAL;
break;
case 0xc0:
return IPV6_ADDR_SCOPE_SITELOCAL;
break;
default:
return IPV6_ADDR_SCOPE_GLOBAL;
break;
}
}
if (addr->s6_addr8[0] == 0xff) {
scope = addr->s6_addr8[1] & 0x0f;
switch (scope) {
case IPV6_ADDR_SCOPE_INTFACELOCAL:
return IPV6_ADDR_SCOPE_INTFACELOCAL;
break;
case IPV6_ADDR_SCOPE_LINKLOCAL:
return IPV6_ADDR_SCOPE_LINKLOCAL;
break;
case IPV6_ADDR_SCOPE_SITELOCAL:
return IPV6_ADDR_SCOPE_SITELOCAL;
break;
default:
return IPV6_ADDR_SCOPE_GLOBAL;
break;
}
}
if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
if (addr->s6_addr8[15] == 1)
return IPV6_ADDR_SCOPE_LINKLOCAL;
if (addr->s6_addr8[15] == 0)
return IPV6_ADDR_SCOPE_GLOBAL;
}
return IPV6_ADDR_SCOPE_GLOBAL;
}
int
in6_addr2scopeid(
struct ifnet *ifp,
struct in6_addr *addr)
{
int scope = in6_addrscope(addr);
int index = ifp->if_index;
int retid = 0;
if (scope6_ids == NULL)
return(0);
lck_mtx_lock(scope6_mutex);
if (index >= if_scope_indexlim) {
lck_mtx_unlock(scope6_mutex);
return(0);
}
#define SID scope6_ids[index]
switch(scope) {
case IPV6_ADDR_SCOPE_NODELOCAL:
retid = -1;
break;
case IPV6_ADDR_SCOPE_LINKLOCAL:
retid=SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
break;
case IPV6_ADDR_SCOPE_SITELOCAL:
retid=SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
break;
case IPV6_ADDR_SCOPE_ORGLOCAL:
retid=SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
break;
default:
break;
}
#undef SID
lck_mtx_unlock(scope6_mutex);
return retid;
}
int
sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
{
struct ifnet *ifp;
u_int32_t zoneid;
if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
zoneid = scope6_addr2default(&sin6->sin6_addr);
if (zoneid != 0 &&
(IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
if (if_index < zoneid)
return (ENXIO);
ifnet_head_lock_shared();
ifp = ifindex2ifnet[zoneid];
if (ifp == NULL) {
ifnet_head_done();
return (ENXIO);
}
ifnet_head_done();
sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
sin6->sin6_scope_id = 0;
}
return 0;
}
void
rtkey_to_sa6(struct rtentry *rt, struct sockaddr_in6 *sin6)
{
VERIFY(rt_key(rt)->sa_family == AF_INET6);
*sin6 = *((struct sockaddr_in6 *)rt_key(rt));
sin6->sin6_scope_id = 0;
}
void
rtgw_to_sa6(struct rtentry *rt, struct sockaddr_in6 *sin6)
{
VERIFY(rt->rt_flags & RTF_GATEWAY);
*sin6 = *((struct sockaddr_in6 *)rt->rt_gateway);
sin6->sin6_scope_id = 0;
}
int
sa6_recoverscope(struct sockaddr_in6 *sin6)
{
u_int32_t zoneid;
if (sin6->sin6_scope_id != 0) {
log(LOG_NOTICE,
"sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
ip6_sprintf(&sin6->sin6_addr), sin6->sin6_scope_id);
}
if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
if (zoneid) {
if (if_index < zoneid)
return (ENXIO);
ifnet_head_lock_shared();
if (ifindex2ifnet[zoneid] == NULL) {
ifnet_head_done();
return (ENXIO);
}
ifnet_head_done();
sin6->sin6_addr.s6_addr16[1] = 0;
sin6->sin6_scope_id = zoneid;
}
}
return 0;
}
void
scope6_setdefault(
struct ifnet *ifp)
{
lck_mtx_lock(scope6_mutex);
if (ifp) {
scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
ifp->if_index;
scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
ifp->if_index;
} else {
scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
}
lck_mtx_unlock(scope6_mutex);
}
int
scope6_get_default(
u_int32_t *idlist)
{
if (scope6_ids == NULL)
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(
struct in6_addr *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);
}
int
in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
{
int scope;
u_int32_t zoneid = 0;
int index = ifp->if_index;
#ifdef DIAGNOSTIC
if (scope6_ids == NULL) {
panic("in6_setscope: scope array is NULL");
}
#endif
if (IN6_IS_ADDR_LOOPBACK(in6)) {
if (!(ifp->if_flags & IFF_LOOPBACK)) {
return (EINVAL);
} else {
if (ret_id != NULL)
*ret_id = 0;
return (0);
}
}
scope = in6_addrscope(in6);
#define SID scope6_ids[index]
lck_mtx_lock(scope6_mutex);
switch (scope) {
case IPV6_ADDR_SCOPE_INTFACELOCAL:
zoneid = SID.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
break;
case IPV6_ADDR_SCOPE_LINKLOCAL:
zoneid = SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
break;
case IPV6_ADDR_SCOPE_SITELOCAL:
zoneid = SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
break;
case IPV6_ADDR_SCOPE_ORGLOCAL:
zoneid = SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
break;
#undef SID
default:
zoneid = 0;
break;
}
lck_mtx_unlock(scope6_mutex);
if (ret_id != NULL)
*ret_id = zoneid;
if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
in6->s6_addr16[1] = htons(zoneid & 0xffff);
return (0);
}
int
in6_clearscope(struct in6_addr *in6)
{
int modified = 0;
if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
if (in6->s6_addr16[1] != 0)
modified = 1;
in6->s6_addr16[1] = 0;
}
return (modified);
}