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.9 2004/03/23 22:19:06 hsu 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! */
51 #include <machine/cpu.h>
54 #include <net/if_types.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_gif.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ipprotosw.h>
71 #include <netinet/in.h>
73 #include <netinet6/in6_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/in6_gif.h>
77 #include <netinet6/ip6protosw.h>
80 #include <netinet/ip_encap.h>
83 #include <net/net_osdep.h>
87 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
88 LIST_HEAD(, gif_softc) gif_softc_list;
90 int gif_clone_create (struct if_clone *, int);
91 void gif_clone_destroy (struct ifnet *);
93 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create,
94 gif_clone_destroy, 0, IF_MAXUNIT);
96 static int gifmodevent (module_t, int, void *);
98 SYSCTL_DECL(_net_link);
99 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
100 "Generic Tunnel Interface");
103 * This macro controls the default upper limitation on nesting of gif tunnels.
104 * Since, setting a large value to this macro with a careless configuration
105 * may introduce system crash, we don't allow any nestings by default.
106 * If you need to configure nested gif tunnels, you can define this macro
107 * in your kernel configuration file. However, if you do so, please be
108 * careful to configure the tunnels so that it won't make a loop.
110 #define MAX_GIF_NEST 1
112 static int max_gif_nesting = MAX_GIF_NEST;
113 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
114 &max_gif_nesting, 0, "Max nested tunnels");
117 * By default, we disallow creation of multiple tunnels between the same
118 * pair of addresses. Some applications require this functionality so
119 * we allow control over this check here.
122 static int parallel_tunnels = 1;
124 static int parallel_tunnels = 0;
126 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
127 ¶llel_tunnels, 0, "Allow parallel tunnels?");
130 gif_clone_create(ifc, unit)
131 struct if_clone *ifc;
134 struct gif_softc *sc;
136 sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
137 bzero(sc, sizeof(struct gif_softc));
139 sc->gif_if.if_softc = sc;
140 if_initname(&(sc->gif_if), GIFNAME, unit);
144 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
150 struct gif_softc *sc;
153 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
155 sc->gif_if.if_addrlen = 0;
156 sc->gif_if.if_mtu = GIF_MTU;
157 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
159 /* turn off ingress filter */
160 sc->gif_if.if_flags |= IFF_LINK2;
162 sc->gif_if.if_ioctl = gif_ioctl;
163 sc->gif_if.if_output = gif_output;
164 sc->gif_if.if_type = IFT_GIF;
165 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
166 if_attach(&sc->gif_if);
167 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
171 gif_clone_destroy(ifp)
175 struct gif_softc *sc = ifp->if_softc;
177 gif_delete_tunnel(&sc->gif_if);
178 LIST_REMOVE(sc, gif_list);
180 if (sc->encap_cookie6 != NULL) {
181 err = encap_detach(sc->encap_cookie6);
182 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
186 if (sc->encap_cookie4 != NULL) {
187 err = encap_detach(sc->encap_cookie4);
188 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
199 gifmodevent(mod, type, data)
207 LIST_INIT(&gif_softc_list);
208 if_clone_attach(&gif_cloner);
211 ip6_gif_hlim = GIF_HLIM;
216 if_clone_detach(&gif_cloner);
218 while (!LIST_EMPTY(&gif_softc_list))
219 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
229 static moduledata_t gif_mod = {
235 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
238 gif_encapcheck(m, off, proto, arg)
239 const struct mbuf *m;
245 struct gif_softc *sc;
247 sc = (struct gif_softc *)arg;
251 if ((sc->gif_if.if_flags & IFF_UP) == 0)
254 /* no physical address */
255 if (!sc->gif_psrc || !sc->gif_pdst)
271 /* Bail on short packets */
272 if (m->m_pkthdr.len < sizeof(ip))
275 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
280 if (sc->gif_psrc->sa_family != AF_INET ||
281 sc->gif_pdst->sa_family != AF_INET)
283 return gif_encapcheck4(m, off, proto, arg);
287 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
289 if (sc->gif_psrc->sa_family != AF_INET6 ||
290 sc->gif_pdst->sa_family != AF_INET6)
292 return gif_encapcheck6(m, off, proto, arg);
300 gif_output(ifp, m, dst, rt)
303 struct sockaddr *dst;
304 struct rtentry *rt; /* added in net2 */
306 struct gif_softc *sc = (struct gif_softc*)ifp;
308 static int called = 0; /* XXX: MUTEX */
311 * gif may cause infinite recursion calls when misconfigured.
312 * We'll prevent this by introducing upper limit.
313 * XXX: this mechanism may introduce another problem about
314 * mutual exclusion of the variable CALLED, especially if we
317 if (++called > max_gif_nesting) {
319 "gif_output: recursively called too many times(%d)\n",
322 error = EIO; /* is there better errno? */
326 m->m_flags &= ~(M_BCAST|M_MCAST);
327 if (!(ifp->if_flags & IFF_UP) ||
328 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
336 * We need to prepend the address family as
337 * a four byte field. Cons up a dummy header
338 * to pacify bpf. This is safe because bpf
339 * will only read from the mbuf (i.e., it won't
340 * try to free it or keep a pointer a to it).
343 u_int32_t af = dst->sa_family;
347 m0.m_data = (char *)⁡
352 ifp->if_obytes += m->m_pkthdr.len;
354 /* inner AF-specific encapsulation */
356 /* XXX should we check if our outer source is legal? */
358 /* dispatch to output logic based on outer AF */
359 switch (sc->gif_psrc->sa_family) {
362 error = in_gif_output(ifp, dst->sa_family, m);
367 error = in6_gif_output(ifp, dst->sa_family, m);
377 called = 0; /* reset recursion counter */
384 gif_input(m, af, ifp)
397 m->m_pkthdr.rcvif = ifp;
401 * We need to prepend the address family as
402 * a four byte field. Cons up a dummy header
403 * to pacify bpf. This is safe because bpf
404 * will only read from the mbuf (i.e., it won't
405 * try to free it or keep a pointer a to it).
412 m0.m_data = (char *)&af1;
418 * Put the packet to the network layer input queue according to the
419 * specified address family.
420 * Note: older versions of gif_input directly called network layer
421 * input functions, e.g. ip6_input, here. We changed the policy to
422 * prevent too many recursive calls of such input functions, which
423 * might cause kernel panic. But the change may introduce another
424 * problem; if the input queue is full, packets are discarded.
425 * The kernel stack overflow really happened, and we believed
426 * queue-full rarely occurs, so we changed the policy.
445 ifp->if_ibytes += m->m_pkthdr.len;
446 netisr_dispatch(isr, m);
451 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
453 gif_ioctl(ifp, cmd, data, cr)
459 struct gif_softc *sc = (struct gif_softc*)ifp;
460 struct ifreq *ifr = (struct ifreq*)data;
462 struct sockaddr *dst, *src;
463 #ifdef SIOCSIFMTU /* xxx */
469 ifp->if_flags |= IFF_UP;
479 #ifdef SIOCSIFMTU /* xxx */
485 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
489 #endif /* SIOCSIFMTU */
495 case SIOCSIFPHYADDR_IN6:
497 case SIOCSLIFPHYADDR:
501 src = (struct sockaddr *)
502 &(((struct in_aliasreq *)data)->ifra_addr);
503 dst = (struct sockaddr *)
504 &(((struct in_aliasreq *)data)->ifra_dstaddr);
508 case SIOCSIFPHYADDR_IN6:
509 src = (struct sockaddr *)
510 &(((struct in6_aliasreq *)data)->ifra_addr);
511 dst = (struct sockaddr *)
512 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
515 case SIOCSLIFPHYADDR:
516 src = (struct sockaddr *)
517 &(((struct if_laddrreq *)data)->addr);
518 dst = (struct sockaddr *)
519 &(((struct if_laddrreq *)data)->dstaddr);
525 /* sa_family must be equal */
526 if (src->sa_family != dst->sa_family)
529 /* validate sa_len */
530 switch (src->sa_family) {
533 if (src->sa_len != sizeof(struct sockaddr_in))
539 if (src->sa_len != sizeof(struct sockaddr_in6))
546 switch (dst->sa_family) {
549 if (dst->sa_len != sizeof(struct sockaddr_in))
555 if (dst->sa_len != sizeof(struct sockaddr_in6))
563 /* check sa_family looks sane for the cmd */
566 if (src->sa_family == AF_INET)
570 case SIOCSIFPHYADDR_IN6:
571 if (src->sa_family == AF_INET6)
575 case SIOCSLIFPHYADDR:
576 /* checks done in the above */
580 error = gif_set_tunnel(&sc->gif_if, src, dst);
583 #ifdef SIOCDIFPHYADDR
585 gif_delete_tunnel(&sc->gif_if);
589 case SIOCGIFPSRCADDR:
591 case SIOCGIFPSRCADDR_IN6:
593 if (sc->gif_psrc == NULL) {
594 error = EADDRNOTAVAIL;
600 case SIOCGIFPSRCADDR:
601 dst = &ifr->ifr_addr;
602 size = sizeof(ifr->ifr_addr);
606 case SIOCGIFPSRCADDR_IN6:
607 dst = (struct sockaddr *)
608 &(((struct in6_ifreq *)data)->ifr_addr);
609 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
613 error = EADDRNOTAVAIL;
616 if (src->sa_len > size)
618 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
621 case SIOCGIFPDSTADDR:
623 case SIOCGIFPDSTADDR_IN6:
625 if (sc->gif_pdst == NULL) {
626 error = EADDRNOTAVAIL;
632 case SIOCGIFPDSTADDR:
633 dst = &ifr->ifr_addr;
634 size = sizeof(ifr->ifr_addr);
638 case SIOCGIFPDSTADDR_IN6:
639 dst = (struct sockaddr *)
640 &(((struct in6_ifreq *)data)->ifr_addr);
641 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
645 error = EADDRNOTAVAIL;
648 if (src->sa_len > size)
650 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
653 case SIOCGLIFPHYADDR:
654 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
655 error = EADDRNOTAVAIL;
661 dst = (struct sockaddr *)
662 &(((struct if_laddrreq *)data)->addr);
663 size = sizeof(((struct if_laddrreq *)data)->addr);
664 if (src->sa_len > size)
666 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
670 dst = (struct sockaddr *)
671 &(((struct if_laddrreq *)data)->dstaddr);
672 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
673 if (src->sa_len > size)
675 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
679 /* if_ioctl() takes care of it */
691 gif_set_tunnel(ifp, src, dst)
693 struct sockaddr *src;
694 struct sockaddr *dst;
696 struct gif_softc *sc = (struct gif_softc *)ifp;
697 struct gif_softc *sc2;
698 struct sockaddr *osrc, *odst, *sa;
704 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
707 if (!sc2->gif_pdst || !sc2->gif_psrc)
709 if (sc2->gif_pdst->sa_family != dst->sa_family ||
710 sc2->gif_pdst->sa_len != dst->sa_len ||
711 sc2->gif_psrc->sa_family != src->sa_family ||
712 sc2->gif_psrc->sa_len != src->sa_len)
716 * Disallow parallel tunnels unless instructed
719 if (!parallel_tunnels &&
720 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
721 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
722 error = EADDRNOTAVAIL;
726 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
729 /* XXX we can detach from both, but be polite just in case */
731 switch (sc->gif_psrc->sa_family) {
734 (void)in_gif_detach(sc);
739 (void)in6_gif_detach(sc);
745 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
746 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
750 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
751 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
754 switch (sc->gif_psrc->sa_family) {
757 error = in_gif_attach(sc);
762 error = in6_gif_attach(sc);
768 free((caddr_t)sc->gif_psrc, M_IFADDR);
769 free((caddr_t)sc->gif_pdst, M_IFADDR);
776 free((caddr_t)osrc, M_IFADDR);
778 free((caddr_t)odst, M_IFADDR);
780 if (sc->gif_psrc && sc->gif_pdst)
781 ifp->if_flags |= IFF_RUNNING;
783 ifp->if_flags &= ~IFF_RUNNING;
789 if (sc->gif_psrc && sc->gif_pdst)
790 ifp->if_flags |= IFF_RUNNING;
792 ifp->if_flags &= ~IFF_RUNNING;
799 gif_delete_tunnel(ifp)
802 struct gif_softc *sc = (struct gif_softc *)ifp;
808 free((caddr_t)sc->gif_psrc, M_IFADDR);
812 free((caddr_t)sc->gif_pdst, M_IFADDR);
815 /* it is safe to detach from both */
817 (void)in_gif_detach(sc);
820 (void)in6_gif_detach(sc);
823 if (sc->gif_psrc && sc->gif_pdst)
824 ifp->if_flags |= IFF_RUNNING;
826 ifp->if_flags &= ~IFF_RUNNING;