xref: /freebsd/sys/netinet6/ip6_gre.c (revision c5fda9bac0325eb8c5b447717862d279006f318f)
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/malloc.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/vnet.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #ifdef INET
54 #include <net/ethernet.h>
55 #include <netinet/ip.h>
56 #endif
57 #include <netinet/ip_encap.h>
58 #include <netinet/ip6.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/in6_var.h>
61 #include <net/if_gre.h>
62 
63 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
64 #define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
65 
66 SYSCTL_DECL(_net_inet6_ip6);
67 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
68     &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
69 
70 static int
71 in6_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
72 {
73 	GRE_RLOCK_TRACKER;
74 	struct gre_softc *sc;
75 	struct ip6_hdr *ip6;
76 
77 	sc = (struct gre_softc *)arg;
78 	if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
79 		return (0);
80 
81 	M_ASSERTPKTHDR(m);
82 
83 	GRE_RLOCK(sc);
84 	if (sc->gre_family == 0)
85 		goto bad;
86 
87 	KASSERT(sc->gre_family == AF_INET6,
88 	    ("wrong gre_family: %d", sc->gre_family));
89 
90 	ip6 = mtod(m, struct ip6_hdr *);
91 	if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, &ip6->ip6_dst) ||
92 	    !IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &ip6->ip6_src))
93 		goto bad;
94 
95 	GRE_RUNLOCK(sc);
96 	return (128 * 2 + 32);
97 bad:
98 	GRE_RUNLOCK(sc);
99 	return (0);
100 }
101 
102 int
103 in6_gre_output(struct mbuf *m, int af, int hlen)
104 {
105 	struct greip6 *gi6;
106 
107 	gi6 = mtod(m, struct greip6 *);
108 	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
109 	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
110 }
111 
112 static const struct encap_config ipv6_encap_cfg = {
113 	.proto = IPPROTO_GRE,
114 	.min_length = sizeof(struct greip6) +
115 #ifdef INET
116 	    sizeof(struct ip),
117 #else
118 	    sizeof(struct ip6_hdr),
119 #endif
120 	.exact_match = (sizeof(struct in6_addr) << 4) + 32,
121 	.check = in6_gre_encapcheck,
122 	.input = gre_input
123 };
124 
125 int
126 in6_gre_attach(struct gre_softc *sc)
127 {
128 
129 	KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
130 	sc->gre_ecookie = ip6_encap_attach(&ipv6_encap_cfg, sc, M_WAITOK);
131 	return (0);
132 }
133