1274246Sae/*-
2274246Sae * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
3274246Sae * All rights reserved.
4274246Sae *
5274246Sae * Redistribution and use in source and binary forms, with or without
6274246Sae * modification, are permitted provided that the following conditions
7274246Sae * are met:
8274246Sae *
9274246Sae * 1. Redistributions of source code must retain the above copyright
10274246Sae *    notice, this list of conditions and the following disclaimer.
11274246Sae * 2. Redistributions in binary form must reproduce the above copyright
12274246Sae *    notice, this list of conditions and the following disclaimer in the
13274246Sae *    documentation and/or other materials provided with the distribution.
14274246Sae *
15274246Sae * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16274246Sae * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17274246Sae * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18274246Sae * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19274246Sae * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20274246Sae * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21274246Sae * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22274246Sae * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23274246Sae * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24274246Sae * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25274246Sae */
26274246Sae
27274246Sae#include <sys/cdefs.h>
28274246Sae__FBSDID("$FreeBSD$");
29274246Sae
30274246Sae#include "opt_inet.h"
31274246Sae#include "opt_inet6.h"
32274246Sae
33274246Sae#include <sys/param.h>
34274246Sae#include <sys/lock.h>
35274246Sae#include <sys/rmlock.h>
36274246Sae#include <sys/systm.h>
37274246Sae#include <sys/socket.h>
38274246Sae#include <sys/sockio.h>
39274246Sae#include <sys/mbuf.h>
40274246Sae#include <sys/errno.h>
41274246Sae#include <sys/kernel.h>
42274246Sae#include <sys/queue.h>
43274246Sae#include <sys/syslog.h>
44274246Sae#include <sys/sysctl.h>
45274246Sae#include <sys/protosw.h>
46274246Sae#include <sys/malloc.h>
47274246Sae
48274246Sae#include <net/if.h>
49274246Sae#include <net/if_var.h>
50274246Sae#include <net/vnet.h>
51274246Sae
52274246Sae#include <netinet/in.h>
53274246Sae#include <netinet/in_systm.h>
54274246Sae#ifdef INET
55274246Sae#include <net/ethernet.h>
56274246Sae#include <netinet/ip.h>
57274246Sae#endif
58274246Sae#include <netinet/ip_encap.h>
59274246Sae#include <netinet/ip6.h>
60284066Sae#include <netinet6/ip6protosw.h>
61274246Sae#include <netinet6/ip6_var.h>
62274246Sae#include <netinet6/in6_var.h>
63274246Sae#include <net/if_gre.h>
64274246Sae
65274246Saeextern  struct domain inet6domain;
66284066Saestruct ip6protosw in6_gre_protosw = {
67274246Sae	.pr_type =	SOCK_RAW,
68274246Sae	.pr_domain =	&inet6domain,
69274246Sae	.pr_protocol =	IPPROTO_GRE,
70274246Sae	.pr_flags =	PR_ATOMIC|PR_ADDR,
71274246Sae	.pr_input =	gre_input,
72274246Sae	.pr_output =	rip6_output,
73274246Sae	.pr_ctloutput =	rip6_ctloutput,
74274246Sae	.pr_usrreqs =	&rip6_usrreqs
75274246Sae};
76274246Sae
77274246SaeVNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
78274246Sae#define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
79274246Sae
80274246SaeSYSCTL_DECL(_net_inet6_ip6);
81274246SaeSYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
82274246Sae    &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
83274246Sae
84274246Saestatic int
85274246Saein6_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
86274246Sae{
87274246Sae	GRE_RLOCK_TRACKER;
88274246Sae	struct gre_softc *sc;
89274246Sae	struct ip6_hdr *ip6;
90274246Sae
91274246Sae	sc = (struct gre_softc *)arg;
92274246Sae	if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
93274246Sae		return (0);
94274246Sae
95274246Sae	M_ASSERTPKTHDR(m);
96274246Sae	/*
97274246Sae	 * We expect that payload contains at least IPv4
98274246Sae	 * or IPv6 packet.
99274246Sae	 */
100274246Sae	if (m->m_pkthdr.len < sizeof(struct greip6) +
101274246Sae#ifdef INET
102274246Sae	    sizeof(struct ip))
103274246Sae#else
104274246Sae	    sizeof(struct ip6_hdr))
105274246Sae#endif
106274246Sae		return (0);
107274246Sae
108274246Sae	GRE_RLOCK(sc);
109274246Sae	if (sc->gre_family == 0)
110274246Sae		goto bad;
111274246Sae
112274246Sae	KASSERT(sc->gre_family == AF_INET6,
113274246Sae	    ("wrong gre_family: %d", sc->gre_family));
114274246Sae
115274246Sae	ip6 = mtod(m, struct ip6_hdr *);
116274246Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, &ip6->ip6_dst) ||
117274246Sae	    !IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &ip6->ip6_src))
118274246Sae		goto bad;
119274246Sae
120274246Sae	GRE_RUNLOCK(sc);
121274246Sae	return (128 * 2);
122274246Saebad:
123274246Sae	GRE_RUNLOCK(sc);
124274246Sae	return (0);
125274246Sae}
126274246Sae
127274246Saeint
128274246Saein6_gre_output(struct mbuf *m, int af, int hlen)
129274246Sae{
130274246Sae	struct greip6 *gi6;
131274246Sae
132274246Sae	gi6 = mtod(m, struct greip6 *);
133274246Sae	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
134274246Sae	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
135274246Sae}
136274246Sae
137274246Saeint
138274246Saein6_gre_attach(struct gre_softc *sc)
139274246Sae{
140274246Sae
141274246Sae	KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
142274246Sae	sc->gre_ecookie = encap_attach_func(AF_INET6, IPPROTO_GRE,
143284066Sae	    in6_gre_encapcheck, (void *)&in6_gre_protosw, sc);
144274246Sae	if (sc->gre_ecookie == NULL)
145274246Sae		return (EEXIST);
146274246Sae	return (0);
147274246Sae}
148