1 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.15 2002/11/08 16:57:13 ume Exp $ */
2 /* $DragonFly: src/sys/net/gif/if_gif.c,v 1.2 2003/06/17 04:28:48 dillon Exp $ */
3 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/errno.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/protosw.h>
50 #include <machine/bus.h> /* XXX: Shouldn't really be required! */
52 #include <machine/cpu.h>
55 #include <net/if_types.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
64 #include <netinet/in_var.h>
65 #include <netinet/in_gif.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ipprotosw.h>
72 #include <netinet/in.h>
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_gif.h>
78 #include <netinet6/ip6protosw.h>
81 #include <netinet/ip_encap.h>
82 #include <net/if_gif.h>
84 #include <net/net_osdep.h>
87 #define GIFDEV "if_gif"
88 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
90 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
91 static struct rman gifunits[1];
92 LIST_HEAD(, gif_softc) gif_softc_list;
94 int gif_clone_create __P((struct if_clone *, int *));
95 void gif_clone_destroy __P((struct ifnet *));
97 struct if_clone gif_cloner =
98 IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
100 static int gifmodevent __P((module_t, int, void *));
102 SYSCTL_DECL(_net_link);
103 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
104 "Generic Tunnel Interface");
107 * This macro controls the default upper limitation on nesting of gif tunnels.
108 * Since, setting a large value to this macro with a careless configuration
109 * may introduce system crash, we don't allow any nestings by default.
110 * If you need to configure nested gif tunnels, you can define this macro
111 * in your kernel configuration file. However, if you do so, please be
112 * careful to configure the tunnels so that it won't make a loop.
114 #define MAX_GIF_NEST 1
116 static int max_gif_nesting = MAX_GIF_NEST;
117 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
118 &max_gif_nesting, 0, "Max nested tunnels");
121 * By default, we disallow creation of multiple tunnels between the same
122 * pair of addresses. Some applications require this functionality so
123 * we allow control over this check here.
126 static int parallel_tunnels = 1;
128 static int parallel_tunnels = 0;
130 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
131 ¶llel_tunnels, 0, "Allow parallel tunnels?");
134 gif_clone_create(ifc, unit)
135 struct if_clone *ifc;
139 struct gif_softc *sc;
141 if (*unit > GIF_MAXUNIT)
145 r = rman_reserve_resource(gifunits, 0, GIF_MAXUNIT, 1,
146 RF_ALLOCATED | RF_ACTIVE, NULL);
149 *unit = rman_get_start(r);
151 r = rman_reserve_resource(gifunits, *unit, *unit, 1,
152 RF_ALLOCATED | RF_ACTIVE, NULL);
157 sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
158 bzero(sc, sizeof(struct gif_softc));
160 sc->gif_if.if_softc = sc;
161 sc->gif_if.if_name = GIFNAME;
162 sc->gif_if.if_unit = *unit;
167 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
173 struct gif_softc *sc;
176 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
178 sc->gif_if.if_addrlen = 0;
179 sc->gif_if.if_mtu = GIF_MTU;
180 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
182 /* turn off ingress filter */
183 sc->gif_if.if_flags |= IFF_LINK2;
185 sc->gif_if.if_ioctl = gif_ioctl;
186 sc->gif_if.if_output = gif_output;
187 sc->gif_if.if_type = IFT_GIF;
188 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
189 if_attach(&sc->gif_if);
190 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
194 gif_clone_destroy(ifp)
198 struct gif_softc *sc = ifp->if_softc;
200 gif_delete_tunnel(&sc->gif_if);
201 LIST_REMOVE(sc, gif_list);
203 if (sc->encap_cookie6 != NULL) {
204 err = encap_detach(sc->encap_cookie6);
205 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
209 if (sc->encap_cookie4 != NULL) {
210 err = encap_detach(sc->encap_cookie4);
211 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
218 err = rman_release_resource(sc->r_unit);
219 KASSERT(err == 0, ("Unexpected error freeing resource"));
225 gifmodevent(mod, type, data)
234 gifunits->rm_type = RMAN_ARRAY;
235 gifunits->rm_descr = "configurable if_gif units";
236 err = rman_init(gifunits);
239 err = rman_manage_region(gifunits, 0, GIF_MAXUNIT);
241 printf("%s: gifunits: rman_manage_region: Failed %d\n",
246 LIST_INIT(&gif_softc_list);
247 if_clone_attach(&gif_cloner);
250 ip6_gif_hlim = GIF_HLIM;
255 if_clone_detach(&gif_cloner);
257 while (!LIST_EMPTY(&gif_softc_list))
258 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
260 err = rman_fini(gifunits);
271 static moduledata_t gif_mod = {
277 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
280 gif_encapcheck(m, off, proto, arg)
281 const struct mbuf *m;
287 struct gif_softc *sc;
289 sc = (struct gif_softc *)arg;
293 if ((sc->gif_if.if_flags & IFF_UP) == 0)
296 /* no physical address */
297 if (!sc->gif_psrc || !sc->gif_pdst)
313 /* Bail on short packets */
314 if (m->m_pkthdr.len < sizeof(ip))
317 /* LINTED const cast */
318 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
323 if (sc->gif_psrc->sa_family != AF_INET ||
324 sc->gif_pdst->sa_family != AF_INET)
326 return gif_encapcheck4(m, off, proto, arg);
330 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
332 if (sc->gif_psrc->sa_family != AF_INET6 ||
333 sc->gif_pdst->sa_family != AF_INET6)
335 return gif_encapcheck6(m, off, proto, arg);
343 gif_output(ifp, m, dst, rt)
346 struct sockaddr *dst;
347 struct rtentry *rt; /* added in net2 */
349 struct gif_softc *sc = (struct gif_softc*)ifp;
351 static int called = 0; /* XXX: MUTEX */
354 * gif may cause infinite recursion calls when misconfigured.
355 * We'll prevent this by introducing upper limit.
356 * XXX: this mechanism may introduce another problem about
357 * mutual exclusion of the variable CALLED, especially if we
360 if (++called > max_gif_nesting) {
362 "gif_output: recursively called too many times(%d)\n",
365 error = EIO; /* is there better errno? */
369 m->m_flags &= ~(M_BCAST|M_MCAST);
370 if (!(ifp->if_flags & IFF_UP) ||
371 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
379 * We need to prepend the address family as
380 * a four byte field. Cons up a dummy header
381 * to pacify bpf. This is safe because bpf
382 * will only read from the mbuf (i.e., it won't
383 * try to free it or keep a pointer a to it).
386 u_int32_t af = dst->sa_family;
390 m0.m_data = (char *)⁡
395 ifp->if_obytes += m->m_pkthdr.len;
397 /* inner AF-specific encapsulation */
399 /* XXX should we check if our outer source is legal? */
401 /* dispatch to output logic based on outer AF */
402 switch (sc->gif_psrc->sa_family) {
405 error = in_gif_output(ifp, dst->sa_family, m);
410 error = in6_gif_output(ifp, dst->sa_family, m);
420 called = 0; /* reset recursion counter */
427 gif_input(m, af, ifp)
433 struct ifqueue *ifq = NULL;
441 m->m_pkthdr.rcvif = ifp;
445 * We need to prepend the address family as
446 * a four byte field. Cons up a dummy header
447 * to pacify bpf. This is safe because bpf
448 * will only read from the mbuf (i.e., it won't
449 * try to free it or keep a pointer a to it).
456 m0.m_data = (char *)&af1;
462 * Put the packet to the network layer input queue according to the
463 * specified address family.
464 * Note: older versions of gif_input directly called network layer
465 * input functions, e.g. ip6_input, here. We changed the policy to
466 * prevent too many recursive calls of such input functions, which
467 * might cause kernel panic. But the change may introduce another
468 * problem; if the input queue is full, packets are discarded.
469 * The kernel stack overflow really happened, and we believed
470 * queue-full rarely occurs, so we changed the policy.
492 IF_DROP(ifq); /* update statistics */
498 ifp->if_ibytes += m->m_pkthdr.len;
500 /* we need schednetisr since the address family may change */
507 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
509 gif_ioctl(ifp, cmd, data)
514 struct gif_softc *sc = (struct gif_softc*)ifp;
515 struct ifreq *ifr = (struct ifreq*)data;
517 struct sockaddr *dst, *src;
518 #ifdef SIOCSIFMTU /* xxx */
524 ifp->if_flags |= IFF_UP;
534 #ifdef SIOCSIFMTU /* xxx */
540 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
544 #endif /* SIOCSIFMTU */
550 case SIOCSIFPHYADDR_IN6:
552 case SIOCSLIFPHYADDR:
556 src = (struct sockaddr *)
557 &(((struct in_aliasreq *)data)->ifra_addr);
558 dst = (struct sockaddr *)
559 &(((struct in_aliasreq *)data)->ifra_dstaddr);
563 case SIOCSIFPHYADDR_IN6:
564 src = (struct sockaddr *)
565 &(((struct in6_aliasreq *)data)->ifra_addr);
566 dst = (struct sockaddr *)
567 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
570 case SIOCSLIFPHYADDR:
571 src = (struct sockaddr *)
572 &(((struct if_laddrreq *)data)->addr);
573 dst = (struct sockaddr *)
574 &(((struct if_laddrreq *)data)->dstaddr);
580 /* sa_family must be equal */
581 if (src->sa_family != dst->sa_family)
584 /* validate sa_len */
585 switch (src->sa_family) {
588 if (src->sa_len != sizeof(struct sockaddr_in))
594 if (src->sa_len != sizeof(struct sockaddr_in6))
601 switch (dst->sa_family) {
604 if (dst->sa_len != sizeof(struct sockaddr_in))
610 if (dst->sa_len != sizeof(struct sockaddr_in6))
618 /* check sa_family looks sane for the cmd */
621 if (src->sa_family == AF_INET)
625 case SIOCSIFPHYADDR_IN6:
626 if (src->sa_family == AF_INET6)
630 case SIOCSLIFPHYADDR:
631 /* checks done in the above */
635 error = gif_set_tunnel(&sc->gif_if, src, dst);
638 #ifdef SIOCDIFPHYADDR
640 gif_delete_tunnel(&sc->gif_if);
644 case SIOCGIFPSRCADDR:
646 case SIOCGIFPSRCADDR_IN6:
648 if (sc->gif_psrc == NULL) {
649 error = EADDRNOTAVAIL;
655 case SIOCGIFPSRCADDR:
656 dst = &ifr->ifr_addr;
657 size = sizeof(ifr->ifr_addr);
661 case SIOCGIFPSRCADDR_IN6:
662 dst = (struct sockaddr *)
663 &(((struct in6_ifreq *)data)->ifr_addr);
664 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
668 error = EADDRNOTAVAIL;
671 if (src->sa_len > size)
673 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
676 case SIOCGIFPDSTADDR:
678 case SIOCGIFPDSTADDR_IN6:
680 if (sc->gif_pdst == NULL) {
681 error = EADDRNOTAVAIL;
687 case SIOCGIFPDSTADDR:
688 dst = &ifr->ifr_addr;
689 size = sizeof(ifr->ifr_addr);
693 case SIOCGIFPDSTADDR_IN6:
694 dst = (struct sockaddr *)
695 &(((struct in6_ifreq *)data)->ifr_addr);
696 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
700 error = EADDRNOTAVAIL;
703 if (src->sa_len > size)
705 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
708 case SIOCGLIFPHYADDR:
709 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
710 error = EADDRNOTAVAIL;
716 dst = (struct sockaddr *)
717 &(((struct if_laddrreq *)data)->addr);
718 size = sizeof(((struct if_laddrreq *)data)->addr);
719 if (src->sa_len > size)
721 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
725 dst = (struct sockaddr *)
726 &(((struct if_laddrreq *)data)->dstaddr);
727 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
728 if (src->sa_len > size)
730 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
734 /* if_ioctl() takes care of it */
746 gif_set_tunnel(ifp, src, dst)
748 struct sockaddr *src;
749 struct sockaddr *dst;
751 struct gif_softc *sc = (struct gif_softc *)ifp;
752 struct gif_softc *sc2;
753 struct sockaddr *osrc, *odst, *sa;
759 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
762 if (!sc2->gif_pdst || !sc2->gif_psrc)
764 if (sc2->gif_pdst->sa_family != dst->sa_family ||
765 sc2->gif_pdst->sa_len != dst->sa_len ||
766 sc2->gif_psrc->sa_family != src->sa_family ||
767 sc2->gif_psrc->sa_len != src->sa_len)
771 * Disallow parallel tunnels unless instructed
774 if (!parallel_tunnels &&
775 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
776 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
777 error = EADDRNOTAVAIL;
781 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
784 /* XXX we can detach from both, but be polite just in case */
786 switch (sc->gif_psrc->sa_family) {
789 (void)in_gif_detach(sc);
794 (void)in6_gif_detach(sc);
800 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
801 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
805 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
806 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
809 switch (sc->gif_psrc->sa_family) {
812 error = in_gif_attach(sc);
817 error = in6_gif_attach(sc);
823 free((caddr_t)sc->gif_psrc, M_IFADDR);
824 free((caddr_t)sc->gif_pdst, M_IFADDR);
831 free((caddr_t)osrc, M_IFADDR);
833 free((caddr_t)odst, M_IFADDR);
835 if (sc->gif_psrc && sc->gif_pdst)
836 ifp->if_flags |= IFF_RUNNING;
838 ifp->if_flags &= ~IFF_RUNNING;
844 if (sc->gif_psrc && sc->gif_pdst)
845 ifp->if_flags |= IFF_RUNNING;
847 ifp->if_flags &= ~IFF_RUNNING;
854 gif_delete_tunnel(ifp)
857 struct gif_softc *sc = (struct gif_softc *)ifp;
863 free((caddr_t)sc->gif_psrc, M_IFADDR);
867 free((caddr_t)sc->gif_pdst, M_IFADDR);
870 /* it is safe to detach from both */
872 (void)in_gif_detach(sc);
875 (void)in6_gif_detach(sc);
878 if (sc->gif_psrc && sc->gif_pdst)
879 ifp->if_flags |= IFF_RUNNING;
881 ifp->if_flags &= ~IFF_RUNNING;