2 /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/errno.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/protosw.h>
49 #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>
70 #include <netinet/in.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/in6_gif.h>
76 #include <netinet6/ip6protosw.h>
79 #include <netinet/ip_encap.h>
80 #include <net/if_gif.h>
82 #include <net/net_osdep.h>
85 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
87 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
88 static struct rman gifunits[1];
89 static LIST_HEAD(, gif_softc) gif_softc_list;
91 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
92 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
93 void (*ng_gif_attach_p)(struct ifnet *ifp);
94 void (*ng_gif_detach_p)(struct ifnet *ifp);
96 int gif_clone_create __P((struct if_clone *, int *));
97 int gif_clone_destroy __P((struct ifnet *));
99 struct if_clone gif_cloner =
100 IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
102 static int gifmodevent __P((module_t, int, void *));
103 void gif_delete_tunnel __P((struct gif_softc *));
104 static int gif_encapcheck __P((const struct mbuf *, int, int, void *));
107 extern struct domain inetdomain;
108 struct protosw in_gif_protosw =
109 { SOCK_RAW, &inetdomain, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
110 in_gif_input, (pr_output_t*)rip_output, 0, rip_ctloutput,
117 extern struct domain inet6domain;
118 struct ip6protosw in6_gif_protosw =
119 { SOCK_RAW, &inet6domain, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
120 in6_gif_input, rip6_output, 0, rip6_ctloutput,
127 SYSCTL_DECL(_net_link);
128 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
129 "Generic Tunnel Interface");
132 * This macro controls the default upper limitation on nesting of gif tunnels.
133 * Since, setting a large value to this macro with a careless configuration
134 * may introduce system crash, we don't allow any nestings by default.
135 * If you need to configure nested gif tunnels, you can define this macro
136 * in your kernel configuration file. However, if you do so, please be
137 * careful to configure the tunnels so that it won't make a loop.
139 #define MAX_GIF_NEST 1
141 static int max_gif_nesting = MAX_GIF_NEST;
142 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
143 &max_gif_nesting, 0, "Max nested tunnels");
146 * By default, we disallow creation of multiple tunnels between the same
147 * pair of addresses. Some applications require this functionality so
148 * we allow control over this check here.
151 static int parallel_tunnels = 1;
153 static int parallel_tunnels = 0;
155 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
156 ¶llel_tunnels, 0, "Allow parallel tunnels?");
159 gif_clone_create(ifc, unit)
160 struct if_clone *ifc;
164 struct gif_softc *sc;
166 if (*unit > GIF_MAXUNIT)
170 r = rman_reserve_resource(gifunits, 0, GIF_MAXUNIT, 1,
171 RF_ALLOCATED | RF_ACTIVE, NULL);
174 *unit = rman_get_start(r);
176 r = rman_reserve_resource(gifunits, *unit, *unit, 1,
177 RF_ALLOCATED | RF_ACTIVE, NULL);
182 sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
183 bzero(sc, sizeof(struct gif_softc));
185 sc->gif_if.if_softc = sc;
186 sc->gif_if.if_name = GIFNAME;
187 sc->gif_if.if_unit = *unit;
190 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
192 sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
193 gif_encapcheck, (struct protosw*)&in_gif_protosw, sc);
194 if (sc->encap_cookie4 == NULL) {
195 printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
197 return (EIO); /* XXX */
201 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
202 gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
203 if (sc->encap_cookie6 == NULL) {
204 if (sc->encap_cookie4) {
205 encap_detach(sc->encap_cookie4);
206 sc->encap_cookie4 = NULL;
208 printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
210 return (EIO); /* XXX */
214 sc->gif_if.if_mtu = GIF_MTU;
215 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
217 /* turn off ingress filter */
218 sc->gif_if.if_flags |= IFF_LINK2;
220 sc->gif_if.if_ioctl = gif_ioctl;
221 sc->gif_if.if_output = gif_output;
222 sc->gif_if.if_type = IFT_GIF;
223 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
224 if_attach(&sc->gif_if);
225 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
226 if (ng_gif_attach_p != NULL)
227 (*ng_gif_attach_p)(&sc->gif_if);
228 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_link);
233 gif_clone_destroy(ifp)
237 struct gif_softc *sc = ifp->if_softc;
239 gif_delete_tunnel(sc);
240 LIST_REMOVE(sc, gif_link);
241 if (sc->encap_cookie4 != NULL) {
242 err = encap_detach(sc->encap_cookie4);
243 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
245 if (sc->encap_cookie6 != NULL) {
246 err = encap_detach(sc->encap_cookie6);
247 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
250 if (ng_gif_detach_p != NULL)
251 (*ng_gif_detach_p)(ifp);
255 err = rman_release_resource(sc->r_unit);
256 KASSERT(err == 0, ("Unexpected error freeing resource"));
263 gifmodevent(mod, type, data)
272 gifunits->rm_type = RMAN_ARRAY;
273 gifunits->rm_descr = "configurable if_gif units";
274 err = rman_init(gifunits);
277 err = rman_manage_region(gifunits, 0, GIF_MAXUNIT);
279 printf("%s: gifunits: rman_manage_region: Failed %d\n",
284 LIST_INIT(&gif_softc_list);
285 if_clone_attach(&gif_cloner);
288 ip6_gif_hlim = GIF_HLIM;
293 if_clone_detach(&gif_cloner);
295 while (!LIST_EMPTY(&gif_softc_list))
296 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
298 err = rman_fini(gifunits);
309 static moduledata_t gif_mod = {
315 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
316 MODULE_VERSION(if_gif, 1);
319 gif_encapcheck(m, off, proto, arg)
320 const struct mbuf *m;
326 struct gif_softc *sc;
328 sc = (struct gif_softc *)arg;
332 if ((sc->gif_if.if_flags & IFF_UP) == 0)
335 /* no physical address */
336 if (!sc->gif_psrc || !sc->gif_pdst)
352 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
357 if (sc->gif_psrc->sa_family != AF_INET ||
358 sc->gif_pdst->sa_family != AF_INET)
360 return gif_encapcheck4(m, off, proto, arg);
364 if (sc->gif_psrc->sa_family != AF_INET6 ||
365 sc->gif_pdst->sa_family != AF_INET6)
367 return gif_encapcheck6(m, off, proto, arg);
375 gif_output(ifp, m, dst, rt)
378 struct sockaddr *dst;
379 struct rtentry *rt; /* added in net2 */
381 struct gif_softc *sc = (struct gif_softc*)ifp;
383 static int called = 0; /* XXX: MUTEX */
386 * gif may cause infinite recursion calls when misconfigured.
387 * We'll prevent this by introducing upper limit.
388 * XXX: this mechanism may introduce another problem about
389 * mutual exclusion of the variable CALLED, especially if we
392 if (++called > max_gif_nesting) {
394 "gif_output: recursively called too many times(%d)\n",
397 error = EIO; /* is there better errno? */
401 m->m_flags &= ~(M_BCAST|M_MCAST);
402 if (!(ifp->if_flags & IFF_UP) ||
403 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
411 * We need to prepend the address family as
412 * a four byte field. Cons up a dummy header
413 * to pacify bpf. This is safe because bpf
414 * will only read from the mbuf (i.e., it won't
415 * try to free it or keep a pointer a to it).
418 u_int32_t af = dst->sa_family;
422 m0.m_data = (char *)⁡
427 ifp->if_obytes += m->m_pkthdr.len;
429 /* inner AF-specific encapsulation */
431 /* XXX should we check if our outer source is legal? */
433 /* dispatch to output logic based on outer AF */
434 switch (sc->gif_psrc->sa_family) {
437 error = in_gif_output(ifp, dst->sa_family, m, rt);
442 error = in6_gif_output(ifp, dst->sa_family, m, rt);
452 called = 0; /* reset recursion counter */
459 gif_input(m, af, gifp)
465 struct ifqueue *ifq = 0;
473 m->m_pkthdr.rcvif = gifp;
477 * We need to prepend the address family as
478 * a four byte field. Cons up a dummy header
479 * to pacify bpf. This is safe because bpf
480 * will only read from the mbuf (i.e., it won't
481 * try to free it or keep a pointer a to it).
488 m0.m_data = (char *)&af1;
493 if (ng_gif_input_p != NULL) {
494 (*ng_gif_input_p)(gifp, &m, af);
500 * Put the packet to the network layer input queue according to the
501 * specified address family.
502 * Note: older versions of gif_input directly called network layer
503 * input functions, e.g. ip6_input, here. We changed the policy to
504 * prevent too many recursive calls of such input functions, which
505 * might cause kernel panic. But the change may introduce another
506 * problem; if the input queue is full, packets are discarded.
507 * We believed it rarely occurs and changed the policy. If we find
508 * it occurs more times than we thought, we may change the policy
525 if (ng_gif_input_orphan_p != NULL)
526 (*ng_gif_input_orphan_p)(gifp, m, af);
533 gifp->if_ibytes += m->m_pkthdr.len;
534 (void) IF_HANDOFF(ifq, m, NULL);
535 /* we need schednetisr since the address family may change */
541 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
543 gif_ioctl(ifp, cmd, data)
548 struct gif_softc *sc = (struct gif_softc*)ifp;
549 struct ifreq *ifr = (struct ifreq*)data;
551 struct sockaddr *dst, *src;
555 struct gif_softc *sc2;
568 #ifdef SIOCSIFMTU /* xxx */
576 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
582 #endif /* SIOCSIFMTU */
586 case SIOCSIFPHYADDR_IN6:
588 case SIOCSLIFPHYADDR:
592 src = (struct sockaddr *)
593 &(((struct in_aliasreq *)data)->ifra_addr);
594 dst = (struct sockaddr *)
595 &(((struct in_aliasreq *)data)->ifra_dstaddr);
599 case SIOCSIFPHYADDR_IN6:
600 src = (struct sockaddr *)
601 &(((struct in6_aliasreq *)data)->ifra_addr);
602 dst = (struct sockaddr *)
603 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
606 case SIOCSLIFPHYADDR:
607 src = (struct sockaddr *)
608 &(((struct if_laddrreq *)data)->addr);
609 dst = (struct sockaddr *)
610 &(((struct if_laddrreq *)data)->dstaddr);
612 error = EADDRNOTAVAIL;
616 /* sa_family must be equal */
617 if (src->sa_family != dst->sa_family)
620 /* validate sa_len */
621 switch (src->sa_family) {
624 if (src->sa_len != sizeof(struct sockaddr_in))
630 if (src->sa_len != sizeof(struct sockaddr_in6))
637 switch (dst->sa_family) {
640 if (dst->sa_len != sizeof(struct sockaddr_in))
646 if (dst->sa_len != sizeof(struct sockaddr_in6))
654 /* check sa_family looks sane for the cmd */
657 if (src->sa_family == AF_INET)
661 case SIOCSIFPHYADDR_IN6:
662 if (src->sa_family == AF_INET6)
666 case SIOCSLIFPHYADDR:
667 /* checks done in the above */
671 TAILQ_FOREACH(ifp2, &ifnet, if_link) {
672 if (strcmp(ifp2->if_name, GIFNAME) != 0)
674 sc2 = ifp2->if_softc;
677 if (!sc2->gif_pdst || !sc2->gif_psrc)
679 if (sc2->gif_pdst->sa_family != dst->sa_family ||
680 sc2->gif_pdst->sa_len != dst->sa_len ||
681 sc2->gif_psrc->sa_family != src->sa_family ||
682 sc2->gif_psrc->sa_len != src->sa_len)
686 * Disallow parallel tunnels unless instructed
689 if (!parallel_tunnels &&
690 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
691 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
692 error = EADDRNOTAVAIL;
696 /* can't configure multiple multi-dest interfaces */
697 #define multidest(x) \
698 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
700 #define multidest6(x) \
701 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
703 if (dst->sa_family == AF_INET &&
704 multidest(dst) && multidest(sc2->gif_pdst)) {
705 error = EADDRNOTAVAIL;
709 if (dst->sa_family == AF_INET6 &&
710 multidest6(dst) && multidest6(sc2->gif_pdst)) {
711 error = EADDRNOTAVAIL;
718 free((caddr_t)sc->gif_psrc, M_IFADDR);
719 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
720 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
724 free((caddr_t)sc->gif_pdst, M_IFADDR);
725 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
726 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
729 ifp->if_flags |= IFF_RUNNING;
731 if_up(ifp); /* mark interface UP and send up RTM_IFINFO */
737 #ifdef SIOCDIFPHYADDR
740 free((caddr_t)sc->gif_psrc, M_IFADDR);
744 free((caddr_t)sc->gif_pdst, M_IFADDR);
747 /* change the IFF_{UP, RUNNING} flag as well? */
751 case SIOCGIFPSRCADDR:
753 case SIOCGIFPSRCADDR_IN6:
755 if (sc->gif_psrc == NULL) {
756 error = EADDRNOTAVAIL;
762 case SIOCGIFPSRCADDR:
763 dst = &ifr->ifr_addr;
764 size = sizeof(ifr->ifr_addr);
768 case SIOCGIFPSRCADDR_IN6:
769 dst = (struct sockaddr *)
770 &(((struct in6_ifreq *)data)->ifr_addr);
771 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
775 error = EADDRNOTAVAIL;
778 if (src->sa_len > size)
780 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
783 case SIOCGIFPDSTADDR:
785 case SIOCGIFPDSTADDR_IN6:
787 if (sc->gif_pdst == NULL) {
788 error = EADDRNOTAVAIL;
794 case SIOCGIFPDSTADDR:
795 dst = &ifr->ifr_addr;
796 size = sizeof(ifr->ifr_addr);
800 case SIOCGIFPDSTADDR_IN6:
801 dst = (struct sockaddr *)
802 &(((struct in6_ifreq *)data)->ifr_addr);
803 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
807 error = EADDRNOTAVAIL;
810 if (src->sa_len > size)
812 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
815 case SIOCGLIFPHYADDR:
816 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
817 error = EADDRNOTAVAIL;
823 dst = (struct sockaddr *)
824 &(((struct if_laddrreq *)data)->addr);
825 size = sizeof(((struct if_laddrreq *)data)->addr);
826 if (src->sa_len > size)
828 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
832 dst = (struct sockaddr *)
833 &(((struct if_laddrreq *)data)->dstaddr);
834 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
835 if (src->sa_len > size)
837 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
841 /* if_ioctl() takes care of it */
853 gif_delete_tunnel(sc)
854 struct gif_softc *sc;
856 /* XXX: NetBSD protects this function with splsoftnet() */
859 free((caddr_t)sc->gif_psrc, M_IFADDR);
863 free((caddr_t)sc->gif_pdst, M_IFADDR);
866 /* change the IFF_UP flag as well? */