Deleted Added
full compact
if_gre.c (219206) if_gre.c (223223)
1/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
1/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2/* $FreeBSD: head/sys/net/if_gre.c 219206 2011-03-02 21:39:08Z bz $ */
2/* $FreeBSD: head/sys/net/if_gre.c 223223 2011-06-18 09:34:03Z bz $ */
3
4/*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 *
11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * Encapsulate L3 protocols into IP
37 * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
38 * If_gre is compatible with Cisco GRE tunnels, so you can
39 * have a NetBSD box as the other end of a tunnel interface of a Cisco
40 * router. See gre(4) for more details.
41 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
42 */
43
44#include "opt_atalk.h"
45#include "opt_inet.h"
46#include "opt_inet6.h"
47
48#include <sys/param.h>
49#include <sys/jail.h>
50#include <sys/kernel.h>
3
4/*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 *
11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * Encapsulate L3 protocols into IP
37 * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
38 * If_gre is compatible with Cisco GRE tunnels, so you can
39 * have a NetBSD box as the other end of a tunnel interface of a Cisco
40 * router. See gre(4) for more details.
41 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
42 */
43
44#include "opt_atalk.h"
45#include "opt_inet.h"
46#include "opt_inet6.h"
47
48#include <sys/param.h>
49#include <sys/jail.h>
50#include <sys/kernel.h>
51#include <sys/libkern.h>
51#include <sys/malloc.h>
52#include <sys/module.h>
53#include <sys/mbuf.h>
54#include <sys/priv.h>
55#include <sys/proc.h>
56#include <sys/protosw.h>
57#include <sys/socket.h>
58#include <sys/sockio.h>
59#include <sys/sysctl.h>
60#include <sys/systm.h>
61
62#include <net/ethernet.h>
63#include <net/if.h>
64#include <net/if_clone.h>
65#include <net/if_types.h>
66#include <net/route.h>
67#include <net/vnet.h>
68
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/in_systm.h>
72#include <netinet/in_var.h>
73#include <netinet/ip.h>
74#include <netinet/ip_gre.h>
75#include <netinet/ip_var.h>
76#include <netinet/ip_encap.h>
77#else
78#error "Huh? if_gre without inet?"
79#endif
80
81#include <net/bpf.h>
82
83#include <net/if_gre.h>
84
85/*
86 * It is not easy to calculate the right value for a GRE MTU.
87 * We leave this task to the admin and use the same default that
88 * other vendors use.
89 */
90#define GREMTU 1476
91
92#define GRENAME "gre"
93
52#include <sys/malloc.h>
53#include <sys/module.h>
54#include <sys/mbuf.h>
55#include <sys/priv.h>
56#include <sys/proc.h>
57#include <sys/protosw.h>
58#include <sys/socket.h>
59#include <sys/sockio.h>
60#include <sys/sysctl.h>
61#include <sys/systm.h>
62
63#include <net/ethernet.h>
64#include <net/if.h>
65#include <net/if_clone.h>
66#include <net/if_types.h>
67#include <net/route.h>
68#include <net/vnet.h>
69
70#ifdef INET
71#include <netinet/in.h>
72#include <netinet/in_systm.h>
73#include <netinet/in_var.h>
74#include <netinet/ip.h>
75#include <netinet/ip_gre.h>
76#include <netinet/ip_var.h>
77#include <netinet/ip_encap.h>
78#else
79#error "Huh? if_gre without inet?"
80#endif
81
82#include <net/bpf.h>
83
84#include <net/if_gre.h>
85
86/*
87 * It is not easy to calculate the right value for a GRE MTU.
88 * We leave this task to the admin and use the same default that
89 * other vendors use.
90 */
91#define GREMTU 1476
92
93#define GRENAME "gre"
94
95#define MTAG_COOKIE_GRE 1307983903
96#define MTAG_GRE_NESTING 1
97struct mtag_gre_nesting {
98 uint16_t count;
99 uint16_t max;
100 struct ifnet *ifp[];
101};
102
94/*
95 * gre_mtx protects all global variables in if_gre.c.
96 * XXX: gre_softc data not protected yet.
97 */
98struct mtx gre_mtx;
99static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
100
101struct gre_softc_head gre_softc_list;
102
103static int gre_clone_create(struct if_clone *, int, caddr_t);
104static void gre_clone_destroy(struct ifnet *);
105static int gre_ioctl(struct ifnet *, u_long, caddr_t);
106static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
107 struct route *ro);
108
109IFC_SIMPLE_DECLARE(gre, 0);
110
111static int gre_compute_route(struct gre_softc *sc);
112
113static void greattach(void);
114
115#ifdef INET
116extern struct domain inetdomain;
117static const struct protosw in_gre_protosw = {
118 .pr_type = SOCK_RAW,
119 .pr_domain = &inetdomain,
120 .pr_protocol = IPPROTO_GRE,
121 .pr_flags = PR_ATOMIC|PR_ADDR,
122 .pr_input = gre_input,
123 .pr_output = (pr_output_t *)rip_output,
124 .pr_ctlinput = rip_ctlinput,
125 .pr_ctloutput = rip_ctloutput,
126 .pr_usrreqs = &rip_usrreqs
127};
128static const struct protosw in_mobile_protosw = {
129 .pr_type = SOCK_RAW,
130 .pr_domain = &inetdomain,
131 .pr_protocol = IPPROTO_MOBILE,
132 .pr_flags = PR_ATOMIC|PR_ADDR,
133 .pr_input = gre_mobile_input,
134 .pr_output = (pr_output_t *)rip_output,
135 .pr_ctlinput = rip_ctlinput,
136 .pr_ctloutput = rip_ctloutput,
137 .pr_usrreqs = &rip_usrreqs
138};
139#endif
140
141SYSCTL_DECL(_net_link);
142SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
143 "Generic Routing Encapsulation");
144#ifndef MAX_GRE_NEST
145/*
146 * This macro controls the default upper limitation on nesting of gre tunnels.
147 * Since, setting a large value to this macro with a careless configuration
148 * may introduce system crash, we don't allow any nestings by default.
149 * If you need to configure nested gre tunnels, you can define this macro
150 * in your kernel configuration file. However, if you do so, please be
151 * careful to configure the tunnels so that it won't make a loop.
152 */
153#define MAX_GRE_NEST 1
154#endif
155static int max_gre_nesting = MAX_GRE_NEST;
156SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
157 &max_gre_nesting, 0, "Max nested tunnels");
158
159/* ARGSUSED */
160static void
161greattach(void)
162{
163
164 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
165 LIST_INIT(&gre_softc_list);
166 if_clone_attach(&gre_cloner);
167}
168
169static int
170gre_clone_create(ifc, unit, params)
171 struct if_clone *ifc;
172 int unit;
173 caddr_t params;
174{
175 struct gre_softc *sc;
176
177 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
178
179 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
180 if (GRE2IFP(sc) == NULL) {
181 free(sc, M_GRE);
182 return (ENOSPC);
183 }
184
185 GRE2IFP(sc)->if_softc = sc;
186 if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
187
188 GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
189 GRE2IFP(sc)->if_addrlen = 0;
190 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
191 GRE2IFP(sc)->if_mtu = GREMTU;
192 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
193 GRE2IFP(sc)->if_output = gre_output;
194 GRE2IFP(sc)->if_ioctl = gre_ioctl;
195 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
196 sc->g_proto = IPPROTO_GRE;
197 GRE2IFP(sc)->if_flags |= IFF_LINK0;
198 sc->encap = NULL;
103/*
104 * gre_mtx protects all global variables in if_gre.c.
105 * XXX: gre_softc data not protected yet.
106 */
107struct mtx gre_mtx;
108static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
109
110struct gre_softc_head gre_softc_list;
111
112static int gre_clone_create(struct if_clone *, int, caddr_t);
113static void gre_clone_destroy(struct ifnet *);
114static int gre_ioctl(struct ifnet *, u_long, caddr_t);
115static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116 struct route *ro);
117
118IFC_SIMPLE_DECLARE(gre, 0);
119
120static int gre_compute_route(struct gre_softc *sc);
121
122static void greattach(void);
123
124#ifdef INET
125extern struct domain inetdomain;
126static const struct protosw in_gre_protosw = {
127 .pr_type = SOCK_RAW,
128 .pr_domain = &inetdomain,
129 .pr_protocol = IPPROTO_GRE,
130 .pr_flags = PR_ATOMIC|PR_ADDR,
131 .pr_input = gre_input,
132 .pr_output = (pr_output_t *)rip_output,
133 .pr_ctlinput = rip_ctlinput,
134 .pr_ctloutput = rip_ctloutput,
135 .pr_usrreqs = &rip_usrreqs
136};
137static const struct protosw in_mobile_protosw = {
138 .pr_type = SOCK_RAW,
139 .pr_domain = &inetdomain,
140 .pr_protocol = IPPROTO_MOBILE,
141 .pr_flags = PR_ATOMIC|PR_ADDR,
142 .pr_input = gre_mobile_input,
143 .pr_output = (pr_output_t *)rip_output,
144 .pr_ctlinput = rip_ctlinput,
145 .pr_ctloutput = rip_ctloutput,
146 .pr_usrreqs = &rip_usrreqs
147};
148#endif
149
150SYSCTL_DECL(_net_link);
151SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
152 "Generic Routing Encapsulation");
153#ifndef MAX_GRE_NEST
154/*
155 * This macro controls the default upper limitation on nesting of gre tunnels.
156 * Since, setting a large value to this macro with a careless configuration
157 * may introduce system crash, we don't allow any nestings by default.
158 * If you need to configure nested gre tunnels, you can define this macro
159 * in your kernel configuration file. However, if you do so, please be
160 * careful to configure the tunnels so that it won't make a loop.
161 */
162#define MAX_GRE_NEST 1
163#endif
164static int max_gre_nesting = MAX_GRE_NEST;
165SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
166 &max_gre_nesting, 0, "Max nested tunnels");
167
168/* ARGSUSED */
169static void
170greattach(void)
171{
172
173 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
174 LIST_INIT(&gre_softc_list);
175 if_clone_attach(&gre_cloner);
176}
177
178static int
179gre_clone_create(ifc, unit, params)
180 struct if_clone *ifc;
181 int unit;
182 caddr_t params;
183{
184 struct gre_softc *sc;
185
186 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
187
188 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
189 if (GRE2IFP(sc) == NULL) {
190 free(sc, M_GRE);
191 return (ENOSPC);
192 }
193
194 GRE2IFP(sc)->if_softc = sc;
195 if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
196
197 GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
198 GRE2IFP(sc)->if_addrlen = 0;
199 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
200 GRE2IFP(sc)->if_mtu = GREMTU;
201 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
202 GRE2IFP(sc)->if_output = gre_output;
203 GRE2IFP(sc)->if_ioctl = gre_ioctl;
204 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
205 sc->g_proto = IPPROTO_GRE;
206 GRE2IFP(sc)->if_flags |= IFF_LINK0;
207 sc->encap = NULL;
199 sc->called = 0;
200 sc->gre_fibnum = curthread->td_proc->p_fibnum;
201 sc->wccp_ver = WCCP_V1;
202 sc->key = 0;
203 if_attach(GRE2IFP(sc));
204 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
205 mtx_lock(&gre_mtx);
206 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
207 mtx_unlock(&gre_mtx);
208 return (0);
209}
210
211static void
212gre_clone_destroy(ifp)
213 struct ifnet *ifp;
214{
215 struct gre_softc *sc = ifp->if_softc;
216
217 mtx_lock(&gre_mtx);
218 LIST_REMOVE(sc, sc_list);
219 mtx_unlock(&gre_mtx);
220
221#ifdef INET
222 if (sc->encap != NULL)
223 encap_detach(sc->encap);
224#endif
225 bpfdetach(ifp);
226 if_detach(ifp);
227 if_free(ifp);
228 free(sc, M_GRE);
229}
230
231/*
232 * The output routine. Takes a packet and encapsulates it in the protocol
233 * given by sc->g_proto. See also RFC 1701 and RFC 2004
234 */
235static int
236gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
237 struct route *ro)
238{
239 int error = 0;
240 struct gre_softc *sc = ifp->if_softc;
241 struct greip *gh;
242 struct ip *ip;
208 sc->gre_fibnum = curthread->td_proc->p_fibnum;
209 sc->wccp_ver = WCCP_V1;
210 sc->key = 0;
211 if_attach(GRE2IFP(sc));
212 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
213 mtx_lock(&gre_mtx);
214 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
215 mtx_unlock(&gre_mtx);
216 return (0);
217}
218
219static void
220gre_clone_destroy(ifp)
221 struct ifnet *ifp;
222{
223 struct gre_softc *sc = ifp->if_softc;
224
225 mtx_lock(&gre_mtx);
226 LIST_REMOVE(sc, sc_list);
227 mtx_unlock(&gre_mtx);
228
229#ifdef INET
230 if (sc->encap != NULL)
231 encap_detach(sc->encap);
232#endif
233 bpfdetach(ifp);
234 if_detach(ifp);
235 if_free(ifp);
236 free(sc, M_GRE);
237}
238
239/*
240 * The output routine. Takes a packet and encapsulates it in the protocol
241 * given by sc->g_proto. See also RFC 1701 and RFC 2004
242 */
243static int
244gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
245 struct route *ro)
246{
247 int error = 0;
248 struct gre_softc *sc = ifp->if_softc;
249 struct greip *gh;
250 struct ip *ip;
251 struct m_tag *mtag;
252 struct mtag_gre_nesting *gt;
253 size_t len;
243 u_short gre_ip_id = 0;
244 uint8_t gre_ip_tos = 0;
245 u_int16_t etype = 0;
246 struct mobile_h mob_h;
247 u_int32_t af;
254 u_short gre_ip_id = 0;
255 uint8_t gre_ip_tos = 0;
256 u_int16_t etype = 0;
257 struct mobile_h mob_h;
258 u_int32_t af;
248 int extra = 0;
259 int extra = 0, max;
249
250 /*
260
261 /*
251 * gre may cause infinite recursion calls when misconfigured.
252 * We'll prevent this by introducing upper limit.
262 * gre may cause infinite recursion calls when misconfigured. High
263 * nesting level may cause stack exhaustion. We'll prevent this by
264 * detecting loops and by introducing upper limit.
253 */
265 */
254 if (++(sc->called) > max_gre_nesting) {
255 printf("%s: gre_output: recursively called too many "
256 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
257 m_freem(m);
258 error = EIO; /* is there better errno? */
259 goto end;
266 mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL);
267 if (mtag != NULL) {
268 struct ifnet **ifp2;
269
270 gt = (struct mtag_gre_nesting *)(mtag + 1);
271 gt->count++;
272 if (gt->count > min(gt->max,max_gre_nesting)) {
273 printf("%s: hit maximum recursion limit %u on %s\n",
274 __func__, gt->count - 1, ifp->if_xname);
275 m_freem(m);
276 error = EIO; /* is there better errno? */
277 goto end;
278 }
279
280 ifp2 = gt->ifp;
281 for (max = gt->count - 1; max > 0; max--) {
282 if (*ifp2 == ifp)
283 break;
284 ifp2++;
285 }
286 if (*ifp2 == ifp) {
287 printf("%s: detected loop with nexting %u on %s\n",
288 __func__, gt->count-1, ifp->if_xname);
289 m_freem(m);
290 error = EIO; /* is there better errno? */
291 goto end;
292 }
293 *ifp2 = ifp;
294
295 } else {
296 /*
297 * Given that people should NOT increase max_gre_nesting beyond
298 * their real needs, we allocate once per packet rather than
299 * allocating an mtag once per passing through gre.
300 *
301 * Note: the sysctl does not actually check for saneness, so we
302 * limit the maximum numbers of possible recursions here.
303 */
304 max = imin(max_gre_nesting, 256);
305 /* If someone sets the sysctl <= 0, we want at least 1. */
306 max = imax(max, 1);
307 len = sizeof(struct mtag_gre_nesting) +
308 max * sizeof(struct ifnet *);
309 mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len,
310 M_NOWAIT);
311 if (mtag == NULL) {
312 m_freem(m);
313 error = ENOMEM;
314 goto end;
315 }
316 gt = (struct mtag_gre_nesting *)(mtag + 1);
317 bzero(gt, len);
318 gt->count = 1;
319 gt->max = max;
320 *gt->ifp = ifp;
321 m_tag_prepend(m, mtag);
260 }
261
262 if (!((ifp->if_flags & IFF_UP) &&
263 (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
264 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
265 m_freem(m);
266 error = ENETDOWN;
267 goto end;
268 }
269
270 gh = NULL;
271 ip = NULL;
272
273 /* BPF writes need to be handled specially. */
274 if (dst->sa_family == AF_UNSPEC) {
275 bcopy(dst->sa_data, &af, sizeof(af));
276 dst->sa_family = af;
277 }
278
279 if (bpf_peers_present(ifp->if_bpf)) {
280 af = dst->sa_family;
281 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
282 }
283
284 m->m_flags &= ~(M_BCAST|M_MCAST);
285
286 if (sc->g_proto == IPPROTO_MOBILE) {
287 if (dst->sa_family == AF_INET) {
288 struct mbuf *m0;
289 int msiz;
290
291 ip = mtod(m, struct ip *);
292
293 /*
294 * RFC2004 specifies that fragmented diagrams shouldn't
295 * be encapsulated.
296 */
297 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
298 _IF_DROP(&ifp->if_snd);
299 m_freem(m);
300 error = EINVAL; /* is there better errno? */
301 goto end;
302 }
303 memset(&mob_h, 0, MOB_H_SIZ_L);
304 mob_h.proto = (ip->ip_p) << 8;
305 mob_h.odst = ip->ip_dst.s_addr;
306 ip->ip_dst.s_addr = sc->g_dst.s_addr;
307
308 /*
309 * If the packet comes from our host, we only change
310 * the destination address in the IP header.
311 * Else we also need to save and change the source
312 */
313 if (in_hosteq(ip->ip_src, sc->g_src)) {
314 msiz = MOB_H_SIZ_S;
315 } else {
316 mob_h.proto |= MOB_H_SBIT;
317 mob_h.osrc = ip->ip_src.s_addr;
318 ip->ip_src.s_addr = sc->g_src.s_addr;
319 msiz = MOB_H_SIZ_L;
320 }
321 mob_h.proto = htons(mob_h.proto);
322 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
323
324 if ((m->m_data - msiz) < m->m_pktdat) {
325 /* need new mbuf */
326 MGETHDR(m0, M_DONTWAIT, MT_DATA);
327 if (m0 == NULL) {
328 _IF_DROP(&ifp->if_snd);
329 m_freem(m);
330 error = ENOBUFS;
331 goto end;
332 }
333 m0->m_next = m;
334 m->m_data += sizeof(struct ip);
335 m->m_len -= sizeof(struct ip);
336 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
337 m0->m_len = msiz + sizeof(struct ip);
338 m0->m_data += max_linkhdr;
339 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
340 sizeof(struct ip));
341 m = m0;
342 } else { /* we have some space left in the old one */
343 m->m_data -= msiz;
344 m->m_len += msiz;
345 m->m_pkthdr.len += msiz;
346 bcopy(ip, mtod(m, caddr_t),
347 sizeof(struct ip));
348 }
349 ip = mtod(m, struct ip *);
350 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
351 ip->ip_len = ntohs(ip->ip_len) + msiz;
352 } else { /* AF_INET */
353 _IF_DROP(&ifp->if_snd);
354 m_freem(m);
355 error = EINVAL;
356 goto end;
357 }
358 } else if (sc->g_proto == IPPROTO_GRE) {
359 switch (dst->sa_family) {
360 case AF_INET:
361 ip = mtod(m, struct ip *);
362 gre_ip_tos = ip->ip_tos;
363 gre_ip_id = ip->ip_id;
364 if (sc->wccp_ver == WCCP_V2) {
365 extra = sizeof(uint32_t);
366 etype = WCCP_PROTOCOL_TYPE;
367 } else {
368 etype = ETHERTYPE_IP;
369 }
370 break;
371#ifdef INET6
372 case AF_INET6:
373 gre_ip_id = ip_newid();
374 etype = ETHERTYPE_IPV6;
375 break;
376#endif
377#ifdef NETATALK
378 case AF_APPLETALK:
379 etype = ETHERTYPE_ATALK;
380 break;
381#endif
382 default:
383 _IF_DROP(&ifp->if_snd);
384 m_freem(m);
385 error = EAFNOSUPPORT;
386 goto end;
387 }
388
389 /* Reserve space for GRE header + optional GRE key */
390 int hdrlen = sizeof(struct greip) + extra;
391 if (sc->key)
392 hdrlen += sizeof(uint32_t);
393 M_PREPEND(m, hdrlen, M_DONTWAIT);
394 } else {
395 _IF_DROP(&ifp->if_snd);
396 m_freem(m);
397 error = EINVAL;
398 goto end;
399 }
400
401 if (m == NULL) { /* mbuf allocation failed */
402 _IF_DROP(&ifp->if_snd);
403 error = ENOBUFS;
404 goto end;
405 }
406
407 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
408
409 gh = mtod(m, struct greip *);
410 if (sc->g_proto == IPPROTO_GRE) {
411 uint32_t *options = gh->gi_options;
412
413 memset((void *)gh, 0, sizeof(struct greip) + extra);
414 gh->gi_ptype = htons(etype);
415 gh->gi_flags = 0;
416
417 /* Add key option */
418 if (sc->key)
419 {
420 gh->gi_flags |= htons(GRE_KP);
421 *(options++) = htonl(sc->key);
422 }
423 }
424
425 gh->gi_pr = sc->g_proto;
426 if (sc->g_proto != IPPROTO_MOBILE) {
427 gh->gi_src = sc->g_src;
428 gh->gi_dst = sc->g_dst;
429 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
430 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
431 ((struct ip*)gh)->ip_ttl = GRE_TTL;
432 ((struct ip*)gh)->ip_tos = gre_ip_tos;
433 ((struct ip*)gh)->ip_id = gre_ip_id;
434 gh->gi_len = m->m_pkthdr.len;
435 }
436
437 ifp->if_opackets++;
438 ifp->if_obytes += m->m_pkthdr.len;
439 /*
440 * Send it off and with IP_FORWARD flag to prevent it from
441 * overwriting the ip_id again. ip_id is already set to the
442 * ip_id of the encapsulated packet.
443 */
444 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
445 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
446 end:
322 }
323
324 if (!((ifp->if_flags & IFF_UP) &&
325 (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
326 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
327 m_freem(m);
328 error = ENETDOWN;
329 goto end;
330 }
331
332 gh = NULL;
333 ip = NULL;
334
335 /* BPF writes need to be handled specially. */
336 if (dst->sa_family == AF_UNSPEC) {
337 bcopy(dst->sa_data, &af, sizeof(af));
338 dst->sa_family = af;
339 }
340
341 if (bpf_peers_present(ifp->if_bpf)) {
342 af = dst->sa_family;
343 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
344 }
345
346 m->m_flags &= ~(M_BCAST|M_MCAST);
347
348 if (sc->g_proto == IPPROTO_MOBILE) {
349 if (dst->sa_family == AF_INET) {
350 struct mbuf *m0;
351 int msiz;
352
353 ip = mtod(m, struct ip *);
354
355 /*
356 * RFC2004 specifies that fragmented diagrams shouldn't
357 * be encapsulated.
358 */
359 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
360 _IF_DROP(&ifp->if_snd);
361 m_freem(m);
362 error = EINVAL; /* is there better errno? */
363 goto end;
364 }
365 memset(&mob_h, 0, MOB_H_SIZ_L);
366 mob_h.proto = (ip->ip_p) << 8;
367 mob_h.odst = ip->ip_dst.s_addr;
368 ip->ip_dst.s_addr = sc->g_dst.s_addr;
369
370 /*
371 * If the packet comes from our host, we only change
372 * the destination address in the IP header.
373 * Else we also need to save and change the source
374 */
375 if (in_hosteq(ip->ip_src, sc->g_src)) {
376 msiz = MOB_H_SIZ_S;
377 } else {
378 mob_h.proto |= MOB_H_SBIT;
379 mob_h.osrc = ip->ip_src.s_addr;
380 ip->ip_src.s_addr = sc->g_src.s_addr;
381 msiz = MOB_H_SIZ_L;
382 }
383 mob_h.proto = htons(mob_h.proto);
384 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
385
386 if ((m->m_data - msiz) < m->m_pktdat) {
387 /* need new mbuf */
388 MGETHDR(m0, M_DONTWAIT, MT_DATA);
389 if (m0 == NULL) {
390 _IF_DROP(&ifp->if_snd);
391 m_freem(m);
392 error = ENOBUFS;
393 goto end;
394 }
395 m0->m_next = m;
396 m->m_data += sizeof(struct ip);
397 m->m_len -= sizeof(struct ip);
398 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
399 m0->m_len = msiz + sizeof(struct ip);
400 m0->m_data += max_linkhdr;
401 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
402 sizeof(struct ip));
403 m = m0;
404 } else { /* we have some space left in the old one */
405 m->m_data -= msiz;
406 m->m_len += msiz;
407 m->m_pkthdr.len += msiz;
408 bcopy(ip, mtod(m, caddr_t),
409 sizeof(struct ip));
410 }
411 ip = mtod(m, struct ip *);
412 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
413 ip->ip_len = ntohs(ip->ip_len) + msiz;
414 } else { /* AF_INET */
415 _IF_DROP(&ifp->if_snd);
416 m_freem(m);
417 error = EINVAL;
418 goto end;
419 }
420 } else if (sc->g_proto == IPPROTO_GRE) {
421 switch (dst->sa_family) {
422 case AF_INET:
423 ip = mtod(m, struct ip *);
424 gre_ip_tos = ip->ip_tos;
425 gre_ip_id = ip->ip_id;
426 if (sc->wccp_ver == WCCP_V2) {
427 extra = sizeof(uint32_t);
428 etype = WCCP_PROTOCOL_TYPE;
429 } else {
430 etype = ETHERTYPE_IP;
431 }
432 break;
433#ifdef INET6
434 case AF_INET6:
435 gre_ip_id = ip_newid();
436 etype = ETHERTYPE_IPV6;
437 break;
438#endif
439#ifdef NETATALK
440 case AF_APPLETALK:
441 etype = ETHERTYPE_ATALK;
442 break;
443#endif
444 default:
445 _IF_DROP(&ifp->if_snd);
446 m_freem(m);
447 error = EAFNOSUPPORT;
448 goto end;
449 }
450
451 /* Reserve space for GRE header + optional GRE key */
452 int hdrlen = sizeof(struct greip) + extra;
453 if (sc->key)
454 hdrlen += sizeof(uint32_t);
455 M_PREPEND(m, hdrlen, M_DONTWAIT);
456 } else {
457 _IF_DROP(&ifp->if_snd);
458 m_freem(m);
459 error = EINVAL;
460 goto end;
461 }
462
463 if (m == NULL) { /* mbuf allocation failed */
464 _IF_DROP(&ifp->if_snd);
465 error = ENOBUFS;
466 goto end;
467 }
468
469 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
470
471 gh = mtod(m, struct greip *);
472 if (sc->g_proto == IPPROTO_GRE) {
473 uint32_t *options = gh->gi_options;
474
475 memset((void *)gh, 0, sizeof(struct greip) + extra);
476 gh->gi_ptype = htons(etype);
477 gh->gi_flags = 0;
478
479 /* Add key option */
480 if (sc->key)
481 {
482 gh->gi_flags |= htons(GRE_KP);
483 *(options++) = htonl(sc->key);
484 }
485 }
486
487 gh->gi_pr = sc->g_proto;
488 if (sc->g_proto != IPPROTO_MOBILE) {
489 gh->gi_src = sc->g_src;
490 gh->gi_dst = sc->g_dst;
491 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
492 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
493 ((struct ip*)gh)->ip_ttl = GRE_TTL;
494 ((struct ip*)gh)->ip_tos = gre_ip_tos;
495 ((struct ip*)gh)->ip_id = gre_ip_id;
496 gh->gi_len = m->m_pkthdr.len;
497 }
498
499 ifp->if_opackets++;
500 ifp->if_obytes += m->m_pkthdr.len;
501 /*
502 * Send it off and with IP_FORWARD flag to prevent it from
503 * overwriting the ip_id again. ip_id is already set to the
504 * ip_id of the encapsulated packet.
505 */
506 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
507 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
508 end:
447 sc->called = 0;
448 if (error)
449 ifp->if_oerrors++;
450 return (error);
451}
452
453static int
454gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
455{
456 struct ifreq *ifr = (struct ifreq *)data;
457 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
458 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
459 struct gre_softc *sc = ifp->if_softc;
460 int s;
461 struct sockaddr_in si;
462 struct sockaddr *sa = NULL;
463 int error, adj;
464 struct sockaddr_in sp, sm, dp, dm;
465 uint32_t key;
466
467 error = 0;
468 adj = 0;
469
470 s = splnet();
471 switch (cmd) {
472 case SIOCSIFADDR:
473 ifp->if_flags |= IFF_UP;
474 break;
475 case SIOCSIFDSTADDR:
476 break;
477 case SIOCSIFFLAGS:
478 /*
479 * XXXRW: Isn't this priv_check() redundant to the ifnet
480 * layer check?
481 */
482 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
483 break;
484 if ((ifr->ifr_flags & IFF_LINK0) != 0)
485 sc->g_proto = IPPROTO_GRE;
486 else
487 sc->g_proto = IPPROTO_MOBILE;
488 if ((ifr->ifr_flags & IFF_LINK2) != 0)
489 sc->wccp_ver = WCCP_V2;
490 else
491 sc->wccp_ver = WCCP_V1;
492 goto recompute;
493 case SIOCSIFMTU:
494 /*
495 * XXXRW: Isn't this priv_check() redundant to the ifnet
496 * layer check?
497 */
498 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
499 break;
500 if (ifr->ifr_mtu < 576) {
501 error = EINVAL;
502 break;
503 }
504 ifp->if_mtu = ifr->ifr_mtu;
505 break;
506 case SIOCGIFMTU:
507 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
508 break;
509 case SIOCADDMULTI:
510 /*
511 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
512 * layer check?
513 */
514 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
515 break;
516 if (ifr == 0) {
517 error = EAFNOSUPPORT;
518 break;
519 }
520 switch (ifr->ifr_addr.sa_family) {
521#ifdef INET
522 case AF_INET:
523 break;
524#endif
525#ifdef INET6
526 case AF_INET6:
527 break;
528#endif
529 default:
530 error = EAFNOSUPPORT;
531 break;
532 }
533 break;
534 case SIOCDELMULTI:
535 /*
536 * XXXRW: Isn't this priv_check() redundant to the ifnet
537 * layer check?
538 */
539 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
540 break;
541 if (ifr == 0) {
542 error = EAFNOSUPPORT;
543 break;
544 }
545 switch (ifr->ifr_addr.sa_family) {
546#ifdef INET
547 case AF_INET:
548 break;
549#endif
550#ifdef INET6
551 case AF_INET6:
552 break;
553#endif
554 default:
555 error = EAFNOSUPPORT;
556 break;
557 }
558 break;
559 case GRESPROTO:
560 /*
561 * XXXRW: Isn't this priv_check() redundant to the ifnet
562 * layer check?
563 */
564 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
565 break;
566 sc->g_proto = ifr->ifr_flags;
567 switch (sc->g_proto) {
568 case IPPROTO_GRE:
569 ifp->if_flags |= IFF_LINK0;
570 break;
571 case IPPROTO_MOBILE:
572 ifp->if_flags &= ~IFF_LINK0;
573 break;
574 default:
575 error = EPROTONOSUPPORT;
576 break;
577 }
578 goto recompute;
579 case GREGPROTO:
580 ifr->ifr_flags = sc->g_proto;
581 break;
582 case GRESADDRS:
583 case GRESADDRD:
584 error = priv_check(curthread, PRIV_NET_GRE);
585 if (error)
586 return (error);
587 /*
588 * set tunnel endpoints, compute a less specific route
589 * to the remote end and mark if as up
590 */
591 sa = &ifr->ifr_addr;
592 if (cmd == GRESADDRS)
593 sc->g_src = (satosin(sa))->sin_addr;
594 if (cmd == GRESADDRD)
595 sc->g_dst = (satosin(sa))->sin_addr;
596 recompute:
597#ifdef INET
598 if (sc->encap != NULL) {
599 encap_detach(sc->encap);
600 sc->encap = NULL;
601 }
602#endif
603 if ((sc->g_src.s_addr != INADDR_ANY) &&
604 (sc->g_dst.s_addr != INADDR_ANY)) {
605 bzero(&sp, sizeof(sp));
606 bzero(&sm, sizeof(sm));
607 bzero(&dp, sizeof(dp));
608 bzero(&dm, sizeof(dm));
609 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
610 sizeof(struct sockaddr_in);
611 sp.sin_family = sm.sin_family = dp.sin_family =
612 dm.sin_family = AF_INET;
613 sp.sin_addr = sc->g_src;
614 dp.sin_addr = sc->g_dst;
615 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
616 INADDR_BROADCAST;
617#ifdef INET
618 sc->encap = encap_attach(AF_INET, sc->g_proto,
619 sintosa(&sp), sintosa(&sm), sintosa(&dp),
620 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
621 &in_gre_protosw : &in_mobile_protosw, sc);
622 if (sc->encap == NULL)
623 printf("%s: unable to attach encap\n",
624 if_name(GRE2IFP(sc)));
625#endif
626 if (sc->route.ro_rt != 0) /* free old route */
627 RTFREE(sc->route.ro_rt);
628 if (gre_compute_route(sc) == 0)
629 ifp->if_drv_flags |= IFF_DRV_RUNNING;
630 else
631 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
632 }
633 break;
634 case GREGADDRS:
635 memset(&si, 0, sizeof(si));
636 si.sin_family = AF_INET;
637 si.sin_len = sizeof(struct sockaddr_in);
638 si.sin_addr.s_addr = sc->g_src.s_addr;
639 sa = sintosa(&si);
640 error = prison_if(curthread->td_ucred, sa);
641 if (error != 0)
642 break;
643 ifr->ifr_addr = *sa;
644 break;
645 case GREGADDRD:
646 memset(&si, 0, sizeof(si));
647 si.sin_family = AF_INET;
648 si.sin_len = sizeof(struct sockaddr_in);
649 si.sin_addr.s_addr = sc->g_dst.s_addr;
650 sa = sintosa(&si);
651 error = prison_if(curthread->td_ucred, sa);
652 if (error != 0)
653 break;
654 ifr->ifr_addr = *sa;
655 break;
656 case SIOCSIFPHYADDR:
657 /*
658 * XXXRW: Isn't this priv_check() redundant to the ifnet
659 * layer check?
660 */
661 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
662 break;
663 if (aifr->ifra_addr.sin_family != AF_INET ||
664 aifr->ifra_dstaddr.sin_family != AF_INET) {
665 error = EAFNOSUPPORT;
666 break;
667 }
668 if (aifr->ifra_addr.sin_len != sizeof(si) ||
669 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
670 error = EINVAL;
671 break;
672 }
673 sc->g_src = aifr->ifra_addr.sin_addr;
674 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
675 goto recompute;
676 case SIOCSLIFPHYADDR:
677 /*
678 * XXXRW: Isn't this priv_check() redundant to the ifnet
679 * layer check?
680 */
681 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
682 break;
683 if (lifr->addr.ss_family != AF_INET ||
684 lifr->dstaddr.ss_family != AF_INET) {
685 error = EAFNOSUPPORT;
686 break;
687 }
688 if (lifr->addr.ss_len != sizeof(si) ||
689 lifr->dstaddr.ss_len != sizeof(si)) {
690 error = EINVAL;
691 break;
692 }
693 sc->g_src = (satosin(&lifr->addr))->sin_addr;
694 sc->g_dst =
695 (satosin(&lifr->dstaddr))->sin_addr;
696 goto recompute;
697 case SIOCDIFPHYADDR:
698 /*
699 * XXXRW: Isn't this priv_check() redundant to the ifnet
700 * layer check?
701 */
702 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
703 break;
704 sc->g_src.s_addr = INADDR_ANY;
705 sc->g_dst.s_addr = INADDR_ANY;
706 goto recompute;
707 case SIOCGLIFPHYADDR:
708 if (sc->g_src.s_addr == INADDR_ANY ||
709 sc->g_dst.s_addr == INADDR_ANY) {
710 error = EADDRNOTAVAIL;
711 break;
712 }
713 memset(&si, 0, sizeof(si));
714 si.sin_family = AF_INET;
715 si.sin_len = sizeof(struct sockaddr_in);
716 si.sin_addr.s_addr = sc->g_src.s_addr;
717 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
718 if (error != 0)
719 break;
720 memcpy(&lifr->addr, &si, sizeof(si));
721 si.sin_addr.s_addr = sc->g_dst.s_addr;
722 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
723 if (error != 0)
724 break;
725 memcpy(&lifr->dstaddr, &si, sizeof(si));
726 break;
727 case SIOCGIFPSRCADDR:
728#ifdef INET6
729 case SIOCGIFPSRCADDR_IN6:
730#endif
731 if (sc->g_src.s_addr == INADDR_ANY) {
732 error = EADDRNOTAVAIL;
733 break;
734 }
735 memset(&si, 0, sizeof(si));
736 si.sin_family = AF_INET;
737 si.sin_len = sizeof(struct sockaddr_in);
738 si.sin_addr.s_addr = sc->g_src.s_addr;
739 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
740 if (error != 0)
741 break;
742 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
743 break;
744 case SIOCGIFPDSTADDR:
745#ifdef INET6
746 case SIOCGIFPDSTADDR_IN6:
747#endif
748 if (sc->g_dst.s_addr == INADDR_ANY) {
749 error = EADDRNOTAVAIL;
750 break;
751 }
752 memset(&si, 0, sizeof(si));
753 si.sin_family = AF_INET;
754 si.sin_len = sizeof(struct sockaddr_in);
755 si.sin_addr.s_addr = sc->g_dst.s_addr;
756 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
757 if (error != 0)
758 break;
759 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
760 break;
761 case GRESKEY:
762 error = priv_check(curthread, PRIV_NET_GRE);
763 if (error)
764 break;
765 error = copyin(ifr->ifr_data, &key, sizeof(key));
766 if (error)
767 break;
768 /* adjust MTU for option header */
769 if (key == 0 && sc->key != 0) /* clear */
770 adj += sizeof(key);
771 else if (key != 0 && sc->key == 0) /* set */
772 adj -= sizeof(key);
773
774 if (ifp->if_mtu + adj < 576) {
775 error = EINVAL;
776 break;
777 }
778 ifp->if_mtu += adj;
779 sc->key = key;
780 break;
781 case GREGKEY:
782 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
783 break;
784
785 default:
786 error = EINVAL;
787 break;
788 }
789
790 splx(s);
791 return (error);
792}
793
794/*
795 * computes a route to our destination that is not the one
796 * which would be taken by ip_output(), as this one will loop back to
797 * us. If the interface is p2p as a--->b, then a routing entry exists
798 * If we now send a packet to b (e.g. ping b), this will come down here
799 * gets src=a, dst=b tacked on and would from ip_output() sent back to
800 * if_gre.
801 * Goal here is to compute a route to b that is less specific than
802 * a-->b. We know that this one exists as in normal operation we have
803 * at least a default route which matches.
804 */
805static int
806gre_compute_route(struct gre_softc *sc)
807{
808 struct route *ro;
809
810 ro = &sc->route;
811
812 memset(ro, 0, sizeof(struct route));
813 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
814 ro->ro_dst.sa_family = AF_INET;
815 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
816
817 /*
818 * toggle last bit, so our interface is not found, but a less
819 * specific route. I'd rather like to specify a shorter mask,
820 * but this is not possible. Should work though. XXX
821 * XXX MRT Use a different FIB for the tunnel to solve this problem.
822 */
823 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
824 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
825 htonl(0x01);
826 }
827
828#ifdef DIAGNOSTIC
829 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
830 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
831#endif
832
833 rtalloc_fib(ro, sc->gre_fibnum);
834
835 /*
836 * check if this returned a route at all and this route is no
837 * recursion to ourself
838 */
839 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
840#ifdef DIAGNOSTIC
841 if (ro->ro_rt == NULL)
842 printf(" - no route found!\n");
843 else
844 printf(" - route loops back to ourself!\n");
845#endif
846 return EADDRNOTAVAIL;
847 }
848
849 /*
850 * now change it back - else ip_output will just drop
851 * the route and search one to this interface ...
852 */
853 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
854 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
855
856#ifdef DIAGNOSTIC
857 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
858 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
859 printf("\n");
860#endif
861
862 return 0;
863}
864
865/*
866 * do a checksum of a buffer - much like in_cksum, which operates on
867 * mbufs.
868 */
869u_int16_t
870gre_in_cksum(u_int16_t *p, u_int len)
871{
872 u_int32_t sum = 0;
873 int nwords = len >> 1;
874
875 while (nwords-- != 0)
876 sum += *p++;
877
878 if (len & 1) {
879 union {
880 u_short w;
881 u_char c[2];
882 } u;
883 u.c[0] = *(u_char *)p;
884 u.c[1] = 0;
885 sum += u.w;
886 }
887
888 /* end-around-carry */
889 sum = (sum >> 16) + (sum & 0xffff);
890 sum += (sum >> 16);
891 return (~sum);
892}
893
894static int
895gremodevent(module_t mod, int type, void *data)
896{
897
898 switch (type) {
899 case MOD_LOAD:
900 greattach();
901 break;
902 case MOD_UNLOAD:
903 if_clone_detach(&gre_cloner);
904 mtx_destroy(&gre_mtx);
905 break;
906 default:
907 return EOPNOTSUPP;
908 }
909 return 0;
910}
911
912static moduledata_t gre_mod = {
913 "if_gre",
914 gremodevent,
915 0
916};
917
918DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
919MODULE_VERSION(if_gre, 1);
509 if (error)
510 ifp->if_oerrors++;
511 return (error);
512}
513
514static int
515gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
516{
517 struct ifreq *ifr = (struct ifreq *)data;
518 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
519 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
520 struct gre_softc *sc = ifp->if_softc;
521 int s;
522 struct sockaddr_in si;
523 struct sockaddr *sa = NULL;
524 int error, adj;
525 struct sockaddr_in sp, sm, dp, dm;
526 uint32_t key;
527
528 error = 0;
529 adj = 0;
530
531 s = splnet();
532 switch (cmd) {
533 case SIOCSIFADDR:
534 ifp->if_flags |= IFF_UP;
535 break;
536 case SIOCSIFDSTADDR:
537 break;
538 case SIOCSIFFLAGS:
539 /*
540 * XXXRW: Isn't this priv_check() redundant to the ifnet
541 * layer check?
542 */
543 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
544 break;
545 if ((ifr->ifr_flags & IFF_LINK0) != 0)
546 sc->g_proto = IPPROTO_GRE;
547 else
548 sc->g_proto = IPPROTO_MOBILE;
549 if ((ifr->ifr_flags & IFF_LINK2) != 0)
550 sc->wccp_ver = WCCP_V2;
551 else
552 sc->wccp_ver = WCCP_V1;
553 goto recompute;
554 case SIOCSIFMTU:
555 /*
556 * XXXRW: Isn't this priv_check() redundant to the ifnet
557 * layer check?
558 */
559 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
560 break;
561 if (ifr->ifr_mtu < 576) {
562 error = EINVAL;
563 break;
564 }
565 ifp->if_mtu = ifr->ifr_mtu;
566 break;
567 case SIOCGIFMTU:
568 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
569 break;
570 case SIOCADDMULTI:
571 /*
572 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
573 * layer check?
574 */
575 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
576 break;
577 if (ifr == 0) {
578 error = EAFNOSUPPORT;
579 break;
580 }
581 switch (ifr->ifr_addr.sa_family) {
582#ifdef INET
583 case AF_INET:
584 break;
585#endif
586#ifdef INET6
587 case AF_INET6:
588 break;
589#endif
590 default:
591 error = EAFNOSUPPORT;
592 break;
593 }
594 break;
595 case SIOCDELMULTI:
596 /*
597 * XXXRW: Isn't this priv_check() redundant to the ifnet
598 * layer check?
599 */
600 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
601 break;
602 if (ifr == 0) {
603 error = EAFNOSUPPORT;
604 break;
605 }
606 switch (ifr->ifr_addr.sa_family) {
607#ifdef INET
608 case AF_INET:
609 break;
610#endif
611#ifdef INET6
612 case AF_INET6:
613 break;
614#endif
615 default:
616 error = EAFNOSUPPORT;
617 break;
618 }
619 break;
620 case GRESPROTO:
621 /*
622 * XXXRW: Isn't this priv_check() redundant to the ifnet
623 * layer check?
624 */
625 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
626 break;
627 sc->g_proto = ifr->ifr_flags;
628 switch (sc->g_proto) {
629 case IPPROTO_GRE:
630 ifp->if_flags |= IFF_LINK0;
631 break;
632 case IPPROTO_MOBILE:
633 ifp->if_flags &= ~IFF_LINK0;
634 break;
635 default:
636 error = EPROTONOSUPPORT;
637 break;
638 }
639 goto recompute;
640 case GREGPROTO:
641 ifr->ifr_flags = sc->g_proto;
642 break;
643 case GRESADDRS:
644 case GRESADDRD:
645 error = priv_check(curthread, PRIV_NET_GRE);
646 if (error)
647 return (error);
648 /*
649 * set tunnel endpoints, compute a less specific route
650 * to the remote end and mark if as up
651 */
652 sa = &ifr->ifr_addr;
653 if (cmd == GRESADDRS)
654 sc->g_src = (satosin(sa))->sin_addr;
655 if (cmd == GRESADDRD)
656 sc->g_dst = (satosin(sa))->sin_addr;
657 recompute:
658#ifdef INET
659 if (sc->encap != NULL) {
660 encap_detach(sc->encap);
661 sc->encap = NULL;
662 }
663#endif
664 if ((sc->g_src.s_addr != INADDR_ANY) &&
665 (sc->g_dst.s_addr != INADDR_ANY)) {
666 bzero(&sp, sizeof(sp));
667 bzero(&sm, sizeof(sm));
668 bzero(&dp, sizeof(dp));
669 bzero(&dm, sizeof(dm));
670 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
671 sizeof(struct sockaddr_in);
672 sp.sin_family = sm.sin_family = dp.sin_family =
673 dm.sin_family = AF_INET;
674 sp.sin_addr = sc->g_src;
675 dp.sin_addr = sc->g_dst;
676 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
677 INADDR_BROADCAST;
678#ifdef INET
679 sc->encap = encap_attach(AF_INET, sc->g_proto,
680 sintosa(&sp), sintosa(&sm), sintosa(&dp),
681 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
682 &in_gre_protosw : &in_mobile_protosw, sc);
683 if (sc->encap == NULL)
684 printf("%s: unable to attach encap\n",
685 if_name(GRE2IFP(sc)));
686#endif
687 if (sc->route.ro_rt != 0) /* free old route */
688 RTFREE(sc->route.ro_rt);
689 if (gre_compute_route(sc) == 0)
690 ifp->if_drv_flags |= IFF_DRV_RUNNING;
691 else
692 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
693 }
694 break;
695 case GREGADDRS:
696 memset(&si, 0, sizeof(si));
697 si.sin_family = AF_INET;
698 si.sin_len = sizeof(struct sockaddr_in);
699 si.sin_addr.s_addr = sc->g_src.s_addr;
700 sa = sintosa(&si);
701 error = prison_if(curthread->td_ucred, sa);
702 if (error != 0)
703 break;
704 ifr->ifr_addr = *sa;
705 break;
706 case GREGADDRD:
707 memset(&si, 0, sizeof(si));
708 si.sin_family = AF_INET;
709 si.sin_len = sizeof(struct sockaddr_in);
710 si.sin_addr.s_addr = sc->g_dst.s_addr;
711 sa = sintosa(&si);
712 error = prison_if(curthread->td_ucred, sa);
713 if (error != 0)
714 break;
715 ifr->ifr_addr = *sa;
716 break;
717 case SIOCSIFPHYADDR:
718 /*
719 * XXXRW: Isn't this priv_check() redundant to the ifnet
720 * layer check?
721 */
722 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
723 break;
724 if (aifr->ifra_addr.sin_family != AF_INET ||
725 aifr->ifra_dstaddr.sin_family != AF_INET) {
726 error = EAFNOSUPPORT;
727 break;
728 }
729 if (aifr->ifra_addr.sin_len != sizeof(si) ||
730 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
731 error = EINVAL;
732 break;
733 }
734 sc->g_src = aifr->ifra_addr.sin_addr;
735 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
736 goto recompute;
737 case SIOCSLIFPHYADDR:
738 /*
739 * XXXRW: Isn't this priv_check() redundant to the ifnet
740 * layer check?
741 */
742 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
743 break;
744 if (lifr->addr.ss_family != AF_INET ||
745 lifr->dstaddr.ss_family != AF_INET) {
746 error = EAFNOSUPPORT;
747 break;
748 }
749 if (lifr->addr.ss_len != sizeof(si) ||
750 lifr->dstaddr.ss_len != sizeof(si)) {
751 error = EINVAL;
752 break;
753 }
754 sc->g_src = (satosin(&lifr->addr))->sin_addr;
755 sc->g_dst =
756 (satosin(&lifr->dstaddr))->sin_addr;
757 goto recompute;
758 case SIOCDIFPHYADDR:
759 /*
760 * XXXRW: Isn't this priv_check() redundant to the ifnet
761 * layer check?
762 */
763 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
764 break;
765 sc->g_src.s_addr = INADDR_ANY;
766 sc->g_dst.s_addr = INADDR_ANY;
767 goto recompute;
768 case SIOCGLIFPHYADDR:
769 if (sc->g_src.s_addr == INADDR_ANY ||
770 sc->g_dst.s_addr == INADDR_ANY) {
771 error = EADDRNOTAVAIL;
772 break;
773 }
774 memset(&si, 0, sizeof(si));
775 si.sin_family = AF_INET;
776 si.sin_len = sizeof(struct sockaddr_in);
777 si.sin_addr.s_addr = sc->g_src.s_addr;
778 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
779 if (error != 0)
780 break;
781 memcpy(&lifr->addr, &si, sizeof(si));
782 si.sin_addr.s_addr = sc->g_dst.s_addr;
783 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
784 if (error != 0)
785 break;
786 memcpy(&lifr->dstaddr, &si, sizeof(si));
787 break;
788 case SIOCGIFPSRCADDR:
789#ifdef INET6
790 case SIOCGIFPSRCADDR_IN6:
791#endif
792 if (sc->g_src.s_addr == INADDR_ANY) {
793 error = EADDRNOTAVAIL;
794 break;
795 }
796 memset(&si, 0, sizeof(si));
797 si.sin_family = AF_INET;
798 si.sin_len = sizeof(struct sockaddr_in);
799 si.sin_addr.s_addr = sc->g_src.s_addr;
800 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
801 if (error != 0)
802 break;
803 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
804 break;
805 case SIOCGIFPDSTADDR:
806#ifdef INET6
807 case SIOCGIFPDSTADDR_IN6:
808#endif
809 if (sc->g_dst.s_addr == INADDR_ANY) {
810 error = EADDRNOTAVAIL;
811 break;
812 }
813 memset(&si, 0, sizeof(si));
814 si.sin_family = AF_INET;
815 si.sin_len = sizeof(struct sockaddr_in);
816 si.sin_addr.s_addr = sc->g_dst.s_addr;
817 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
818 if (error != 0)
819 break;
820 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
821 break;
822 case GRESKEY:
823 error = priv_check(curthread, PRIV_NET_GRE);
824 if (error)
825 break;
826 error = copyin(ifr->ifr_data, &key, sizeof(key));
827 if (error)
828 break;
829 /* adjust MTU for option header */
830 if (key == 0 && sc->key != 0) /* clear */
831 adj += sizeof(key);
832 else if (key != 0 && sc->key == 0) /* set */
833 adj -= sizeof(key);
834
835 if (ifp->if_mtu + adj < 576) {
836 error = EINVAL;
837 break;
838 }
839 ifp->if_mtu += adj;
840 sc->key = key;
841 break;
842 case GREGKEY:
843 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
844 break;
845
846 default:
847 error = EINVAL;
848 break;
849 }
850
851 splx(s);
852 return (error);
853}
854
855/*
856 * computes a route to our destination that is not the one
857 * which would be taken by ip_output(), as this one will loop back to
858 * us. If the interface is p2p as a--->b, then a routing entry exists
859 * If we now send a packet to b (e.g. ping b), this will come down here
860 * gets src=a, dst=b tacked on and would from ip_output() sent back to
861 * if_gre.
862 * Goal here is to compute a route to b that is less specific than
863 * a-->b. We know that this one exists as in normal operation we have
864 * at least a default route which matches.
865 */
866static int
867gre_compute_route(struct gre_softc *sc)
868{
869 struct route *ro;
870
871 ro = &sc->route;
872
873 memset(ro, 0, sizeof(struct route));
874 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
875 ro->ro_dst.sa_family = AF_INET;
876 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
877
878 /*
879 * toggle last bit, so our interface is not found, but a less
880 * specific route. I'd rather like to specify a shorter mask,
881 * but this is not possible. Should work though. XXX
882 * XXX MRT Use a different FIB for the tunnel to solve this problem.
883 */
884 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
885 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
886 htonl(0x01);
887 }
888
889#ifdef DIAGNOSTIC
890 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
891 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
892#endif
893
894 rtalloc_fib(ro, sc->gre_fibnum);
895
896 /*
897 * check if this returned a route at all and this route is no
898 * recursion to ourself
899 */
900 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
901#ifdef DIAGNOSTIC
902 if (ro->ro_rt == NULL)
903 printf(" - no route found!\n");
904 else
905 printf(" - route loops back to ourself!\n");
906#endif
907 return EADDRNOTAVAIL;
908 }
909
910 /*
911 * now change it back - else ip_output will just drop
912 * the route and search one to this interface ...
913 */
914 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
915 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
916
917#ifdef DIAGNOSTIC
918 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
919 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
920 printf("\n");
921#endif
922
923 return 0;
924}
925
926/*
927 * do a checksum of a buffer - much like in_cksum, which operates on
928 * mbufs.
929 */
930u_int16_t
931gre_in_cksum(u_int16_t *p, u_int len)
932{
933 u_int32_t sum = 0;
934 int nwords = len >> 1;
935
936 while (nwords-- != 0)
937 sum += *p++;
938
939 if (len & 1) {
940 union {
941 u_short w;
942 u_char c[2];
943 } u;
944 u.c[0] = *(u_char *)p;
945 u.c[1] = 0;
946 sum += u.w;
947 }
948
949 /* end-around-carry */
950 sum = (sum >> 16) + (sum & 0xffff);
951 sum += (sum >> 16);
952 return (~sum);
953}
954
955static int
956gremodevent(module_t mod, int type, void *data)
957{
958
959 switch (type) {
960 case MOD_LOAD:
961 greattach();
962 break;
963 case MOD_UNLOAD:
964 if_clone_detach(&gre_cloner);
965 mtx_destroy(&gre_mtx);
966 break;
967 default:
968 return EOPNOTSUPP;
969 }
970 return 0;
971}
972
973static moduledata_t gre_mod = {
974 "if_gre",
975 gremodevent,
976 0
977};
978
979DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
980MODULE_VERSION(if_gre, 1);