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>
60274246Sae#include <netinet6/ip6_var.h>
61274246Sae#include <netinet6/in6_var.h>
62274246Sae#include <net/if_gre.h>
63274246Sae
64274246Saeextern  struct domain inet6domain;
65274246Saestruct protosw in6_gre_protosw = {
66274246Sae	.pr_type =	SOCK_RAW,
67274246Sae	.pr_domain =	&inet6domain,
68274246Sae	.pr_protocol =	IPPROTO_GRE,
69274246Sae	.pr_flags =	PR_ATOMIC|PR_ADDR,
70274246Sae	.pr_input =	gre_input,
71274246Sae	.pr_output =	rip6_output,
72274246Sae	.pr_ctloutput =	rip6_ctloutput,
73274246Sae	.pr_usrreqs =	&rip6_usrreqs
74274246Sae};
75274246Sae
76274246SaeVNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
77274246Sae#define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
78274246Sae
79274246SaeSYSCTL_DECL(_net_inet6_ip6);
80274246SaeSYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
81274246Sae    &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
82274246Sae
83274246Saestatic int
84274246Saein6_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
85274246Sae{
86274246Sae	GRE_RLOCK_TRACKER;
87274246Sae	struct gre_softc *sc;
88274246Sae	struct ip6_hdr *ip6;
89274246Sae
90274246Sae	sc = (struct gre_softc *)arg;
91274246Sae	if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
92274246Sae		return (0);
93274246Sae
94274246Sae	M_ASSERTPKTHDR(m);
95274246Sae	/*
96274246Sae	 * We expect that payload contains at least IPv4
97274246Sae	 * or IPv6 packet.
98274246Sae	 */
99274246Sae	if (m->m_pkthdr.len < sizeof(struct greip6) +
100274246Sae#ifdef INET
101274246Sae	    sizeof(struct ip))
102274246Sae#else
103274246Sae	    sizeof(struct ip6_hdr))
104274246Sae#endif
105274246Sae		return (0);
106274246Sae
107274246Sae	GRE_RLOCK(sc);
108274246Sae	if (sc->gre_family == 0)
109274246Sae		goto bad;
110274246Sae
111274246Sae	KASSERT(sc->gre_family == AF_INET6,
112274246Sae	    ("wrong gre_family: %d", sc->gre_family));
113274246Sae
114274246Sae	ip6 = mtod(m, struct ip6_hdr *);
115274246Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, &ip6->ip6_dst) ||
116274246Sae	    !IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &ip6->ip6_src))
117274246Sae		goto bad;
118274246Sae
119274246Sae	GRE_RUNLOCK(sc);
120274246Sae	return (128 * 2);
121274246Saebad:
122274246Sae	GRE_RUNLOCK(sc);
123274246Sae	return (0);
124274246Sae}
125274246Sae
126274246Saeint
127274246Saein6_gre_output(struct mbuf *m, int af, int hlen)
128274246Sae{
129274246Sae	struct greip6 *gi6;
130274246Sae
131274246Sae	gi6 = mtod(m, struct greip6 *);
132274246Sae	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
133274246Sae	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
134274246Sae}
135274246Sae
136274246Saeint
137274246Saein6_gre_attach(struct gre_softc *sc)
138274246Sae{
139274246Sae
140274246Sae	KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
141274246Sae	sc->gre_ecookie = encap_attach_func(AF_INET6, IPPROTO_GRE,
142274246Sae	    in6_gre_encapcheck, &in6_gre_protosw, sc);
143274246Sae	if (sc->gre_ecookie == NULL)
144274246Sae		return (EEXIST);
145274246Sae	return (0);
146274246Sae}
147