in6_gif.c revision 147256
11573Srgrimes/*	$FreeBSD: head/sys/netinet6/in6_gif.c 147256 2005-06-10 16:49:24Z brooks $	*/
21573Srgrimes/*	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $	*/
31573Srgrimes
41573Srgrimes/*-
51573Srgrimes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
61573Srgrimes * All rights reserved.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. Neither the name of the project nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#include "opt_inet.h"
341573Srgrimes#include "opt_inet6.h"
351573Srgrimes
361573Srgrimes#include <sys/param.h>
371573Srgrimes#include <sys/systm.h>
381573Srgrimes#include <sys/socket.h>
391573Srgrimes#include <sys/sockio.h>
401573Srgrimes#include <sys/mbuf.h>
411573Srgrimes#include <sys/errno.h>
421573Srgrimes#include <sys/queue.h>
431573Srgrimes#include <sys/syslog.h>
441573Srgrimes#include <sys/protosw.h>
451573Srgrimes
461573Srgrimes#include <sys/malloc.h>
471573Srgrimes
481573Srgrimes#include <net/if.h>
491573Srgrimes#include <net/route.h>
501573Srgrimes
511573Srgrimes#include <netinet/in.h>
521573Srgrimes#include <netinet/in_systm.h>
531573Srgrimes#ifdef INET
541573Srgrimes#include <netinet/ip.h>
551573Srgrimes#endif
561573Srgrimes#include <netinet/ip_encap.h>
571573Srgrimes#ifdef INET6
581573Srgrimes#include <netinet/ip6.h>
591573Srgrimes#include <netinet6/ip6_var.h>
601573Srgrimes#include <netinet6/in6_gif.h>
611573Srgrimes#include <netinet6/in6_var.h>
621573Srgrimes#endif
631573Srgrimes#include <netinet6/ip6protosw.h>
641573Srgrimes#include <netinet/ip_ecn.h>
651573Srgrimes#ifdef INET6
661573Srgrimes#include <netinet6/ip6_ecn.h>
671573Srgrimes#endif
681573Srgrimes
691573Srgrimes#include <net/if_gif.h>
701573Srgrimes
711573Srgrimes#include <net/net_osdep.h>
721573Srgrimes
731573Srgrimesstatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
741573Srgrimes			 struct ifnet *);
751573Srgrimes
761573Srgrimesextern  struct domain inet6domain;
771573Srgrimesstruct ip6protosw in6_gif_protosw =
781573Srgrimes{ SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
791573Srgrimes  in6_gif_input, rip6_output,	0,		rip6_ctloutput,
801573Srgrimes  0,
811573Srgrimes  0,		0,		0,		0,
821573Srgrimes  &rip6_usrreqs
831573Srgrimes};
841573Srgrimes
851573Srgrimesint
861573Srgrimesin6_gif_output(ifp, family, m)
871573Srgrimes	struct ifnet *ifp;
881573Srgrimes	int family; /* family of the packet to be encapsulate. */
891573Srgrimes	struct mbuf *m;
901573Srgrimes{
911573Srgrimes	struct gif_softc *sc = ifp->if_softc;
921573Srgrimes	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
931573Srgrimes	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
941573Srgrimes	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
951573Srgrimes	struct ip6_hdr *ip6;
961573Srgrimes	int proto, error;
971573Srgrimes	u_int8_t itos, otos;
981573Srgrimes
991573Srgrimes	if (sin6_src == NULL || sin6_dst == NULL ||
1001573Srgrimes	    sin6_src->sin6_family != AF_INET6 ||
1011573Srgrimes	    sin6_dst->sin6_family != AF_INET6) {
1021573Srgrimes		m_freem(m);
1031573Srgrimes		return EAFNOSUPPORT;
1041573Srgrimes	}
1051573Srgrimes
1061573Srgrimes	switch (family) {
1071573Srgrimes#ifdef INET
1081573Srgrimes	case AF_INET:
1091573Srgrimes	    {
1101573Srgrimes		struct ip *ip;
1111573Srgrimes
1121573Srgrimes		proto = IPPROTO_IPV4;
1131573Srgrimes		if (m->m_len < sizeof(*ip)) {
1141573Srgrimes			m = m_pullup(m, sizeof(*ip));
1151573Srgrimes			if (!m)
1161573Srgrimes				return ENOBUFS;
1171573Srgrimes		}
1181573Srgrimes		ip = mtod(m, struct ip *);
1191573Srgrimes		itos = ip->ip_tos;
1201573Srgrimes		break;
1211573Srgrimes	    }
1221573Srgrimes#endif
1231573Srgrimes#ifdef INET6
1241573Srgrimes	case AF_INET6:
1251573Srgrimes	    {
1261573Srgrimes		struct ip6_hdr *ip6;
1271573Srgrimes		proto = IPPROTO_IPV6;
1281573Srgrimes		if (m->m_len < sizeof(*ip6)) {
1291573Srgrimes			m = m_pullup(m, sizeof(*ip6));
1301573Srgrimes			if (!m)
1311573Srgrimes				return ENOBUFS;
1321573Srgrimes		}
1331573Srgrimes		ip6 = mtod(m, struct ip6_hdr *);
1341573Srgrimes		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1351573Srgrimes		break;
1361573Srgrimes	    }
1371573Srgrimes#endif
1381573Srgrimes	default:
1391573Srgrimes#ifdef DEBUG
1401573Srgrimes		printf("in6_gif_output: warning: unknown family %d passed\n",
1411573Srgrimes			family);
1421573Srgrimes#endif
1431573Srgrimes		m_freem(m);
1441573Srgrimes		return EAFNOSUPPORT;
1451573Srgrimes	}
1461573Srgrimes
1471573Srgrimes	/* prepend new IP header */
1481573Srgrimes	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
1491573Srgrimes	if (m && m->m_len < sizeof(struct ip6_hdr))
1501573Srgrimes		m = m_pullup(m, sizeof(struct ip6_hdr));
1511573Srgrimes	if (m == NULL) {
1521573Srgrimes		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
1531573Srgrimes		return ENOBUFS;
1541573Srgrimes	}
1551573Srgrimes
1561573Srgrimes	ip6 = mtod(m, struct ip6_hdr *);
1571573Srgrimes	ip6->ip6_flow	= 0;
1581573Srgrimes	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
159	ip6->ip6_vfc	|= IPV6_VERSION;
160	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
161	ip6->ip6_nxt	= proto;
162	ip6->ip6_hlim	= ip6_gif_hlim;
163	ip6->ip6_src	= sin6_src->sin6_addr;
164	/* bidirectional configured tunnel mode */
165	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
166		ip6->ip6_dst = sin6_dst->sin6_addr;
167	else  {
168		m_freem(m);
169		return ENETUNREACH;
170	}
171	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
172		       &otos, &itos);
173	ip6->ip6_flow &= ~htonl(0xff << 20);
174	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
175
176	if (dst->sin6_family != sin6_dst->sin6_family ||
177	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
178		/* cache route doesn't match */
179		bzero(dst, sizeof(*dst));
180		dst->sin6_family = sin6_dst->sin6_family;
181		dst->sin6_len = sizeof(struct sockaddr_in6);
182		dst->sin6_addr = sin6_dst->sin6_addr;
183		if (sc->gif_ro6.ro_rt) {
184			RTFREE(sc->gif_ro6.ro_rt);
185			sc->gif_ro6.ro_rt = NULL;
186		}
187#if 0
188		GIF2IFP(sc)->if_mtu = GIF_MTU;
189#endif
190	}
191
192	if (sc->gif_ro6.ro_rt == NULL) {
193		rtalloc((struct route *)&sc->gif_ro6);
194		if (sc->gif_ro6.ro_rt == NULL) {
195			m_freem(m);
196			return ENETUNREACH;
197		}
198
199		/* if it constitutes infinite encapsulation, punt. */
200		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
201			m_freem(m);
202			return ENETUNREACH;	/*XXX*/
203		}
204#if 0
205		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
206			- sizeof(struct ip6_hdr);
207#endif
208	}
209
210#ifdef IPV6_MINMTU
211	/*
212	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
213	 * it is too painful to ask for resend of inner packet, to achieve
214	 * path MTU discovery for encapsulated packets.
215	 */
216	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
217#else
218	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
219#endif
220
221	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
222	    sc->gif_ro6.ro_rt != NULL) {
223		RTFREE(sc->gif_ro6.ro_rt);
224		sc->gif_ro6.ro_rt = NULL;
225	}
226
227	return (error);
228}
229
230int
231in6_gif_input(mp, offp, proto)
232	struct mbuf **mp;
233	int *offp, proto;
234{
235	struct mbuf *m = *mp;
236	struct ifnet *gifp = NULL;
237	struct ip6_hdr *ip6;
238	int af = 0;
239	u_int32_t otos;
240
241	ip6 = mtod(m, struct ip6_hdr *);
242
243	gifp = (struct ifnet *)encap_getarg(m);
244
245	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
246		m_freem(m);
247		ip6stat.ip6s_nogif++;
248		return IPPROTO_DONE;
249	}
250
251	otos = ip6->ip6_flow;
252	m_adj(m, *offp);
253
254	switch (proto) {
255#ifdef INET
256	case IPPROTO_IPV4:
257	    {
258		struct ip *ip;
259		u_int8_t otos8;
260		af = AF_INET;
261		otos8 = (ntohl(otos) >> 20) & 0xff;
262		if (m->m_len < sizeof(*ip)) {
263			m = m_pullup(m, sizeof(*ip));
264			if (!m)
265				return IPPROTO_DONE;
266		}
267		ip = mtod(m, struct ip *);
268		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
269				  ECN_ALLOWED : ECN_NOCARE,
270				  &otos8, &ip->ip_tos) == 0) {
271			m_freem(m);
272			return IPPROTO_DONE;
273		}
274		break;
275	    }
276#endif /* INET */
277#ifdef INET6
278	case IPPROTO_IPV6:
279	    {
280		struct ip6_hdr *ip6;
281		af = AF_INET6;
282		if (m->m_len < sizeof(*ip6)) {
283			m = m_pullup(m, sizeof(*ip6));
284			if (!m)
285				return IPPROTO_DONE;
286		}
287		ip6 = mtod(m, struct ip6_hdr *);
288		if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
289				   ECN_ALLOWED : ECN_NOCARE,
290				   &otos, &ip6->ip6_flow) == 0) {
291			m_freem(m);
292			return IPPROTO_DONE;
293		}
294		break;
295	    }
296#endif
297	default:
298		ip6stat.ip6s_nogif++;
299		m_freem(m);
300		return IPPROTO_DONE;
301	}
302
303	gif_input(m, af, gifp);
304	return IPPROTO_DONE;
305}
306
307/*
308 * validate outer address.
309 */
310static int
311gif_validate6(ip6, sc, ifp)
312	const struct ip6_hdr *ip6;
313	struct gif_softc *sc;
314	struct ifnet *ifp;
315{
316	struct sockaddr_in6 *src, *dst;
317
318	src = (struct sockaddr_in6 *)sc->gif_psrc;
319	dst = (struct sockaddr_in6 *)sc->gif_pdst;
320
321	/*
322	 * Check for address match.  Note that the check is for an incoming
323	 * packet.  We should compare the *source* address in our configuration
324	 * and the *destination* address of the packet, and vice versa.
325	 */
326	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
327	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
328		return 0;
329
330	/* martian filters on outer source - done in ip6_input */
331
332	/* ingress filters on outer source */
333	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
334		struct sockaddr_in6 sin6;
335		struct rtentry *rt;
336
337		bzero(&sin6, sizeof(sin6));
338		sin6.sin6_family = AF_INET6;
339		sin6.sin6_len = sizeof(struct sockaddr_in6);
340		sin6.sin6_addr = ip6->ip6_src;
341		sin6.sin6_scope_id = 0; /* XXX */
342
343		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
344		if (!rt || rt->rt_ifp != ifp) {
345#if 0
346			log(LOG_WARNING, "%s: packet from %s dropped "
347			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
348			    ip6_sprintf(&sin6.sin6_addr));
349#endif
350			if (rt)
351				rtfree(rt);
352			return 0;
353		}
354		rtfree(rt);
355	}
356
357	return 128 * 2;
358}
359
360/*
361 * we know that we are in IFF_UP, outer address available, and outer family
362 * matched the physical addr family.  see gif_encapcheck().
363 * sanity check for arg should have been done in the caller.
364 */
365int
366gif_encapcheck6(m, off, proto, arg)
367	const struct mbuf *m;
368	int off;
369	int proto;
370	void *arg;
371{
372	struct ip6_hdr ip6;
373	struct gif_softc *sc;
374	struct ifnet *ifp;
375
376	/* sanity check done in caller */
377	sc = (struct gif_softc *)arg;
378
379	/* LINTED const cast */
380	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
381	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
382
383	return gif_validate6(&ip6, sc, ifp);
384}
385
386int
387in6_gif_attach(sc)
388	struct gif_softc *sc;
389{
390	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
391	    (struct protosw *)&in6_gif_protosw, sc);
392	if (sc->encap_cookie6 == NULL)
393		return EEXIST;
394	return 0;
395}
396
397int
398in6_gif_detach(sc)
399	struct gif_softc *sc;
400{
401	int error;
402
403	error = encap_detach(sc->encap_cookie6);
404	if (error == 0)
405		sc->encap_cookie6 = NULL;
406	return error;
407}
408