#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/thread2.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet6/in6_var.h>
#include <netinet6/scope6_var.h>
static struct scope6_id sid_default;
static __inline
struct scope6_id *
SID(struct ifnet *ifp)
{
struct in6_ifextra *xtra;
xtra = ifp->if_afdata[AF_INET6];
if (xtra)
return (xtra->scope6_id);
return(NULL);
}
void
scope6_init(void)
{
bzero(&sid_default, sizeof(sid_default));
}
struct scope6_id *
scope6_ifattach(struct ifnet *ifp)
{
struct scope6_id *sid;
sid = (struct scope6_id *)kmalloc(sizeof(*sid), M_IFADDR,
M_WAITOK | M_ZERO);
crit_enter();
sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
#ifdef MULTI_SCOPE
sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
#endif
crit_exit();
return sid;
}
void
scope6_ifdetach(struct scope6_id *sid)
{
kfree(sid, M_IFADDR);
}
int
scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
{
int i;
int error = 0;
struct scope6_id *sid = SID(ifp);
if (!sid)
return (EINVAL);
crit_enter();
for (i = 0; i < 16; i++) {
if (idlist->s6id_list[i] &&
idlist->s6id_list[i] != sid->s6id_list[i]) {
if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
idlist->s6id_list[i] != ifp->if_index) {
crit_exit();
return (EINVAL);
}
if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
idlist->s6id_list[i] > if_index) {
crit_exit();
return (EINVAL);
}
sid->s6id_list[i] = idlist->s6id_list[i];
}
}
crit_exit();
return (error);
}
int
scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
{
struct scope6_id *sid = SID(ifp);
if (sid == NULL)
return (EINVAL);
*idlist = *sid;
return (0);
}
int
in6_addrscope(struct in6_addr *addr)
{
int scope;
if (addr->s6_addr[0] == 0xfe) {
scope = addr->s6_addr[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_addr[0] == 0xff) {
scope = addr->s6_addr[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(&kin6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
if (addr->s6_addr[15] == 1)
return IPV6_ADDR_SCOPE_LINKLOCAL;
if (addr->s6_addr[15] == 0)
return IPV6_ADDR_SCOPE_GLOBAL;
}
return IPV6_ADDR_SCOPE_GLOBAL;
}
int
in6_addr2zoneid(struct ifnet *ifp,
struct in6_addr *addr,
u_int32_t *ret_id)
{
int scope;
u_int32_t zoneid = 0;
struct scope6_id *sid = SID(ifp);
#ifdef DIAGNOSTIC
if (sid == NULL) {
panic("in6_addr2zoneid: scope array is NULL");
}
if (ret_id == NULL) {
panic("in6_addr2zoneid: return ID is null");
}
#endif
if (IN6_IS_ADDR_LOOPBACK(addr)) {
if (!(ifp->if_flags & IFF_LOOPBACK)) {
return (-1);
} else {
*ret_id = 0;
return (0);
}
}
scope = in6_addrscope(addr);
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;
default:
zoneid = 0;
break;
}
*ret_id = zoneid;
return (0);
}
void
scope6_setdefault(struct ifnet *ifp)
{
if (ifp) {
sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
ifp->if_index;
sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
ifp->if_index;
} else {
sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
}
}
int
scope6_get_default(struct scope6_id *idlist)
{
*idlist = sid_default;
return (0);
}
u_int32_t
scope6_addr2default(struct in6_addr *addr)
{
if (IN6_IS_ADDR_LOOPBACK(addr))
return (0);
return (sid_default.s6id_list[in6_addrscope(addr)]);
}
int
in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
{
int scope;
u_int32_t zoneid = 0;
struct scope6_id *sid;
ifnet_serialize_all(ifp);
sid = SID(ifp);
#ifdef DIAGNOSTIC
if (sid == NULL) {
panic("in6_setscope: scope array is NULL");
}
#endif
if (IN6_IS_ADDR_LOOPBACK(in6)) {
if (!(ifp->if_flags & IFF_LOOPBACK)) {
ifnet_deserialize_all(ifp);
return (EINVAL);
} else {
if (ret_id != NULL)
*ret_id = 0;
ifnet_deserialize_all(ifp);
return (0);
}
}
scope = in6_addrscope(in6);
switch (scope) {
case IPV6_ADDR_SCOPE_NODELOCAL:
zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_NODELOCAL];
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;
default:
zoneid = 0;
break;
}
ifnet_deserialize_all(ifp);
if (ret_id != NULL)
*ret_id = zoneid;
if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_NODELOCAL(in6) )
in6->s6_addr16[1] = htons(zoneid & 0xffff);
return (0);
}