git: 19dc64451179 - main - if_stf: add 6rd support

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Sat, 20 Nov 2021 19:04:42 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=19dc644511796d80bd82f62ef49cb1cb4b86add3

commit 19dc644511796d80bd82f62ef49cb1cb4b86add3
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2021-11-08 08:46:47 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2021-11-20 18:29:01 +0000

    if_stf: add 6rd support
    
    Implement IPv6 Rapid Deployment (RFC5969) on top of the existing 6to4
    (RFC3056) if_stf code.
    
    PR:             253328
    Reviewed by:    hrs
    Obtained from:  pfSense
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D33037
---
 sbin/ifconfig/Makefile |   1 +
 sbin/ifconfig/ifstf.c  | 152 ++++++++++++++++++++++++
 sys/net/if_stf.c       | 314 +++++++++++++++++++++++++++++++++++++++----------
 sys/net/if_stf.h       |  46 ++++++++
 sys/netinet6/in6_var.h |   1 +
 5 files changed, 452 insertions(+), 62 deletions(-)

diff --git a/sbin/ifconfig/Makefile b/sbin/ifconfig/Makefile
index b178dc0c7e6a..c48375b8c6d9 100644
--- a/sbin/ifconfig/Makefile
+++ b/sbin/ifconfig/Makefile
@@ -24,6 +24,7 @@ SRCS+=	af_inet6.c		# IPv6 support
 .endif
 .if ${MK_INET6_SUPPORT} != "no"
 SRCS+=	af_nd6.c		# ND6 support
+SRCS+=	ifstf.c			# STF configuration options
 .endif
 
 SRCS+=	ifclone.c		# clone device support
diff --git a/sbin/ifconfig/ifstf.c b/sbin/ifconfig/ifstf.c
new file mode 100644
index 000000000000..f6c3cb5d5447
--- /dev/null
+++ b/sbin/ifconfig/ifstf.c
@@ -0,0 +1,152 @@
+/*-
+ * Copyright 2013 Ermal Luci
+ * All rights reserved.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
+ * 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.
+ */
+
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/sockio.h>
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <net/route.h>
+
+#include <netinet/in.h>
+#include <sys/mbuf.h>
+#include <net/if_stf.h>
+#include <arpa/inet.h>
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <err.h>
+#include <errno.h>
+
+#include "ifconfig.h"
+
+static int
+do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
+{
+	struct ifdrv ifd;
+
+	memset(&ifd, 0, sizeof(ifd));
+
+	strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
+	ifd.ifd_cmd = op;
+	ifd.ifd_len = argsize;
+	ifd.ifd_data = arg;
+
+	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
+}
+
+static void
+stf_status(int s)
+{
+	struct stfv4args param;
+
+	if (do_cmd(s, STF6RD_GV4NET, &param, sizeof(param), 0) < 0)
+		return;
+
+	printf("\tv4net %s/%d -> ", inet_ntoa(param.srcv4_addr),
+	    param.v4_prefixlen ? param.v4_prefixlen : 32);
+	printf("tv4br %s\n", inet_ntoa(param.braddr));
+}
+
+static void
+setstf_br(const char *val, int d, int s, const struct afswtch *afp)
+{
+	struct stfv4args req;
+	struct sockaddr_in sin;
+
+	memset(&req, 0, sizeof(req));
+
+	sin.sin_len = sizeof(sin);
+	sin.sin_family = AF_INET;
+
+	if (!inet_aton(val, &sin.sin_addr))
+		errx(1, "%s: bad value", val);
+
+	req.braddr = sin.sin_addr;
+	if (do_cmd(s, STF6RD_SBR, &req, sizeof(req), 1) < 0)
+		err(1, "STF6RD_SBR%s",  val);
+}
+
+static void
+setstf_set(const char *val, int d, int s, const struct afswtch *afp)
+{
+	struct stfv4args req;
+	struct sockaddr_in sin;
+	const char *errstr;
+	char *p = NULL;
+
+	memset(&req, 0, sizeof(req));
+
+	sin.sin_len = sizeof(sin);
+	sin.sin_family = AF_INET;
+
+	p = strrchr(val, '/');
+	if (p == NULL)
+		errx(2, "Wrong argument given");
+
+	*p = '\0';
+	req.v4_prefixlen = (int)strtonum(p + 1, 0, 32, &errstr);
+	if (errstr != NULL || req.v4_prefixlen == 0) {
+		*p = '/';
+		errx(1, "%s: bad value (prefix length %s)", val, errstr);
+	}
+
+	if (!inet_aton(val, &sin.sin_addr))
+		errx(1, "%s: bad value", val);
+
+	memcpy(&req.srcv4_addr, &sin.sin_addr, sizeof(req.srcv4_addr));
+	if (do_cmd(s, STF6RD_SV4NET, &req, sizeof(req), 1) < 0)
+		err(1, "STF6RD_SV4NET %s",  val);
+}
+
+static struct cmd stf_cmds[] = {
+	DEF_CMD_ARG("stfv4net",		setstf_set),
+	DEF_CMD_ARG("stfv4br",		setstf_br),
+};
+
+static struct afswtch af_stf = {
+	.af_name		= "af_stf",
+	.af_af			= AF_UNSPEC,
+	.af_other_status	= stf_status,
+};
+
+static __constructor void
+stf_ctor(void)
+{
+	int i;
+
+	for (i = 0; i < nitems(stf_cmds);  i++)
+		cmd_register(&stf_cmds[i]);
+	af_register(&af_stf);
+}
diff --git a/sys/net/if_stf.c b/sys/net/if_stf.c
index f80ba96e2f59..528e38c8d26e 100644
--- a/sys/net/if_stf.c
+++ b/sys/net/if_stf.c
@@ -5,6 +5,9 @@
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Copyright (C) 2000 WIDE Project.
+ * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org>
+ * Copyright (c) 2013 Ermal Luci <eri@FreeBSD.org>
+ * Copyright (c) 2017-2021 Rubicon Communications, LLC (Netgate)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -81,10 +84,12 @@
 #include <sys/socket.h>
 #include <sys/sockio.h>
 #include <sys/mbuf.h>
+#include <sys/endian.h>
 #include <sys/errno.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
 #include <sys/module.h>
+#include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/queue.h>
 #include <sys/sysctl.h>
@@ -98,6 +103,7 @@
 #include <net/route.h>
 #include <net/route/nhop.h>
 #include <net/netisr.h>
+#include <net/if_stf.h>
 #include <net/if_types.h>
 #include <net/vnet.h>
 
@@ -109,6 +115,7 @@
 #include <netinet/in_var.h>
 
 #include <netinet/ip6.h>
+#include <netinet6/in6_fib.h>
 #include <netinet6/ip6_var.h>
 #include <netinet6/in6_var.h>
 #include <netinet/ip_ecn.h>
@@ -141,7 +148,10 @@ SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN,
 
 struct stf_softc {
 	struct ifnet	*sc_ifp;
-	u_int	sc_fibnum;
+	in_addr_t	braddr;		/* Border relay IPv4 address */
+	in_addr_t	srcv4_addr;	/* Our IPv4 WAN address */
+	u_int		v4prefixlen;	/* How much of the v4 address to include in our address. */
+	u_int		sc_fibnum;
 	const struct encaptab *encap_cookie;
 };
 #define STF2IFP(sc)	((sc)->sc_ifp)
@@ -164,6 +174,11 @@ static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
 	struct ifnet *);
 static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
 	struct ifnet *);
+static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *,
+	struct sockaddr_in *, struct in6_addr, struct in6_addr,
+	struct in6_addr);
+static struct sockaddr_in *stf_getin4addr(struct stf_softc *,
+	struct sockaddr_in *, struct in6_addr, struct in6_addr);
 static int stf_ioctl(struct ifnet *, u_long, caddr_t);
 
 static int stf_clone_match(struct if_clone *, const char *);
@@ -324,14 +339,15 @@ static moduledata_t stf_mod = {
 };
 
 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
+MODULE_VERSION(if_stf, 2);
 
 static int
 stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
 {
 	struct ip ip;
 	struct stf_softc *sc;
-	struct in_addr a, b, mask;
 	struct in6_addr addr6, mask6;
+	struct sockaddr_in sin4addr, sin4mask;
 
 	sc = (struct stf_softc *)arg;
 	if (sc == NULL)
@@ -355,29 +371,43 @@ stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
 	if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
 		return (0);
 
-	/*
-	 * check if IPv4 dst matches the IPv4 address derived from the
-	 * local 6to4 address.
-	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
-	 */
-	if (bcmp(GET_V4(&addr6), &ip.ip_dst, sizeof(ip.ip_dst)) != 0)
-		return (0);
+	if (sc->srcv4_addr != INADDR_ANY) {
+		sin4addr.sin_addr.s_addr = sc->srcv4_addr;
+		sin4addr.sin_family = AF_INET;
+	} else
+		if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL)
+			return (0);
 
-	/*
-	 * check if IPv4 src matches the IPv4 address derived from the
-	 * local 6to4 address masked by prefixmask.
-	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
-	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
-	 */
-	bzero(&a, sizeof(a));
-	bcopy(GET_V4(&addr6), &a, sizeof(a));
-	bcopy(GET_V4(&mask6), &mask, sizeof(mask));
-	a.s_addr &= mask.s_addr;
-	b = ip.ip_src;
-	b.s_addr &= mask.s_addr;
-	if (a.s_addr != b.s_addr)
+	if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr)
 		return (0);
 
+	if (IN6_IS_ADDR_6TO4(&addr6)) {
+		/*
+		 * 6to4 (RFC 3056).
+		 * Check if IPv4 src matches the IPv4 address derived
+		 * from the local 6to4 address masked by prefixmask.
+		 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
+		 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
+		 */
+		memcpy(&sin4mask.sin_addr, GET_V4(&mask6),
+		    sizeof(sin4mask.sin_addr));
+		if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) !=
+		    (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr))
+			return (0);
+	} else {
+		/* 6rd (RFC 5569) */
+		/*
+		 * No restriction on the src address in the case of
+		 * 6rd because the stf(4) interface always has a
+		 * prefix which covers whole of IPv4 src address
+		 * range.  So, stf_output() will catch all of
+		 * 6rd-capsuled IPv4 traffic with suspicious inner dst
+		 * IPv4 address (i.e. the IPv6 destination address is
+		 * one the admin does not like to route to outside),
+		 * and then it discard them silently.
+		 */
+	}
+
 	/* stf interface makes single side match only */
 	return (32);
 }
@@ -387,30 +417,38 @@ stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
 {
 	struct ifaddr *ia;
 	struct in_ifaddr *ia4;
-	struct in6_ifaddr *ia6;
-	struct sockaddr_in6 *sin6;
+	struct in6_addr addr6, mask6;
+	struct sockaddr_in sin4;
+	struct stf_softc *sc;
 	struct in_addr in;
 
 	NET_EPOCH_ASSERT();
 
+	sc = ifp->if_softc;
+
 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
 		if (ia->ifa_addr->sa_family != AF_INET6)
 			continue;
-		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
-			continue;
 
-		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
+		addr6 = *IFA_IN6(ia);
+		mask6 = *IFA_MASKIN6(ia);
+		if (sc->srcv4_addr != INADDR_ANY)
+			bcopy(&sc->srcv4_addr, &in, sizeof(in));
+		else {
+			if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL)
+				continue;
+			bcopy(&sin4.sin_addr, &in, sizeof(in));
+		}
+
 		CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
 				break;
 		if (ia4 == NULL)
 			continue;
 
-		ia6 = (struct in6_ifaddr *)ia;
+		*addr = addr6;
+		*mask = mask6;
 
-		*addr = sin6->sin6_addr;
-		*mask = ia6->ia_prefixmask.sin6_addr;
 		return (0);
 	}
 
@@ -423,8 +461,7 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 {
 	struct stf_softc *sc;
 	const struct sockaddr_in6 *dst6;
-	struct in_addr in4;
-	const void *ptr;
+	struct sockaddr_in dst4, src4;
 	u_int8_t tos;
 	struct ip *ip;
 	struct ip6_hdr *ip6;
@@ -474,17 +511,17 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 	 * Pickup the right outer dst addr from the list of candidates.
 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
 	 */
-	ptr = NULL;
-	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
-		ptr = GET_V4(&ip6->ip6_dst);
-	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
-		ptr = GET_V4(&dst6->sin6_addr);
-	else {
-		m_freem(m);
-		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
-		return (ENETUNREACH);
+	if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
+	    ip6->ip6_dst) == NULL) {
+		if (sc->braddr != INADDR_ANY)
+			dst4.sin_addr.s_addr = sc->braddr;
+		else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
+		    dst6->sin6_addr) == NULL) {
+			m_freem(m);
+			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
+			return (ENETUNREACH);
+		}
 	}
-	bcopy(ptr, &in4, sizeof(in4));
 
 	if (bpf_peers_present(ifp->if_bpf)) {
 		/*
@@ -507,8 +544,16 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 
 	bzero(ip, sizeof(*ip));
 
-	bcopy(GET_V4(&addr6), &ip->ip_src, sizeof(ip->ip_src));
-	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
+	if (sc->srcv4_addr != INADDR_ANY)
+		src4.sin_addr.s_addr = sc->srcv4_addr;
+	else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) {
+		m_freem(m);
+		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
+		return (ENETUNREACH);
+	}
+	bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
+	bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
+
 	ip->ip_p = IPPROTO_IPV6;
 	ip->ip_ttl = ip_stf_ttl;
 	ip->ip_len = htons(m->m_pkthdr.len);
@@ -556,13 +601,6 @@ stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
 		return (-1);
 	}
 
-	/*
-	 * reject packets with private address range.
-	 * (requirement from RFC3056 section 2 1st paragraph)
-	 */
-	if (isrfc1918addr(in))
-		return (-1);
-
 	/*
 	 * reject packets with broadcast
 	 */
@@ -624,6 +662,7 @@ in_stf_input(struct mbuf *m, int off, int proto, void *arg)
 	struct ip6_hdr *ip6;
 	u_int8_t otos, itos;
 	struct ifnet *ifp;
+	struct nhop_object *nh;
 
 	NET_EPOCH_ASSERT();
 
@@ -674,6 +713,32 @@ in_stf_input(struct mbuf *m, int off, int proto, void *arg)
 		return (IPPROTO_DONE);
 	}
 
+	/*
+	 * reject packets with private address range.
+	 * (requirement from RFC3056 section 2 1st paragraph)
+	 */
+	if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip->ip_src)) ||
+	    (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip->ip_dst))) {
+		m_freem(m);
+		return (IPPROTO_DONE);
+	}
+
+	/*
+	 * Ignore if the destination is the same stf interface because
+	 * all of valid IPv6 outgoing traffic should go interfaces
+	 * except for it.
+	 */
+	nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0);
+	if (nh == NULL) {
+		m_free(m);
+		return (IPPROTO_DONE);
+	}
+	if ((nh->nh_ifp == ifp) &&
+	    (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) {
+		m_free(m);
+		return (IPPROTO_DONE);
+	}
+
 	itos = IPV6_TRAFFIC_CLASS(ip6);
 	if ((ifp->if_flags & IFF_LINK1) != 0)
 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
@@ -709,34 +774,159 @@ in_stf_input(struct mbuf *m, int off, int proto, void *arg)
 	return (IPPROTO_DONE);
 }
 
+static struct sockaddr_in *
+stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
+    struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6)
+{
+       int i;
+
+	/*
+	* When (src addr & src mask) != (in6 & src mask),
+	* the dst is not in the 6rd domain.  The IPv4 address must
+	* not be used.
+	*/
+	for (i = 0; i < sizeof(addr6); i++) {
+		if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) !=
+		    (((u_char *)&in6)[i] & ((u_char *)&mask6)[i]))
+		return (NULL);
+	}
+
+	/* After the mask check, use in6 instead of addr6. */
+	return (stf_getin4addr(sc, sin, in6, mask6));
+}
+
+static struct sockaddr_in *
+stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
+    struct in6_addr addr6, struct in6_addr mask6)
+{
+	struct in_addr *in;
+
+	memset(sin, 0, sizeof(*sin));
+	in = &sin->sin_addr;
+	if (IN6_IS_ADDR_6TO4(&addr6)) {
+		/* 6to4 (RFC 3056) */
+		bcopy(GET_V4(&addr6), in, sizeof(*in));
+		if (isrfc1918addr(in))
+			return (NULL);
+	} else {
+		/* 6rd (RFC 5569) */
+		in_addr_t v4prefix;
+		uint8_t *v6 = (uint8_t*)&addr6;
+		uint64_t v6prefix;
+		u_int plen;
+		u_int v4suffixlen;
+
+		v4prefix = 0;
+		if (sc->v4prefixlen < 32) {
+			v4suffixlen = 32 - sc->v4prefixlen;
+			v4prefix = ntohl(sc->srcv4_addr) &
+			    (0xffffffffU << v4suffixlen);
+		} else {
+			MPASS(sc->v4prefixlen == 32);
+			v4suffixlen = 32;
+		}
+
+		plen = in6_mask2len(&mask6, NULL);
+		if (plen > 64)
+			return (NULL);
+
+		/* To make this simple we do not support prefixes longer than
+		 * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64
+		 * or shorter." so this is a moderately safe assumption. */
+		v6prefix = be64toh(*(uint64_t *)v6);
+
+		/* Shift away the v6 prefix itself. */
+		v6prefix <<= plen;
+		v6prefix >>= plen;
+
+		/* Now shift away everything after the v4 address. */
+		v6prefix >>= 64 - plen - v4suffixlen;
+
+		sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix);
+	}
+
+	return (sin);
+}
+
 static int
 stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 {
 	struct ifaddr *ifa;
+	struct ifdrv *ifd;
 	struct ifreq *ifr;
-	struct sockaddr_in6 *sin6;
-	struct in_addr addr;
+	struct sockaddr_in sin4;
+	struct stf_softc *sc_cur;
+	struct stfv4args args;
 	int error, mtu;
 
 	error = 0;
+	sc_cur = ifp->if_softc;
+
 	switch (cmd) {
+	case SIOCSDRVSPEC:
+		ifd = (struct ifdrv *)data;
+		error = priv_check(curthread, PRIV_NET_ADDIFADDR);
+		if (error)
+			break;
+		if (ifd->ifd_cmd == STF6RD_SV4NET) {
+			if (ifd->ifd_len != sizeof(args)) {
+				error = EINVAL;
+				break;
+			}
+			bzero(&args, sizeof(args));
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
+			if (error)
+				break;
+
+			if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) {
+				error = EINVAL;
+				break;
+			}
+
+			bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr,
+			    sizeof(sc_cur->srcv4_addr));
+			sc_cur->v4prefixlen = args.v4_prefixlen;
+		} else if (ifd->ifd_cmd == STF6RD_SBR) {
+			if (ifd->ifd_len != sizeof(args)) {
+				error = EINVAL;
+				break;
+			}
+			bzero(&args, sizeof(args));
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
+			if (error)
+				break;
+			sc_cur->braddr = args.braddr.s_addr;
+		} else
+			error = EINVAL;
+		break;
+	case SIOCGDRVSPEC:
+		ifd = (struct ifdrv *)data;
+		if (ifd->ifd_cmd != STF6RD_GV4NET) {
+			error = EINVAL;
+			break;
+		}
+		if (ifd->ifd_len != sizeof(args)) {
+			error = EINVAL;
+			break;
+		}
+		bzero(&args, sizeof(args));
+		args.srcv4_addr.s_addr = sc_cur->srcv4_addr;
+		args.braddr.s_addr = sc_cur->braddr;
+		args.v4_prefixlen = sc_cur->v4prefixlen;
+		error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
+		break;
 	case SIOCSIFADDR:
 		ifa = (struct ifaddr *)data;
 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
 			error = EAFNOSUPPORT;
 			break;
 		}
-		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
+		if (stf_getin4addr(sc_cur, &sin4,
+		    satosin6(ifa->ifa_addr)->sin6_addr,
+		    satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) {
 			error = EINVAL;
 			break;
 		}
-		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
-		if (isrfc1918addr(&addr)) {
-			error = EINVAL;
-			break;
-		}
-
 		ifp->if_flags |= IFF_UP;
 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
 		break;
diff --git a/sys/net/if_stf.h b/sys/net/if_stf.h
new file mode 100644
index 000000000000..26c8f2f04e00
--- /dev/null
+++ b/sys/net/if_stf.h
@@ -0,0 +1,46 @@
+/*-
+ * Copyright (C) 2000 WIDE Project.
+ * All rights reserved.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#ifndef _NET_IF_STF_H_
+#define _NET_IF_STF_H_
+
+struct stfv4args {
+	struct in_addr srcv4_addr;	/* Our IPv4 src/WAN address */
+	struct in_addr braddr;		/* The border relay IPv4 address */
+	int v4_prefixlen;		/* The length of the IPv4 prefix. (I.e.
+					   the number of bits of the IPv4
+					   address not encoded in the IPv6
+					   address. */
+};
+
+#define STF6RD_SV4NET	1
+#define STF6RD_GV4NET	2
+#define STF6RD_SBR	3
+
+#endif /* _NET_IF_STF_H_ */
diff --git a/sys/netinet6/in6_var.h b/sys/netinet6/in6_var.h
index 1caa4511a2b1..d3da832d2b90 100644
--- a/sys/netinet6/in6_var.h
+++ b/sys/netinet6/in6_var.h
@@ -407,6 +407,7 @@ struct	in6_rrenumreq {
 #define IA6_DSTSIN6(ia)	(&((ia)->ia_dstaddr))
 #define IFA_IN6(x)	(&((struct sockaddr_in6 *)((x)->ifa_addr))->sin6_addr)
 #define IFA_DSTIN6(x)	(&((struct sockaddr_in6 *)((x)->ifa_dstaddr))->sin6_addr)
+#define IFA_MASKIN6(x)	(&((struct sockaddr_in6 *)((x)->ifa_netmask))->sin6_addr)
 
 #define IFPR_IN6(x)	(&((struct sockaddr_in6 *)((x)->ifpr_prefix))->sin6_addr)