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.5 2003/08/26 20:49:48 rob 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>
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 (struct if_clone *, int *);
95 void gif_clone_destroy (struct ifnet *);
97 struct if_clone gif_cloner =
98 IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
100 static int gifmodevent (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 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
322 if (sc->gif_psrc->sa_family != AF_INET ||
323 sc->gif_pdst->sa_family != AF_INET)
325 return gif_encapcheck4(m, off, proto, arg);
329 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
331 if (sc->gif_psrc->sa_family != AF_INET6 ||
332 sc->gif_pdst->sa_family != AF_INET6)
334 return gif_encapcheck6(m, off, proto, arg);
342 gif_output(ifp, m, dst, rt)
345 struct sockaddr *dst;
346 struct rtentry *rt; /* added in net2 */
348 struct gif_softc *sc = (struct gif_softc*)ifp;
350 static int called = 0; /* XXX: MUTEX */
353 * gif may cause infinite recursion calls when misconfigured.
354 * We'll prevent this by introducing upper limit.
355 * XXX: this mechanism may introduce another problem about
356 * mutual exclusion of the variable CALLED, especially if we
359 if (++called > max_gif_nesting) {
361 "gif_output: recursively called too many times(%d)\n",
364 error = EIO; /* is there better errno? */
368 m->m_flags &= ~(M_BCAST|M_MCAST);
369 if (!(ifp->if_flags & IFF_UP) ||
370 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
378 * We need to prepend the address family as
379 * a four byte field. Cons up a dummy header
380 * to pacify bpf. This is safe because bpf
381 * will only read from the mbuf (i.e., it won't
382 * try to free it or keep a pointer a to it).
385 u_int32_t af = dst->sa_family;
389 m0.m_data = (char *)⁡
394 ifp->if_obytes += m->m_pkthdr.len;
396 /* inner AF-specific encapsulation */
398 /* XXX should we check if our outer source is legal? */
400 /* dispatch to output logic based on outer AF */
401 switch (sc->gif_psrc->sa_family) {
404 error = in_gif_output(ifp, dst->sa_family, m);
409 error = in6_gif_output(ifp, dst->sa_family, m);
419 called = 0; /* reset recursion counter */
426 gif_input(m, af, ifp)
432 struct ifqueue *ifq = NULL;
440 m->m_pkthdr.rcvif = ifp;
444 * We need to prepend the address family as
445 * a four byte field. Cons up a dummy header
446 * to pacify bpf. This is safe because bpf
447 * will only read from the mbuf (i.e., it won't
448 * try to free it or keep a pointer a to it).
455 m0.m_data = (char *)&af1;
461 * Put the packet to the network layer input queue according to the
462 * specified address family.
463 * Note: older versions of gif_input directly called network layer
464 * input functions, e.g. ip6_input, here. We changed the policy to
465 * prevent too many recursive calls of such input functions, which
466 * might cause kernel panic. But the change may introduce another
467 * problem; if the input queue is full, packets are discarded.
468 * The kernel stack overflow really happened, and we believed
469 * queue-full rarely occurs, so we changed the policy.
491 IF_DROP(ifq); /* update statistics */
497 ifp->if_ibytes += m->m_pkthdr.len;
499 /* we need schednetisr since the address family may change */
506 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
508 gif_ioctl(ifp, cmd, data)
513 struct gif_softc *sc = (struct gif_softc*)ifp;
514 struct ifreq *ifr = (struct ifreq*)data;
516 struct sockaddr *dst, *src;
517 #ifdef SIOCSIFMTU /* xxx */
523 ifp->if_flags |= IFF_UP;
533 #ifdef SIOCSIFMTU /* xxx */
539 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
543 #endif /* SIOCSIFMTU */
549 case SIOCSIFPHYADDR_IN6:
551 case SIOCSLIFPHYADDR:
555 src = (struct sockaddr *)
556 &(((struct in_aliasreq *)data)->ifra_addr);
557 dst = (struct sockaddr *)
558 &(((struct in_aliasreq *)data)->ifra_dstaddr);
562 case SIOCSIFPHYADDR_IN6:
563 src = (struct sockaddr *)
564 &(((struct in6_aliasreq *)data)->ifra_addr);
565 dst = (struct sockaddr *)
566 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
569 case SIOCSLIFPHYADDR:
570 src = (struct sockaddr *)
571 &(((struct if_laddrreq *)data)->addr);
572 dst = (struct sockaddr *)
573 &(((struct if_laddrreq *)data)->dstaddr);
579 /* sa_family must be equal */
580 if (src->sa_family != dst->sa_family)
583 /* validate sa_len */
584 switch (src->sa_family) {
587 if (src->sa_len != sizeof(struct sockaddr_in))
593 if (src->sa_len != sizeof(struct sockaddr_in6))
600 switch (dst->sa_family) {
603 if (dst->sa_len != sizeof(struct sockaddr_in))
609 if (dst->sa_len != sizeof(struct sockaddr_in6))
617 /* check sa_family looks sane for the cmd */
620 if (src->sa_family == AF_INET)
624 case SIOCSIFPHYADDR_IN6:
625 if (src->sa_family == AF_INET6)
629 case SIOCSLIFPHYADDR:
630 /* checks done in the above */
634 error = gif_set_tunnel(&sc->gif_if, src, dst);
637 #ifdef SIOCDIFPHYADDR
639 gif_delete_tunnel(&sc->gif_if);
643 case SIOCGIFPSRCADDR:
645 case SIOCGIFPSRCADDR_IN6:
647 if (sc->gif_psrc == NULL) {
648 error = EADDRNOTAVAIL;
654 case SIOCGIFPSRCADDR:
655 dst = &ifr->ifr_addr;
656 size = sizeof(ifr->ifr_addr);
660 case SIOCGIFPSRCADDR_IN6:
661 dst = (struct sockaddr *)
662 &(((struct in6_ifreq *)data)->ifr_addr);
663 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
667 error = EADDRNOTAVAIL;
670 if (src->sa_len > size)
672 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
675 case SIOCGIFPDSTADDR:
677 case SIOCGIFPDSTADDR_IN6:
679 if (sc->gif_pdst == NULL) {
680 error = EADDRNOTAVAIL;
686 case SIOCGIFPDSTADDR:
687 dst = &ifr->ifr_addr;
688 size = sizeof(ifr->ifr_addr);
692 case SIOCGIFPDSTADDR_IN6:
693 dst = (struct sockaddr *)
694 &(((struct in6_ifreq *)data)->ifr_addr);
695 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
699 error = EADDRNOTAVAIL;
702 if (src->sa_len > size)
704 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
707 case SIOCGLIFPHYADDR:
708 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
709 error = EADDRNOTAVAIL;
715 dst = (struct sockaddr *)
716 &(((struct if_laddrreq *)data)->addr);
717 size = sizeof(((struct if_laddrreq *)data)->addr);
718 if (src->sa_len > size)
720 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
724 dst = (struct sockaddr *)
725 &(((struct if_laddrreq *)data)->dstaddr);
726 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
727 if (src->sa_len > size)
729 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
733 /* if_ioctl() takes care of it */
745 gif_set_tunnel(ifp, src, dst)
747 struct sockaddr *src;
748 struct sockaddr *dst;
750 struct gif_softc *sc = (struct gif_softc *)ifp;
751 struct gif_softc *sc2;
752 struct sockaddr *osrc, *odst, *sa;
758 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
761 if (!sc2->gif_pdst || !sc2->gif_psrc)
763 if (sc2->gif_pdst->sa_family != dst->sa_family ||
764 sc2->gif_pdst->sa_len != dst->sa_len ||
765 sc2->gif_psrc->sa_family != src->sa_family ||
766 sc2->gif_psrc->sa_len != src->sa_len)
770 * Disallow parallel tunnels unless instructed
773 if (!parallel_tunnels &&
774 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
775 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
776 error = EADDRNOTAVAIL;
780 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
783 /* XXX we can detach from both, but be polite just in case */
785 switch (sc->gif_psrc->sa_family) {
788 (void)in_gif_detach(sc);
793 (void)in6_gif_detach(sc);
799 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
800 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
804 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
805 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
808 switch (sc->gif_psrc->sa_family) {
811 error = in_gif_attach(sc);
816 error = in6_gif_attach(sc);
822 free((caddr_t)sc->gif_psrc, M_IFADDR);
823 free((caddr_t)sc->gif_pdst, M_IFADDR);
830 free((caddr_t)osrc, M_IFADDR);
832 free((caddr_t)odst, M_IFADDR);
834 if (sc->gif_psrc && sc->gif_pdst)
835 ifp->if_flags |= IFF_RUNNING;
837 ifp->if_flags &= ~IFF_RUNNING;
843 if (sc->gif_psrc && sc->gif_pdst)
844 ifp->if_flags |= IFF_RUNNING;
846 ifp->if_flags &= ~IFF_RUNNING;
853 gif_delete_tunnel(ifp)
856 struct gif_softc *sc = (struct gif_softc *)ifp;
862 free((caddr_t)sc->gif_psrc, M_IFADDR);
866 free((caddr_t)sc->gif_pdst, M_IFADDR);
869 /* it is safe to detach from both */
871 (void)in_gif_detach(sc);
874 (void)in6_gif_detach(sc);
877 if (sc->gif_psrc && sc->gif_pdst)
878 ifp->if_flags |= IFF_RUNNING;
880 ifp->if_flags &= ~IFF_RUNNING;