xref: /freebsd/sys/netinet6/ip6_gre.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1 /*-
2  * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/rmlock.h>
36 #include <sys/systm.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/mbuf.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/syslog.h>
44 #include <sys/sysctl.h>
45 #include <sys/protosw.h>
46 #include <sys/malloc.h>
47 
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/vnet.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #ifdef INET
55 #include <net/ethernet.h>
56 #include <netinet/ip.h>
57 #endif
58 #include <netinet/ip_encap.h>
59 #include <netinet/ip6.h>
60 #include <netinet6/ip6_var.h>
61 #include <netinet6/in6_var.h>
62 #include <net/if_gre.h>
63 
64 extern  struct domain inet6domain;
65 extern int gre_input(struct mbuf **, int *, int);
66 
67 int in6_gre_attach(struct gre_softc *);
68 int in6_gre_output(struct mbuf *, int, int);
69 
70 struct protosw in6_gre_protosw = {
71 	.pr_type =	SOCK_RAW,
72 	.pr_domain =	&inet6domain,
73 	.pr_protocol =	IPPROTO_GRE,
74 	.pr_flags =	PR_ATOMIC|PR_ADDR,
75 	.pr_input =	gre_input,
76 	.pr_output =	rip6_output,
77 	.pr_ctloutput =	rip6_ctloutput,
78 	.pr_usrreqs =	&rip6_usrreqs
79 };
80 
81 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
82 #define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
83 
84 SYSCTL_DECL(_net_inet6_ip6);
85 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
86     &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
87 
88 static int
89 in6_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
90 {
91 	GRE_RLOCK_TRACKER;
92 	struct gre_softc *sc;
93 	struct ip6_hdr *ip6;
94 
95 	sc = (struct gre_softc *)arg;
96 	if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
97 		return (0);
98 
99 	M_ASSERTPKTHDR(m);
100 	/*
101 	 * We expect that payload contains at least IPv4
102 	 * or IPv6 packet.
103 	 */
104 	if (m->m_pkthdr.len < sizeof(struct greip6) +
105 #ifdef INET
106 	    sizeof(struct ip))
107 #else
108 	    sizeof(struct ip6_hdr))
109 #endif
110 		return (0);
111 
112 	GRE_RLOCK(sc);
113 	if (sc->gre_family == 0)
114 		goto bad;
115 
116 	KASSERT(sc->gre_family == AF_INET6,
117 	    ("wrong gre_family: %d", sc->gre_family));
118 
119 	ip6 = mtod(m, struct ip6_hdr *);
120 	if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, &ip6->ip6_dst) ||
121 	    !IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &ip6->ip6_src))
122 		goto bad;
123 
124 	GRE_RUNLOCK(sc);
125 	return (128 * 2);
126 bad:
127 	GRE_RUNLOCK(sc);
128 	return (0);
129 }
130 
131 int
132 in6_gre_output(struct mbuf *m, int af, int hlen)
133 {
134 	struct greip6 *gi6;
135 
136 	gi6 = mtod(m, struct greip6 *);
137 	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
138 	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
139 }
140 
141 int
142 in6_gre_attach(struct gre_softc *sc)
143 {
144 
145 	KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
146 	sc->gre_ecookie = encap_attach_func(AF_INET6, IPPROTO_GRE,
147 	    in6_gre_encapcheck, &in6_gre_protosw, sc);
148 	if (sc->gre_ecookie == NULL)
149 		return (EEXIST);
150 	return (0);
151 }
152