2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/rmlock.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
50 #include <sys/errno.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
56 #include <sys/protosw.h>
58 #include <machine/cpu.h>
61 #include <net/if_var.h>
62 #include <net/if_clone.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_ecn.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
80 #include <netinet/in.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_ecn.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #include <netinet6/ip6protosw.h>
90 #include <netinet/ip_encap.h>
91 #include <net/ethernet.h>
92 #include <net/if_bridgevar.h>
93 #include <net/if_gif.h>
95 #include <security/mac/mac_framework.h>
97 static const char gifname[] = "gif";
100 * gif_mtx protects a per-vnet gif_softc_list.
102 static VNET_DEFINE(struct mtx, gif_mtx);
103 #define V_gif_mtx VNET(gif_mtx)
104 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
105 static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
106 #define V_gif_softc_list VNET(gif_softc_list)
107 static struct sx gif_ioctl_sx;
108 SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
110 #define GIF_LIST_LOCK_INIT(x) mtx_init(&V_gif_mtx, "gif_mtx", \
112 #define GIF_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gif_mtx)
113 #define GIF_LIST_LOCK(x) mtx_lock(&V_gif_mtx)
114 #define GIF_LIST_UNLOCK(x) mtx_unlock(&V_gif_mtx)
116 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
117 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
118 void (*ng_gif_attach_p)(struct ifnet *ifp);
119 void (*ng_gif_detach_p)(struct ifnet *ifp);
121 static int gif_check_nesting(struct ifnet *, struct mbuf *);
122 static int gif_set_tunnel(struct ifnet *, struct sockaddr *,
124 static void gif_delete_tunnel(struct ifnet *);
125 static int gif_ioctl(struct ifnet *, u_long, caddr_t);
126 static int gif_transmit(struct ifnet *, struct mbuf *);
127 static void gif_qflush(struct ifnet *);
128 static int gif_clone_create(struct if_clone *, int, caddr_t);
129 static void gif_clone_destroy(struct ifnet *);
130 static VNET_DEFINE(struct if_clone *, gif_cloner);
131 #define V_gif_cloner VNET(gif_cloner)
133 static int gifmodevent(module_t, int, void *);
135 SYSCTL_DECL(_net_link);
136 static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
137 "Generic Tunnel Interface");
140 * This macro controls the default upper limitation on nesting of gif tunnels.
141 * Since, setting a large value to this macro with a careless configuration
142 * may introduce system crash, we don't allow any nestings by default.
143 * If you need to configure nested gif tunnels, you can define this macro
144 * in your kernel configuration file. However, if you do so, please be
145 * careful to configure the tunnels so that it won't make a loop.
147 #define MAX_GIF_NEST 1
149 static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
150 #define V_max_gif_nesting VNET(max_gif_nesting)
151 SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
152 &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
155 * By default, we disallow creation of multiple tunnels between the same
156 * pair of addresses. Some applications require this functionality so
157 * we allow control over this check here.
160 static VNET_DEFINE(int, parallel_tunnels) = 1;
162 static VNET_DEFINE(int, parallel_tunnels) = 0;
164 #define V_parallel_tunnels VNET(parallel_tunnels)
165 SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
166 &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
168 /* copy from src/sys/net/if_ethersubr.c */
169 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
170 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
171 #ifndef ETHER_IS_BROADCAST
172 #define ETHER_IS_BROADCAST(addr) \
173 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
177 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
179 struct gif_softc *sc;
181 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
182 sc->gif_fibnum = curthread->td_proc->p_fibnum;
183 GIF2IFP(sc) = if_alloc(IFT_GIF);
185 GIF2IFP(sc)->if_softc = sc;
186 if_initname(GIF2IFP(sc), gifname, unit);
188 GIF2IFP(sc)->if_addrlen = 0;
189 GIF2IFP(sc)->if_mtu = GIF_MTU;
190 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
192 /* turn off ingress filter */
193 GIF2IFP(sc)->if_flags |= IFF_LINK2;
195 GIF2IFP(sc)->if_ioctl = gif_ioctl;
196 GIF2IFP(sc)->if_transmit = gif_transmit;
197 GIF2IFP(sc)->if_qflush = gif_qflush;
198 GIF2IFP(sc)->if_output = gif_output;
199 GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
200 GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
201 if_attach(GIF2IFP(sc));
202 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
203 if (ng_gif_attach_p != NULL)
204 (*ng_gif_attach_p)(GIF2IFP(sc));
207 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
213 gif_clone_destroy(struct ifnet *ifp)
215 struct gif_softc *sc;
217 sx_xlock(&gif_ioctl_sx);
219 gif_delete_tunnel(ifp);
221 LIST_REMOVE(sc, gif_list);
223 if (ng_gif_detach_p != NULL)
224 (*ng_gif_detach_p)(ifp);
227 ifp->if_softc = NULL;
228 sx_xunlock(&gif_ioctl_sx);
231 GIF_LOCK_DESTROY(sc);
236 vnet_gif_init(const void *unused __unused)
239 LIST_INIT(&V_gif_softc_list);
240 GIF_LIST_LOCK_INIT();
241 V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
242 gif_clone_destroy, 0);
244 VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
245 vnet_gif_init, NULL);
248 vnet_gif_uninit(const void *unused __unused)
251 if_clone_detach(V_gif_cloner);
252 GIF_LIST_LOCK_DESTROY();
254 VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
255 vnet_gif_uninit, NULL);
258 gifmodevent(module_t mod, int type, void *data)
271 static moduledata_t gif_mod = {
277 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
278 MODULE_VERSION(if_gif, 1);
281 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
284 struct gif_softc *sc;
288 sc = (struct gif_softc *)arg;
289 if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0)
295 /* no physical address */
296 if (sc->gif_family == 0)
306 case IPPROTO_ETHERIP:
312 /* Bail on short packets */
313 if (m->m_pkthdr.len < sizeof(struct ip))
316 m_copydata(m, 0, 1, &ver);
320 if (sc->gif_family != AF_INET)
322 ret = in_gif_encapcheck(m, off, proto, arg);
327 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
329 if (sc->gif_family != AF_INET6)
331 ret = in6_gif_encapcheck(m, off, proto, arg);
341 gif_transmit(struct ifnet *ifp, struct mbuf *m)
343 struct gif_softc *sc;
344 struct etherip_header *eth;
357 error = mac_ifnet_check_transmit(ifp, m);
365 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
366 (ifp->if_flags & IFF_UP) == 0 ||
367 sc->gif_family == 0 ||
368 (error = gif_check_nesting(ifp, m)) != 0) {
372 /* Now pull back the af that we stashed in the csum_data. */
376 af = m->m_pkthdr.csum_data;
377 m->m_flags &= ~(M_BCAST|M_MCAST);
378 M_SETFIB(m, sc->gif_fibnum);
379 BPF_MTAP2(ifp, &af, sizeof(af), m);
380 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
381 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
382 /* inner AF-specific encapsulation */
387 proto = IPPROTO_IPV4;
388 if (m->m_len < sizeof(struct ip))
389 m = m_pullup(m, sizeof(struct ip));
394 ip = mtod(m, struct ip *);
395 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
396 ECN_NOCARE, &ecn, &ip->ip_tos);
401 proto = IPPROTO_IPV6;
402 if (m->m_len < sizeof(struct ip6_hdr))
403 m = m_pullup(m, sizeof(struct ip6_hdr));
409 ip6 = mtod(m, struct ip6_hdr *);
410 ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
411 ECN_NOCARE, &t, &ip6->ip6_flow);
412 ecn = (ntohl(t) >> 20) & 0xff;
416 proto = IPPROTO_ETHERIP;
417 M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
422 eth = mtod(m, struct etherip_header *);
424 eth->eip_ver = ETHERIP_VERSION;
428 error = EAFNOSUPPORT;
432 /* XXX should we check if our outer source is legal? */
433 /* dispatch to output logic based on outer AF */
434 switch (sc->gif_family) {
437 error = in_gif_output(ifp, m, proto, ecn);
442 error = in6_gif_output(ifp, m, proto, ecn);
450 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
455 gif_qflush(struct ifnet *ifp __unused)
460 #define MTAG_GIF 1080679712
462 gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
468 * gif may cause infinite recursion calls when misconfigured.
469 * We'll prevent this by detecting loops.
471 * High nesting level may cause stack exhaustion.
472 * We'll prevent this by introducing upper limit.
476 while ((mtag = m_tag_locate(m, MTAG_GIF, 0, mtag)) != NULL) {
477 if (*(struct ifnet **)(mtag + 1) == ifp) {
478 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
483 if (count > V_max_gif_nesting) {
485 "%s: if_output recursively called too many times(%d)\n",
486 if_name(ifp), count);
489 mtag = m_tag_alloc(MTAG_GIF, 0, sizeof(struct ifnet *), M_NOWAIT);
492 *(struct ifnet **)(mtag + 1) = ifp;
493 m_tag_prepend(m, mtag);
498 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
503 if (dst->sa_family == AF_UNSPEC)
504 bcopy(dst->sa_data, &af, sizeof(af));
508 * Now save the af in the inbound pkt csum data, this is a cheat since
509 * we are using the inbound csum_data field to carry the af over to
510 * the gif_transmit() routine, avoiding using yet another mtag.
512 m->m_pkthdr.csum_data = af;
513 return (ifp->if_transmit(ifp, m));
517 gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
519 struct etherip_header *eip;
527 struct gif_softc *sc;
528 struct ether_header *eh;
529 struct ifnet *oldifp;
530 uint32_t gif_options;
539 gif_options = sc->gif_options;
540 m->m_pkthdr.rcvif = ifp;
546 if (m->m_len < sizeof(struct ip))
547 m = m_pullup(m, sizeof(struct ip));
550 ip = mtod(m, struct ip *);
551 if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
552 ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
561 if (m->m_len < sizeof(struct ip6_hdr))
562 m = m_pullup(m, sizeof(struct ip6_hdr));
565 t = htonl((uint32_t)ecn << 20);
566 ip6 = mtod(m, struct ip6_hdr *);
567 if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
568 ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
574 case IPPROTO_ETHERIP:
583 mac_ifnet_create_mbuf(ifp, m);
586 if (bpf_peers_present(ifp->if_bpf)) {
588 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
591 if ((ifp->if_flags & IFF_MONITOR) != 0) {
592 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
593 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
598 if (ng_gif_input_p != NULL) {
599 (*ng_gif_input_p)(ifp, &m, af);
605 * Put the packet to the network layer input queue according to the
606 * specified address family.
607 * Note: older versions of gif_input directly called network layer
608 * input functions, e.g. ip6_input, here. We changed the policy to
609 * prevent too many recursive calls of such input functions, which
610 * might cause kernel panic. But the change may introduce another
611 * problem; if the input queue is full, packets are discarded.
612 * The kernel stack overflow really happened, and we believed
613 * queue-full rarely occurs, so we changed the policy.
627 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
632 eip = mtod(m, struct etherip_header *);
633 if (eip->eip_ver != ETHERIP_VERSION) {
634 /* discard unknown versions */
638 m_adj(m, sizeof(struct etherip_header));
640 m->m_flags &= ~(M_BCAST|M_MCAST);
641 m->m_pkthdr.rcvif = ifp;
643 if (ifp->if_bridge) {
645 eh = mtod(m, struct ether_header *);
646 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
647 if (ETHER_IS_BROADCAST(eh->ether_dhost))
648 m->m_flags |= M_BCAST;
650 m->m_flags |= M_MCAST;
651 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
653 BRIDGE_INPUT(ifp, m);
655 if (m != NULL && ifp != oldifp) {
657 * The bridge gave us back itself or one of the
658 * members for which the frame is addressed.
669 if (ng_gif_input_orphan_p != NULL)
670 (*ng_gif_input_orphan_p)(ifp, m, af);
676 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
677 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
678 M_SETFIB(m, ifp->if_fib);
679 netisr_dispatch(isr, m);
682 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
685 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
687 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
690 struct ifreq *ifr = (struct ifreq*)data;
691 struct sockaddr *dst, *src;
692 struct gif_softc *sc;
694 struct sockaddr_in *sin = NULL;
697 struct sockaddr_in6 *sin6 = NULL;
704 ifp->if_flags |= IFF_UP;
711 if (ifr->ifr_mtu < GIF_MTU_MIN ||
712 ifr->ifr_mtu > GIF_MTU_MAX)
715 ifp->if_mtu = ifr->ifr_mtu;
718 sx_xlock(&gif_ioctl_sx);
728 case SIOCSIFPHYADDR_IN6:
734 src = (struct sockaddr *)
735 &(((struct in_aliasreq *)data)->ifra_addr);
736 dst = (struct sockaddr *)
737 &(((struct in_aliasreq *)data)->ifra_dstaddr);
741 case SIOCSIFPHYADDR_IN6:
742 src = (struct sockaddr *)
743 &(((struct in6_aliasreq *)data)->ifra_addr);
744 dst = (struct sockaddr *)
745 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
751 /* sa_family must be equal */
752 if (src->sa_family != dst->sa_family ||
753 src->sa_len != dst->sa_len)
756 /* validate sa_len */
757 /* check sa_family looks sane for the cmd */
758 switch (src->sa_family) {
761 if (src->sa_len != sizeof(struct sockaddr_in))
763 if (cmd != SIOCSIFPHYADDR) {
764 error = EAFNOSUPPORT;
767 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
768 satosin(dst)->sin_addr.s_addr == INADDR_ANY) {
769 error = EADDRNOTAVAIL;
776 if (src->sa_len != sizeof(struct sockaddr_in6))
778 if (cmd != SIOCSIFPHYADDR_IN6) {
779 error = EAFNOSUPPORT;
782 error = EADDRNOTAVAIL;
783 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
785 IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
788 * Check validity of the scope zone ID of the
789 * addresses, and convert it into the kernel
790 * internal form if necessary.
792 error = sa6_embedscope(satosin6(src), 0);
795 error = sa6_embedscope(satosin6(dst), 0);
801 error = EAFNOSUPPORT;
804 error = gif_set_tunnel(ifp, src, dst);
807 gif_delete_tunnel(ifp);
809 case SIOCGIFPSRCADDR:
810 case SIOCGIFPDSTADDR:
812 case SIOCGIFPSRCADDR_IN6:
813 case SIOCGIFPDSTADDR_IN6:
815 if (sc->gif_family == 0) {
816 error = EADDRNOTAVAIL;
822 case SIOCGIFPSRCADDR:
823 case SIOCGIFPDSTADDR:
824 if (sc->gif_family != AF_INET) {
825 error = EADDRNOTAVAIL;
828 sin = (struct sockaddr_in *)&ifr->ifr_addr;
829 memset(sin, 0, sizeof(*sin));
830 sin->sin_family = AF_INET;
831 sin->sin_len = sizeof(*sin);
835 case SIOCGIFPSRCADDR_IN6:
836 case SIOCGIFPDSTADDR_IN6:
837 if (sc->gif_family != AF_INET6) {
838 error = EADDRNOTAVAIL;
841 sin6 = (struct sockaddr_in6 *)
842 &(((struct in6_ifreq *)data)->ifr_addr);
843 memset(sin6, 0, sizeof(*sin6));
844 sin6->sin6_family = AF_INET6;
845 sin6->sin6_len = sizeof(*sin6);
849 error = EAFNOSUPPORT;
854 case SIOCGIFPSRCADDR:
855 sin->sin_addr = sc->gif_iphdr->ip_src;
857 case SIOCGIFPDSTADDR:
858 sin->sin_addr = sc->gif_iphdr->ip_dst;
862 case SIOCGIFPSRCADDR_IN6:
863 sin6->sin6_addr = sc->gif_ip6hdr->ip6_src;
865 case SIOCGIFPDSTADDR_IN6:
866 sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst;
876 case SIOCGIFPSRCADDR:
877 case SIOCGIFPDSTADDR:
878 error = prison_if(curthread->td_ucred,
879 (struct sockaddr *)sin);
881 memset(sin, 0, sizeof(*sin));
885 case SIOCGIFPSRCADDR_IN6:
886 case SIOCGIFPDSTADDR_IN6:
887 error = prison_if(curthread->td_ucred,
888 (struct sockaddr *)sin6);
890 error = sa6_recoverscope(sin6);
892 memset(sin6, 0, sizeof(*sin6));
897 ifr->ifr_fib = sc->gif_fibnum;
900 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
902 if (ifr->ifr_fib >= rt_numfibs)
905 sc->gif_fibnum = ifr->ifr_fib;
908 options = sc->gif_options;
909 error = copyout(&options, ifr->ifr_data, sizeof(options));
912 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
914 error = copyin(ifr->ifr_data, &options, sizeof(options));
917 if (options & ~GIF_OPTMASK)
920 sc->gif_options = options;
927 sx_xunlock(&gif_ioctl_sx);
932 gif_detach(struct gif_softc *sc)
935 sx_assert(&gif_ioctl_sx, SA_XLOCKED);
936 if (sc->gif_ecookie != NULL)
937 encap_detach(sc->gif_ecookie);
938 sc->gif_ecookie = NULL;
942 gif_attach(struct gif_softc *sc, int af)
945 sx_assert(&gif_ioctl_sx, SA_XLOCKED);
949 return (in_gif_attach(sc));
953 return (in6_gif_attach(sc));
956 return (EAFNOSUPPORT);
960 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
962 struct gif_softc *sc = ifp->if_softc;
963 struct gif_softc *tsc;
975 /* Disallow parallel tunnels unless instructed otherwise. */
976 if (V_parallel_tunnels == 0) {
978 LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) {
979 if (tsc == sc || tsc->gif_family != src->sa_family)
982 if (tsc->gif_family == AF_INET &&
983 tsc->gif_iphdr->ip_src.s_addr ==
984 satosin(src)->sin_addr.s_addr &&
985 tsc->gif_iphdr->ip_dst.s_addr ==
986 satosin(dst)->sin_addr.s_addr) {
987 error = EADDRNOTAVAIL;
993 if (tsc->gif_family == AF_INET6 &&
994 IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src,
995 &satosin6(src)->sin6_addr) &&
996 IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst,
997 &satosin6(dst)->sin6_addr)) {
998 error = EADDRNOTAVAIL;
1006 switch (src->sa_family) {
1009 hdr = ip = malloc(sizeof(struct ip), M_GIF,
1011 ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr;
1012 ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr;
1017 hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF,
1019 ip6->ip6_src = satosin6(src)->sin6_addr;
1020 ip6->ip6_dst = satosin6(dst)->sin6_addr;
1021 ip6->ip6_vfc = IPV6_VERSION;
1025 return (EAFNOSUPPORT);
1028 if (sc->gif_family != src->sa_family)
1030 if (sc->gif_family == 0 ||
1031 sc->gif_family != src->sa_family)
1032 error = gif_attach(sc, src->sa_family);
1035 if (sc->gif_family != 0)
1036 free(sc->gif_hdr, M_GIF);
1037 sc->gif_family = src->sa_family;
1040 #if defined(INET) || defined(INET6)
1043 if (error == 0 && sc->gif_family != 0) {
1044 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1045 if_link_state_change(ifp, LINK_STATE_UP);
1047 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1048 if_link_state_change(ifp, LINK_STATE_DOWN);
1054 gif_delete_tunnel(struct ifnet *ifp)
1056 struct gif_softc *sc = ifp->if_softc;
1063 family = sc->gif_family;
1068 free(sc->gif_hdr, M_GIF);
1070 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1071 if_link_state_change(ifp, LINK_STATE_DOWN);