]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/ether_inet6_pr_module.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / ether_inet6_pr_module.c
index e8411dec64657da761378d4e04e720506fc9f86f..c332fd523682b6460644a31b2c85498f23548ed5 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -22,7 +22,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
@@ -68,8 +68,8 @@
 #include <sys/sockio.h>
 #include <sys/sysctl.h>
 #include <sys/socketvar.h>
-#include <kern/lock.h>
 
+#include <net/dlil.h>
 #include <net/if.h>
 #include <net/route.h>
 #include <net/if_llc.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
 
-#if INET6
 #include <netinet6/nd6.h>
 #include <netinet6/in6_ifattach.h>
-#endif
-
-#if LLC && CCITT
-extern struct ifqueue pkintrq;
-#endif
+#include <netinet6/ip6_var.h>
 
 /* #include "vlan.h" */
 #if NVLAN > 0
@@ -101,6 +96,9 @@ extern struct ifqueue pkintrq;
 
 #include <net/ether_if_module.h>
 
+static const u_char etherip6allnodes[ETHER_ADDR_LEN] =
+{ 0x33, 0x33, 0, 0, 0, 1 };
+
 /*
  * Process a received Ethernet packet;
  * the packet is in the mbuf chain m without
@@ -111,9 +109,12 @@ ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
     mbuf_t packet, char *header)
 {
 #pragma unused(ifp, protocol)
-       struct ether_header *eh = (struct ether_header *)header;
+       struct ether_header *eh = (struct ether_header *)(void *)header;
+       u_int16_t ether_type;
+
+       bcopy(&eh->ether_type, &ether_type, sizeof(ether_type));
 
-       if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
+       if (ether_type == htons(ETHERTYPE_IPV6)) {
                struct ifnet *mifp;
                /*
                 * Trust the ifp in the mbuf, rather than ifproto's
@@ -131,13 +132,26 @@ ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
                            ETHER_ADDR_LEN);
                }
 
-               if (proto_input(protocol, packet) != 0)
+               /* Save the Ethernet source address for all-nodes multicasts */
+               if (!bcmp(eh->ether_dhost, etherip6allnodes, ETHER_ADDR_LEN)) {
+                       struct ip6aux *ip6a;
+
+                       ip6a = ip6_addaux(packet);
+                       if (ip6a) {
+                               ip6a->ip6a_flags |= IP6A_HASEEN;
+                               bcopy(eh->ether_shost, ip6a->ip6a_ehsrc,
+                                   ETHER_ADDR_LEN);
+                       }
+               }
+
+               if (proto_input(protocol, packet) != 0) {
                        m_freem(packet);
+               }
        } else {
                m_freem(packet);
        }
 
-       return (EJUSTRETURN);
+       return EJUSTRETURN;
 }
 
 static errno_t
@@ -146,8 +160,8 @@ ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
     char *type, char *edst)
 {
 #pragma unused(protocol_family)
-       errno_t result;
-       struct sockaddr_dl sdl;
+       errno_t result;
+       struct sockaddr_dl sdl = {};
        struct mbuf *m = *m0;
 
        /*
@@ -155,15 +169,17 @@ ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
         */
        m->m_flags |= M_LOOP;
 
-       result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6 *)dst_netaddr,
-           &sdl, sizeof (sdl), route, *m0);
+       result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6 *)
+           (uintptr_t)(size_t)dst_netaddr, &sdl, sizeof(sdl), route, *m0);
 
        if (result == 0) {
-               *(u_int16_t *)type = htons(ETHERTYPE_IPV6);
+               u_int16_t ethertype_ipv6 = htons(ETHERTYPE_IPV6);
+
+               bcopy(&ethertype_ipv6, type, sizeof(ethertype_ipv6));
                bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
        }
 
-       return (result);
+       return result;
 }
 
 static int
@@ -173,16 +189,19 @@ ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
        static const size_t minsize =
            offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
        const struct sockaddr_in6 *sin6 =
-           (const struct sockaddr_in6 *)proto_addr;
+           (const struct sockaddr_in6 *)(uintptr_t)(size_t)proto_addr;
 
-       if (proto_addr->sa_family != AF_INET6)
-               return (EAFNOSUPPORT);
+       if (proto_addr->sa_family != AF_INET6) {
+               return EAFNOSUPPORT;
+       }
 
-       if (proto_addr->sa_len < sizeof (struct sockaddr_in6))
-               return (EINVAL);
+       if (proto_addr->sa_len < sizeof(struct sockaddr_in6)) {
+               return EINVAL;
+       }
 
-       if (ll_len < minsize)
-               return (EMSGSIZE);
+       if (ll_len < minsize) {
+               return EMSGSIZE;
+       }
 
        bzero(out_ll, minsize);
        out_ll->sdl_len = minsize;
@@ -194,7 +213,7 @@ ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
        out_ll->sdl_slen = 0;
        ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
 
-       return (0);
+       return 0;
 }
 
 static errno_t
@@ -202,42 +221,48 @@ ether_inet6_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
     u_long command, void *data)
 {
 #pragma unused(protocol_family)
-       struct ifreq *ifr = (struct ifreq *)data;
        int error = 0;
 
        switch (command) {
-       case SIOCSIFADDR:
+       case SIOCSIFADDR:               /* struct ifaddr pointer */
+               /*
+                * Note: caller of ifnet_ioctl() passes in pointer to
+                * struct ifaddr as parameter to SIOCSIFADDR, for legacy
+                * reasons.
+                */
                if ((ifp->if_flags & IFF_RUNNING) == 0) {
                        ifnet_set_flags(ifp, IFF_UP, IFF_UP);
                        ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
                }
                break;
 
-       case SIOCGIFADDR:
-               (void) ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data,
-                   ETHER_ADDR_LEN);
+       case SIOCGIFADDR: {             /* struct ifreq */
+               struct ifreq *ifr = (struct ifreq *)(void *)data;
+               (void) ifnet_guarded_lladdr_copy_bytes(ifp,
+                   ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
                break;
+       }
 
        default:
                error = EOPNOTSUPP;
                break;
        }
-       return (error);
+       return error;
 }
 
 errno_t
 ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
 {
 #pragma unused(protocol_family)
-       struct ifnet_attach_proto_param proto;
+       struct ifnet_attach_proto_param proto;
        struct ifnet_demux_desc demux[1];
        u_short en_6native = htons(ETHERTYPE_IPV6);
-       errno_t error;
+       errno_t error;
 
-       bzero(&proto, sizeof (proto));
+       bzero(&proto, sizeof(proto));
        demux[0].type = DLIL_DESC_ETYPE2;
        demux[0].data = &en_6native;
-       demux[0].datalen = sizeof (en_6native);
+       demux[0].datalen = sizeof(en_6native);
        proto.demux_list = demux;
        proto.demux_count = 1;
        proto.input = ether_inet6_input;
@@ -246,11 +271,11 @@ ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
        proto.resolve = ether_inet6_resolve_multi;
        error = ifnet_attach_protocol(ifp, protocol_family, &proto);
        if (error && error != EEXIST) {
-               printf("WARNING: %s can't attach ipv6 to %s%d\n", __func__,
-                   ifp->if_name, ifp->if_unit);
+               printf("WARNING: %s can't attach ipv6 to %s\n", __func__,
+                   if_name(ifp));
        }
 
-       return (error);
+       return error;
 }
 
 void