xref: /openbsd/sys/netinet/ip_ipip.c (revision b2d46992)
1*b2d46992Sbluhm /*	$OpenBSD: ip_ipip.c,v 1.102 2024/05/17 20:44:36 bluhm Exp $ */
29a570b6dSangelos /*
39a570b6dSangelos  * The authors of this code are John Ioannidis (ji@tla.org),
49a570b6dSangelos  * Angelos D. Keromytis (kermit@csd.uch.gr) and
59a570b6dSangelos  * Niels Provos (provos@physnet.uni-hamburg.de).
69a570b6dSangelos  *
7b1efc16cSangelos  * The original version of this code was written by John Ioannidis
8b1efc16cSangelos  * for BSD/OS in Athens, Greece, in November 1995.
99a570b6dSangelos  *
109a570b6dSangelos  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
119a570b6dSangelos  * by Angelos D. Keromytis.
129a570b6dSangelos  *
139a570b6dSangelos  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
149a570b6dSangelos  * and Niels Provos.
159a570b6dSangelos  *
169a570b6dSangelos  * Additional features in 1999 by Angelos D. Keromytis.
179a570b6dSangelos  *
189a570b6dSangelos  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
199a570b6dSangelos  * Angelos D. Keromytis and Niels Provos.
200775ebe4Sangelos  * Copyright (c) 2001, Angelos D. Keromytis.
219a570b6dSangelos  *
220775ebe4Sangelos  * Permission to use, copy, and modify this software with or without fee
239a570b6dSangelos  * is hereby granted, provided that this entire notice is included in
249a570b6dSangelos  * all copies of any software which is or includes a copy or
259a570b6dSangelos  * modification of this software.
269a570b6dSangelos  * You may use this code under the GNU public license if you so wish. Please
279a570b6dSangelos  * contribute changes back to the authors under this freer than GPL license
289a570b6dSangelos  * so that we may further the use of strong encryption without limitations to
299a570b6dSangelos  * all.
309a570b6dSangelos  *
319a570b6dSangelos  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
329a570b6dSangelos  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
339a570b6dSangelos  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
349a570b6dSangelos  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
359a570b6dSangelos  * PURPOSE.
369a570b6dSangelos  */
379a570b6dSangelos 
389a570b6dSangelos /*
399a570b6dSangelos  * IP-inside-IP processing
409a570b6dSangelos  */
419a570b6dSangelos 
42ab2f8295Sbluhm #include "bpfilter.h"
43ab2f8295Sbluhm #include "gif.h"
440c8db51fShenning #include "pf.h"
450c8db51fShenning 
469a570b6dSangelos #include <sys/param.h>
4764c11355Sderaadt #include <sys/systm.h>
489a570b6dSangelos #include <sys/mbuf.h>
499a570b6dSangelos #include <sys/socket.h>
509a570b6dSangelos #include <sys/sysctl.h>
519a570b6dSangelos 
529a570b6dSangelos #include <net/if.h>
53ab2f8295Sbluhm #include <net/if_types.h>
540deb6685Smpi #include <net/if_var.h>
559a570b6dSangelos #include <net/route.h>
569a570b6dSangelos #include <net/netisr.h>
579acf6950Sangelos #include <net/bpf.h>
589a570b6dSangelos 
599a570b6dSangelos #include <netinet/in.h>
609a570b6dSangelos #include <netinet/ip.h>
619a570b6dSangelos #include <netinet/in_pcb.h>
629a570b6dSangelos #include <netinet/ip_var.h>
63940d25acSbluhm #include <netinet6/ip6_var.h>
649a570b6dSangelos #include <netinet/ip_ecn.h>
65ea784840Sbluhm #include <netinet/ip_ipip.h>
669a570b6dSangelos 
679a570b6dSangelos #ifdef MROUTING
689a570b6dSangelos #include <netinet/ip_mroute.h>
699a570b6dSangelos #endif
709a570b6dSangelos 
710c8db51fShenning #if NPF > 0
720c8db51fShenning #include <net/pfvar.h>
730c8db51fShenning #endif
740c8db51fShenning 
759a570b6dSangelos #ifdef ENCDEBUG
76698a75ddSbluhm #define DPRINTF(fmt, args...)						\
77698a75ddSbluhm 	do {								\
78698a75ddSbluhm 		if (encdebug)						\
79698a75ddSbluhm 			printf("%s: " fmt "\n", __func__, ## args);	\
80698a75ddSbluhm 	} while (0)
819a570b6dSangelos #else
82698a75ddSbluhm #define DPRINTF(fmt, args...)						\
83698a75ddSbluhm 	do { } while (0)
849a570b6dSangelos #endif
859a570b6dSangelos 
869a570b6dSangelos /*
879a570b6dSangelos  * We can control the acceptance of IP4 packets by altering the sysctl
889a570b6dSangelos  * net.inet.ipip.allow value.  Zero means drop them, all else is acceptance.
899a570b6dSangelos  */
909a570b6dSangelos int ipip_allow = 0;
919a570b6dSangelos 
9260dafcf9Sjca struct cpumem *ipipcounters;
9360dafcf9Sjca 
9460dafcf9Sjca void
ipip_init(void)9560dafcf9Sjca ipip_init(void)
9660dafcf9Sjca {
9760dafcf9Sjca 	ipipcounters = counters_alloc(ipips_ncounters);
9860dafcf9Sjca }
999a570b6dSangelos 
1009a570b6dSangelos /*
101ab2f8295Sbluhm  * Really only a wrapper for ipip_input_if(), for use with pr_input.
1029a570b6dSangelos  */
1039a570b6dSangelos int
ipip_input(struct mbuf ** mp,int * offp,int nxt,int af)104ab2f8295Sbluhm ipip_input(struct mbuf **mp, int *offp, int nxt, int af)
1059a570b6dSangelos {
106ab2f8295Sbluhm 	struct ifnet *ifp;
107ab2f8295Sbluhm 
108ae26b3edSitojun 	/* If we do not accept IP-in-IP explicitly, drop.  */
109f0c13189Sderaadt 	if (!ipip_allow && ((*mp)->m_flags & (M_AUTH|M_CONF)) == 0) {
110698a75ddSbluhm 		DPRINTF("dropped due to policy");
11160dafcf9Sjca 		ipipstat_inc(ipips_pdrops);
11236c0109eSbluhm 		m_freemp(mp);
1139a570b6dSangelos 		return IPPROTO_DONE;
1149a570b6dSangelos 	}
1159a570b6dSangelos 
116ab2f8295Sbluhm 	ifp = if_get((*mp)->m_pkthdr.ph_ifidx);
117ab2f8295Sbluhm 	if (ifp == NULL) {
118ab2f8295Sbluhm 		m_freemp(mp);
119ab2f8295Sbluhm 		return IPPROTO_DONE;
120ab2f8295Sbluhm 	}
121ab2f8295Sbluhm 	nxt = ipip_input_if(mp, offp, nxt, af, ifp);
122ab2f8295Sbluhm 	if_put(ifp);
123ab2f8295Sbluhm 
124ab2f8295Sbluhm 	return nxt;
1259a570b6dSangelos }
1269a570b6dSangelos 
1279a570b6dSangelos /*
1289a570b6dSangelos  * ipip_input gets called when we receive an IP{46} encapsulated packet,
1299a570b6dSangelos  * either because we got it at a real interface, or because AH or ESP
130fb492c37Smpi  * were being used in tunnel mode (in which case the ph_ifidx element
131fb492c37Smpi  * will contain the index of the encX interface associated with the
132fb492c37Smpi  * tunnel.
1339a570b6dSangelos  */
1349a570b6dSangelos 
13596be3d96Sbluhm int
ipip_input_if(struct mbuf ** mp,int * offp,int proto,int oaf,struct ifnet * ifp)136ab2f8295Sbluhm ipip_input_if(struct mbuf **mp, int *offp, int proto, int oaf,
137ab2f8295Sbluhm     struct ifnet *ifp)
1389a570b6dSangelos {
13996be3d96Sbluhm 	struct mbuf *m = *mp;
14064aa4cc7Sitojun 	struct sockaddr_in *sin;
1411bcb5a76Sbluhm 	struct ip *ip;
1429a570b6dSangelos #ifdef INET6
14364aa4cc7Sitojun 	struct sockaddr_in6 *sin6;
14421579e10Sclaudio 	struct ip6_hdr *ip6;
1459a570b6dSangelos #endif
14698a920fdSdlg 	int mode, hlen;
147c9857360Smarkus 	u_int8_t itos, otos;
148ea784840Sbluhm 	sa_family_t iaf;
1499a570b6dSangelos 
15060dafcf9Sjca 	ipipstat_inc(ipips_ipackets);
1519a570b6dSangelos 
152289a9ae9Sbluhm 	switch (oaf) {
153289a9ae9Sbluhm 	case AF_INET:
1549a570b6dSangelos 		hlen = sizeof(struct ip);
1559a570b6dSangelos 		break;
1569a570b6dSangelos #ifdef INET6
157289a9ae9Sbluhm 	case AF_INET6:
1589a570b6dSangelos 		hlen = sizeof(struct ip6_hdr);
1599a570b6dSangelos 		break;
1609a570b6dSangelos #endif
161795830a0Sangelos 	default:
162289a9ae9Sbluhm 		unhandled_af(oaf);
1639a570b6dSangelos 	}
1649a570b6dSangelos 
1659a570b6dSangelos 	/* Bring the IP header in the first mbuf, if not there already */
1666593f73dSangelos 	if (m->m_len < hlen) {
167eabae73bSbluhm 		if ((m = *mp = m_pullup(m, hlen)) == NULL) {
168698a75ddSbluhm 			DPRINTF("m_pullup() failed");
16960dafcf9Sjca 			ipipstat_inc(ipips_hdrops);
170458cfda4Sbluhm 			goto bad;
1719a570b6dSangelos 		}
1729a570b6dSangelos 	}
1739a570b6dSangelos 
1746593f73dSangelos 	/* Keep outer ecn field. */
175289a9ae9Sbluhm 	switch (oaf) {
176289a9ae9Sbluhm 	case AF_INET:
1771bcb5a76Sbluhm 		ip = mtod(m, struct ip *);
1781bcb5a76Sbluhm 		otos = ip->ip_tos;
179ae26b3edSitojun 		break;
1809a570b6dSangelos #ifdef INET6
181289a9ae9Sbluhm 	case AF_INET6:
18221579e10Sclaudio 		ip6 = mtod(m, struct ip6_hdr *);
18321579e10Sclaudio 		otos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
184ae26b3edSitojun 		break;
1859a570b6dSangelos #endif
186ae26b3edSitojun 	}
1879a570b6dSangelos 
1882b1f9644Sangelos 	/* Remove outer IP header */
1891bcb5a76Sbluhm 	KASSERT(*offp > 0);
1901bcb5a76Sbluhm 	m_adj(m, *offp);
1911bcb5a76Sbluhm 	*offp = 0;
1921bcb5a76Sbluhm 	ip = NULL;
1931bcb5a76Sbluhm #ifdef INET6
1941bcb5a76Sbluhm 	ip6 = NULL;
1951bcb5a76Sbluhm #endif
1966d386255Sangelos 
19721579e10Sclaudio 	switch (proto) {
19821579e10Sclaudio 	case IPPROTO_IPV4:
1999a570b6dSangelos 		hlen = sizeof(struct ip);
2009a570b6dSangelos 		break;
2019a570b6dSangelos 
2029a570b6dSangelos #ifdef INET6
20321579e10Sclaudio 	case IPPROTO_IPV6:
2049a570b6dSangelos 		hlen = sizeof(struct ip6_hdr);
2059a570b6dSangelos 		break;
2069a570b6dSangelos #endif
207ae26b3edSitojun 	default:
20860dafcf9Sjca 		ipipstat_inc(ipips_family);
209458cfda4Sbluhm 		goto bad;
2109a570b6dSangelos 	}
2119a570b6dSangelos 
2121bcb5a76Sbluhm 	/* Sanity check */
2131bcb5a76Sbluhm 	if (m->m_pkthdr.len < hlen) {
2141bcb5a76Sbluhm 		ipipstat_inc(ipips_hdrops);
215458cfda4Sbluhm 		goto bad;
2161bcb5a76Sbluhm 	}
2171bcb5a76Sbluhm 
2186593f73dSangelos 	/*
21921579e10Sclaudio 	 * Bring the inner header into the first mbuf, if not there already.
2206593f73dSangelos 	 */
2216593f73dSangelos 	if (m->m_len < hlen) {
222eabae73bSbluhm 		if ((m = *mp = m_pullup(m, hlen)) == NULL) {
223698a75ddSbluhm 			DPRINTF("m_pullup() failed");
22460dafcf9Sjca 			ipipstat_inc(ipips_hdrops);
225458cfda4Sbluhm 			goto bad;
2269a570b6dSangelos 		}
2279a570b6dSangelos 	}
2289a570b6dSangelos 
2299a570b6dSangelos 	/*
2309a570b6dSangelos 	 * RFC 1853 specifies that the inner TTL should not be touched on
2319a570b6dSangelos 	 * decapsulation. There's no reason this comment should be here, but
2329a570b6dSangelos 	 * this is as good as any a position.
2339a570b6dSangelos 	 */
2349a570b6dSangelos 
235ae26b3edSitojun 	/* Some sanity checks in the inner IP header */
23621579e10Sclaudio 	switch (proto) {
23721579e10Sclaudio 	case IPPROTO_IPV4:
238ca37dd6cSbluhm 		iaf = AF_INET;
2391bcb5a76Sbluhm 		ip = mtod(m, struct ip *);
2401bcb5a76Sbluhm 		hlen = ip->ip_hl << 2;
2411bcb5a76Sbluhm 		if (m->m_pkthdr.len < hlen) {
2421bcb5a76Sbluhm 			ipipstat_inc(ipips_hdrops);
243ab2f8295Sbluhm 			goto bad;
2441bcb5a76Sbluhm 		}
2451bcb5a76Sbluhm 		itos = ip->ip_tos;
246c9857360Smarkus 		mode = m->m_flags & (M_AUTH|M_CONF) ?
247c9857360Smarkus 		    ECN_ALLOWED_IPSEC : ECN_ALLOWED;
248412c1a0aSdlg 		if (!ip_ecn_egress(mode, &otos, &itos)) {
249698a75ddSbluhm 			DPRINTF("ip_ecn_egress() failed");
25060dafcf9Sjca 			ipipstat_inc(ipips_pdrops);
251458cfda4Sbluhm 			goto bad;
25274e8fb40Skjc 		}
253c9857360Smarkus 		/* re-calculate the checksum if ip_tos was changed */
254412c1a0aSdlg 		if (itos != ip->ip_tos)
255412c1a0aSdlg 			ip_tos_patch(ip, itos);
2569a570b6dSangelos 		break;
2579a570b6dSangelos #ifdef INET6
25821579e10Sclaudio 	case IPPROTO_IPV6:
259ca37dd6cSbluhm 		iaf = AF_INET6;
26021579e10Sclaudio 		ip6 = mtod(m, struct ip6_hdr *);
2619a570b6dSangelos 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
26274e8fb40Skjc 		if (!ip_ecn_egress(ECN_ALLOWED, &otos, &itos)) {
263698a75ddSbluhm 			DPRINTF("ip_ecn_egress() failed");
26460dafcf9Sjca 			ipipstat_inc(ipips_pdrops);
265458cfda4Sbluhm 			goto bad;
26674e8fb40Skjc 		}
2679a570b6dSangelos 		ip6->ip6_flow &= ~htonl(0xff << 20);
2689a570b6dSangelos 		ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
2699a570b6dSangelos 		break;
2709a570b6dSangelos #endif
2719a570b6dSangelos 	}
2729a570b6dSangelos 
2739a570b6dSangelos 	/* Check for local address spoofing. */
274ab2f8295Sbluhm 	if (!(ifp->if_flags & IFF_LOOPBACK) && ipip_allow != 2) {
275d33580a9Smpi 		struct sockaddr_storage ss;
276d33580a9Smpi 		struct rtentry *rt;
277d33580a9Smpi 
278d33580a9Smpi 		memset(&ss, 0, sizeof(ss));
279d33580a9Smpi 
2801bcb5a76Sbluhm 		if (ip) {
281d33580a9Smpi 			sin = (struct sockaddr_in *)&ss;
282d33580a9Smpi 			sin->sin_family = AF_INET;
283d33580a9Smpi 			sin->sin_len = sizeof(*sin);
2841bcb5a76Sbluhm 			sin->sin_addr = ip->ip_src;
2859a570b6dSangelos #ifdef INET6
286d33580a9Smpi 		} else if (ip6) {
287d33580a9Smpi 			sin6 = (struct sockaddr_in6 *)&ss;
288d33580a9Smpi 			sin6->sin6_family = AF_INET6;
289d33580a9Smpi 			sin6->sin6_len = sizeof(*sin6);
290d33580a9Smpi 			sin6->sin6_addr = ip6->ip6_src;
2919a570b6dSangelos #endif /* INET6 */
2929a570b6dSangelos 		}
2939c681c75Sbluhm 		rt = rtalloc(sstosa(&ss), 0, m->m_pkthdr.ph_rtableid);
294d33580a9Smpi 		if ((rt != NULL) && (rt->rt_flags & RTF_LOCAL)) {
29560dafcf9Sjca 			ipipstat_inc(ipips_spoof);
296d33580a9Smpi 			rtfree(rt);
297458cfda4Sbluhm 			goto bad;
2989a570b6dSangelos 		}
299d33580a9Smpi 		rtfree(rt);
3009a570b6dSangelos 	}
3019a570b6dSangelos 
3029a570b6dSangelos 	/* Statistics */
3031bcb5a76Sbluhm 	ipipstat_add(ipips_ibytes, m->m_pkthdr.len - hlen);
3049a570b6dSangelos 
305ab2f8295Sbluhm #if NBPFILTER > 0 && NGIF > 0
306ab2f8295Sbluhm 	if (ifp->if_type == IFT_GIF && ifp->if_bpf != NULL)
307ab2f8295Sbluhm 		bpf_mtap_af(ifp->if_bpf, iaf, m, BPF_DIRECTION_IN);
308ca37dd6cSbluhm #endif
309ca37dd6cSbluhm #if NPF > 0
310ca37dd6cSbluhm 	pf_pkt_addr_changed(m);
311ca37dd6cSbluhm #endif
312ca37dd6cSbluhm 
3139a570b6dSangelos 	/*
3149a570b6dSangelos 	 * Interface pointer stays the same; if no IPsec processing has
3159a570b6dSangelos 	 * been done (or will be done), this will point to a normal
3169a570b6dSangelos 	 * interface. Otherwise, it'll point to an enc interface, which
3179a570b6dSangelos 	 * will allow a packet filter to distinguish between secure and
3189a570b6dSangelos 	 * untrusted packets.
3199a570b6dSangelos 	 */
3209a570b6dSangelos 
32121579e10Sclaudio 	switch (proto) {
32221579e10Sclaudio 	case IPPROTO_IPV4:
323bb24c526Sbluhm 		return ip_input_if(mp, offp, proto, oaf, ifp);
3249a570b6dSangelos #ifdef INET6
32521579e10Sclaudio 	case IPPROTO_IPV6:
326bb24c526Sbluhm 		return ip6_input_if(mp, offp, proto, oaf, ifp);
327ae26b3edSitojun #endif
3289a570b6dSangelos 	}
329458cfda4Sbluhm  bad:
33036c0109eSbluhm 	m_freemp(mp);
33196be3d96Sbluhm 	return IPPROTO_DONE;
3329a570b6dSangelos }
3339a570b6dSangelos 
3349a570b6dSangelos int
ipip_output(struct mbuf ** mp,struct tdb * tdb)335ead5a062Sbluhm ipip_output(struct mbuf **mp, struct tdb *tdb)
3369a570b6dSangelos {
337ead5a062Sbluhm 	struct mbuf *m = *mp;
3382edaa7baSmpi 	u_int8_t tp, otos, itos;
3392edaa7baSmpi 	u_int64_t obytes;
3409a570b6dSangelos 	struct ip *ipo;
3419a570b6dSangelos #ifdef INET6
342d5b56032Sitojun 	struct ip6_hdr *ip6, *ip6o;
3439a570b6dSangelos #endif /* INET6 */
3443514aacbSmikeb #ifdef ENCDEBUG
3453514aacbSmikeb 	char buf[INET6_ADDRSTRLEN];
3463514aacbSmikeb #endif
347e4b51ed2Sbluhm 	int error;
3489a570b6dSangelos 
3496593f73dSangelos 	/* XXX Deal with empty TDB source/destination addresses. */
3509a570b6dSangelos 
3519a570b6dSangelos 	m_copydata(m, 0, 1, &tp);
3526593f73dSangelos 	tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
3539a570b6dSangelos 
3546593f73dSangelos 	switch (tdb->tdb_dst.sa.sa_family) {
3559a570b6dSangelos 	case AF_INET:
3566593f73dSangelos 		if (tdb->tdb_src.sa.sa_family != AF_INET ||
3576593f73dSangelos 		    tdb->tdb_src.sin.sin_addr.s_addr == INADDR_ANY ||
3586593f73dSangelos 		    tdb->tdb_dst.sin.sin_addr.s_addr == INADDR_ANY) {
3596593f73dSangelos 
360df8d9afdSjsg 			DPRINTF("unspecified tunnel endpoint address "
361698a75ddSbluhm 			    "in SA %s/%08x",
3623514aacbSmikeb 			    ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
363698a75ddSbluhm 			    ntohl(tdb->tdb_spi));
3646593f73dSangelos 
36560dafcf9Sjca 			ipipstat_inc(ipips_unspec);
366e4b51ed2Sbluhm 			error = EINVAL;
367e4b51ed2Sbluhm 			goto drop;
3689a570b6dSangelos 		}
3699a570b6dSangelos 
370ead5a062Sbluhm 		M_PREPEND(*mp, sizeof(struct ip), M_DONTWAIT);
371ead5a062Sbluhm 		if (*mp == NULL) {
372698a75ddSbluhm 			DPRINTF("M_PREPEND failed");
37360dafcf9Sjca 			ipipstat_inc(ipips_hdrops);
374e4b51ed2Sbluhm 			error = ENOBUFS;
375e4b51ed2Sbluhm 			goto drop;
3769a570b6dSangelos 		}
377ead5a062Sbluhm 		m = *mp;
3789a570b6dSangelos 
3799a570b6dSangelos 		ipo = mtod(m, struct ip *);
3809a570b6dSangelos 
3819a570b6dSangelos 		ipo->ip_v = IPVERSION;
3829a570b6dSangelos 		ipo->ip_hl = 5;
3839a570b6dSangelos 		ipo->ip_len = htons(m->m_pkthdr.len);
3849a570b6dSangelos 		ipo->ip_ttl = ip_defttl;
3859a570b6dSangelos 		ipo->ip_sum = 0;
3869a570b6dSangelos 		ipo->ip_src = tdb->tdb_src.sin.sin_addr;
3879a570b6dSangelos 		ipo->ip_dst = tdb->tdb_dst.sin.sin_addr;
3889a570b6dSangelos 
3899a570b6dSangelos 		/*
3909a570b6dSangelos 		 * We do the htons() to prevent snoopers from determining our
3919a570b6dSangelos 		 * endianness.
3929a570b6dSangelos 		 */
3939a570b6dSangelos 		ipo->ip_id = htons(ip_randomid());
3949a570b6dSangelos 
3956593f73dSangelos 		/* If the inner protocol is IP... */
3966593f73dSangelos 		if (tp == IPVERSION) {
3979a570b6dSangelos 			/* Save ECN notification */
3986593f73dSangelos 			m_copydata(m, sizeof(struct ip) +
3996593f73dSangelos 			    offsetof(struct ip, ip_tos),
4009a570b6dSangelos 			    sizeof(u_int8_t), (caddr_t) &itos);
4019a570b6dSangelos 
4029a570b6dSangelos 			ipo->ip_p = IPPROTO_IPIP;
4039a570b6dSangelos 
4049a570b6dSangelos 			/*
4056593f73dSangelos 			 * We should be keeping tunnel soft-state and
4066593f73dSangelos 			 * send back ICMPs if needed.
4079a570b6dSangelos 			 */
4086593f73dSangelos 			m_copydata(m, sizeof(struct ip) +
4096593f73dSangelos 			    offsetof(struct ip, ip_off),
4109a570b6dSangelos 			    sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
411faf7e06fSmpi 			ipo->ip_off = ntohs(ipo->ip_off);
41270d36f83Sangelos 			ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
413faf7e06fSmpi 			ipo->ip_off = htons(ipo->ip_off);
4149a570b6dSangelos 		}
4159a570b6dSangelos #ifdef INET6
4166593f73dSangelos 		else if (tp == (IPV6_VERSION >> 4)) {
4179a570b6dSangelos 			u_int32_t itos32;
4186593f73dSangelos 
4196593f73dSangelos 			/* Save ECN notification. */
4209a570b6dSangelos 			m_copydata(m, sizeof(struct ip) +
4219a570b6dSangelos 			    offsetof(struct ip6_hdr, ip6_flow),
4229a570b6dSangelos 			    sizeof(u_int32_t), (caddr_t) &itos32);
4239a570b6dSangelos 			itos = ntohl(itos32) >> 20;
4249a570b6dSangelos 			ipo->ip_p = IPPROTO_IPV6;
4259a570b6dSangelos 			ipo->ip_off = 0;
4269a570b6dSangelos 		}
4279a570b6dSangelos #endif /* INET6 */
4286593f73dSangelos 		else {
42960dafcf9Sjca 			ipipstat_inc(ipips_family);
430e4b51ed2Sbluhm 			error = EAFNOSUPPORT;
431e4b51ed2Sbluhm 			goto drop;
4329a570b6dSangelos 		}
4339a570b6dSangelos 
4349a570b6dSangelos 		otos = 0;
4359a570b6dSangelos 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
4369a570b6dSangelos 		ipo->ip_tos = otos;
437e4b51ed2Sbluhm 
438e4b51ed2Sbluhm 		obytes = m->m_pkthdr.len - sizeof(struct ip);
439e4b51ed2Sbluhm 		if (tdb->tdb_xform->xf_type == XF_IP4)
440e4b51ed2Sbluhm 			tdb->tdb_cur_bytes += obytes;
4419a570b6dSangelos 		break;
4429a570b6dSangelos 
4439a570b6dSangelos #ifdef INET6
4449a570b6dSangelos 	case AF_INET6:
4459a570b6dSangelos 		if (IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_dst.sin6.sin6_addr) ||
4466593f73dSangelos 		    tdb->tdb_src.sa.sa_family != AF_INET6 ||
4476593f73dSangelos 		    IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_src.sin6.sin6_addr)) {
4486593f73dSangelos 
449df8d9afdSjsg 			DPRINTF("unspecified tunnel endpoint address "
450698a75ddSbluhm 			    "in SA %s/%08x",
4513514aacbSmikeb 			    ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
452698a75ddSbluhm 			    ntohl(tdb->tdb_spi));
4536593f73dSangelos 
45460dafcf9Sjca 			ipipstat_inc(ipips_unspec);
455e4b51ed2Sbluhm 			error = EINVAL;
456e4b51ed2Sbluhm 			goto drop;
4579a570b6dSangelos 		}
4589a570b6dSangelos 
459424a21eaStodd 		/* If the inner protocol is IPv6, clear link local scope */
460424a21eaStodd 		if (tp == (IPV6_VERSION >> 4)) {
461d5b56032Sitojun 			/* scoped address handling */
462d5b56032Sitojun 			ip6 = mtod(m, struct ip6_hdr *);
46365b748d4Sitojun 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
464d5b56032Sitojun 				ip6->ip6_src.s6_addr16[1] = 0;
46565b748d4Sitojun 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
466d5b56032Sitojun 				ip6->ip6_dst.s6_addr16[1] = 0;
467424a21eaStodd 		}
468d5b56032Sitojun 
469ead5a062Sbluhm 		M_PREPEND(*mp, sizeof(struct ip6_hdr), M_DONTWAIT);
470ead5a062Sbluhm 		if (*mp == NULL) {
471698a75ddSbluhm 			DPRINTF("M_PREPEND failed");
47260dafcf9Sjca 			ipipstat_inc(ipips_hdrops);
473e4b51ed2Sbluhm 			error = ENOBUFS;
474e4b51ed2Sbluhm 			goto drop;
4759a570b6dSangelos 		}
476ead5a062Sbluhm 		m = *mp;
4779a570b6dSangelos 
4789a570b6dSangelos 		/* Initialize IPv6 header */
4799a570b6dSangelos 		ip6o = mtod(m, struct ip6_hdr *);
4809a570b6dSangelos 		ip6o->ip6_flow = 0;
4819a570b6dSangelos 		ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
4829a570b6dSangelos 		ip6o->ip6_vfc |= IPV6_VERSION;
483086d35e9Smarkus 		ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
484*b2d46992Sbluhm 		ip6o->ip6_hlim = ip6_defhlim;
485952c6363Sbluhm 		in6_embedscope(&ip6o->ip6_src, &tdb->tdb_src.sin6, NULL, NULL);
486952c6363Sbluhm 		in6_embedscope(&ip6o->ip6_dst, &tdb->tdb_dst.sin6, NULL, NULL);
4879a570b6dSangelos 
4886593f73dSangelos 		if (tp == IPVERSION) {
4899a570b6dSangelos 			/* Save ECN notification */
4909a570b6dSangelos 			m_copydata(m, sizeof(struct ip6_hdr) +
4919a570b6dSangelos 			    offsetof(struct ip, ip_tos), sizeof(u_int8_t),
4929a570b6dSangelos 			    (caddr_t) &itos);
4939a570b6dSangelos 
4946593f73dSangelos 			/* This is really IPVERSION. */
4956593f73dSangelos 			ip6o->ip6_nxt = IPPROTO_IPIP;
4969a570b6dSangelos 		}
4979a570b6dSangelos 		else
4986593f73dSangelos 			if (tp == (IPV6_VERSION >> 4)) {
4999a570b6dSangelos 				u_int32_t itos32;
5006593f73dSangelos 
5016593f73dSangelos 				/* Save ECN notification. */
5029a570b6dSangelos 				m_copydata(m, sizeof(struct ip6_hdr) +
5039a570b6dSangelos 				    offsetof(struct ip6_hdr, ip6_flow),
5049a570b6dSangelos 				    sizeof(u_int32_t), (caddr_t) &itos32);
5059a570b6dSangelos 				itos = ntohl(itos32) >> 20;
5069a570b6dSangelos 
5079a570b6dSangelos 				ip6o->ip6_nxt = IPPROTO_IPV6;
5086593f73dSangelos 			} else {
50960dafcf9Sjca 				ipipstat_inc(ipips_family);
510e4b51ed2Sbluhm 				error = EAFNOSUPPORT;
511e4b51ed2Sbluhm 				goto drop;
5129a570b6dSangelos 			}
5139a570b6dSangelos 
5149a570b6dSangelos 		otos = 0;
5159a570b6dSangelos 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
5169a570b6dSangelos 		ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
517e4b51ed2Sbluhm 
518e4b51ed2Sbluhm 		obytes = m->m_pkthdr.len - sizeof(struct ip6_hdr);
519e4b51ed2Sbluhm 		if (tdb->tdb_xform->xf_type == XF_IP4)
520e4b51ed2Sbluhm 			tdb->tdb_cur_bytes += obytes;
5219a570b6dSangelos 		break;
5229a570b6dSangelos #endif /* INET6 */
5239a570b6dSangelos 
5249a570b6dSangelos 	default:
525698a75ddSbluhm 		DPRINTF("unsupported protocol family %d",
526698a75ddSbluhm 		    tdb->tdb_dst.sa.sa_family);
527e4b51ed2Sbluhm 		ipipstat_inc(ipips_family);
528bc489a1cSbluhm 		error = EPFNOSUPPORT;
529e4b51ed2Sbluhm 		goto drop;
530e4b51ed2Sbluhm 	}
531e4b51ed2Sbluhm 
532e4b51ed2Sbluhm 	ipipstat_pkt(ipips_opackets, ipips_obytes, obytes);
533e4b51ed2Sbluhm 	return 0;
534e4b51ed2Sbluhm 
535e4b51ed2Sbluhm  drop:
536ead5a062Sbluhm 	m_freemp(mp);
537e4b51ed2Sbluhm 	return error;
5389a570b6dSangelos }
5399a570b6dSangelos 
5409a570b6dSangelos #ifdef IPSEC
5419a570b6dSangelos int
ipe4_attach(void)542c799dc6dSnaddy ipe4_attach(void)
5439a570b6dSangelos {
5449a570b6dSangelos 	return 0;
5459a570b6dSangelos }
5469a570b6dSangelos 
5479a570b6dSangelos int
ipe4_init(struct tdb * tdbp,const struct xformsw * xsp,struct ipsecinit * ii)5485e3836acSbluhm ipe4_init(struct tdb *tdbp, const struct xformsw *xsp, struct ipsecinit *ii)
5499a570b6dSangelos {
5509a570b6dSangelos 	tdbp->tdb_xform = xsp;
5519a570b6dSangelos 	return 0;
5529a570b6dSangelos }
5539a570b6dSangelos 
5549a570b6dSangelos int
ipe4_zeroize(struct tdb * tdbp)5559a570b6dSangelos ipe4_zeroize(struct tdb *tdbp)
5569a570b6dSangelos {
5579a570b6dSangelos 	return 0;
5589a570b6dSangelos }
5599a570b6dSangelos 
560dde15c32Sbluhm int
ipe4_input(struct mbuf ** mp,struct tdb * tdb,int hlen,int proto)561d5072c26Sbluhm ipe4_input(struct mbuf **mp, struct tdb *tdb, int hlen, int proto)
5629a570b6dSangelos {
5636593f73dSangelos 	/* This is a rather serious mistake, so no conditional printing. */
564698a75ddSbluhm 	printf("%s: should never be called\n", __func__);
565d5072c26Sbluhm 	m_freemp(mp);
566dde15c32Sbluhm 	return EINVAL;
5679a570b6dSangelos }
5689a570b6dSangelos #endif	/* IPSEC */
5699a570b6dSangelos 
5709a570b6dSangelos int
ipip_sysctl_ipipstat(void * oldp,size_t * oldlenp,void * newp)57160dafcf9Sjca ipip_sysctl_ipipstat(void *oldp, size_t *oldlenp, void *newp)
57260dafcf9Sjca {
57360dafcf9Sjca 	struct ipipstat ipipstat;
57460dafcf9Sjca 
57560dafcf9Sjca 	CTASSERT(sizeof(ipipstat) == (ipips_ncounters * sizeof(uint64_t)));
576c8915b1eSderaadt 	memset(&ipipstat, 0, sizeof ipipstat);
577bf0d449cSmpi 	counters_read(ipipcounters, (uint64_t *)&ipipstat, ipips_ncounters,
578bf0d449cSmpi 	    NULL);
57960dafcf9Sjca 	return (sysctl_rdstruct(oldp, oldlenp, newp,
58060dafcf9Sjca 	    &ipipstat, sizeof(ipipstat)));
58160dafcf9Sjca }
58260dafcf9Sjca 
58360dafcf9Sjca int
ipip_sysctl(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen)5846593f73dSangelos ipip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
5856593f73dSangelos     size_t newlen)
5869a570b6dSangelos {
587df4a15bcSmpi 	int error;
588df4a15bcSmpi 
5899a570b6dSangelos 	/* All sysctl names at this level are terminal. */
5909a570b6dSangelos 	if (namelen != 1)
5919a570b6dSangelos 		return (ENOTDIR);
5929a570b6dSangelos 
5939a570b6dSangelos 	switch (name[0]) {
5949a570b6dSangelos 	case IPIPCTL_ALLOW:
595df4a15bcSmpi 		NET_LOCK();
59670464ff2Sgnezdo 		error = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
59770464ff2Sgnezdo 		    &ipip_allow, 0, 2);
598df4a15bcSmpi 		NET_UNLOCK();
599df4a15bcSmpi 		return (error);
600ab46c28dSderaadt 	case IPIPCTL_STATS:
60160dafcf9Sjca 		return (ipip_sysctl_ipipstat(oldp, oldlenp, newp));
6029a570b6dSangelos 	default:
6039a570b6dSangelos 		return (ENOPROTOOPT);
6049a570b6dSangelos 	}
6059a570b6dSangelos 	/* NOTREACHED */
6069a570b6dSangelos }
607