/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2012 Apple Inc. All rights reserved.
*
- * @APPLE_LICENSE_HEADER_START@
- *
- * Copyright (c) 1999-2003 Apple Computer, 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
- * 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
* 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) 1999 Apple Computer, Inc.
- *
- * Data Link Inteface Layer
- * Author: Ted Walker
+ * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
+ * support for mandatory and extensible security protections. This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
*/
-
-
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
+#include <sys/domain.h>
+#include <sys/user.h>
+#include <sys/random.h>
#include <net/if_dl.h>
#include <net/if.h>
+#include <net/route.h>
#include <net/if_var.h>
#include <net/dlil.h>
+#include <net/if_arp.h>
#include <sys/kern_event.h>
#include <sys/kdebug.h>
-#include <string.h>
+#include <sys/mcache.h>
+#include <kern/assert.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/sched_prim.h>
+#include <kern/locks.h>
+#include <kern/zalloc.h>
+#include <net/kpi_protocol.h>
-#include <net/netisr.h>
#include <net/if_types.h>
+#include <net/if_llreach.h>
+#include <net/kpi_interfacefilter.h>
+
+#if INET
+#include <netinet/in_var.h>
+#include <netinet/igmp_var.h>
+#endif /* INET */
+
+#if INET6
+#include <netinet6/in6_var.h>
+#include <netinet6/nd6.h>
+#include <netinet6/mld6_var.h>
+#endif /* INET6 */
+
+#if NETAT
+#include <netat/at_var.h>
+#endif /* NETAT */
+
+#include <libkern/OSAtomic.h>
#include <machine/machine_routines.h>
+#include <mach/thread_act.h>
+#include <mach/sdt.h>
+
+#if CONFIG_MACF_NET
+#include <security/mac_framework.h>
+#endif /* MAC_NET */
+
+#if PF
+#include <net/pfvar.h>
+#endif /* PF */
+
#define DBG_LAYER_BEG DLILDBG_CODE(DBG_DLIL_STATIC, 0)
#define DBG_LAYER_END DLILDBG_CODE(DBG_DLIL_STATIC, 2)
#define DBG_FNC_DLIL_INPUT DLILDBG_CODE(DBG_DLIL_STATIC, (1 << 8))
#define DBG_FNC_DLIL_IFOUT DLILDBG_CODE(DBG_DLIL_STATIC, (3 << 8))
-#define MAX_DL_TAGS 16
-#define MAX_DLIL_FILTERS 16
#define MAX_FRAME_TYPE_SIZE 4 /* LONGWORDS */
#define MAX_LINKADDR 4 /* LONGWORDS */
#define M_NKE M_IFADDR
-#define PFILT(x) ((struct dlil_filterq_entry *) (x))->variants.pr_filter
-#define IFILT(x) ((struct dlil_filterq_entry *) (x))->variants.if_filter
+#if 1
+#define DLIL_PRINTF printf
+#else
+#define DLIL_PRINTF kprintf
+#endif
-struct dl_tag_str {
- struct ifnet *ifp;
- struct if_proto *proto;
- struct dlil_filterq_head *pr_flt_head;
-};
+#define _CASSERT(x) \
+ switch (0) { case 0: case (x): ; }
+#define IF_DATA_REQUIRE_ALIGNED_64(f) \
+ _CASSERT(!(offsetof(struct if_data_internal, f) % sizeof (u_int64_t)))
-struct dlil_ifnet {
- /* ifnet and drvr_ext are used by the stack and drivers
- drvr_ext extends the public ifnet and must follow dl_if */
- struct ifnet dl_if; /* public ifnet */
- void *drvr_ext[4]; /* driver reserved (e.g arpcom extension for enet) */
-
- /* dlil private fields */
- TAILQ_ENTRY(dlil_ifnet) dl_if_link; /* dlil_ifnet are link together */
- /* it is not the ifnet list */
- void *if_uniqueid; /* unique id identifying the interface */
- size_t if_uniqueid_len;/* length of the unique id */
- char if_namestorage[IFNAMSIZ]; /* interface name storage for detached interfaces */
+#define IFNET_IF_DATA_REQUIRE_ALIGNED_64(f) \
+ _CASSERT(!(offsetof(struct ifnet, if_data.f) % sizeof (u_int64_t)))
+
+#define IFNET_IF_TC_REQUIRE_ALIGNED_64(f) \
+ _CASSERT(!(offsetof(struct ifnet, if_tc.f) % sizeof (u_int64_t)))
+
+enum {
+ kProtoKPI_v1 = 1,
+ kProtoKPI_v2 = 2
};
-struct dlil_stats_str {
- int inject_pr_in1;
- int inject_pr_in2;
- int inject_pr_out1;
- int inject_pr_out2;
- int inject_if_in1;
- int inject_if_in2;
- int inject_if_out1;
- int inject_if_out2;
+/*
+ * List of if_proto structures in if_proto_hash[] is protected by
+ * the ifnet lock. The rest of the fields are initialized at protocol
+ * attach time and never change, thus no lock required as long as
+ * a reference to it is valid, via if_proto_ref().
+ */
+struct if_proto {
+ SLIST_ENTRY(if_proto) next_hash;
+ u_int32_t refcount;
+ u_int32_t detached;
+ struct ifnet *ifp;
+ protocol_family_t protocol_family;
+ int proto_kpi;
+ union {
+ struct {
+ proto_media_input input;
+ proto_media_preout pre_output;
+ proto_media_event event;
+ proto_media_ioctl ioctl;
+ proto_media_detached detached;
+ proto_media_resolve_multi resolve_multi;
+ proto_media_send_arp send_arp;
+ } v1;
+ struct {
+ proto_media_input_v2 input;
+ proto_media_preout pre_output;
+ proto_media_event event;
+ proto_media_ioctl ioctl;
+ proto_media_detached detached;
+ proto_media_resolve_multi resolve_multi;
+ proto_media_send_arp send_arp;
+ } v2;
+ } kpi;
};
+SLIST_HEAD(proto_hash_entry, if_proto);
+
+#define DLIL_SDLMAXLEN 64
+#define DLIL_SDLDATALEN \
+ (DLIL_SDLMAXLEN - offsetof(struct sockaddr_dl, sdl_data[0]))
-struct dlil_filter_id_str {
- int type;
- struct dlil_filterq_head *head;
- struct dlil_filterq_entry *filter_ptr;
- struct ifnet *ifp;
- struct if_proto *proto;
+struct dlil_ifnet {
+ struct ifnet dl_if; /* public ifnet */
+ /*
+ * dlil private fields, protected by dl_if_lock
+ */
+ decl_lck_mtx_data(, dl_if_lock);
+ TAILQ_ENTRY(dlil_ifnet) dl_if_link; /* dlil_ifnet link */
+ u_int32_t dl_if_flags; /* flags (below) */
+ u_int32_t dl_if_refcnt; /* refcnt */
+ void (*dl_if_trace)(struct dlil_ifnet *, int); /* ref trace callback */
+ void *dl_if_uniqueid; /* unique interface id */
+ size_t dl_if_uniqueid_len; /* length of the unique id */
+ char dl_if_namestorage[IFNAMSIZ]; /* interface name storage */
+ struct {
+ struct ifaddr ifa; /* lladdr ifa */
+ u_int8_t asdl[DLIL_SDLMAXLEN]; /* addr storage */
+ u_int8_t msdl[DLIL_SDLMAXLEN]; /* mask storage */
+ } dl_if_lladdr;
+ ctrace_t dl_if_attach; /* attach PC stacktrace */
+ ctrace_t dl_if_detach; /* detach PC stacktrace */
};
+/* Values for dl_if_flags (private to DLIL) */
+#define DLIF_INUSE 0x1 /* DLIL ifnet recycler, ifnet in use */
+#define DLIF_REUSE 0x2 /* DLIL ifnet recycles, ifnet is not new */
+#define DLIF_DEBUG 0x4 /* has debugging info */
+#define IF_REF_TRACE_HIST_SIZE 8 /* size of ref trace history */
-struct if_family_str {
- TAILQ_ENTRY(if_family_str) if_fam_next;
- u_long if_family;
- int refcnt;
- int flags;
+/* For gdb */
+__private_extern__ unsigned int if_ref_trace_hist_size = IF_REF_TRACE_HIST_SIZE;
-#define DLIL_SHUTDOWN 1
+struct dlil_ifnet_dbg {
+ struct dlil_ifnet dldbg_dlif; /* dlil_ifnet */
+ u_int16_t dldbg_if_refhold_cnt; /* # ifnet references */
+ u_int16_t dldbg_if_refrele_cnt; /* # ifnet releases */
+ /*
+ * Circular lists of ifnet_{reference,release} callers.
+ */
+ ctrace_t dldbg_if_refhold[IF_REF_TRACE_HIST_SIZE];
+ ctrace_t dldbg_if_refrele[IF_REF_TRACE_HIST_SIZE];
+};
- int (*add_if)(struct ifnet *ifp);
- int (*del_if)(struct ifnet *ifp);
- int (*init_if)(struct ifnet *ifp);
- int (*add_proto)(struct ddesc_head_str *demux_desc_head,
- struct if_proto *proto, u_long dl_tag);
- int (*del_proto)(struct if_proto *proto, u_long dl_tag);
- int (*ifmod_ioctl)(struct ifnet *ifp, u_long command, caddr_t data);
- int (*shutdown)();
+#define DLIL_TO_IFP(s) (&s->dl_if)
+#define IFP_TO_DLIL(s) ((struct dlil_ifnet *)s)
+
+struct ifnet_filter {
+ TAILQ_ENTRY(ifnet_filter) filt_next;
+ u_int32_t filt_skip;
+ ifnet_t filt_ifp;
+ const char *filt_name;
+ void *filt_cookie;
+ protocol_family_t filt_protocol;
+ iff_input_func filt_input;
+ iff_output_func filt_output;
+ iff_event_func filt_event;
+ iff_ioctl_func filt_ioctl;
+ iff_detached_func filt_detached;
};
+struct proto_input_entry;
+static TAILQ_HEAD(, dlil_ifnet) dlil_ifnet_head;
+static lck_grp_t *dlil_lock_group;
+lck_grp_t *ifnet_lock_group;
+static lck_grp_t *ifnet_head_lock_group;
+lck_attr_t *ifnet_lock_attr;
+decl_lck_rw_data(static, ifnet_head_lock);
+decl_lck_mtx_data(static, dlil_ifnet_lock);
+u_int32_t dlil_filter_count = 0;
+extern u_int32_t ipv4_ll_arp_aware;
-struct dlil_stats_str dlil_stats;
+#if DEBUG
+static unsigned int ifnet_debug = 1; /* debugging (enabled) */
+#else
+static unsigned int ifnet_debug; /* debugging (disabled) */
+#endif /* !DEBUG */
+static unsigned int dlif_size; /* size of dlil_ifnet to allocate */
+static unsigned int dlif_bufsize; /* size of dlif_size + headroom */
+static struct zone *dlif_zone; /* zone for dlil_ifnet */
-static
-struct dlil_filter_id_str *dlil_filters;
+#define DLIF_ZONE_MAX 64 /* maximum elements in zone */
+#define DLIF_ZONE_NAME "ifnet" /* zone name */
-static
-struct dl_tag_str *dl_tag_array;
+static unsigned int dlif_filt_size; /* size of ifnet_filter */
+static struct zone *dlif_filt_zone; /* zone for ifnet_filter */
-static
-TAILQ_HEAD(, dlil_ifnet) dlil_ifnet_head;
+#define DLIF_FILT_ZONE_MAX 8 /* maximum elements in zone */
+#define DLIF_FILT_ZONE_NAME "ifnet_filter" /* zone name */
-static
-TAILQ_HEAD(, if_family_str) if_family_head;
+static unsigned int dlif_inp_size; /* size of dlil_threading_info */
+static struct zone *dlif_inp_zone; /* zone for dlil_threading_info */
-static ifnet_inited = 0;
-static u_long dl_tag_nb = 0;
-static u_long dlil_filters_nb = 0;
+#define DLIF_INP_ZONE_MAX DLIF_ZONE_MAX /* maximum elements in zone */
+#define DLIF_INP_ZONE_NAME "ifnet_thread" /* zone name */
-int dlil_initialized = 0;
-decl_simple_lock_data(, dlil_input_lock)
-int dlil_input_thread_wakeup = 0;
-int dlil_expand_mcl;
-static struct mbuf *dlil_input_mbuf_head = NULL;
-static struct mbuf *dlil_input_mbuf_tail = NULL;
-#if NLOOP > 1
-#error dlil_input() needs to be revised to support more than on loopback interface
-#endif
-static struct mbuf *dlil_input_loop_head = NULL;
-static struct mbuf *dlil_input_loop_tail = NULL;
+static unsigned int dlif_phash_size; /* size of ifnet proto hash table */
+static struct zone *dlif_phash_zone; /* zone for ifnet proto hash table */
-static void dlil_input_thread(void);
-extern void run_netisr(void);
-extern void bpfdetach(struct ifnet*);
+#define DLIF_PHASH_ZONE_MAX DLIF_ZONE_MAX /* maximum elements in zone */
+#define DLIF_PHASH_ZONE_NAME "ifnet_proto_hash" /* zone name */
+static unsigned int dlif_proto_size; /* size of if_proto */
+static struct zone *dlif_proto_zone; /* zone for if_proto */
+
+#define DLIF_PROTO_ZONE_MAX (DLIF_ZONE_MAX*2) /* maximum elements in zone */
+#define DLIF_PROTO_ZONE_NAME "ifnet_proto" /* zone name */
/*
- * Internal functions.
+ * Updating this variable should be done by first acquiring the global
+ * radix node head (rnh_lock), in tandem with settting/clearing the
+ * PR_AGGDRAIN for routedomain.
*/
+u_int32_t ifnet_aggressive_drainers;
+static u_int32_t net_rtref;
+
+static struct dlil_threading_info dlil_lo_thread;
+__private_extern__ struct dlil_threading_info *dlil_lo_thread_ptr = &dlil_lo_thread;
+
+static struct mbuf *dlil_lo_input_mbuf_head = NULL;
+static struct mbuf *dlil_lo_input_mbuf_tail = NULL;
+
+static int dlil_event_internal(struct ifnet *ifp, struct kev_msg *msg);
+static int dlil_detach_filter_internal(interface_filter_t filter, int detached);
+static void dlil_if_trace(struct dlil_ifnet *, int);
+static void if_proto_ref(struct if_proto *);
+static void if_proto_free(struct if_proto *);
+static struct if_proto *find_attached_proto(struct ifnet *, u_int32_t);
+static int dlil_ifp_proto_count(struct ifnet *);
+static void if_flt_monitor_busy(struct ifnet *);
+static void if_flt_monitor_unbusy(struct ifnet *);
+static void if_flt_monitor_enter(struct ifnet *);
+static void if_flt_monitor_leave(struct ifnet *);
+static int dlil_interface_filters_input(struct ifnet *, struct mbuf **,
+ char **, protocol_family_t);
+static int dlil_interface_filters_output(struct ifnet *, struct mbuf **,
+ protocol_family_t);
+static struct ifaddr *dlil_alloc_lladdr(struct ifnet *,
+ const struct sockaddr_dl *);
+static int ifnet_lookup(struct ifnet *);
+static void if_purgeaddrs(struct ifnet *);
+
+static errno_t ifproto_media_input_v1(struct ifnet *, protocol_family_t,
+ struct mbuf *, char *);
+static errno_t ifproto_media_input_v2(struct ifnet *, protocol_family_t,
+ struct mbuf *);
+static errno_t ifproto_media_preout(struct ifnet *, protocol_family_t,
+ mbuf_t *, const struct sockaddr *, void *, char *, char *);
+static void ifproto_media_event(struct ifnet *, protocol_family_t,
+ const struct kev_msg *);
+static errno_t ifproto_media_ioctl(struct ifnet *, protocol_family_t,
+ unsigned long, void *);
+static errno_t ifproto_media_resolve_multi(ifnet_t, const struct sockaddr *,
+ struct sockaddr_dl *, size_t);
+static errno_t ifproto_media_send_arp(struct ifnet *, u_short,
+ const struct sockaddr_dl *, const struct sockaddr *,
+ const struct sockaddr_dl *, const struct sockaddr *);
+
+static errno_t ifp_if_output(struct ifnet *, struct mbuf *);
+static errno_t ifp_if_demux(struct ifnet *, struct mbuf *, char *,
+ protocol_family_t *);
+static errno_t ifp_if_add_proto(struct ifnet *, protocol_family_t,
+ const struct ifnet_demux_desc *, u_int32_t);
+static errno_t ifp_if_del_proto(struct ifnet *, protocol_family_t);
+static errno_t ifp_if_check_multi(struct ifnet *, const struct sockaddr *);
+static errno_t ifp_if_framer(struct ifnet *, struct mbuf **,
+ const struct sockaddr *, const char *, const char *);
+static errno_t ifp_if_ioctl(struct ifnet *, unsigned long, void *);
+static errno_t ifp_if_set_bpf_tap(struct ifnet *, bpf_tap_mode, bpf_packet_func);
+static void ifp_if_free(struct ifnet *);
+static void ifp_if_event(struct ifnet *, const struct kev_msg *);
+
+static void dlil_input_thread_func(struct dlil_threading_info *inpthread);
+static int dlil_create_input_thread(ifnet_t, struct dlil_threading_info *);
+
+static void ifnet_delayed_thread_func(void);
+static void ifnet_detach_final(struct ifnet *);
+static void ifnet_detaching_enqueue(struct ifnet *);
+static struct ifnet *ifnet_detaching_dequeue(void);
+
+static void ifp_src_route_copyout(struct ifnet *, struct route *);
+static void ifp_src_route_copyin(struct ifnet *, struct route *);
+#if INET6
+static void ifp_src_route6_copyout(struct ifnet *, struct route_in6 *);
+static void ifp_src_route6_copyin(struct ifnet *, struct route_in6 *);
+#endif /* INET6 */
+
+/* The following are protected by dlil_ifnet_lock */
+static TAILQ_HEAD(, ifnet) ifnet_detaching_head;
+static u_int32_t ifnet_detaching_cnt;
+static void *ifnet_delayed_run; /* wait channel for detaching thread */
-static
-struct if_family_str *find_family_module(u_long if_family)
-{
- struct if_family_str *mod = NULL;
+extern void bpfdetach(struct ifnet*);
+extern void proto_input_run(void);
+
+__private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *);
+
+#if DEBUG
+static int dlil_verbose = 1;
+#else
+static int dlil_verbose = 0;
+#endif /* DEBUG */
+static int dlil_multithreaded_input = 1;
+static int cur_dlil_input_threads = 0;
+#if IFNET_INPUT_SANITY_CHK
+static int dlil_lo_input_mbuf_count = 0;
+/* sanity checking of input packet lists received */
+static int dlil_input_sanity_check = 0;
+#endif
- TAILQ_FOREACH(mod, &if_family_head, if_fam_next) {
- if (mod->if_family == (if_family & 0xffff))
- break;
- }
+SYSCTL_DECL(_net_link_generic_system);
- return mod;
-}
+SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_verbose, CTLFLAG_RW,
+ &dlil_verbose, 0, "Log DLIL error messages");
+
+SYSCTL_INT(_net_link_generic_system, OID_AUTO, multi_threaded_input, CTLFLAG_RW,
+ &dlil_multithreaded_input , 0, "Uses multiple input thread for DLIL input");
+
+#if IFNET_INPUT_SANITY_CHK
+SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_input_sanity_check,
+ CTLFLAG_RW, &dlil_input_sanity_check , 0,
+ "Turn on sanity checking in DLIL input");
+#endif
+
+unsigned int net_affinity = 1;
+static kern_return_t dlil_affinity_set(struct thread *, u_int32_t);
+
+extern u_int32_t inject_buckets;
+
+static lck_grp_attr_t *dlil_grp_attributes = NULL;
+static lck_attr_t *dlil_lck_attributes = NULL;
+static lck_grp_t *dlil_input_lock_grp = NULL;
+#define PROTO_HASH_SLOTS 0x5
/*
- * Public functions.
+ * Internal functions.
*/
-struct ifnet *ifbyfamily(u_long family, short unit)
+static int
+proto_hash_value(u_int32_t protocol_family)
+{
+ /*
+ * dlil_proto_unplumb_all() depends on the mapping between
+ * the hash bucket index and the protocol family defined
+ * here; future changes must be applied there as well.
+ */
+ switch(protocol_family) {
+ case PF_INET:
+ return (0);
+ case PF_INET6:
+ return (1);
+ case PF_APPLETALK:
+ return (2);
+ case PF_VLAN:
+ return (3);
+ case PF_UNSPEC:
+ default:
+ return (4);
+ }
+}
+
+/*
+ * Caller must already be holding ifnet lock.
+ */
+static struct if_proto *
+find_attached_proto(struct ifnet *ifp, u_int32_t protocol_family)
{
- struct ifnet *ifp;
+ struct if_proto *proto = NULL;
+ u_int32_t i = proto_hash_value(protocol_family);
+
+ ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_OWNED);
+
+ if (ifp->if_proto_hash != NULL)
+ proto = SLIST_FIRST(&ifp->if_proto_hash[i]);
+
+ while (proto != NULL && proto->protocol_family != protocol_family)
+ proto = SLIST_NEXT(proto, next_hash);
- TAILQ_FOREACH(ifp, &ifnet, if_link)
- if ((family == ifp->if_family) &&
- (ifp->if_unit == unit))
- return ifp;
+ if (proto != NULL)
+ if_proto_ref(proto);
- return 0;
+ return (proto);
}
-struct if_proto *dlttoproto(u_long dl_tag)
+static void
+if_proto_ref(struct if_proto *proto)
{
- if (dl_tag < dl_tag_nb && dl_tag_array[dl_tag].ifp)
- return dl_tag_array[dl_tag].proto;
- return 0;
+ atomic_add_32(&proto->refcount, 1);
}
+extern void if_rtproto_del(struct ifnet *ifp, int protocol);
-static int dlil_ifp_proto_count(struct ifnet * ifp)
+static void
+if_proto_free(struct if_proto *proto)
{
- int count = 0;
- struct if_proto * proto;
- struct dlil_proto_head * tmp;
+ u_int32_t oldval;
+ struct ifnet *ifp = proto->ifp;
+ u_int32_t proto_family = proto->protocol_family;
+ struct kev_dl_proto_data ev_pr_data;
- tmp = (struct dlil_proto_head *) &ifp->proto_head;
+ oldval = atomic_add_32_ov(&proto->refcount, -1);
+ if (oldval > 1)
+ return;
- TAILQ_FOREACH(proto, tmp, next)
- count++;
+ /* No more reference on this, protocol must have been detached */
+ VERIFY(proto->detached);
- return count;
-}
+ if (proto->proto_kpi == kProtoKPI_v1) {
+ if (proto->kpi.v1.detached)
+ proto->kpi.v1.detached(ifp, proto->protocol_family);
+ }
+ if (proto->proto_kpi == kProtoKPI_v2) {
+ if (proto->kpi.v2.detached)
+ proto->kpi.v2.detached(ifp, proto->protocol_family);
+ }
-u_long ifptodlt(struct ifnet *ifp, u_long proto_family)
-{
- struct if_proto *proto;
- struct dlil_proto_head *tmp = (struct dlil_proto_head *) &ifp->proto_head;
+ /*
+ * Cleanup routes that may still be in the routing table for that
+ * interface/protocol pair.
+ */
+ if_rtproto_del(ifp, proto_family);
+ /*
+ * The reserved field carries the number of protocol still attached
+ * (subject to change)
+ */
+ ifnet_lock_shared(ifp);
+ ev_pr_data.proto_family = proto_family;
+ ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
+ ifnet_lock_done(ifp);
- TAILQ_FOREACH(proto, tmp, next)
- if (proto->protocol_family == proto_family)
- return proto->dl_tag;
+ dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_DETACHED,
+ (struct net_event_data *)&ev_pr_data,
+ sizeof(struct kev_dl_proto_data));
- return 0;
+ zfree(dlif_proto_zone, proto);
}
-
-int dlil_find_dltag(u_long if_family, short unit, u_long proto_family, u_long *dl_tag)
+__private_extern__ void
+ifnet_lock_assert(struct ifnet *ifp, ifnet_lock_assert_t what)
{
- struct ifnet *ifp;
+ unsigned int type = 0;
+ int ass = 1;
+
+ switch (what) {
+ case IFNET_LCK_ASSERT_EXCLUSIVE:
+ type = LCK_RW_ASSERT_EXCLUSIVE;
+ break;
+
+ case IFNET_LCK_ASSERT_SHARED:
+ type = LCK_RW_ASSERT_SHARED;
+ break;
- ifp = ifbyfamily(if_family, unit);
- if (!ifp)
- return ENOENT;
+ case IFNET_LCK_ASSERT_OWNED:
+ type = LCK_RW_ASSERT_HELD;
+ break;
+
+ case IFNET_LCK_ASSERT_NOTOWNED:
+ /* nothing to do here for RW lock; bypass assert */
+ ass = 0;
+ break;
- *dl_tag = ifptodlt(ifp, proto_family);
- if (*dl_tag == 0)
- return EPROTONOSUPPORT;
- else
- return 0;
+ default:
+ panic("bad ifnet assert type: %d", what);
+ /* NOTREACHED */
+ }
+ if (ass)
+ lck_rw_assert(&ifp->if_lock, type);
}
+__private_extern__ void
+ifnet_lock_shared(struct ifnet *ifp)
+{
+ lck_rw_lock_shared(&ifp->if_lock);
+}
-void dlil_post_msg(struct ifnet *ifp, u_long event_subclass, u_long event_code,
- struct net_event_data *event_data, u_long event_data_len)
+__private_extern__ void
+ifnet_lock_exclusive(struct ifnet *ifp)
{
- struct net_event_data ev_data;
- struct kev_msg ev_msg;
+ lck_rw_lock_exclusive(&ifp->if_lock);
+}
- /*
- * a net event always start with a net_event_data structure
- * but the caller can generate a simple net event or
- * provide a longer event structure to post
- */
-
- ev_msg.vendor_code = KEV_VENDOR_APPLE;
- ev_msg.kev_class = KEV_NETWORK_CLASS;
- ev_msg.kev_subclass = event_subclass;
- ev_msg.event_code = event_code;
-
- if (event_data == 0) {
- event_data = &ev_data;
- event_data_len = sizeof(struct net_event_data);
- }
-
- strncpy(&event_data->if_name[0], ifp->if_name, IFNAMSIZ);
- event_data->if_family = ifp->if_family;
- event_data->if_unit = (unsigned long) ifp->if_unit;
+__private_extern__ void
+ifnet_lock_done(struct ifnet *ifp)
+{
+ lck_rw_done(&ifp->if_lock);
+}
- ev_msg.dv[0].data_length = event_data_len;
- ev_msg.dv[0].data_ptr = event_data;
- ev_msg.dv[1].data_length = 0;
+__private_extern__ void
+ifnet_head_lock_shared(void)
+{
+ lck_rw_lock_shared(&ifnet_head_lock);
+}
- kev_post_msg(&ev_msg);
+__private_extern__ void
+ifnet_head_lock_exclusive(void)
+{
+ lck_rw_lock_exclusive(&ifnet_head_lock);
}
+__private_extern__ void
+ifnet_head_done(void)
+{
+ lck_rw_done(&ifnet_head_lock);
+}
+/*
+ * Caller must already be holding ifnet lock.
+ */
+static int
+dlil_ifp_proto_count(struct ifnet * ifp)
+{
+ int i, count = 0;
-void
-dlil_init()
-{
- int i;
-
- TAILQ_INIT(&dlil_ifnet_head);
- TAILQ_INIT(&if_family_head);
-
- // create the dl tag array
- MALLOC(dl_tag_array, void *, sizeof(struct dl_tag_str) * MAX_DL_TAGS, M_NKE, M_WAITOK);
- if (dl_tag_array == 0) {
- printf("dlil_init tags array allocation failed\n");
- return; //very bad
- }
- bzero(dl_tag_array, sizeof(struct dl_tag_str) * MAX_DL_TAGS);
- dl_tag_nb = MAX_DL_TAGS;
-
- // create the dl filters array
- MALLOC(dlil_filters, void *, sizeof(struct dlil_filter_id_str) * MAX_DLIL_FILTERS, M_NKE, M_WAITOK);
- if (dlil_filters == 0) {
- printf("dlil_init filters array allocation failed\n");
- return; //very bad
- }
- bzero(dlil_filters, sizeof(struct dlil_filter_id_str) * MAX_DLIL_FILTERS);
- dlil_filters_nb = MAX_DLIL_FILTERS;
-
- bzero(&dlil_stats, sizeof(dlil_stats));
-
- simple_lock_init(&dlil_input_lock);
-
- /*
- * Start up the dlil input thread once everything is initialized
- */
- (void) kernel_thread(kernel_task, dlil_input_thread);
-}
-
-u_long get_new_filter_id()
-{
- u_long i;
- u_char *p;
-
- for (i=1; i < dlil_filters_nb; i++)
- if (dlil_filters[i].type == 0)
- break;
-
- if (i == dlil_filters_nb) {
- // expand the filters array by MAX_DLIL_FILTERS
- MALLOC(p, u_char *, sizeof(struct dlil_filter_id_str) * (dlil_filters_nb + MAX_DLIL_FILTERS), M_NKE, M_WAITOK);
- if (p == 0)
- return 0;
-
- bcopy(dlil_filters, p, sizeof(struct dlil_filter_id_str) * dlil_filters_nb);
- bzero(p + sizeof(struct dlil_filter_id_str) * dlil_filters_nb, sizeof(struct dlil_filter_id_str) * MAX_DL_TAGS);
- dlil_filters_nb += MAX_DLIL_FILTERS;
- FREE(dlil_filters, M_NKE);
- dlil_filters = (struct dlil_filter_id_str *)p;
- }
-
- return i;
-}
-
-
-int dlil_attach_interface_filter(struct ifnet *ifp,
- struct dlil_if_flt_str *if_filter,
- u_long *filter_id,
- int insertion_point)
-{
- int s;
- int retval = 0;
- struct dlil_filterq_entry *tmp_ptr;
- struct dlil_filterq_entry *if_filt;
- struct dlil_filterq_head *fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- boolean_t funnel_state;
-
- MALLOC(tmp_ptr, struct dlil_filterq_entry *, sizeof(*tmp_ptr), M_NKE, M_WAITOK);
- if (tmp_ptr == NULL)
- return (ENOBUFS);
-
- bcopy((caddr_t) if_filter, (caddr_t) &tmp_ptr->variants.if_filter,
- sizeof(struct dlil_if_flt_str));
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- *filter_id = get_new_filter_id();
- if (*filter_id == 0) {
- FREE(tmp_ptr, M_NKE);
- retval = ENOMEM;
- goto end;
- }
-
- dlil_filters[*filter_id].filter_ptr = tmp_ptr;
- dlil_filters[*filter_id].head = (struct dlil_filterq_head *) &ifp->if_flt_head;
- dlil_filters[*filter_id].type = DLIL_IF_FILTER;
- dlil_filters[*filter_id].ifp = ifp;
- tmp_ptr->filter_id = *filter_id;
- tmp_ptr->type = DLIL_IF_FILTER;
-
- if (insertion_point != DLIL_LAST_FILTER) {
- TAILQ_FOREACH(if_filt, fhead, que)
- if (insertion_point == if_filt->filter_id) {
- TAILQ_INSERT_BEFORE(if_filt, tmp_ptr, que);
- break;
- }
- }
- else
- TAILQ_INSERT_TAIL(fhead, tmp_ptr, que);
+ ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_OWNED);
-end:
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return retval;
-}
-
-
-int dlil_attach_protocol_filter(u_long dl_tag,
- struct dlil_pr_flt_str *pr_filter,
- u_long *filter_id,
- int insertion_point)
-{
- struct dlil_filterq_entry *tmp_ptr, *pr_filt;
- int s;
- int retval = 0;
- boolean_t funnel_state;
-
- if (dl_tag >= dl_tag_nb || dl_tag_array[dl_tag].ifp == 0)
- return (ENOENT);
-
- MALLOC(tmp_ptr, struct dlil_filterq_entry *, sizeof(*tmp_ptr), M_NKE, M_WAITOK);
- if (tmp_ptr == NULL)
- return (ENOBUFS);
-
- bcopy((caddr_t) pr_filter, (caddr_t) &tmp_ptr->variants.pr_filter,
- sizeof(struct dlil_pr_flt_str));
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- *filter_id = get_new_filter_id();
- if (*filter_id == 0) {
- FREE(tmp_ptr, M_NKE);
- retval = ENOMEM;
- goto end;
- }
-
- dlil_filters[*filter_id].filter_ptr = tmp_ptr;
- dlil_filters[*filter_id].head = dl_tag_array[dl_tag].pr_flt_head;
- dlil_filters[*filter_id].type = DLIL_PR_FILTER;
- dlil_filters[*filter_id].proto = dl_tag_array[dl_tag].proto;
- dlil_filters[*filter_id].ifp = dl_tag_array[dl_tag].ifp;
- tmp_ptr->filter_id = *filter_id;
- tmp_ptr->type = DLIL_PR_FILTER;
-
- if (insertion_point != DLIL_LAST_FILTER) {
- TAILQ_FOREACH(pr_filt, dl_tag_array[dl_tag].pr_flt_head, que)
- if (insertion_point == pr_filt->filter_id) {
- TAILQ_INSERT_BEFORE(pr_filt, tmp_ptr, que);
- break;
- }
- }
- else
- TAILQ_INSERT_TAIL(dl_tag_array[dl_tag].pr_flt_head, tmp_ptr, que);
+ if (ifp->if_proto_hash == NULL)
+ goto done;
-end:
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return retval;
+ for (i = 0; i < PROTO_HASH_SLOTS; i++) {
+ struct if_proto *proto;
+ SLIST_FOREACH(proto, &ifp->if_proto_hash[i], next_hash) {
+ count++;
+ }
+ }
+done:
+ return (count);
}
-
-int
-dlil_detach_filter(u_long filter_id)
+__private_extern__ void
+dlil_post_msg(struct ifnet *ifp, u_int32_t event_subclass,
+ u_int32_t event_code, struct net_event_data *event_data,
+ u_int32_t event_data_len)
{
- struct dlil_filter_id_str *flt;
- int s, retval = 0;
- boolean_t funnel_state;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- if (filter_id >= dlil_filters_nb || dlil_filters[filter_id].type == 0) {
- retval = ENOENT;
- goto end;
- }
-
- flt = &dlil_filters[filter_id];
+ struct net_event_data ev_data;
+ struct kev_msg ev_msg;
- if (flt->type == DLIL_IF_FILTER) {
- if (IFILT(flt->filter_ptr).filter_detach)
- (*IFILT(flt->filter_ptr).filter_detach)(IFILT(flt->filter_ptr).cookie);
- }
- else {
- if (flt->type == DLIL_PR_FILTER) {
- if (PFILT(flt->filter_ptr).filter_detach)
- (*PFILT(flt->filter_ptr).filter_detach)(PFILT(flt->filter_ptr).cookie);
+ bzero(&ev_msg, sizeof (ev_msg));
+ bzero(&ev_data, sizeof (ev_data));
+ /*
+ * a net event always starts with a net_event_data structure
+ * but the caller can generate a simple net event or
+ * provide a longer event structure to post
+ */
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = event_subclass;
+ ev_msg.event_code = event_code;
+
+ if (event_data == NULL) {
+ event_data = &ev_data;
+ event_data_len = sizeof(struct net_event_data);
}
- }
- TAILQ_REMOVE(flt->head, flt->filter_ptr, que);
- FREE(flt->filter_ptr, M_NKE);
- flt->type = 0;
+ strncpy(&event_data->if_name[0], ifp->if_name, IFNAMSIZ);
+ event_data->if_family = ifp->if_family;
+ event_data->if_unit = (u_int32_t) ifp->if_unit;
-end:
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return retval;
+ ev_msg.dv[0].data_length = event_data_len;
+ ev_msg.dv[0].data_ptr = event_data;
+ ev_msg.dv[1].data_length = 0;
+
+ dlil_event_internal(ifp, &ev_msg);
}
+static int
+dlil_create_input_thread(ifnet_t ifp, struct dlil_threading_info *inputthread)
+{
+ int error;
+
+ bzero(inputthread, sizeof(*inputthread));
+ /* loopback ifp may not be configured at dlil_init time. */
+ if (ifp == lo_ifp) {
+ (void) strlcat(inputthread->input_name,
+ "dlil_input_main_thread_mtx", DLIL_THREADNAME_LEN);
+ } else {
+ (void) snprintf(inputthread->input_name, DLIL_THREADNAME_LEN,
+ "dlil_input_%s%d_mtx", ifp->if_name, ifp->if_unit);
+ }
-void
-dlil_input_thread_continue(void)
-{
- while (1) {
- struct mbuf *m, *m_loop;
- int expand_mcl;
-
- usimple_lock(&dlil_input_lock);
- m = dlil_input_mbuf_head;
- dlil_input_mbuf_head = NULL;
- dlil_input_mbuf_tail = NULL;
- m_loop = dlil_input_loop_head;
- dlil_input_loop_head = NULL;
- dlil_input_loop_tail = NULL;
- usimple_unlock(&dlil_input_lock);
-
- MBUF_LOCK();
- expand_mcl = dlil_expand_mcl;
- dlil_expand_mcl = 0;
- MBUF_UNLOCK();
- if (expand_mcl) {
- caddr_t p;
- MCLALLOC(p, M_WAIT);
- if (p) MCLFREE(p);
- }
-
- /*
- * NOTE warning %%% attention !!!!
- * We should think about putting some thread starvation safeguards if
- * we deal with long chains of packets.
- */
- while (m) {
- struct mbuf *m0 = m->m_nextpkt;
- void *header = m->m_pkthdr.header;
-
- m->m_nextpkt = NULL;
- m->m_pkthdr.header = NULL;
- (void) dlil_input_packet(m->m_pkthdr.rcvif, m, header);
- m = m0;
- }
- m = m_loop;
- while (m) {
- struct mbuf *m0 = m->m_nextpkt;
- void *header = m->m_pkthdr.header;
- struct ifnet *ifp = &loif[0];
-
- m->m_nextpkt = NULL;
- m->m_pkthdr.header = NULL;
- (void) dlil_input_packet(ifp, m, header);
- m = m0;
- }
-
- if (netisr != 0)
- run_netisr();
-
- if (dlil_input_mbuf_head == NULL &&
- dlil_input_loop_head == NULL &&
- netisr == 0) {
- assert_wait(&dlil_input_thread_wakeup, THREAD_UNINT);
- (void) thread_block(dlil_input_thread_continue);
- /* NOTREACHED */
- }
- }
-}
-
-void dlil_input_thread(void)
-{
- register thread_t self = current_thread();
- extern void stack_privilege(thread_t thread);
-
- /*
- * Make sure that this thread
- * always has a kernel stack, and
- * bind it to the master cpu.
- */
- stack_privilege(self);
- ml_thread_policy(current_thread(), MACHINE_GROUP,
- (MACHINE_NETWORK_GROUP|MACHINE_NETWORK_NETISR));
-
- /* The dlil thread is always funneled */
- thread_funnel_set(network_flock, TRUE);
- dlil_initialized = 1;
- dlil_input_thread_continue();
+ inputthread->lck_grp = lck_grp_alloc_init(inputthread->input_name,
+ dlil_grp_attributes);
+ lck_mtx_init(&inputthread->input_lck, inputthread->lck_grp,
+ dlil_lck_attributes);
+
+ error= kernel_thread_start((thread_continue_t)dlil_input_thread_func,
+ inputthread, &inputthread->input_thread);
+ if (error == 0) {
+ ml_thread_policy(inputthread->input_thread, MACHINE_GROUP,
+ (MACHINE_NETWORK_GROUP|MACHINE_NETWORK_NETISR));
+ /*
+ * Except for the loopback dlil input thread, we create
+ * an affinity set so that the matching workloop thread
+ * can be scheduled on the same processor set.
+ */
+ if (net_affinity && inputthread != dlil_lo_thread_ptr) {
+ struct thread *tp = inputthread->input_thread;
+ u_int32_t tag;
+ /*
+ * Randomize to reduce the probability
+ * of affinity tag namespace collision.
+ */
+ read_random(&tag, sizeof (tag));
+ if (dlil_affinity_set(tp, tag) == KERN_SUCCESS) {
+ thread_reference(tp);
+ inputthread->tag = tag;
+ inputthread->net_affinity = TRUE;
+ }
+ }
+ } else {
+ panic("%s: couldn't create thread", __func__);
+ /* NOTREACHED */
+ }
+ OSAddAtomic(1, &cur_dlil_input_threads);
+#if DLIL_DEBUG
+ printf("%s: threadinfo: %p input_thread=%p threads: cur=%d max=%d\n",
+ __func__, inputthread, inputthread->input_thread,
+ dlil_multithreaded_input, cur_dlil_input_threads);
+#endif
+ return (error);
}
-int
-dlil_input(struct ifnet *ifp, struct mbuf *m_head, struct mbuf *m_tail)
-{
- /* WARNING
- * Because of loopbacked multicast we cannot stuff the ifp in
- * the rcvif of the packet header: loopback has its own dlil
- * input queue
- */
-
- usimple_lock(&dlil_input_lock);
- if (ifp->if_type != IFT_LOOP) {
- if (dlil_input_mbuf_head == NULL)
- dlil_input_mbuf_head = m_head;
- else if (dlil_input_mbuf_tail != NULL)
- dlil_input_mbuf_tail->m_nextpkt = m_head;
- dlil_input_mbuf_tail = m_tail ? m_tail : m_head;
- } else {
- if (dlil_input_loop_head == NULL)
- dlil_input_loop_head = m_head;
- else if (dlil_input_loop_tail != NULL)
- dlil_input_loop_tail->m_nextpkt = m_head;
- dlil_input_loop_tail = m_tail ? m_tail : m_head;
- }
- usimple_unlock(&dlil_input_lock);
-
- wakeup((caddr_t)&dlil_input_thread_wakeup);
-
- return 0;
-}
+static kern_return_t
+dlil_affinity_set(struct thread *tp, u_int32_t tag)
+{
+ thread_affinity_policy_data_t policy;
-int
-dlil_input_packet(struct ifnet *ifp, struct mbuf *m,
- char *frame_header)
-{
- struct ifnet *orig_ifp = 0;
- struct dlil_filterq_entry *tmp;
- int retval;
- struct if_proto *ifproto = 0;
- struct if_proto *proto;
- struct dlil_filterq_head *fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
-
-
- KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_START,0,0,0,0,0);
-
- /*
- * Run interface filters
- */
-
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
-
- TAILQ_FOREACH_REVERSE(tmp, fhead, que, dlil_filterq_head) {
- if (IFILT(tmp).filter_if_input) {
- retval = (*IFILT(tmp).filter_if_input)(IFILT(tmp).cookie,
- &ifp,
- &m,
- &frame_header);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
- }
- }
+ bzero(&policy, sizeof (policy));
+ policy.affinity_tag = tag;
+ return (thread_policy_set(tp, THREAD_AFFINITY_POLICY,
+ (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT));
+}
- if (ifp != orig_ifp)
- break;
- }
- }
+void
+dlil_init(void)
+{
+ thread_t thread = THREAD_NULL;
- ifp->if_lastchange = time;
-
- /*
- * Call family demux module. If the demux module finds a match
- * for the frame it will fill-in the ifproto pointer.
- */
+ /*
+ * The following fields must be 64-bit aligned for atomic operations.
+ */
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_ipackets);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_ierrors)
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_opackets);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_oerrors);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_collisions);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_ibytes);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_obytes);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_imcasts);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_omcasts);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_iqdrops);
+ IF_DATA_REQUIRE_ALIGNED_64(ifi_noproto);
+
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ipackets);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ierrors)
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_opackets);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_oerrors);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_collisions);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ibytes);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_obytes);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_imcasts);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_omcasts);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_iqdrops);
+ IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_noproto);
+
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ibkpackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ibkbytes);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_obkpackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_obkbytes);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivipackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivibytes);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovipackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovibytes);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivopackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivobytes);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovopackets);
+ IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovobytes);
- retval = (*ifp->if_demux)(ifp, m, frame_header, &ifproto );
+ /*
+ * These IF_HWASSIST_ flags must be equal to their IFNET_* counterparts.
+ */
+ _CASSERT(IF_HWASSIST_CSUM_IP == IFNET_CSUM_IP);
+ _CASSERT(IF_HWASSIST_CSUM_TCP == IFNET_CSUM_TCP);
+ _CASSERT(IF_HWASSIST_CSUM_UDP == IFNET_CSUM_UDP);
+ _CASSERT(IF_HWASSIST_CSUM_IP_FRAGS == IFNET_CSUM_FRAGMENT);
+ _CASSERT(IF_HWASSIST_CSUM_FRAGMENT == IFNET_IP_FRAGMENT);
+ _CASSERT(IF_HWASSIST_CSUM_TCP_SUM16 == IFNET_CSUM_SUM16);
+ _CASSERT(IF_HWASSIST_VLAN_TAGGING == IFNET_VLAN_TAGGING);
+ _CASSERT(IF_HWASSIST_VLAN_MTU == IFNET_VLAN_MTU);
+ _CASSERT(IF_HWASSIST_TSO_V4 == IFNET_TSO_IPV4);
+ _CASSERT(IF_HWASSIST_TSO_V6 == IFNET_TSO_IPV6);
- if (m->m_flags & (M_BCAST|M_MCAST))
- ifp->if_imcasts++;
-
- if ((retval) && (retval != EJUSTRETURN) && (ifp->offercnt)) {
/*
- * No match was found, look for any offers.
+ * Make sure we have at least IF_LLREACH_MAXLEN in the llreach info.
*/
- struct dlil_proto_head *tmp = (struct dlil_proto_head *) &ifp->proto_head;
- TAILQ_FOREACH(proto, tmp, next) {
- if ((proto->dl_offer) && (proto->dl_offer(m, frame_header) == 0)) {
- ifproto = proto;
- retval = 0;
- break;
- }
- }
- }
+ _CASSERT(IF_LLREACH_MAXLEN <= IF_LLREACHINFO_ADDRLEN);
- if (retval) {
- if (retval != EJUSTRETURN) {
- m_freem(m);
- return retval;
- }
- else
- return 0;
- }
- else
- if (ifproto == 0) {
- printf("ERROR - dlil_input - if_demux didn't return an if_proto pointer\n");
- m_freem(m);
- return 0;
- }
+ PE_parse_boot_argn("net_affinity", &net_affinity,
+ sizeof (net_affinity));
-/*
- * Call any attached protocol filters.
- */
+ PE_parse_boot_argn("net_rtref", &net_rtref, sizeof (net_rtref));
- TAILQ_FOREACH_REVERSE(tmp, &ifproto->pr_flt_head, que, dlil_filterq_head) {
- if (PFILT(tmp).filter_dl_input) {
- retval = (*PFILT(tmp).filter_dl_input)(PFILT(tmp).cookie,
- &m,
- &frame_header,
- &ifp);
-
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
- }
- }
- }
+ PE_parse_boot_argn("ifnet_debug", &ifnet_debug, sizeof (ifnet_debug));
+ dlif_size = (ifnet_debug == 0) ? sizeof (struct dlil_ifnet) :
+ sizeof (struct dlil_ifnet_dbg);
+ /* Enforce 64-bit alignment for dlil_ifnet structure */
+ dlif_bufsize = dlif_size + sizeof (void *) + sizeof (u_int64_t);
+ dlif_bufsize = P2ROUNDUP(dlif_bufsize, sizeof (u_int64_t));
+ dlif_zone = zinit(dlif_bufsize, DLIF_ZONE_MAX * dlif_bufsize,
+ 0, DLIF_ZONE_NAME);
+ if (dlif_zone == NULL) {
+ panic("%s: failed allocating %s", __func__, DLIF_ZONE_NAME);
+ /* NOTREACHED */
+ }
+ zone_change(dlif_zone, Z_EXPAND, TRUE);
+ zone_change(dlif_zone, Z_CALLERACCT, FALSE);
+
+ dlif_filt_size = sizeof (struct ifnet_filter);
+ dlif_filt_zone = zinit(dlif_filt_size,
+ DLIF_FILT_ZONE_MAX * dlif_filt_size, 0, DLIF_FILT_ZONE_NAME);
+ if (dlif_filt_zone == NULL) {
+ panic("%s: failed allocating %s", __func__,
+ DLIF_FILT_ZONE_NAME);
+ /* NOTREACHED */
+ }
+ zone_change(dlif_filt_zone, Z_EXPAND, TRUE);
+ zone_change(dlif_filt_zone, Z_CALLERACCT, FALSE);
+
+ dlif_inp_size = sizeof (struct dlil_threading_info);
+ dlif_inp_zone = zinit(dlif_inp_size,
+ DLIF_INP_ZONE_MAX * dlif_inp_size, 0, DLIF_INP_ZONE_NAME);
+ if (dlif_inp_zone == NULL) {
+ panic("%s: failed allocating %s", __func__, DLIF_INP_ZONE_NAME);
+ /* NOTREACHED */
+ }
+ zone_change(dlif_inp_zone, Z_EXPAND, TRUE);
+ zone_change(dlif_inp_zone, Z_CALLERACCT, FALSE);
+
+ dlif_phash_size = sizeof (struct proto_hash_entry) * PROTO_HASH_SLOTS;
+ dlif_phash_zone = zinit(dlif_phash_size,
+ DLIF_PHASH_ZONE_MAX * dlif_phash_size, 0, DLIF_PHASH_ZONE_NAME);
+ if (dlif_phash_zone == NULL) {
+ panic("%s: failed allocating %s", __func__,
+ DLIF_PHASH_ZONE_NAME);
+ /* NOTREACHED */
+ }
+ zone_change(dlif_phash_zone, Z_EXPAND, TRUE);
+ zone_change(dlif_phash_zone, Z_CALLERACCT, FALSE);
+
+ dlif_proto_size = sizeof (struct if_proto);
+ dlif_proto_zone = zinit(dlif_proto_size,
+ DLIF_PROTO_ZONE_MAX * dlif_proto_size, 0, DLIF_PROTO_ZONE_NAME);
+ if (dlif_proto_zone == NULL) {
+ panic("%s: failed allocating %s", __func__,
+ DLIF_PROTO_ZONE_NAME);
+ /* NOTREACHED */
+ }
+ zone_change(dlif_proto_zone, Z_EXPAND, TRUE);
+ zone_change(dlif_proto_zone, Z_CALLERACCT, FALSE);
+ ifnet_llreach_init();
- retval = (*ifproto->dl_input)(m, frame_header,
- ifp, ifproto->dl_tag,
- TRUE);
-
- if (retval == EJUSTRETURN)
- retval = 0;
- else
- if (retval)
- m_freem(m);
+ TAILQ_INIT(&dlil_ifnet_head);
+ TAILQ_INIT(&ifnet_head);
+ TAILQ_INIT(&ifnet_detaching_head);
- KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_END,0,0,0,0,0);
- return retval;
-}
+ /* Setup the lock groups we will use */
+ dlil_grp_attributes = lck_grp_attr_alloc_init();
+ dlil_lock_group = lck_grp_alloc_init("dlil internal locks",
+ dlil_grp_attributes);
+ ifnet_lock_group = lck_grp_alloc_init("ifnet locks",
+ dlil_grp_attributes);
+ ifnet_head_lock_group = lck_grp_alloc_init("ifnet head lock",
+ dlil_grp_attributes);
+ dlil_input_lock_grp = lck_grp_alloc_init("dlil input lock",
+ dlil_grp_attributes);
+ /* Setup the lock attributes we will use */
+ dlil_lck_attributes = lck_attr_alloc_init();
-void ether_input(ifp, eh, m)
- struct ifnet *ifp;
- struct ether_header *eh;
- struct mbuf *m;
+ ifnet_lock_attr = lck_attr_alloc_init();
-{
- kprintf("Someone is calling ether_input!!\n");
+ lck_rw_init(&ifnet_head_lock, ifnet_head_lock_group,
+ dlil_lck_attributes);
+ lck_mtx_init(&dlil_ifnet_lock, dlil_lock_group, dlil_lck_attributes);
- dlil_input(ifp, m, NULL);
-}
+ lck_attr_free(dlil_lck_attributes);
+ dlil_lck_attributes = NULL;
+ ifa_init();
-int
-dlil_event(struct ifnet *ifp, struct kern_event_msg *event)
-{
- struct dlil_filterq_entry *filt;
- int retval = 0;
- struct ifnet *orig_ifp = 0;
- struct if_proto *proto;
- struct dlil_filterq_head *fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- struct kev_msg kev_msg;
- struct dlil_proto_head *tmp = (struct dlil_proto_head *) &ifp->proto_head;
- boolean_t funnel_state;
-
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
-
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
-
- TAILQ_FOREACH_REVERSE(filt, fhead, que, dlil_filterq_head) {
- if (IFILT(filt).filter_if_event) {
- retval = (*IFILT(filt).filter_if_event)(IFILT(filt).cookie,
- &ifp,
- &event);
-
- if (retval) {
- (void) thread_funnel_set(network_flock, funnel_state);
- if (retval == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
- }
-
- if (ifp != orig_ifp)
- break;
- }
- }
-
-
- /*
- * Call Interface Module event hook, if any.
- */
-
- if (ifp->if_event) {
- retval = ifp->if_event(ifp, (caddr_t) event);
-
- if (retval) {
- (void) thread_funnel_set(network_flock, funnel_state);
-
- if (retval == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
- }
-
- /*
- * Call dl_event entry point for all protocols attached to this interface
- */
-
- TAILQ_FOREACH(proto, tmp, next) {
/*
- * Call any attached protocol filters.
+ * Create and start up the first dlil input thread once everything
+ * is initialized.
*/
+ dlil_create_input_thread(lo_ifp, dlil_lo_thread_ptr);
- TAILQ_FOREACH_REVERSE(filt, &proto->pr_flt_head, que, dlil_filterq_head) {
- if (PFILT(filt).filter_dl_event) {
- retval = (*PFILT(filt).filter_dl_event)(PFILT(filt).cookie,
- event);
+ if (kernel_thread_start((thread_continue_t)ifnet_delayed_thread_func,
+ NULL, &thread) != 0) {
+ panic("%s: couldn't create detach thread", __func__);
+ /* NOTREACHED */
+ }
+ thread_deallocate(thread);
- if (retval) {
- (void) thread_funnel_set(network_flock, funnel_state);
- if (retval == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
- }
- }
+#if PF
+ /* Initialize the packet filter */
+ pfinit();
+#endif /* PF */
+}
+static void
+if_flt_monitor_busy(struct ifnet *ifp)
+{
+ lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED);
- /*
- * Finally, call the dl_event entry point (if any)
- */
+ ++ifp->if_flt_busy;
+ VERIFY(ifp->if_flt_busy != 0);
+}
- if (proto->dl_event)
- retval = (*proto->dl_event)(event, proto->dl_tag);
+static void
+if_flt_monitor_unbusy(struct ifnet *ifp)
+{
+ if_flt_monitor_leave(ifp);
+}
- if (retval == EJUSTRETURN) {
- (void) thread_funnel_set(network_flock, funnel_state);
- return 0;
- }
- }
-
+static void
+if_flt_monitor_enter(struct ifnet *ifp)
+{
+ lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED);
- /*
- * Now, post this event to the Kernel Event message queue
- */
+ while (ifp->if_flt_busy) {
+ ++ifp->if_flt_waiters;
+ (void) msleep(&ifp->if_flt_head, &ifp->if_flt_lock,
+ (PZERO - 1), "if_flt_monitor", NULL);
+ }
+ if_flt_monitor_busy(ifp);
+}
- kev_msg.vendor_code = event->vendor_code;
- kev_msg.kev_class = event->kev_class;
- kev_msg.kev_subclass = event->kev_subclass;
- kev_msg.event_code = event->event_code;
- kev_msg.dv[0].data_ptr = &event->event_data[0];
- kev_msg.dv[0].data_length = event->total_size - KEV_MSG_HEADER_SIZE;
- kev_msg.dv[1].data_length = 0;
+static void
+if_flt_monitor_leave(struct ifnet *ifp)
+{
+ lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED);
- kev_post_msg(&kev_msg);
+ VERIFY(ifp->if_flt_busy != 0);
+ --ifp->if_flt_busy;
- (void) thread_funnel_set(network_flock, funnel_state);
- return 0;
+ if (ifp->if_flt_busy == 0 && ifp->if_flt_waiters > 0) {
+ ifp->if_flt_waiters = 0;
+ wakeup(&ifp->if_flt_head);
+ }
}
-
-
-int
-dlil_output(u_long dl_tag,
- struct mbuf *m,
- caddr_t route,
- struct sockaddr *dest,
- int raw
- )
-{
- char *frame_type;
- char *dst_linkaddr;
- struct ifnet *orig_ifp = 0;
- struct ifnet *ifp;
- struct if_proto *proto;
- struct dlil_filterq_entry *tmp;
- int retval = 0;
- char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4];
- char dst_linkaddr_buffer[MAX_LINKADDR * 4];
- struct dlil_filterq_head *fhead;
-
- KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_START,0,0,0,0,0);
-
- if (dl_tag >= dl_tag_nb || dl_tag_array[dl_tag].ifp == 0) {
- m_freem(m);
- return ENOENT;
- }
-
- ifp = dl_tag_array[dl_tag].ifp;
- proto = dl_tag_array[dl_tag].proto;
-
- frame_type = frame_type_buffer;
- dst_linkaddr = dst_linkaddr_buffer;
-
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
-
- if ((raw == 0) && (proto->dl_pre_output)) {
- retval = (*proto->dl_pre_output)(ifp, &m, dest, route,
- frame_type, dst_linkaddr, dl_tag);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
+__private_extern__ int
+dlil_attach_filter(struct ifnet *ifp, const struct iff_filter *if_filter,
+ interface_filter_t *filter_ref)
+{
+ int retval = 0;
+ struct ifnet_filter *filter = NULL;
+
+ ifnet_head_lock_shared();
+ /* Check that the interface is in the global list */
+ if (!ifnet_lookup(ifp)) {
+ retval = ENXIO;
+ goto done;
}
- }
-
-/*
- * Run any attached protocol filters.
- */
- if (TAILQ_EMPTY(dl_tag_array[dl_tag].pr_flt_head) == 0) {
- TAILQ_FOREACH(tmp, dl_tag_array[dl_tag].pr_flt_head, que) {
- if (PFILT(tmp).filter_dl_output) {
- retval = (*PFILT(tmp).filter_dl_output)(PFILT(tmp).cookie,
- &m, &ifp, &dest, dst_linkaddr, frame_type);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
- }
- }
+ filter = zalloc(dlif_filt_zone);
+ if (filter == NULL) {
+ retval = ENOMEM;
+ goto done;
}
- }
+ bzero(filter, dlif_filt_size);
+ /* refcnt held above during lookup */
+ filter->filt_ifp = ifp;
+ filter->filt_cookie = if_filter->iff_cookie;
+ filter->filt_name = if_filter->iff_name;
+ filter->filt_protocol = if_filter->iff_protocol;
+ filter->filt_input = if_filter->iff_input;
+ filter->filt_output = if_filter->iff_output;
+ filter->filt_event = if_filter->iff_event;
+ filter->filt_ioctl = if_filter->iff_ioctl;
+ filter->filt_detached = if_filter->iff_detached;
-/*
- * Call framing module
- */
- if ((raw == 0) && (ifp->if_framer)) {
- retval = (*ifp->if_framer)(ifp, &m, dest, dst_linkaddr, frame_type);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else
- {
- m_freem(m);
- return retval;
- }
- }
- }
-
-#if BRIDGE
- if (do_bridge) {
- struct mbuf *m0 = m ;
- struct ether_header *eh = mtod(m, struct ether_header *);
-
- if (m->m_pkthdr.rcvif)
- m->m_pkthdr.rcvif = NULL ;
- ifp = bridge_dst_lookup(eh);
- bdg_forward(&m0, ifp);
- if (m0)
- m_freem(m0);
-
- return 0;
- }
-#endif
+ lck_mtx_lock(&ifp->if_flt_lock);
+ if_flt_monitor_enter(ifp);
+ lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED);
+ TAILQ_INSERT_TAIL(&ifp->if_flt_head, filter, filt_next);
-/*
- * Let interface filters (if any) do their thing ...
- */
+ if_flt_monitor_leave(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- if (TAILQ_EMPTY(fhead) == 0) {
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
- TAILQ_FOREACH(tmp, fhead, que) {
- if (IFILT(tmp).filter_if_output) {
- retval = (*IFILT(tmp).filter_if_output)(IFILT(tmp).cookie,
- &ifp,
- &m);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
- }
+ *filter_ref = filter;
- }
-
- if (ifp != orig_ifp)
- break;
- }
- }
- }
+ /*
+ * Bump filter count and route_generation ID to let TCP
+ * know it shouldn't do TSO on this connection
+ */
+ OSAddAtomic(1, &dlil_filter_count);
+ if (use_routegenid)
+ routegenid_update();
-/*
- * Finally, call the driver.
- */
+ if (dlil_verbose) {
+ printf("%s%d: %s filter attached\n", ifp->if_name,
+ ifp->if_unit, if_filter->iff_name);
+ }
+done:
+ ifnet_head_done();
+ if (retval != 0 && ifp != NULL) {
+ DLIL_PRINTF("%s%d: failed to attach %s (err=%d)\n",
+ ifp->if_name, ifp->if_unit, if_filter->iff_name, retval);
+ }
+ if (retval != 0 && filter != NULL)
+ zfree(dlif_filt_zone, filter);
- KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
- retval = (*ifp->if_output)(ifp, m);
- KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
+ return (retval);
+}
- KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_END,0,0,0,0,0);
+static int
+dlil_detach_filter_internal(interface_filter_t filter, int detached)
+{
+ int retval = 0;
+
+ if (detached == 0) {
+ ifnet_t ifp = NULL;
+
+ ifnet_head_lock_shared();
+ TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
+ interface_filter_t entry = NULL;
+
+ lck_mtx_lock(&ifp->if_flt_lock);
+ TAILQ_FOREACH(entry, &ifp->if_flt_head, filt_next) {
+ if (entry != filter || entry->filt_skip)
+ continue;
+ /*
+ * We've found a match; since it's possible
+ * that the thread gets blocked in the monitor,
+ * we do the lock dance. Interface should
+ * not be detached since we still have a use
+ * count held during filter attach.
+ */
+ entry->filt_skip = 1; /* skip input/output */
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ ifnet_head_done();
+
+ lck_mtx_lock(&ifp->if_flt_lock);
+ if_flt_monitor_enter(ifp);
+ lck_mtx_assert(&ifp->if_flt_lock,
+ LCK_MTX_ASSERT_OWNED);
+
+ /* Remove the filter from the list */
+ TAILQ_REMOVE(&ifp->if_flt_head, filter,
+ filt_next);
+
+ if_flt_monitor_leave(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ if (dlil_verbose) {
+ printf("%s%d: %s filter detached\n",
+ ifp->if_name, ifp->if_unit,
+ filter->filt_name);
+ }
+ goto destroy;
+ }
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ }
+ ifnet_head_done();
- if ((retval == 0) || (retval == EJUSTRETURN))
- return 0;
- else
- return retval;
-}
+ /* filter parameter is not a valid filter ref */
+ retval = EINVAL;
+ goto done;
+ }
+ if (dlil_verbose)
+ printf("%s filter detached\n", filter->filt_name);
-int
-dlil_ioctl(u_long proto_fam,
- struct ifnet *ifp,
- u_long ioctl_code,
- caddr_t ioctl_arg)
-{
- struct dlil_filterq_entry *tmp;
- struct dlil_filterq_head *fhead;
- int retval = EOPNOTSUPP;
- int retval2 = EOPNOTSUPP;
- u_long dl_tag;
- struct if_family_str *if_family;
-
-
- if (proto_fam) {
- retval = dlil_find_dltag(ifp->if_family, ifp->if_unit,
- proto_fam, &dl_tag);
-
- if (retval == 0) {
- if (dl_tag_array[dl_tag].ifp != ifp)
- return ENOENT;
-
-/*
- * Run any attached protocol filters.
- */
- TAILQ_FOREACH(tmp, dl_tag_array[dl_tag].pr_flt_head, que) {
- if (PFILT(tmp).filter_dl_ioctl) {
- retval =
- (*PFILT(tmp).filter_dl_ioctl)(PFILT(tmp).cookie,
- dl_tag_array[dl_tag].ifp,
- ioctl_code,
- ioctl_arg);
-
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
- }
- }
-
- if (dl_tag_array[dl_tag].proto->dl_ioctl)
- retval =
- (*dl_tag_array[dl_tag].proto->dl_ioctl)(dl_tag,
- dl_tag_array[dl_tag].ifp,
- ioctl_code,
- ioctl_arg);
- else
- retval = EOPNOTSUPP;
- }
- else
- retval = 0;
- }
-
- if ((retval) && (retval != EOPNOTSUPP)) {
- if (retval == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
-
-
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- TAILQ_FOREACH(tmp, fhead, que) {
- if (IFILT(tmp).filter_if_ioctl) {
- retval2 = (*IFILT(tmp).filter_if_ioctl)(IFILT(tmp).cookie, ifp,
- ioctl_code, ioctl_arg);
- if (retval2) {
- if (retval2 == EJUSTRETURN)
- return 0;
- else
- return retval2;
- }
- }
- }
-
-
- if_family = find_family_module(ifp->if_family);
- if ((if_family) && (if_family->ifmod_ioctl)) {
- retval2 = (*if_family->ifmod_ioctl)(ifp, ioctl_code, ioctl_arg);
-
- if ((retval2) && (retval2 != EOPNOTSUPP)) {
- if (retval2 == EJUSTRETURN)
- return 0;
- else
- return retval;
- }
-
- if (retval == EOPNOTSUPP)
- retval = retval2;
- }
-
- if (ifp->if_ioctl)
- retval2 = (*ifp->if_ioctl)(ifp, ioctl_code, ioctl_arg);
-
- if (retval == EOPNOTSUPP)
- return retval2;
- else {
- if (retval2 == EOPNOTSUPP)
- return 0;
- else
- return retval2;
- }
-}
+destroy:
+ /* Call the detached function if there is one */
+ if (filter->filt_detached)
+ filter->filt_detached(filter->filt_cookie, filter->filt_ifp);
-int
-dlil_attach_protocol(struct dlil_proto_reg_str *proto,
- u_long *dl_tag)
-{
- struct ifnet *ifp;
- struct if_proto *ifproto;
- u_long i;
- struct if_family_str *if_family;
- struct dlil_proto_head *tmp;
- struct kev_dl_proto_data ev_pr_data;
- int s, retval = 0;
- boolean_t funnel_state;
- u_char *p;
-
- if ((proto->protocol_family == 0) || (proto->interface_family == 0))
- return EINVAL;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
- if_family = find_family_module(proto->interface_family);
- if ((!if_family) || (if_family->flags & DLIL_SHUTDOWN)) {
- kprintf("dlil_attach_protocol -- no interface family module %d",
- proto->interface_family);
- retval = ENOENT;
- goto end;
- }
-
- ifp = ifbyfamily(proto->interface_family, proto->unit_number);
- if (!ifp) {
- kprintf("dlil_attach_protocol -- no such interface %d unit %d\n",
- proto->interface_family, proto->unit_number);
- retval = ENOENT;
- goto end;
- }
-
- if (dlil_find_dltag(proto->interface_family, proto->unit_number,
- proto->protocol_family, &i) == 0) {
- retval = EEXIST;
- goto end;
- }
-
- for (i=1; i < dl_tag_nb; i++)
- if (dl_tag_array[i].ifp == 0)
- break;
-
- if (i == dl_tag_nb) {
- // expand the tag array by MAX_DL_TAGS
- MALLOC(p, u_char *, sizeof(struct dl_tag_str) * (dl_tag_nb + MAX_DL_TAGS), M_NKE, M_WAITOK);
- if (p == 0) {
- retval = ENOBUFS;
- goto end;
- }
- bcopy(dl_tag_array, p, sizeof(struct dl_tag_str) * dl_tag_nb);
- bzero(p + sizeof(struct dl_tag_str) * dl_tag_nb, sizeof(struct dl_tag_str) * MAX_DL_TAGS);
- dl_tag_nb += MAX_DL_TAGS;
- FREE(dl_tag_array, M_NKE);
- dl_tag_array = (struct dl_tag_str *)p;
- }
-
- /*
- * Allocate and init a new if_proto structure
- */
-
- ifproto = _MALLOC(sizeof(struct if_proto), M_IFADDR, M_WAITOK);
- if (!ifproto) {
- printf("ERROR - DLIL failed if_proto allocation\n");
- retval = ENOMEM;
- goto end;
- }
-
- bzero(ifproto, sizeof(struct if_proto));
-
- dl_tag_array[i].ifp = ifp;
- dl_tag_array[i].proto = ifproto;
- dl_tag_array[i].pr_flt_head = &ifproto->pr_flt_head;
- ifproto->dl_tag = i;
- *dl_tag = i;
-
- if (proto->default_proto) {
- if (ifp->if_data.default_proto == 0)
- ifp->if_data.default_proto = i;
- else
- printf("ERROR - dlil_attach_protocol -- Attempt to attach more than one default protocol\n");
- }
-
- ifproto->protocol_family = proto->protocol_family;
- ifproto->dl_input = proto->input;
- ifproto->dl_pre_output = proto->pre_output;
- ifproto->dl_event = proto->event;
- ifproto->dl_offer = proto->offer;
- ifproto->dl_ioctl = proto->ioctl;
- ifproto->ifp = ifp;
- TAILQ_INIT(&ifproto->pr_flt_head);
-
- /*
- * Call family module add_proto routine so it can refine the
- * demux descriptors as it wishes.
- */
- retval = (*if_family->add_proto)(&proto->demux_desc_head, ifproto, *dl_tag);
- if (retval) {
- dl_tag_array[i].ifp = 0;
- FREE(ifproto, M_IFADDR);
- goto end;
- }
-
- /*
- * Add to if_proto list for this interface
- */
-
- tmp = (struct dlil_proto_head *) &ifp->proto_head;
- TAILQ_INSERT_TAIL(tmp, ifproto, next);
- ifp->refcnt++;
- if (ifproto->dl_offer)
- ifp->offercnt++;
-
- /* the reserved field carries the number of protocol still attached (subject to change) */
- ev_pr_data.proto_family = proto->protocol_family;
- ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_ATTACHED,
- (struct net_event_data *)&ev_pr_data,
- sizeof(struct kev_dl_proto_data));
+ /* Free the filter */
+ zfree(dlif_filt_zone, filter);
-end:
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return retval;
+ /*
+ * Decrease filter count and route_generation ID to let TCP
+ * know it should reevalute doing TSO or not
+ */
+ OSAddAtomic(-1, &dlil_filter_count);
+ if (use_routegenid)
+ routegenid_update();
+
+done:
+ if (retval != 0) {
+ DLIL_PRINTF("failed to detach %s filter (err=%d)\n",
+ filter->filt_name, retval);
+ }
+ return (retval);
}
+__private_extern__ void
+dlil_detach_filter(interface_filter_t filter)
+{
+ if (filter == NULL)
+ return;
+ dlil_detach_filter_internal(filter, 0);
+}
-
-int
-dlil_detach_protocol(u_long dl_tag)
-{
- struct ifnet *ifp;
- struct ifnet *orig_ifp=0;
- struct if_proto *proto;
- struct dlil_proto_head *tmp;
- struct if_family_str *if_family;
- struct dlil_filterq_entry *filter;
- int s, retval = 0;
- struct dlil_filterq_head *fhead;
- struct kev_dl_proto_data ev_pr_data;
- boolean_t funnel_state;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- if (dl_tag >= dl_tag_nb || dl_tag_array[dl_tag].ifp == 0) {
- retval = ENOENT;
- goto end;
- }
-
- ifp = dl_tag_array[dl_tag].ifp;
- proto = dl_tag_array[dl_tag].proto;
-
- if_family = find_family_module(ifp->if_family);
- if (if_family == NULL) {
- retval = ENOENT;
- goto end;
- }
-
- tmp = (struct dlil_proto_head *) &ifp->proto_head;
-
- /*
- * Call family module del_proto
- */
-
- (*if_family->del_proto)(proto, dl_tag);
-
-
- /*
- * Remove and deallocate any attached protocol filters
- */
-
- while (filter = TAILQ_FIRST(&proto->pr_flt_head))
- dlil_detach_filter(filter->filter_id);
-
- if (proto->dl_offer)
- ifp->offercnt--;
-
- if (ifp->if_data.default_proto == dl_tag)
- ifp->if_data.default_proto = 0;
- dl_tag_array[dl_tag].ifp = 0;
-
- /* the reserved field carries the number of protocol still attached (subject to change) */
- ev_pr_data.proto_family = proto->protocol_family;
-
- /*
- * Cleanup routes that may still be in the routing table for that interface/protocol pair.
- */
-
- if_rtproto_del(ifp, proto->protocol_family);
-
- TAILQ_REMOVE(tmp, proto, next);
- FREE(proto, M_IFADDR);
-
- ifp->refcnt--;
- ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_DETACHED,
- (struct net_event_data *)&ev_pr_data,
- sizeof(struct kev_dl_proto_data));
-
- if (ifp->refcnt == 0) {
-
- TAILQ_REMOVE(&ifnet, ifp, if_link);
-
- (*if_family->del_if)(ifp);
-
- if (--if_family->refcnt == 0) {
- if (if_family->shutdown)
- (*if_family->shutdown)();
-
- TAILQ_REMOVE(&if_family_head, if_family, if_fam_next);
- FREE(if_family, M_IFADDR);
- }
-
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
-
- TAILQ_FOREACH(filter, fhead, que) {
- if (IFILT(filter).filter_if_free) {
- retval = (*IFILT(filter).filter_if_free)(IFILT(filter).cookie, ifp);
- if (retval) {
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return 0;
- }
+static void
+dlil_input_thread_func(struct dlil_threading_info *inputthread)
+{
+ while (1) {
+ struct mbuf *m = NULL, *m_loop = NULL;
+#if IFNET_INPUT_SANITY_CHK
+ int loop_cnt = 0, mbuf_cnt;
+ int count;
+ struct mbuf *m1;
+#endif /* IFNET_INPUT_SANITY_CHK */
+
+ lck_mtx_lock_spin(&inputthread->input_lck);
+
+ /* Wait until there is work to be done */
+ while (!(inputthread->input_waiting & ~DLIL_INPUT_RUNNING)) {
+ inputthread->input_waiting &= ~DLIL_INPUT_RUNNING;
+ msleep(&inputthread->input_waiting,
+ &inputthread->input_lck, 0,
+ inputthread->input_name, 0);
}
- if (ifp != orig_ifp)
- break;
- }
- }
-
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHED, 0, 0);
- (*ifp->if_free)(ifp);
- }
+ lck_mtx_assert(&inputthread->input_lck, LCK_MTX_ASSERT_OWNED);
-end:
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return retval;
-}
+ m = inputthread->mbuf_head;
+ inputthread->mbuf_head = NULL;
+ inputthread->mbuf_tail = NULL;
+ if (inputthread->input_waiting & DLIL_INPUT_TERMINATE) {
+ lck_mtx_unlock(&inputthread->input_lck);
+ if (m != NULL)
+ mbuf_freem_list(m);
+ OSAddAtomic(-1, &cur_dlil_input_threads);
+ lck_mtx_destroy(&inputthread->input_lck,
+ inputthread->lck_grp);
+ lck_grp_free(inputthread->lck_grp);
-int
-dlil_if_attach(struct ifnet *ifp)
-{
- u_long interface_family = ifp->if_family;
- struct if_family_str *if_family;
- struct dlil_proto_head *tmp;
- int stat;
- int s;
- boolean_t funnel_state;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
- if (ifnet_inited == 0) {
- TAILQ_INIT(&ifnet);
- ifnet_inited = 1;
- }
-
- if_family = find_family_module(interface_family);
-
- if ((!if_family) || (if_family->flags & DLIL_SHUTDOWN)) {
- splx(s);
- kprintf("Attempt to attach interface without family module - %d\n",
- interface_family);
- thread_funnel_set(network_flock, funnel_state);
- return ENODEV;
- }
-
- if (ifp->refcnt == 0) {
- /*
- * Call the family module to fill in the appropriate fields in the
- * ifnet structure.
- */
-
- stat = (*if_family->add_if)(ifp);
- if (stat) {
- splx(s);
- kprintf("dlil_if_attach -- add_if failed with %d\n", stat);
- thread_funnel_set(network_flock, funnel_state);
- return stat;
- }
- if_family->refcnt++;
-
- /*
- * Add the ifp to the interface list.
- */
-
- tmp = (struct dlil_proto_head *) &ifp->proto_head;
- TAILQ_INIT(tmp);
-
- ifp->if_data.default_proto = 0;
- ifp->offercnt = 0;
- TAILQ_INIT(&ifp->if_flt_head);
- old_if_attach(ifp);
-
- if (if_family->init_if) {
- stat = (*if_family->init_if)(ifp);
- if (stat) {
- kprintf("dlil_if_attach -- init_if failed with %d\n", stat);
- }
- }
- }
-
- ifp->refcnt++;
-
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_ATTACHED, 0, 0);
-
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return 0;
-}
+ zfree(dlif_inp_zone, inputthread);
+ /* for the extra refcnt from kernel_thread_start() */
+ thread_deallocate(current_thread());
-int
-dlil_if_detach(struct ifnet *ifp)
-{
- struct if_proto *proto;
- struct dlil_filterq_entry *if_filter;
- struct if_family_str *if_family;
- struct dlil_filterq_head *fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- int s;
- struct kev_msg ev_msg;
- boolean_t funnel_state;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- if_family = find_family_module(ifp->if_family);
-
- if (!if_family) {
- kprintf("Attempt to detach interface without family module - %s\n",
- ifp->if_name);
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return ENODEV;
- }
-
- while (if_filter = TAILQ_FIRST(fhead))
- dlil_detach_filter(if_filter->filter_id);
-
- ifp->refcnt--;
-
- if (ifp->refcnt == 0) {
- /* Let BPF know the interface is detaching. */
- bpfdetach(ifp);
- TAILQ_REMOVE(&ifnet, ifp, if_link);
-
- (*if_family->del_if)(ifp);
-
- if (--if_family->refcnt == 0) {
- if (if_family->shutdown)
- (*if_family->shutdown)();
-
- TAILQ_REMOVE(&if_family_head, if_family, if_fam_next);
- FREE(if_family, M_IFADDR);
- }
-
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHED, 0, 0);
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return 0;
- }
- else
- {
- dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHING, 0, 0);
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return DLIL_WAIT_FOR_FREE;
- }
-}
+ /* this is the end */
+ thread_terminate(current_thread());
+ /* NOTREACHED */
+ return;
+ }
+ inputthread->input_waiting |= DLIL_INPUT_RUNNING;
+ inputthread->input_waiting &= ~DLIL_INPUT_WAITING;
-int
-dlil_reg_if_modules(u_long interface_family,
- struct dlil_ifmod_reg_str *ifmod)
-{
- struct if_family_str *if_family;
- int s;
- boolean_t funnel_state;
-
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
- if (find_family_module(interface_family)) {
- kprintf("Attempt to register dlil family module more than once - %d\n",
- interface_family);
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return EEXIST;
- }
-
- if ((!ifmod->add_if) || (!ifmod->del_if) ||
- (!ifmod->add_proto) || (!ifmod->del_proto)) {
- kprintf("dlil_reg_if_modules passed at least one null pointer\n");
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return EINVAL;
- }
-
- /*
- * The following is a gross hack to keep from breaking
- * Vicomsoft's internet gateway on Jaguar. Vicomsoft
- * does not zero the reserved fields in dlil_ifmod_reg_str.
- * As a result, we have to zero any function that used to
- * be reserved fields at the time Vicomsoft built their
- * kext. Radar #2974305
- */
- if (ifmod->reserved[0] != 0 || ifmod->reserved[1] != 0 || ifmod->reserved[2]) {
- if (interface_family == 123) { /* Vicom */
- ifmod->init_if = 0;
- } else {
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return EINVAL;
+ if (inputthread == dlil_lo_thread_ptr) {
+ m_loop = dlil_lo_input_mbuf_head;
+ dlil_lo_input_mbuf_head = NULL;
+ dlil_lo_input_mbuf_tail = NULL;
}
- }
-
- if_family = (struct if_family_str *) _MALLOC(sizeof(struct if_family_str), M_IFADDR, M_WAITOK);
- if (!if_family) {
- kprintf("dlil_reg_if_modules failed allocation\n");
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return ENOMEM;
- }
-
- bzero(if_family, sizeof(struct if_family_str));
-
- if_family->if_family = interface_family & 0xffff;
- if_family->shutdown = ifmod->shutdown;
- if_family->add_if = ifmod->add_if;
- if_family->del_if = ifmod->del_if;
- if_family->init_if = ifmod->init_if;
- if_family->add_proto = ifmod->add_proto;
- if_family->del_proto = ifmod->del_proto;
- if_family->ifmod_ioctl = ifmod->ifmod_ioctl;
- if_family->refcnt = 1;
- if_family->flags = 0;
-
- TAILQ_INSERT_TAIL(&if_family_head, if_family, if_fam_next);
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return 0;
-}
-
-int dlil_dereg_if_modules(u_long interface_family)
-{
- struct if_family_str *if_family;
- int s, ret = 0;
- boolean_t funnel_state;
-
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
- if_family = find_family_module(interface_family);
- if (if_family == 0) {
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return ENOENT;
- }
-
- if (--if_family->refcnt == 0) {
- if (if_family->shutdown)
- (*if_family->shutdown)();
-
- TAILQ_REMOVE(&if_family_head, if_family, if_fam_next);
- FREE(if_family, M_IFADDR);
- }
- else {
- if_family->flags |= DLIL_SHUTDOWN;
- ret = DLIL_WAIT_FOR_FREE;
- }
-
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return ret;
-}
-
-
-
+#if IFNET_INPUT_SANITY_CHK
+ if (dlil_input_sanity_check != 0) {
+ mbuf_cnt = inputthread->mbuf_count;
+ inputthread->mbuf_count = 0;
+ if (inputthread == dlil_lo_thread_ptr) {
+ loop_cnt = dlil_lo_input_mbuf_count;
+ dlil_lo_input_mbuf_count = 0;
+ }
-/*
- * Old if_attach no-op'ed function defined here for temporary backwards compatibility
- */
+ lck_mtx_unlock(&inputthread->input_lck);
-void if_attach(ifp)
- struct ifnet *ifp;
-{
- dlil_if_attach(ifp);
-}
+ for (m1 = m, count = 0; m1; m1 = mbuf_nextpkt(m1)) {
+ count++;
+ }
+ if (count != mbuf_cnt) {
+ panic("%s - thread=%p reg. loop queue "
+ "has %d packets, should have %d\n",
+ __func__, inputthread, count, mbuf_cnt);
+ /* NOTREACHED */
+ }
+ if (inputthread == dlil_lo_thread_ptr) {
+ for (m1 = m_loop, count = 0; m1;
+ m1 = mbuf_nextpkt(m1)) {
+ count++;
+ }
+ if (count != loop_cnt) {
+ panic("%s - thread=%p loop queue "
+ "has %d packets, should have %d\n",
+ __func__, inputthread, count,
+ loop_cnt);
+ /* NOTREACHED */
+ }
+ }
+ } else
+#endif /* IFNET_INPUT_SANITY_CHK */
+ {
+ lck_mtx_unlock(&inputthread->input_lck);
+ }
-int
-dlil_inject_if_input(struct mbuf *m, char *frame_header, u_long from_id)
-{
- struct ifnet *orig_ifp = 0;
- struct ifnet *ifp;
- struct if_proto *ifproto;
- struct if_proto *proto;
- struct dlil_filterq_entry *tmp;
- int retval = 0;
- struct dlil_filterq_head *fhead;
- int match_found;
+ /*
+ * NOTE warning %%% attention !!!!
+ * We should think about putting some thread starvation
+ * safeguards if we deal with long chains of packets.
+ */
+ if (m_loop) {
+ if (inputthread == dlil_lo_thread_ptr) {
+ dlil_input_packet_list(lo_ifp, m_loop);
+ }
+#if IFNET_INPUT_SANITY_CHK
+ else {
+ panic("%s - thread=%p loop queue has %d "
+ "packets, should have none!\n", __func__,
+ inputthread, loop_cnt);
+ /* NOTREACHED */
+ }
+#endif /* IFNET_INPUT_SANITY_CHK */
+ }
- dlil_stats.inject_if_in1++;
+ if (m != NULL)
+ dlil_input_packet_list(0, m);
- if (from_id >= dlil_filters_nb || dlil_filters[from_id].type != DLIL_IF_FILTER)
- return ENOENT;
+ lck_mtx_lock_spin(&inputthread->input_lck);
- ifp = dlil_filters[from_id].ifp;
+ if (inputthread->input_waiting &
+ (DLIL_PROTO_WAITING | DLIL_PROTO_REGISTER)) {
+ lck_mtx_unlock(&inputthread->input_lck);
+ proto_input_run();
+ } else {
+ lck_mtx_unlock(&inputthread->input_lck);
+ }
+ }
+}
-/*
- * Let interface filters (if any) do their thing ...
- */
+errno_t
+ifnet_input(ifnet_t ifp, mbuf_t m_head,
+ const struct ifnet_stat_increment_param *stats)
+{
+ struct thread *tp = current_thread();
+ mbuf_t m_tail;
+ struct dlil_threading_info *inp;
+#if IFNET_INPUT_SANITY_CHK
+ u_int32_t pkt_count = 0;
+#endif /* IFNET_INPUT_SANITY_CHK */
+
+ if (ifp == NULL || m_head == NULL) {
+ if (m_head != NULL)
+ mbuf_freem_list(m_head);
+ return (EINVAL);
+ }
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- match_found = 0;
-
- if (TAILQ_EMPTY(fhead) == 0) {
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
- TAILQ_FOREACH_REVERSE(tmp, fhead, que, dlil_filterq_head) {
- if ((match_found) && (IFILT(tmp).filter_if_input)) {
- retval = (*IFILT(tmp).filter_if_input)(IFILT(tmp).cookie,
- &ifp,
- &m,
- &frame_header);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
+ m_tail = m_head;
+ while (1) {
+#if IFNET_INPUT_SANITY_CHK
+ if (dlil_input_sanity_check != 0) {
+ ifnet_t rcvif;
+
+ rcvif = mbuf_pkthdr_rcvif(m_tail);
+ pkt_count++;
+
+ if (rcvif == NULL ||
+ (ifp->if_type != IFT_LOOP && rcvif != ifp) ||
+ !(mbuf_flags(m_head) & MBUF_PKTHDR)) {
+ panic("%s - invalid mbuf %p\n",
+ __func__, m_tail);
+ /* NOTREACHED */
}
- }
-
}
-
- if (ifp != orig_ifp)
- break;
-
- if (from_id == tmp->filter_id)
- match_found = 1;
- }
+#endif /* IFNET_INPUT_SANITY_CHK */
+ if (mbuf_nextpkt(m_tail) == NULL)
+ break;
+ m_tail = mbuf_nextpkt(m_tail);
}
- }
- ifp->if_lastchange = time;
+ inp = ifp->if_input_thread;
- /*
- * Call family demux module. If the demux module finds a match
- * for the frame it will fill-in the ifproto pointer.
- */
-
- retval = (*ifp->if_demux)(ifp, m, frame_header, &ifproto );
+ if (dlil_multithreaded_input == 0 || inp == NULL)
+ inp = dlil_lo_thread_ptr;
- if (m->m_flags & (M_BCAST|M_MCAST))
- ifp->if_imcasts++;
-
- if ((retval) && (ifp->offercnt)) {
/*
- * No match was found, look for any offers.
+ * If there is a matching dlil input thread associated with an
+ * affinity set, associate this workloop thread with the same set.
+ * We will only do this once.
*/
- struct dlil_proto_head *tmp = (struct dlil_proto_head *) &ifp->proto_head;
- TAILQ_FOREACH(proto, tmp, next) {
- if ((proto->dl_offer) && (proto->dl_offer(m, frame_header) == 0)) {
- ifproto = proto;
- retval = 0;
- break;
- }
+ lck_mtx_lock_spin(&inp->input_lck);
+ if (inp->net_affinity && inp->workloop_thread == NULL) {
+ u_int32_t tag = inp->tag;
+ inp->workloop_thread = tp;
+ lck_mtx_unlock(&inp->input_lck);
+
+ /* Associated the current thread with the new affinity tag */
+ (void) dlil_affinity_set(tp, tag);
+
+ /*
+ * Take a reference on the workloop (current) thread; during
+ * detach, we will need to refer to it in order ot tear down
+ * its affinity.
+ */
+ thread_reference(tp);
+ lck_mtx_lock_spin(&inp->input_lck);
}
- }
- if (retval) {
- if (retval != EJUSTRETURN) {
- m_freem(m);
- return retval;
- }
- else
- return 0;
- }
- else
- if (ifproto == 0) {
- printf("ERROR - dlil_inject_if_input -- if_demux didn't return an if_proto pointer\n");
- m_freem(m);
- return 0;
- }
-
-/*
- * Call any attached protocol filters.
- */
- TAILQ_FOREACH_REVERSE(tmp, &ifproto->pr_flt_head, que, dlil_filterq_head) {
- if (PFILT(tmp).filter_dl_input) {
- retval = (*PFILT(tmp).filter_dl_input)(PFILT(tmp).cookie,
- &m,
- &frame_header,
- &ifp);
-
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
+ /* WARNING
+ * Because of loopbacked multicast we cannot stuff the ifp in
+ * the rcvif of the packet header: loopback has its own dlil
+ * input queue
+ */
+
+ if (inp == dlil_lo_thread_ptr && ifp->if_type == IFT_LOOP) {
+ if (dlil_lo_input_mbuf_head == NULL)
+ dlil_lo_input_mbuf_head = m_head;
+ else if (dlil_lo_input_mbuf_tail != NULL)
+ dlil_lo_input_mbuf_tail->m_nextpkt = m_head;
+ dlil_lo_input_mbuf_tail = m_tail;
+#if IFNET_INPUT_SANITY_CHK
+ if (dlil_input_sanity_check != 0) {
+ dlil_lo_input_mbuf_count += pkt_count;
+ inp->input_mbuf_cnt += pkt_count;
+ inp->input_wake_cnt++;
+
+ lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_OWNED);
+ }
+#endif
+ } else {
+ if (inp->mbuf_head == NULL)
+ inp->mbuf_head = m_head;
+ else if (inp->mbuf_tail != NULL)
+ inp->mbuf_tail->m_nextpkt = m_head;
+ inp->mbuf_tail = m_tail;
+#if IFNET_INPUT_SANITY_CHK
+ if (dlil_input_sanity_check != 0) {
+ inp->mbuf_count += pkt_count;
+ inp->input_mbuf_cnt += pkt_count;
+ inp->input_wake_cnt++;
+
+ lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_OWNED);
}
- }
- }
- }
+#endif
+ }
+ inp->input_waiting |= DLIL_INPUT_WAITING;
+ if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) {
+ wakeup((caddr_t)&inp->input_waiting);
+ }
+ lck_mtx_unlock(&inp->input_lck);
+ if (stats) {
+ atomic_add_64(&ifp->if_data.ifi_ipackets, stats->packets_in);
+ atomic_add_64(&ifp->if_data.ifi_ibytes, stats->bytes_in);
+ atomic_add_64(&ifp->if_data.ifi_ierrors, stats->errors_in);
- retval = (*ifproto->dl_input)(m, frame_header,
- ifp, ifproto->dl_tag,
- FALSE);
-
- dlil_stats.inject_if_in2++;
- if (retval == EJUSTRETURN)
- retval = 0;
- else
- if (retval)
- m_freem(m);
+ atomic_add_64(&ifp->if_data.ifi_opackets, stats->packets_out);
+ atomic_add_64(&ifp->if_data.ifi_obytes, stats->bytes_out);
+ atomic_add_64(&ifp->if_data.ifi_oerrors, stats->errors_out);
- return retval;
+ atomic_add_64(&ifp->if_data.ifi_collisions, stats->collisions);
+ atomic_add_64(&ifp->if_data.ifi_iqdrops, stats->dropped);
+ }
+ return (0);
}
+static int
+dlil_interface_filters_input(struct ifnet *ifp, struct mbuf **m_p,
+ char **frame_header_p, protocol_family_t protocol_family)
+{
+ struct ifnet_filter *filter;
+ /*
+ * Pass the inbound packet to the interface filters
+ */
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ /* prevent filter list from changing in case we drop the lock */
+ if_flt_monitor_busy(ifp);
+ TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
+ int result;
+
+ if (!filter->filt_skip && filter->filt_input != NULL &&
+ (filter->filt_protocol == 0 ||
+ filter->filt_protocol == protocol_family)) {
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ result = (*filter->filt_input)(filter->filt_cookie,
+ ifp, protocol_family, m_p, frame_header_p);
+
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ if (result != 0) {
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ return (result);
+ }
+ }
+ }
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ /*
+ * Strip away M_PROTO1 bit prior to sending packet up the stack as
+ * it is meant to be local to a subsystem -- if_bridge for M_PROTO1
+ */
+ if (*m_p != NULL)
+ (*m_p)->m_flags &= ~M_PROTO1;
+ return (0);
+}
-int
-dlil_inject_pr_input(struct mbuf *m, char *frame_header, u_long from_id)
+static int
+dlil_interface_filters_output(struct ifnet *ifp, struct mbuf **m_p,
+ protocol_family_t protocol_family)
{
- struct ifnet *orig_ifp = 0;
- struct dlil_filterq_entry *tmp;
- int retval;
- struct if_proto *ifproto = 0;
- int match_found;
- struct ifnet *ifp;
+ struct ifnet_filter *filter;
- dlil_stats.inject_pr_in1++;
- if (from_id >= dlil_filters_nb || dlil_filters[from_id].type != DLIL_PR_FILTER)
- return ENOENT;
+ /*
+ * Pass the outbound packet to the interface filters
+ */
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ /* prevent filter list from changing in case we drop the lock */
+ if_flt_monitor_busy(ifp);
+ TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
+ int result;
+
+ if (!filter->filt_skip && filter->filt_output != NULL &&
+ (filter->filt_protocol == 0 ||
+ filter->filt_protocol == protocol_family)) {
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ result = filter->filt_output(filter->filt_cookie, ifp,
+ protocol_family, m_p);
+
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ if (result != 0) {
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ return (result);
+ }
+ }
+ }
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
- ifproto = dlil_filters[from_id].proto;
- ifp = dlil_filters[from_id].ifp;
+ return (0);
+}
-/*
- * Call any attached protocol filters.
- */
+static void
+dlil_ifproto_input(struct if_proto * ifproto, mbuf_t m)
+{
+ int error;
+
+ if (ifproto->proto_kpi == kProtoKPI_v1) {
+ /* Version 1 protocols get one packet at a time */
+ while (m != NULL) {
+ char * frame_header;
+ mbuf_t next_packet;
+
+ next_packet = m->m_nextpkt;
+ m->m_nextpkt = NULL;
+ frame_header = m->m_pkthdr.header;
+ m->m_pkthdr.header = NULL;
+ error = (*ifproto->kpi.v1.input)(ifproto->ifp,
+ ifproto->protocol_family, m, frame_header);
+ if (error != 0 && error != EJUSTRETURN)
+ m_freem(m);
+ m = next_packet;
+ }
+ } else if (ifproto->proto_kpi == kProtoKPI_v2) {
+ /* Version 2 protocols support packet lists */
+ error = (*ifproto->kpi.v2.input)(ifproto->ifp,
+ ifproto->protocol_family, m);
+ if (error != 0 && error != EJUSTRETURN)
+ m_freem_list(m);
+ }
+ return;
+}
+
+__private_extern__ void
+dlil_input_packet_list(struct ifnet * ifp_param, struct mbuf *m)
+{
+ int error = 0;
+ protocol_family_t protocol_family;
+ mbuf_t next_packet;
+ ifnet_t ifp = ifp_param;
+ char * frame_header;
+ struct if_proto * last_ifproto = NULL;
+ mbuf_t pkt_first = NULL;
+ mbuf_t * pkt_next = NULL;
+
+ KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_START,0,0,0,0,0);
+
+
+ while (m != NULL) {
+ struct if_proto *ifproto = NULL;
+ int iorefcnt = 0;
+
+ if (ifp_param == NULL)
+ ifp = m->m_pkthdr.rcvif;
+
+ /* Check if this mbuf looks valid */
+ MBUF_INPUT_CHECK(m, ifp);
+
+ next_packet = m->m_nextpkt;
+ m->m_nextpkt = NULL;
+ frame_header = m->m_pkthdr.header;
+ m->m_pkthdr.header = NULL;
+
+ /* Get an IO reference count if the interface is not
+ * loopback and it is attached.
+ */
+ if (ifp != lo_ifp) {
+ if (!ifnet_is_attached(ifp, 1)) {
+ m_freem(m);
+ goto next;
+ }
+ iorefcnt = 1;
+ }
+
+ switch (m->m_pkthdr.prio) {
+ case MBUF_TC_BK:
+ atomic_add_64(&ifp->if_tc.ifi_ibkpackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_ibkbytes, m->m_pkthdr.len);
+ break;
+ case MBUF_TC_VI:
+ atomic_add_64(&ifp->if_tc.ifi_ivipackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_ivibytes, m->m_pkthdr.len);
+ break;
+ case MBUF_TC_VO:
+ atomic_add_64(&ifp->if_tc.ifi_ivopackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_ivobytes, m->m_pkthdr.len);
+ break;
+ default:
+ break;
+ }
+
+ /* find which protocol family this packet is for */
+ ifnet_lock_shared(ifp);
+ error = (*ifp->if_demux)(ifp, m, frame_header,
+ &protocol_family);
+ ifnet_lock_done(ifp);
+ if (error != 0) {
+ if (error == EJUSTRETURN)
+ goto next;
+ protocol_family = 0;
+ }
+
+ if (m->m_flags & (M_BCAST|M_MCAST))
+ atomic_add_64(&ifp->if_imcasts, 1);
+
+ /* run interface filters, exclude VLAN packets PR-3586856 */
+ if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) {
+ error = dlil_interface_filters_input(ifp, &m,
+ &frame_header, protocol_family);
+ if (error != 0) {
+ if (error != EJUSTRETURN)
+ m_freem(m);
+ goto next;
+ }
+ }
+ if (error != 0 || ((m->m_flags & M_PROMISC) != 0) ) {
+ m_freem(m);
+ goto next;
+ }
- match_found = 0;
- TAILQ_FOREACH_REVERSE(tmp, &ifproto->pr_flt_head, que, dlil_filterq_head) {
- if ((match_found) && (PFILT(tmp).filter_dl_input)) {
- retval = (*PFILT(tmp).filter_dl_input)(PFILT(tmp).cookie,
- &m,
- &frame_header,
- &ifp);
-
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
+ /* Lookup the protocol attachment to this interface */
+ if (protocol_family == 0) {
+ ifproto = NULL;
+ } else if (last_ifproto != NULL && last_ifproto->ifp == ifp &&
+ (last_ifproto->protocol_family == protocol_family)) {
+ VERIFY(ifproto == NULL);
+ ifproto = last_ifproto;
+ if_proto_ref(last_ifproto);
+ } else {
+ VERIFY(ifproto == NULL);
+ ifnet_lock_shared(ifp);
+ /* callee holds a proto refcnt upon success */
+ ifproto = find_attached_proto(ifp, protocol_family);
+ ifnet_lock_done(ifp);
+ }
+ if (ifproto == NULL) {
+ /* no protocol for this packet, discard */
+ m_freem(m);
+ goto next;
+ }
+ if (ifproto != last_ifproto) {
+ if (last_ifproto != NULL) {
+ /* pass up the list for the previous protocol */
+ dlil_ifproto_input(last_ifproto, pkt_first);
+ pkt_first = NULL;
+ if_proto_free(last_ifproto);
+ }
+ last_ifproto = ifproto;
+ if_proto_ref(ifproto);
+ }
+ /* extend the list */
+ m->m_pkthdr.header = frame_header;
+ if (pkt_first == NULL) {
+ pkt_first = m;
+ } else {
+ *pkt_next = m;
+ }
+ pkt_next = &m->m_nextpkt;
+
+next:
+ if (next_packet == NULL && last_ifproto != NULL) {
+ /* pass up the last list of packets */
+ dlil_ifproto_input(last_ifproto, pkt_first);
+ if_proto_free(last_ifproto);
+ last_ifproto = NULL;
}
- }
- }
-
- if (tmp->filter_id == from_id)
- match_found = 1;
- }
-
-
- retval = (*ifproto->dl_input)(m, frame_header,
- ifp, ifproto->dl_tag,
- FALSE);
-
- if (retval == EJUSTRETURN)
- retval = 0;
- else
- if (retval)
- m_freem(m);
-
- dlil_stats.inject_pr_in2++;
- return retval;
+ if (ifproto != NULL) {
+ if_proto_free(ifproto);
+ ifproto = NULL;
+ }
+
+ m = next_packet;
+
+ /* update the driver's multicast filter, if needed */
+ if (ifp->if_updatemcasts > 0 && if_mcasts_update(ifp) == 0)
+ ifp->if_updatemcasts = 0;
+ if (iorefcnt == 1)
+ ifnet_decr_iorefcnt(ifp);
+ }
+
+ KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_END,0,0,0,0,0);
+ return;
+}
+
+errno_t
+if_mcasts_update(struct ifnet *ifp)
+{
+ errno_t err;
+
+ err = ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
+ if (err == EAFNOSUPPORT)
+ err = 0;
+ printf("%s%d: %s %d suspended link-layer multicast membership(s) "
+ "(err=%d)\n", ifp->if_name, ifp->if_unit,
+ (err == 0 ? "successfully restored" : "failed to restore"),
+ ifp->if_updatemcasts, err);
+
+ /* just return success */
+ return (0);
}
+static int
+dlil_event_internal(struct ifnet *ifp, struct kev_msg *event)
+{
+ struct ifnet_filter *filter;
+ /* Get an io ref count if the interface is attached */
+ if (!ifnet_is_attached(ifp, 1))
+ goto done;
-int
-dlil_inject_pr_output(struct mbuf *m,
- struct sockaddr *dest,
- int raw,
- char *frame_type,
- char *dst_linkaddr,
- u_long from_id)
-{
- struct ifnet *orig_ifp = 0;
- struct ifnet *ifp;
- struct dlil_filterq_entry *tmp;
- int retval = 0;
- char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4];
- char dst_linkaddr_buffer[MAX_LINKADDR * 4];
- struct dlil_filterq_head *fhead;
- int match_found;
- u_long dl_tag;
-
- dlil_stats.inject_pr_out1++;
- if (raw == 0) {
- if (frame_type)
- bcopy(frame_type, &frame_type_buffer[0], MAX_FRAME_TYPE_SIZE * 4);
- else
- return EINVAL;
+ /*
+ * Pass the event to the interface filters
+ */
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ /* prevent filter list from changing in case we drop the lock */
+ if_flt_monitor_busy(ifp);
+ TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
+ if (filter->filt_event != NULL) {
+ lck_mtx_unlock(&ifp->if_flt_lock);
- if (dst_linkaddr)
- bcopy(dst_linkaddr, &dst_linkaddr_buffer, MAX_LINKADDR * 4);
- else
- return EINVAL;
- }
+ filter->filt_event(filter->filt_cookie, ifp,
+ filter->filt_protocol, event);
- if (from_id >= dlil_filters_nb || dlil_filters[from_id].type != DLIL_PR_FILTER)
- return ENOENT;
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ }
+ }
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
- ifp = dlil_filters[from_id].ifp;
- dl_tag = dlil_filters[from_id].proto->dl_tag;
+ ifnet_lock_shared(ifp);
+ if (ifp->if_proto_hash != NULL) {
+ int i;
- frame_type = frame_type_buffer;
- dst_linkaddr = dst_linkaddr_buffer;
+ for (i = 0; i < PROTO_HASH_SLOTS; i++) {
+ struct if_proto *proto;
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
-
-/*
- * Run any attached protocol filters.
- */
- match_found = 0;
-
- if (TAILQ_EMPTY(dl_tag_array[dl_tag].pr_flt_head) == 0) {
- TAILQ_FOREACH(tmp, dl_tag_array[dl_tag].pr_flt_head, que) {
- if ((match_found) && (PFILT(tmp).filter_dl_output)) {
- retval = (*PFILT(tmp).filter_dl_output)(PFILT(tmp).cookie,
- &m, &ifp, &dest, dst_linkaddr, frame_type);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
- }
+ SLIST_FOREACH(proto, &ifp->if_proto_hash[i],
+ next_hash) {
+ proto_media_event eventp =
+ (proto->proto_kpi == kProtoKPI_v1 ?
+ proto->kpi.v1.event :
+ proto->kpi.v2.event);
+
+ if (eventp != NULL) {
+ if_proto_ref(proto);
+ ifnet_lock_done(ifp);
+
+ eventp(ifp, proto->protocol_family,
+ event);
+
+ ifnet_lock_shared(ifp);
+ if_proto_free(proto);
+ }
+ }
}
- }
+ }
+ ifnet_lock_done(ifp);
+
+ /* Pass the event to the interface */
+ if (ifp->if_event != NULL)
+ ifp->if_event(ifp, event);
+
+ /* Release the io ref count */
+ ifnet_decr_iorefcnt(ifp);
+
+done:
+ return (kev_post_msg(event));
+}
+
+errno_t
+ifnet_event(ifnet_t ifp, struct kern_event_msg *event)
+{
+ struct kev_msg kev_msg;
+ int result = 0;
+
+ if (ifp == NULL || event == NULL)
+ return (EINVAL);
+
+ bzero(&kev_msg, sizeof (kev_msg));
+ kev_msg.vendor_code = event->vendor_code;
+ kev_msg.kev_class = event->kev_class;
+ kev_msg.kev_subclass = event->kev_subclass;
+ kev_msg.event_code = event->event_code;
+ kev_msg.dv[0].data_ptr = &event->event_data[0];
+ kev_msg.dv[0].data_length = event->total_size - KEV_MSG_HEADER_SIZE;
+ kev_msg.dv[1].data_length = 0;
+
+ result = dlil_event_internal(ifp, &kev_msg);
- if (tmp->filter_id == from_id)
- match_found = 1;
+ return (result);
+}
+
+#if CONFIG_MACF_NET
+#include <netinet/ip6.h>
+#include <netinet/ip.h>
+static int
+dlil_get_socket_type(struct mbuf **mp, int family, int raw)
+{
+ struct mbuf *m;
+ struct ip *ip;
+ struct ip6_hdr *ip6;
+ int type = SOCK_RAW;
+
+ if (!raw) {
+ switch (family) {
+ case PF_INET:
+ m = m_pullup(*mp, sizeof(struct ip));
+ if (m == NULL)
+ break;
+ *mp = m;
+ ip = mtod(m, struct ip *);
+ if (ip->ip_p == IPPROTO_TCP)
+ type = SOCK_STREAM;
+ else if (ip->ip_p == IPPROTO_UDP)
+ type = SOCK_DGRAM;
+ break;
+ case PF_INET6:
+ m = m_pullup(*mp, sizeof(struct ip6_hdr));
+ if (m == NULL)
+ break;
+ *mp = m;
+ ip6 = mtod(m, struct ip6_hdr *);
+ if (ip6->ip6_nxt == IPPROTO_TCP)
+ type = SOCK_STREAM;
+ else if (ip6->ip6_nxt == IPPROTO_UDP)
+ type = SOCK_DGRAM;
+ break;
+ }
}
- }
+ return (type);
+}
+#endif
+
+static void
+if_inc_traffic_class_out(ifnet_t ifp, mbuf_t m)
+{
+ if (!(m->m_flags & M_PKTHDR))
+ return;
+
+ switch (m->m_pkthdr.prio) {
+ case MBUF_TC_BK:
+ atomic_add_64(&ifp->if_tc.ifi_obkpackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_obkbytes, m->m_pkthdr.len);
+ break;
+ case MBUF_TC_VI:
+ atomic_add_64(&ifp->if_tc.ifi_ovipackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_ovibytes, m->m_pkthdr.len);
+ break;
+ case MBUF_TC_VO:
+ atomic_add_64(&ifp->if_tc.ifi_ovopackets, 1);
+ atomic_add_64(&ifp->if_tc.ifi_ovobytes, m->m_pkthdr.len);
+ break;
+ default:
+ break;
+ }
+}
/*
- * Call framing module
+ * dlil_output
+ *
+ * Caller should have a lock on the protocol domain if the protocol
+ * doesn't support finer grained locking. In most cases, the lock
+ * will be held from the socket layer and won't be released until
+ * we return back to the socket layer.
+ *
+ * This does mean that we must take a protocol lock before we take
+ * an interface lock if we're going to take both. This makes sense
+ * because a protocol is likely to interact with an ifp while it
+ * is under the protocol lock.
*/
- if ((raw == 0) && (ifp->if_framer)) {
- retval = (*ifp->if_framer)(ifp, &m, dest, dst_linkaddr, frame_type);
+errno_t
+dlil_output(ifnet_t ifp, protocol_family_t proto_family, mbuf_t packetlist,
+ void *route, const struct sockaddr *dest, int raw)
+{
+ char *frame_type = NULL;
+ char *dst_linkaddr = NULL;
+ int retval = 0;
+ char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4];
+ char dst_linkaddr_buffer[MAX_LINKADDR * 4];
+ struct if_proto *proto = NULL;
+ mbuf_t m;
+ mbuf_t send_head = NULL;
+ mbuf_t *send_tail = &send_head;
+ int iorefcnt = 0;
+
+ KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_START,0,0,0,0,0);
+
+ /* Get an io refcnt if the interface is attached to prevent ifnet_detach
+ * from happening while this operation is in progress */
+ if (!ifnet_is_attached(ifp, 1)) {
+ retval = ENXIO;
+ goto cleanup;
+ }
+ iorefcnt = 1;
+
+ /* update the driver's multicast filter, if needed */
+ if (ifp->if_updatemcasts > 0 && if_mcasts_update(ifp) == 0)
+ ifp->if_updatemcasts = 0;
+
+ frame_type = frame_type_buffer;
+ dst_linkaddr = dst_linkaddr_buffer;
+
+ if (raw == 0) {
+ ifnet_lock_shared(ifp);
+ /* callee holds a proto refcnt upon success */
+ proto = find_attached_proto(ifp, proto_family);
+ if (proto == NULL) {
+ ifnet_lock_done(ifp);
+ retval = ENXIO;
+ goto cleanup;
+ }
+ ifnet_lock_done(ifp);
+ }
+
+preout_again:
+ if (packetlist == NULL)
+ goto cleanup;
+
+ m = packetlist;
+ packetlist = packetlist->m_nextpkt;
+ m->m_nextpkt = NULL;
+
+ if (raw == 0) {
+ proto_media_preout preoutp = (proto->proto_kpi == kProtoKPI_v1 ?
+ proto->kpi.v1.pre_output : proto->kpi.v2.pre_output);
+ retval = 0;
+ if (preoutp != NULL) {
+ retval = preoutp(ifp, proto_family, &m, dest, route,
+ frame_type, dst_linkaddr);
+
+ if (retval != 0) {
+ if (retval == EJUSTRETURN)
+ goto preout_again;
+ m_freem(m);
+ goto cleanup;
+ }
+ }
+ }
+
+#if CONFIG_MACF_NET
+ retval = mac_ifnet_check_transmit(ifp, m, proto_family,
+ dlil_get_socket_type(&m, proto_family, raw));
if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else
- {
m_freem(m);
- return retval;
- }
- }
- }
-
-
-#if BRIDGE
- if (do_bridge) {
- struct mbuf *m0 = m ;
- struct ether_header *eh = mtod(m, struct ether_header *);
-
- if (m->m_pkthdr.rcvif)
- m->m_pkthdr.rcvif = NULL ;
- ifp = bridge_dst_lookup(eh);
- bdg_forward(&m0, ifp);
- if (m0)
- m_freem(m0);
-
- return 0;
- }
+ goto cleanup;
+ }
#endif
+ do {
+#if CONFIG_DTRACE
+ if (proto_family == PF_INET) {
+ struct ip *ip = mtod(m, struct ip*);
+ DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL,
+ struct ip *, ip, struct ifnet *, ifp,
+ struct ip *, ip, struct ip6_hdr *, NULL);
+
+ } else if (proto_family == PF_INET6) {
+ struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
+ DTRACE_IP6(send, struct mbuf*, m, struct inpcb *, NULL,
+ struct ip6_hdr *, ip6, struct ifnet*, ifp,
+ struct ip*, NULL, struct ip6_hdr *, ip6);
+ }
+#endif /* CONFIG_DTRACE */
+
+ if (raw == 0 && ifp->if_framer) {
+ int rcvif_set = 0;
+
+ /*
+ * If this is a broadcast packet that needs to be
+ * looped back into the system, set the inbound ifp
+ * to that of the outbound ifp. This will allow
+ * us to determine that it is a legitimate packet
+ * for the system. Only set the ifp if it's not
+ * already set, just to be safe.
+ */
+ if ((m->m_flags & (M_BCAST | M_LOOP)) &&
+ m->m_pkthdr.rcvif == NULL) {
+ m->m_pkthdr.rcvif = ifp;
+ rcvif_set = 1;
+ }
-/*
- * Let interface filters (if any) do their thing ...
- */
+ retval = ifp->if_framer(ifp, &m, dest, dst_linkaddr,
+ frame_type);
+ if (retval) {
+ if (retval != EJUSTRETURN)
+ m_freem(m);
+ goto next;
+ }
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- if (TAILQ_EMPTY(fhead) == 0) {
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
- TAILQ_FOREACH(tmp, fhead, que) {
- if (IFILT(tmp).filter_if_output) {
- retval = (*IFILT(tmp).filter_if_output)(IFILT(tmp).cookie,
- &ifp,
- &m);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
+ /*
+ * Clear the ifp if it was set above, and to be
+ * safe, only if it is still the same as the
+ * outbound ifp we have in context. If it was
+ * looped back, then a copy of it was sent to the
+ * loopback interface with the rcvif set, and we
+ * are clearing the one that will go down to the
+ * layer below.
+ */
+ if (rcvif_set && m->m_pkthdr.rcvif == ifp)
+ m->m_pkthdr.rcvif = NULL;
+ }
+
+ /*
+ * Let interface filters (if any) do their thing ...
+ */
+ /* Do not pass VLAN tagged packets to filters PR-3586856 */
+ if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) {
+ retval = dlil_interface_filters_output(ifp,
+ &m, proto_family);
+ if (retval != 0) {
+ if (retval != EJUSTRETURN)
+ m_freem(m);
+ goto next;
}
- }
+ }
+ /*
+ * Strip away M_PROTO1 bit prior to sending packet to the driver
+ * as this field may be used by the driver
+ */
+ m->m_flags &= ~M_PROTO1;
+
+ /*
+ * If the underlying interface is not capable of handling a
+ * packet whose data portion spans across physically disjoint
+ * pages, we need to "normalize" the packet so that we pass
+ * down a chain of mbufs where each mbuf points to a span that
+ * resides in the system page boundary. If the packet does
+ * not cross page(s), the following is a no-op.
+ */
+ if (!(ifp->if_hwassist & IFNET_MULTIPAGES)) {
+ if ((m = m_normalize(m)) == NULL)
+ goto next;
+ }
+
+ /*
+ * If this is a TSO packet, make sure the interface still
+ * advertise TSO capability.
+ */
+
+ if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV4) &&
+ !(ifp->if_hwassist & IFNET_TSO_IPV4)) {
+ retval = EMSGSIZE;
+ m_freem(m);
+ goto cleanup;
+ }
+ if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV6) &&
+ !(ifp->if_hwassist & IFNET_TSO_IPV6)) {
+ retval = EMSGSIZE;
+ m_freem(m);
+ goto cleanup;
}
-
- if (ifp != orig_ifp)
- break;
- }
+
+ /*
+ * Finally, call the driver.
+ */
+ if ((ifp->if_eflags & IFEF_SENDLIST) != 0) {
+ *send_tail = m;
+ send_tail = &m->m_nextpkt;
+ } else {
+ if_inc_traffic_class_out(ifp, m);
+ KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START,
+ 0,0,0,0,0);
+ retval = ifp->if_output(ifp, m);
+ if (retval && dlil_verbose) {
+ printf("%s: output error on %s%d retval = %d\n",
+ __func__, ifp->if_name, ifp->if_unit,
+ retval);
+ }
+ KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END,
+ 0,0,0,0,0);
+ }
+ KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
+
+next:
+ m = packetlist;
+ if (m) {
+ packetlist = packetlist->m_nextpkt;
+ m->m_nextpkt = NULL;
+ }
+ } while (m);
+
+ if (send_head) {
+ if_inc_traffic_class_out(ifp, send_head);
+
+ KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
+ retval = ifp->if_output(ifp, send_head);
+ if (retval && dlil_verbose) {
+ printf("%s: output error on %s%d retval = %d\n",
+ __func__, ifp->if_name, ifp->if_unit, retval);
+ }
+ KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
}
- }
-/*
- * Finally, call the driver.
- */
+ KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_END,0,0,0,0,0);
- retval = (*ifp->if_output)(ifp, m);
- dlil_stats.inject_pr_out2++;
- if ((retval == 0) || (retval == EJUSTRETURN))
- return 0;
- else
- return retval;
-}
+cleanup:
+ if (proto != NULL)
+ if_proto_free(proto);
+ if (packetlist) /* if any packets are left, clean up */
+ mbuf_freem_list(packetlist);
+ if (retval == EJUSTRETURN)
+ retval = 0;
+ if (iorefcnt == 1)
+ ifnet_decr_iorefcnt(ifp);
+ return (retval);
+}
-int
-dlil_inject_if_output(struct mbuf *m, u_long from_id)
+errno_t
+ifnet_ioctl(ifnet_t ifp, protocol_family_t proto_fam, u_long ioctl_code,
+ void *ioctl_arg)
{
- struct ifnet *orig_ifp = 0;
- struct ifnet *ifp;
- struct dlil_filterq_entry *tmp;
- int retval = 0;
- struct dlil_filterq_head *fhead;
- int match_found;
-
- dlil_stats.inject_if_out1++;
- if (from_id > dlil_filters_nb || dlil_filters[from_id].type != DLIL_IF_FILTER)
- return ENOENT;
+ struct ifnet_filter *filter;
+ int retval = EOPNOTSUPP;
+ int result = 0;
- ifp = dlil_filters[from_id].ifp;
+ if (ifp == NULL || ioctl_code == 0)
+ return (EINVAL);
-/*
- * Let interface filters (if any) do their thing ...
- */
+ /* Get an io ref count if the interface is attached */
+ if (!ifnet_is_attached(ifp, 1))
+ return (EOPNOTSUPP);
- fhead = (struct dlil_filterq_head *) &ifp->if_flt_head;
- match_found = 0;
-
- if (TAILQ_EMPTY(fhead) == 0) {
- while (orig_ifp != ifp) {
- orig_ifp = ifp;
- TAILQ_FOREACH(tmp, fhead, que) {
- if ((match_found) && (IFILT(tmp).filter_if_output)) {
- retval = (*IFILT(tmp).filter_if_output)(IFILT(tmp).cookie,
- &ifp,
- &m);
- if (retval) {
- if (retval == EJUSTRETURN)
- return 0;
- else {
- m_freem(m);
- return retval;
+ /* Run the interface filters first.
+ * We want to run all filters before calling the protocol,
+ * interface family, or interface.
+ */
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ /* prevent filter list from changing in case we drop the lock */
+ if_flt_monitor_busy(ifp);
+ TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
+ if (filter->filt_ioctl != NULL && (filter->filt_protocol == 0 ||
+ filter->filt_protocol == proto_fam)) {
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ result = filter->filt_ioctl(filter->filt_cookie, ifp,
+ proto_fam, ioctl_code, ioctl_arg);
+
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+
+ /* Only update retval if no one has handled the ioctl */
+ if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
+ if (result == ENOTSUP)
+ result = EOPNOTSUPP;
+ retval = result;
+ if (retval != 0 && retval != EOPNOTSUPP) {
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+ goto cleanup;
+ }
}
- }
+ }
+ }
+ /* we're done with the filter list */
+ if_flt_monitor_unbusy(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ /* Allow the protocol to handle the ioctl */
+ if (proto_fam != 0) {
+ struct if_proto *proto;
+
+ /* callee holds a proto refcnt upon success */
+ ifnet_lock_shared(ifp);
+ proto = find_attached_proto(ifp, proto_fam);
+ ifnet_lock_done(ifp);
+ if (proto != NULL) {
+ proto_media_ioctl ioctlp =
+ (proto->proto_kpi == kProtoKPI_v1 ?
+ proto->kpi.v1.ioctl : proto->kpi.v2.ioctl);
+ result = EOPNOTSUPP;
+ if (ioctlp != NULL)
+ result = ioctlp(ifp, proto_fam, ioctl_code,
+ ioctl_arg);
+ if_proto_free(proto);
+
+ /* Only update retval if no one has handled the ioctl */
+ if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
+ if (result == ENOTSUP)
+ result = EOPNOTSUPP;
+ retval = result;
+ if (retval && retval != EOPNOTSUPP)
+ goto cleanup;
+ }
+ }
+ }
+
+ /* retval is either 0 or EOPNOTSUPP */
+ /*
+ * Let the interface handle this ioctl.
+ * If it returns EOPNOTSUPP, ignore that, we may have
+ * already handled this in the protocol or family.
+ */
+ if (ifp->if_ioctl)
+ result = (*ifp->if_ioctl)(ifp, ioctl_code, ioctl_arg);
+
+ /* Only update retval if no one has handled the ioctl */
+ if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
+ if (result == ENOTSUP)
+ result = EOPNOTSUPP;
+ retval = result;
+ if (retval && retval != EOPNOTSUPP) {
+ goto cleanup;
}
-
- if (ifp != orig_ifp)
- break;
+ }
+
+cleanup:
+ if (retval == EJUSTRETURN)
+ retval = 0;
+
+ ifnet_decr_iorefcnt(ifp);
+
+ return (retval);
+}
+
+__private_extern__ errno_t
+dlil_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode, bpf_packet_func callback)
+{
+ errno_t error = 0;
+
+
+ if (ifp->if_set_bpf_tap) {
+ /* Get an io reference on the interface if it is attached */
+ if (!ifnet_is_attached(ifp, 1))
+ return ENXIO;
+ error = ifp->if_set_bpf_tap(ifp, mode, callback);
+ ifnet_decr_iorefcnt(ifp);
+ }
+ return (error);
+}
+
+errno_t
+dlil_resolve_multi(struct ifnet *ifp, const struct sockaddr *proto_addr,
+ struct sockaddr *ll_addr, size_t ll_len)
+{
+ errno_t result = EOPNOTSUPP;
+ struct if_proto *proto;
+ const struct sockaddr *verify;
+ proto_media_resolve_multi resolvep;
+
+ if (!ifnet_is_attached(ifp, 1))
+ return result;
+
+ bzero(ll_addr, ll_len);
+
+ /* Call the protocol first; callee holds a proto refcnt upon success */
+ ifnet_lock_shared(ifp);
+ proto = find_attached_proto(ifp, proto_addr->sa_family);
+ ifnet_lock_done(ifp);
+ if (proto != NULL) {
+ resolvep = (proto->proto_kpi == kProtoKPI_v1 ?
+ proto->kpi.v1.resolve_multi : proto->kpi.v2.resolve_multi);
+ if (resolvep != NULL)
+ result = resolvep(ifp, proto_addr,
+ (struct sockaddr_dl*)ll_addr, ll_len);
+ if_proto_free(proto);
+ }
+
+ /* Let the interface verify the multicast address */
+ if ((result == EOPNOTSUPP || result == 0) && ifp->if_check_multi) {
+ if (result == 0)
+ verify = ll_addr;
+ else
+ verify = proto_addr;
+ result = ifp->if_check_multi(ifp, verify);
+ }
+
+ ifnet_decr_iorefcnt(ifp);
+ return (result);
+}
+
+__private_extern__ errno_t
+dlil_send_arp_internal(ifnet_t ifp, u_short arpop,
+ const struct sockaddr_dl* sender_hw, const struct sockaddr* sender_proto,
+ const struct sockaddr_dl* target_hw, const struct sockaddr* target_proto)
+{
+ struct if_proto *proto;
+ errno_t result = 0;
+
+ /* callee holds a proto refcnt upon success */
+ ifnet_lock_shared(ifp);
+ proto = find_attached_proto(ifp, target_proto->sa_family);
+ ifnet_lock_done(ifp);
+ if (proto == NULL) {
+ result = ENOTSUP;
+ } else {
+ proto_media_send_arp arpp;
+ arpp = (proto->proto_kpi == kProtoKPI_v1 ?
+ proto->kpi.v1.send_arp : proto->kpi.v2.send_arp);
+ if (arpp == NULL)
+ result = ENOTSUP;
+ else
+ result = arpp(ifp, arpop, sender_hw, sender_proto,
+ target_hw, target_proto);
+ if_proto_free(proto);
+ }
+
+ return (result);
+}
+
+static __inline__ int
+_is_announcement(const struct sockaddr_in * sender_sin,
+ const struct sockaddr_in * target_sin)
+{
+ if (sender_sin == NULL) {
+ return (FALSE);
+ }
+ return (sender_sin->sin_addr.s_addr == target_sin->sin_addr.s_addr);
+}
+
+__private_extern__ errno_t
+dlil_send_arp(ifnet_t ifp, u_short arpop, const struct sockaddr_dl* sender_hw,
+ const struct sockaddr* sender_proto, const struct sockaddr_dl* target_hw,
+ const struct sockaddr* target_proto)
+{
+ errno_t result = 0;
+ const struct sockaddr_in * sender_sin;
+ const struct sockaddr_in * target_sin;
+
+ if (target_proto == NULL || (sender_proto != NULL &&
+ sender_proto->sa_family != target_proto->sa_family))
+ return (EINVAL);
+
+ /*
+ * If this is an ARP request and the target IP is IPv4LL,
+ * send the request on all interfaces. The exception is
+ * an announcement, which must only appear on the specific
+ * interface.
+ */
+ sender_sin = (const struct sockaddr_in *)sender_proto;
+ target_sin = (const struct sockaddr_in *)target_proto;
+ if (target_proto->sa_family == AF_INET &&
+ IN_LINKLOCAL(ntohl(target_sin->sin_addr.s_addr)) &&
+ ipv4_ll_arp_aware != 0 && arpop == ARPOP_REQUEST &&
+ !_is_announcement(target_sin, sender_sin)) {
+ ifnet_t *ifp_list;
+ u_int32_t count;
+ u_int32_t ifp_on;
+
+ result = ENOTSUP;
+
+ if (ifnet_list_get(IFNET_FAMILY_ANY, &ifp_list, &count) == 0) {
+ for (ifp_on = 0; ifp_on < count; ifp_on++) {
+ errno_t new_result;
+ ifaddr_t source_hw = NULL;
+ ifaddr_t source_ip = NULL;
+ struct sockaddr_in source_ip_copy;
+ struct ifnet *cur_ifp = ifp_list[ifp_on];
+
+ /*
+ * Only arp on interfaces marked for IPv4LL
+ * ARPing. This may mean that we don't ARP on
+ * the interface the subnet route points to.
+ */
+ if (!(cur_ifp->if_eflags & IFEF_ARPLL))
+ continue;
+
+ /* Find the source IP address */
+ ifnet_lock_shared(cur_ifp);
+ source_hw = cur_ifp->if_lladdr;
+ TAILQ_FOREACH(source_ip, &cur_ifp->if_addrhead,
+ ifa_link) {
+ IFA_LOCK(source_ip);
+ if (source_ip->ifa_addr != NULL &&
+ source_ip->ifa_addr->sa_family ==
+ AF_INET) {
+ /* Copy the source IP address */
+ source_ip_copy =
+ *(struct sockaddr_in *)
+ source_ip->ifa_addr;
+ IFA_UNLOCK(source_ip);
+ break;
+ }
+ IFA_UNLOCK(source_ip);
+ }
+
+ /* No IP Source, don't arp */
+ if (source_ip == NULL) {
+ ifnet_lock_done(cur_ifp);
+ continue;
+ }
+
+ IFA_ADDREF(source_hw);
+ ifnet_lock_done(cur_ifp);
- if (from_id == tmp->filter_id)
- match_found = 1;
- }
+ /* Send the ARP */
+ new_result = dlil_send_arp_internal(cur_ifp,
+ arpop,
+ (struct sockaddr_dl *)source_hw->ifa_addr,
+ (struct sockaddr *)&source_ip_copy, NULL,
+ target_proto);
+
+ IFA_REMREF(source_hw);
+ if (result == ENOTSUP) {
+ result = new_result;
+ }
+ }
+ ifnet_list_free(ifp_list);
+ }
+ } else {
+ result = dlil_send_arp_internal(ifp, arpop, sender_hw,
+ sender_proto, target_hw, target_proto);
}
- }
+ return (result);
+}
+
+/*
+ * Caller must hold ifnet head lock.
+ */
+static int
+ifnet_lookup(struct ifnet *ifp)
+{
+ struct ifnet *_ifp;
+
+ lck_rw_assert(&ifnet_head_lock, LCK_RW_ASSERT_HELD);
+ TAILQ_FOREACH(_ifp, &ifnet_head, if_link) {
+ if (_ifp == ifp)
+ break;
+ }
+ return (_ifp != NULL);
+}
/*
- * Finally, call the driver.
+ * Caller has to pass a non-zero refio argument to get a
+ * IO reference count. This will prevent ifnet_detach from
+ * being called when there are outstanding io reference counts.
*/
-
- retval = (*ifp->if_output)(ifp, m);
- dlil_stats.inject_if_out2++;
- if ((retval == 0) || (retval == EJUSTRETURN))
- return 0;
- else
- return retval;
+int
+ifnet_is_attached(struct ifnet *ifp, int refio)
+{
+ int ret;
+
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ if ((ret = ((ifp->if_refflags & (IFRF_ATTACHED | IFRF_DETACHING)) ==
+ IFRF_ATTACHED))) {
+ if (refio > 0)
+ ifp->if_refio++;
+ }
+ lck_mtx_unlock(&ifp->if_ref_lock);
+
+ return (ret);
}
-static
-int dlil_recycle_ioctl(struct ifnet *ifnet_ptr, u_long ioctl_code, void *ioctl_arg)
+void
+ifnet_decr_iorefcnt(struct ifnet *ifp)
{
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ VERIFY(ifp->if_refio > 0);
+ VERIFY((ifp->if_refflags & (IFRF_ATTACHED | IFRF_DETACHING)) != 0);
+ ifp->if_refio--;
- return EOPNOTSUPP;
+ /* if there are no more outstanding io references, wakeup the
+ * ifnet_detach thread if detaching flag is set.
+ */
+ if (ifp->if_refio == 0 &&
+ (ifp->if_refflags & IFRF_DETACHING) != 0) {
+ /* Convert the spinlock to a regular mutex if we have
+ * to wait for any reason while doing a wakeup.
+ */
+ lck_mtx_convert_spin(&ifp->if_ref_lock);
+ wakeup(&(ifp->if_refio));
+ }
+ lck_mtx_unlock(&ifp->if_ref_lock);
}
-static
-int dlil_recycle_output(struct ifnet *ifnet_ptr, struct mbuf *m)
+static void
+dlil_if_trace(struct dlil_ifnet *dl_if, int refhold)
{
+ struct dlil_ifnet_dbg *dl_if_dbg = (struct dlil_ifnet_dbg *)dl_if;
+ ctrace_t *tr;
+ u_int32_t idx;
+ u_int16_t *cnt;
+
+ if (!(dl_if->dl_if_flags & DLIF_DEBUG)) {
+ panic("%s: dl_if %p has no debug structure", __func__, dl_if);
+ /* NOTREACHED */
+ }
+
+ if (refhold) {
+ cnt = &dl_if_dbg->dldbg_if_refhold_cnt;
+ tr = dl_if_dbg->dldbg_if_refhold;
+ } else {
+ cnt = &dl_if_dbg->dldbg_if_refrele_cnt;
+ tr = dl_if_dbg->dldbg_if_refrele;
+ }
- m_freem(m);
- return 0;
+ idx = atomic_add_16_ov(cnt, 1) % IF_REF_TRACE_HIST_SIZE;
+ ctrace_record(&tr[idx]);
}
-static
-int dlil_recycle_free(struct ifnet *ifnet_ptr)
+errno_t
+dlil_if_ref(struct ifnet *ifp)
{
- return 0;
+ struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp;
+
+ if (dl_if == NULL)
+ return (EINVAL);
+
+ lck_mtx_lock_spin(&dl_if->dl_if_lock);
+ ++dl_if->dl_if_refcnt;
+ if (dl_if->dl_if_refcnt == 0) {
+ panic("%s: wraparound refcnt for ifp=%p", __func__, ifp);
+ /* NOTREACHED */
+ }
+ if (dl_if->dl_if_trace != NULL)
+ (*dl_if->dl_if_trace)(dl_if, TRUE);
+ lck_mtx_unlock(&dl_if->dl_if_lock);
+
+ return (0);
}
-static
-int dlil_recycle_set_bpf_tap(struct ifnet *ifp, int mode,
- int (*bpf_callback)(struct ifnet *, struct mbuf *))
+errno_t
+dlil_if_free(struct ifnet *ifp)
{
- /* XXX not sure what to do here */
- return 0;
+ struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp;
+
+ if (dl_if == NULL)
+ return (EINVAL);
+
+ lck_mtx_lock_spin(&dl_if->dl_if_lock);
+ if (dl_if->dl_if_refcnt == 0) {
+ panic("%s: negative refcnt for ifp=%p", __func__, ifp);
+ /* NOTREACHED */
+ }
+ --dl_if->dl_if_refcnt;
+ if (dl_if->dl_if_trace != NULL)
+ (*dl_if->dl_if_trace)(dl_if, FALSE);
+ lck_mtx_unlock(&dl_if->dl_if_lock);
+
+ return (0);
}
-int dlil_if_acquire(u_long family, void *uniqueid, size_t uniqueid_len,
- struct ifnet **ifp)
+static errno_t
+dlil_attach_protocol_internal(struct if_proto *proto,
+ const struct ifnet_demux_desc *demux_list, u_int32_t demux_count)
{
- struct ifnet *ifp1 = NULL;
- struct dlil_ifnet *dlifp1 = NULL;
- int s, ret = 0;
- boolean_t funnel_state;
+ struct kev_dl_proto_data ev_pr_data;
+ struct ifnet *ifp = proto->ifp;
+ int retval = 0;
+ u_int32_t hash_value = proto_hash_value(proto->protocol_family);
+ struct if_proto *prev_proto;
+ struct if_proto *_proto;
+
+ /* callee holds a proto refcnt upon success */
+ ifnet_lock_exclusive(ifp);
+ _proto = find_attached_proto(ifp, proto->protocol_family);
+ if (_proto != NULL) {
+ ifnet_lock_done(ifp);
+ if_proto_free(_proto);
+ return (EEXIST);
+ }
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
+ /*
+ * Call family module add_proto routine so it can refine the
+ * demux descriptors as it wishes.
+ */
+ retval = ifp->if_add_proto(ifp, proto->protocol_family, demux_list,
+ demux_count);
+ if (retval) {
+ ifnet_lock_done(ifp);
+ return (retval);
+ }
- TAILQ_FOREACH(dlifp1, &dlil_ifnet_head, dl_if_link) {
-
- ifp1 = (struct ifnet *)dlifp1;
-
- if (ifp1->if_family == family) {
-
- /* same uniqueid and same len or no unique id specified */
- if ((uniqueid_len == dlifp1->if_uniqueid_len)
- && !bcmp(uniqueid, dlifp1->if_uniqueid, uniqueid_len)) {
-
- /* check for matching interface in use */
- if (ifp1->if_eflags & IFEF_INUSE) {
- if (uniqueid_len) {
- ret = EBUSY;
- goto end;
- }
- }
- else {
-
- ifp1->if_eflags |= (IFEF_INUSE + IFEF_REUSE);
- *ifp = ifp1;
- goto end;
- }
- }
- }
- }
-
- /* no interface found, allocate a new one */
- MALLOC(dlifp1, struct dlil_ifnet *, sizeof(*dlifp1), M_NKE, M_WAITOK);
- if (dlifp1 == 0) {
- ret = ENOMEM;
- goto end;
- }
-
- bzero(dlifp1, sizeof(*dlifp1));
-
- if (uniqueid_len) {
- MALLOC(dlifp1->if_uniqueid, void *, uniqueid_len, M_NKE, M_WAITOK);
- if (dlifp1->if_uniqueid == 0) {
- FREE(dlifp1, M_NKE);
- ret = ENOMEM;
- goto end;
- }
- bcopy(uniqueid, dlifp1->if_uniqueid, uniqueid_len);
- dlifp1->if_uniqueid_len = uniqueid_len;
- }
-
- ifp1 = (struct ifnet *)dlifp1;
- ifp1->if_eflags |= IFEF_INUSE;
-
- TAILQ_INSERT_TAIL(&dlil_ifnet_head, dlifp1, dl_if_link);
-
- *ifp = ifp1;
+ /*
+ * Insert the protocol in the hash
+ */
+ prev_proto = SLIST_FIRST(&ifp->if_proto_hash[hash_value]);
+ while (prev_proto != NULL && SLIST_NEXT(prev_proto, next_hash) != NULL)
+ prev_proto = SLIST_NEXT(prev_proto, next_hash);
+ if (prev_proto)
+ SLIST_INSERT_AFTER(prev_proto, proto, next_hash);
+ else
+ SLIST_INSERT_HEAD(&ifp->if_proto_hash[hash_value],
+ proto, next_hash);
+
+ /* hold a proto refcnt for attach */
+ if_proto_ref(proto);
+
+ /*
+ * The reserved field carries the number of protocol still attached
+ * (subject to change)
+ */
+ ev_pr_data.proto_family = proto->protocol_family;
+ ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
+ ifnet_lock_done(ifp);
+
+ dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_ATTACHED,
+ (struct net_event_data *)&ev_pr_data,
+ sizeof (struct kev_dl_proto_data));
+ return (retval);
+}
+
+errno_t
+ifnet_attach_protocol(ifnet_t ifp, protocol_family_t protocol,
+ const struct ifnet_attach_proto_param *proto_details)
+{
+ int retval = 0;
+ struct if_proto *ifproto = NULL;
+
+ ifnet_head_lock_shared();
+ if (ifp == NULL || protocol == 0 || proto_details == NULL) {
+ retval = EINVAL;
+ goto end;
+ }
+ /* Check that the interface is in the global list */
+ if (!ifnet_lookup(ifp)) {
+ retval = ENXIO;
+ goto end;
+ }
+
+ ifproto = zalloc(dlif_proto_zone);
+ if (ifproto == NULL) {
+ retval = ENOMEM;
+ goto end;
+ }
+ bzero(ifproto, dlif_proto_size);
+
+ /* refcnt held above during lookup */
+ ifproto->ifp = ifp;
+ ifproto->protocol_family = protocol;
+ ifproto->proto_kpi = kProtoKPI_v1;
+ ifproto->kpi.v1.input = proto_details->input;
+ ifproto->kpi.v1.pre_output = proto_details->pre_output;
+ ifproto->kpi.v1.event = proto_details->event;
+ ifproto->kpi.v1.ioctl = proto_details->ioctl;
+ ifproto->kpi.v1.detached = proto_details->detached;
+ ifproto->kpi.v1.resolve_multi = proto_details->resolve;
+ ifproto->kpi.v1.send_arp = proto_details->send_arp;
+
+ retval = dlil_attach_protocol_internal(ifproto,
+ proto_details->demux_list, proto_details->demux_count);
+
+ if (dlil_verbose) {
+ printf("%s%d: attached v1 protocol %d\n", ifp->if_name,
+ ifp->if_unit, protocol);
+ }
end:
+ if (retval != 0 && retval != EEXIST && ifp != NULL) {
+ DLIL_PRINTF("%s%d: failed to attach v1 protocol %d (err=%d)\n",
+ ifp->if_name, ifp->if_unit, protocol, retval);
+ }
+ ifnet_head_done();
+ if (retval != 0 && ifproto != NULL)
+ zfree(dlif_proto_zone, ifproto);
+ return (retval);
+}
+
+errno_t
+ifnet_attach_protocol_v2(ifnet_t ifp, protocol_family_t protocol,
+ const struct ifnet_attach_proto_param_v2 *proto_details)
+{
+ int retval = 0;
+ struct if_proto *ifproto = NULL;
+
+ ifnet_head_lock_shared();
+ if (ifp == NULL || protocol == 0 || proto_details == NULL) {
+ retval = EINVAL;
+ goto end;
+ }
+ /* Check that the interface is in the global list */
+ if (!ifnet_lookup(ifp)) {
+ retval = ENXIO;
+ goto end;
+ }
+
+ ifproto = zalloc(dlif_proto_zone);
+ if (ifproto == NULL) {
+ retval = ENOMEM;
+ goto end;
+ }
+ bzero(ifproto, sizeof(*ifproto));
+
+ /* refcnt held above during lookup */
+ ifproto->ifp = ifp;
+ ifproto->protocol_family = protocol;
+ ifproto->proto_kpi = kProtoKPI_v2;
+ ifproto->kpi.v2.input = proto_details->input;
+ ifproto->kpi.v2.pre_output = proto_details->pre_output;
+ ifproto->kpi.v2.event = proto_details->event;
+ ifproto->kpi.v2.ioctl = proto_details->ioctl;
+ ifproto->kpi.v2.detached = proto_details->detached;
+ ifproto->kpi.v2.resolve_multi = proto_details->resolve;
+ ifproto->kpi.v2.send_arp = proto_details->send_arp;
+
+ retval = dlil_attach_protocol_internal(ifproto,
+ proto_details->demux_list, proto_details->demux_count);
+
+ if (dlil_verbose) {
+ printf("%s%d: attached v2 protocol %d\n", ifp->if_name,
+ ifp->if_unit, protocol);
+ }
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
- return ret;
+end:
+ if (retval != 0 && retval != EEXIST && ifp != NULL) {
+ DLIL_PRINTF("%s%d: failed to attach v2 protocol %d (err=%d)\n",
+ ifp->if_name, ifp->if_unit, protocol, retval);
+ }
+ ifnet_head_done();
+ if (retval != 0 && ifproto != NULL)
+ zfree(dlif_proto_zone, ifproto);
+ return (retval);
}
-void dlil_if_release(struct ifnet *ifp)
+errno_t
+ifnet_detach_protocol(ifnet_t ifp, protocol_family_t proto_family)
{
- struct dlil_ifnet *dlifp = (struct dlil_ifnet *)ifp;
- int s;
- boolean_t funnel_state;
+ struct if_proto *proto = NULL;
+ int retval = 0;
+
+ if (ifp == NULL || proto_family == 0) {
+ retval = EINVAL;
+ goto end;
+ }
+
+ ifnet_lock_exclusive(ifp);
+ /* callee holds a proto refcnt upon success */
+ proto = find_attached_proto(ifp, proto_family);
+ if (proto == NULL) {
+ retval = ENXIO;
+ ifnet_lock_done(ifp);
+ goto end;
+ }
+
+ /* call family module del_proto */
+ if (ifp->if_del_proto)
+ ifp->if_del_proto(ifp, proto->protocol_family);
+
+ SLIST_REMOVE(&ifp->if_proto_hash[proto_hash_value(proto_family)],
+ proto, if_proto, next_hash);
+
+ if (proto->proto_kpi == kProtoKPI_v1) {
+ proto->kpi.v1.input = ifproto_media_input_v1;
+ proto->kpi.v1.pre_output= ifproto_media_preout;
+ proto->kpi.v1.event = ifproto_media_event;
+ proto->kpi.v1.ioctl = ifproto_media_ioctl;
+ proto->kpi.v1.resolve_multi = ifproto_media_resolve_multi;
+ proto->kpi.v1.send_arp = ifproto_media_send_arp;
+ } else {
+ proto->kpi.v2.input = ifproto_media_input_v2;
+ proto->kpi.v2.pre_output = ifproto_media_preout;
+ proto->kpi.v2.event = ifproto_media_event;
+ proto->kpi.v2.ioctl = ifproto_media_ioctl;
+ proto->kpi.v2.resolve_multi = ifproto_media_resolve_multi;
+ proto->kpi.v2.send_arp = ifproto_media_send_arp;
+ }
+ proto->detached = 1;
+ ifnet_lock_done(ifp);
+
+ if (dlil_verbose) {
+ printf("%s%d: detached %s protocol %d\n", ifp->if_name,
+ ifp->if_unit, (proto->proto_kpi == kProtoKPI_v1) ?
+ "v1" : "v2", proto_family);
+ }
+
+ /* release proto refcnt held during protocol attach */
+ if_proto_free(proto);
- funnel_state = thread_funnel_set(network_flock, TRUE);
- s = splnet();
-
- ifp->if_eflags &= ~IFEF_INUSE;
- ifp->if_ioctl = dlil_recycle_ioctl;
- ifp->if_output = dlil_recycle_output;
- ifp->if_free = dlil_recycle_free;
- ifp->if_set_bpf_tap = dlil_recycle_set_bpf_tap;
+ /*
+ * Release proto refcnt held during lookup; the rest of
+ * protocol detach steps will happen when the last proto
+ * reference is released.
+ */
+ if_proto_free(proto);
- strncpy(dlifp->if_namestorage, ifp->if_name, IFNAMSIZ);
- ifp->if_name = dlifp->if_namestorage;
-
- splx(s);
- thread_funnel_set(network_flock, funnel_state);
+end:
+ return (retval);
}
+
+static errno_t
+ifproto_media_input_v1(struct ifnet *ifp, protocol_family_t protocol,
+ struct mbuf *packet, char *header)
+{
+#pragma unused(ifp, protocol, packet, header)
+ return (ENXIO);
+}
+
+static errno_t
+ifproto_media_input_v2(struct ifnet *ifp, protocol_family_t protocol,
+ struct mbuf *packet)
+{
+#pragma unused(ifp, protocol, packet)
+ return (ENXIO);
+
+}
+
+static errno_t
+ifproto_media_preout(struct ifnet *ifp, protocol_family_t protocol,
+ mbuf_t *packet, const struct sockaddr *dest, void *route, char *frame_type,
+ char *link_layer_dest)
+{
+#pragma unused(ifp, protocol, packet, dest, route, frame_type, link_layer_dest)
+ return (ENXIO);
+
+}
+
+static void
+ifproto_media_event(struct ifnet *ifp, protocol_family_t protocol,
+ const struct kev_msg *event)
+{
+#pragma unused(ifp, protocol, event)
+}
+
+static errno_t
+ifproto_media_ioctl(struct ifnet *ifp, protocol_family_t protocol,
+ unsigned long command, void *argument)
+{
+#pragma unused(ifp, protocol, command, argument)
+ return (ENXIO);
+}
+
+static errno_t
+ifproto_media_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
+ struct sockaddr_dl *out_ll, size_t ll_len)
+{
+#pragma unused(ifp, proto_addr, out_ll, ll_len)
+ return (ENXIO);
+}
+
+static errno_t
+ifproto_media_send_arp(struct ifnet *ifp, u_short arpop,
+ const struct sockaddr_dl *sender_hw, const struct sockaddr *sender_proto,
+ const struct sockaddr_dl *target_hw, const struct sockaddr *target_proto)
+{
+#pragma unused(ifp, arpop, sender_hw, sender_proto, target_hw, target_proto)
+ return (ENXIO);
+}
+
+extern int if_next_index(void);
+
+errno_t
+ifnet_attach(ifnet_t ifp, const struct sockaddr_dl *ll_addr)
+{
+ struct ifnet *tmp_if;
+ struct ifaddr *ifa;
+ struct if_data_internal if_data_saved;
+ struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp;
+
+ if (ifp == NULL)
+ return (EINVAL);
+
+ /*
+ * Serialize ifnet attach using dlil_ifnet_lock, in order to
+ * prevent the interface from being configured while it is
+ * embryonic, as ifnet_head_lock is dropped and reacquired
+ * below prior to marking the ifnet with IFRF_ATTACHED.
+ */
+ dlil_if_lock();
+ ifnet_head_lock_exclusive();
+ /* Verify we aren't already on the list */
+ TAILQ_FOREACH(tmp_if, &ifnet_head, if_link) {
+ if (tmp_if == ifp) {
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (EEXIST);
+ }
+ }
+
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ if (ifp->if_refflags & IFRF_ATTACHED) {
+ panic("%s: flags mismatch (attached set) ifp=%p",
+ __func__, ifp);
+ /* NOTREACHED */
+ }
+ lck_mtx_unlock(&ifp->if_ref_lock);
+
+ ifnet_lock_exclusive(ifp);
+
+ /* Sanity check */
+ VERIFY(ifp->if_detaching_link.tqe_next == NULL);
+ VERIFY(ifp->if_detaching_link.tqe_prev == NULL);
+
+ if (ll_addr != NULL) {
+ if (ifp->if_addrlen == 0) {
+ ifp->if_addrlen = ll_addr->sdl_alen;
+ } else if (ll_addr->sdl_alen != ifp->if_addrlen) {
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (EINVAL);
+ }
+ }
+
+ /*
+ * Allow interfaces without protocol families to attach
+ * only if they have the necessary fields filled out.
+ */
+ if (ifp->if_add_proto == NULL || ifp->if_del_proto == NULL) {
+ DLIL_PRINTF("%s: Attempt to attach interface without "
+ "family module - %d\n", __func__, ifp->if_family);
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (ENODEV);
+ }
+
+ /* Allocate protocol hash table */
+ VERIFY(ifp->if_proto_hash == NULL);
+ ifp->if_proto_hash = zalloc(dlif_phash_zone);
+ if (ifp->if_proto_hash == NULL) {
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (ENOBUFS);
+ }
+ bzero(ifp->if_proto_hash, dlif_phash_size);
+
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ VERIFY(TAILQ_EMPTY(&ifp->if_flt_head));
+ TAILQ_INIT(&ifp->if_flt_head);
+ VERIFY(ifp->if_flt_busy == 0);
+ VERIFY(ifp->if_flt_waiters == 0);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ VERIFY(TAILQ_EMPTY(&ifp->if_prefixhead));
+ TAILQ_INIT(&ifp->if_prefixhead);
+
+ if (!(dl_if->dl_if_flags & DLIF_REUSE)) {
+ VERIFY(LIST_EMPTY(&ifp->if_multiaddrs));
+ LIST_INIT(&ifp->if_multiaddrs);
+ }
+
+ VERIFY(ifp->if_allhostsinm == NULL);
+ VERIFY(TAILQ_EMPTY(&ifp->if_addrhead));
+ TAILQ_INIT(&ifp->if_addrhead);
+
+ if (ifp->if_snd.ifq_maxlen == 0)
+ ifp->if_snd.ifq_maxlen = ifqmaxlen;
+
+ if (ifp->if_index == 0) {
+ int idx = if_next_index();
+
+ if (idx == -1) {
+ ifp->if_index = 0;
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (ENOBUFS);
+ }
+ ifp->if_index = idx;
+ }
+ /* There should not be anything occupying this slot */
+ VERIFY(ifindex2ifnet[ifp->if_index] == NULL);
+
+ /* allocate (if needed) and initialize a link address */
+ VERIFY(!(dl_if->dl_if_flags & DLIF_REUSE) || ifp->if_lladdr != NULL);
+ ifa = dlil_alloc_lladdr(ifp, ll_addr);
+ if (ifa == NULL) {
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ dlil_if_unlock();
+ return (ENOBUFS);
+ }
+
+ VERIFY(ifnet_addrs[ifp->if_index - 1] == NULL);
+ ifnet_addrs[ifp->if_index - 1] = ifa;
+
+ /* make this address the first on the list */
+ IFA_LOCK(ifa);
+ /* hold a reference for ifnet_addrs[] */
+ IFA_ADDREF_LOCKED(ifa);
+ /* if_attach_link_ifa() holds a reference for ifa_link */
+ if_attach_link_ifa(ifp, ifa);
+ IFA_UNLOCK(ifa);
+
+#if CONFIG_MACF_NET
+ mac_ifnet_label_associate(ifp);
+#endif
+
+ TAILQ_INSERT_TAIL(&ifnet_head, ifp, if_link);
+ ifindex2ifnet[ifp->if_index] = ifp;
+
+ /* Hold a reference to the underlying dlil_ifnet */
+ ifnet_reference(ifp);
+
+ /*
+ * A specific dlil input thread is created per Ethernet/cellular
+ * interface. pseudo interfaces or other types of interfaces use
+ * the main ("loopback") thread.
+ *
+ * If the sysctl "net.link.generic.system.multi_threaded_input" is set
+ * to zero, all packets will be handled by the main loopback thread,
+ * reverting to 10.4.x behaviour.
+ */
+ if (dlil_multithreaded_input &&
+ (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_CELLULAR)) {
+ int err;
+
+ ifp->if_input_thread = zalloc(dlif_inp_zone);
+ if (ifp->if_input_thread == NULL) {
+ panic("%s: ifp=%p couldn't alloc threading",
+ __func__, ifp);
+ /* NOTREACHED */
+ }
+ bzero(ifp->if_input_thread, dlif_inp_size);
+ err = dlil_create_input_thread(ifp, ifp->if_input_thread);
+ if (err != 0) {
+ panic("%s: ifp=%p couldn't get a thread. "
+ "err=%d", __func__, ifp, err);
+ /* NOTREACHED */
+ }
+#ifdef DLIL_DEBUG
+ printf("%s: dlil thread for ifp=%p if_index=%d\n",
+ __func__, ifp, ifp->if_index);
+#endif
+ }
+
+ /* Clear stats (save and restore other fields that we care) */
+ if_data_saved = ifp->if_data;
+ bzero(&ifp->if_data, sizeof (ifp->if_data));
+ ifp->if_data.ifi_type = if_data_saved.ifi_type;
+ ifp->if_data.ifi_typelen = if_data_saved.ifi_typelen;
+ ifp->if_data.ifi_physical = if_data_saved.ifi_physical;
+ ifp->if_data.ifi_addrlen = if_data_saved.ifi_addrlen;
+ ifp->if_data.ifi_hdrlen = if_data_saved.ifi_hdrlen;
+ ifp->if_data.ifi_mtu = if_data_saved.ifi_mtu;
+ ifp->if_data.ifi_baudrate = if_data_saved.ifi_baudrate;
+ ifp->if_data.ifi_hwassist = if_data_saved.ifi_hwassist;
+ ifp->if_data.ifi_tso_v4_mtu = if_data_saved.ifi_tso_v4_mtu;
+ ifp->if_data.ifi_tso_v6_mtu = if_data_saved.ifi_tso_v6_mtu;
+ ifnet_touch_lastchange(ifp);
+
+ /* Record attach PC stacktrace */
+ ctrace_record(&((struct dlil_ifnet *)ifp)->dl_if_attach);
+
+ ifp->if_updatemcasts = 0;
+ if (!LIST_EMPTY(&ifp->if_multiaddrs)) {
+ struct ifmultiaddr *ifma;
+ LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
+ IFMA_LOCK(ifma);
+ if (ifma->ifma_addr->sa_family == AF_LINK ||
+ ifma->ifma_addr->sa_family == AF_UNSPEC)
+ ifp->if_updatemcasts++;
+ IFMA_UNLOCK(ifma);
+ }
+
+ printf("%s%d: attached with %d suspended link-layer multicast "
+ "membership(s)\n", ifp->if_name, ifp->if_unit,
+ ifp->if_updatemcasts);
+ }
+
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+
+ lck_mtx_lock(&ifp->if_cached_route_lock);
+ /* Enable forwarding cached route */
+ ifp->if_fwd_cacheok = 1;
+ /* Clean up any existing cached routes */
+ if (ifp->if_fwd_route.ro_rt != NULL)
+ rtfree(ifp->if_fwd_route.ro_rt);
+ bzero(&ifp->if_fwd_route, sizeof (ifp->if_fwd_route));
+ if (ifp->if_src_route.ro_rt != NULL)
+ rtfree(ifp->if_src_route.ro_rt);
+ bzero(&ifp->if_src_route, sizeof (ifp->if_src_route));
+ if (ifp->if_src_route6.ro_rt != NULL)
+ rtfree(ifp->if_src_route6.ro_rt);
+ bzero(&ifp->if_src_route6, sizeof (ifp->if_src_route6));
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+
+ ifnet_llreach_ifattach(ifp, (dl_if->dl_if_flags & DLIF_REUSE));
+
+ /*
+ * Allocate and attach IGMPv3/MLDv2 interface specific variables
+ * and trees; do this before the ifnet is marked as attached.
+ * The ifnet keeps the reference to the info structures even after
+ * the ifnet is detached, since the network-layer records still
+ * refer to the info structures even after that. This also
+ * makes it possible for them to still function after the ifnet
+ * is recycled or reattached.
+ */
+#if INET
+ if (IGMP_IFINFO(ifp) == NULL) {
+ IGMP_IFINFO(ifp) = igmp_domifattach(ifp, M_WAITOK);
+ VERIFY(IGMP_IFINFO(ifp) != NULL);
+ } else {
+ VERIFY(IGMP_IFINFO(ifp)->igi_ifp == ifp);
+ igmp_domifreattach(IGMP_IFINFO(ifp));
+ }
+#endif /* INET */
+#if INET6
+ if (MLD_IFINFO(ifp) == NULL) {
+ MLD_IFINFO(ifp) = mld_domifattach(ifp, M_WAITOK);
+ VERIFY(MLD_IFINFO(ifp) != NULL);
+ } else {
+ VERIFY(MLD_IFINFO(ifp)->mli_ifp == ifp);
+ mld_domifreattach(MLD_IFINFO(ifp));
+ }
+#endif /* INET6 */
+
+ /*
+ * Finally, mark this ifnet as attached.
+ */
+ lck_mtx_lock(rnh_lock);
+ ifnet_lock_exclusive(ifp);
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ ifp->if_refflags = IFRF_ATTACHED;
+ lck_mtx_unlock(&ifp->if_ref_lock);
+ if (net_rtref) {
+ /* boot-args override; enable idle notification */
+ (void) ifnet_set_idle_flags_locked(ifp, IFRF_IDLE_NOTIFY,
+ IFRF_IDLE_NOTIFY);
+ } else {
+ /* apply previous request(s) to set the idle flags, if any */
+ (void) ifnet_set_idle_flags_locked(ifp, ifp->if_idle_new_flags,
+ ifp->if_idle_new_flags_mask);
+
+ }
+ ifnet_lock_done(ifp);
+ lck_mtx_unlock(rnh_lock);
+ dlil_if_unlock();
+
+#if PF
+ /*
+ * Attach packet filter to this interface, if enabled.
+ */
+ pf_ifnet_hook(ifp, 1);
+#endif /* PF */
+
+ dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_ATTACHED, NULL, 0);
+
+ if (dlil_verbose) {
+ printf("%s%d: attached%s\n", ifp->if_name, ifp->if_unit,
+ (dl_if->dl_if_flags & DLIF_REUSE) ? " (recycled)" : "");
+ }
+
+ return (0);
+}
+
+/*
+ * Prepare the storage for the first/permanent link address, which must
+ * must have the same lifetime as the ifnet itself. Although the link
+ * address gets removed from if_addrhead and ifnet_addrs[] at detach time,
+ * its location in memory must never change as it may still be referred
+ * to by some parts of the system afterwards (unfortunate implementation
+ * artifacts inherited from BSD.)
+ *
+ * Caller must hold ifnet lock as writer.
+ */
+static struct ifaddr *
+dlil_alloc_lladdr(struct ifnet *ifp, const struct sockaddr_dl *ll_addr)
+{
+ struct ifaddr *ifa, *oifa;
+ struct sockaddr_dl *asdl, *msdl;
+ char workbuf[IFNAMSIZ*2];
+ int namelen, masklen, socksize;
+ struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp;
+
+ ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
+ VERIFY(ll_addr == NULL || ll_addr->sdl_alen == ifp->if_addrlen);
+
+ namelen = snprintf(workbuf, sizeof (workbuf), "%s%d",
+ ifp->if_name, ifp->if_unit);
+ masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
+ socksize = masklen + ifp->if_addrlen;
+#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof (u_int32_t) - 1)))
+ if ((u_int32_t)socksize < sizeof (struct sockaddr_dl))
+ socksize = sizeof(struct sockaddr_dl);
+ socksize = ROUNDUP(socksize);
+#undef ROUNDUP
+
+ ifa = ifp->if_lladdr;
+ if (socksize > DLIL_SDLMAXLEN ||
+ (ifa != NULL && ifa != &dl_if->dl_if_lladdr.ifa)) {
+ /*
+ * Rare, but in the event that the link address requires
+ * more storage space than DLIL_SDLMAXLEN, allocate the
+ * largest possible storages for address and mask, such
+ * that we can reuse the same space when if_addrlen grows.
+ * This same space will be used when if_addrlen shrinks.
+ */
+ if (ifa == NULL || ifa == &dl_if->dl_if_lladdr.ifa) {
+ int ifasize = sizeof (*ifa) + 2 * SOCK_MAXADDRLEN;
+ ifa = _MALLOC(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
+ if (ifa == NULL)
+ return (NULL);
+ ifa_lock_init(ifa);
+ /* Don't set IFD_ALLOC, as this is permanent */
+ ifa->ifa_debug = IFD_LINK;
+ }
+ IFA_LOCK(ifa);
+ /* address and mask sockaddr_dl locations */
+ asdl = (struct sockaddr_dl *)(ifa + 1);
+ bzero(asdl, SOCK_MAXADDRLEN);
+ msdl = (struct sockaddr_dl *)((char *)asdl + SOCK_MAXADDRLEN);
+ bzero(msdl, SOCK_MAXADDRLEN);
+ } else {
+ VERIFY(ifa == NULL || ifa == &dl_if->dl_if_lladdr.ifa);
+ /*
+ * Use the storage areas for address and mask within the
+ * dlil_ifnet structure. This is the most common case.
+ */
+ if (ifa == NULL) {
+ ifa = &dl_if->dl_if_lladdr.ifa;
+ ifa_lock_init(ifa);
+ /* Don't set IFD_ALLOC, as this is permanent */
+ ifa->ifa_debug = IFD_LINK;
+ }
+ IFA_LOCK(ifa);
+ /* address and mask sockaddr_dl locations */
+ asdl = (struct sockaddr_dl *)&dl_if->dl_if_lladdr.asdl;
+ bzero(asdl, sizeof (dl_if->dl_if_lladdr.asdl));
+ msdl = (struct sockaddr_dl *)&dl_if->dl_if_lladdr.msdl;
+ bzero(msdl, sizeof (dl_if->dl_if_lladdr.msdl));
+ }
+
+ /* hold a permanent reference for the ifnet itself */
+ IFA_ADDREF_LOCKED(ifa);
+ oifa = ifp->if_lladdr;
+ ifp->if_lladdr = ifa;
+
+ VERIFY(ifa->ifa_debug == IFD_LINK);
+ ifa->ifa_ifp = ifp;
+ ifa->ifa_rtrequest = link_rtrequest;
+ ifa->ifa_addr = (struct sockaddr *)asdl;
+ asdl->sdl_len = socksize;
+ asdl->sdl_family = AF_LINK;
+ bcopy(workbuf, asdl->sdl_data, namelen);
+ asdl->sdl_nlen = namelen;
+ asdl->sdl_index = ifp->if_index;
+ asdl->sdl_type = ifp->if_type;
+ if (ll_addr != NULL) {
+ asdl->sdl_alen = ll_addr->sdl_alen;
+ bcopy(CONST_LLADDR(ll_addr), LLADDR(asdl), asdl->sdl_alen);
+ } else {
+ asdl->sdl_alen = 0;
+ }
+ ifa->ifa_netmask = (struct sockaddr*)msdl;
+ msdl->sdl_len = masklen;
+ while (namelen != 0)
+ msdl->sdl_data[--namelen] = 0xff;
+ IFA_UNLOCK(ifa);
+
+ if (oifa != NULL)
+ IFA_REMREF(oifa);
+
+ return (ifa);
+}
+
+static void
+if_purgeaddrs(struct ifnet *ifp)
+{
+#if INET
+ in_purgeaddrs(ifp);
+#endif /* INET */
+#if INET6
+ in6_purgeaddrs(ifp);
+#endif /* INET6 */
+#if NETAT
+ at_purgeaddrs(ifp);
+#endif
+}
+
+errno_t
+ifnet_detach(ifnet_t ifp)
+{
+ if (ifp == NULL)
+ return (EINVAL);
+
+ ifnet_head_lock_exclusive();
+ lck_mtx_lock(rnh_lock);
+ ifnet_lock_exclusive(ifp);
+
+ /*
+ * Check to see if this interface has previously triggered
+ * aggressive protocol draining; if so, decrement the global
+ * refcnt and clear PR_AGGDRAIN on the route domain if
+ * there are no more of such an interface around.
+ */
+ (void) ifnet_set_idle_flags_locked(ifp, 0, ~0);
+
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ if (!(ifp->if_refflags & IFRF_ATTACHED)) {
+ lck_mtx_unlock(&ifp->if_ref_lock);
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ lck_mtx_unlock(rnh_lock);
+ return (EINVAL);
+ } else if (ifp->if_refflags & IFRF_DETACHING) {
+ /* Interface has already been detached */
+ lck_mtx_unlock(&ifp->if_ref_lock);
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ lck_mtx_unlock(rnh_lock);
+ return (ENXIO);
+ }
+ /* Indicate this interface is being detached */
+ ifp->if_refflags &= ~IFRF_ATTACHED;
+ ifp->if_refflags |= IFRF_DETACHING;
+ lck_mtx_unlock(&ifp->if_ref_lock);
+
+ if (dlil_verbose)
+ printf("%s%d: detaching\n", ifp->if_name, ifp->if_unit);
+
+ /*
+ * Remove ifnet from the ifnet_head, ifindex2ifnet[]; it will
+ * no longer be visible during lookups from this point.
+ */
+ VERIFY(ifindex2ifnet[ifp->if_index] == ifp);
+ TAILQ_REMOVE(&ifnet_head, ifp, if_link);
+ ifp->if_link.tqe_next = NULL;
+ ifp->if_link.tqe_prev = NULL;
+ ifindex2ifnet[ifp->if_index] = NULL;
+
+ /* Record detach PC stacktrace */
+ ctrace_record(&((struct dlil_ifnet *)ifp)->dl_if_detach);
+
+ ifnet_lock_done(ifp);
+ ifnet_head_done();
+ lck_mtx_unlock(rnh_lock);
+
+ /* Let BPF know we're detaching */
+ bpfdetach(ifp);
+
+ /* Mark the interface as DOWN */
+ if_down(ifp);
+
+ /* Disable forwarding cached route */
+ lck_mtx_lock(&ifp->if_cached_route_lock);
+ ifp->if_fwd_cacheok = 0;
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+
+ /*
+ * Drain any deferred IGMPv3/MLDv2 query responses, but keep the
+ * references to the info structures and leave them attached to
+ * this ifnet.
+ */
+#if INET
+ igmp_domifdetach(ifp);
+#endif /* INET */
+#if INET6
+ mld_domifdetach(ifp);
+#endif /* INET6 */
+
+ dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHING, NULL, 0);
+
+ /* Let worker thread take care of the rest, to avoid reentrancy */
+ dlil_if_lock();
+ ifnet_detaching_enqueue(ifp);
+ dlil_if_unlock();
+
+ return (0);
+}
+
+static void
+ifnet_detaching_enqueue(struct ifnet *ifp)
+{
+ dlil_if_lock_assert();
+
+ ++ifnet_detaching_cnt;
+ VERIFY(ifnet_detaching_cnt != 0);
+ TAILQ_INSERT_TAIL(&ifnet_detaching_head, ifp, if_detaching_link);
+ wakeup((caddr_t)&ifnet_delayed_run);
+}
+
+static struct ifnet *
+ifnet_detaching_dequeue(void)
+{
+ struct ifnet *ifp;
+
+ dlil_if_lock_assert();
+
+ ifp = TAILQ_FIRST(&ifnet_detaching_head);
+ VERIFY(ifnet_detaching_cnt != 0 || ifp == NULL);
+ if (ifp != NULL) {
+ VERIFY(ifnet_detaching_cnt != 0);
+ --ifnet_detaching_cnt;
+ TAILQ_REMOVE(&ifnet_detaching_head, ifp, if_detaching_link);
+ ifp->if_detaching_link.tqe_next = NULL;
+ ifp->if_detaching_link.tqe_prev = NULL;
+ }
+ return (ifp);
+}
+
+static void
+ifnet_delayed_thread_func(void)
+{
+ struct ifnet *ifp;
+
+ for (;;) {
+ dlil_if_lock();
+ while (ifnet_detaching_cnt == 0) {
+ (void) msleep(&ifnet_delayed_run, &dlil_ifnet_lock,
+ (PZERO - 1), "ifnet_delayed_thread", NULL);
+ }
+
+ VERIFY(TAILQ_FIRST(&ifnet_detaching_head) != NULL);
+
+ /* Take care of detaching ifnet */
+ ifp = ifnet_detaching_dequeue();
+ dlil_if_unlock();
+ if (ifp != NULL)
+ ifnet_detach_final(ifp);
+ }
+}
+
+static void
+ifnet_detach_final(struct ifnet *ifp)
+{
+ struct ifnet_filter *filter, *filter_next;
+ struct ifnet_filter_head fhead;
+ struct dlil_threading_info *inputthread;
+ struct ifaddr *ifa;
+ ifnet_detached_func if_free;
+ int i;
+
+ lck_mtx_lock(&ifp->if_ref_lock);
+ if (!(ifp->if_refflags & IFRF_DETACHING)) {
+ panic("%s: flags mismatch (detaching not set) ifp=%p",
+ __func__, ifp);
+ /* NOTREACHED */
+ }
+
+ /* Wait until the existing IO references get released
+ * before we proceed with ifnet_detach
+ */
+ while (ifp->if_refio > 0) {
+ printf("%s: Waiting for IO references on %s%d interface "
+ "to be released\n", __func__, ifp->if_name, ifp->if_unit);
+ (void) msleep(&(ifp->if_refio), &ifp->if_ref_lock,
+ (PZERO - 1), "ifnet_ioref_wait", NULL);
+ }
+ lck_mtx_unlock(&ifp->if_ref_lock);
+
+ /* Detach interface filters */
+ lck_mtx_lock(&ifp->if_flt_lock);
+ if_flt_monitor_enter(ifp);
+
+ lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED);
+ fhead = ifp->if_flt_head;
+ TAILQ_INIT(&ifp->if_flt_head);
+
+ for (filter = TAILQ_FIRST(&fhead); filter; filter = filter_next) {
+ filter_next = TAILQ_NEXT(filter, filt_next);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ dlil_detach_filter_internal(filter, 1);
+ lck_mtx_lock(&ifp->if_flt_lock);
+ }
+ if_flt_monitor_leave(ifp);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ /* Tell upper layers to drop their network addresses */
+ if_purgeaddrs(ifp);
+
+ ifnet_lock_exclusive(ifp);
+
+ /* Uplumb all protocols */
+ for (i = 0; i < PROTO_HASH_SLOTS; i++) {
+ struct if_proto *proto;
+
+ proto = SLIST_FIRST(&ifp->if_proto_hash[i]);
+ while (proto != NULL) {
+ protocol_family_t family = proto->protocol_family;
+ ifnet_lock_done(ifp);
+ proto_unplumb(family, ifp);
+ ifnet_lock_exclusive(ifp);
+ proto = SLIST_FIRST(&ifp->if_proto_hash[i]);
+ }
+ /* There should not be any protocols left */
+ VERIFY(SLIST_EMPTY(&ifp->if_proto_hash[i]));
+ }
+ zfree(dlif_phash_zone, ifp->if_proto_hash);
+ ifp->if_proto_hash = NULL;
+
+ /* Detach (permanent) link address from if_addrhead */
+ ifa = TAILQ_FIRST(&ifp->if_addrhead);
+ VERIFY(ifnet_addrs[ifp->if_index - 1] == ifa);
+ IFA_LOCK(ifa);
+ if_detach_link_ifa(ifp, ifa);
+ IFA_UNLOCK(ifa);
+
+ /* Remove (permanent) link address from ifnet_addrs[] */
+ IFA_REMREF(ifa);
+ ifnet_addrs[ifp->if_index - 1] = NULL;
+
+ /* This interface should not be on {ifnet_head,detaching} */
+ VERIFY(ifp->if_link.tqe_next == NULL);
+ VERIFY(ifp->if_link.tqe_prev == NULL);
+ VERIFY(ifp->if_detaching_link.tqe_next == NULL);
+ VERIFY(ifp->if_detaching_link.tqe_prev == NULL);
+
+ /* Prefix list should be empty by now */
+ VERIFY(TAILQ_EMPTY(&ifp->if_prefixhead));
+
+ /* The slot should have been emptied */
+ VERIFY(ifindex2ifnet[ifp->if_index] == NULL);
+
+ /* There should not be any addresses left */
+ VERIFY(TAILQ_EMPTY(&ifp->if_addrhead));
+
+ /*
+ * If thread affinity was set for the workloop thread, we will need
+ * to tear down the affinity and release the extra reference count
+ * taken at attach time;
+ */
+ if ((inputthread = ifp->if_input_thread) != NULL) {
+ if (inputthread->net_affinity) {
+ struct thread *tp;
+
+ if (inputthread == dlil_lo_thread_ptr) {
+ panic("%s: Thread affinity should not be "
+ "enabled on the loopback dlil input "
+ "thread", __func__);
+ /* NOTREACHED */
+ }
+
+ lck_mtx_lock_spin(&inputthread->input_lck);
+ tp = inputthread->workloop_thread;
+ inputthread->workloop_thread = NULL;
+ inputthread->tag = 0;
+ inputthread->net_affinity = FALSE;
+ lck_mtx_unlock(&inputthread->input_lck);
+
+ /* Tear down workloop thread affinity */
+ if (tp != NULL) {
+ (void) dlil_affinity_set(tp,
+ THREAD_AFFINITY_TAG_NULL);
+ thread_deallocate(tp);
+ }
+
+ /* Tear down dlil input thread affinity */
+ tp = inputthread->input_thread;
+ (void) dlil_affinity_set(tp, THREAD_AFFINITY_TAG_NULL);
+ thread_deallocate(tp);
+ }
+
+ /* cleanup ifp dlil input thread, if any */
+ ifp->if_input_thread = NULL;
+
+ if (inputthread != dlil_lo_thread_ptr) {
+#ifdef DLIL_DEBUG
+ printf("%s: wakeup thread threadinfo: %p "
+ "input_thread=%p threads: cur=%d max=%d\n",
+ __func__, inputthread, inputthread->input_thread,
+ dlil_multithreaded_input, cur_dlil_input_threads);
+#endif
+ lck_mtx_lock_spin(&inputthread->input_lck);
+
+ inputthread->input_waiting |= DLIL_INPUT_TERMINATE;
+ if (!(inputthread->input_waiting & DLIL_INPUT_RUNNING))
+ wakeup((caddr_t)&inputthread->input_waiting);
+
+ lck_mtx_unlock(&inputthread->input_lck);
+ }
+ }
+
+ /* The driver might unload, so point these to ourselves */
+ if_free = ifp->if_free;
+ ifp->if_output = ifp_if_output;
+ ifp->if_ioctl = ifp_if_ioctl;
+ ifp->if_set_bpf_tap = ifp_if_set_bpf_tap;
+ ifp->if_free = ifp_if_free;
+ ifp->if_demux = ifp_if_demux;
+ ifp->if_event = ifp_if_event;
+ ifp->if_framer = ifp_if_framer;
+ ifp->if_add_proto = ifp_if_add_proto;
+ ifp->if_del_proto = ifp_if_del_proto;
+ ifp->if_check_multi = ifp_if_check_multi;
+
+ ifnet_lock_done(ifp);
+
+#if PF
+ /*
+ * Detach this interface from packet filter, if enabled.
+ */
+ pf_ifnet_hook(ifp, 0);
+#endif /* PF */
+
+ /* Filter list should be empty */
+ lck_mtx_lock_spin(&ifp->if_flt_lock);
+ VERIFY(TAILQ_EMPTY(&ifp->if_flt_head));
+ VERIFY(ifp->if_flt_busy == 0);
+ VERIFY(ifp->if_flt_waiters == 0);
+ lck_mtx_unlock(&ifp->if_flt_lock);
+
+ /* Last chance to cleanup any cached route */
+ lck_mtx_lock(&ifp->if_cached_route_lock);
+ VERIFY(!ifp->if_fwd_cacheok);
+ if (ifp->if_fwd_route.ro_rt != NULL)
+ rtfree(ifp->if_fwd_route.ro_rt);
+ bzero(&ifp->if_fwd_route, sizeof (ifp->if_fwd_route));
+ if (ifp->if_src_route.ro_rt != NULL)
+ rtfree(ifp->if_src_route.ro_rt);
+ bzero(&ifp->if_src_route, sizeof (ifp->if_src_route));
+ if (ifp->if_src_route6.ro_rt != NULL)
+ rtfree(ifp->if_src_route6.ro_rt);
+ bzero(&ifp->if_src_route6, sizeof (ifp->if_src_route6));
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+
+ ifnet_llreach_ifdetach(ifp);
+
+ dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHED, NULL, 0);
+
+ if (if_free != NULL)
+ if_free(ifp);
+
+ /*
+ * Finally, mark this ifnet as detached.
+ */
+ lck_mtx_lock_spin(&ifp->if_ref_lock);
+ if (!(ifp->if_refflags & IFRF_DETACHING)) {
+ panic("%s: flags mismatch (detaching not set) ifp=%p",
+ __func__, ifp);
+ /* NOTREACHED */
+ }
+ ifp->if_refflags &= ~IFRF_DETACHING;
+ lck_mtx_unlock(&ifp->if_ref_lock);
+
+ if (dlil_verbose)
+ printf("%s%d: detached\n", ifp->if_name, ifp->if_unit);
+
+ /* Release reference held during ifnet attach */
+ ifnet_release(ifp);
+}
+
+static errno_t
+ifp_if_output(struct ifnet *ifp, struct mbuf *m)
+{
+#pragma unused(ifp)
+ m_freem(m);
+ return (0);
+}
+
+static errno_t
+ifp_if_demux(struct ifnet *ifp, struct mbuf *m, char *fh, protocol_family_t *pf)
+{
+#pragma unused(ifp, fh, pf)
+ m_freem(m);
+ return (EJUSTRETURN);
+}
+
+static errno_t
+ifp_if_add_proto(struct ifnet *ifp, protocol_family_t pf,
+ const struct ifnet_demux_desc *da, u_int32_t dc)
+{
+#pragma unused(ifp, pf, da, dc)
+ return (EINVAL);
+}
+
+static errno_t
+ifp_if_del_proto(struct ifnet *ifp, protocol_family_t pf)
+{
+#pragma unused(ifp, pf)
+ return (EINVAL);
+}
+
+static errno_t
+ifp_if_check_multi(struct ifnet *ifp, const struct sockaddr *sa)
+{
+#pragma unused(ifp, sa)
+ return (EOPNOTSUPP);
+}
+
+static errno_t
+ifp_if_framer(struct ifnet *ifp, struct mbuf **m,
+ const struct sockaddr *sa, const char *ll, const char *t)
+{
+#pragma unused(ifp, m, sa, ll, t)
+ m_freem(*m);
+ *m = NULL;
+ return (EJUSTRETURN);
+}
+
+static errno_t
+ifp_if_ioctl(struct ifnet *ifp, unsigned long cmd, void *arg)
+{
+#pragma unused(ifp, cmd, arg)
+ return (EOPNOTSUPP);
+}
+
+static errno_t
+ifp_if_set_bpf_tap(struct ifnet *ifp, bpf_tap_mode tm, bpf_packet_func f)
+{
+#pragma unused(ifp, tm, f)
+ /* XXX not sure what to do here */
+ return (0);
+}
+
+static void
+ifp_if_free(struct ifnet *ifp)
+{
+#pragma unused(ifp)
+}
+
+static void
+ifp_if_event(struct ifnet *ifp, const struct kev_msg *e)
+{
+#pragma unused(ifp, e)
+}
+
+__private_extern__
+int dlil_if_acquire(u_int32_t family, const void *uniqueid,
+ size_t uniqueid_len, struct ifnet **ifp)
+{
+ struct ifnet *ifp1 = NULL;
+ struct dlil_ifnet *dlifp1 = NULL;
+ void *buf, *base, **pbuf;
+ int ret = 0;
+
+ dlil_if_lock();
+ TAILQ_FOREACH(dlifp1, &dlil_ifnet_head, dl_if_link) {
+ ifp1 = (struct ifnet *)dlifp1;
+
+ if (ifp1->if_family != family)
+ continue;
+
+ lck_mtx_lock(&dlifp1->dl_if_lock);
+ /* same uniqueid and same len or no unique id specified */
+ if ((uniqueid_len == dlifp1->dl_if_uniqueid_len) &&
+ !bcmp(uniqueid, dlifp1->dl_if_uniqueid, uniqueid_len)) {
+ /* check for matching interface in use */
+ if (dlifp1->dl_if_flags & DLIF_INUSE) {
+ if (uniqueid_len) {
+ ret = EBUSY;
+ lck_mtx_unlock(&dlifp1->dl_if_lock);
+ goto end;
+ }
+ } else {
+ dlifp1->dl_if_flags |= (DLIF_INUSE|DLIF_REUSE);
+ lck_mtx_unlock(&dlifp1->dl_if_lock);
+ *ifp = ifp1;
+ goto end;
+ }
+ }
+ lck_mtx_unlock(&dlifp1->dl_if_lock);
+ }
+
+ /* no interface found, allocate a new one */
+ buf = zalloc(dlif_zone);
+ if (buf == NULL) {
+ ret = ENOMEM;
+ goto end;
+ }
+ bzero(buf, dlif_bufsize);
+
+ /* Get the 64-bit aligned base address for this object */
+ base = (void *)P2ROUNDUP((intptr_t)buf + sizeof (u_int64_t),
+ sizeof (u_int64_t));
+ VERIFY(((intptr_t)base + dlif_size) <= ((intptr_t)buf + dlif_bufsize));
+
+ /*
+ * Wind back a pointer size from the aligned base and
+ * save the original address so we can free it later.
+ */
+ pbuf = (void **)((intptr_t)base - sizeof (void *));
+ *pbuf = buf;
+ dlifp1 = base;
+
+ if (uniqueid_len) {
+ MALLOC(dlifp1->dl_if_uniqueid, void *, uniqueid_len,
+ M_NKE, M_WAITOK);
+ if (dlifp1->dl_if_uniqueid == NULL) {
+ zfree(dlif_zone, dlifp1);
+ ret = ENOMEM;
+ goto end;
+ }
+ bcopy(uniqueid, dlifp1->dl_if_uniqueid, uniqueid_len);
+ dlifp1->dl_if_uniqueid_len = uniqueid_len;
+ }
+
+ ifp1 = (struct ifnet *)dlifp1;
+ dlifp1->dl_if_flags = DLIF_INUSE;
+ if (ifnet_debug) {
+ dlifp1->dl_if_flags |= DLIF_DEBUG;
+ dlifp1->dl_if_trace = dlil_if_trace;
+ }
+ ifp1->if_name = dlifp1->dl_if_namestorage;
+#if CONFIG_MACF_NET
+ mac_ifnet_label_init(ifp1);
+#endif
+
+ lck_mtx_init(&dlifp1->dl_if_lock, ifnet_lock_group, ifnet_lock_attr);
+ lck_rw_init(&ifp1->if_lock, ifnet_lock_group, ifnet_lock_attr);
+ lck_mtx_init(&ifp1->if_ref_lock, ifnet_lock_group, ifnet_lock_attr);
+ lck_mtx_init(&ifp1->if_flt_lock, ifnet_lock_group, ifnet_lock_attr);
+ lck_mtx_init(&ifp1->if_cached_route_lock, ifnet_lock_group,
+ ifnet_lock_attr);
+ lck_mtx_init(&ifp1->if_addrconfig_lock, ifnet_lock_group,
+ ifnet_lock_attr);
+ lck_rw_init(&ifp1->if_llreach_lock, ifnet_lock_group, ifnet_lock_attr);
+
+ TAILQ_INSERT_TAIL(&dlil_ifnet_head, dlifp1, dl_if_link);
+
+ *ifp = ifp1;
+
+end:
+ dlil_if_unlock();
+
+ VERIFY(dlifp1 == NULL || (IS_P2ALIGNED(dlifp1, sizeof (u_int64_t)) &&
+ IS_P2ALIGNED(&ifp1->if_data, sizeof (u_int64_t))));
+
+ return (ret);
+}
+
+__private_extern__ void
+dlil_if_release(ifnet_t ifp)
+{
+ struct dlil_ifnet *dlifp = (struct dlil_ifnet *)ifp;
+
+ ifnet_lock_exclusive(ifp);
+ lck_mtx_lock(&dlifp->dl_if_lock);
+ dlifp->dl_if_flags &= ~DLIF_INUSE;
+ strncpy(dlifp->dl_if_namestorage, ifp->if_name, IFNAMSIZ);
+ ifp->if_name = dlifp->dl_if_namestorage;
+ lck_mtx_unlock(&dlifp->dl_if_lock);
+#if CONFIG_MACF_NET
+ /*
+ * We can either recycle the MAC label here or in dlil_if_acquire().
+ * It seems logical to do it here but this means that anything that
+ * still has a handle on ifp will now see it as unlabeled.
+ * Since the interface is "dead" that may be OK. Revisit later.
+ */
+ mac_ifnet_label_recycle(ifp);
+#endif
+ ifnet_lock_done(ifp);
+}
+
+__private_extern__ void
+dlil_if_lock(void)
+{
+ lck_mtx_lock(&dlil_ifnet_lock);
+}
+
+__private_extern__ void
+dlil_if_unlock(void)
+{
+ lck_mtx_unlock(&dlil_ifnet_lock);
+}
+
+__private_extern__ void
+dlil_if_lock_assert(void)
+{
+ lck_mtx_assert(&dlil_ifnet_lock, LCK_MTX_ASSERT_OWNED);
+}
+
+__private_extern__ void
+dlil_proto_unplumb_all(struct ifnet *ifp)
+{
+ /*
+ * if_proto_hash[0-3] are for PF_INET, PF_INET6, PF_APPLETALK
+ * and PF_VLAN, where each bucket contains exactly one entry;
+ * PF_VLAN does not need an explicit unplumb.
+ *
+ * if_proto_hash[4] is for other protocols; we expect anything
+ * in this bucket to respond to the DETACHING event (which would
+ * have happened by now) and do the unplumb then.
+ */
+ (void) proto_unplumb(PF_INET, ifp);
+#if INET6
+ (void) proto_unplumb(PF_INET6, ifp);
+#endif /* INET6 */
+#if NETAT
+ (void) proto_unplumb(PF_APPLETALK, ifp);
+#endif /* NETAT */
+}
+
+static void
+ifp_src_route_copyout(struct ifnet *ifp, struct route *dst)
+{
+ lck_mtx_lock_spin(&ifp->if_cached_route_lock);
+ lck_mtx_convert_spin(&ifp->if_cached_route_lock);
+
+ route_copyout(dst, &ifp->if_src_route, sizeof (*dst));
+
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+}
+
+static void
+ifp_src_route_copyin(struct ifnet *ifp, struct route *src)
+{
+ lck_mtx_lock_spin(&ifp->if_cached_route_lock);
+ lck_mtx_convert_spin(&ifp->if_cached_route_lock);
+
+ if (ifp->if_fwd_cacheok) {
+ route_copyin(src, &ifp->if_src_route, sizeof (*src));
+ } else {
+ rtfree(src->ro_rt);
+ src->ro_rt = NULL;
+ }
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+}
+
+#if INET6
+static void
+ifp_src_route6_copyout(struct ifnet *ifp, struct route_in6 *dst)
+{
+ lck_mtx_lock_spin(&ifp->if_cached_route_lock);
+ lck_mtx_convert_spin(&ifp->if_cached_route_lock);
+
+ route_copyout((struct route *)dst, (struct route *)&ifp->if_src_route6,
+ sizeof (*dst));
+
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+}
+
+static void
+ifp_src_route6_copyin(struct ifnet *ifp, struct route_in6 *src)
+{
+ lck_mtx_lock_spin(&ifp->if_cached_route_lock);
+ lck_mtx_convert_spin(&ifp->if_cached_route_lock);
+
+ if (ifp->if_fwd_cacheok) {
+ route_copyin((struct route *)src,
+ (struct route *)&ifp->if_src_route6, sizeof (*src));
+ } else {
+ rtfree(src->ro_rt);
+ src->ro_rt = NULL;
+ }
+ lck_mtx_unlock(&ifp->if_cached_route_lock);
+}
+#endif /* INET6 */
+
+struct rtentry *
+ifnet_cached_rtlookup_inet(struct ifnet *ifp, struct in_addr src_ip)
+{
+ struct route src_rt;
+ struct sockaddr_in *dst = (struct sockaddr_in *)(&src_rt.ro_dst);
+
+ ifp_src_route_copyout(ifp, &src_rt);
+
+ if (src_rt.ro_rt == NULL || !(src_rt.ro_rt->rt_flags & RTF_UP) ||
+ src_ip.s_addr != dst->sin_addr.s_addr ||
+ src_rt.ro_rt->generation_id != route_generation) {
+ if (src_rt.ro_rt != NULL) {
+ rtfree(src_rt.ro_rt);
+ src_rt.ro_rt = NULL;
+ } else if (dst->sin_family != AF_INET) {
+ bzero(&src_rt.ro_dst, sizeof (src_rt.ro_dst));
+ dst->sin_len = sizeof (src_rt.ro_dst);
+ dst->sin_family = AF_INET;
+ }
+ dst->sin_addr = src_ip;
+
+ if (src_rt.ro_rt == NULL) {
+ src_rt.ro_rt = rtalloc1_scoped((struct sockaddr *)dst,
+ 0, 0, ifp->if_index);
+
+ if (src_rt.ro_rt != NULL) {
+ /* retain a ref, copyin consumes one */
+ struct rtentry *rte = src_rt.ro_rt;
+ RT_ADDREF(rte);
+ ifp_src_route_copyin(ifp, &src_rt);
+ src_rt.ro_rt = rte;
+ }
+ }
+ }
+
+ return (src_rt.ro_rt);
+}
+
+#if INET6
+struct rtentry*
+ifnet_cached_rtlookup_inet6(struct ifnet *ifp, struct in6_addr *src_ip6)
+{
+ struct route_in6 src_rt;
+
+ ifp_src_route6_copyout(ifp, &src_rt);
+
+ if (src_rt.ro_rt == NULL || !(src_rt.ro_rt->rt_flags & RTF_UP) ||
+ !IN6_ARE_ADDR_EQUAL(src_ip6, &src_rt.ro_dst.sin6_addr) ||
+ src_rt.ro_rt->generation_id != route_generation) {
+ if (src_rt.ro_rt != NULL) {
+ rtfree(src_rt.ro_rt);
+ src_rt.ro_rt = NULL;
+ } else if (src_rt.ro_dst.sin6_family != AF_INET6) {
+ bzero(&src_rt.ro_dst, sizeof (src_rt.ro_dst));
+ src_rt.ro_dst.sin6_len = sizeof (src_rt.ro_dst);
+ src_rt.ro_dst.sin6_family = AF_INET6;
+ }
+ src_rt.ro_dst.sin6_scope_id = in6_addr2scopeid(ifp, src_ip6);
+ src_rt.ro_dst.sin6_addr = *src_ip6;
+
+ if (src_rt.ro_rt == NULL) {
+ src_rt.ro_rt = rtalloc1_scoped(
+ (struct sockaddr *)&src_rt.ro_dst, 0, 0,
+ ifp->if_index);
+
+ if (src_rt.ro_rt != NULL) {
+ /* retain a ref, copyin consumes one */
+ struct rtentry *rte = src_rt.ro_rt;
+ RT_ADDREF(rte);
+ ifp_src_route6_copyin(ifp, &src_rt);
+ src_rt.ro_rt = rte;
+ }
+ }
+ }
+
+ return (src_rt.ro_rt);
+}
+#endif /* INET6 */