/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
- * @APPLE_LICENSE_HEADER_START@
- *
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
- *
* 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
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * 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,
* 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_LICENSE_HEADER_END@
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1982, 1989, 1993
*
*/
-
-
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
+#include <sys/socketvar.h>
+#include <net/dlil.h>
#include <net/if.h>
-#include <net/netisr.h>
#include <net/route.h>
#include <net/if_llc.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/ndrv.h>
+#include <net/kpi_protocol.h>
+#include <net/dlil.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
-#if INET6
#include <netinet6/nd6.h>
#include <netinet6/in6_ifattach.h>
-#endif
-
-
-
-#include <sys/socketvar.h>
-
-#include <net/dlil.h>
-
-
-#if LLC && CCITT
-extern struct ifqueue pkintrq;
-#endif
-
-
-#if BRIDGE
-#include <net/bridge.h>
-#endif
+#include <netinet6/ip6_var.h>
/* #include "vlan.h" */
#if NVLAN > 0
#include <net/if_vlan_var.h>
#endif /* NVLAN > 0 */
-static u_long lo_dlt = 0;
-static ivedonethis = 0;
-static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-
-#define IFP2AC(IFP) ((struct arpcom *)IFP)
-
-
+#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
* the ether header, which is provided separately.
*/
-int
-inet6_ether_input(m, frame_header, ifp, dl_tag, sync_ok)
- struct mbuf *m;
- char *frame_header;
- struct ifnet *ifp;
- u_long dl_tag;
- int sync_ok;
-
+static errno_t
+ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
+ mbuf_t packet, char *header)
{
- register struct ether_header *eh = (struct ether_header *) frame_header;
- register struct ifqueue *inq=0;
- u_short ether_type;
- int s;
- u_int16_t ptype = -1;
- unsigned char buf[18];
-
-
-
- if ((ifp->if_flags & IFF_UP) == 0) {
- m_freem(m);
- return EJUSTRETURN;
- }
-
- ifp->if_lastchange = time;
+#pragma unused(ifp, protocol)
+ struct ether_header *eh = (struct ether_header *)(void *)header;
+ u_int16_t ether_type;
+
+ bcopy(&eh->ether_type, ðer_type, sizeof(ether_type));
+
+ if (ether_type == htons(ETHERTYPE_IPV6)) {
+ struct ifnet *mifp;
+ /*
+ * Trust the ifp in the mbuf, rather than ifproto's
+ * since the packet could have been injected via
+ * a dlil_input_packet_list() using an ifp that is
+ * different than the one where the packet really
+ * came from.
+ */
+ mifp = mbuf_pkthdr_rcvif(packet);
+
+ /* Update L2 reachability record, if present (and not bcast) */
+ if (bcmp(eh->ether_shost, etherbroadcastaddr,
+ ETHER_ADDR_LEN) != 0) {
+ nd6_llreach_set_reachable(mifp, eh->ether_shost,
+ ETHER_ADDR_LEN);
+ }
+
+ /* 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);
+ }
- if (eh->ether_dhost[0] & 1) {
- if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
- sizeof(etherbroadcastaddr)) == 0)
- m->m_flags |= M_BCAST;
- else
- m->m_flags |= M_MCAST;
- }
- if (m->m_flags & (M_BCAST|M_MCAST))
- ifp->if_imcasts++;
+ return EJUSTRETURN;
+}
- ether_type = ntohs(eh->ether_type);
+static errno_t
+ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
+ mbuf_t *m0, const struct sockaddr *dst_netaddr, void *route,
+ char *type, char *edst)
+{
+#pragma unused(protocol_family)
+ errno_t result;
+ struct sockaddr_dl sdl = {};
+ struct mbuf *m = *m0;
+ /*
+ * Tell ether_frameout it's ok to loop packet if necessary
+ */
+ m->m_flags |= M_LOOP;
- switch (ether_type) {
+ result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6 *)
+ (uintptr_t)(size_t)dst_netaddr, &sdl, sizeof(sdl), route, *m0);
- case ETHERTYPE_IPV6:
- schednetisr(NETISR_IPV6);
- inq = &ip6intrq;
- break;
+ if (result == 0) {
+ u_int16_t ethertype_ipv6 = htons(ETHERTYPE_IPV6);
- default: {
- return ENOENT;
+ bcopy(ðertype_ipv6, type, sizeof(ethertype_ipv6));
+ bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
}
- }
-
- if (inq == 0)
- return ENOENT;
-
- s = splimp();
- if (IF_QFULL(inq)) {
- IF_DROP(inq);
- m_freem(m);
- splx(s);
- return EJUSTRETURN;
- } else
- IF_ENQUEUE(inq, m);
- splx(s);
-
- return 0;
+ return result;
}
-
-
-
-int
-inet6_ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag )
- struct ifnet *ifp;
- struct mbuf **m0;
- struct sockaddr *dst_netaddr;
- caddr_t route;
- char *type;
- char *edst;
- u_long dl_tag;
+static int
+ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
+ struct sockaddr_dl *out_ll, size_t ll_len)
{
- struct rtentry *rt0 = (struct rtentry *) route;
- int s;
- register struct mbuf *m = *m0;
- register struct rtentry *rt;
- register struct ether_header *eh;
- int hlen; /* link layer header lenght */
- struct arpcom *ac = IFP2AC(ifp);
-
-
-
- if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
- return ENETDOWN;
-
- rt = rt0;
- if (rt) {
- if ((rt->rt_flags & RTF_UP) == 0) {
- rt0 = rt = rtalloc1(dst_netaddr, 1, 0UL);
- if (rt0)
- rtunref(rt);
- else
- return EHOSTUNREACH;
- }
+ static const size_t minsize =
+ offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
+ const struct sockaddr_in6 *sin6 =
+ (const struct sockaddr_in6 *)(uintptr_t)(size_t)proto_addr;
- if (rt->rt_flags & RTF_GATEWAY) {
- if (rt->rt_gwroute == 0)
- goto lookup;
- if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
- rtfree(rt); rt = rt0;
- lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
- 0UL);
- if ((rt = rt->rt_gwroute) == 0)
- return (EHOSTUNREACH);
- }
+ if (proto_addr->sa_family != AF_INET6) {
+ return EAFNOSUPPORT;
}
-
- if (rt->rt_flags & RTF_REJECT)
- if (rt->rt_rmx.rmx_expire == 0 ||
- time_second < rt->rt_rmx.rmx_expire)
- return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
- }
-
- hlen = ETHER_HDR_LEN;
-
- /*
- * Tell ether_frameout it's ok to loop packet unless negated below.
- */
- m->m_flags |= M_LOOP;
-
- switch (dst_netaddr->sa_family) {
-
-
- case AF_INET6:
- if (!nd6_storelladdr(&ac->ac_if, rt, m, dst_netaddr, (u_char *)edst)) {
- /* this must be impossible, so we bark */
- printf("nd6_storelladdr failed\n");
- return(EADDRNOTAVAIL); /* dlil_output will free the mbuf */
- }
- *(u_short *)type = htons(ETHERTYPE_IPV6);
- break;
-
- default:
- printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
- dst_netaddr->sa_family);
+ if (proto_addr->sa_len < sizeof(struct sockaddr_in6)) {
+ return EINVAL;
+ }
- /* dlil_output will free the mbuf */
- return EAFNOSUPPORT;
- }
+ if (ll_len < minsize) {
+ return EMSGSIZE;
+ }
- return (0);
+ bzero(out_ll, minsize);
+ out_ll->sdl_len = minsize;
+ out_ll->sdl_family = AF_LINK;
+ out_ll->sdl_index = ifp->if_index;
+ out_ll->sdl_type = IFT_ETHER;
+ out_ll->sdl_nlen = 0;
+ out_ll->sdl_alen = ETHER_ADDR_LEN;
+ out_ll->sdl_slen = 0;
+ ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
+
+ return 0;
}
-
-int
-ether_inet6_prmod_ioctl(dl_tag, ifp, command, data)
- u_long dl_tag;
- struct ifnet *ifp;
- int command;
- caddr_t data;
+static errno_t
+ether_inet6_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
+ u_long command, void *data)
{
- struct ifaddr *ifa = (struct ifaddr *) data;
- struct ifreq *ifr = (struct ifreq *) data;
- struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data;
- int error = 0;
- boolean_t funnel_state;
- struct arpcom *ac = (struct arpcom *) ifp;
- struct sockaddr_dl *sdl;
- struct sockaddr_in *sin;
- struct sockaddr_in6 *sin6;
-
- u_char *e_addr;
-
-
- switch (command) {
- case SIOCRSLVMULTI: {
- switch(rsreq->sa->sa_family) {
-
- case AF_INET6:
- sin6 = (struct sockaddr_in6 *)rsreq->sa;
- if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
- /*
- * An IP6 address of 0 means listen to all
- * of the Ethernet multicast address used for IP6.
- * (This is used for multicast routers.)
- */
- ifp->if_flags |= IFF_ALLMULTI;
- *rsreq->llsa = 0;
- return 0;
- }
- MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
- M_WAITOK);
- sdl->sdl_len = sizeof *sdl;
- sdl->sdl_family = AF_LINK;
- sdl->sdl_index = ifp->if_index;
- sdl->sdl_type = IFT_ETHER;
- sdl->sdl_nlen = 0;
- sdl->sdl_alen = ETHER_ADDR_LEN;
- sdl->sdl_slen = 0;
- e_addr = LLADDR(sdl);
- ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
-#ifndef __APPLE__
- printf("ether_resolvemulti AF_INET6 Adding %x:%x:%x:%x:%x:%x\n",
- e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]);
-#endif
- *rsreq->llsa = (struct sockaddr *)sdl;
- return 0;
-
- default:
- /*
- * Well, the text isn't quite right, but it's the name
- * that counts...
+#pragma unused(protocol_family)
+ int error = 0;
+
+ switch (command) {
+ case SIOCSIFADDR: /* struct ifaddr pointer */
+ /*
+ * Note: caller of ifnet_ioctl() passes in pointer to
+ * struct ifaddr as parameter to SIOCSIFADDR, for legacy
+ * reasons.
*/
- return EAFNOSUPPORT;
+ if ((ifp->if_flags & IFF_RUNNING) == 0) {
+ ifnet_set_flags(ifp, IFF_UP, IFF_UP);
+ ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
+ }
+ break;
+
+ 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;
}
- }
- case SIOCSIFADDR:
- if ((ifp->if_flags & IFF_RUNNING) == 0) {
- ifp->if_flags |= IFF_UP;
- dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
- }
-
- break;
-
- case SIOCGIFADDR:
- {
- struct sockaddr *sa;
-
- sa = (struct sockaddr *) & ifr->ifr_data;
- bcopy(IFP2AC(ifp)->ac_enaddr,
- (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
- }
- break;
-
- case SIOCSIFMTU:
- /*
- * IOKit IONetworkFamily will set the right MTU according to the driver
- */
-
- return (0);
-
- default:
- return EOPNOTSUPP;
- }
-
- return (error);
+ default:
+ error = EOPNOTSUPP;
+ break;
+ }
+ return error;
}
-
-
-
-
-int ether_attach_inet6(struct ifnet *ifp, u_long *dl_tag)
+errno_t
+ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
{
- struct dlil_proto_reg_str reg;
- struct dlil_demux_desc desc;
- u_short en_6native=ETHERTYPE_IPV6;
- int stat;
- int i;
-
-
- stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, dl_tag);
- if (stat == 0)
- return stat;
-
- TAILQ_INIT(®.demux_desc_head);
- desc.type = DLIL_DESC_RAW;
- desc.variants.bitmask.proto_id_length = 0;
- desc.variants.bitmask.proto_id = 0;
- desc.variants.bitmask.proto_id_mask = 0;
- desc.native_type = (char *) &en_6native;
- TAILQ_INSERT_TAIL(®.demux_desc_head, &desc, next);
- reg.interface_family = ifp->if_family;
- reg.unit_number = ifp->if_unit;
- reg.input = inet6_ether_input;
- reg.pre_output = inet6_ether_pre_output;
- reg.event = 0;
- reg.offer = 0;
- reg.ioctl = ether_inet6_prmod_ioctl;
- reg.default_proto = 0;
- reg.protocol_family = PF_INET6;
-
- stat = dlil_attach_protocol(®, dl_tag);
- if (stat) {
- printf("WARNING: ether_attach_inet6 can't attach ip to interface\n");
- }
-
- return stat;
+#pragma unused(protocol_family)
+ struct ifnet_attach_proto_param proto;
+ struct ifnet_demux_desc demux[1];
+ u_short en_6native = htons(ETHERTYPE_IPV6);
+ errno_t error;
+
+ bzero(&proto, sizeof(proto));
+ demux[0].type = DLIL_DESC_ETYPE2;
+ demux[0].data = &en_6native;
+ demux[0].datalen = sizeof(en_6native);
+ proto.demux_list = demux;
+ proto.demux_count = 1;
+ proto.input = ether_inet6_input;
+ proto.pre_output = ether_inet6_pre_output;
+ proto.ioctl = ether_inet6_prmod_ioctl;
+ 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\n", __func__,
+ if_name(ifp));
+ }
+
+ return error;
}
-int ether_detach_inet6(struct ifnet *ifp, u_long dl_tag)
+void
+ether_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
{
- int stat;
-
- stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, &dl_tag);
- if (stat == 0) {
- stat = dlil_detach_protocol(dl_tag);
- if (stat) {
- printf("WARNING: ether_detach_inet6 can't detach ip6 from interface\n");
- }
- }
- return stat;
+ (void) ifnet_detach_protocol(ifp, protocol_family);
}
-