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