2005-01-07 02:30:35 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2000-07-04 16:35:15 +00:00
|
|
|
* Copyright (C) 2000 WIDE Project.
|
|
|
|
* All rights reserved.
|
2003-10-09 16:13:47 +00:00
|
|
|
*
|
2000-07-04 16:35:15 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2003-10-09 16:13:47 +00:00
|
|
|
*
|
2000-07-04 16:35:15 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2007-12-10 16:03:40 +00:00
|
|
|
*
|
|
|
|
* $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
|
2000-07-04 16:35:15 +00:00
|
|
|
*/
|
|
|
|
|
2007-12-10 16:03:40 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
2014-08-22 19:21:08 +00:00
|
|
|
#include <sys/sockio.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <sys/systm.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <sys/queue.h>
|
2012-12-05 19:45:24 +00:00
|
|
|
#include <sys/sysctl.h>
|
2005-07-25 12:31:43 +00:00
|
|
|
#include <sys/syslog.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
2008-12-02 21:37:28 +00:00
|
|
|
#include <net/vnet.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2008-12-02 21:37:28 +00:00
|
|
|
#include <netinet/ip6.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet6/in6_var.h>
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
#include <netinet6/ip6_var.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet6/scope6_var.h>
|
|
|
|
|
2010-04-29 11:52:42 +00:00
|
|
|
#ifdef ENABLE_DEFAULT_SCOPE
|
|
|
|
VNET_DEFINE(int, ip6_use_defzone) = 1;
|
|
|
|
#else
|
|
|
|
VNET_DEFINE(int, ip6_use_defzone) = 0;
|
|
|
|
#endif
|
2012-12-05 19:45:24 +00:00
|
|
|
SYSCTL_DECL(_net_inet6_ip6);
|
2005-07-25 12:31:43 +00:00
|
|
|
|
2003-10-22 15:13:36 +00:00
|
|
|
/*
|
2004-11-29 03:10:35 +00:00
|
|
|
* The scope6_lock protects the global sid default stored in
|
|
|
|
* sid_default below.
|
2003-10-22 15:13:36 +00:00
|
|
|
*/
|
|
|
|
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)
|
|
|
|
|
2018-07-24 16:35:52 +00:00
|
|
|
VNET_DEFINE_STATIC(struct scope6_id, sid_default);
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_sid_default VNET(sid_default)
|
2008-11-19 09:39:34 +00:00
|
|
|
|
2003-10-17 15:46:31 +00:00
|
|
|
#define SID(ifp) \
|
|
|
|
(((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2014-08-22 19:21:08 +00:00
|
|
|
static int scope6_get(struct ifnet *, struct scope6_id *);
|
|
|
|
static int scope6_set(struct ifnet *, struct scope6_id *);
|
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
void
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_init(void)
|
2003-10-17 15:46:31 +00:00
|
|
|
{
|
|
|
|
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
bzero(&V_sid_default, sizeof(V_sid_default));
|
First pass at separating per-vnet initializer functions
from existing functions for initializing global state.
At this stage, the new per-vnet initializer functions are
directly called from the existing global initialization code,
which should in most cases result in compiler inlining those
new functions, hence yielding a near-zero functional change.
Modify the existing initializer functions which are invoked via
protosw, like ip_init() et. al., to allow them to be invoked
multiple times, i.e. per each vnet. Global state, if any,
is initialized only if such functions are called within the
context of vnet0, which will be determined via the
IS_DEFAULT_VNET(curvnet) check (currently always true).
While here, V_irtualize a few remaining global UMA zones
used by net/netinet/netipsec networking code. While it is
not yet clear to me or anybody else whether this is the right
thing to do, at this stage this makes the code more readable,
and makes it easier to track uncollected UMA-zone-backed
objects on vnet removal. In the long run, it's quite possible
that some form of shared use of UMA zone pools among multiple
vnets should be considered.
Bump __FreeBSD_version due to changes in layout of structs
vnet_ipfw, vnet_inet and vnet_net.
Approved by: julian (mentor)
2009-04-06 22:29:41 +00:00
|
|
|
|
|
|
|
if (!IS_DEFAULT_VNET(curvnet))
|
|
|
|
return;
|
|
|
|
|
|
|
|
SCOPE6_LOCK_INIT();
|
2003-10-17 15:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct scope6_id *
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_ifattach(struct ifnet *ifp)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2003-10-17 15:46:31 +00:00
|
|
|
struct scope6_id *sid;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2014-09-11 12:54:17 +00:00
|
|
|
sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
|
2000-07-04 16:35:15 +00:00
|
|
|
/*
|
|
|
|
* XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
|
|
|
|
* Should we rather hardcode here?
|
|
|
|
*/
|
2003-10-21 20:05:32 +00:00
|
|
|
sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
|
2003-10-17 15:46:31 +00:00
|
|
|
sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
|
2014-09-11 12:54:17 +00:00
|
|
|
return (sid);
|
2003-10-17 15:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_ifdetach(struct scope6_id *sid)
|
2003-10-17 15:46:31 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
free(sid, M_IFADDR);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-08-22 19:21:08 +00:00
|
|
|
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
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2004-11-29 03:10:35 +00:00
|
|
|
int i;
|
2000-07-04 16:35:15 +00:00
|
|
|
int error = 0;
|
2004-11-29 03:10:35 +00:00
|
|
|
struct scope6_id *sid = NULL;
|
|
|
|
|
2012-11-14 17:36:06 +00:00
|
|
|
IF_AFDATA_WLOCK(ifp);
|
2004-11-29 03:10:35 +00:00
|
|
|
sid = SID(ifp);
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2004-11-29 03:10:35 +00:00
|
|
|
if (!sid) { /* paranoid? */
|
2012-11-14 17:36:06 +00:00
|
|
|
IF_AFDATA_WUNLOCK(ifp);
|
2003-10-06 14:02:09 +00:00
|
|
|
return (EINVAL);
|
2004-11-29 03:10:35 +00:00
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: We need more consistency checks of the relationship among
|
|
|
|
* scopes (e.g. an organization should be larger than a site).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO(XXX): after setting, we should reflect the changes to
|
2003-10-09 16:13:47 +00:00
|
|
|
* interface addresses, routing table entries, PCB entries...
|
2000-07-04 16:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
2003-10-17 15:46:31 +00:00
|
|
|
if (idlist->s6id_list[i] &&
|
|
|
|
idlist->s6id_list[i] != sid->s6id_list[i]) {
|
2003-10-21 20:05:32 +00:00
|
|
|
/*
|
|
|
|
* An interface zone ID must be the corresponding
|
|
|
|
* interface index by definition.
|
|
|
|
*/
|
|
|
|
if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
|
|
|
|
idlist->s6id_list[i] != ifp->if_index) {
|
2012-11-14 17:36:06 +00:00
|
|
|
IF_AFDATA_WUNLOCK(ifp);
|
2003-10-21 20:05:32 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-12-04 17:49:36 +00:00
|
|
|
if (i == IPV6_ADDR_SCOPE_LINKLOCAL) {
|
|
|
|
struct epoch_tracker et;
|
|
|
|
|
|
|
|
NET_EPOCH_ENTER(et);
|
|
|
|
if (!ifnet_byindex(idlist->s6id_list[i])) {
|
|
|
|
/*
|
|
|
|
* XXX: theoretically, there should be
|
|
|
|
* no relationship between link IDs and
|
|
|
|
* interface IDs, but we check the
|
|
|
|
* consistency for safety in later use.
|
|
|
|
*/
|
|
|
|
NET_EPOCH_EXIT(et);
|
|
|
|
IF_AFDATA_WUNLOCK(ifp);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
NET_EPOCH_EXIT(et);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: we must need lots of work in this case,
|
|
|
|
* but we simply set the new value in this initial
|
|
|
|
* implementation.
|
|
|
|
*/
|
2003-10-17 15:46:31 +00:00
|
|
|
sid->s6id_list[i] = idlist->s6id_list[i];
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-14 17:36:06 +00:00
|
|
|
IF_AFDATA_WUNLOCK(ifp);
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2003-10-06 14:02:09 +00:00
|
|
|
return (error);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:08 +00:00
|
|
|
static int
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2019-01-09 01:11:19 +00:00
|
|
|
struct epoch_tracker et;
|
2012-11-14 17:36:06 +00:00
|
|
|
struct scope6_id *sid;
|
2003-10-17 15:46:31 +00:00
|
|
|
|
2012-11-14 17:36:06 +00:00
|
|
|
/* We only need to lock the interface's afdata for SID() to work. */
|
2019-01-09 01:11:19 +00:00
|
|
|
NET_EPOCH_ENTER(et);
|
2012-11-14 17:36:06 +00:00
|
|
|
sid = SID(ifp);
|
2004-11-29 03:10:35 +00:00
|
|
|
if (sid == NULL) { /* paranoid? */
|
2019-01-09 01:11:19 +00:00
|
|
|
NET_EPOCH_EXIT(et);
|
2003-10-06 14:02:09 +00:00
|
|
|
return (EINVAL);
|
2004-11-29 03:10:35 +00:00
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2003-10-17 15:46:31 +00:00
|
|
|
*idlist = *sid;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2019-01-09 01:11:19 +00:00
|
|
|
NET_EPOCH_EXIT(et);
|
2003-10-06 14:02:09 +00:00
|
|
|
return (0);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a scope of the address. Node-local, link-local, site-local or global.
|
|
|
|
*/
|
|
|
|
int
|
2014-09-11 10:27:59 +00:00
|
|
|
in6_addrscope(const struct in6_addr *addr)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
|
|
|
|
2014-09-11 10:27:59 +00:00
|
|
|
if (IN6_IS_ADDR_MULTICAST(addr)) {
|
2000-07-04 16:35:15 +00:00
|
|
|
/*
|
2014-09-11 10:27:59 +00:00
|
|
|
* Addresses with reserved value F must be treated as
|
|
|
|
* global multicast addresses.
|
2000-07-04 16:35:15 +00:00
|
|
|
*/
|
2014-09-11 10:27:59 +00:00
|
|
|
if (IPV6_ADDR_MC_SCOPE(addr) == 0x0f)
|
|
|
|
return (IPV6_ADDR_SCOPE_GLOBAL);
|
|
|
|
return (IPV6_ADDR_MC_SCOPE(addr));
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
2014-09-11 10:27:59 +00:00
|
|
|
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);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:23:49 +00:00
|
|
|
/*
|
|
|
|
* ifp - note that this might be NULL
|
|
|
|
*/
|
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
void
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_setdefault(struct ifnet *ifp)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit
Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.
Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().
Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).
All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).
(*) netipsec/keysock.c did not validate depending on compile time options.
Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-10-02 15:37:58 +00:00
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
/*
|
2004-11-29 03:10:35 +00:00
|
|
|
* Currently, this function just sets the default "interfaces"
|
2003-10-17 15:46:31 +00:00
|
|
|
* and "links" according to the given interface.
|
2000-07-04 16:35:15 +00:00
|
|
|
* We might eventually have to separate the notion of "link" from
|
|
|
|
* "interface" and provide a user interface to set the default.
|
|
|
|
*/
|
2003-10-22 15:13:36 +00:00
|
|
|
SCOPE6_LOCK();
|
2000-07-04 16:35:15 +00:00
|
|
|
if (ifp) {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
|
2003-10-17 15:46:31 +00:00
|
|
|
ifp->if_index;
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
|
2000-07-04 16:35:15 +00:00
|
|
|
ifp->if_index;
|
2003-10-09 16:13:47 +00:00
|
|
|
} else {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
|
|
|
|
V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
|
2003-10-09 16:13:47 +00:00
|
|
|
}
|
2003-10-22 15:13:36 +00:00
|
|
|
SCOPE6_UNLOCK();
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_get_default(struct scope6_id *idlist)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2003-10-22 15:13:36 +00:00
|
|
|
|
|
|
|
SCOPE6_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
*idlist = V_sid_default;
|
2003-10-22 15:13:36 +00:00
|
|
|
SCOPE6_UNLOCK();
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2003-10-06 14:02:09 +00:00
|
|
|
return (0);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t
|
2007-07-05 16:23:49 +00:00
|
|
|
scope6_addr2default(struct in6_addr *addr)
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2003-10-22 15:13:36 +00:00
|
|
|
u_int32_t id;
|
|
|
|
|
2003-10-17 15:46:31 +00:00
|
|
|
/*
|
|
|
|
* special case: The loopback address should be considered as
|
|
|
|
* link-local, but there's no ambiguity in the syntax.
|
|
|
|
*/
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK(addr))
|
|
|
|
return (0);
|
|
|
|
|
2003-10-22 15:13:36 +00:00
|
|
|
/*
|
|
|
|
* XXX: 32-bit read is atomic on all our platforms, is it OK
|
|
|
|
* not to lock here?
|
|
|
|
*/
|
|
|
|
SCOPE6_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
id = V_sid_default.s6id_list[in6_addrscope(addr)];
|
2003-10-22 15:13:36 +00:00
|
|
|
SCOPE6_UNLOCK();
|
|
|
|
return (id);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
2005-07-25 12:31:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the specified scope zone ID in the sin6_scope_id field. If the ID
|
|
|
|
* is unspecified (=0), needs to be specified, and the default zone ID can be
|
|
|
|
* used, the default value will be used.
|
|
|
|
* This routine then generates the kernel-internal form: if the address scope
|
|
|
|
* of is interface-local or link-local, embed the interface index in the
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
|
2005-07-25 12:31:43 +00:00
|
|
|
{
|
|
|
|
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))) {
|
2021-12-04 17:49:36 +00:00
|
|
|
struct epoch_tracker et;
|
|
|
|
|
2005-07-25 12:31:43 +00:00
|
|
|
/*
|
|
|
|
* At this moment, we only check interface-local and
|
|
|
|
* link-local scope IDs, and use interface indices as the
|
|
|
|
* zone IDs assuming a one-to-one mapping between interfaces
|
|
|
|
* and links.
|
|
|
|
*/
|
2021-12-04 17:49:36 +00:00
|
|
|
NET_EPOCH_ENTER(et);
|
|
|
|
if (ifnet_byindex(zoneid) == NULL) {
|
|
|
|
NET_EPOCH_EXIT(et);
|
2005-07-25 12:31:43 +00:00
|
|
|
return (ENXIO);
|
2021-12-04 17:49:36 +00:00
|
|
|
}
|
|
|
|
NET_EPOCH_EXIT(et);
|
2005-07-25 12:31:43 +00:00
|
|
|
|
|
|
|
/* XXX assignment to 16bit from 32bit variable */
|
|
|
|
sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
|
|
|
|
sin6->sin6_scope_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate standard sockaddr_in6 from embedded form.
|
|
|
|
*/
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
sa6_recoverscope(struct sockaddr_in6 *sin6)
|
2005-07-25 12:31:43 +00:00
|
|
|
{
|
2006-12-12 12:17:58 +00:00
|
|
|
char ip6buf[INET6_ADDRSTRLEN];
|
2005-07-25 12:31:43 +00:00
|
|
|
u_int32_t zoneid;
|
|
|
|
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
|
|
|
|
/*
|
|
|
|
* KAME assumption: link id == interface id
|
|
|
|
*/
|
|
|
|
zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
|
|
|
|
if (zoneid) {
|
2021-12-04 17:49:36 +00:00
|
|
|
struct epoch_tracker et;
|
|
|
|
|
|
|
|
NET_EPOCH_ENTER(et);
|
2005-07-25 12:31:43 +00:00
|
|
|
/* sanity check */
|
2021-12-04 17:49:36 +00:00
|
|
|
if (!ifnet_byindex(zoneid)) {
|
|
|
|
NET_EPOCH_EXIT(et);
|
2005-07-25 12:31:43 +00:00
|
|
|
return (ENXIO);
|
2021-12-04 17:49:36 +00:00
|
|
|
}
|
|
|
|
NET_EPOCH_EXIT(et);
|
2012-11-18 16:06:51 +00:00
|
|
|
if (sin6->sin6_scope_id != 0 &&
|
|
|
|
zoneid != sin6->sin6_scope_id) {
|
|
|
|
log(LOG_NOTICE,
|
|
|
|
"%s: embedded scope mismatch: %s%%%d. "
|
2016-02-12 21:17:00 +00:00
|
|
|
"sin6_scope_id was overridden\n", __func__,
|
2012-11-18 16:06:51 +00:00
|
|
|
ip6_sprintf(ip6buf, &sin6->sin6_addr),
|
|
|
|
sin6->sin6_scope_id);
|
|
|
|
}
|
2005-07-25 12:31:43 +00:00
|
|
|
sin6->sin6_addr.s6_addr16[1] = 0;
|
|
|
|
sin6->sin6_scope_id = zoneid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
|
|
|
|
* non NULL, it is set to the zone ID. If the zone ID needs to be embedded
|
|
|
|
* in the in6_addr structure, in6 will be modified.
|
2007-07-05 16:23:49 +00:00
|
|
|
*
|
|
|
|
* ret_id - unnecessary?
|
2005-07-25 12:31:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
|
2005-07-25 12:31:43 +00:00
|
|
|
{
|
|
|
|
int scope;
|
|
|
|
u_int32_t zoneid = 0;
|
2005-07-25 17:05:37 +00:00
|
|
|
struct scope6_id *sid;
|
|
|
|
|
2005-07-25 12:31:43 +00:00
|
|
|
/*
|
|
|
|
* special case: the loopback address can only belong to a loopback
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK(in6)) {
|
2013-01-10 00:10:24 +00:00
|
|
|
if (!(ifp->if_flags & IFF_LOOPBACK))
|
2005-07-25 12:31:43 +00:00
|
|
|
return (EINVAL);
|
2013-01-10 00:10:24 +00:00
|
|
|
} else {
|
|
|
|
scope = in6_addrscope(in6);
|
|
|
|
if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
|
|
|
|
scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
|
|
|
|
/*
|
2018-01-23 19:40:05 +00:00
|
|
|
* Currently we use interface indices as the
|
2013-01-10 00:10:24 +00:00
|
|
|
* zone IDs for interface-local and link-local
|
|
|
|
* scopes.
|
|
|
|
*/
|
|
|
|
zoneid = ifp->if_index;
|
|
|
|
in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
|
|
|
|
} else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
|
2019-01-09 01:11:19 +00:00
|
|
|
struct epoch_tracker et;
|
|
|
|
|
|
|
|
NET_EPOCH_ENTER(et);
|
2020-06-03 17:26:00 +00:00
|
|
|
if (ifp->if_afdata[AF_INET6] == NULL) {
|
2020-06-03 16:57:30 +00:00
|
|
|
NET_EPOCH_EXIT(et);
|
|
|
|
return (ENETDOWN);
|
|
|
|
}
|
2013-01-10 00:10:24 +00:00
|
|
|
sid = SID(ifp);
|
|
|
|
zoneid = sid->s6id_list[scope];
|
2019-01-09 01:11:19 +00:00
|
|
|
NET_EPOCH_EXIT(et);
|
2005-07-25 12:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret_id != NULL)
|
|
|
|
*ret_id = zoneid;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just clear the embedded scope identifier. Return 0 if the original address
|
|
|
|
* is intact; return non 0 if the address is modified.
|
|
|
|
*/
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
in6_clearscope(struct in6_addr *in6)
|
2005-07-25 12:31:43 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2012-05-24 16:30:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the scope identifier or zero.
|
|
|
|
*/
|
|
|
|
uint16_t
|
2018-06-05 20:54:29 +00:00
|
|
|
in6_getscope(const struct in6_addr *in6)
|
2012-05-24 16:30:13 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
|
|
|
|
return (in6->s6_addr16[1]);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2014-09-11 12:33:37 +00:00
|
|
|
|
2020-04-15 12:56:05 +00:00
|
|
|
/*
|
|
|
|
* Returns scope zone id for the unicast address @in6.
|
|
|
|
*
|
|
|
|
* Returns 0 for global unicast and loopback addresses.
|
|
|
|
* Returns interface index for the link-local addresses.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-11 12:33:37 +00:00
|
|
|
/*
|
|
|
|
* Return pointer to ifnet structure, corresponding to the zone id of
|
|
|
|
* link-local scope.
|
|
|
|
*/
|
|
|
|
struct ifnet*
|
|
|
|
in6_getlinkifnet(uint32_t zoneid)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (ifnet_byindex((u_short)zoneid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return zone id for the specified scope.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
/*
|
|
|
|
* Extracts scope from adddress @dst, stores cleared address
|
|
|
|
* inside @dst and zone inside @scopeid
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-11 12:33:37 +00:00
|
|
|
/*
|
|
|
|
* This function is for checking sockaddr_in6 structure passed
|
|
|
|
* from the application level (usually).
|
|
|
|
*
|
|
|
|
* sin6_scope_id should be set for link-local unicast, link-local and
|
|
|
|
* interface-local multicast addresses.
|
|
|
|
*
|
|
|
|
* If it is zero, then look into default zone ids. If default zone id is
|
|
|
|
* not set or disabled, then return error.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Since ::1 address always configured on the lo0, we can
|
|
|
|
* automatically set its zone id, when it is not specified.
|
|
|
|
* Return error, when specified zone id doesn't match with
|
|
|
|
* actual value.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/* XXX: we can validate sin6_scope_id here */
|
|
|
|
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 error if we can't determine zone id */
|
|
|
|
return (sa6->sin6_scope_id ? 0: EADDRNOTAVAIL);
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:12:51 +00:00
|
|
|
/*
|
|
|
|
* This function is similar to sa6_checkzone, but it uses given ifp
|
|
|
|
* to initialize sin6_scope_id.
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|