162587Sitojun/*	$FreeBSD$	*/
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);
124248085Smariusstatic 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
130245258Saestatic int stf_permit_rfc1918 = 0;
131245258SaeTUNABLE_INT("net.link.stf.permit_rfc1918", &stf_permit_rfc1918);
132245258SaeSYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RW | CTLFLAG_TUN,
133245258Sae    &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
134245258Sae
13583655Sbrooks#define STFNAME		"stf"
136130933Sbrooks#define STFUNIT		0
13783655Sbrooks
13862587Sitojun#define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
13962587Sitojun
140109322Ssuz/*
141109322Ssuz * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
142109322Ssuz * struct in_addr *; use bcopy() instead.
143109322Ssuz */
144109322Ssuz#define GET_V4(x)	((caddr_t)(&(x)->s6_addr16[1]))
145109322Ssuz
14662587Sitojunstruct stf_softc {
147147256Sbrooks	struct ifnet	*sc_ifp;
14862587Sitojun	union {
14962587Sitojun		struct route  __sc_ro4;
15062587Sitojun		struct route_in6 __sc_ro6; /* just for safety */
15162587Sitojun	} __sc_ro46;
15262587Sitojun#define sc_ro	__sc_ro46.__sc_ro4
153183351Sdwmalone	struct mtx	sc_ro_mtx;
154178888Sjulian	u_int	sc_fibnum;
15562587Sitojun	const struct encaptab *encap_cookie;
15662587Sitojun};
157147256Sbrooks#define STF2IFP(sc)	((sc)->sc_ifp)
15862587Sitojun
159126783Srwatson/*
160183351Sdwmalone * Note that mutable fields in the softc are not currently locked.
161183351Sdwmalone * We do lock sc_ro in stf_output though.
162126783Srwatson */
16383655Sbrooksstatic MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
164126709Srwatsonstatic const int ip_stf_ttl = 40;
16562587Sitojun
16679106Sbrooksextern  struct domain inetdomain;
167152242Srustruct protosw in_stf_protosw = {
168152242Sru	.pr_type =		SOCK_RAW,
169152242Sru	.pr_domain =		&inetdomain,
170152242Sru	.pr_protocol =		IPPROTO_IPV6,
171152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
172152242Sru	.pr_input =		in_stf_input,
173152242Sru	.pr_output =		(pr_output_t *)rip_output,
174152242Sru	.pr_ctloutput =		rip_ctloutput,
175152242Sru	.pr_usrreqs =		&rip_usrreqs
17679106Sbrooks};
17762587Sitojun
178130933Sbrooksstatic char *stfnames[] = {"stf0", "stf", "6to4", NULL};
179130933Sbrooks
18092725Salfredstatic int stfmodevent(module_t, int, void *);
18192725Salfredstatic int stf_encapcheck(const struct mbuf *, int, int, void *);
18292725Salfredstatic struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
18392725Salfredstatic int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
184191148Skmacy	struct route *);
185103475Sumestatic int isrfc1918addr(struct in_addr *);
18692725Salfredstatic int stf_checkaddr4(struct stf_softc *, struct in_addr *,
18792725Salfred	struct ifnet *);
18892725Salfredstatic int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
18992725Salfred	struct ifnet *);
19092725Salfredstatic void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
19192725Salfredstatic int stf_ioctl(struct ifnet *, u_long, caddr_t);
19262587Sitojun
193130933Sbrooksstatic int stf_clone_match(struct if_clone *, const char *);
194160195Ssamstatic int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
195130933Sbrooksstatic int stf_clone_destroy(struct if_clone *, struct ifnet *);
196130933Sbrooksstruct if_clone stf_cloner = IFC_CLONE_INITIALIZER(STFNAME, NULL, 0,
197130933Sbrooks    NULL, stf_clone_match, stf_clone_create, stf_clone_destroy);
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);
244130933Sbrooks	ifp->if_dname = ifc->ifc_name;
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:
29483655Sbrooks		if_clone_attach(&stf_cloner);
29579106Sbrooks		break;
29679106Sbrooks	case MOD_UNLOAD:
29783655Sbrooks		if_clone_detach(&stf_cloner);
29879106Sbrooks		break;
299132199Sphk	default:
300132199Sphk		return (EOPNOTSUPP);
30162587Sitojun	}
30279106Sbrooks
30379106Sbrooks	return (0);
30462587Sitojun}
30562587Sitojun
30679106Sbrooksstatic moduledata_t stf_mod = {
30779106Sbrooks	"if_stf",
30879106Sbrooks	stfmodevent,
30979106Sbrooks	0
31079106Sbrooks};
31162587Sitojun
31279106SbrooksDECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
31379106Sbrooks
31462587Sitojunstatic int
31562587Sitojunstf_encapcheck(m, off, proto, arg)
31662587Sitojun	const struct mbuf *m;
31762587Sitojun	int off;
31862587Sitojun	int proto;
31962587Sitojun	void *arg;
32062587Sitojun{
32162587Sitojun	struct ip ip;
32262587Sitojun	struct in6_ifaddr *ia6;
32362587Sitojun	struct stf_softc *sc;
324108710Sfenner	struct in_addr a, b, mask;
32562587Sitojun
32662587Sitojun	sc = (struct stf_softc *)arg;
32762587Sitojun	if (sc == NULL)
32862587Sitojun		return 0;
32962587Sitojun
330147256Sbrooks	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
33162587Sitojun		return 0;
33262587Sitojun
33378064Sume	/* IFF_LINK0 means "no decapsulation" */
334147256Sbrooks	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
33578064Sume		return 0;
33678064Sume
33762587Sitojun	if (proto != IPPROTO_IPV6)
33862587Sitojun		return 0;
33962587Sitojun
34062587Sitojun	/* LINTED const cast */
34191452Speter	m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
34262587Sitojun
34362587Sitojun	if (ip.ip_v != 4)
34462587Sitojun		return 0;
34562587Sitojun
346147256Sbrooks	ia6 = stf_getsrcifa6(STF2IFP(sc));
34762587Sitojun	if (ia6 == NULL)
34862587Sitojun		return 0;
34962587Sitojun
35062587Sitojun	/*
35162587Sitojun	 * check if IPv4 dst matches the IPv4 address derived from the
35262587Sitojun	 * local 6to4 address.
35362587Sitojun	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
35462587Sitojun	 */
35562587Sitojun	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
356194812Srwatson	    sizeof(ip.ip_dst)) != 0) {
357194812Srwatson		ifa_free(&ia6->ia_ifa);
35862587Sitojun		return 0;
359194812Srwatson	}
36062587Sitojun
36162587Sitojun	/*
36262587Sitojun	 * check if IPv4 src matches the IPv4 address derived from the
36362587Sitojun	 * local 6to4 address masked by prefixmask.
36462587Sitojun	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
36562587Sitojun	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
36662587Sitojun	 */
36762587Sitojun	bzero(&a, sizeof(a));
368108710Sfenner	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
369108710Sfenner	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
370194812Srwatson	ifa_free(&ia6->ia_ifa);
371108710Sfenner	a.s_addr &= mask.s_addr;
37262587Sitojun	b = ip.ip_src;
373108710Sfenner	b.s_addr &= mask.s_addr;
37462587Sitojun	if (a.s_addr != b.s_addr)
37562587Sitojun		return 0;
37662587Sitojun
37762587Sitojun	/* stf interface makes single side match only */
37862587Sitojun	return 32;
37962587Sitojun}
38062587Sitojun
38162587Sitojunstatic struct in6_ifaddr *
38262587Sitojunstf_getsrcifa6(ifp)
38362587Sitojun	struct ifnet *ifp;
38462587Sitojun{
38562587Sitojun	struct ifaddr *ia;
38662587Sitojun	struct in_ifaddr *ia4;
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));
39984104Sjlemon		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
40062587Sitojun			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
40162587Sitojun				break;
40262587Sitojun		if (ia4 == NULL)
40362587Sitojun			continue;
40462587Sitojun
405194812Srwatson		ifa_ref(ia);
406195022Srwatson		if_addr_runlock(ifp);
40762587Sitojun		return (struct in6_ifaddr *)ia;
40862587Sitojun	}
409195022Srwatson	if_addr_runlock(ifp);
41062587Sitojun
41162587Sitojun	return NULL;
41262587Sitojun}
41362587Sitojun
41462587Sitojunstatic int
415191148Skmacystf_output(ifp, m, dst, ro)
41662587Sitojun	struct ifnet *ifp;
41762587Sitojun	struct mbuf *m;
41862587Sitojun	struct sockaddr *dst;
419191148Skmacy	struct route *ro;
42062587Sitojun{
42162587Sitojun	struct stf_softc *sc;
42262587Sitojun	struct sockaddr_in6 *dst6;
423183351Sdwmalone	struct route *cached_route;
424109322Ssuz	struct in_addr in4;
425109322Ssuz	caddr_t ptr;
42662587Sitojun	struct sockaddr_in *dst4;
42762587Sitojun	u_int8_t tos;
42862587Sitojun	struct ip *ip;
42962587Sitojun	struct ip6_hdr *ip6;
43062587Sitojun	struct in6_ifaddr *ia6;
431147611Sdwmalone	u_int32_t af;
432105580Srwatson	int error;
43362587Sitojun
434183351Sdwmalone#ifdef MAC
435172930Srwatson	error = mac_ifnet_check_transmit(ifp, m);
436105580Srwatson	if (error) {
437105580Srwatson		m_freem(m);
438105580Srwatson		return (error);
439105580Srwatson	}
440105580Srwatson#endif
441105580Srwatson
442147256Sbrooks	sc = ifp->if_softc;
44362587Sitojun	dst6 = (struct sockaddr_in6 *)dst;
44462587Sitojun
44562587Sitojun	/* just in case */
44662587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
44762587Sitojun		m_freem(m);
448103487Sume		ifp->if_oerrors++;
44962587Sitojun		return ENETDOWN;
45062587Sitojun	}
45162587Sitojun
45262587Sitojun	/*
45362587Sitojun	 * If we don't have an ip4 address that match my inner ip6 address,
45462587Sitojun	 * we shouldn't generate output.  Without this check, we'll end up
45562587Sitojun	 * using wrong IPv4 source.
45662587Sitojun	 */
45762587Sitojun	ia6 = stf_getsrcifa6(ifp);
45862587Sitojun	if (ia6 == NULL) {
45962587Sitojun		m_freem(m);
460103487Sume		ifp->if_oerrors++;
46162587Sitojun		return ENETDOWN;
46262587Sitojun	}
46362587Sitojun
46462587Sitojun	if (m->m_len < sizeof(*ip6)) {
46562587Sitojun		m = m_pullup(m, sizeof(*ip6));
466103487Sume		if (!m) {
467194812Srwatson			ifa_free(&ia6->ia_ifa);
468103487Sume			ifp->if_oerrors++;
46962587Sitojun			return ENOBUFS;
470103487Sume		}
47162587Sitojun	}
47262587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
47362587Sitojun	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
47462587Sitojun
47578064Sume	/*
476147611Sdwmalone	 * BPF writes need to be handled specially.
477147611Sdwmalone	 * This is a null operation, nothing here checks dst->sa_family.
478147611Sdwmalone	 */
479147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
480147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
481147611Sdwmalone		dst->sa_family = af;
482147611Sdwmalone	}
483147611Sdwmalone
484147611Sdwmalone	/*
48578064Sume	 * Pickup the right outer dst addr from the list of candidates.
48678064Sume	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
48778064Sume	 */
488109322Ssuz	ptr = NULL;
48978064Sume	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
490109322Ssuz		ptr = GET_V4(&ip6->ip6_dst);
49178064Sume	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
492109322Ssuz		ptr = GET_V4(&dst6->sin6_addr);
49378064Sume	else {
494194812Srwatson		ifa_free(&ia6->ia_ifa);
49578064Sume		m_freem(m);
496103487Sume		ifp->if_oerrors++;
49778064Sume		return ENETUNREACH;
49878064Sume	}
499109322Ssuz	bcopy(ptr, &in4, sizeof(in4));
50078064Sume
501159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
50278701Sume		/*
50378701Sume		 * We need to prepend the address family as
50478701Sume		 * a four byte field.  Cons up a dummy header
50578701Sume		 * to pacify bpf.  This is safe because bpf
50678701Sume		 * will only read from the mbuf (i.e., it won't
50778701Sume		 * try to free it or keep a pointer a to it).
50878701Sume		 */
509147611Sdwmalone		af = AF_INET6;
510123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
51178701Sume	}
51278701Sume
513111119Simp	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
51462587Sitojun	if (m && m->m_len < sizeof(struct ip))
51562587Sitojun		m = m_pullup(m, sizeof(struct ip));
516103487Sume	if (m == NULL) {
517194812Srwatson		ifa_free(&ia6->ia_ifa);
518103487Sume		ifp->if_oerrors++;
51962587Sitojun		return ENOBUFS;
520103487Sume	}
52162587Sitojun	ip = mtod(m, struct ip *);
52262587Sitojun
52362587Sitojun	bzero(ip, sizeof(*ip));
52462587Sitojun
52562587Sitojun	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
52662587Sitojun	    &ip->ip_src, sizeof(ip->ip_src));
527194812Srwatson	ifa_free(&ia6->ia_ifa);
528109322Ssuz	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
52962587Sitojun	ip->ip_p = IPPROTO_IPV6;
53079106Sbrooks	ip->ip_ttl = ip_stf_ttl;
53162587Sitojun	ip->ip_len = m->m_pkthdr.len;	/*host order*/
53262587Sitojun	if (ifp->if_flags & IFF_LINK1)
53362587Sitojun		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
53478064Sume	else
53578064Sume		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
53662587Sitojun
537183351Sdwmalone	if (!stf_route_cache) {
538183351Sdwmalone		cached_route = NULL;
539183351Sdwmalone		goto sendit;
540183351Sdwmalone	}
541183351Sdwmalone
542126783Srwatson	/*
543183351Sdwmalone	 * Do we have a cached route?
544126783Srwatson	 */
545183351Sdwmalone	mtx_lock(&(sc)->sc_ro_mtx);
54662587Sitojun	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
54762587Sitojun	if (dst4->sin_family != AF_INET ||
54862587Sitojun	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
54962587Sitojun		/* cache route doesn't match */
55062587Sitojun		dst4->sin_family = AF_INET;
55162587Sitojun		dst4->sin_len = sizeof(struct sockaddr_in);
55262587Sitojun		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
55362587Sitojun		if (sc->sc_ro.ro_rt) {
55462587Sitojun			RTFREE(sc->sc_ro.ro_rt);
55562587Sitojun			sc->sc_ro.ro_rt = NULL;
55662587Sitojun		}
55762587Sitojun	}
55862587Sitojun
55962587Sitojun	if (sc->sc_ro.ro_rt == NULL) {
560178888Sjulian		rtalloc_fib(&sc->sc_ro, sc->sc_fibnum);
56162587Sitojun		if (sc->sc_ro.ro_rt == NULL) {
56262587Sitojun			m_freem(m);
563183351Sdwmalone			mtx_unlock(&(sc)->sc_ro_mtx);
564103487Sume			ifp->if_oerrors++;
56562587Sitojun			return ENETUNREACH;
56662587Sitojun		}
56762587Sitojun	}
568183351Sdwmalone	cached_route = &sc->sc_ro;
56962587Sitojun
570183351Sdwmalonesendit:
571178888Sjulian	M_SETFIB(m, sc->sc_fibnum);
572103487Sume	ifp->if_opackets++;
573183351Sdwmalone	error = ip_output(m, NULL, cached_route, 0, NULL, NULL);
574183351Sdwmalone
575183351Sdwmalone	if (cached_route != NULL)
576183351Sdwmalone		mtx_unlock(&(sc)->sc_ro_mtx);
577183351Sdwmalone	return error;
57862587Sitojun}
57962587Sitojun
58062587Sitojunstatic int
581103475Sumeisrfc1918addr(in)
582109322Ssuz	struct in_addr *in;
583103475Sume{
584103475Sume	/*
585103475Sume	 * returns 1 if private address range:
586103475Sume	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
587103475Sume	 */
588245258Sae	if (stf_permit_rfc1918 == 0 && (
589245258Sae	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
590109322Ssuz	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
591245258Sae	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
592103475Sume		return 1;
593103475Sume
594103475Sume	return 0;
595103475Sume}
596103475Sume
597103475Sumestatic int
59878064Sumestf_checkaddr4(sc, in, inifp)
59978064Sume	struct stf_softc *sc;
600109322Ssuz	struct in_addr *in;
60178064Sume	struct ifnet *inifp;	/* incoming interface */
60262587Sitojun{
60362587Sitojun	struct in_ifaddr *ia4;
60462587Sitojun
60562587Sitojun	/*
60662587Sitojun	 * reject packets with the following address:
60762587Sitojun	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
60862587Sitojun	 */
609109322Ssuz	if (IN_MULTICAST(ntohl(in->s_addr)))
61062587Sitojun		return -1;
611109322Ssuz	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
612109322Ssuz	case 0: case 127: case 255:
61362587Sitojun		return -1;
614109322Ssuz	}
61562587Sitojun
61662587Sitojun	/*
617103475Sume	 * reject packets with private address range.
618103475Sume	 * (requirement from RFC3056 section 2 1st paragraph)
619103475Sume	 */
620103475Sume	if (isrfc1918addr(in))
621103475Sume		return -1;
622103475Sume
623103475Sume	/*
62462587Sitojun	 * reject packets with broadcast
62562587Sitojun	 */
626194951Srwatson	IN_IFADDR_RLOCK();
627181803Sbz	for (ia4 = TAILQ_FIRST(&V_in_ifaddrhead);
62862587Sitojun	     ia4;
62962587Sitojun	     ia4 = TAILQ_NEXT(ia4, ia_link))
63062587Sitojun	{
63162587Sitojun		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
63262587Sitojun			continue;
633194951Srwatson		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
634194951Srwatson			IN_IFADDR_RUNLOCK();
63562587Sitojun			return -1;
636194951Srwatson		}
63762587Sitojun	}
638194951Srwatson	IN_IFADDR_RUNLOCK();
63962587Sitojun
64062587Sitojun	/*
64162587Sitojun	 * perform ingress filter
64262587Sitojun	 */
643147256Sbrooks	if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
64462587Sitojun		struct sockaddr_in sin;
64562587Sitojun		struct rtentry *rt;
64662587Sitojun
64762587Sitojun		bzero(&sin, sizeof(sin));
64862587Sitojun		sin.sin_family = AF_INET;
64962587Sitojun		sin.sin_len = sizeof(struct sockaddr_in);
650109322Ssuz		sin.sin_addr = *in;
651178888Sjulian		rt = rtalloc1_fib((struct sockaddr *)&sin, 0,
652178888Sjulian		    0UL, sc->sc_fibnum);
65378064Sume		if (!rt || rt->rt_ifp != inifp) {
65478064Sume#if 0
65578064Sume			log(LOG_WARNING, "%s: packet from 0x%x dropped "
656147256Sbrooks			    "due to ingress filter\n", if_name(STF2IFP(sc)),
65778064Sume			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
65878064Sume#endif
65978064Sume			if (rt)
660172307Scsjp				RTFREE_LOCKED(rt);
66162587Sitojun			return -1;
66262587Sitojun		}
663172307Scsjp		RTFREE_LOCKED(rt);
66462587Sitojun	}
66562587Sitojun
66662587Sitojun	return 0;
66762587Sitojun}
66862587Sitojun
66962587Sitojunstatic int
67078064Sumestf_checkaddr6(sc, in6, inifp)
67178064Sume	struct stf_softc *sc;
67262587Sitojun	struct in6_addr *in6;
67378064Sume	struct ifnet *inifp;	/* incoming interface */
67462587Sitojun{
67562587Sitojun	/*
67662587Sitojun	 * check 6to4 addresses
67762587Sitojun	 */
678109322Ssuz	if (IN6_IS_ADDR_6TO4(in6)) {
679109322Ssuz		struct in_addr in4;
680109322Ssuz		bcopy(GET_V4(in6), &in4, sizeof(in4));
681109322Ssuz		return stf_checkaddr4(sc, &in4, inifp);
682109322Ssuz	}
68362587Sitojun
68462587Sitojun	/*
68562587Sitojun	 * reject anything that look suspicious.  the test is implemented
68662587Sitojun	 * in ip6_input too, but we check here as well to
68762587Sitojun	 * (1) reject bad packets earlier, and
68862587Sitojun	 * (2) to be safe against future ip6_input change.
68962587Sitojun	 */
69062587Sitojun	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
69162587Sitojun		return -1;
69262587Sitojun
69362587Sitojun	return 0;
69462587Sitojun}
69562587Sitojun
69662587Sitojunvoid
69783187Sjulianin_stf_input(m, off)
69878064Sume	struct mbuf *m;
69983187Sjulian	int off;
70062587Sitojun{
70183187Sjulian	int proto;
70262587Sitojun	struct stf_softc *sc;
70362587Sitojun	struct ip *ip;
70462587Sitojun	struct ip6_hdr *ip6;
70562587Sitojun	u_int8_t otos, itos;
70662587Sitojun	struct ifnet *ifp;
70762587Sitojun
70882884Sjulian	proto = mtod(m, struct ip *)->ip_p;
70962587Sitojun
71062587Sitojun	if (proto != IPPROTO_IPV6) {
71162587Sitojun		m_freem(m);
71262587Sitojun		return;
71362587Sitojun	}
71462587Sitojun
71562587Sitojun	ip = mtod(m, struct ip *);
71662587Sitojun
71762587Sitojun	sc = (struct stf_softc *)encap_getarg(m);
71862587Sitojun
719147256Sbrooks	if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
72062587Sitojun		m_freem(m);
72162587Sitojun		return;
72262587Sitojun	}
72362587Sitojun
724147256Sbrooks	ifp = STF2IFP(sc);
72562587Sitojun
726105580Srwatson#ifdef MAC
727172930Srwatson	mac_ifnet_create_mbuf(ifp, m);
728105580Srwatson#endif
729105580Srwatson
73062587Sitojun	/*
73162587Sitojun	 * perform sanity check against outer src/dst.
73262587Sitojun	 * for source, perform ingress filter as well.
73362587Sitojun	 */
73478064Sume	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
73578064Sume	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
73662587Sitojun		m_freem(m);
73762587Sitojun		return;
73862587Sitojun	}
73962587Sitojun
74062587Sitojun	otos = ip->ip_tos;
74162587Sitojun	m_adj(m, off);
74262587Sitojun
74362587Sitojun	if (m->m_len < sizeof(*ip6)) {
74462587Sitojun		m = m_pullup(m, sizeof(*ip6));
74562587Sitojun		if (!m)
74662587Sitojun			return;
74762587Sitojun	}
74862587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
74962587Sitojun
75062587Sitojun	/*
75162587Sitojun	 * perform sanity check against inner src/dst.
75262587Sitojun	 * for source, perform ingress filter as well.
75362587Sitojun	 */
75478064Sume	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
75578064Sume	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
75662587Sitojun		m_freem(m);
75762587Sitojun		return;
75862587Sitojun	}
75962587Sitojun
76062587Sitojun	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
76162587Sitojun	if ((ifp->if_flags & IFF_LINK1) != 0)
76262587Sitojun		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
76378064Sume	else
76478064Sume		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
76562587Sitojun	ip6->ip6_flow &= ~htonl(0xff << 20);
76662587Sitojun	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
76762587Sitojun
76862587Sitojun	m->m_pkthdr.rcvif = ifp;
76962587Sitojun
770159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
77162587Sitojun		/*
77262587Sitojun		 * We need to prepend the address family as
77362587Sitojun		 * a four byte field.  Cons up a dummy header
77462587Sitojun		 * to pacify bpf.  This is safe because bpf
77562587Sitojun		 * will only read from the mbuf (i.e., it won't
77662587Sitojun		 * try to free it or keep a pointer a to it).
77762587Sitojun		 */
778123922Ssam		u_int32_t af = AF_INET6;
779140042Sume		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
78062587Sitojun	}
78162587Sitojun
78262587Sitojun	/*
78362587Sitojun	 * Put the packet to the network layer input queue according to the
78462587Sitojun	 * specified address family.
78562587Sitojun	 * See net/if_gif.c for possible issues with packet processing
78662587Sitojun	 * reorder due to extra queueing.
78762587Sitojun	 */
78862587Sitojun	ifp->if_ipackets++;
789111888Sjlemon	ifp->if_ibytes += m->m_pkthdr.len;
790223741Sbz	M_SETFIB(m, ifp->if_fib);
791111888Sjlemon	netisr_dispatch(NETISR_IPV6, m);
79262587Sitojun}
79362587Sitojun
79462587Sitojun/* ARGSUSED */
79562587Sitojunstatic void
79685074Srustf_rtrequest(cmd, rt, info)
79762587Sitojun	int cmd;
79862587Sitojun	struct rtentry *rt;
79985074Sru	struct rt_addrinfo *info;
80062587Sitojun{
801120727Ssam	RT_LOCK_ASSERT(rt);
802248743Smelifaro	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
80362587Sitojun}
80462587Sitojun
80562587Sitojunstatic int
80662587Sitojunstf_ioctl(ifp, cmd, data)
80762587Sitojun	struct ifnet *ifp;
80862587Sitojun	u_long cmd;
80962587Sitojun	caddr_t data;
81062587Sitojun{
81162587Sitojun	struct ifaddr *ifa;
81262587Sitojun	struct ifreq *ifr;
81362587Sitojun	struct sockaddr_in6 *sin6;
814109322Ssuz	struct in_addr addr;
815248743Smelifaro	int error, mtu;
81662587Sitojun
81762587Sitojun	error = 0;
81862587Sitojun	switch (cmd) {
81962587Sitojun	case SIOCSIFADDR:
82062587Sitojun		ifa = (struct ifaddr *)data;
82162587Sitojun		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
82262587Sitojun			error = EAFNOSUPPORT;
82362587Sitojun			break;
82462587Sitojun		}
82562587Sitojun		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
826109322Ssuz		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
82762587Sitojun			error = EINVAL;
828109322Ssuz			break;
829109322Ssuz		}
830109322Ssuz		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
831109322Ssuz		if (isrfc1918addr(&addr)) {
832109322Ssuz			error = EINVAL;
833109322Ssuz			break;
834109322Ssuz		}
835109322Ssuz
836109322Ssuz		ifa->ifa_rtrequest = stf_rtrequest;
837109322Ssuz		ifp->if_flags |= IFF_UP;
83862587Sitojun		break;
83962587Sitojun
84062587Sitojun	case SIOCADDMULTI:
84162587Sitojun	case SIOCDELMULTI:
84262587Sitojun		ifr = (struct ifreq *)data;
84362587Sitojun		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
84462587Sitojun			;
84562587Sitojun		else
84662587Sitojun			error = EAFNOSUPPORT;
84762587Sitojun		break;
84862587Sitojun
849248743Smelifaro	case SIOCGIFMTU:
850248743Smelifaro		break;
851248743Smelifaro
852248743Smelifaro	case SIOCSIFMTU:
853248743Smelifaro		ifr = (struct ifreq *)data;
854248743Smelifaro		mtu = ifr->ifr_mtu;
855248743Smelifaro		/* RFC 4213 3.2 ideal world MTU */
856248743Smelifaro		if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
857248743Smelifaro			return (EINVAL);
858248743Smelifaro		ifp->if_mtu = mtu;
859248743Smelifaro		break;
860248743Smelifaro
86162587Sitojun	default:
86262587Sitojun		error = EINVAL;
86362587Sitojun		break;
86462587Sitojun	}
86562587Sitojun
86662587Sitojun	return error;
86762587Sitojun}
868