2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1999 Apple Computer, Inc.
33 * Data Link Inteface Layer
39 #include <sys/kernel_types.h>
40 #include <net/kpi_interface.h>
53 #define DLIL_LAST_FILTER -1
54 #define DLIL_NULL_FILTER -2
56 #define DLIL_WAIT_FOR_FREE -2
58 #define DLIL_BLUEBOX 1
63 #include <net/if_var.h>
64 #include <sys/kern_event.h>
79 struct dlil_if_flt_str
{
81 int (*filter_if_input
)(caddr_t cookie
,
83 struct mbuf
**mbuf_ptr
,
86 int (*filter_if_event
)(caddr_t cookie
,
88 struct kev_msg
*event_msg_ptr
);
90 int (*filter_if_output
)(caddr_t cookie
,
92 struct mbuf
**mbuf_ptr
);
95 int (*filter_if_ioctl
)(caddr_t cookie
,
97 u_long ioctl_code_ptr
,
98 caddr_t ioctl_arg_ptr
);
100 int (*filter_if_free
)(caddr_t cookie
,
103 int (*filter_detach
)(caddr_t cookie
);
107 #define DLIL_PR_FILTER 1
108 #define DLIL_IF_FILTER 2
112 typedef int (*dl_input_func
)(struct mbuf
*m
, char *frame_header
,
113 struct ifnet
*ifp
, u_long protocol_family
, int sync_ok
);
114 typedef int (*dl_pre_output_func
)(struct ifnet
*ifp
,
115 u_long protocol_family
,
117 const struct sockaddr
*dest
,
122 typedef void (*dl_event_func
)(struct ifnet
*ifp
, struct kev_msg
*event
);
124 typedef int (*dl_offer_func
)(struct mbuf
*m
, char *frame_header
);
125 typedef int (*dl_ioctl_func
)(u_long protocol_family
,
129 typedef int (*dl_detached_func
)(u_long protocol_family
, struct ifnet
*ifp
);
132 #define DLIL_DESC_RAW 1
133 #define DLIL_DESC_802_2 2
134 #define DLIL_DESC_802_2_SNAP 3
136 * DLIL_DESC_RAW - obsolete type, data in variants.bitmask or native_type
137 * if variants.bitmask.proto_id_length, native_type in host
139 * DLIL_DESC_802_2 - obsolete, data in variants.desc_802_2
140 * DLIL_DESC_802_2_SNAP - obsolete, data in variants.desc_802_2_SNAP
141 * protocol field in host byte order
143 #endif KERNEL_PRIVATE
145 /* Ethernet specific types */
146 #define DLIL_DESC_ETYPE2 4
147 #define DLIL_DESC_SAP 5
148 #define DLIL_DESC_SNAP 6
150 * DLIL_DESC_ETYPE2 - native_type must point to 2 byte ethernet raw protocol,
151 * variants.native_type_length must be set to 2
152 * DLIL_DESC_SAP - native_type must point to 3 byte SAP protocol
153 * variants.native_type_length must be set to 3
154 * DLIL_DESC_SNAP - native_type must point to 5 byte SNAP protocol
155 * variants.native_type_length must be set to 5
157 * All protocols must be in Network byte order.
159 * Future interface families may define more protocol types they know about.
160 * The type implies the offset and context of the protocol data at native_type.
161 * The length of the protocol data specified at native_type must be set in
162 * variants.native_type_length.
165 #ifdef KERNEL_PRIVATE
166 struct dlil_demux_desc
{
167 TAILQ_ENTRY(dlil_demux_desc
) next
;
173 /* Structs in this union are obsolete. They exist for binary compatability only */
174 /* Only the native_type_length is used */
176 u_long proto_id_length
; /* IN LONGWORDS!!! */
177 u_char
*proto_id
; /* No longer supported by Ethernet family */
178 u_char
*proto_id_mask
;
189 u_char dsap
; /* Ignored, assumed to be 0xAA */
190 u_char ssap
; /* Ignored, assumed to be 0xAA */
191 u_char control_code
; /* Ignored, assumed to be 0x03 */
193 u_short protocol_type
; /* In host byte order */
196 /* Length of data pointed to by native_type, must be set correctly */
197 u_int32_t native_type_length
;
201 TAILQ_HEAD(ddesc_head_str
, dlil_demux_desc
);
203 struct dlil_proto_reg_str
{
204 struct ddesc_head_str demux_desc_head
;
205 u_long interface_family
;
206 u_long protocol_family
;
208 int default_proto
; /* 0 or 1 */
210 dl_pre_output_func pre_output
;
214 dl_detached_func detached
;
219 int dlil_attach_filter(struct ifnet
*ifp
, const struct iff_filter
*if_filter
,
220 interface_filter_t
*filter_ref
);
222 struct ifnet_stat_increment_param
;
225 dlil_input_with_stats(struct ifnet
*ifp
, struct mbuf
*m_head
, struct mbuf
*m_tail
,
226 const struct ifnet_stat_increment_param
*stats
);
229 dlil_input(struct ifnet
*ifp
, struct mbuf
*m_head
, struct mbuf
*m_tail
);
234 u_long protocol_family
,
235 struct mbuf
*packetlist
,
237 const struct sockaddr
*dest
,
243 u_long protocol_family
,
246 const struct sockaddr
*dest
,
251 dlil_ioctl(u_long proto_family
,
259 const struct sockaddr
*proto_addr
,
260 struct sockaddr
*ll_addr
,
264 * Send arp internal bypasses the check for
268 dlil_send_arp_internal(
271 const struct sockaddr_dl
* sender_hw
,
272 const struct sockaddr
* sender_proto
,
273 const struct sockaddr_dl
* target_hw
,
274 const struct sockaddr
* target_proto
);
280 const struct sockaddr_dl
* sender_hw
,
281 const struct sockaddr
* sender_proto
,
282 const struct sockaddr_dl
* target_hw
,
283 const struct sockaddr
* target_proto
);
286 dlil_ioctl_locked(u_long proto_family
,
292 dlil_attach_protocol(struct dlil_proto_reg_str
*proto
);
295 dlil_detach_protocol(struct ifnet
*ifp
, u_long protocol_family
);
298 dlil_if_attach(struct ifnet
*ifp
);
300 #ifdef BSD_KERNEL_PRIVATE
303 dlil_if_attach_with_address(
305 const struct sockaddr_dl
*ll_addr
);
308 dlil_attach_protocol_kpi(ifnet_t ifp
, protocol_family_t protocol
,
309 const struct ifnet_attach_proto_param
*proto_details
);
311 errno_t
dlil_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
312 bpf_packet_func callback
);
317 dlil_detach_filter(interface_filter_t filter
);
319 struct dlil_ifmod_reg_str
{
320 int (*add_if
)(struct ifnet
*ifp
);
321 int (*del_if
)(struct ifnet
*ifp
);
322 int (*add_proto
)(struct ifnet
*ifp
, u_long protocol_family
,
323 struct ddesc_head_str
*demux_desc_head
);
324 #ifdef __KPI_INTERFACE__
325 ifnet_del_proto_func del_proto
;
326 ifnet_ioctl_func ifmod_ioctl
;
331 int (*shutdown
)(void);
332 int (*init_if
)(struct ifnet
*ifp
);
337 int dlil_reg_if_modules(u_long interface_family
,
338 struct dlil_ifmod_reg_str
*ifmod_reg
);
342 Function : dlil_reg_proto_module
344 A DLIL protocol module is a piece of code that know how to handle a certain type
345 of protocol (PF_INET, PF_INET6, ...) for a certain family of interface (APPLE_IF_FAM_ETHERNET,
346 APPLE_IF_FAM_PPP, ...).
348 dlil_reg_proto_module() allows the registration of such a protocol/interface handler before any
349 interface is attached.
350 Typically, the attach and detach function of the protocol handler will call
351 dlil_{attach/detach}_protocol with the parameter specific to the protocol.
353 The goal of this modules is to insulate the actual protocol (IP, IPv6) from the DLIL details.
356 'protocol_family' is PF_INET, PF_INET6, ...
357 'interface_family' is APPLE_IF_FAM_ETHERNET, APPLE_IF_FAM_PPP, ...
358 'protomod_reg' is the protocol registration structure.
359 'attach_proto' funtion is mandatory.
360 'detach_proto' funtion is optional (DLIL will manage it).
370 No memory can be allocated for internal data structure.
374 The protocol family has already been registered for this interface family.
378 The dlil_protomod_reg_str structure contains incorrect values.
382 int dlil_reg_proto_module(u_long protocol_family
, u_long interface_family
,
383 int (*attach
)(struct ifnet
*ifp
, u_long protocol_family
),
384 int (*detach
)(struct ifnet
*ifp
, u_long protocol_family
));
388 Function : dlil_dereg_proto_module
390 dlil_dereg_proto_module() will unregister the protocol module previously
391 registered with dlil_dereg_proto_module().
393 There is no restriction when to call it.
394 Interfaces or protoco can be attached, it will not prevent the deregistration of the module.
397 'protocol_family' is PF_INET, PF_INET6, ...
398 'interface_family' is APPLE_IF_FAM_ETHERNET, APPLE_IF_FAM_PPP, ...
408 No module was registered..
412 int dlil_dereg_proto_module(u_long protocol_family
, u_long interface_family
);
416 Function : dlil_plumb_protocol
418 dlil_plumb_protocol() will plumb a protocol to an actual interface.
419 This will find a registered protocol module and call its attach function.
420 The module will typically call dlil_attach_protocol with the appropriate parameters.
423 'protocol_family' is PF_INET, PF_INET6, ...
424 'ifp' is the interface to plumb the protocol to.
434 No module was registered.
438 Error returned by the attach_proto function
441 int dlil_plumb_protocol(u_long protocol_family
, struct ifnet
*ifp
);
445 Function : dlil_unplumb_protocol
447 dlil_unplumb_protocol() will unplumb a protocol from an interface.
448 This will find a registered protocol module and call its detach function.
449 The module will typically call dlil_detach_protocol with the appropriate parameters.
450 If no module is found, this function will call dlil_detach_protocol directly.
453 'protocol_family' is PF_INET, PF_INET6, ...
454 'ifp' is APPLE_IF_FAM_ETHERNET, APPLE_IF_FAM_PPP, ...
464 No module was registered.
468 Error returned by the attach_proto function
471 int dlil_unplumb_protocol(u_long protocol_family
, struct ifnet
*ifp
);
474 dlil_inject_if_input(struct mbuf
*m
, char *frame_header
, u_long from_id
);
477 dlil_inject_pr_input(struct mbuf
*m
, char *frame_header
, u_long from_id
);
480 dlil_inject_pr_output(struct mbuf
*m
,
481 struct sockaddr
*dest
,
488 dlil_inject_if_output(struct mbuf
*m
, u_long from_id
);
490 #ifdef KERNEL_PRIVATE
492 dlil_post_msg(struct ifnet
*ifp
,u_long event_subclass
, u_long event_code
,
493 struct net_event_data
*event_data
, u_long event_data_len
);
497 dlil_event(struct ifnet
*ifp
, struct kern_event_msg
*event
);
499 int dlil_dereg_if_modules(u_long interface_family
);
502 dlil_if_detach(struct ifnet
*ifp
);
505 ifp_reference(struct ifnet
*ifp
);
508 ifp_release(struct ifnet
*ifp
);
513 Function : dlil_if_acquire
515 DLIL manages the list of ifnet interfaces allocated using the dlil_if_acquire
516 function. This list if not the same as the list of attached interfaces,
517 visible with ifconfig.
518 This list contains attached as well as detached interfaces.
519 Detached interfaces are kept in the list to prevent the kernel from crashing
522 if it succeeds, dlil_if_acquire returns an ifnet data structure.
523 This ifnet can either be a new allocated block of memory, or an ifnet
524 that already existed and that DLIL has found in its list of unused
525 interface and that matches the family/uniqueid tuple.
527 dlil_if_acquire can fail if the requested interface is already in use,
528 or if no memory is available to create a new interface.
530 The typical sequence of call for a driver will be :
531 dlil_if_acquire(... &ifp)
532 ... Fill in the ifnet ...
538 Important : ifnet allocated by DLIL are managed by DLIL. DLIL takes care
539 of them, and keeps them until a driver wants to reuse them, but DLIL may
540 also decide to free them when not in use by a driver.
542 Note : the structure returned will actually be large enough to contain
543 an arpcom structure (ifnet + ethernet) structure.
544 Drivers cannot extend the structure and must to store their private
545 information in if_sofc and if_private.
548 'family' uniquely identifies DLIL interface family.
549 'uniqueid' is a unique identifier for that interface, managed by the
550 driver (for example MAC address for ethernet).
551 'uniqueid_len' is the length of the unique id.
552 'ifp' contains on output the allocated ifnet.
558 If an ifnet matching the uniqueid is found, the matching ifnet is returned
559 in ifp and the flags IFEF_REUSE and IF_INUSE are set in the if_eflags.
560 The fields in the ifnet are NOT zeroed and may contain old values that
561 the driver can reuse. [They are not necessarily the values that were
562 there when the driver released the ifnet, as protocol might have
563 continued to update them].
565 If no matching ifnet is found, a new structure is allocated and returned
566 in ifp with all fields initialized to 0.
567 The flag IF_INUSE is set in the if_eflags. IFEF_REUSE is NOT set.
568 dlil_if_acquire will copy the uniqueid and keep it for matching purpose.
570 If 'uniqueid' is NULL, then dlil_if_acquire will return the first
571 ifnet that contains a null uniqueid for that family, with the flags
572 IFEF_REUSE and IF_INUSE set.
573 If no ifnet is available, a new one will be created.
577 If no matching interface is found, and no memory can be allocated,
578 dlil_if_acquire will return ENOMEM.
583 If the unique id matches the id of an interface currently in use,
584 dlil_if_acquire will return EBUSY.
585 An interface 'in use' is an allocated interface, not necessarily attached.
589 int dlil_if_acquire(u_long family
, const void *uniqueid
, size_t uniqueid_len
,
595 Function : dlil_if_release
597 dlil_if_release will transfer control of the ifnet to DLIL.
598 DLIL will keep the interface in its list, marking it unused.
599 The fields will be left in their current state, so the driver can reuse
600 the ifnet later, by calling dlil_if_acquire.
601 The if_eflags IF_INUSE will be cleared.
602 The fields if_output, if_ioctl, if_free and if_set_bpf_tap will be changed
603 to point to DLIL private functions.
604 After calling dlil_if_release, the driver can safely terminate and
606 Note: your driver should only call dlil_if_release once your if_free
607 function has been called.
610 ifp is the pointer to the ifnet to release.
614 void dlil_if_release(struct ifnet
*ifp
);
616 #endif /* KERNEL_PRIVATE */