]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/netinet/ip_gre.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / netinet / ip_gre.c
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Heiko W.Rupp <hwr@pilhuhn.de>
8  *
9  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/errno.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/rmlock.h>
52 #include <sys/sysctl.h>
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/vnet.h>
57 #include <net/raw_cb.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_encap.h>
64 #include <netinet/ip_var.h>
65 #include <machine/in_cksum.h>
66
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #endif
70
71 /* Needs IP headers. */
72 #include <net/if_gre.h>
73 #include <machine/stdarg.h>
74
75 extern struct domain inetdomain;
76 static void gre_input10(struct mbuf *, int);
77 static const struct protosw in_gre_protosw = {
78         .pr_type =              SOCK_RAW,
79         .pr_domain =            &inetdomain,
80         .pr_protocol =          IPPROTO_GRE,
81         .pr_flags =             PR_ATOMIC|PR_ADDR,
82         .pr_input =             gre_input10,
83         .pr_output =            (pr_output_t *)rip_output,
84         .pr_ctlinput =          rip_ctlinput,
85         .pr_ctloutput =         rip_ctloutput,
86         .pr_usrreqs =           &rip_usrreqs
87 };
88
89 #define GRE_TTL                 30
90 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
91 #define V_ip_gre_ttl            VNET(ip_gre_ttl)
92 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_RW,
93         &VNET_NAME(ip_gre_ttl), 0, "");
94
95 static void
96 gre_input10(struct mbuf *m, int off)
97 {
98         int proto;
99
100         proto = (mtod(m, struct ip *))->ip_p;
101         gre_input(&m, &off, proto);
102 }
103
104 static int
105 in_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
106 {
107         GRE_RLOCK_TRACKER;
108         struct gre_softc *sc;
109         struct ip *ip;
110
111         sc = (struct gre_softc *)arg;
112         if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
113                 return (0);
114
115         M_ASSERTPKTHDR(m);
116         /*
117          * We expect that payload contains at least IPv4
118          * or IPv6 packet.
119          */
120         if (m->m_pkthdr.len < sizeof(struct greip) + sizeof(struct ip))
121                 return (0);
122
123         GRE_RLOCK(sc);
124         if (sc->gre_family == 0)
125                 goto bad;
126
127         KASSERT(sc->gre_family == AF_INET,
128             ("wrong gre_family: %d", sc->gre_family));
129
130         ip = mtod(m, struct ip *);
131         if (sc->gre_oip.ip_src.s_addr != ip->ip_dst.s_addr ||
132             sc->gre_oip.ip_dst.s_addr != ip->ip_src.s_addr)
133                 goto bad;
134
135         GRE_RUNLOCK(sc);
136         return (32 * 2);
137 bad:
138         GRE_RUNLOCK(sc);
139         return (0);
140 }
141
142 int
143 in_gre_output(struct mbuf *m, int af, int hlen)
144 {
145         struct greip *gi;
146
147         gi = mtod(m, struct greip *);
148         switch (af) {
149         case AF_INET:
150                 /*
151                  * gre_transmit() has used M_PREPEND() that doesn't guarantee
152                  * m_data is contiguous more than hlen bytes. Use m_copydata()
153                  * here to avoid m_pullup().
154                  */
155                 m_copydata(m, hlen + offsetof(struct ip, ip_tos),
156                     sizeof(u_char), &gi->gi_ip.ip_tos);
157                 m_copydata(m, hlen + offsetof(struct ip, ip_id),
158                     sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
159                 break;
160 #ifdef INET6
161         case AF_INET6:
162                 gi->gi_ip.ip_tos = 0; /* XXX */
163                 gi->gi_ip.ip_id = ip_newid();
164                 break;
165 #endif
166         }
167         gi->gi_ip.ip_ttl = V_ip_gre_ttl;
168         gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
169         return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
170 }
171
172 int
173 in_gre_attach(struct gre_softc *sc)
174 {
175
176         KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
177         sc->gre_ecookie = encap_attach_func(AF_INET, IPPROTO_GRE,
178             in_gre_encapcheck, &in_gre_protosw, sc);
179         if (sc->gre_ecookie == NULL)
180                 return (EEXIST);
181         return (0);
182 }