in6_gif.c revision 276148
1266077Sdes/*-
2266077Sdes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3266077Sdes * All rights reserved.
4266077Sdes *
5266077Sdes * Redistribution and use in source and binary forms, with or without
6266077Sdes * modification, are permitted provided that the following conditions
7266077Sdes * are met:
8266077Sdes * 1. Redistributions of source code must retain the above copyright
9266077Sdes *    notice, this list of conditions and the following disclaimer.
10266077Sdes * 2. Redistributions in binary form must reproduce the above copyright
11266077Sdes *    notice, this list of conditions and the following disclaimer in the
12266077Sdes *    documentation and/or other materials provided with the distribution.
13266077Sdes * 3. Neither the name of the project nor the names of its contributors
14266077Sdes *    may be used to endorse or promote products derived from this software
15266077Sdes *    without specific prior written permission.
16266077Sdes *
17266077Sdes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18266077Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19266077Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20266077Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21266077Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22266077Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23266077Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24266077Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25266077Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26266077Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27266077Sdes * SUCH DAMAGE.
28266077Sdes *
29266077Sdes *	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30266077Sdes */
31266077Sdes
32266077Sdes#include <sys/cdefs.h>
33266077Sdes__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 276148 2014-12-23 16:17:37Z ae $");
34266077Sdes
35266077Sdes#include "opt_inet.h"
36266077Sdes#include "opt_inet6.h"
37266077Sdes
38266077Sdes#include <sys/param.h>
39266077Sdes#include <sys/lock.h>
40266077Sdes#include <sys/rmlock.h>
41266077Sdes#include <sys/systm.h>
42266077Sdes#include <sys/socket.h>
43266077Sdes#include <sys/sockio.h>
44266077Sdes#include <sys/mbuf.h>
45266077Sdes#include <sys/errno.h>
46266077Sdes#include <sys/kernel.h>
47266077Sdes#include <sys/queue.h>
48266077Sdes#include <sys/syslog.h>
49266077Sdes#include <sys/sysctl.h>
50266077Sdes#include <sys/protosw.h>
51266077Sdes#include <sys/malloc.h>
52266077Sdes
53266077Sdes#include <net/if.h>
54266077Sdes#include <net/if_var.h>
55266077Sdes#include <net/route.h>
56266077Sdes#include <net/vnet.h>
57266077Sdes
58266077Sdes#include <netinet/in.h>
59266077Sdes#include <netinet/in_systm.h>
60266077Sdes#ifdef INET
61266077Sdes#include <netinet/ip.h>
62266077Sdes#endif
63266077Sdes#include <netinet/ip_encap.h>
64266077Sdes#ifdef INET6
65266077Sdes#include <netinet/ip6.h>
66266077Sdes#include <netinet6/ip6_var.h>
67266077Sdes#include <netinet6/in6_var.h>
68266077Sdes#endif
69266077Sdes#include <netinet/ip_ecn.h>
70266077Sdes#ifdef INET6
71266077Sdes#include <netinet6/ip6_ecn.h>
72266077Sdes#endif
73266077Sdes
74266077Sdes#include <net/if_gif.h>
75266077Sdes
76266077Sdes#define GIF_HLIM	30
77266077Sdesstatic VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
78266077Sdes#define	V_ip6_gif_hlim			VNET(ip6_gif_hlim)
79266077Sdes
80266077SdesSYSCTL_DECL(_net_inet6_ip6);
81266077SdesSYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_VNET | CTLFLAG_RW,
82266077Sdes    &VNET_NAME(ip6_gif_hlim), 0, "");
83266077Sdes
84266077Sdesint in6_gif_output(struct ifnet *, struct mbuf *, int, uint8_t);
85266077Sdesint in6_gif_encapcheck(const struct mbuf *, int, int, void *);
86266077Sdesint in6_gif_attach(struct gif_softc *);
87266077Sdes
88266077Sdesstatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
89266077Sdes			 struct ifnet *);
90266077Sdesstatic int in6_gif_input(struct mbuf **, int *, int);
91266077Sdes
92266077Sdesextern  struct domain inet6domain;
93266077Sdesstatic struct protosw in6_gif_protosw = {
94266077Sdes	.pr_type =	SOCK_RAW,
95266077Sdes	.pr_domain =	&inet6domain,
96266077Sdes	.pr_protocol =	0,			/* IPPROTO_IPV[46] */
97266077Sdes	.pr_flags =	PR_ATOMIC|PR_ADDR,
98266077Sdes	.pr_input =	in6_gif_input,
99266077Sdes	.pr_output =	rip6_output,
100266077Sdes	.pr_ctloutput =	rip6_ctloutput,
101266077Sdes	.pr_usrreqs =	&rip6_usrreqs
102266077Sdes};
103266077Sdes
104266077Sdesint
105266077Sdesin6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
106266077Sdes{
107266077Sdes	GIF_RLOCK_TRACKER;
108266077Sdes	struct gif_softc *sc = ifp->if_softc;
109266077Sdes	struct ip6_hdr *ip6;
110266077Sdes	int len;
111266077Sdes
112266077Sdes	/* prepend new IP header */
113	len = sizeof(struct ip6_hdr);
114#ifndef __NO_STRICT_ALIGNMENT
115	if (proto == IPPROTO_ETHERIP)
116		len += ETHERIP_ALIGN;
117#endif
118	M_PREPEND(m, len, M_NOWAIT);
119	if (m == NULL)
120		return (ENOBUFS);
121#ifndef __NO_STRICT_ALIGNMENT
122	if (proto == IPPROTO_ETHERIP) {
123		len = mtod(m, vm_offset_t) & 3;
124		KASSERT(len == 0 || len == ETHERIP_ALIGN,
125		    ("in6_gif_output: unexpected misalignment"));
126		m->m_data += len;
127		m->m_len -= ETHERIP_ALIGN;
128	}
129#endif
130
131	ip6 = mtod(m, struct ip6_hdr *);
132	GIF_RLOCK(sc);
133	if (sc->gif_family != AF_INET6) {
134		m_freem(m);
135		GIF_RUNLOCK(sc);
136		return (ENETDOWN);
137	}
138	bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
139	GIF_RUNLOCK(sc);
140
141	ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
142	ip6->ip6_nxt	= proto;
143	ip6->ip6_hlim	= V_ip6_gif_hlim;
144	/*
145	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
146	 * it is too painful to ask for resend of inner packet, to achieve
147	 * path MTU discovery for encapsulated packets.
148	 */
149	return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
150}
151
152static int
153in6_gif_input(struct mbuf **mp, int *offp, int proto)
154{
155	struct mbuf *m = *mp;
156	struct ifnet *gifp;
157	struct gif_softc *sc;
158	struct ip6_hdr *ip6;
159	uint8_t ecn;
160
161	sc = encap_getarg(m);
162	if (sc == NULL) {
163		m_freem(m);
164		IP6STAT_INC(ip6s_nogif);
165		return (IPPROTO_DONE);
166	}
167	gifp = GIF2IFP(sc);
168	if ((gifp->if_flags & IFF_UP) != 0) {
169		ip6 = mtod(m, struct ip6_hdr *);
170		ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
171		m_adj(m, *offp);
172		gif_input(m, gifp, proto, ecn);
173	} else {
174		m_freem(m);
175		IP6STAT_INC(ip6s_nogif);
176	}
177	return (IPPROTO_DONE);
178}
179
180/*
181 * validate outer address.
182 */
183static int
184gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
185    struct ifnet *ifp)
186{
187
188	GIF_RLOCK_ASSERT(sc);
189	/*
190	 * Check for address match.  Note that the check is for an incoming
191	 * packet.  We should compare the *source* address in our configuration
192	 * and the *destination* address of the packet, and vice versa.
193	 */
194	if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst) ||
195	    !IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src))
196		return (0);
197
198	/* martian filters on outer source - done in ip6_input */
199
200	/* ingress filters on outer source */
201	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
202		struct sockaddr_in6 sin6;
203		struct rtentry *rt;
204
205		bzero(&sin6, sizeof(sin6));
206		sin6.sin6_family = AF_INET6;
207		sin6.sin6_len = sizeof(struct sockaddr_in6);
208		sin6.sin6_addr = ip6->ip6_src;
209		sin6.sin6_scope_id = 0; /* XXX */
210
211		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
212		    sc->gif_fibnum);
213		if (!rt || rt->rt_ifp != ifp) {
214			if (rt)
215				RTFREE_LOCKED(rt);
216			return (0);
217		}
218		RTFREE_LOCKED(rt);
219	}
220
221	return (128 * 2);
222}
223
224/*
225 * we know that we are in IFF_UP, outer address available, and outer family
226 * matched the physical addr family.  see gif_encapcheck().
227 */
228int
229in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
230{
231	struct ip6_hdr ip6;
232	struct gif_softc *sc;
233	struct ifnet *ifp;
234
235	/* sanity check done in caller */
236	sc = (struct gif_softc *)arg;
237	GIF_RLOCK_ASSERT(sc);
238
239	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
240	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
241	return (gif_validate6(&ip6, sc, ifp));
242}
243
244int
245in6_gif_attach(struct gif_softc *sc)
246{
247
248	KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
249	sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
250	    (void *)&in6_gif_protosw, sc);
251	if (sc->gif_ecookie == NULL)
252		return (EEXIST);
253	return (0);
254}
255