]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/dlil.h
9c3d26ca76a702793e6fa0d8015922a968903768
[apple/xnu.git] / bsd / net / dlil.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1999 Apple Computer, Inc.
32 *
33 * Data Link Inteface Layer
34 * Author: Ted Walker
35 */
36 #ifndef DLIL_H
37 #define DLIL_H
38 #ifdef KERNEL
39 #include <sys/kernel_types.h>
40 #include <net/kpi_interface.h>
41
42 #if __STDC__
43
44 struct ifnet;
45 struct mbuf;
46 struct ether_header;
47 struct sockaddr_dl;
48
49 #endif
50
51
52 #ifdef KERNEL_PRIVATE
53 #define DLIL_LAST_FILTER -1
54 #define DLIL_NULL_FILTER -2
55
56 #define DLIL_WAIT_FOR_FREE -2
57
58 #define DLIL_BLUEBOX 1
59
60
61
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <sys/kern_event.h>
65
66 #endif KERNEL_PRIVATE
67
68 enum {
69 BPF_TAP_DISABLE,
70 BPF_TAP_INPUT,
71 BPF_TAP_OUTPUT,
72 BPF_TAP_INPUT_OUTPUT
73 };
74
75 #ifdef KERNEL_PRIVATE
76 struct kev_msg;
77 struct iff_filter;
78
79 struct dlil_if_flt_str {
80 caddr_t cookie;
81 int (*filter_if_input)(caddr_t cookie,
82 struct ifnet **ifp,
83 struct mbuf **mbuf_ptr,
84 char **frame_ptr);
85
86 int (*filter_if_event)(caddr_t cookie,
87 struct ifnet *ifp,
88 struct kev_msg *event_msg_ptr);
89
90 int (*filter_if_output)(caddr_t cookie,
91 struct ifnet **ifp,
92 struct mbuf **mbuf_ptr);
93
94
95 int (*filter_if_ioctl)(caddr_t cookie,
96 struct ifnet *ifp,
97 u_long ioctl_code_ptr,
98 caddr_t ioctl_arg_ptr);
99
100 int (*filter_if_free)(caddr_t cookie,
101 struct ifnet *ifp);
102
103 int (*filter_detach)(caddr_t cookie);
104 u_long reserved[2];
105 };
106
107 #define DLIL_PR_FILTER 1
108 #define DLIL_IF_FILTER 2
109
110
111
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,
116 struct mbuf **m,
117 const struct sockaddr *dest,
118 caddr_t route_entry,
119 char *frame_type,
120 char *dst_addr);
121
122 typedef void (*dl_event_func)(struct ifnet *ifp, struct kev_msg *event);
123
124 typedef int (*dl_offer_func)(struct mbuf *m, char *frame_header);
125 typedef int (*dl_ioctl_func)(u_long protocol_family,
126 struct ifnet *ifp,
127 u_long ioctl_cmd,
128 caddr_t ioctl_arg);
129 typedef int (*dl_detached_func)(u_long protocol_family, struct ifnet *ifp);
130
131 /* Obsolete types */
132 #define DLIL_DESC_RAW 1
133 #define DLIL_DESC_802_2 2
134 #define DLIL_DESC_802_2_SNAP 3
135 /*
136 * DLIL_DESC_RAW - obsolete type, data in variants.bitmask or native_type
137 * if variants.bitmask.proto_id_length, native_type in host
138 * byte order.
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
142 */
143 #endif KERNEL_PRIVATE
144
145 /* Ethernet specific types */
146 #define DLIL_DESC_ETYPE2 4
147 #define DLIL_DESC_SAP 5
148 #define DLIL_DESC_SNAP 6
149 /*
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
156 *
157 * All protocols must be in Network byte order.
158 *
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.
163 */
164
165 #ifdef KERNEL_PRIVATE
166 struct dlil_demux_desc {
167 TAILQ_ENTRY(dlil_demux_desc) next;
168
169 int type;
170 u_char *native_type;
171
172 union {
173 /* Structs in this union are obsolete. They exist for binary compatability only */
174 /* Only the native_type_length is used */
175 struct {
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;
179 } bitmask;
180
181 struct {
182 u_char dsap;
183 u_char ssap;
184 u_char control_code;
185 u_char pad;
186 } desc_802_2;
187
188 struct {
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 */
192 u_char org[3];
193 u_short protocol_type; /* In host byte order */
194 } desc_802_2_SNAP;
195
196 /* Length of data pointed to by native_type, must be set correctly */
197 u_int32_t native_type_length;
198 } variants;
199 };
200
201 TAILQ_HEAD(ddesc_head_str, dlil_demux_desc);
202
203 struct dlil_proto_reg_str {
204 struct ddesc_head_str demux_desc_head;
205 u_long interface_family;
206 u_long protocol_family;
207 short unit_number;
208 int default_proto; /* 0 or 1 */
209 dl_input_func input;
210 dl_pre_output_func pre_output;
211 dl_event_func event;
212 dl_offer_func offer;
213 dl_ioctl_func ioctl;
214 dl_detached_func detached;
215 u_long reserved[3];
216 };
217
218
219 int dlil_attach_filter(struct ifnet *ifp, const struct iff_filter *if_filter,
220 interface_filter_t *filter_ref);
221
222 struct ifnet_stat_increment_param;
223
224 int
225 dlil_input_with_stats(struct ifnet *ifp, struct mbuf *m_head, struct mbuf *m_tail,
226 const struct ifnet_stat_increment_param *stats);
227
228 int
229 dlil_input(struct ifnet *ifp, struct mbuf *m_head, struct mbuf *m_tail);
230
231 int
232 dlil_output_list(
233 struct ifnet *ifp,
234 u_long protocol_family,
235 struct mbuf *packetlist,
236 caddr_t route,
237 const struct sockaddr *dest,
238 int raw);
239
240 int
241 dlil_output(
242 struct ifnet *ifp,
243 u_long protocol_family,
244 struct mbuf *m,
245 caddr_t route,
246 const struct sockaddr *dest,
247 int raw);
248
249
250 int
251 dlil_ioctl(u_long proto_family,
252 struct ifnet *ifp,
253 u_long ioctl_code,
254 caddr_t ioctl_arg);
255
256 errno_t
257 dlil_resolve_multi(
258 struct ifnet *ifp,
259 const struct sockaddr *proto_addr,
260 struct sockaddr *ll_addr,
261 size_t ll_len);
262
263 /*
264 * Send arp internal bypasses the check for
265 * IPv4LL.
266 */
267 errno_t
268 dlil_send_arp_internal(
269 ifnet_t ifp,
270 u_int16_t arpop,
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);
275
276 errno_t
277 dlil_send_arp(
278 ifnet_t ifp,
279 u_int16_t arpop,
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);
284
285 int
286 dlil_ioctl_locked(u_long proto_family,
287 struct ifnet *ifp,
288 u_long ioctl_code,
289 caddr_t ioctl_arg);
290
291 int
292 dlil_attach_protocol(struct dlil_proto_reg_str *proto);
293
294 int
295 dlil_detach_protocol(struct ifnet *ifp, u_long protocol_family);
296
297 int
298 dlil_if_attach(struct ifnet *ifp);
299
300 #ifdef BSD_KERNEL_PRIVATE
301
302 int
303 dlil_if_attach_with_address(
304 struct ifnet *ifp,
305 const struct sockaddr_dl *ll_addr);
306
307 int
308 dlil_attach_protocol_kpi(ifnet_t ifp, protocol_family_t protocol,
309 const struct ifnet_attach_proto_param *proto_details);
310
311 errno_t dlil_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode,
312 bpf_packet_func callback);
313
314 #endif
315
316 void
317 dlil_detach_filter(interface_filter_t filter);
318
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;
327 #else
328 void* del_proto;
329 void* ifmod_ioctl;
330 #endif
331 int (*shutdown)(void);
332 int (*init_if)(struct ifnet *ifp);
333 u_long reserved[3];
334 };
335
336
337 int dlil_reg_if_modules(u_long interface_family,
338 struct dlil_ifmod_reg_str *ifmod_reg);
339
340 /*
341
342 Function : dlil_reg_proto_module
343
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, ...).
347
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.
352
353 The goal of this modules is to insulate the actual protocol (IP, IPv6) from the DLIL details.
354
355 Parameters :
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).
361
362 Return code :
363
364 0 :
365
366 No error.
367
368 ENOMEM:
369
370 No memory can be allocated for internal data structure.
371
372 EEXIST:
373
374 The protocol family has already been registered for this interface family.
375
376 EINVAL:
377
378 The dlil_protomod_reg_str structure contains incorrect values.
379
380 */
381
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));
385
386 /*
387
388 Function : dlil_dereg_proto_module
389
390 dlil_dereg_proto_module() will unregister the protocol module previously
391 registered with dlil_dereg_proto_module().
392
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.
395
396 Parameters :
397 'protocol_family' is PF_INET, PF_INET6, ...
398 'interface_family' is APPLE_IF_FAM_ETHERNET, APPLE_IF_FAM_PPP, ...
399
400 Return code :
401
402 0 :
403
404 No error.
405
406 ENOENT:
407
408 No module was registered..
409
410 */
411
412 int dlil_dereg_proto_module(u_long protocol_family, u_long interface_family);
413
414 /*
415
416 Function : dlil_plumb_protocol
417
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.
421
422 Parameters :
423 'protocol_family' is PF_INET, PF_INET6, ...
424 'ifp' is the interface to plumb the protocol to.
425
426 Return code :
427
428 0 :
429
430 No error.
431
432 ENOENT:
433
434 No module was registered.
435
436 other:
437
438 Error returned by the attach_proto function
439
440 */
441 int dlil_plumb_protocol(u_long protocol_family, struct ifnet *ifp);
442
443 /*
444
445 Function : dlil_unplumb_protocol
446
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.
451
452 Parameters :
453 'protocol_family' is PF_INET, PF_INET6, ...
454 'ifp' is APPLE_IF_FAM_ETHERNET, APPLE_IF_FAM_PPP, ...
455
456 Return code :
457
458 0 :
459
460 No error.
461
462 ENOENT:
463
464 No module was registered.
465
466 other:
467
468 Error returned by the attach_proto function
469
470 */
471 int dlil_unplumb_protocol(u_long protocol_family, struct ifnet *ifp);
472
473 int
474 dlil_inject_if_input(struct mbuf *m, char *frame_header, u_long from_id);
475
476 int
477 dlil_inject_pr_input(struct mbuf *m, char *frame_header, u_long from_id);
478
479 int
480 dlil_inject_pr_output(struct mbuf *m,
481 struct sockaddr *dest,
482 int raw,
483 char *frame_type,
484 char *dst_linkaddr,
485 u_long from_id);
486
487 int
488 dlil_inject_if_output(struct mbuf *m, u_long from_id);
489
490 #ifdef KERNEL_PRIVATE
491 void
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);
494 #endif
495
496 int
497 dlil_event(struct ifnet *ifp, struct kern_event_msg *event);
498
499 int dlil_dereg_if_modules(u_long interface_family);
500
501 int
502 dlil_if_detach(struct ifnet *ifp);
503
504 void
505 ifp_reference(struct ifnet *ifp);
506
507 void
508 ifp_release(struct ifnet *ifp);
509
510
511 /*
512
513 Function : dlil_if_acquire
514
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
520 by using an old ifp.
521
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.
526
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.
529
530 The typical sequence of call for a driver will be :
531 dlil_if_acquire(... &ifp)
532 ... Fill in the ifnet ...
533 dlil_if_attach(ifp)
534 ... Driver work ...
535 dlil_if_detach(ifp)
536 dlil_if_release(ifp)
537
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.
541
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.
546
547 Parameters :
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.
553
554 Return code :
555
556 0 :
557
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].
564
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.
569
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.
574
575 ENOMEM:
576
577 If no matching interface is found, and no memory can be allocated,
578 dlil_if_acquire will return ENOMEM.
579
580
581 EBUSY:
582
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.
586
587 */
588
589 int dlil_if_acquire(u_long family, const void *uniqueid, size_t uniqueid_len,
590 struct ifnet **ifp);
591
592
593 /*
594
595 Function : dlil_if_release
596
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
605 unload if necessary.
606 Note: your driver should only call dlil_if_release once your if_free
607 function has been called.
608
609 Parameters :
610 ifp is the pointer to the ifnet to release.
611
612 */
613
614 void dlil_if_release(struct ifnet *ifp);
615
616 #endif /* KERNEL_PRIVATE */
617 #endif /* KERNEL */
618 #endif /* DLIL_H */