1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1998 The NetBSD Foundation, Inc. |
---|
5 | * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org> |
---|
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 | * $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ |
---|
35 | */ |
---|
36 | |
---|
37 | #include <sys/cdefs.h> |
---|
38 | __FBSDID("$FreeBSD$"); |
---|
39 | |
---|
40 | #include <rtems/bsd/local/opt_inet.h> |
---|
41 | #include <rtems/bsd/local/opt_inet6.h> |
---|
42 | |
---|
43 | #include <sys/param.h> |
---|
44 | #include <sys/jail.h> |
---|
45 | #include <sys/kernel.h> |
---|
46 | #include <sys/lock.h> |
---|
47 | #include <sys/libkern.h> |
---|
48 | #include <sys/malloc.h> |
---|
49 | #include <sys/module.h> |
---|
50 | #include <sys/mbuf.h> |
---|
51 | #include <sys/priv.h> |
---|
52 | #include <sys/proc.h> |
---|
53 | #include <sys/protosw.h> |
---|
54 | #include <sys/rmlock.h> |
---|
55 | #include <sys/socket.h> |
---|
56 | #include <sys/sockio.h> |
---|
57 | #include <sys/sx.h> |
---|
58 | #include <sys/sysctl.h> |
---|
59 | #include <sys/syslog.h> |
---|
60 | #include <sys/systm.h> |
---|
61 | |
---|
62 | #include <net/ethernet.h> |
---|
63 | #include <net/if.h> |
---|
64 | #include <net/if_var.h> |
---|
65 | #include <net/if_clone.h> |
---|
66 | #include <net/if_types.h> |
---|
67 | #include <net/netisr.h> |
---|
68 | #include <net/vnet.h> |
---|
69 | #include <net/route.h> |
---|
70 | |
---|
71 | #include <netinet/in.h> |
---|
72 | #ifdef INET |
---|
73 | #include <netinet/in_systm.h> |
---|
74 | #include <netinet/in_var.h> |
---|
75 | #include <netinet/ip.h> |
---|
76 | #include <netinet/ip_var.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | #ifdef INET6 |
---|
80 | #include <netinet/ip6.h> |
---|
81 | #include <netinet6/in6_var.h> |
---|
82 | #include <netinet6/ip6_var.h> |
---|
83 | #include <netinet6/scope6_var.h> |
---|
84 | #endif |
---|
85 | |
---|
86 | #include <netinet/ip_encap.h> |
---|
87 | #include <net/bpf.h> |
---|
88 | #include <net/if_gre.h> |
---|
89 | |
---|
90 | #include <machine/in_cksum.h> |
---|
91 | #include <security/mac/mac_framework.h> |
---|
92 | |
---|
93 | #define GREMTU 1500 |
---|
94 | static const char grename[] = "gre"; |
---|
95 | static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation"); |
---|
96 | static VNET_DEFINE(struct mtx, gre_mtx); |
---|
97 | #define V_gre_mtx VNET(gre_mtx) |
---|
98 | #define GRE_LIST_LOCK_INIT(x) mtx_init(&V_gre_mtx, "gre_mtx", NULL, \ |
---|
99 | MTX_DEF) |
---|
100 | #define GRE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gre_mtx) |
---|
101 | #define GRE_LIST_LOCK(x) mtx_lock(&V_gre_mtx) |
---|
102 | #define GRE_LIST_UNLOCK(x) mtx_unlock(&V_gre_mtx) |
---|
103 | |
---|
104 | static VNET_DEFINE(LIST_HEAD(, gre_softc), gre_softc_list); |
---|
105 | #define V_gre_softc_list VNET(gre_softc_list) |
---|
106 | static struct sx gre_ioctl_sx; |
---|
107 | SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl"); |
---|
108 | |
---|
109 | static int gre_clone_create(struct if_clone *, int, caddr_t); |
---|
110 | static void gre_clone_destroy(struct ifnet *); |
---|
111 | static VNET_DEFINE(struct if_clone *, gre_cloner); |
---|
112 | #define V_gre_cloner VNET(gre_cloner) |
---|
113 | |
---|
114 | static void gre_qflush(struct ifnet *); |
---|
115 | static int gre_transmit(struct ifnet *, struct mbuf *); |
---|
116 | static int gre_ioctl(struct ifnet *, u_long, caddr_t); |
---|
117 | static int gre_output(struct ifnet *, struct mbuf *, |
---|
118 | const struct sockaddr *, struct route *); |
---|
119 | |
---|
120 | static void gre_updatehdr(struct gre_softc *); |
---|
121 | static int gre_set_tunnel(struct ifnet *, struct sockaddr *, |
---|
122 | struct sockaddr *); |
---|
123 | static void gre_delete_tunnel(struct ifnet *); |
---|
124 | |
---|
125 | SYSCTL_DECL(_net_link); |
---|
126 | static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, |
---|
127 | "Generic Routing Encapsulation"); |
---|
128 | #ifndef MAX_GRE_NEST |
---|
129 | /* |
---|
130 | * This macro controls the default upper limitation on nesting of gre tunnels. |
---|
131 | * Since, setting a large value to this macro with a careless configuration |
---|
132 | * may introduce system crash, we don't allow any nestings by default. |
---|
133 | * If you need to configure nested gre tunnels, you can define this macro |
---|
134 | * in your kernel configuration file. However, if you do so, please be |
---|
135 | * careful to configure the tunnels so that it won't make a loop. |
---|
136 | */ |
---|
137 | #define MAX_GRE_NEST 1 |
---|
138 | #endif |
---|
139 | |
---|
140 | static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST; |
---|
141 | #define V_max_gre_nesting VNET(max_gre_nesting) |
---|
142 | SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET, |
---|
143 | &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels"); |
---|
144 | |
---|
145 | static void |
---|
146 | vnet_gre_init(const void *unused __unused) |
---|
147 | { |
---|
148 | LIST_INIT(&V_gre_softc_list); |
---|
149 | GRE_LIST_LOCK_INIT(); |
---|
150 | V_gre_cloner = if_clone_simple(grename, gre_clone_create, |
---|
151 | gre_clone_destroy, 0); |
---|
152 | } |
---|
153 | VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, |
---|
154 | vnet_gre_init, NULL); |
---|
155 | |
---|
156 | static void |
---|
157 | vnet_gre_uninit(const void *unused __unused) |
---|
158 | { |
---|
159 | |
---|
160 | if_clone_detach(V_gre_cloner); |
---|
161 | GRE_LIST_LOCK_DESTROY(); |
---|
162 | } |
---|
163 | VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, |
---|
164 | vnet_gre_uninit, NULL); |
---|
165 | |
---|
166 | static int |
---|
167 | gre_clone_create(struct if_clone *ifc, int unit, caddr_t params) |
---|
168 | { |
---|
169 | struct gre_softc *sc; |
---|
170 | |
---|
171 | sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); |
---|
172 | #ifndef __rtems__ |
---|
173 | sc->gre_fibnum = curthread->td_proc->p_fibnum; |
---|
174 | #else /* __rtems__ */ |
---|
175 | sc->gre_fibnum = BSD_DEFAULT_FIB; |
---|
176 | #endif /* __rtems__ */ |
---|
177 | GRE2IFP(sc) = if_alloc(IFT_TUNNEL); |
---|
178 | GRE_LOCK_INIT(sc); |
---|
179 | GRE2IFP(sc)->if_softc = sc; |
---|
180 | if_initname(GRE2IFP(sc), grename, unit); |
---|
181 | |
---|
182 | GRE2IFP(sc)->if_mtu = sc->gre_mtu = GREMTU; |
---|
183 | GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; |
---|
184 | GRE2IFP(sc)->if_output = gre_output; |
---|
185 | GRE2IFP(sc)->if_ioctl = gre_ioctl; |
---|
186 | GRE2IFP(sc)->if_transmit = gre_transmit; |
---|
187 | GRE2IFP(sc)->if_qflush = gre_qflush; |
---|
188 | GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE; |
---|
189 | GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE; |
---|
190 | if_attach(GRE2IFP(sc)); |
---|
191 | bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); |
---|
192 | GRE_LIST_LOCK(); |
---|
193 | LIST_INSERT_HEAD(&V_gre_softc_list, sc, gre_list); |
---|
194 | GRE_LIST_UNLOCK(); |
---|
195 | return (0); |
---|
196 | } |
---|
197 | |
---|
198 | static void |
---|
199 | gre_clone_destroy(struct ifnet *ifp) |
---|
200 | { |
---|
201 | struct gre_softc *sc; |
---|
202 | |
---|
203 | sx_xlock(&gre_ioctl_sx); |
---|
204 | sc = ifp->if_softc; |
---|
205 | gre_delete_tunnel(ifp); |
---|
206 | GRE_LIST_LOCK(); |
---|
207 | LIST_REMOVE(sc, gre_list); |
---|
208 | GRE_LIST_UNLOCK(); |
---|
209 | bpfdetach(ifp); |
---|
210 | if_detach(ifp); |
---|
211 | ifp->if_softc = NULL; |
---|
212 | sx_xunlock(&gre_ioctl_sx); |
---|
213 | |
---|
214 | if_free(ifp); |
---|
215 | GRE_LOCK_DESTROY(sc); |
---|
216 | free(sc, M_GRE); |
---|
217 | } |
---|
218 | |
---|
219 | static int |
---|
220 | gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) |
---|
221 | { |
---|
222 | GRE_RLOCK_TRACKER; |
---|
223 | struct ifreq *ifr = (struct ifreq *)data; |
---|
224 | struct sockaddr *src, *dst; |
---|
225 | struct gre_softc *sc; |
---|
226 | #ifdef INET |
---|
227 | struct sockaddr_in *sin = NULL; |
---|
228 | #endif |
---|
229 | #ifdef INET6 |
---|
230 | struct sockaddr_in6 *sin6 = NULL; |
---|
231 | #endif |
---|
232 | uint32_t opt; |
---|
233 | int error; |
---|
234 | |
---|
235 | switch (cmd) { |
---|
236 | case SIOCSIFMTU: |
---|
237 | /* XXX: */ |
---|
238 | if (ifr->ifr_mtu < 576) |
---|
239 | return (EINVAL); |
---|
240 | break; |
---|
241 | case SIOCSIFADDR: |
---|
242 | ifp->if_flags |= IFF_UP; |
---|
243 | case SIOCSIFFLAGS: |
---|
244 | case SIOCADDMULTI: |
---|
245 | case SIOCDELMULTI: |
---|
246 | return (0); |
---|
247 | case GRESADDRS: |
---|
248 | case GRESADDRD: |
---|
249 | case GREGADDRS: |
---|
250 | case GREGADDRD: |
---|
251 | case GRESPROTO: |
---|
252 | case GREGPROTO: |
---|
253 | return (EOPNOTSUPP); |
---|
254 | } |
---|
255 | src = dst = NULL; |
---|
256 | sx_xlock(&gre_ioctl_sx); |
---|
257 | sc = ifp->if_softc; |
---|
258 | if (sc == NULL) { |
---|
259 | error = ENXIO; |
---|
260 | goto end; |
---|
261 | } |
---|
262 | error = 0; |
---|
263 | switch (cmd) { |
---|
264 | case SIOCSIFMTU: |
---|
265 | GRE_WLOCK(sc); |
---|
266 | sc->gre_mtu = ifr->ifr_mtu; |
---|
267 | gre_updatehdr(sc); |
---|
268 | GRE_WUNLOCK(sc); |
---|
269 | goto end; |
---|
270 | case SIOCSIFPHYADDR: |
---|
271 | #ifdef INET6 |
---|
272 | case SIOCSIFPHYADDR_IN6: |
---|
273 | #endif |
---|
274 | error = EINVAL; |
---|
275 | switch (cmd) { |
---|
276 | #ifdef INET |
---|
277 | case SIOCSIFPHYADDR: |
---|
278 | src = (struct sockaddr *) |
---|
279 | &(((struct in_aliasreq *)data)->ifra_addr); |
---|
280 | dst = (struct sockaddr *) |
---|
281 | &(((struct in_aliasreq *)data)->ifra_dstaddr); |
---|
282 | break; |
---|
283 | #endif |
---|
284 | #ifdef INET6 |
---|
285 | case SIOCSIFPHYADDR_IN6: |
---|
286 | src = (struct sockaddr *) |
---|
287 | &(((struct in6_aliasreq *)data)->ifra_addr); |
---|
288 | dst = (struct sockaddr *) |
---|
289 | &(((struct in6_aliasreq *)data)->ifra_dstaddr); |
---|
290 | break; |
---|
291 | #endif |
---|
292 | default: |
---|
293 | error = EAFNOSUPPORT; |
---|
294 | goto end; |
---|
295 | } |
---|
296 | /* sa_family must be equal */ |
---|
297 | if (src->sa_family != dst->sa_family || |
---|
298 | src->sa_len != dst->sa_len) |
---|
299 | goto end; |
---|
300 | |
---|
301 | /* validate sa_len */ |
---|
302 | switch (src->sa_family) { |
---|
303 | #ifdef INET |
---|
304 | case AF_INET: |
---|
305 | if (src->sa_len != sizeof(struct sockaddr_in)) |
---|
306 | goto end; |
---|
307 | break; |
---|
308 | #endif |
---|
309 | #ifdef INET6 |
---|
310 | case AF_INET6: |
---|
311 | if (src->sa_len != sizeof(struct sockaddr_in6)) |
---|
312 | goto end; |
---|
313 | break; |
---|
314 | #endif |
---|
315 | default: |
---|
316 | error = EAFNOSUPPORT; |
---|
317 | goto end; |
---|
318 | } |
---|
319 | /* check sa_family looks sane for the cmd */ |
---|
320 | error = EAFNOSUPPORT; |
---|
321 | switch (cmd) { |
---|
322 | #ifdef INET |
---|
323 | case SIOCSIFPHYADDR: |
---|
324 | if (src->sa_family == AF_INET) |
---|
325 | break; |
---|
326 | goto end; |
---|
327 | #endif |
---|
328 | #ifdef INET6 |
---|
329 | case SIOCSIFPHYADDR_IN6: |
---|
330 | if (src->sa_family == AF_INET6) |
---|
331 | break; |
---|
332 | goto end; |
---|
333 | #endif |
---|
334 | } |
---|
335 | error = EADDRNOTAVAIL; |
---|
336 | switch (src->sa_family) { |
---|
337 | #ifdef INET |
---|
338 | case AF_INET: |
---|
339 | if (satosin(src)->sin_addr.s_addr == INADDR_ANY || |
---|
340 | satosin(dst)->sin_addr.s_addr == INADDR_ANY) |
---|
341 | goto end; |
---|
342 | break; |
---|
343 | #endif |
---|
344 | #ifdef INET6 |
---|
345 | case AF_INET6: |
---|
346 | if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr) |
---|
347 | || |
---|
348 | IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr)) |
---|
349 | goto end; |
---|
350 | /* |
---|
351 | * Check validity of the scope zone ID of the |
---|
352 | * addresses, and convert it into the kernel |
---|
353 | * internal form if necessary. |
---|
354 | */ |
---|
355 | error = sa6_embedscope(satosin6(src), 0); |
---|
356 | if (error != 0) |
---|
357 | goto end; |
---|
358 | error = sa6_embedscope(satosin6(dst), 0); |
---|
359 | if (error != 0) |
---|
360 | goto end; |
---|
361 | #endif |
---|
362 | } |
---|
363 | error = gre_set_tunnel(ifp, src, dst); |
---|
364 | break; |
---|
365 | case SIOCDIFPHYADDR: |
---|
366 | gre_delete_tunnel(ifp); |
---|
367 | break; |
---|
368 | case SIOCGIFPSRCADDR: |
---|
369 | case SIOCGIFPDSTADDR: |
---|
370 | #ifdef INET6 |
---|
371 | case SIOCGIFPSRCADDR_IN6: |
---|
372 | case SIOCGIFPDSTADDR_IN6: |
---|
373 | #endif |
---|
374 | if (sc->gre_family == 0) { |
---|
375 | error = EADDRNOTAVAIL; |
---|
376 | break; |
---|
377 | } |
---|
378 | GRE_RLOCK(sc); |
---|
379 | switch (cmd) { |
---|
380 | #ifdef INET |
---|
381 | case SIOCGIFPSRCADDR: |
---|
382 | case SIOCGIFPDSTADDR: |
---|
383 | if (sc->gre_family != AF_INET) { |
---|
384 | error = EADDRNOTAVAIL; |
---|
385 | break; |
---|
386 | } |
---|
387 | sin = (struct sockaddr_in *)&ifr->ifr_addr; |
---|
388 | memset(sin, 0, sizeof(*sin)); |
---|
389 | sin->sin_family = AF_INET; |
---|
390 | sin->sin_len = sizeof(*sin); |
---|
391 | break; |
---|
392 | #endif |
---|
393 | #ifdef INET6 |
---|
394 | case SIOCGIFPSRCADDR_IN6: |
---|
395 | case SIOCGIFPDSTADDR_IN6: |
---|
396 | if (sc->gre_family != AF_INET6) { |
---|
397 | error = EADDRNOTAVAIL; |
---|
398 | break; |
---|
399 | } |
---|
400 | sin6 = (struct sockaddr_in6 *) |
---|
401 | &(((struct in6_ifreq *)data)->ifr_addr); |
---|
402 | memset(sin6, 0, sizeof(*sin6)); |
---|
403 | sin6->sin6_family = AF_INET6; |
---|
404 | sin6->sin6_len = sizeof(*sin6); |
---|
405 | break; |
---|
406 | #endif |
---|
407 | } |
---|
408 | if (error == 0) { |
---|
409 | switch (cmd) { |
---|
410 | #ifdef INET |
---|
411 | case SIOCGIFPSRCADDR: |
---|
412 | sin->sin_addr = sc->gre_oip.ip_src; |
---|
413 | break; |
---|
414 | case SIOCGIFPDSTADDR: |
---|
415 | sin->sin_addr = sc->gre_oip.ip_dst; |
---|
416 | break; |
---|
417 | #endif |
---|
418 | #ifdef INET6 |
---|
419 | case SIOCGIFPSRCADDR_IN6: |
---|
420 | sin6->sin6_addr = sc->gre_oip6.ip6_src; |
---|
421 | break; |
---|
422 | case SIOCGIFPDSTADDR_IN6: |
---|
423 | sin6->sin6_addr = sc->gre_oip6.ip6_dst; |
---|
424 | break; |
---|
425 | #endif |
---|
426 | } |
---|
427 | } |
---|
428 | GRE_RUNLOCK(sc); |
---|
429 | if (error != 0) |
---|
430 | break; |
---|
431 | switch (cmd) { |
---|
432 | #ifdef INET |
---|
433 | case SIOCGIFPSRCADDR: |
---|
434 | case SIOCGIFPDSTADDR: |
---|
435 | error = prison_if(curthread->td_ucred, |
---|
436 | (struct sockaddr *)sin); |
---|
437 | if (error != 0) |
---|
438 | memset(sin, 0, sizeof(*sin)); |
---|
439 | break; |
---|
440 | #endif |
---|
441 | #ifdef INET6 |
---|
442 | case SIOCGIFPSRCADDR_IN6: |
---|
443 | case SIOCGIFPDSTADDR_IN6: |
---|
444 | error = prison_if(curthread->td_ucred, |
---|
445 | (struct sockaddr *)sin6); |
---|
446 | if (error == 0) |
---|
447 | error = sa6_recoverscope(sin6); |
---|
448 | if (error != 0) |
---|
449 | memset(sin6, 0, sizeof(*sin6)); |
---|
450 | #endif |
---|
451 | } |
---|
452 | break; |
---|
453 | case SIOCGTUNFIB: |
---|
454 | ifr->ifr_fib = sc->gre_fibnum; |
---|
455 | break; |
---|
456 | case SIOCSTUNFIB: |
---|
457 | if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) |
---|
458 | break; |
---|
459 | if (ifr->ifr_fib >= rt_numfibs) |
---|
460 | error = EINVAL; |
---|
461 | else |
---|
462 | sc->gre_fibnum = ifr->ifr_fib; |
---|
463 | break; |
---|
464 | case GRESKEY: |
---|
465 | if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) |
---|
466 | break; |
---|
467 | if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0) |
---|
468 | break; |
---|
469 | if (sc->gre_key != opt) { |
---|
470 | GRE_WLOCK(sc); |
---|
471 | sc->gre_key = opt; |
---|
472 | gre_updatehdr(sc); |
---|
473 | GRE_WUNLOCK(sc); |
---|
474 | } |
---|
475 | break; |
---|
476 | case GREGKEY: |
---|
477 | error = copyout(&sc->gre_key, ifr->ifr_data, |
---|
478 | sizeof(sc->gre_key)); |
---|
479 | break; |
---|
480 | case GRESOPTS: |
---|
481 | if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) |
---|
482 | break; |
---|
483 | if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0) |
---|
484 | break; |
---|
485 | if (opt & ~GRE_OPTMASK) |
---|
486 | error = EINVAL; |
---|
487 | else { |
---|
488 | if (sc->gre_options != opt) { |
---|
489 | GRE_WLOCK(sc); |
---|
490 | sc->gre_options = opt; |
---|
491 | gre_updatehdr(sc); |
---|
492 | GRE_WUNLOCK(sc); |
---|
493 | } |
---|
494 | } |
---|
495 | break; |
---|
496 | |
---|
497 | case GREGOPTS: |
---|
498 | error = copyout(&sc->gre_options, ifr->ifr_data, |
---|
499 | sizeof(sc->gre_options)); |
---|
500 | break; |
---|
501 | default: |
---|
502 | error = EINVAL; |
---|
503 | break; |
---|
504 | } |
---|
505 | end: |
---|
506 | sx_xunlock(&gre_ioctl_sx); |
---|
507 | return (error); |
---|
508 | } |
---|
509 | |
---|
510 | static void |
---|
511 | gre_updatehdr(struct gre_softc *sc) |
---|
512 | { |
---|
513 | struct grehdr *gh = NULL; |
---|
514 | uint32_t *opts; |
---|
515 | uint16_t flags; |
---|
516 | |
---|
517 | GRE_WLOCK_ASSERT(sc); |
---|
518 | switch (sc->gre_family) { |
---|
519 | #ifdef INET |
---|
520 | case AF_INET: |
---|
521 | sc->gre_hlen = sizeof(struct greip); |
---|
522 | sc->gre_oip.ip_v = IPPROTO_IPV4; |
---|
523 | sc->gre_oip.ip_hl = sizeof(struct ip) >> 2; |
---|
524 | sc->gre_oip.ip_p = IPPROTO_GRE; |
---|
525 | gh = &sc->gre_gihdr->gi_gre; |
---|
526 | break; |
---|
527 | #endif |
---|
528 | #ifdef INET6 |
---|
529 | case AF_INET6: |
---|
530 | sc->gre_hlen = sizeof(struct greip6); |
---|
531 | sc->gre_oip6.ip6_vfc = IPV6_VERSION; |
---|
532 | sc->gre_oip6.ip6_nxt = IPPROTO_GRE; |
---|
533 | gh = &sc->gre_gi6hdr->gi6_gre; |
---|
534 | break; |
---|
535 | #endif |
---|
536 | default: |
---|
537 | return; |
---|
538 | } |
---|
539 | flags = 0; |
---|
540 | opts = gh->gre_opts; |
---|
541 | if (sc->gre_options & GRE_ENABLE_CSUM) { |
---|
542 | flags |= GRE_FLAGS_CP; |
---|
543 | sc->gre_hlen += 2 * sizeof(uint16_t); |
---|
544 | *opts++ = 0; |
---|
545 | } |
---|
546 | if (sc->gre_key != 0) { |
---|
547 | flags |= GRE_FLAGS_KP; |
---|
548 | sc->gre_hlen += sizeof(uint32_t); |
---|
549 | *opts++ = htonl(sc->gre_key); |
---|
550 | } |
---|
551 | if (sc->gre_options & GRE_ENABLE_SEQ) { |
---|
552 | flags |= GRE_FLAGS_SP; |
---|
553 | sc->gre_hlen += sizeof(uint32_t); |
---|
554 | *opts++ = 0; |
---|
555 | } else |
---|
556 | sc->gre_oseq = 0; |
---|
557 | gh->gre_flags = htons(flags); |
---|
558 | GRE2IFP(sc)->if_mtu = sc->gre_mtu - sc->gre_hlen; |
---|
559 | } |
---|
560 | |
---|
561 | static void |
---|
562 | gre_detach(struct gre_softc *sc) |
---|
563 | { |
---|
564 | |
---|
565 | sx_assert(&gre_ioctl_sx, SA_XLOCKED); |
---|
566 | if (sc->gre_ecookie != NULL) |
---|
567 | encap_detach(sc->gre_ecookie); |
---|
568 | sc->gre_ecookie = NULL; |
---|
569 | } |
---|
570 | |
---|
571 | static int |
---|
572 | gre_set_tunnel(struct ifnet *ifp, struct sockaddr *src, |
---|
573 | struct sockaddr *dst) |
---|
574 | { |
---|
575 | struct gre_softc *sc, *tsc; |
---|
576 | #ifdef INET6 |
---|
577 | struct ip6_hdr *ip6; |
---|
578 | #endif |
---|
579 | #ifdef INET |
---|
580 | struct ip *ip; |
---|
581 | #endif |
---|
582 | void *hdr; |
---|
583 | int error; |
---|
584 | |
---|
585 | sx_assert(&gre_ioctl_sx, SA_XLOCKED); |
---|
586 | GRE_LIST_LOCK(); |
---|
587 | sc = ifp->if_softc; |
---|
588 | LIST_FOREACH(tsc, &V_gre_softc_list, gre_list) { |
---|
589 | if (tsc == sc || tsc->gre_family != src->sa_family) |
---|
590 | continue; |
---|
591 | #ifdef INET |
---|
592 | if (tsc->gre_family == AF_INET && |
---|
593 | tsc->gre_oip.ip_src.s_addr == |
---|
594 | satosin(src)->sin_addr.s_addr && |
---|
595 | tsc->gre_oip.ip_dst.s_addr == |
---|
596 | satosin(dst)->sin_addr.s_addr) { |
---|
597 | GRE_LIST_UNLOCK(); |
---|
598 | return (EADDRNOTAVAIL); |
---|
599 | } |
---|
600 | #endif |
---|
601 | #ifdef INET6 |
---|
602 | if (tsc->gre_family == AF_INET6 && |
---|
603 | IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_src, |
---|
604 | &satosin6(src)->sin6_addr) && |
---|
605 | IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_dst, |
---|
606 | &satosin6(dst)->sin6_addr)) { |
---|
607 | GRE_LIST_UNLOCK(); |
---|
608 | return (EADDRNOTAVAIL); |
---|
609 | } |
---|
610 | #endif |
---|
611 | } |
---|
612 | GRE_LIST_UNLOCK(); |
---|
613 | |
---|
614 | switch (src->sa_family) { |
---|
615 | #ifdef INET |
---|
616 | case AF_INET: |
---|
617 | hdr = ip = malloc(sizeof(struct greip) + |
---|
618 | 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO); |
---|
619 | ip->ip_src = satosin(src)->sin_addr; |
---|
620 | ip->ip_dst = satosin(dst)->sin_addr; |
---|
621 | break; |
---|
622 | #endif |
---|
623 | #ifdef INET6 |
---|
624 | case AF_INET6: |
---|
625 | hdr = ip6 = malloc(sizeof(struct greip6) + |
---|
626 | 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO); |
---|
627 | ip6->ip6_src = satosin6(src)->sin6_addr; |
---|
628 | ip6->ip6_dst = satosin6(dst)->sin6_addr; |
---|
629 | break; |
---|
630 | #endif |
---|
631 | default: |
---|
632 | return (EAFNOSUPPORT); |
---|
633 | } |
---|
634 | if (sc->gre_family != 0) |
---|
635 | gre_detach(sc); |
---|
636 | GRE_WLOCK(sc); |
---|
637 | if (sc->gre_family != 0) |
---|
638 | free(sc->gre_hdr, M_GRE); |
---|
639 | sc->gre_family = src->sa_family; |
---|
640 | sc->gre_hdr = hdr; |
---|
641 | sc->gre_oseq = 0; |
---|
642 | sc->gre_iseq = UINT32_MAX; |
---|
643 | gre_updatehdr(sc); |
---|
644 | GRE_WUNLOCK(sc); |
---|
645 | |
---|
646 | error = 0; |
---|
647 | switch (src->sa_family) { |
---|
648 | #ifdef INET |
---|
649 | case AF_INET: |
---|
650 | error = in_gre_attach(sc); |
---|
651 | break; |
---|
652 | #endif |
---|
653 | #ifdef INET6 |
---|
654 | case AF_INET6: |
---|
655 | error = in6_gre_attach(sc); |
---|
656 | break; |
---|
657 | #endif |
---|
658 | } |
---|
659 | if (error == 0) { |
---|
660 | ifp->if_drv_flags |= IFF_DRV_RUNNING; |
---|
661 | if_link_state_change(ifp, LINK_STATE_UP); |
---|
662 | } |
---|
663 | return (error); |
---|
664 | } |
---|
665 | |
---|
666 | static void |
---|
667 | gre_delete_tunnel(struct ifnet *ifp) |
---|
668 | { |
---|
669 | struct gre_softc *sc = ifp->if_softc; |
---|
670 | int family; |
---|
671 | |
---|
672 | GRE_WLOCK(sc); |
---|
673 | family = sc->gre_family; |
---|
674 | sc->gre_family = 0; |
---|
675 | GRE_WUNLOCK(sc); |
---|
676 | if (family != 0) { |
---|
677 | gre_detach(sc); |
---|
678 | free(sc->gre_hdr, M_GRE); |
---|
679 | } |
---|
680 | ifp->if_drv_flags &= ~IFF_DRV_RUNNING; |
---|
681 | if_link_state_change(ifp, LINK_STATE_DOWN); |
---|
682 | } |
---|
683 | |
---|
684 | int |
---|
685 | gre_input(struct mbuf **mp, int *offp, int proto) |
---|
686 | { |
---|
687 | struct gre_softc *sc; |
---|
688 | struct grehdr *gh; |
---|
689 | struct ifnet *ifp; |
---|
690 | struct mbuf *m; |
---|
691 | uint32_t *opts; |
---|
692 | #ifdef notyet |
---|
693 | uint32_t key; |
---|
694 | #endif |
---|
695 | uint16_t flags; |
---|
696 | int hlen, isr, af; |
---|
697 | |
---|
698 | m = *mp; |
---|
699 | sc = encap_getarg(m); |
---|
700 | KASSERT(sc != NULL, ("encap_getarg returned NULL")); |
---|
701 | |
---|
702 | ifp = GRE2IFP(sc); |
---|
703 | hlen = *offp + sizeof(struct grehdr) + 4 * sizeof(uint32_t); |
---|
704 | if (m->m_pkthdr.len < hlen) |
---|
705 | goto drop; |
---|
706 | if (m->m_len < hlen) { |
---|
707 | m = m_pullup(m, hlen); |
---|
708 | if (m == NULL) |
---|
709 | goto drop; |
---|
710 | } |
---|
711 | gh = (struct grehdr *)mtodo(m, *offp); |
---|
712 | flags = ntohs(gh->gre_flags); |
---|
713 | if (flags & ~GRE_FLAGS_MASK) |
---|
714 | goto drop; |
---|
715 | opts = gh->gre_opts; |
---|
716 | hlen = 2 * sizeof(uint16_t); |
---|
717 | if (flags & GRE_FLAGS_CP) { |
---|
718 | /* reserved1 field must be zero */ |
---|
719 | if (((uint16_t *)opts)[1] != 0) |
---|
720 | goto drop; |
---|
721 | if (in_cksum_skip(m, m->m_pkthdr.len, *offp) != 0) |
---|
722 | goto drop; |
---|
723 | hlen += 2 * sizeof(uint16_t); |
---|
724 | opts++; |
---|
725 | } |
---|
726 | if (flags & GRE_FLAGS_KP) { |
---|
727 | #ifdef notyet |
---|
728 | /* |
---|
729 | * XXX: The current implementation uses the key only for outgoing |
---|
730 | * packets. But we can check the key value here, or even in the |
---|
731 | * encapcheck function. |
---|
732 | */ |
---|
733 | key = ntohl(*opts); |
---|
734 | #endif |
---|
735 | hlen += sizeof(uint32_t); |
---|
736 | opts++; |
---|
737 | } |
---|
738 | #ifdef notyet |
---|
739 | } else |
---|
740 | key = 0; |
---|
741 | |
---|
742 | if (sc->gre_key != 0 && (key != sc->gre_key || key != 0)) |
---|
743 | goto drop; |
---|
744 | #endif |
---|
745 | if (flags & GRE_FLAGS_SP) { |
---|
746 | #ifdef notyet |
---|
747 | seq = ntohl(*opts); |
---|
748 | #endif |
---|
749 | hlen += sizeof(uint32_t); |
---|
750 | } |
---|
751 | switch (ntohs(gh->gre_proto)) { |
---|
752 | case ETHERTYPE_WCCP: |
---|
753 | /* |
---|
754 | * For WCCP skip an additional 4 bytes if after GRE header |
---|
755 | * doesn't follow an IP header. |
---|
756 | */ |
---|
757 | if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40) |
---|
758 | hlen += sizeof(uint32_t); |
---|
759 | /* FALLTHROUGH */ |
---|
760 | case ETHERTYPE_IP: |
---|
761 | isr = NETISR_IP; |
---|
762 | af = AF_INET; |
---|
763 | break; |
---|
764 | case ETHERTYPE_IPV6: |
---|
765 | isr = NETISR_IPV6; |
---|
766 | af = AF_INET6; |
---|
767 | break; |
---|
768 | default: |
---|
769 | goto drop; |
---|
770 | } |
---|
771 | m_adj(m, *offp + hlen); |
---|
772 | m_clrprotoflags(m); |
---|
773 | m->m_pkthdr.rcvif = ifp; |
---|
774 | M_SETFIB(m, ifp->if_fib); |
---|
775 | #ifdef MAC |
---|
776 | mac_ifnet_create_mbuf(ifp, m); |
---|
777 | #endif |
---|
778 | BPF_MTAP2(ifp, &af, sizeof(af), m); |
---|
779 | if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); |
---|
780 | if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); |
---|
781 | if ((ifp->if_flags & IFF_MONITOR) != 0) |
---|
782 | m_freem(m); |
---|
783 | else |
---|
784 | netisr_dispatch(isr, m); |
---|
785 | return (IPPROTO_DONE); |
---|
786 | drop: |
---|
787 | if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); |
---|
788 | m_freem(m); |
---|
789 | return (IPPROTO_DONE); |
---|
790 | } |
---|
791 | |
---|
792 | #define MTAG_GRE 1307983903 |
---|
793 | static int |
---|
794 | gre_check_nesting(struct ifnet *ifp, struct mbuf *m) |
---|
795 | { |
---|
796 | struct m_tag *mtag; |
---|
797 | int count; |
---|
798 | |
---|
799 | count = 1; |
---|
800 | mtag = NULL; |
---|
801 | while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) { |
---|
802 | if (*(struct ifnet **)(mtag + 1) == ifp) { |
---|
803 | log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname); |
---|
804 | return (EIO); |
---|
805 | } |
---|
806 | count++; |
---|
807 | } |
---|
808 | if (count > V_max_gre_nesting) { |
---|
809 | log(LOG_NOTICE, |
---|
810 | "%s: if_output recursively called too many times(%d)\n", |
---|
811 | ifp->if_xname, count); |
---|
812 | return (EIO); |
---|
813 | } |
---|
814 | mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT); |
---|
815 | if (mtag == NULL) |
---|
816 | return (ENOMEM); |
---|
817 | *(struct ifnet **)(mtag + 1) = ifp; |
---|
818 | m_tag_prepend(m, mtag); |
---|
819 | return (0); |
---|
820 | } |
---|
821 | |
---|
822 | static int |
---|
823 | gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, |
---|
824 | struct route *ro) |
---|
825 | { |
---|
826 | uint32_t af; |
---|
827 | int error; |
---|
828 | |
---|
829 | #ifdef MAC |
---|
830 | error = mac_ifnet_check_transmit(ifp, m); |
---|
831 | if (error != 0) |
---|
832 | goto drop; |
---|
833 | #endif |
---|
834 | if ((ifp->if_flags & IFF_MONITOR) != 0 || |
---|
835 | (ifp->if_flags & IFF_UP) == 0) { |
---|
836 | error = ENETDOWN; |
---|
837 | goto drop; |
---|
838 | } |
---|
839 | |
---|
840 | error = gre_check_nesting(ifp, m); |
---|
841 | if (error != 0) |
---|
842 | goto drop; |
---|
843 | |
---|
844 | m->m_flags &= ~(M_BCAST|M_MCAST); |
---|
845 | if (dst->sa_family == AF_UNSPEC) |
---|
846 | bcopy(dst->sa_data, &af, sizeof(af)); |
---|
847 | else |
---|
848 | af = dst->sa_family; |
---|
849 | BPF_MTAP2(ifp, &af, sizeof(af), m); |
---|
850 | m->m_pkthdr.csum_data = af; /* save af for if_transmit */ |
---|
851 | return (ifp->if_transmit(ifp, m)); |
---|
852 | drop: |
---|
853 | m_freem(m); |
---|
854 | if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); |
---|
855 | return (error); |
---|
856 | } |
---|
857 | |
---|
858 | static void |
---|
859 | gre_setseqn(struct grehdr *gh, uint32_t seq) |
---|
860 | { |
---|
861 | uint32_t *opts; |
---|
862 | uint16_t flags; |
---|
863 | |
---|
864 | opts = gh->gre_opts; |
---|
865 | flags = ntohs(gh->gre_flags); |
---|
866 | KASSERT((flags & GRE_FLAGS_SP) != 0, |
---|
867 | ("gre_setseqn called, but GRE_FLAGS_SP isn't set ")); |
---|
868 | if (flags & GRE_FLAGS_CP) |
---|
869 | opts++; |
---|
870 | if (flags & GRE_FLAGS_KP) |
---|
871 | opts++; |
---|
872 | *opts = htonl(seq); |
---|
873 | } |
---|
874 | |
---|
875 | static int |
---|
876 | gre_transmit(struct ifnet *ifp, struct mbuf *m) |
---|
877 | { |
---|
878 | GRE_RLOCK_TRACKER; |
---|
879 | struct gre_softc *sc; |
---|
880 | struct grehdr *gh; |
---|
881 | uint32_t iaf, oaf, oseq; |
---|
882 | int error, hlen, olen, plen; |
---|
883 | int want_seq, want_csum; |
---|
884 | |
---|
885 | plen = 0; |
---|
886 | sc = ifp->if_softc; |
---|
887 | if (sc == NULL) { |
---|
888 | error = ENETDOWN; |
---|
889 | m_freem(m); |
---|
890 | goto drop; |
---|
891 | } |
---|
892 | GRE_RLOCK(sc); |
---|
893 | if (sc->gre_family == 0) { |
---|
894 | GRE_RUNLOCK(sc); |
---|
895 | error = ENETDOWN; |
---|
896 | m_freem(m); |
---|
897 | goto drop; |
---|
898 | } |
---|
899 | iaf = m->m_pkthdr.csum_data; |
---|
900 | oaf = sc->gre_family; |
---|
901 | hlen = sc->gre_hlen; |
---|
902 | want_seq = (sc->gre_options & GRE_ENABLE_SEQ) != 0; |
---|
903 | if (want_seq) |
---|
904 | oseq = sc->gre_oseq++; /* XXX */ |
---|
905 | else |
---|
906 | oseq = 0; /* Make compiler happy. */ |
---|
907 | want_csum = (sc->gre_options & GRE_ENABLE_CSUM) != 0; |
---|
908 | M_SETFIB(m, sc->gre_fibnum); |
---|
909 | M_PREPEND(m, hlen, M_NOWAIT); |
---|
910 | if (m == NULL) { |
---|
911 | GRE_RUNLOCK(sc); |
---|
912 | error = ENOBUFS; |
---|
913 | goto drop; |
---|
914 | } |
---|
915 | bcopy(sc->gre_hdr, mtod(m, void *), hlen); |
---|
916 | GRE_RUNLOCK(sc); |
---|
917 | switch (oaf) { |
---|
918 | #ifdef INET |
---|
919 | case AF_INET: |
---|
920 | olen = sizeof(struct ip); |
---|
921 | break; |
---|
922 | #endif |
---|
923 | #ifdef INET6 |
---|
924 | case AF_INET6: |
---|
925 | olen = sizeof(struct ip6_hdr); |
---|
926 | break; |
---|
927 | #endif |
---|
928 | default: |
---|
929 | error = ENETDOWN; |
---|
930 | goto drop; |
---|
931 | } |
---|
932 | gh = (struct grehdr *)mtodo(m, olen); |
---|
933 | switch (iaf) { |
---|
934 | #ifdef INET |
---|
935 | case AF_INET: |
---|
936 | gh->gre_proto = htons(ETHERTYPE_IP); |
---|
937 | break; |
---|
938 | #endif |
---|
939 | #ifdef INET6 |
---|
940 | case AF_INET6: |
---|
941 | gh->gre_proto = htons(ETHERTYPE_IPV6); |
---|
942 | break; |
---|
943 | #endif |
---|
944 | default: |
---|
945 | error = ENETDOWN; |
---|
946 | goto drop; |
---|
947 | } |
---|
948 | if (want_seq) |
---|
949 | gre_setseqn(gh, oseq); |
---|
950 | if (want_csum) { |
---|
951 | *(uint16_t *)gh->gre_opts = in_cksum_skip(m, |
---|
952 | m->m_pkthdr.len, olen); |
---|
953 | } |
---|
954 | plen = m->m_pkthdr.len - hlen; |
---|
955 | switch (oaf) { |
---|
956 | #ifdef INET |
---|
957 | case AF_INET: |
---|
958 | error = in_gre_output(m, iaf, hlen); |
---|
959 | break; |
---|
960 | #endif |
---|
961 | #ifdef INET6 |
---|
962 | case AF_INET6: |
---|
963 | error = in6_gre_output(m, iaf, hlen); |
---|
964 | break; |
---|
965 | #endif |
---|
966 | default: |
---|
967 | m_freem(m); |
---|
968 | error = ENETDOWN; |
---|
969 | } |
---|
970 | drop: |
---|
971 | if (error) |
---|
972 | if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); |
---|
973 | else { |
---|
974 | if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); |
---|
975 | if_inc_counter(ifp, IFCOUNTER_OBYTES, plen); |
---|
976 | } |
---|
977 | return (error); |
---|
978 | } |
---|
979 | |
---|
980 | static void |
---|
981 | gre_qflush(struct ifnet *ifp __unused) |
---|
982 | { |
---|
983 | |
---|
984 | } |
---|
985 | |
---|
986 | static int |
---|
987 | gremodevent(module_t mod, int type, void *data) |
---|
988 | { |
---|
989 | |
---|
990 | switch (type) { |
---|
991 | case MOD_LOAD: |
---|
992 | case MOD_UNLOAD: |
---|
993 | break; |
---|
994 | default: |
---|
995 | return (EOPNOTSUPP); |
---|
996 | } |
---|
997 | return (0); |
---|
998 | } |
---|
999 | |
---|
1000 | static moduledata_t gre_mod = { |
---|
1001 | "if_gre", |
---|
1002 | gremodevent, |
---|
1003 | 0 |
---|
1004 | }; |
---|
1005 | |
---|
1006 | DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); |
---|
1007 | MODULE_VERSION(if_gre, 1); |
---|