2 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 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>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/errno.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
51 #include <sys/protosw.h>
53 #include <machine/cpu.h>
56 #include <net/if_clone.h>
57 #include <net/if_types.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_gif.h>
69 #include <netinet/ip_var.h>
74 #include <netinet/in.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/scope6_var.h>
80 #include <netinet6/in6_gif.h>
81 #include <netinet6/ip6protosw.h>
84 #include <netinet/ip_encap.h>
85 #include <net/ethernet.h>
86 #include <net/if_bridgevar.h>
87 #include <net/if_gif.h>
89 #include <security/mac/mac_framework.h>
91 static const char gifname[] = "gif";
94 * gif_mtx protects a per-vnet gif_softc_list.
96 static VNET_DEFINE(struct mtx, gif_mtx);
97 #define V_gif_mtx VNET(gif_mtx)
98 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
99 static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
100 #define V_gif_softc_list VNET(gif_softc_list)
102 #define GIF_LIST_LOCK_INIT(x) mtx_init(&V_gif_mtx, "gif_mtx", \
104 #define GIF_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gif_mtx)
105 #define GIF_LIST_LOCK(x) mtx_lock(&V_gif_mtx)
106 #define GIF_LIST_UNLOCK(x) mtx_unlock(&V_gif_mtx)
108 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
109 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
110 void (*ng_gif_attach_p)(struct ifnet *ifp);
111 void (*ng_gif_detach_p)(struct ifnet *ifp);
113 static void gif_start(struct ifnet *);
114 static int gif_clone_create(struct if_clone *, int, caddr_t);
115 static void gif_clone_destroy(struct ifnet *);
116 static VNET_DEFINE(struct if_clone *, gif_cloner);
117 #define V_gif_cloner VNET(gif_cloner)
119 static int gifmodevent(module_t, int, void *);
121 SYSCTL_DECL(_net_link);
122 static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
123 "Generic Tunnel Interface");
126 * This macro controls the default upper limitation on nesting of gif tunnels.
127 * Since, setting a large value to this macro with a careless configuration
128 * may introduce system crash, we don't allow any nestings by default.
129 * If you need to configure nested gif tunnels, you can define this macro
130 * in your kernel configuration file. However, if you do so, please be
131 * careful to configure the tunnels so that it won't make a loop.
133 #define MAX_GIF_NEST 1
135 static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
136 #define V_max_gif_nesting VNET(max_gif_nesting)
137 SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
138 &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
141 * By default, we disallow creation of multiple tunnels between the same
142 * pair of addresses. Some applications require this functionality so
143 * we allow control over this check here.
146 static VNET_DEFINE(int, parallel_tunnels) = 1;
148 static VNET_DEFINE(int, parallel_tunnels) = 0;
150 #define V_parallel_tunnels VNET(parallel_tunnels)
151 SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
152 &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
154 /* copy from src/sys/net/if_ethersubr.c */
155 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
156 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
157 #ifndef ETHER_IS_BROADCAST
158 #define ETHER_IS_BROADCAST(addr) \
159 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
163 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
165 struct gif_softc *sc;
167 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
168 sc->gif_fibnum = curthread->td_proc->p_fibnum;
169 GIF2IFP(sc) = if_alloc(IFT_GIF);
170 if (GIF2IFP(sc) == NULL) {
177 GIF2IFP(sc)->if_softc = sc;
178 if_initname(GIF2IFP(sc), gifname, unit);
180 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
183 GIF2IFP(sc)->if_addrlen = 0;
184 GIF2IFP(sc)->if_mtu = GIF_MTU;
185 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
187 /* turn off ingress filter */
188 GIF2IFP(sc)->if_flags |= IFF_LINK2;
190 GIF2IFP(sc)->if_ioctl = gif_ioctl;
191 GIF2IFP(sc)->if_start = gif_start;
192 GIF2IFP(sc)->if_output = gif_output;
193 GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
194 if_attach(GIF2IFP(sc));
195 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
196 if (ng_gif_attach_p != NULL)
197 (*ng_gif_attach_p)(GIF2IFP(sc));
200 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
207 gif_clone_destroy(struct ifnet *ifp)
209 #if defined(INET) || defined(INET6)
212 struct gif_softc *sc = ifp->if_softc;
215 LIST_REMOVE(sc, gif_list);
218 gif_delete_tunnel(ifp);
220 if (sc->encap_cookie6 != NULL) {
221 err = encap_detach(sc->encap_cookie6);
222 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
226 if (sc->encap_cookie4 != NULL) {
227 err = encap_detach(sc->encap_cookie4);
228 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
232 if (ng_gif_detach_p != NULL)
233 (*ng_gif_detach_p)(ifp);
238 GIF_LOCK_DESTROY(sc);
244 vnet_gif_init(const void *unused __unused)
247 LIST_INIT(&V_gif_softc_list);
248 GIF_LIST_LOCK_INIT();
249 V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
250 gif_clone_destroy, 0);
252 VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
253 vnet_gif_init, NULL);
256 vnet_gif_uninit(const void *unused __unused)
259 if_clone_detach(V_gif_cloner);
260 GIF_LIST_LOCK_DESTROY();
262 VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
263 vnet_gif_uninit, NULL);
266 gifmodevent(module_t mod, int type, void *data)
279 static moduledata_t gif_mod = {
285 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
286 MODULE_VERSION(if_gif, 1);
289 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
292 struct gif_softc *sc;
294 sc = (struct gif_softc *)arg;
298 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
301 /* no physical address */
302 if (!sc->gif_psrc || !sc->gif_pdst)
314 case IPPROTO_ETHERIP:
321 /* Bail on short packets */
322 if (m->m_pkthdr.len < sizeof(ip))
325 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
330 if (sc->gif_psrc->sa_family != AF_INET ||
331 sc->gif_pdst->sa_family != AF_INET)
333 return gif_encapcheck4(m, off, proto, arg);
337 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
339 if (sc->gif_psrc->sa_family != AF_INET6 ||
340 sc->gif_pdst->sa_family != AF_INET6)
342 return gif_encapcheck6(m, off, proto, arg);
349 #define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip))
352 #define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr))
356 gif_start(struct ifnet *ifp)
358 struct gif_softc *sc;
365 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
366 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
368 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
373 /* Take out those altq bytes we add in gif_output */
375 if (sc->gif_psrc->sa_family == AF_INET)
376 m->m_pkthdr.len -= GIF_HDR_LEN;
379 if (sc->gif_psrc->sa_family == AF_INET6)
380 m->m_pkthdr.len -= GIF_HDR_LEN6;
384 * Now pull back the af that we
385 * stashed in the csum_data.
387 af = m->m_pkthdr.csum_data;
389 /* override to IPPROTO_ETHERIP for bridged traffic */
393 BPF_MTAP2(ifp, &af, sizeof(af), m);
396 /* Done by IFQ_HANDOFF */
397 /* ifp->if_obytes += m->m_pkthdr.len;*/
399 M_SETFIB(m, sc->gif_fibnum);
400 /* inner AF-specific encapsulation */
401 /* XXX should we check if our outer source is legal? */
402 /* dispatch to output logic based on outer AF */
403 switch (sc->gif_psrc->sa_family) {
406 error = in_gif_output(ifp, af, m);
411 error = in6_gif_output(ifp, af, m);
422 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
428 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
431 struct gif_softc *sc = ifp->if_softc;
437 error = mac_ifnet_check_transmit(ifp, m);
443 if ((ifp->if_flags & IFF_MONITOR) != 0) {
450 * gif may cause infinite recursion calls when misconfigured.
451 * We'll prevent this by detecting loops.
453 * High nesting level may cause stack exhaustion.
454 * We'll prevent this by introducing upper limit.
457 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
458 while (mtag != NULL) {
459 if (*(struct ifnet **)(mtag + 1) == ifp) {
461 "gif_output: loop detected on %s\n",
462 (*(struct ifnet **)(mtag + 1))->if_xname);
464 error = EIO; /* is there better errno? */
467 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
470 if (gif_called > V_max_gif_nesting) {
472 "gif_output: recursively called too many times(%d)\n",
475 error = EIO; /* is there better errno? */
478 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
485 *(struct ifnet **)(mtag + 1) = ifp;
486 m_tag_prepend(m, mtag);
488 m->m_flags &= ~(M_BCAST|M_MCAST);
489 /* BPF writes need to be handled specially. */
490 if (dst->sa_family == AF_UNSPEC)
491 bcopy(dst->sa_data, &af, sizeof(af));
495 * Now save the af in the inbound pkt csum
496 * data, this is a cheat since we are using
497 * the inbound csum_data field to carry the
498 * af over to the gif_start() routine, avoiding
499 * using yet another mtag.
501 m->m_pkthdr.csum_data = af;
502 if (!(ifp->if_flags & IFF_UP) ||
503 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
510 * Make altq aware of the bytes we will add
511 * when we actually send it.
514 if (sc->gif_psrc->sa_family == AF_INET)
515 m->m_pkthdr.len += GIF_HDR_LEN;
518 if (sc->gif_psrc->sa_family == AF_INET6)
519 m->m_pkthdr.len += GIF_HDR_LEN6;
523 * Queue message on interface, update output statistics if
524 * successful, and start output if interface not yet active.
526 IFQ_HANDOFF(ifp, m, error);
534 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
537 struct gif_softc *sc;
538 struct etherip_header *eip;
539 struct ether_header *eh;
540 struct ifnet *oldifp;
548 m->m_pkthdr.rcvif = ifp;
552 mac_ifnet_create_mbuf(ifp, m);
555 if (bpf_peers_present(ifp->if_bpf)) {
557 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
560 if ((ifp->if_flags & IFF_MONITOR) != 0) {
562 ifp->if_ibytes += m->m_pkthdr.len;
567 if (ng_gif_input_p != NULL) {
568 (*ng_gif_input_p)(ifp, &m, af);
574 * Put the packet to the network layer input queue according to the
575 * specified address family.
576 * Note: older versions of gif_input directly called network layer
577 * input functions, e.g. ip6_input, here. We changed the policy to
578 * prevent too many recursive calls of such input functions, which
579 * might cause kernel panic. But the change may introduce another
580 * problem; if the input queue is full, packets are discarded.
581 * The kernel stack overflow really happened, and we believed
582 * queue-full rarely occurs, so we changed the policy.
596 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
605 eip = mtod(m, struct etherip_header *);
607 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
608 * accepts an EtherIP packet with revered version field in
609 * the header. This is a knob for backward compatibility
610 * with FreeBSD 7.2R or prior.
612 if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
613 if (eip->eip_resvl != ETHERIP_VERSION
614 && eip->eip_ver != ETHERIP_VERSION) {
615 /* discard unknown versions */
620 if (eip->eip_ver != ETHERIP_VERSION) {
621 /* discard unknown versions */
626 m_adj(m, sizeof(struct etherip_header));
628 m->m_flags &= ~(M_BCAST|M_MCAST);
629 m->m_pkthdr.rcvif = ifp;
631 if (ifp->if_bridge) {
633 eh = mtod(m, struct ether_header *);
634 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
635 if (ETHER_IS_BROADCAST(eh->ether_dhost))
636 m->m_flags |= M_BCAST;
638 m->m_flags |= M_MCAST;
641 BRIDGE_INPUT(ifp, m);
643 if (m != NULL && ifp != oldifp) {
645 * The bridge gave us back itself or one of the
646 * members for which the frame is addressed.
657 if (ng_gif_input_orphan_p != NULL)
658 (*ng_gif_input_orphan_p)(ifp, m, af);
665 ifp->if_ibytes += m->m_pkthdr.len;
666 M_SETFIB(m, ifp->if_fib);
667 netisr_dispatch(isr, m);
670 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
672 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
674 struct gif_softc *sc = ifp->if_softc;
675 struct ifreq *ifr = (struct ifreq*)data;
678 struct sockaddr *dst, *src;
679 #ifdef SIOCSIFMTU /* xxx */
685 ifp->if_flags |= IFF_UP;
692 #ifdef SIOCSIFMTU /* xxx */
698 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
702 #endif /* SIOCSIFMTU */
708 case SIOCSIFPHYADDR_IN6:
710 case SIOCSLIFPHYADDR:
714 src = (struct sockaddr *)
715 &(((struct in_aliasreq *)data)->ifra_addr);
716 dst = (struct sockaddr *)
717 &(((struct in_aliasreq *)data)->ifra_dstaddr);
721 case SIOCSIFPHYADDR_IN6:
722 src = (struct sockaddr *)
723 &(((struct in6_aliasreq *)data)->ifra_addr);
724 dst = (struct sockaddr *)
725 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
728 case SIOCSLIFPHYADDR:
729 src = (struct sockaddr *)
730 &(((struct if_laddrreq *)data)->addr);
731 dst = (struct sockaddr *)
732 &(((struct if_laddrreq *)data)->dstaddr);
738 /* sa_family must be equal */
739 if (src->sa_family != dst->sa_family)
742 /* validate sa_len */
743 switch (src->sa_family) {
746 if (src->sa_len != sizeof(struct sockaddr_in))
752 if (src->sa_len != sizeof(struct sockaddr_in6))
759 switch (dst->sa_family) {
762 if (dst->sa_len != sizeof(struct sockaddr_in))
768 if (dst->sa_len != sizeof(struct sockaddr_in6))
776 /* check sa_family looks sane for the cmd */
779 if (src->sa_family == AF_INET)
783 case SIOCSIFPHYADDR_IN6:
784 if (src->sa_family == AF_INET6)
788 case SIOCSLIFPHYADDR:
789 /* checks done in the above */
793 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
796 #ifdef SIOCDIFPHYADDR
798 gif_delete_tunnel(GIF2IFP(sc));
802 case SIOCGIFPSRCADDR:
804 case SIOCGIFPSRCADDR_IN6:
806 if (sc->gif_psrc == NULL) {
807 error = EADDRNOTAVAIL;
813 case SIOCGIFPSRCADDR:
814 dst = &ifr->ifr_addr;
815 size = sizeof(ifr->ifr_addr);
819 case SIOCGIFPSRCADDR_IN6:
820 dst = (struct sockaddr *)
821 &(((struct in6_ifreq *)data)->ifr_addr);
822 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
826 error = EADDRNOTAVAIL;
829 if (src->sa_len > size)
831 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
833 if (dst->sa_family == AF_INET6) {
834 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
841 case SIOCGIFPDSTADDR:
843 case SIOCGIFPDSTADDR_IN6:
845 if (sc->gif_pdst == NULL) {
846 error = EADDRNOTAVAIL;
852 case SIOCGIFPDSTADDR:
853 dst = &ifr->ifr_addr;
854 size = sizeof(ifr->ifr_addr);
858 case SIOCGIFPDSTADDR_IN6:
859 dst = (struct sockaddr *)
860 &(((struct in6_ifreq *)data)->ifr_addr);
861 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
865 error = EADDRNOTAVAIL;
868 if (src->sa_len > size)
870 error = prison_if(curthread->td_ucred, src);
873 error = prison_if(curthread->td_ucred, dst);
876 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
878 if (dst->sa_family == AF_INET6) {
879 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
886 case SIOCGLIFPHYADDR:
887 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
888 error = EADDRNOTAVAIL;
894 dst = (struct sockaddr *)
895 &(((struct if_laddrreq *)data)->addr);
896 size = sizeof(((struct if_laddrreq *)data)->addr);
897 if (src->sa_len > size)
899 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
903 dst = (struct sockaddr *)
904 &(((struct if_laddrreq *)data)->dstaddr);
905 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
906 if (src->sa_len > size)
908 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
912 /* if_ioctl() takes care of it */
916 options = sc->gif_options;
917 error = copyout(&options, ifr->ifr_data,
922 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
924 error = copyin(ifr->ifr_data, &options, sizeof(options));
927 if (options & ~GIF_OPTMASK)
930 sc->gif_options = options;
942 * XXXRW: There's a general event-ordering issue here: the code to check
943 * if a given tunnel is already present happens before we perform a
944 * potentially blocking setup of the tunnel. This code needs to be
945 * re-ordered so that the check and replacement can be atomic using
949 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
951 struct gif_softc *sc = ifp->if_softc;
952 struct gif_softc *sc2;
953 struct sockaddr *osrc, *odst, *sa;
957 LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
960 if (!sc2->gif_pdst || !sc2->gif_psrc)
962 if (sc2->gif_pdst->sa_family != dst->sa_family ||
963 sc2->gif_pdst->sa_len != dst->sa_len ||
964 sc2->gif_psrc->sa_family != src->sa_family ||
965 sc2->gif_psrc->sa_len != src->sa_len)
969 * Disallow parallel tunnels unless instructed
972 if (!V_parallel_tunnels &&
973 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
974 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
975 error = EADDRNOTAVAIL;
980 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
984 /* XXX we can detach from both, but be polite just in case */
986 switch (sc->gif_psrc->sa_family) {
989 (void)in_gif_detach(sc);
994 (void)in6_gif_detach(sc);
1000 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1001 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1004 odst = sc->gif_pdst;
1005 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1006 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1009 switch (sc->gif_psrc->sa_family) {
1012 error = in_gif_attach(sc);
1018 * Check validity of the scope zone ID of the addresses, and
1019 * convert it into the kernel internal form if necessary.
1021 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1024 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1027 error = in6_gif_attach(sc);
1033 free((caddr_t)sc->gif_psrc, M_IFADDR);
1034 free((caddr_t)sc->gif_pdst, M_IFADDR);
1035 sc->gif_psrc = osrc;
1036 sc->gif_pdst = odst;
1041 free((caddr_t)osrc, M_IFADDR);
1043 free((caddr_t)odst, M_IFADDR);
1046 if (sc->gif_psrc && sc->gif_pdst)
1047 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1049 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1055 gif_delete_tunnel(struct ifnet *ifp)
1057 struct gif_softc *sc = ifp->if_softc;
1060 free((caddr_t)sc->gif_psrc, M_IFADDR);
1061 sc->gif_psrc = NULL;
1064 free((caddr_t)sc->gif_pdst, M_IFADDR);
1065 sc->gif_pdst = NULL;
1067 /* it is safe to detach from both */
1069 (void)in_gif_detach(sc);
1072 (void)in6_gif_detach(sc);
1074 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;