#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet6/in6_var.h>
#include <netinet6/ip6_var.h>
#include <netinet6/scope6_var.h>
#ifdef ENABLE_DEFAULT_SCOPE
VNET_DEFINE(int, ip6_use_defzone) = 1;
#else
VNET_DEFINE(int, ip6_use_defzone) = 0;
#endif
SYSCTL_DECL(_net_inet6_ip6);
static struct mtx scope6_lock;
#define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
#define SCOPE6_LOCK() mtx_lock(&scope6_lock)
#define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
#define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
VNET_DEFINE_STATIC(struct scope6_id, sid_default);
#define V_sid_default VNET(sid_default)
#define SID(ifp) \
(((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
static int scope6_get(struct ifnet *, struct scope6_id *);
static int scope6_set(struct ifnet *, struct scope6_id *);
void
scope6_init(void)
{
bzero(&V_sid_default, sizeof(V_sid_default));
if (!IS_DEFAULT_VNET(curvnet))
return;
SCOPE6_LOCK_INIT();
}
struct scope6_id *
scope6_ifattach(struct ifnet *ifp)
{
struct scope6_id *sid;
sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
return (sid);
}
void
scope6_ifdetach(struct scope6_id *sid)
{
free(sid, M_IFADDR);
}
int
scope6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
{
struct in6_ifreq *ifr;
if (ifp->if_afdata[AF_INET6] == NULL)
return (EPFNOSUPPORT);
ifr = (struct in6_ifreq *)data;
switch (cmd) {
case SIOCSSCOPE6:
return (scope6_set(ifp,
(struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
case SIOCGSCOPE6:
return (scope6_get(ifp,
(struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
case SIOCGSCOPE6DEF:
return (scope6_get_default(
(struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
default:
return (EOPNOTSUPP);
}
}
static int
scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
{
int i;
int error = 0;
struct scope6_id *sid = NULL;
IF_AFDATA_WLOCK(ifp);
sid = SID(ifp);
if (!sid) {
IF_AFDATA_WUNLOCK(ifp);
return (EINVAL);
}
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) {
IF_AFDATA_WUNLOCK(ifp);
return (EINVAL);
}
if (i == IPV6_ADDR_SCOPE_LINKLOCAL) {
struct epoch_tracker et;
NET_EPOCH_ENTER(et);
if (!ifnet_byindex(idlist->s6id_list[i])) {
NET_EPOCH_EXIT(et);
IF_AFDATA_WUNLOCK(ifp);
return (EINVAL);
}
NET_EPOCH_EXIT(et);
}
sid->s6id_list[i] = idlist->s6id_list[i];
}
}
IF_AFDATA_WUNLOCK(ifp);
return (error);
}
static int
scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
{
struct epoch_tracker et;
struct scope6_id *sid;
NET_EPOCH_ENTER(et);
sid = SID(ifp);
if (sid == NULL) {
NET_EPOCH_EXIT(et);
return (EINVAL);
}
*idlist = *sid;
NET_EPOCH_EXIT(et);
return (0);
}
int
in6_addrscope(const struct in6_addr *addr)
{
if (IN6_IS_ADDR_MULTICAST(addr)) {
if (IPV6_ADDR_MC_SCOPE(addr) == 0x0f)
return (IPV6_ADDR_SCOPE_GLOBAL);
return (IPV6_ADDR_MC_SCOPE(addr));
}
if (IN6_IS_ADDR_LINKLOCAL(addr) ||
IN6_IS_ADDR_LOOPBACK(addr))
return (IPV6_ADDR_SCOPE_LINKLOCAL);
if (IN6_IS_ADDR_SITELOCAL(addr))
return (IPV6_ADDR_SCOPE_SITELOCAL);
return (IPV6_ADDR_SCOPE_GLOBAL);
}
void
scope6_setdefault(struct ifnet *ifp)
{
SCOPE6_LOCK();
if (ifp) {
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
ifp->if_index;
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
ifp->if_index;
} else {
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
}
SCOPE6_UNLOCK();
}
int
scope6_get_default(struct scope6_id *idlist)
{
SCOPE6_LOCK();
*idlist = V_sid_default;
SCOPE6_UNLOCK();
return (0);
}
u_int32_t
scope6_addr2default(struct in6_addr *addr)
{
u_int32_t id;
if (IN6_IS_ADDR_LOOPBACK(addr))
return (0);
SCOPE6_LOCK();
id = V_sid_default.s6id_list[in6_addrscope(addr)];
SCOPE6_UNLOCK();
return (id);
}
int
sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
{
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))) {
struct epoch_tracker et;
NET_EPOCH_ENTER(et);
if (ifnet_byindex(zoneid) == NULL) {
NET_EPOCH_EXIT(et);
return (ENXIO);
}
NET_EPOCH_EXIT(et);
sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
sin6->sin6_scope_id = 0;
}
return 0;
}
int
sa6_recoverscope(struct sockaddr_in6 *sin6)
{
char ip6buf[INET6_ADDRSTRLEN];
u_int32_t zoneid;
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) {
struct epoch_tracker et;
NET_EPOCH_ENTER(et);
if (!ifnet_byindex(zoneid)) {
NET_EPOCH_EXIT(et);
return (ENXIO);
}
NET_EPOCH_EXIT(et);
if (sin6->sin6_scope_id != 0 &&
zoneid != sin6->sin6_scope_id) {
log(LOG_NOTICE,
"%s: embedded scope mismatch: %s%%%d. "
"sin6_scope_id was overridden\n", __func__,
ip6_sprintf(ip6buf, &sin6->sin6_addr),
sin6->sin6_scope_id);
}
sin6->sin6_addr.s6_addr16[1] = 0;
sin6->sin6_scope_id = zoneid;
}
}
return 0;
}
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;
if (IN6_IS_ADDR_LOOPBACK(in6)) {
if (!(ifp->if_flags & IFF_LOOPBACK))
return (EINVAL);
} else {
scope = in6_addrscope(in6);
if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
zoneid = ifp->if_index;
in6->s6_addr16[1] = htons(zoneid & 0xffff);
} else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
struct epoch_tracker et;
NET_EPOCH_ENTER(et);
if (ifp->if_afdata[AF_INET6] == NULL) {
NET_EPOCH_EXIT(et);
return (ENETDOWN);
}
sid = SID(ifp);
zoneid = sid->s6id_list[scope];
NET_EPOCH_EXIT(et);
}
}
if (ret_id != NULL)
*ret_id = zoneid;
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);
}
uint16_t
in6_getscope(const struct in6_addr *in6)
{
if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
return (in6->s6_addr16[1]);
return (0);
}
uint32_t
in6_get_unicast_scopeid(const struct in6_addr *in6, const struct ifnet *ifp)
{
if (IN6_IS_SCOPE_LINKLOCAL(in6))
return (ifp->if_index);
return (0);
}
void
in6_set_unicast_scopeid(struct in6_addr *in6, uint32_t scopeid)
{
in6->s6_addr16[1] = htons(scopeid & 0xffff);
}
struct ifnet*
in6_getlinkifnet(uint32_t zoneid)
{
struct ifnet *ifp;
ifp = ifnet_byindex((u_short)zoneid);
if (ifp == NULL)
return (NULL);
if (ifp->if_afdata[AF_INET6] == NULL) {
log(LOG_NOTICE,
"%s: embedded scope points to an interface without "
"IPv6: %s%%%d.\n", __func__,
if_name(ifp), zoneid);
return (NULL);
}
return (ifp);
}
uint32_t
in6_getscopezone(const struct ifnet *ifp, int scope)
{
if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
scope == IPV6_ADDR_SCOPE_LINKLOCAL)
return (ifp->if_index);
if (scope >= 0 && scope < IPV6_ADDR_SCOPES_COUNT)
return (SID(ifp)->s6id_list[scope]);
return (0);
}
void
in6_splitscope(const struct in6_addr *src, struct in6_addr *dst,
uint32_t *scopeid)
{
uint32_t zoneid;
*dst = *src;
zoneid = ntohs(in6_getscope(dst));
in6_clearscope(dst);
*scopeid = zoneid;
}
int
sa6_checkzone(struct sockaddr_in6 *sa6)
{
int scope;
scope = in6_addrscope(&sa6->sin6_addr);
if (scope == IPV6_ADDR_SCOPE_GLOBAL)
return (sa6->sin6_scope_id ? EINVAL: 0);
if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr) &&
scope != IPV6_ADDR_SCOPE_LINKLOCAL &&
scope != IPV6_ADDR_SCOPE_INTFACELOCAL) {
if (sa6->sin6_scope_id == 0 && V_ip6_use_defzone != 0)
sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
return (0);
}
if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) {
if (sa6->sin6_scope_id == 0)
sa6->sin6_scope_id = in6_getscopezone(V_loif, scope);
else if (sa6->sin6_scope_id != in6_getscopezone(V_loif, scope))
return (EADDRNOTAVAIL);
}
if (sa6->sin6_scope_id != 0)
return (0);
if (V_ip6_use_defzone != 0)
sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
return (sa6->sin6_scope_id ? 0: EADDRNOTAVAIL);
}
int
sa6_checkzone_ifp(struct ifnet *ifp, struct sockaddr_in6 *sa6)
{
int scope;
scope = in6_addrscope(&sa6->sin6_addr);
if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
scope == IPV6_ADDR_SCOPE_INTFACELOCAL) {
if (sa6->sin6_scope_id == 0) {
sa6->sin6_scope_id = in6_getscopezone(ifp, scope);
return (0);
} else if (sa6->sin6_scope_id != in6_getscopezone(ifp, scope))
return (EADDRNOTAVAIL);
}
return (sa6_checkzone(sa6));
}