]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/if_gif.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / net / if_gif.c
index e0caf004ba54443dd2b13d82445aae0ea4076c5a..9f9e6c574e74d9309de9e052002b133161edd7e2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  *
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
-#if    INET
+#if     INET
 #include <netinet/in_var.h>
 #include <netinet/in_gif.h>
 #include <netinet/ip_var.h>
-#endif /* INET */
+#endif  /* INET */
 
 #if INET6
 #include <netinet6/in6_var.h>
 #include <security/mac_framework.h>
 #endif
 
-#define        GIFNAME         "gif"
-#define        GIFDEV          "if_gif"
+#define GIFNAME         "gif"
+#define GIFDEV          "if_gif"
 
-#define        GIF_MAXUNIT     IF_MAXUNIT
-#define        GIF_ZONE_MAX_ELEM       MIN(IFNETS_MAX, GIF_MAXUNIT)
+#define GIF_MAXUNIT     IF_MAXUNIT
+#define GIF_ZONE_MAX_ELEM       MIN(IFNETS_MAX, GIF_MAXUNIT)
 
 /* gif lock variables */
-static lck_grp_t       *gif_mtx_grp;
-static lck_grp_attr_t  *gif_mtx_grp_attr;
-static lck_attr_t      *gif_mtx_attr;
+static lck_grp_t        *gif_mtx_grp;
+static lck_grp_attr_t   *gif_mtx_grp_attr;
+static lck_attr_t       *gif_mtx_attr;
 decl_lck_mtx_data(static, gif_mtx_data);
-static lck_mtx_t       *gif_mtx = &gif_mtx_data;
+static lck_mtx_t        *gif_mtx = &gif_mtx_data;
 
 TAILQ_HEAD(gifhead, gif_softc) gifs = TAILQ_HEAD_INITIALIZER(gifs);
 
@@ -133,28 +133,28 @@ static errno_t gif_input(ifnet_t ifp, protocol_family_t protocol_family,
     mbuf_t m, char *frame_header);
 static errno_t gif_ioctl(ifnet_t ifp, u_long cmd, void *data);
 
-static int ngif = 0;           /* number of interfaces */
+static int ngif = 0;            /* number of interfaces */
 
 #if INET
 static struct protosw in_gif_protosw =
 {
-       .pr_type =              SOCK_RAW,
-       .pr_protocol =          0, /* IPPROTO_IPV[46] */
-       .pr_flags =             PR_ATOMIC|PR_ADDR,
-       .pr_input =             in_gif_input,
-       .pr_usrreqs =           &rip_usrreqs,
-       .pr_unlock =            rip_unlock,
+       .pr_type =              SOCK_RAW,
+       .pr_protocol =          0, /* IPPROTO_IPV[46] */
+       .pr_flags =             PR_ATOMIC | PR_ADDR,
+       .pr_input =             in_gif_input,
+       .pr_usrreqs =           &rip_usrreqs,
+       .pr_unlock =            rip_unlock,
 };
 #endif
 #if INET6
 static struct ip6protosw in6_gif_protosw =
 {
-       .pr_type =              SOCK_RAW,
-       .pr_protocol =          0, /* IPPROTO_IPV[46] */
-       .pr_flags =             PR_ATOMIC|PR_ADDR,
-       .pr_input =             in6_gif_input,
-       .pr_usrreqs =           &rip6_usrreqs,
-       .pr_unlock =            rip_unlock,
+       .pr_type =              SOCK_RAW,
+       .pr_protocol =          0, /* IPPROTO_IPV[46] */
+       .pr_flags =             PR_ATOMIC | PR_ADDR,
+       .pr_input =             in6_gif_input,
+       .pr_usrreqs =           &rip6_usrreqs,
+       .pr_unlock =            rip_unlock,
 };
 #endif
 
@@ -166,7 +166,7 @@ static void gif_detach(struct ifnet *);
 
 static struct if_clone gif_cloner =
     IF_CLONE_INITIALIZER(GIFNAME, gif_clone_create, gif_clone_destroy,
-        0, GIF_MAXUNIT, GIF_ZONE_MAX_ELEM, sizeof(struct gif_softc));
+    0, GIF_MAXUNIT, GIF_ZONE_MAX_ELEM, sizeof(struct gif_softc));
 /*
  * Theory of operation: initially, one gif interface is created.
  * Any time a gif interface is configured, if there are no other
@@ -192,7 +192,7 @@ gif_demux(
        *protocol_family = sc->gif_proto;
        GIF_UNLOCK(sc);
 
-       return (0);
+       return 0;
 }
 
 static errno_t
@@ -206,14 +206,15 @@ gif_add_proto(
        struct gif_softc *sc = ifnet_softc(ifp);
 
        GIF_LOCK(sc);
-       if (sc->gif_proto != 0)
+       if (sc->gif_proto != 0) {
                printf("gif_add_proto: request add_proto for gif%d\n",
                    ifnet_unit(ifp));
+       }
 
        sc->gif_proto = protocol_family;
        GIF_UNLOCK(sc);
 
-       return (0);
+       return 0;
 }
 
 static errno_t
@@ -224,11 +225,12 @@ gif_del_proto(
        struct gif_softc *sc = ifnet_softc(ifp);
 
        GIF_LOCK(sc);
-       if (sc->gif_proto == protocol_family)
+       if (sc->gif_proto == protocol_family) {
                sc->gif_proto = 0;
+       }
        GIF_UNLOCK(sc);
 
-       return (0);
+       return 0;
 }
 
 /* Glue code to attach inet to a gif interface through DLIL */
@@ -240,7 +242,7 @@ gif_attach_proto_family(
        struct ifnet_attach_proto_param reg;
        errno_t stat;
 
-       bzero(&reg, sizeof (reg));
+       bzero(&reg, sizeof(reg));
        reg.input = gif_input;
 
        stat = ifnet_attach_protocol(ifp, protocol_family, &reg);
@@ -249,7 +251,7 @@ gif_attach_proto_family(
                    fam=%d\n", protocol_family);
        }
 
-       return (stat);
+       return stat;
 }
 
 /* Function to setup the first gif interface */
@@ -270,19 +272,22 @@ gif_init(void)
        /* Register protocol registration functions */
        result = proto_register_plumber(PF_INET, APPLE_IF_FAM_GIF,
            gif_attach_proto_family, NULL);
-       if (result != 0)
+       if (result != 0) {
                printf("proto_register_plumber failed for AF_INET error=%d\n",
                    result);
+       }
 
        result = proto_register_plumber(PF_INET6, APPLE_IF_FAM_GIF,
            gif_attach_proto_family, NULL);
-       if (result != 0)
+       if (result != 0) {
                printf("proto_register_plumber failed for AF_INET6 error=%d\n",
                    result);
+       }
 
        result = if_clone_attach(&gif_cloner);
-       if (result != 0)
+       if (result != 0) {
                panic("%s: if_clone_attach() failed, error %d\n", __func__, result);
+       }
 
        gif_clone_create(&gif_cloner, 0, NULL);
 }
@@ -300,7 +305,7 @@ gif_set_bpf_tap(
        sc->tap_callback = callback;
        GIF_UNLOCK(sc);
 
-       return (0);
+       return 0;
 }
 
 static void
@@ -337,14 +342,14 @@ gif_clone_create(struct if_clone *ifc, uint32_t unit, __unused void *params)
        }
 
        /* use the interface name as the unique id for ifp recycle */
-       snprintf(sc->gif_ifname, sizeof (sc->gif_ifname), "%s%d",
+       snprintf(sc->gif_ifname, sizeof(sc->gif_ifname), "%s%d",
            ifc->ifc_name, unit);
 
        lck_mtx_init(&sc->gif_lock, gif_mtx_grp, gif_mtx_attr);
 
-       bzero(&gif_init_params, sizeof (gif_init_params));
+       bzero(&gif_init_params, sizeof(gif_init_params));
        gif_init_params.ver = IFNET_INIT_CURRENT_VERSION;
-       gif_init_params.len = sizeof (gif_init_params);
+       gif_init_params.len = sizeof(gif_init_params);
        gif_init_params.flags = IFNET_INIT_LEGACY;
        gif_init_params.uniqueid = sc->gif_ifname;
        gif_init_params.uniqueid_len = strlen(sc->gif_ifname);
@@ -372,7 +377,7 @@ gif_clone_create(struct if_clone *ifc, uint32_t unit, __unused void *params)
        sc->encap_cookie4 = sc->encap_cookie6 = NULL;
 #if INET
        sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
-                       gif_encapcheck, &in_gif_protosw, sc);
+           gif_encapcheck, &in_gif_protosw, sc);
        if (sc->encap_cookie4 == NULL) {
                printf("%s: unable to attach encap4\n", if_name(sc->gif_if));
                ifnet_release(sc->gif_if);
@@ -422,14 +427,14 @@ gif_clone_create(struct if_clone *ifc, uint32_t unit, __unused void *params)
 #if CONFIG_MACF_NET
        mac_ifnet_label_init(&sc->gif_if);
 #endif
-       bpfattach(sc->gif_if, DLT_NULL, sizeof (u_int));
+       bpfattach(sc->gif_if, DLT_NULL, sizeof(u_int));
        sc->gif_flags &= ~IFGIF_DETACHING;
        TAILQ_INSERT_TAIL(&gifs, sc, gif_link);
        ngif++;
 done:
        lck_mtx_unlock(gif_mtx);
 
-       return (error);
+       return error;
 }
 
 static int
@@ -437,6 +442,8 @@ gif_remove(struct ifnet *ifp)
 {
        int error = 0;
        struct gif_softc *sc = NULL;
+       const struct encaptab *encap_cookie4 = NULL;
+       const struct encaptab *encap_cookie6 = NULL;
 
        lck_mtx_lock(gif_mtx);
        sc = ifp->if_softc;
@@ -458,25 +465,30 @@ gif_remove(struct ifnet *ifp)
 
        gif_delete_tunnel(sc);
 #ifdef INET6
-       if (sc->encap_cookie6 != NULL) {
-               error = encap_detach(sc->encap_cookie6);
-               KASSERT(error == 0, ("gif_clone_destroy: Unexpected "
-                   "error detaching encap_cookie6"));
-       }
+       encap_cookie6 = sc->encap_cookie6;
 #endif
 #ifdef INET
-       if (sc->encap_cookie4 != NULL) {
-               error = encap_detach(sc->encap_cookie4);
-               KASSERT(error == 0, ("gif_clone_destroy: Unexpected "
-                   "error detaching encap_cookie4"));
-       }
+       encap_cookie4 = sc->encap_cookie4;
 #endif
 done:
-       if (sc != NULL)
+       if (sc != NULL) {
                GIF_UNLOCK(sc);
+       }
        lck_mtx_unlock(gif_mtx);
 
-       return (error);
+       if (encap_cookie6 != NULL) {
+               error = encap_detach(encap_cookie6);
+               KASSERT(error == 0, ("gif_clone_destroy: Unexpected "
+                   "error detaching encap_cookie6"));
+       }
+
+       if (encap_cookie4 != NULL) {
+               error = encap_detach(encap_cookie4);
+               KASSERT(error == 0, ("gif_clone_destroy: Unexpected "
+                   "error detaching encap_cookie4"));
+       }
+
+       return error;
 }
 
 static int
@@ -487,7 +499,7 @@ gif_clone_destroy(struct ifnet *ifp)
        error = gif_remove(ifp);
        if (error != 0) {
                printf("gif_clone_destroy: gif remove failed %d\n", error);
-               return (error);
+               return error;
        }
 
        error = ifnet_set_flags(ifp, 0, IFF_UP);
@@ -496,10 +508,11 @@ gif_clone_destroy(struct ifnet *ifp)
        }
 
        error = ifnet_detach(ifp);
-       if (error != 0)
+       if (error != 0) {
                panic("gif_clone_destroy: ifnet_detach(%p) failed %d\n", ifp,
                    error);
-       return (0);
+       }
+       return 0;
 }
 
 static int
@@ -514,16 +527,19 @@ gif_encapcheck(
        struct gif_softc *sc;
 
        sc = (struct gif_softc *)arg;
-       if (sc == NULL)
-               return (error);
+       if (sc == NULL) {
+               return error;
+       }
 
        GIF_LOCK(sc);
-       if ((ifnet_flags(sc->gif_if) & IFF_UP) == 0)
+       if ((ifnet_flags(sc->gif_if) & IFF_UP) == 0) {
                goto done;
+       }
 
        /* no physical address */
-       if (!sc->gif_psrc || !sc->gif_pdst)
+       if (!sc->gif_psrc || !sc->gif_pdst) {
                goto done;
+       }
 
        switch (proto) {
 #if INET
@@ -538,21 +554,23 @@ gif_encapcheck(
                goto done;
        }
 
-       mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof (ip), &ip);
+       mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip), &ip);
 
        switch (ip.ip_v) {
 #if INET
        case 4:
                if (sc->gif_psrc->sa_family != AF_INET ||
-                   sc->gif_pdst->sa_family != AF_INET)
+                   sc->gif_pdst->sa_family != AF_INET) {
                        goto done;
+               }
                error = gif_encapcheck4(m, off, proto, arg);
 #endif
 #if INET6
        case 6:
                if (sc->gif_psrc->sa_family != AF_INET6 ||
-                   sc->gif_pdst->sa_family != AF_INET6)
+                   sc->gif_pdst->sa_family != AF_INET6) {
                        goto done;
+               }
                error = gif_encapcheck6(m, off, proto, arg);
 #endif
        default:
@@ -560,7 +578,7 @@ gif_encapcheck(
        }
 done:
        GIF_UNLOCK(sc);
-       return (error);
+       return error;
 }
 
 static errno_t
@@ -583,16 +601,16 @@ gif_output(
         * because there is no guaruntee that we won't be called
         * concurrently from more than one thread.
         */
-       m->m_flags &= ~(M_BCAST|M_MCAST);
+       m->m_flags &= ~(M_BCAST | M_MCAST);
        if (!(ifnet_flags(ifp) & IFF_UP) ||
            gif_psrc == NULL || gif_pdst == NULL) {
                ifnet_touch_lastchange(ifp);
-               m_freem(m);     /* free it here not in dlil_output */
+               m_freem(m);     /* free it here not in dlil_output */
                error = ENETDOWN;
                goto end;
        }
 
-       bpf_tap_out(ifp, 0, m, &sc->gif_proto, sizeof (sc->gif_proto));
+       bpf_tap_out(ifp, 0, m, &sc->gif_proto, sizeof(sc->gif_proto));
 
        GIF_LOCK(sc);
 
@@ -600,6 +618,12 @@ gif_output(
 
        /* XXX should we check if our outer source is legal? */
 
+       /*
+        * Save the length as m may be free by the output functions
+        * as they call m_pullup
+        */
+       u_int32_t bytes_out = m->m_pkthdr.len;
+
        /* dispatch to output logic based on outer AF */
        switch (sc->gif_psrc->sa_family) {
 #if INET
@@ -618,16 +642,17 @@ gif_output(
        }
 
        GIF_UNLOCK(sc);
-end:        
+end:
        if (error) {
                /* the mbuf was freed either by in_gif_output or in here */
                ifnet_stat_increment_out(ifp, 0, 0, 1);
        } else {
-               ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0);
+               ifnet_stat_increment_out(ifp, 1, bytes_out, 0);
        }
-       if (error == 0)
+       if (error == 0) {
                error = EJUSTRETURN; /* if no error, packet got sent already */
-       return (error);
+       }
+       return error;
 }
 
 /*
@@ -642,7 +667,7 @@ gif_input(
 {
        struct gif_softc *sc = ifnet_softc(ifp);
 
-       bpf_tap_in(ifp, 0, m, &sc->gif_proto, sizeof (sc->gif_proto));
+       bpf_tap_in(ifp, 0, m, &sc->gif_proto, sizeof(sc->gif_proto));
 
        /*
         * Put the packet to the network layer input queue according to the
@@ -656,23 +681,23 @@ gif_input(
         * it occurs more times than we thought, we may change the policy
         * again.
         */
-    int32_t pktlen = m->m_pkthdr.len;
+       int32_t pktlen = m->m_pkthdr.len;
        if (proto_input(protocol_family, m) != 0) {
                ifnet_stat_increment_in(ifp, 0, 0, 1);
                m_freem(m);
-    } else {
+       } else {
                ifnet_stat_increment_in(ifp, 1, pktlen, 0);
-    }
+       }
 
-       return (0);
+       return 0;
 }
 
 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
 static errno_t
 gif_ioctl(
-       ifnet_t                 ifp,
-       u_long                  cmd,
-       void                    *data)
+       ifnet_t                 ifp,
+       u_long                  cmd,
+       void                    *data)
 {
        struct gif_softc *sc  = ifnet_softc(ifp);
        struct ifreq *ifr = (struct ifreq *)data;
@@ -693,20 +718,20 @@ gif_ioctl(
        case SIOCDELMULTI:
                break;
 
-#ifdef SIOCSIFMTU /* xxx */
+#ifdef  SIOCSIFMTU /* xxx */
        case SIOCGIFMTU:
                break;
 
        case SIOCSIFMTU:
-               {
-                       u_int32_t mtu;
-                       mtu = ifr->ifr_mtu;
-                       if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
-                               return (EINVAL);
-                       }
-                       ifnet_set_mtu(ifp, mtu);
+       {
+               u_int32_t mtu;
+               mtu = ifr->ifr_mtu;
+               if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
+                       return EINVAL;
                }
-               break;
+               ifnet_set_mtu(ifp, mtu);
+       }
+       break;
 #endif /* SIOCSIFMTU */
 
        case SIOCSIFPHYADDR:
@@ -718,9 +743,9 @@ gif_ioctl(
 #if INET
                case SIOCSIFPHYADDR:
                        src = (struct sockaddr *)
-                               &(((struct in_aliasreq *)data)->ifra_addr);
+                           &(((struct in_aliasreq *)data)->ifra_addr);
                        dst = (struct sockaddr *)
-                               &(((struct in_aliasreq *)data)->ifra_dstaddr);
+                           &(((struct in_aliasreq *)data)->ifra_dstaddr);
                        break;
 #endif
 #if INET6
@@ -745,83 +770,92 @@ gif_ioctl(
                }
 
                /* sa_family must be equal */
-               if (src->sa_family != dst->sa_family)
-                       return (EINVAL);
+               if (src->sa_family != dst->sa_family) {
+                       return EINVAL;
+               }
 
                /* validate sa_len */
                switch (src->sa_family) {
 #if INET
                case AF_INET:
-                       if (src->sa_len != sizeof (struct sockaddr_in))
-                               return (EINVAL);
+                       if (src->sa_len != sizeof(struct sockaddr_in)) {
+                               return EINVAL;
+                       }
                        break;
 #endif
 #if INET6
                case AF_INET6:
-                       if (src->sa_len != sizeof (struct sockaddr_in6))
-                               return (EINVAL);
+                       if (src->sa_len != sizeof(struct sockaddr_in6)) {
+                               return EINVAL;
+                       }
                        break;
 #endif
                default:
-                       return (EAFNOSUPPORT);
+                       return EAFNOSUPPORT;
                }
                switch (dst->sa_family) {
 #if INET
                case AF_INET:
-                       if (dst->sa_len != sizeof (struct sockaddr_in))
-                               return (EINVAL);
+                       if (dst->sa_len != sizeof(struct sockaddr_in)) {
+                               return EINVAL;
+                       }
                        break;
 #endif
 #if INET6
                case AF_INET6:
-                       if (dst->sa_len != sizeof (struct sockaddr_in6))
-                               return (EINVAL);
+                       if (dst->sa_len != sizeof(struct sockaddr_in6)) {
+                               return EINVAL;
+                       }
                        break;
 #endif
                default:
-                       return (EAFNOSUPPORT);
+                       return EAFNOSUPPORT;
                }
 
                /* check sa_family looks sane for the cmd */
                switch (cmd) {
                case SIOCSIFPHYADDR:
-                       if (src->sa_family == AF_INET)
+                       if (src->sa_family == AF_INET) {
                                break;
-                       return (EAFNOSUPPORT);
+                       }
+                       return EAFNOSUPPORT;
 #if INET6
                case SIOCSIFPHYADDR_IN6_32:
                case SIOCSIFPHYADDR_IN6_64:
-                       if (src->sa_family == AF_INET6)
+                       if (src->sa_family == AF_INET6) {
                                break;
-                       return (EAFNOSUPPORT);
+                       }
+                       return EAFNOSUPPORT;
 #endif /* INET6 */
                }
 
-#define        GIF_ORDERED_LOCK(sc, sc2)       \
-       if (sc < sc2) {                 \
-               GIF_LOCK(sc);           \
-               GIF_LOCK(sc2);          \
-       } else {                        \
-               GIF_LOCK(sc2);          \
-               GIF_LOCK(sc);           \
+#define GIF_ORDERED_LOCK(sc, sc2)       \
+       if (sc < sc2) {                 \
+               GIF_LOCK(sc);           \
+               GIF_LOCK(sc2);          \
+       } else {                        \
+               GIF_LOCK(sc2);          \
+               GIF_LOCK(sc);           \
        }
 
-#define        GIF_ORDERED_UNLOCK(sc, sc2)     \
-       if (sc > sc2) {                 \
-               GIF_UNLOCK(sc);         \
-               GIF_UNLOCK(sc2);        \
-       } else {                        \
-               GIF_UNLOCK(sc2);        \
-               GIF_UNLOCK(sc);         \
+#define GIF_ORDERED_UNLOCK(sc, sc2)     \
+       if (sc > sc2) {                 \
+               GIF_UNLOCK(sc);         \
+               GIF_UNLOCK(sc2);        \
+       } else {                        \
+               GIF_UNLOCK(sc2);        \
+               GIF_UNLOCK(sc);         \
        }
 
                ifnet_head_lock_shared();
                TAILQ_FOREACH(ifp2, &ifnet_head, if_link) {
-                       if (strcmp(ifnet_name(ifp2), GIFNAME) != 0)
+                       if (strcmp(ifnet_name(ifp2), GIFNAME) != 0) {
                                continue;
+                       }
                        sc2 = ifnet_softc(ifp2);
-                       if (sc2 == sc)
+                       if (sc2 == sc) {
                                continue;
+                       }
                        /* lock sc and sc2 in increasing order of ifnet index */
                        GIF_ORDERED_LOCK(sc, sc2);
                        if (!sc2->gif_pdst || !sc2->gif_psrc) {
@@ -847,11 +881,11 @@ gif_ioctl(
 #endif
 
                        /* can't configure multiple multi-dest interfaces */
-#define        multidest(x) \
+#define multidest(x) \
        (((struct sockaddr_in *)(void *)(x))->sin_addr.s_addr == INADDR_ANY)
 #if INET6
-#define        multidest6(x) \
-       (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)      \
+#define multidest6(x) \
+       (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)      \
            (void *)(x))->sin6_addr))
 #endif
                        if (dst->sa_family == AF_INET &&
@@ -875,24 +909,26 @@ gif_ioctl(
                ifnet_head_done();
 
                GIF_LOCK(sc);
-               if (sc->gif_psrc)
-                       FREE((caddr_t)sc->gif_psrc, M_IFADDR);
+               if (sc->gif_psrc) {
+                       FREE(sc->gif_psrc, M_IFADDR);
+               }
                sa = (struct sockaddr *)_MALLOC(src->sa_len, M_IFADDR,
                    M_WAITOK);
                if (sa == NULL) {
                        GIF_UNLOCK(sc);
-                       return (ENOBUFS);
+                       return ENOBUFS;
                }
                bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
                sc->gif_psrc = sa;
 
-               if (sc->gif_pdst)
-                       FREE((caddr_t)sc->gif_pdst, M_IFADDR);
+               if (sc->gif_pdst) {
+                       FREE(sc->gif_pdst, M_IFADDR);
+               }
                sa = (struct sockaddr *)_MALLOC(dst->sa_len, M_IFADDR,
                    M_WAITOK);
                if (sa == NULL) {
                        GIF_UNLOCK(sc);
-                       return (ENOBUFS);
+                       return ENOBUFS;
                }
                bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
                sc->gif_pdst = sa;
@@ -908,11 +944,11 @@ gif_ioctl(
        case SIOCDIFPHYADDR:
                GIF_LOCK(sc);
                if (sc->gif_psrc) {
-                       FREE((caddr_t)sc->gif_psrc, M_IFADDR);
+                       FREE(sc->gif_psrc, M_IFADDR);
                        sc->gif_psrc = NULL;
                }
                if (sc->gif_pdst) {
-                       FREE((caddr_t)sc->gif_pdst, M_IFADDR);
+                       FREE(sc->gif_pdst, M_IFADDR);
                        sc->gif_pdst = NULL;
                }
                GIF_UNLOCK(sc);
@@ -935,14 +971,14 @@ gif_ioctl(
 #if INET
                case SIOCGIFPSRCADDR:
                        dst = &ifr->ifr_addr;
-                       size = sizeof (ifr->ifr_addr);
+                       size = sizeof(ifr->ifr_addr);
                        break;
 #endif /* INET */
 #if INET6
                case SIOCGIFPSRCADDR_IN6:
                        dst = (struct sockaddr *)
-                               &(((struct in6_ifreq *)data)->ifr_addr);
-                       size = sizeof (((struct in6_ifreq *)data)->ifr_addr);
+                           &(((struct in6_ifreq *)data)->ifr_addr);
+                       size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
                        break;
 #endif /* INET6 */
                default:
@@ -952,7 +988,7 @@ gif_ioctl(
                }
                if (src->sa_len > size) {
                        GIF_UNLOCK(sc);
-                       return (EINVAL);
+                       return EINVAL;
                }
                bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
                GIF_UNLOCK(sc);
@@ -973,14 +1009,14 @@ gif_ioctl(
 #if INET
                case SIOCGIFPDSTADDR:
                        dst = &ifr->ifr_addr;
-                       size = sizeof (ifr->ifr_addr);
+                       size = sizeof(ifr->ifr_addr);
                        break;
 #endif /* INET */
 #if INET6
                case SIOCGIFPDSTADDR_IN6:
                        dst = (struct sockaddr *)
-                               &(((struct in6_ifreq *)data)->ifr_addr);
-                       size = sizeof (((struct in6_ifreq *)data)->ifr_addr);
+                           &(((struct in6_ifreq *)data)->ifr_addr);
+                       size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
                        break;
 #endif /* INET6 */
                default:
@@ -990,7 +1026,7 @@ gif_ioctl(
                }
                if (src->sa_len > size) {
                        GIF_UNLOCK(sc);
-                       return (EINVAL);
+                       return EINVAL;
                }
                bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
                GIF_UNLOCK(sc);
@@ -1005,7 +1041,7 @@ gif_ioctl(
                break;
        }
 bad:
-       return (error);
+       return error;
 }
 
 static void
@@ -1013,11 +1049,11 @@ gif_delete_tunnel(struct gif_softc *sc)
 {
        GIF_LOCK_ASSERT(sc);
        if (sc->gif_psrc) {
-               FREE((caddr_t)sc->gif_psrc, M_IFADDR);
+               FREE(sc->gif_psrc, M_IFADDR);
                sc->gif_psrc = NULL;
        }
        if (sc->gif_pdst) {
-               FREE((caddr_t)sc->gif_pdst, M_IFADDR);
+               FREE(sc->gif_pdst, M_IFADDR);
                sc->gif_pdst = NULL;
        }
        ROUTE_RELEASE(&sc->gif_ro);