162587Sitojun/*	$FreeBSD: stable/10/sys/net/if_stf.c 340958 2018-11-26 12:19:30Z eugen $	*/
295023Ssuz/*	$KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $	*/
362587Sitojun
4139823Simp/*-
562587Sitojun * Copyright (C) 2000 WIDE Project.
662587Sitojun * All rights reserved.
762587Sitojun *
862587Sitojun * Redistribution and use in source and binary forms, with or without
962587Sitojun * modification, are permitted provided that the following conditions
1062587Sitojun * are met:
1162587Sitojun * 1. Redistributions of source code must retain the above copyright
1262587Sitojun *    notice, this list of conditions and the following disclaimer.
1362587Sitojun * 2. Redistributions in binary form must reproduce the above copyright
1462587Sitojun *    notice, this list of conditions and the following disclaimer in the
1562587Sitojun *    documentation and/or other materials provided with the distribution.
1662587Sitojun * 3. Neither the name of the project nor the names of its contributors
1762587Sitojun *    may be used to endorse or promote products derived from this software
1862587Sitojun *    without specific prior written permission.
1962587Sitojun *
2062587Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2162587Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2262587Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2362587Sitojun * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2462587Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2562587Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2662587Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2762587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2862587Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2962587Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3062587Sitojun * SUCH DAMAGE.
3162587Sitojun */
3262587Sitojun
3362587Sitojun/*
3478064Sume * 6to4 interface, based on RFC3056.
3562587Sitojun *
3662587Sitojun * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
3762587Sitojun * There is no address mapping defined from IPv6 multicast address to IPv4
3862587Sitojun * address.  Therefore, we do not have IFF_MULTICAST on the interface.
3962587Sitojun *
4062587Sitojun * Due to the lack of address mapping for link-local addresses, we cannot
4162587Sitojun * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
4262587Sitojun * packets to link-local multicast addresses (ff02::x).
4362587Sitojun *
4462587Sitojun * Here are interesting symptoms due to the lack of link-local address:
4562587Sitojun *
4662587Sitojun * Unicast routing exchange:
4762587Sitojun * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
4862587Sitojun *   and link-local addresses as nexthop.
4962587Sitojun * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
5062587Sitojun *   assigned to the link, and makes use of them.  Also, HELLO packets use
5162587Sitojun *   link-local multicast addresses (ff02::5 and ff02::6).
5262587Sitojun * - BGP4+: Maybe.  You can only use global address as nexthop, and global
5362587Sitojun *   address as TCP endpoint address.
5462587Sitojun *
5562587Sitojun * Multicast routing protocols:
5662587Sitojun * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
5762587Sitojun *   Adjacent PIM routers must be configured manually (is it really spec-wise
5862587Sitojun *   correct thing to do?).
5962587Sitojun *
6062587Sitojun * ICMPv6:
6162587Sitojun * - Redirects cannot be used due to the lack of link-local address.
6262587Sitojun *
6378064Sume * stf interface does not have, and will not need, a link-local address.
6478064Sume * It seems to have no real benefit and does not help the above symptoms much.
6578064Sume * Even if we assign link-locals to interface, we cannot really
6678064Sume * use link-local unicast/multicast on top of 6to4 cloud (since there's no
6778064Sume * encapsulation defined for link-local address), and the above analysis does
6878064Sume * not change.  RFC3056 does not mandate the assignment of link-local address
6978064Sume * either.
7062587Sitojun *
7162587Sitojun * 6to4 interface has security issues.  Refer to
7262587Sitojun * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
7362587Sitojun * for details.  The code tries to filter out some of malicious packets.
7462587Sitojun * Note that there is no way to be 100% secure.
7562587Sitojun */
7662587Sitojun
7762587Sitojun#include "opt_inet.h"
7862587Sitojun#include "opt_inet6.h"
7962587Sitojun
8062587Sitojun#include <sys/param.h>
8162587Sitojun#include <sys/systm.h>
8262587Sitojun#include <sys/socket.h>
8362587Sitojun#include <sys/sockio.h>
8462587Sitojun#include <sys/mbuf.h>
8562587Sitojun#include <sys/errno.h>
8683655Sbrooks#include <sys/kernel.h>
87129880Sphk#include <sys/module.h>
8862587Sitojun#include <sys/protosw.h>
89178888Sjulian#include <sys/proc.h>
9083655Sbrooks#include <sys/queue.h>
91183351Sdwmalone#include <sys/sysctl.h>
9262587Sitojun#include <machine/cpu.h>
9362587Sitojun
9462587Sitojun#include <sys/malloc.h>
9562587Sitojun
9662587Sitojun#include <net/if.h>
97130933Sbrooks#include <net/if_clone.h>
9862587Sitojun#include <net/route.h>
9962587Sitojun#include <net/netisr.h>
10062587Sitojun#include <net/if_types.h>
10162587Sitojun#include <net/if_stf.h>
102196019Srwatson#include <net/vnet.h>
10362587Sitojun
10462587Sitojun#include <netinet/in.h>
10562587Sitojun#include <netinet/in_systm.h>
10662587Sitojun#include <netinet/ip.h>
10762587Sitojun#include <netinet/ip_var.h>
10862587Sitojun#include <netinet/in_var.h>
10962587Sitojun
11062587Sitojun#include <netinet/ip6.h>
11162587Sitojun#include <netinet6/ip6_var.h>
11262587Sitojun#include <netinet6/in6_var.h>
11362587Sitojun#include <netinet/ip_ecn.h>
11462587Sitojun
11562587Sitojun#include <netinet/ip_encap.h>
11662587Sitojun
11762587Sitojun#include <machine/stdarg.h>
11862587Sitojun
11962587Sitojun#include <net/bpf.h>
12062587Sitojun
121163606Srwatson#include <security/mac/mac_framework.h>
122163606Srwatson
123183351SdwmaloneSYSCTL_DECL(_net_link);
124227309Sedstatic SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW, 0, "6to4 Interface");
125183351Sdwmalone
126183351Sdwmalonestatic int stf_route_cache = 1;
127183351SdwmaloneSYSCTL_INT(_net_link_stf, OID_AUTO, route_cache, CTLFLAG_RW,
128183351Sdwmalone    &stf_route_cache, 0, "Caching of IPv4 routes for 6to4 Output");
129183351Sdwmalone
130244750Saestatic int stf_permit_rfc1918 = 0;
131244752SaeTUNABLE_INT("net.link.stf.permit_rfc1918", &stf_permit_rfc1918);
132244752SaeSYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RW | CTLFLAG_TUN,
133244750Sae    &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
134244750Sae
135130933Sbrooks#define STFUNIT		0
13683655Sbrooks
13762587Sitojun#define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
13862587Sitojun
139109322Ssuz/*
140109322Ssuz * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
141109322Ssuz * struct in_addr *; use bcopy() instead.
142109322Ssuz */
143249925Sglebius#define GET_V4(x)	(&(x)->s6_addr16[1])
144109322Ssuz
14562587Sitojunstruct stf_softc {
146147256Sbrooks	struct ifnet	*sc_ifp;
14762587Sitojun	union {
14862587Sitojun		struct route  __sc_ro4;
14962587Sitojun		struct route_in6 __sc_ro6; /* just for safety */
15062587Sitojun	} __sc_ro46;
15162587Sitojun#define sc_ro	__sc_ro46.__sc_ro4
152183351Sdwmalone	struct mtx	sc_ro_mtx;
153178888Sjulian	u_int	sc_fibnum;
15462587Sitojun	const struct encaptab *encap_cookie;
15562587Sitojun};
156147256Sbrooks#define STF2IFP(sc)	((sc)->sc_ifp)
15762587Sitojun
158241610Sglebiusstatic const char stfname[] = "stf";
159241610Sglebius
160126783Srwatson/*
161183351Sdwmalone * Note that mutable fields in the softc are not currently locked.
162183351Sdwmalone * We do lock sc_ro in stf_output though.
163126783Srwatson */
164241610Sglebiusstatic MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
165126709Srwatsonstatic const int ip_stf_ttl = 40;
16662587Sitojun
16779106Sbrooksextern  struct domain inetdomain;
168152242Srustruct protosw in_stf_protosw = {
169152242Sru	.pr_type =		SOCK_RAW,
170152242Sru	.pr_domain =		&inetdomain,
171152242Sru	.pr_protocol =		IPPROTO_IPV6,
172152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
173152242Sru	.pr_input =		in_stf_input,
174152242Sru	.pr_output =		(pr_output_t *)rip_output,
175152242Sru	.pr_ctloutput =		rip_ctloutput,
176152242Sru	.pr_usrreqs =		&rip_usrreqs
17779106Sbrooks};
17862587Sitojun
179130933Sbrooksstatic char *stfnames[] = {"stf0", "stf", "6to4", NULL};
180130933Sbrooks
18192725Salfredstatic int stfmodevent(module_t, int, void *);
18292725Salfredstatic int stf_encapcheck(const struct mbuf *, int, int, void *);
18392725Salfredstatic struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
184249925Sglebiusstatic int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
185191148Skmacy	struct route *);
186103475Sumestatic int isrfc1918addr(struct in_addr *);
18792725Salfredstatic int stf_checkaddr4(struct stf_softc *, struct in_addr *,
18892725Salfred	struct ifnet *);
18992725Salfredstatic int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
19092725Salfred	struct ifnet *);
19192725Salfredstatic void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
19292725Salfredstatic int stf_ioctl(struct ifnet *, u_long, caddr_t);
19362587Sitojun
194130933Sbrooksstatic int stf_clone_match(struct if_clone *, const char *);
195160195Ssamstatic int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
196130933Sbrooksstatic int stf_clone_destroy(struct if_clone *, struct ifnet *);
197241610Sglebiusstatic struct if_clone *stf_cloner;
19883655Sbrooks
199130933Sbrooksstatic int
200130933Sbrooksstf_clone_match(struct if_clone *ifc, const char *name)
201130933Sbrooks{
202130933Sbrooks	int i;
20383655Sbrooks
204130933Sbrooks	for(i = 0; stfnames[i] != NULL; i++) {
205130933Sbrooks		if (strcmp(stfnames[i], name) == 0)
206130933Sbrooks			return (1);
207130933Sbrooks	}
208130933Sbrooks
209130933Sbrooks	return (0);
210130933Sbrooks}
211130933Sbrooks
212128209Sbrooksstatic int
213160195Ssamstf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
21483655Sbrooks{
215130933Sbrooks	int err, unit;
21683655Sbrooks	struct stf_softc *sc;
217128417Sbrooks	struct ifnet *ifp;
21883655Sbrooks
219130933Sbrooks	/*
220130933Sbrooks	 * We can only have one unit, but since unit allocation is
221130933Sbrooks	 * already locked, we use it to keep from allocating extra
222130933Sbrooks	 * interfaces.
223130933Sbrooks	 */
224130933Sbrooks	unit = STFUNIT;
225130933Sbrooks	err = ifc_alloc_unit(ifc, &unit);
226130933Sbrooks	if (err != 0)
227130933Sbrooks		return (err);
228130933Sbrooks
229111119Simp	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
230147346Sbrooks	ifp = STF2IFP(sc) = if_alloc(IFT_STF);
231147256Sbrooks	if (ifp == NULL) {
232147256Sbrooks		free(sc, M_STF);
233147256Sbrooks		ifc_free_unit(ifc, unit);
234147256Sbrooks		return (ENOSPC);
235147256Sbrooks	}
236147346Sbrooks	ifp->if_softc = sc;
237178888Sjulian	sc->sc_fibnum = curthread->td_proc->p_fibnum;
238147346Sbrooks
239130933Sbrooks	/*
240130933Sbrooks	 * Set the name manually rather then using if_initname because
241130933Sbrooks	 * we don't conform to the default naming convention for interfaces.
242130933Sbrooks	 */
243130933Sbrooks	strlcpy(ifp->if_xname, name, IFNAMSIZ);
244241610Sglebius	ifp->if_dname = stfname;
245130933Sbrooks	ifp->if_dunit = IF_DUNIT_NONE;
24683655Sbrooks
247183351Sdwmalone	mtx_init(&(sc)->sc_ro_mtx, "stf ro", NULL, MTX_DEF);
24883655Sbrooks	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
24983655Sbrooks	    stf_encapcheck, &in_stf_protosw, sc);
25083655Sbrooks	if (sc->encap_cookie == NULL) {
251128417Sbrooks		if_printf(ifp, "attach failed\n");
25283655Sbrooks		free(sc, M_STF);
253130933Sbrooks		ifc_free_unit(ifc, unit);
25483655Sbrooks		return (ENOMEM);
25583655Sbrooks	}
25683655Sbrooks
257128417Sbrooks	ifp->if_mtu    = IPV6_MMTU;
258128417Sbrooks	ifp->if_ioctl  = stf_ioctl;
259128417Sbrooks	ifp->if_output = stf_output;
260207554Ssobomax	ifp->if_snd.ifq_maxlen = ifqmaxlen;
261128417Sbrooks	if_attach(ifp);
262147611Sdwmalone	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
26383655Sbrooks	return (0);
26483655Sbrooks}
26583655Sbrooks
266130933Sbrooksstatic int
267130933Sbrooksstf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
26883655Sbrooks{
269147256Sbrooks	struct stf_softc *sc = ifp->if_softc;
270151266Sthompsa	int err;
27183655Sbrooks
272151266Sthompsa	err = encap_detach(sc->encap_cookie);
273151266Sthompsa	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
274183351Sdwmalone	mtx_destroy(&(sc)->sc_ro_mtx);
275151266Sthompsa	bpfdetach(ifp);
276151266Sthompsa	if_detach(ifp);
277151266Sthompsa	if_free(ifp);
278151266Sthompsa
279151266Sthompsa	free(sc, M_STF);
280130933Sbrooks	ifc_free_unit(ifc, STFUNIT);
281130933Sbrooks
282130933Sbrooks	return (0);
28383655Sbrooks}
28483655Sbrooks
28579106Sbrooksstatic int
28679106Sbrooksstfmodevent(mod, type, data)
28779106Sbrooks	module_t mod;
28879106Sbrooks	int type;
28979106Sbrooks	void *data;
29062587Sitojun{
29162587Sitojun
29279106Sbrooks	switch (type) {
29379106Sbrooks	case MOD_LOAD:
294241610Sglebius		stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
295241610Sglebius		    stf_clone_create, stf_clone_destroy);
29679106Sbrooks		break;
29779106Sbrooks	case MOD_UNLOAD:
298241610Sglebius		if_clone_detach(stf_cloner);
29979106Sbrooks		break;
300132199Sphk	default:
301132199Sphk		return (EOPNOTSUPP);
30262587Sitojun	}
30379106Sbrooks
30479106Sbrooks	return (0);
30562587Sitojun}
30662587Sitojun
30779106Sbrooksstatic moduledata_t stf_mod = {
30879106Sbrooks	"if_stf",
30979106Sbrooks	stfmodevent,
310241394Skevlo	0
31179106Sbrooks};
31262587Sitojun
31379106SbrooksDECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
31479106Sbrooks
31562587Sitojunstatic int
31662587Sitojunstf_encapcheck(m, off, proto, arg)
31762587Sitojun	const struct mbuf *m;
31862587Sitojun	int off;
31962587Sitojun	int proto;
32062587Sitojun	void *arg;
32162587Sitojun{
32262587Sitojun	struct ip ip;
32362587Sitojun	struct in6_ifaddr *ia6;
32462587Sitojun	struct stf_softc *sc;
325108710Sfenner	struct in_addr a, b, mask;
32662587Sitojun
32762587Sitojun	sc = (struct stf_softc *)arg;
32862587Sitojun	if (sc == NULL)
32962587Sitojun		return 0;
33062587Sitojun
331147256Sbrooks	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
33262587Sitojun		return 0;
33362587Sitojun
33478064Sume	/* IFF_LINK0 means "no decapsulation" */
335147256Sbrooks	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
33678064Sume		return 0;
33778064Sume
33862587Sitojun	if (proto != IPPROTO_IPV6)
33962587Sitojun		return 0;
34062587Sitojun
34162587Sitojun	/* LINTED const cast */
34291452Speter	m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
34362587Sitojun
34462587Sitojun	if (ip.ip_v != 4)
34562587Sitojun		return 0;
34662587Sitojun
347147256Sbrooks	ia6 = stf_getsrcifa6(STF2IFP(sc));
34862587Sitojun	if (ia6 == NULL)
34962587Sitojun		return 0;
35062587Sitojun
35162587Sitojun	/*
35262587Sitojun	 * check if IPv4 dst matches the IPv4 address derived from the
35362587Sitojun	 * local 6to4 address.
35462587Sitojun	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
35562587Sitojun	 */
35662587Sitojun	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
357194812Srwatson	    sizeof(ip.ip_dst)) != 0) {
358194812Srwatson		ifa_free(&ia6->ia_ifa);
35962587Sitojun		return 0;
360194812Srwatson	}
36162587Sitojun
36262587Sitojun	/*
36362587Sitojun	 * check if IPv4 src matches the IPv4 address derived from the
36462587Sitojun	 * local 6to4 address masked by prefixmask.
36562587Sitojun	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
36662587Sitojun	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
36762587Sitojun	 */
36862587Sitojun	bzero(&a, sizeof(a));
369108710Sfenner	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
370108710Sfenner	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
371194812Srwatson	ifa_free(&ia6->ia_ifa);
372108710Sfenner	a.s_addr &= mask.s_addr;
37362587Sitojun	b = ip.ip_src;
374108710Sfenner	b.s_addr &= mask.s_addr;
37562587Sitojun	if (a.s_addr != b.s_addr)
37662587Sitojun		return 0;
37762587Sitojun
37862587Sitojun	/* stf interface makes single side match only */
37962587Sitojun	return 32;
38062587Sitojun}
38162587Sitojun
38262587Sitojunstatic struct in6_ifaddr *
38362587Sitojunstf_getsrcifa6(ifp)
38462587Sitojun	struct ifnet *ifp;
38562587Sitojun{
38662587Sitojun	struct ifaddr *ia;
38762587Sitojun	struct sockaddr_in6 *sin6;
38862587Sitojun	struct in_addr in;
38962587Sitojun
390195022Srwatson	if_addr_rlock(ifp);
391191339Srwatson	TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
39262587Sitojun		if (ia->ifa_addr->sa_family != AF_INET6)
39362587Sitojun			continue;
39462587Sitojun		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
39562587Sitojun		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
39662587Sitojun			continue;
39762587Sitojun
39862587Sitojun		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
399340958Seugen		if (!in_localip(in))
40062587Sitojun			continue;
40162587Sitojun
402194812Srwatson		ifa_ref(ia);
403195022Srwatson		if_addr_runlock(ifp);
40462587Sitojun		return (struct in6_ifaddr *)ia;
40562587Sitojun	}
406195022Srwatson	if_addr_runlock(ifp);
40762587Sitojun
40862587Sitojun	return NULL;
40962587Sitojun}
41062587Sitojun
41162587Sitojunstatic int
412249925Sglebiusstf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
413249925Sglebius	struct route *ro)
41462587Sitojun{
41562587Sitojun	struct stf_softc *sc;
416249925Sglebius	const struct sockaddr_in6 *dst6;
417183351Sdwmalone	struct route *cached_route;
418109322Ssuz	struct in_addr in4;
419249925Sglebius	const void *ptr;
42062587Sitojun	struct sockaddr_in *dst4;
42162587Sitojun	u_int8_t tos;
42262587Sitojun	struct ip *ip;
42362587Sitojun	struct ip6_hdr *ip6;
42462587Sitojun	struct in6_ifaddr *ia6;
425105580Srwatson	int error;
42662587Sitojun
427183351Sdwmalone#ifdef MAC
428172930Srwatson	error = mac_ifnet_check_transmit(ifp, m);
429105580Srwatson	if (error) {
430105580Srwatson		m_freem(m);
431105580Srwatson		return (error);
432105580Srwatson	}
433105580Srwatson#endif
434105580Srwatson
435147256Sbrooks	sc = ifp->if_softc;
436249925Sglebius	dst6 = (const struct sockaddr_in6 *)dst;
43762587Sitojun
43862587Sitojun	/* just in case */
43962587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
44062587Sitojun		m_freem(m);
441103487Sume		ifp->if_oerrors++;
44262587Sitojun		return ENETDOWN;
44362587Sitojun	}
44462587Sitojun
44562587Sitojun	/*
44662587Sitojun	 * If we don't have an ip4 address that match my inner ip6 address,
44762587Sitojun	 * we shouldn't generate output.  Without this check, we'll end up
44862587Sitojun	 * using wrong IPv4 source.
44962587Sitojun	 */
45062587Sitojun	ia6 = stf_getsrcifa6(ifp);
45162587Sitojun	if (ia6 == NULL) {
45262587Sitojun		m_freem(m);
453103487Sume		ifp->if_oerrors++;
45462587Sitojun		return ENETDOWN;
45562587Sitojun	}
45662587Sitojun
45762587Sitojun	if (m->m_len < sizeof(*ip6)) {
45862587Sitojun		m = m_pullup(m, sizeof(*ip6));
459103487Sume		if (!m) {
460194812Srwatson			ifa_free(&ia6->ia_ifa);
461103487Sume			ifp->if_oerrors++;
46262587Sitojun			return ENOBUFS;
463103487Sume		}
46462587Sitojun	}
46562587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
46662587Sitojun	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
46762587Sitojun
46878064Sume	/*
46978064Sume	 * Pickup the right outer dst addr from the list of candidates.
47078064Sume	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
47178064Sume	 */
472109322Ssuz	ptr = NULL;
47378064Sume	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
474109322Ssuz		ptr = GET_V4(&ip6->ip6_dst);
47578064Sume	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
476109322Ssuz		ptr = GET_V4(&dst6->sin6_addr);
47778064Sume	else {
478194812Srwatson		ifa_free(&ia6->ia_ifa);
47978064Sume		m_freem(m);
480103487Sume		ifp->if_oerrors++;
48178064Sume		return ENETUNREACH;
48278064Sume	}
483109322Ssuz	bcopy(ptr, &in4, sizeof(in4));
48478064Sume
485159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
48678701Sume		/*
48778701Sume		 * We need to prepend the address family as
48878701Sume		 * a four byte field.  Cons up a dummy header
48978701Sume		 * to pacify bpf.  This is safe because bpf
49078701Sume		 * will only read from the mbuf (i.e., it won't
49178701Sume		 * try to free it or keep a pointer a to it).
49278701Sume		 */
493249925Sglebius		u_int af = AF_INET6;
494123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
49578701Sume	}
49678701Sume
497243882Sglebius	M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
498103487Sume	if (m == NULL) {
499194812Srwatson		ifa_free(&ia6->ia_ifa);
500103487Sume		ifp->if_oerrors++;
50162587Sitojun		return ENOBUFS;
502103487Sume	}
50362587Sitojun	ip = mtod(m, struct ip *);
50462587Sitojun
50562587Sitojun	bzero(ip, sizeof(*ip));
50662587Sitojun
50762587Sitojun	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
50862587Sitojun	    &ip->ip_src, sizeof(ip->ip_src));
509194812Srwatson	ifa_free(&ia6->ia_ifa);
510109322Ssuz	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
51162587Sitojun	ip->ip_p = IPPROTO_IPV6;
51279106Sbrooks	ip->ip_ttl = ip_stf_ttl;
513241913Sglebius	ip->ip_len = htons(m->m_pkthdr.len);
51462587Sitojun	if (ifp->if_flags & IFF_LINK1)
51562587Sitojun		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
51678064Sume	else
51778064Sume		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
51862587Sitojun
519183351Sdwmalone	if (!stf_route_cache) {
520183351Sdwmalone		cached_route = NULL;
521183351Sdwmalone		goto sendit;
522183351Sdwmalone	}
523183351Sdwmalone
524126783Srwatson	/*
525183351Sdwmalone	 * Do we have a cached route?
526126783Srwatson	 */
527183351Sdwmalone	mtx_lock(&(sc)->sc_ro_mtx);
52862587Sitojun	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
52962587Sitojun	if (dst4->sin_family != AF_INET ||
53062587Sitojun	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
53162587Sitojun		/* cache route doesn't match */
53262587Sitojun		dst4->sin_family = AF_INET;
53362587Sitojun		dst4->sin_len = sizeof(struct sockaddr_in);
53462587Sitojun		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
53562587Sitojun		if (sc->sc_ro.ro_rt) {
53662587Sitojun			RTFREE(sc->sc_ro.ro_rt);
53762587Sitojun			sc->sc_ro.ro_rt = NULL;
53862587Sitojun		}
53962587Sitojun	}
54062587Sitojun
54162587Sitojun	if (sc->sc_ro.ro_rt == NULL) {
542178888Sjulian		rtalloc_fib(&sc->sc_ro, sc->sc_fibnum);
54362587Sitojun		if (sc->sc_ro.ro_rt == NULL) {
54462587Sitojun			m_freem(m);
545183351Sdwmalone			mtx_unlock(&(sc)->sc_ro_mtx);
546103487Sume			ifp->if_oerrors++;
54762587Sitojun			return ENETUNREACH;
54862587Sitojun		}
54962587Sitojun	}
550183351Sdwmalone	cached_route = &sc->sc_ro;
55162587Sitojun
552183351Sdwmalonesendit:
553178888Sjulian	M_SETFIB(m, sc->sc_fibnum);
554103487Sume	ifp->if_opackets++;
555183351Sdwmalone	error = ip_output(m, NULL, cached_route, 0, NULL, NULL);
556183351Sdwmalone
557183351Sdwmalone	if (cached_route != NULL)
558183351Sdwmalone		mtx_unlock(&(sc)->sc_ro_mtx);
559183351Sdwmalone	return error;
56062587Sitojun}
56162587Sitojun
56262587Sitojunstatic int
563103475Sumeisrfc1918addr(in)
564109322Ssuz	struct in_addr *in;
565103475Sume{
566103475Sume	/*
567103475Sume	 * returns 1 if private address range:
568103475Sume	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
569103475Sume	 */
570244750Sae	if (stf_permit_rfc1918 == 0 && (
571244750Sae	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
572109322Ssuz	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
573244750Sae	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
574103475Sume		return 1;
575103475Sume
576103475Sume	return 0;
577103475Sume}
578103475Sume
579103475Sumestatic int
58078064Sumestf_checkaddr4(sc, in, inifp)
58178064Sume	struct stf_softc *sc;
582109322Ssuz	struct in_addr *in;
58378064Sume	struct ifnet *inifp;	/* incoming interface */
58462587Sitojun{
58562587Sitojun	struct in_ifaddr *ia4;
58662587Sitojun
58762587Sitojun	/*
58862587Sitojun	 * reject packets with the following address:
58962587Sitojun	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
59062587Sitojun	 */
591109322Ssuz	if (IN_MULTICAST(ntohl(in->s_addr)))
59262587Sitojun		return -1;
593109322Ssuz	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
594109322Ssuz	case 0: case 127: case 255:
59562587Sitojun		return -1;
596109322Ssuz	}
59762587Sitojun
59862587Sitojun	/*
599103475Sume	 * reject packets with private address range.
600103475Sume	 * (requirement from RFC3056 section 2 1st paragraph)
601103475Sume	 */
602103475Sume	if (isrfc1918addr(in))
603103475Sume		return -1;
604103475Sume
605103475Sume	/*
60662587Sitojun	 * reject packets with broadcast
60762587Sitojun	 */
608194951Srwatson	IN_IFADDR_RLOCK();
609239357Sjhb	TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
61062587Sitojun		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
61162587Sitojun			continue;
612194951Srwatson		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
613194951Srwatson			IN_IFADDR_RUNLOCK();
61462587Sitojun			return -1;
615194951Srwatson		}
61662587Sitojun	}
617194951Srwatson	IN_IFADDR_RUNLOCK();
61862587Sitojun
61962587Sitojun	/*
62062587Sitojun	 * perform ingress filter
62162587Sitojun	 */
622147256Sbrooks	if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
62362587Sitojun		struct sockaddr_in sin;
62462587Sitojun		struct rtentry *rt;
62562587Sitojun
62662587Sitojun		bzero(&sin, sizeof(sin));
62762587Sitojun		sin.sin_family = AF_INET;
62862587Sitojun		sin.sin_len = sizeof(struct sockaddr_in);
629109322Ssuz		sin.sin_addr = *in;
630178888Sjulian		rt = rtalloc1_fib((struct sockaddr *)&sin, 0,
631178888Sjulian		    0UL, sc->sc_fibnum);
63278064Sume		if (!rt || rt->rt_ifp != inifp) {
63378064Sume#if 0
63478064Sume			log(LOG_WARNING, "%s: packet from 0x%x dropped "
635147256Sbrooks			    "due to ingress filter\n", if_name(STF2IFP(sc)),
63678064Sume			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
63778064Sume#endif
63878064Sume			if (rt)
639172307Scsjp				RTFREE_LOCKED(rt);
64062587Sitojun			return -1;
64162587Sitojun		}
642172307Scsjp		RTFREE_LOCKED(rt);
64362587Sitojun	}
64462587Sitojun
64562587Sitojun	return 0;
64662587Sitojun}
64762587Sitojun
64862587Sitojunstatic int
64978064Sumestf_checkaddr6(sc, in6, inifp)
65078064Sume	struct stf_softc *sc;
65162587Sitojun	struct in6_addr *in6;
65278064Sume	struct ifnet *inifp;	/* incoming interface */
65362587Sitojun{
65462587Sitojun	/*
65562587Sitojun	 * check 6to4 addresses
65662587Sitojun	 */
657109322Ssuz	if (IN6_IS_ADDR_6TO4(in6)) {
658109322Ssuz		struct in_addr in4;
659109322Ssuz		bcopy(GET_V4(in6), &in4, sizeof(in4));
660109322Ssuz		return stf_checkaddr4(sc, &in4, inifp);
661109322Ssuz	}
66262587Sitojun
66362587Sitojun	/*
66462587Sitojun	 * reject anything that look suspicious.  the test is implemented
66562587Sitojun	 * in ip6_input too, but we check here as well to
66662587Sitojun	 * (1) reject bad packets earlier, and
66762587Sitojun	 * (2) to be safe against future ip6_input change.
66862587Sitojun	 */
66962587Sitojun	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
67062587Sitojun		return -1;
67162587Sitojun
67262587Sitojun	return 0;
67362587Sitojun}
67462587Sitojun
67562587Sitojunvoid
67683187Sjulianin_stf_input(m, off)
67778064Sume	struct mbuf *m;
67883187Sjulian	int off;
67962587Sitojun{
68083187Sjulian	int proto;
68162587Sitojun	struct stf_softc *sc;
68262587Sitojun	struct ip *ip;
68362587Sitojun	struct ip6_hdr *ip6;
68462587Sitojun	u_int8_t otos, itos;
68562587Sitojun	struct ifnet *ifp;
68662587Sitojun
68782884Sjulian	proto = mtod(m, struct ip *)->ip_p;
68862587Sitojun
68962587Sitojun	if (proto != IPPROTO_IPV6) {
69062587Sitojun		m_freem(m);
69162587Sitojun		return;
69262587Sitojun	}
69362587Sitojun
69462587Sitojun	ip = mtod(m, struct ip *);
69562587Sitojun
69662587Sitojun	sc = (struct stf_softc *)encap_getarg(m);
69762587Sitojun
698147256Sbrooks	if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
69962587Sitojun		m_freem(m);
70062587Sitojun		return;
70162587Sitojun	}
70262587Sitojun
703147256Sbrooks	ifp = STF2IFP(sc);
70462587Sitojun
705105580Srwatson#ifdef MAC
706172930Srwatson	mac_ifnet_create_mbuf(ifp, m);
707105580Srwatson#endif
708105580Srwatson
70962587Sitojun	/*
71062587Sitojun	 * perform sanity check against outer src/dst.
71162587Sitojun	 * for source, perform ingress filter as well.
71262587Sitojun	 */
71378064Sume	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
71478064Sume	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
71562587Sitojun		m_freem(m);
71662587Sitojun		return;
71762587Sitojun	}
71862587Sitojun
71962587Sitojun	otos = ip->ip_tos;
72062587Sitojun	m_adj(m, off);
72162587Sitojun
72262587Sitojun	if (m->m_len < sizeof(*ip6)) {
72362587Sitojun		m = m_pullup(m, sizeof(*ip6));
72462587Sitojun		if (!m)
72562587Sitojun			return;
72662587Sitojun	}
72762587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
72862587Sitojun
72962587Sitojun	/*
73062587Sitojun	 * perform sanity check against inner src/dst.
73162587Sitojun	 * for source, perform ingress filter as well.
73262587Sitojun	 */
73378064Sume	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
73478064Sume	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
73562587Sitojun		m_freem(m);
73662587Sitojun		return;
73762587Sitojun	}
73862587Sitojun
73962587Sitojun	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
74062587Sitojun	if ((ifp->if_flags & IFF_LINK1) != 0)
74162587Sitojun		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
74278064Sume	else
74378064Sume		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
74462587Sitojun	ip6->ip6_flow &= ~htonl(0xff << 20);
74562587Sitojun	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
74662587Sitojun
74762587Sitojun	m->m_pkthdr.rcvif = ifp;
74862587Sitojun
749159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
75062587Sitojun		/*
75162587Sitojun		 * We need to prepend the address family as
75262587Sitojun		 * a four byte field.  Cons up a dummy header
75362587Sitojun		 * to pacify bpf.  This is safe because bpf
75462587Sitojun		 * will only read from the mbuf (i.e., it won't
75562587Sitojun		 * try to free it or keep a pointer a to it).
75662587Sitojun		 */
757123922Ssam		u_int32_t af = AF_INET6;
758140042Sume		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
75962587Sitojun	}
76062587Sitojun
76162587Sitojun	/*
76262587Sitojun	 * Put the packet to the network layer input queue according to the
76362587Sitojun	 * specified address family.
76462587Sitojun	 * See net/if_gif.c for possible issues with packet processing
76562587Sitojun	 * reorder due to extra queueing.
76662587Sitojun	 */
76762587Sitojun	ifp->if_ipackets++;
768111888Sjlemon	ifp->if_ibytes += m->m_pkthdr.len;
769223741Sbz	M_SETFIB(m, ifp->if_fib);
770111888Sjlemon	netisr_dispatch(NETISR_IPV6, m);
77162587Sitojun}
77262587Sitojun
77362587Sitojun/* ARGSUSED */
77462587Sitojunstatic void
77585074Srustf_rtrequest(cmd, rt, info)
77662587Sitojun	int cmd;
77762587Sitojun	struct rtentry *rt;
77885074Sru	struct rt_addrinfo *info;
77962587Sitojun{
780120727Ssam	RT_LOCK_ASSERT(rt);
781263478Sglebius	rt->rt_mtu = rt->rt_ifp->if_mtu;
78262587Sitojun}
78362587Sitojun
78462587Sitojunstatic int
78562587Sitojunstf_ioctl(ifp, cmd, data)
78662587Sitojun	struct ifnet *ifp;
78762587Sitojun	u_long cmd;
78862587Sitojun	caddr_t data;
78962587Sitojun{
79062587Sitojun	struct ifaddr *ifa;
79162587Sitojun	struct ifreq *ifr;
79262587Sitojun	struct sockaddr_in6 *sin6;
793109322Ssuz	struct in_addr addr;
794238492Smelifaro	int error, mtu;
79562587Sitojun
79662587Sitojun	error = 0;
79762587Sitojun	switch (cmd) {
79862587Sitojun	case SIOCSIFADDR:
79962587Sitojun		ifa = (struct ifaddr *)data;
80062587Sitojun		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
80162587Sitojun			error = EAFNOSUPPORT;
80262587Sitojun			break;
80362587Sitojun		}
80462587Sitojun		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
805109322Ssuz		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
80662587Sitojun			error = EINVAL;
807109322Ssuz			break;
808109322Ssuz		}
809109322Ssuz		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
810109322Ssuz		if (isrfc1918addr(&addr)) {
811109322Ssuz			error = EINVAL;
812109322Ssuz			break;
813109322Ssuz		}
814109322Ssuz
815109322Ssuz		ifa->ifa_rtrequest = stf_rtrequest;
816109322Ssuz		ifp->if_flags |= IFF_UP;
81762587Sitojun		break;
81862587Sitojun
81962587Sitojun	case SIOCADDMULTI:
82062587Sitojun	case SIOCDELMULTI:
82162587Sitojun		ifr = (struct ifreq *)data;
82262587Sitojun		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
82362587Sitojun			;
82462587Sitojun		else
82562587Sitojun			error = EAFNOSUPPORT;
82662587Sitojun		break;
82762587Sitojun
828238492Smelifaro	case SIOCGIFMTU:
829238492Smelifaro		break;
830238492Smelifaro
831238492Smelifaro	case SIOCSIFMTU:
832238492Smelifaro		ifr = (struct ifreq *)data;
833238492Smelifaro		mtu = ifr->ifr_mtu;
834238492Smelifaro		/* RFC 4213 3.2 ideal world MTU */
835238492Smelifaro		if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
836238492Smelifaro			return (EINVAL);
837238492Smelifaro		ifp->if_mtu = mtu;
838238492Smelifaro		break;
839238492Smelifaro
84062587Sitojun	default:
84162587Sitojun		error = EINVAL;
84262587Sitojun		break;
84362587Sitojun	}
84462587Sitojun
84562587Sitojun	return error;
84662587Sitojun}
847