]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/kpi_interface.h
8f09d0985d3e3a305faafa16d908be85e323471d
[apple/xnu.git] / bsd / net / kpi_interface.h
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*!
23 @header kpi_interface.h
24 This header defines an API to interact with network interfaces in
25 the kernel. The network interface KPI may be used to implement
26 network interfaces or to attach protocols to existing interfaces.
27 */
28
29 #ifndef __KPI_INTERFACE__
30 #define __KPI_INTERFACE__
31 #include <sys/kernel_types.h>
32
33 #ifndef _SA_FAMILY_T
34 #define _SA_FAMILY_T
35 typedef __uint8_t sa_family_t;
36 #endif
37
38 struct timeval;
39 struct sockaddr;
40 struct sockaddr_dl;
41 struct kern_event_msg;
42 struct kev_msg;
43 struct ifnet_demux_desc;
44
45 /*!
46 @enum Interface Families
47 @abstract Constants defining interface families.
48 @constant IFNET_FAMILY_ANY Match interface of any family type.
49 @constant IFNET_FAMILY_LOOPBACK A software loopback interface.
50 @constant IFNET_FAMILY_ETHERNET An Ethernet interface.
51 @constant IFNET_FAMILY_SLIP A SLIP interface.
52 @constant IFNET_FAMILY_TUN A tunnel interface.
53 @constant IFNET_FAMILY_VLAN A virtual LAN interface.
54 @constant IFNET_FAMILY_PPP A PPP interface.
55 @constant IFNET_FAMILY_PVC A PVC interface.
56 @constant IFNET_FAMILY_DISC A DISC interface.
57 @constant IFNET_FAMILY_MDECAP A MDECAP interface.
58 @constant IFNET_FAMILY_GIF A generic tunnel interface.
59 @constant IFNET_FAMILY_FAITH A FAITH (IPv4/IPv6 translation) interface.
60 @constant IFNET_FAMILY_STF A 6to4 interface.
61 @constant IFNET_FAMILY_FIREWIRE An IEEE 1394 (firewire) interface.
62 @constant IFNET_FAMILY_BOND A virtual bonded interface.
63 */
64
65 enum {
66 IFNET_FAMILY_ANY = 0,
67 IFNET_FAMILY_LOOPBACK = 1,
68 IFNET_FAMILY_ETHERNET = 2,
69 IFNET_FAMILY_SLIP = 3,
70 IFNET_FAMILY_TUN = 4,
71 IFNET_FAMILY_VLAN = 5,
72 IFNET_FAMILY_PPP = 6,
73 IFNET_FAMILY_PVC = 7,
74 IFNET_FAMILY_DISC = 8,
75 IFNET_FAMILY_MDECAP = 9,
76 IFNET_FAMILY_GIF = 10,
77 IFNET_FAMILY_FAITH = 11,
78 IFNET_FAMILY_STF = 12,
79 IFNET_FAMILY_FIREWIRE = 13,
80 IFNET_FAMILY_BOND = 14
81 };
82 /*!
83 @typedef ifnet_family_t
84 @abstract Storage type for the interface family.
85 */
86 typedef u_int32_t ifnet_family_t;
87
88 /*!
89 @enum BPF tap mode
90 @abstract Constants defining interface families.
91 @constant BPF_MODE_DISABLED Disable bpf.
92 @constant BPF_MODE_INPUT Enable input only.
93 @constant BPF_MODE_OUTPUT Enable output only.
94 @constant BPF_MODE_INPUT_OUTPUT Enable input and output.
95 */
96
97 enum {
98 BPF_MODE_DISABLED = 0,
99 BPF_MODE_INPUT = 1,
100 BPF_MODE_OUTPUT = 2,
101 BPF_MODE_INPUT_OUTPUT = 3
102 };
103 /*!
104 @typedef bpf_tap_mode
105 @abstract Mode for tapping. BPF_MODE_DISABLED/BPF_MODE_INPUT_OUTPUT etc.
106 */
107 typedef u_int32_t bpf_tap_mode;
108
109 /*!
110 @typedef protocol_family_t
111 @abstract Storage type for the protocol family.
112 */
113 typedef u_int32_t protocol_family_t;
114
115 /*!
116 @enum Interface Abilities
117 @abstract Constants defining interface offload support.
118 @constant IFNET_CSUM_IP Hardware will calculate IPv4 checksums.
119 @constant IFNET_CSUM_TCP Hardware will calculate TCP checksums.
120 @constant IFNET_CSUM_UDP Hardware will calculate UDP checksums.
121 @constant IFNET_CSUM_FRAGMENT Hardware will checksum IP fragments.
122 @constant IFNET_IP_FRAGMENT Hardware will fragment IP packets.
123 @constant IFNET_VLAN_TAGGING Hardware will generate VLAN headers.
124 @constant IFNET_VLAN_MTU Hardware supports VLAN MTU.
125 */
126
127 enum {
128 IFNET_CSUM_IP = 0x00000001,
129 IFNET_CSUM_TCP = 0x00000002,
130 IFNET_CSUM_UDP = 0x00000004,
131 IFNET_CSUM_FRAGMENT = 0x00000008,
132 IFNET_IP_FRAGMENT = 0x00000010,
133 #ifdef KERNEL_PRIVATE
134 IFNET_CSUM_SUM16 = 0x00001000,
135 #endif
136 IFNET_VLAN_TAGGING = 0x00010000,
137 IFNET_VLAN_MTU = 0x00020000,
138 };
139 /*!
140 @typedef ifnet_offload_t
141 @abstract Flags indicating the offload support of the interface.
142 */
143 typedef u_int32_t ifnet_offload_t;
144
145 /*
146 * Callbacks
147 *
148 * These are function pointers you supply to the kernel in the interface.
149 */
150 /*!
151 @typedef bpf_packet_func
152
153 @discussion bpf_packet_func The bpf_packet_func is used to intercept
154 inbound and outbound packets. The tap function will never free
155 the mbuf. The tap function will only copy the mbuf in to various
156 bpf file descriptors tapping this interface.
157 @param interface The interface being sent or received on.
158 @param data The packet to be transmitted or received.
159 @result An errno value or zero upon success.
160 */
161 /* Fast path - do not block or spend excessive amounts of time */
162 typedef errno_t (*bpf_packet_func)(ifnet_t interface, mbuf_t data);
163
164 /*!
165 @typedef ifnet_output_func
166
167 @discussion ifnet_output_func is used to transmit packets. The stack
168 will pass fully formed packets, including frame header, to the
169 ifnet_output function for an interface. The driver is
170 responsible for freeing the mbuf.
171 @param interface The interface being sent on.
172 @param data The packet to be sent.
173 */
174 /* Fast path - do not block or spend excessive amounts of time */
175 typedef errno_t (*ifnet_output_func)(ifnet_t interface, mbuf_t data);
176
177 /*!
178 @typedef ifnet_ioctl_func
179 @discussion ifnet_ioctl_func is used to communicate ioctls from the
180 stack to the driver.
181 @param interface The interface the ioctl is being sent to.
182 @param proto_family The protocol family to handle the ioctl, may be
183 zero for no protocol_family.
184 @param cmd The ioctl command.
185 @param data A pointer to any data related to the ioctl.
186 */
187 typedef errno_t (*ifnet_ioctl_func)(ifnet_t interface, u_int32_t cmd, void *data);
188
189 /*!
190 @typedef ifnet_set_bpf_tap
191 @discussion ifnet_set_bpf_tap is used to set the bpf tap function to
192 be called when packets are sent and/or received.
193 @param interface The interface the bpf tap function is being set on.
194 @param mode Sets the mode of the tap to either disabled, input,
195 output, or input/output.
196 @param callback A function pointer to be called when a packet is
197 sent or received.
198 */
199 typedef errno_t (*ifnet_set_bpf_tap)(ifnet_t interface, bpf_tap_mode mode,
200 bpf_packet_func callback);
201
202 /*!
203 @typedef ifnet_detached_func
204 @discussion ifnet_detached_func is called an interface is detached
205 from the list of interfaces. When ifnet_detach is called, it may
206 not detach the interface immediately if protocols are attached.
207 ifnet_detached_func is used to notify the interface that it has
208 been detached from the networking stack. This is the last
209 function that will be called on an interface. Until this
210 function returns, you must not unload a kext supplying function
211 pointers to this interface, even if ifnet_detacah has been
212 called. Your detach function may be called during your call to
213 ifnet_detach.
214 @param interface The interface that has been detached.
215 event.
216 */
217 typedef void (*ifnet_detached_func)(ifnet_t interface);
218
219 /*!
220 @typedef ifnet_demux_func
221 @discussion ifnet_demux_func is called for each inbound packet to determine
222 which protocol family the packet belongs to. This information is then
223 used by the stack to determine which protocol to pass the packet to.
224 This function may return protocol families for protocols that are
225 not attached. If the protocol family has not been attached to the
226 interface, the packet will be discarded.
227 @param interface The interface the packet was received on.
228 @param packet The mbuf containing the packet.
229 @param frame_header A pointer to the frame header.
230 @param protocol_family Upon return, the protocol family matching the
231 packet should be stored here.
232 @result
233 If the result is zero, processing will continue normally.
234 If the result is EJUSTRETURN, processing will stop but the packet will not be freed.
235 If the result is anything else, the processing will stop and the packet will be freed.
236 */
237 typedef errno_t (*ifnet_demux_func)(ifnet_t interface, mbuf_t packet,
238 char *frame_header,
239 protocol_family_t *protocol_family);
240
241 /*!
242 @typedef ifnet_event_func
243 @discussion ifnet_event_func is called when an event occurs on a
244 specific interface.
245 @param interface The interface the event occurred on.
246 @param event_ptr Pointer to a kern_event structure describing the
247 event.
248 */
249 typedef void (*ifnet_event_func)(ifnet_t interface, const struct kev_msg *msg);
250
251 /*!
252 @typedef ifnet_framer_func
253 @discussion ifnet_framer_func is called for each outbound packet to
254 give the interface an opportunity to prepend interface specific
255 headers.
256 @param interface The interface the packet is being sent on.
257 @param packet Pointer to the mbuf containing the packet, caller may
258 set this to a different mbuf upon return. This can happen if the
259 frameout function needs to prepend another mbuf to the chain to
260 have enough space for the header.
261 @param dest The higher layer protocol destination (i.e. IP address).
262 @param dest_linkaddr The link layer address as determined by the
263 protocol's pre-output function.
264 @param frame_type The frame type as determined by the protocol's
265 pre-output function.
266 @result
267 If the result is zero, processing will continue normally.
268 If the result is EJUSTRETURN, processing will stop but the packet will not be freed.
269 If the result is anything else, the processing will stop and the packet will be freed.
270 */
271 typedef errno_t (*ifnet_framer_func)(ifnet_t interface, mbuf_t *packet,
272 const struct sockaddr *dest,
273 const char *desk_linkaddr,
274 const char *frame_type);
275
276 /*!
277 @typedef ifnet_add_proto_func
278 @discussion if_add_proto_func is called by the stack when a protocol
279 is attached to an interface. This gives the interface an
280 opportunity to get a list of protocol description structures
281 for demuxing packets to this protocol (demux descriptors).
282 @param interface The interface the protocol will be attached to.
283 @param protocol_family The family of the protocol being attached.
284 @param demux_array An array of demux descriptors that describe
285 the interface specific ways of identifying packets belonging
286 to this protocol family.
287 @param demux_count The number of demux descriptors in the array.
288 @result
289 If the result is zero, processing will continue normally.
290 If the result is anything else, the add protocol will be aborted.
291 */
292 typedef errno_t (*ifnet_add_proto_func)(ifnet_t interface,
293 protocol_family_t protocol_family,
294 const struct ifnet_demux_desc *demux_array,
295 u_int32_t demux_count);
296
297 /*!
298 @typedef if_del_proto_func
299 @discussion if_del_proto_func is called by the stack when a protocol
300 is being detached from an interface. This gives the interface an
301 opportunity to free any storage related to this specific
302 protocol being attached to this interface.
303 @param interface The interface the protocol will be detached from.
304 @param protocol_family The family of the protocol being detached.
305 @result
306 If the result is zero, processing will continue normally.
307 If the result is anything else, the detach will continue
308 and the error will be returned to the caller.
309 */
310 typedef errno_t (*ifnet_del_proto_func)(ifnet_t interface,
311 protocol_family_t protocol_family);
312
313 /*!
314 @typedef ifnet_check_multi
315 @discussion ifnet_check_multi is called for each multicast address
316 added to an interface. This gives the interface an opportunity
317 to reject invalid multicast addresses before they are attached
318 to the interface.
319
320 To prevent an address from being added to your multicast list,
321 return EADDRNOTAVAIL. If you don't know how to parse/translate
322 the address, return EOPNOTSUPP.
323 @param The interface.
324 @param mcast The multicast address.
325 @result
326 Zero upon success, EADDRNOTAVAIL on invalid multicast,
327 EOPNOTSUPP for addresses the interface does not understand.
328 */
329 typedef errno_t (*ifnet_check_multi)(ifnet_t interface,
330 const struct sockaddr* mcast);
331
332 /*!
333 @typedef proto_media_input
334 @discussion proto_media_input is called for all inbound packets for
335 a specific protocol on a specific interface. This function is
336 registered on an interface using ifnet_attach_protocol.
337 @param ifp The interface the packet was received on.
338 @param protocol_family The protocol of the packet received.
339 @param packet The packet being input.
340 @param header The frame header.
341 @result
342 If the result is zero, the caller will assume the packet was passed
343 to the protocol.
344 If the result is non-zero and not EJUSTRETURN, the caller will free
345 the packet.
346 */
347 typedef errno_t (*proto_media_input)(ifnet_t ifp, protocol_family_t protocol,
348 mbuf_t packet, char* header);
349
350 /*!
351 @typedef proto_media_preout
352 @discussion proto_media_preout is called just before the packet
353 is transmitted. This gives the proto_media_preout function an
354 opportunity to specify the media specific frame type and
355 destination.
356 @param ifp The interface the packet will be sent on.
357 @param protocol_family The protocol of the packet being sent
358 (PF_INET/etc...).
359 @param packet The packet being sent.
360 @param dest The protocol level destination address.
361 @param route A pointer to the routing structure for the packet.
362 @param frame_type The media specific frame type.
363 @param link_layer_dest The media specific destination.
364 @result
365 If the result is zero, processing will continue normally. If the
366 result is non-zero, processing will stop. If the result is
367 non-zero and not EJUSTRETURN, the packet will be freed by the
368 caller.
369 */
370 typedef errno_t (*proto_media_preout)(ifnet_t ifp, protocol_family_t protocol,
371 mbuf_t *packet, const struct sockaddr *dest,
372 void *route, char *frame_type, char *link_layer_dest);
373
374 /*!
375 @typedef proto_media_event
376 @discussion proto_media_event is called to notify this layer of
377 interface specific events.
378 @param ifp The interface.
379 @param protocol_family The protocol family.
380 @param kev_msg The event.
381 */
382 typedef void (*proto_media_event)(ifnet_t ifp, protocol_family_t protocol,
383 const struct kev_msg *event);
384
385 /*!
386 @typedef proto_media_ioctl
387 @discussion proto_media_event allows this layer to handle ioctls.
388 When an ioctl is handled, it is passed to the interface filters,
389 protocol filters, protocol, and interface. If you do not support
390 this ioctl, return EOPNOTSUPP. If you successfully handle the
391 ioctl, return zero. If you return any error other than
392 EOPNOTSUPP, other parts of the stack may not get an opportunity
393 to process the ioctl. If you return EJUSTRETURN, processing will
394 stop and a result of zero will be returned to the caller.
395 @param ifp The interface.
396 @param protocol_family The protocol family.
397 @param command The ioctl command.
398 @param argument The argument to the ioctl.
399 @result
400 See the discussion.
401 */
402 typedef errno_t (*proto_media_ioctl)(ifnet_t ifp, protocol_family_t protocol,
403 u_int32_t command, void* argument);
404
405 /*!
406 @typedef proto_media_detached
407 @discussion proto_media_detached notifies you that your protocol
408 has been detached.
409 @param ifp The interface.
410 @param protocol_family The protocol family.
411 @result
412 See the discussion.
413 */
414 typedef errno_t (*proto_media_detached)(ifnet_t ifp, protocol_family_t protocol);
415
416
417 /*!
418 @typedef proto_media_resolve_multi
419 @discussion proto_media_resolve_multi is called to resolve a
420 protocol layer mulitcast address to a link layer multicast
421 address.
422 @param ifp The interface.
423 @param proto_addr The protocol address.
424 @param out_ll A sockaddr_dl to copy the link layer multicast in to.
425 @param ll_len The length of data allocated for out_ll.
426 @result Return zero on success or an errno error value on failure.
427 */
428 typedef errno_t (*proto_media_resolve_multi)(ifnet_t ifp,
429 const struct sockaddr *proto_addr,
430 struct sockaddr_dl *out_ll, size_t ll_len);
431
432 /*!
433 @typedef proto_media_send_arp
434 @discussion proto_media_send_arp is called by the stack to generate
435 an ARP packet. This field is currently only used with IP. This
436 function should inspect the parameters and transmit an arp
437 packet using the information passed in.
438 @param ifp The interface the arp packet should be sent on.
439 @param protocol_family The protocol family of the addresses
440 (PF_INET).
441 @param arpop The arp operation (usually ARPOP_REQUEST or
442 ARPOP_REPLY).
443 @param sender_hw The value to use for the sender hardware
444 address field. If this is NULL, use the hardware address
445 of the interface.
446 @param sender_proto The value to use for the sender protocol
447 address field. This will not be NULL.
448 @param target_hw The value to use for the target hardware address.
449 If this is NULL, the target hardware address in the ARP packet
450 should be NULL and the link-layer destination for the back
451 should be a broadcast. If this is not NULL, this value should be
452 used for both the link-layer destination and the target hardware
453 address.
454 @param target_proto The target protocol address. This will not be
455 NULL.
456 @result Return zero on success or an errno error value on failure.
457 */
458 typedef errno_t (*proto_media_send_arp)(ifnet_t ifp,
459 u_short arpop,
460 const struct sockaddr_dl* sender_hw,
461 const struct sockaddr* sender_proto,
462 const struct sockaddr_dl* target_hw,
463 const struct sockaddr* target_proto);
464
465 /*!
466 @struct ifnet_stat_increment_param
467 @discussion This structure is used increment the counters on a
468 network interface.
469 @field packets_in The number of packets received.
470 @field bytes_in The number of bytes received.
471 @field errors_in The number of receive errors.
472 @field packets_out The number of packets transmitted.
473 @field bytes_out The number of bytes transmitted.
474 @field errors_out The number of transmission errors.
475 @field collisions The number of collisions seen by this interface.
476 @field dropped The number of packets dropped.
477 */
478
479 struct ifnet_stat_increment_param {
480 u_int32_t packets_in;
481 u_int32_t bytes_in;
482 u_int32_t errors_in;
483
484 u_int32_t packets_out;
485 u_int32_t bytes_out;
486 u_int32_t errors_out;
487
488 u_int32_t collisions;
489 u_int32_t dropped;
490 };
491
492 /*!
493 @struct ifnet_init_params
494 @discussion This structure is used to define various properties of
495 the interface when calling ifnet_init. A copy of these values
496 will be stored in the ifnet and can not be modified while the
497 interface is attached.
498 @field uniqueid An identifier unique to this instance of the
499 interface.
500 @field uniqueid_len The length, in bytes, of the uniqueid.
501 @field name The interface name (i.e. en).
502 @field unit The interface unit number (en0's unit number is 0).
503 @field family The interface family.
504 @field type The interface type (see sys/if_types.h). Must be less
505 than 256. For new types, use IFT_OTHER.
506 @field output The output function for the interface. Every packet the
507 stack attempts to send through this interface will go out through
508 this function.
509 @field demux The function used to determine the protocol family of an
510 incoming packet.
511 @field add_proto The function used to attach a protocol to this interface.
512 @field del_proto The function used to remove a protocol from this interface.
513 @field framer The function used to frame outbound packets, may be NULL.
514 @field softc Driver specific storage. This value can be retrieved from the
515 ifnet using the ifnet_softc function.
516 @field ioctl The function used to handle ioctls.
517 @field set_bpf_tap The function used to set the bpf_tap function.
518 @field detach The function called to let the driver know the interface has been detached.
519 @field event The function to notify the interface of various interface specific kernel events.
520 @field broadcast_addr The link-layer broadcast address for this interface.
521 @field broadcast_len The length of the link-layer broadcast address.
522 */
523
524 struct ifnet_init_params {
525 /* used to match recycled interface */
526 const void* uniqueid; /* optional */
527 u_int32_t uniqueid_len; /* optional */
528
529 /* used to fill out initial values for interface */
530 const char* name; /* required */
531 u_int32_t unit; /* required */
532 ifnet_family_t family; /* required */
533 u_int32_t type; /* required */
534 ifnet_output_func output; /* required */
535 ifnet_demux_func demux; /* required */
536 ifnet_add_proto_func add_proto; /* required */
537 ifnet_del_proto_func del_proto; /* required */
538 ifnet_check_multi check_multi; /* required for non point-to-point interfaces */
539 ifnet_framer_func framer; /* optional */
540 void* softc; /* optional */
541 ifnet_ioctl_func ioctl; /* optional */
542 ifnet_set_bpf_tap set_bpf_tap; /* optional */
543 ifnet_detached_func detach; /* optional */
544 ifnet_event_func event; /* optional */
545 const void *broadcast_addr;/* required for non point-to-point interfaces */
546 u_int32_t broadcast_len; /* required for non point-to-point interfaces */
547 };
548
549 /*!
550 @struct ifnet_stats_param
551 @discussion This structure is used get and set the interface
552 statistics.
553 @field packets_in The number of packets received.
554 @field bytes_in The number of bytes received.
555 @field errors_in The number of receive errors.
556 @field packets_out The number of packets transmitted.
557 @field bytes_out The number of bytes transmitted.
558 @field errors_out The number of transmission errors.
559 @field collisions The number of collisions seen by this interface.
560 @field dropped The number of packets dropped.
561 */
562
563 struct ifnet_stats_param {
564 u_int64_t packets_in;
565 u_int64_t bytes_in;
566 u_int64_t multicasts_in;
567 u_int64_t errors_in;
568
569 u_int64_t packets_out;
570 u_int64_t bytes_out;
571 u_int64_t multicasts_out;
572 u_int64_t errors_out;
573
574 u_int64_t collisions;
575 u_int64_t dropped;
576 u_int64_t no_protocol;
577 };
578
579 /*!
580 @struct ifnet_demux_desc
581 @discussion This structure is to identify packets that belong to a
582 specific protocol. The types supported are interface specific.
583 Ethernet supports ETHER_DESC_ETYPE2, ETHER_DESC_SAP, and
584 ETHER_DESC_SNAP. The type defines the offset in the packet where
585 the data will be matched as well as context. For example, if
586 ETHER_DESC_SNAP is specified, the only valid datalen is 5 and
587 only in the 5 bytes will only be matched when the packet header
588 indicates that the packet is a SNAP packet.
589 @field type The type of identifier data (i.e. ETHER_DESC_ETYPE2)
590 @field data A pointer to an entry of type (i.e. pointer to 0x0800).
591 @field datalen The number of bytes of data used to describe the
592 packet.
593 */
594
595 struct ifnet_demux_desc {
596 u_int32_t type;
597 void* data;
598 u_int32_t datalen;
599 };
600
601 /*!
602 @struct ifnet_attach_proto_param
603 @discussion This structure is used to attach a protocol to an
604 interface. This structure provides the various functions for
605 handling operations related to the protocol on the interface as
606 well as information for how to demux packets for this protocol.
607 @field demux_array An array of ifnet_demux_desc structures
608 describing the protocol.
609 @field demux_count The number of entries in the demux_array array.
610 @field input The function to be called for inbound packets.
611 @field pre_output The function to be called for outbound packets.
612 @field event The function to be called for interface events.
613 @field ioctl The function to be called for ioctls.
614 @field detached The function to be called for handling the detach.
615 */
616 #ifdef KERNEL_PRIVATE
617 #define demux_list demux_array
618 #endif /* KERNEL_PRIVATE */
619
620 struct ifnet_attach_proto_param {
621 struct ifnet_demux_desc *demux_array; /* interface may/may not require */
622 u_int32_t demux_count; /* interface may/may not require */
623
624 proto_media_input input; /* required */
625 proto_media_preout pre_output; /* required */
626 proto_media_event event; /* optional */
627 proto_media_ioctl ioctl; /* optional */
628 proto_media_detached detached; /* optional */
629 proto_media_resolve_multi resolve; /* optional */
630 proto_media_send_arp send_arp; /* optional */
631 };
632
633 __BEGIN_DECLS
634
635 /*
636 * Ifnet creation and reference counting
637 */
638
639 /*!
640 @function ifnet_allocate
641 @discussion Allocate an ifnet_t with an initial refcount of 1. Many
642 parts of the stack do not properly refcount the ifnet_t. In
643 order to avoid freeing the ifnet_t while some parts of the stack
644 may contain a reference to it, the ifnet_ts are only recycled,
645 never freed. A unique id is used to try and recycle the same
646 ifnet_t when allocating an interface. For example, for an
647 ethernet interface, the hardware address of the ethernet card is
648 usually used for the uniqueid. If a PC Card is removed and
649 inserted again, if the ethernet address of the PC card is used,
650 the same ifnet_t will be used for the card the second time it is
651 inserted. In the future, when the ifnet_t is correctly
652 refcounted by all of the stack, the interfaces may be freed and
653 the unique ids ignored.
654 @param init The initial values for the interface. These values can
655 not be changed after the interface has been allocated.
656 @param interface The interface allocated upon success.
657 @result May return ENOMEM if there is insufficient memory or EEXIST
658 if an interface with the same uniqueid and family has already
659 been allocated and is in use.
660 */
661 errno_t ifnet_allocate(const struct ifnet_init_params *init, ifnet_t *interface);
662
663 /*!
664 @function ifnet_reference
665 @discussion Increment the reference count of the ifnet to assure
666 that it will not go away. The interface must already have at
667 least one reference.
668 @param interface The interface to increment the reference count of.
669 @result May return EINVAL if the interface is not valid.
670 */
671 errno_t ifnet_reference(ifnet_t interface);
672
673 /*!
674 @function ifnet_release
675 @discussion Release a reference of the ifnet, this may trigger a
676 free if the reference count reaches 0.
677 @param interface The interface to decrement the reference count of
678 and possibly free.
679 @result May return EINVAL if the interface is not valid.
680 */
681 errno_t ifnet_release(ifnet_t interface);
682
683 /*!
684 @function ifnet_attach
685 @discussion Attaches an interface to the global interface list. The
686 interface must be setup properly before calling attach. The
687 stack will take a reference on the interface and hold it until
688 ifnet_detach is called.
689
690 This function is intended to be called by the driver. A kext
691 must not call this function on an interface the kext does not
692 own.
693 @param interface The interface to attach.
694 @param ll_addr The link layer address of the interface. This is used
695 to fill out the first ifaddr in the list of addresses for the
696 interface. This parameter is not required for interfaces such as
697 PPP that have no link-layer address.
698 @result Will return an error if there is anything wrong with the
699 interface.
700 */
701 errno_t ifnet_attach(ifnet_t interface, const struct sockaddr_dl *ll_addr);
702
703 /*!
704 @function ifnet_detach
705 @discussion Detaches the interface.
706
707 Call this to indicate this interface is no longer valid (i.e. PC
708 Card was removed). This function will begin the process of
709 removing knowledge of this interface from the stack.
710
711 The function will return before the interface is detached. The
712 functions you supplied in to the interface may continue to be
713 called. When the detach has been completed, your detached
714 function will be called. Your kext must not unload until the
715 detached function has been called. The interface will be
716 properly freed when the reference count reaches zero.
717
718 An interface may not be attached again. You must call
719 ifnet_allocate to create a new interface to attach.
720
721 This function is intended to be called by the driver. A kext
722 must not call this function on an interface the kext does not
723 own.
724 @param interface The interface to detach.
725 @result 0 on success, otherwise errno error.
726 */
727 errno_t ifnet_detach(ifnet_t interface);
728
729 /*
730 * Interface manipulation.
731 */
732
733 /*!
734 @function ifnet_softc
735 @discussion Returns the driver's private storage on the interface.
736 @param interface Interface to retrieve the storage from.
737 @result Driver's private storage.
738 */
739 void* ifnet_softc(ifnet_t interface);
740
741 /*!
742 @function ifnet_name
743 @discussion Returns a pointer to the name of the interface.
744 @param interface Interface to retrieve the name from.
745 @result Pointer to the name.
746 */
747 const char* ifnet_name(ifnet_t interface);
748
749 /*!
750 @function ifnet_family
751 @discussion Returns the family of the interface.
752 @param interface Interface to retrieve the unit number from.
753 @result Unit number.
754 */
755 ifnet_family_t ifnet_family(ifnet_t interface);
756
757 /*!
758 @function ifnet_unit
759 @discussion Returns the unit number of the interface.
760 @param interface Interface to retrieve the unit number from.
761 @result Unit number.
762 */
763 u_int32_t ifnet_unit(ifnet_t interface);
764
765 /*!
766 @function ifnet_index
767 @discussion Returns the index of the interface. This index value
768 will match the index you would find in a sockaddr_dl or using
769 if_nametoindex or if_indextoname in user space. The value of the
770 interface index is undefined for an interface that is not
771 currently attached.
772 @param interface Interface to retrieve the index of.
773 @result Index.
774 */
775 u_int32_t ifnet_index(ifnet_t interface);
776
777 /*!
778 @function ifnet_set_flags
779 @discussion Sets the interface flags to match new_flags.
780 @discussion Sets the interface flags to new_flags. This function
781 lets you specify which flags you want to change using the mask.
782 The kernel will effectively take the lock, then set the
783 interface's flags to (if_flags & ~mask) | (new_flags & mask).
784 @param interface Interface to set the flags on.
785 @param new_flags The new set of flags that should be set. These
786 flags are defined in net/if.h
787 @result 0 on success otherwise the errno error.
788 */
789 errno_t ifnet_set_flags(ifnet_t interface, u_int16_t new_flags, u_int16_t mask);
790
791 /*!
792 @function ifnet_flags
793 @discussion Returns the interface flags that are set.
794 @param interface Interface to retrieve the flags from.
795 @result Flags. These flags are defined in net/if.h
796 */
797 u_int16_t ifnet_flags(ifnet_t interface);
798
799
800 #ifdef KERNEL_PRIVATE
801 /*!
802 @function ifnet_set_eflags
803 @discussion Sets the extended interface flags to new_flags. This
804 function lets you specify which flags you want to change using
805 the mask. The kernel will effectively take the lock, then set
806 the interface's extended flags to (if_eflags & ~mask) |
807 (new_flags & mask).
808 @param interface The interface.
809 @param new_flags The new set of flags that should be set. These
810 flags are defined in net/if.h
811 @param mask The mask of flags to be modified.
812 @result 0 on success otherwise the errno error.
813 */
814 errno_t ifnet_set_eflags(ifnet_t interface, u_int32_t new_flags, u_int32_t mask);
815
816 /*!
817 @function ifnet_eflags
818 @discussion Returns the extended interface flags that are set.
819 @param interface Interface to retrieve the flags from.
820 @result Extended flags. These flags are defined in net/if.h
821 */
822 u_int32_t ifnet_eflags(ifnet_t interface);
823 #endif
824
825 /*!
826 @function ifnet_set_offload
827 @discussion Sets a bitfield to indicate special hardware offload
828 support provided by the interface such as hardware checksums and
829 VLAN. This replaces the if_hwassist flags field. Any flags
830 unrecognized by the stack will not be set.
831 @param interface The interface.
832 @param offload The new set of flags indicating which offload options
833 the device supports.
834 @param mask The mask of flags to be modified.
835 @result 0 on success otherwise the errno error.
836 */
837 errno_t ifnet_set_offload(ifnet_t interface, ifnet_offload_t offload);
838
839 /*!
840 @function ifnet_offload
841 @discussion Returns flags indicating which operations can be
842 offloaded to the interface.
843 @param interface Interface to retrieve the offload from.
844 @result Abilities flags, see ifnet_offload_t.
845 */
846 ifnet_offload_t ifnet_offload(ifnet_t interface);
847
848 /*!
849 @function ifnet_set_link_mib_data
850 @discussion Sets the mib link data. The ifnet_t will store the
851 pointer you supply and copy mibLen bytes from the pointer
852 whenever the sysctl for getting interface specific MIB data is
853 used. Since the ifnet_t stores a pointer to your data instead of
854 a copy, you may update the data at the address at any time.
855
856 This function is intended to be called by the driver. A kext
857 must not call this function on an interface the kext does not
858 own.
859 @param interface Interface to set the unit number of.
860 @param mibData A pointer to the data.
861 @param mibLen Length of data pointed to.
862 @result 0 on success otherwise the errno error.
863 */
864 errno_t ifnet_set_link_mib_data(ifnet_t interface, void *mibData, u_int32_t mibLen);
865
866 /*!
867 @function ifnet_get_link_mib_data
868 @discussion Copies the link MIB data in to mibData, up to mibLen
869 bytes. Returns error if the buffer is too small to hold all of
870 the MIB data.
871 @param interface The interface.
872 @param mibData A pointer to space for the mibData to be copied in
873 to.
874 @param mibLen When calling, this should be the size of the buffer
875 passed in mibData. Upon return, this will be the size of data
876 copied in to mibData.
877 @result Returns an error if the buffer size is too small or there is
878 no data.
879 */
880 errno_t ifnet_get_link_mib_data(ifnet_t interface, void *mibData, u_int32_t *mibLen);
881
882 /*!
883 @function ifnet_get_link_mib_data_length
884 @discussion Retrieve the size of the mib data.
885 @param interface The interface.
886 @result Returns the number of bytes of mib data associated with the
887 interface.
888 */
889 u_int32_t ifnet_get_link_mib_data_length(ifnet_t interface);
890
891 /*!
892 @function ifnet_attach_protocol
893 @discussion Attaches a protocol to an interface.
894 @param interface The interface.
895 @param protocol_family The protocol family being attached
896 (PF_INET/PF_APPLETALK/etc...).
897 @param proto_details Details of the protocol being attached.
898 @result 0 on success otherwise the errno error.
899 */
900 errno_t ifnet_attach_protocol(ifnet_t interface, protocol_family_t protocol_family,
901 const struct ifnet_attach_proto_param *proto_details);
902
903 /*!
904 @function ifnet_detach_protocol
905 @discussion Detaches a protocol from an interface.
906 @param interface The interface.
907 @param protocol_family The protocol family of the protocol to
908 detach.
909 @result 0 on success otherwise the errno error.
910 */
911 errno_t ifnet_detach_protocol(ifnet_t interface, protocol_family_t protocol_family);
912
913 /*!
914 @function ifnet_output
915 @discussion Handles an outbound packet on the interface by calling
916 any filters, a protocol preoutput function, the interface framer
917 function, and finally the interface's output function. The
918 protocol_family will be used to apply protocol filters and
919 determine which preoutput function to call. The route and dest
920 parameters will be passed to the preoutput function defined for
921 the attachment of the specified protocol to the specified
922 interface. ifnet_output will free the mbuf chain in the event of
923 an error.
924 @param interface The interface.
925 @param protocol_family The family of the protocol generating this
926 packet (i.e. AF_INET).
927 @param packet The packet to be transmitted.
928 @param route A pointer to a routing structure for this packet. The
929 preoutput function determines whether this value may be NULL or
930 not.
931 @param dest The destination address of protocol_family type. This
932 will be passed to the preoutput function. If the preoutput
933 function does not require this value, you may pass NULL.
934 @result 0 on success otherwise the errno error.
935 */
936 errno_t ifnet_output(ifnet_t interface, protocol_family_t protocol_family, mbuf_t packet,
937 void* route, const struct sockaddr *dest);
938
939 /*!
940 @function ifnet_output_raw
941 @discussion Handles and outbond raw packet on the interface by
942 calling any filters followed by the interface's output function.
943 protocol_family may be zero. If the packet is from a specific
944 protocol the protocol_family will be used to apply protocol
945 filters. All interface filters will be applied to the outgoing
946 packet. Processing, such as calling the protocol preoutput and
947 interface framer functions will be bypassed. The packet will
948 pass through the filters and be sent on the interface as is.
949 ifnet_output_raw will free the packet chain in the event of an
950 error.
951 @param interface The interface.
952 @param protocol_family The family of the protocol generating this
953 packet (i.e. AF_INET).
954 @param packet The fully formed packet to be transmitted.
955 @result 0 on success otherwise the errno error.
956 */
957 errno_t ifnet_output_raw(ifnet_t interface, protocol_family_t protocol_family, mbuf_t packet);
958
959 /*!
960 @function ifnet_input
961 @discussion Inputs packets from the interface. The interface's demux
962 will be called to determine the protocol. Once the protocol is
963 determined, the interface filters and protocol filters will be
964 called. From there, the packet will be passed to the registered
965 protocol. If there is an error, the mbuf chain will be freed.
966 @param interface The interface.
967 @param first_packet The first packet in a chain of packets.
968 @param stats Counts to be integrated in to the stats. The interface
969 statistics will be incremented by the amounts specified in
970 stats. This parameter may be NULL.
971 @result 0 on success otherwise the errno error.
972 */
973 errno_t ifnet_input(ifnet_t interface, mbuf_t first_packet,
974 const struct ifnet_stat_increment_param *stats);
975
976 /*!
977 @function ifnet_ioctl
978 @discussion Calls the interface's ioctl function with the parameters
979 passed.
980 @param interface The interface.
981 @param protocol The protocol family of the protocol to send the
982 ioctl to (may be zero). Some ioctls apply to a protocol while
983 other ioctls apply to just an interface.
984 @param ioctl_code The ioctl to perform.
985 @param ioctl_arg Any parameters to the ioctl.
986 @result 0 on success otherwise the errno error.
987 */
988 errno_t ifnet_ioctl(ifnet_t interface, protocol_family_t protocol,
989 u_int32_t ioctl_code, void *ioctl_arg);
990
991 /*!
992 @function ifnet_event
993 @discussion Calls the interface's event function.
994 @param interface The interface.
995 @param event_ptr Pointer to an kern_event structure describing the
996 event.
997 @result 0 on success otherwise the errno error.
998 */
999 errno_t ifnet_event(ifnet_t interface, struct kern_event_msg* event_ptr);
1000
1001 /*!
1002 @function ifnet_set_mtu
1003 @discussion Sets the value of the MTU in the interface structure.
1004 Calling this function will not notify the driver that the MTU
1005 should be changed. Use the appropriate ioctl.
1006
1007 This function is intended to be called by the driver. A kext
1008 must not call this function on an interface the kext does not
1009 own.
1010 @param interface The interface.
1011 @param mtu The new MTU.
1012 @result 0 on success otherwise the errno error.
1013 */
1014 errno_t ifnet_set_mtu(ifnet_t interface, u_int32_t mtu);
1015
1016 /*!
1017 @function ifnet_mtu
1018 @param interface The interface.
1019 @result The MTU.
1020 */
1021 u_int32_t ifnet_mtu(ifnet_t interface);
1022
1023 /*!
1024 @function ifnet_type
1025 @param interface The interface.
1026 @result The type. See net/if_types.h.
1027 */
1028 u_int8_t ifnet_type(ifnet_t interface);
1029
1030 /*!
1031 @function ifnet_set_addrlen
1032 @discussion
1033 This function is intended to be called by the driver. A kext
1034 must not call this function on an interface the kext does not
1035 own.
1036 @param interface The interface.
1037 @param addrlen The new address length.
1038 @result 0 on success otherwise the errno error.
1039 */
1040 errno_t ifnet_set_addrlen(ifnet_t interface, u_int8_t addrlen);
1041
1042 /*!
1043 @function ifnet_addrlen
1044 @param interface The interface.
1045 @result The address length.
1046 */
1047 u_int8_t ifnet_addrlen(ifnet_t interface);
1048
1049 /*!
1050 @function ifnet_set_hdrlen
1051 @discussion
1052 This function is intended to be called by the driver. A kext
1053 must not call this function on an interface the kext does not
1054 own.
1055 @param interface The interface.
1056 @param hdrlen The new header length.
1057 @result 0 on success otherwise the errno error.
1058 */
1059 errno_t ifnet_set_hdrlen(ifnet_t interface, u_int8_t hdrlen);
1060
1061 /*!
1062 @function ifnet_hdrlen
1063 @param interface The interface.
1064 @result The header length.
1065 */
1066 u_int8_t ifnet_hdrlen(ifnet_t interface);
1067
1068 /*!
1069 @function ifnet_set_metric
1070 @discussion
1071 This function is intended to be called by the driver. A kext
1072 must not call this function on an interface the kext does not
1073 own.
1074 @param interface The interface.
1075 @param metric The new metric.
1076 @result 0 on success otherwise the errno error.
1077 */
1078 errno_t ifnet_set_metric(ifnet_t interface, u_int32_t metric);
1079
1080 /*!
1081 @function ifnet_metric
1082 @param interface The interface.
1083 @result The metric.
1084 */
1085 u_int32_t ifnet_metric(ifnet_t interface);
1086
1087 /*!
1088 @function ifnet_set_baudrate
1089 @discussion
1090 This function is intended to be called by the driver. A kext
1091 must not call this function on an interface the kext does not
1092 own.
1093 @param interface The interface.
1094 @param baudrate The new baudrate.
1095 @result 0 on success otherwise the errno error.
1096 */
1097 errno_t ifnet_set_baudrate(ifnet_t interface, u_int64_t baudrate);
1098
1099 /*!
1100 @function ifnet_baudrate
1101 @param interface The interface.
1102 @result The baudrate.
1103 */
1104 u_int64_t ifnet_baudrate(ifnet_t interface);
1105
1106 /*!
1107 @function ifnet_stat_increment
1108 @discussion
1109 This function is intended to be called by the driver. A kext
1110 must not call this function on an interface the kext does not
1111 own.
1112 @param interface The interface.
1113 @param counts A pointer to a structure containing the amount to
1114 increment each counter by. Any counts not appearing in the
1115 ifnet_counter_increment structure are handled in the stack.
1116 @result 0 on success otherwise the errno error.
1117 */
1118 errno_t ifnet_stat_increment(ifnet_t interface,
1119 const struct ifnet_stat_increment_param *counts);
1120
1121 /*!
1122 @function ifnet_stat_increment_in
1123 @discussion
1124 This function is intended to be called by the driver. This
1125 function allows a driver to update the inbound interface counts.
1126 The most efficient time to update these counts is when calling
1127 ifnet_input.
1128
1129 A lock protects the counts, this makes the increment functions
1130 expensive. The increment function will update the lastchanged
1131 value.
1132 @param interface The interface.
1133 @param packets_in The number of additional packets received.
1134 @param bytes_in The number of additional bytes received.
1135 @param errors_in The number of additional receive errors.
1136 @result 0 on success otherwise the errno error.
1137 */
1138 errno_t ifnet_stat_increment_in(ifnet_t interface,
1139 u_int32_t packets_in, u_int32_t bytes_in,
1140 u_int32_t errors_in);
1141
1142 /*!
1143 @function ifnet_stat_increment_out
1144 @discussion
1145 This function is intended to be called by the driver. This
1146 function allows a driver to update the outbound interface counts.
1147
1148 A lock protects the counts, this makes the increment functions
1149 expensive. The increment function will update the lastchanged
1150 value.
1151 @param interface The interface.
1152 @param packets_out The number of additional packets sent.
1153 @param bytes_out The number of additional bytes sent.
1154 @param errors_out The number of additional send errors.
1155 @result 0 on success otherwise the errno error.
1156 */
1157 errno_t ifnet_stat_increment_out(ifnet_t interface,
1158 u_int32_t packets_out, u_int32_t bytes_out,
1159 u_int32_t errors_out);
1160
1161 /*!
1162 @function ifnet_set_stat
1163 @discussion
1164 This function is intended to be called by the driver. A kext
1165 must not call this function on an interface the kext does not
1166 own.
1167
1168 The one exception would be the case where a kext wants to zero
1169 all of the counters.
1170 @param interface The interface.
1171 @param counts The new stats values.
1172 @result 0 on success otherwise the errno error.
1173 */
1174 errno_t ifnet_set_stat(ifnet_t interface,
1175 const struct ifnet_stats_param *stats);
1176
1177 /*!
1178 @function ifnet_stat
1179 @param interface The interface.
1180 @param out_stats Storage for the values.
1181 @result 0 on success otherwise the errno error.
1182 */
1183 errno_t ifnet_stat(ifnet_t interface,
1184 struct ifnet_stats_param *out_stats);
1185
1186 /*!
1187 @function ifnet_set_promiscuous
1188 @discussion Enable or disable promiscuous mode on the interface. The
1189 interface keeps an internal count of the number of times
1190 promiscuous mode has been enabled. Promiscuous mode is only
1191 disabled when this count reaches zero. Be sure to disable
1192 promiscuous mode only once for every time you enable it.
1193 @param interface The interface to toggle promiscuous mode on.
1194 @param on If set, the number of promicuous on requests will be
1195 incremented. If this is the first requrest, promiscuous mode
1196 will be enabled. If this is not set, the number of promiscous
1197 clients will be decremented. If this causes the number to reach
1198 zero, promiscuous mode will be disabled.
1199 @result 0 on success otherwise the errno error.
1200 */
1201 errno_t ifnet_set_promiscuous(ifnet_t interface, int on);
1202
1203 /*!
1204 @function ifnet_touch_lastchange
1205 @discussion Updates the lastchange value to now.
1206 @param interface The interface.
1207 @result 0 on success otherwise the errno error.
1208 */
1209 errno_t ifnet_touch_lastchange(ifnet_t interface);
1210
1211 /*!
1212 @function ifnet_lastchange
1213 @param interface The interface.
1214 @param last_change A timeval struct to copy the last time changed in
1215 to.
1216 */
1217 errno_t ifnet_lastchange(ifnet_t interface, struct timeval *last_change);
1218
1219 /*!
1220 @function ifnet_get_address_list
1221 @discussion Get a list of addresses on the interface. Passing NULL
1222 for the interface will return a list of all addresses. The
1223 addresses will have their reference count bumped so they will
1224 not go away. Calling ifnet_free_address_list will decrement the
1225 refcount and free the array. If you wish to hold on to a
1226 reference to an ifaddr_t, be sure to bump the reference count
1227 before calling ifnet_free_address_list.
1228 @param interface The interface.
1229 @param addresses A pointer to a NULL terminated array of ifaddr_ts.
1230 @result 0 on success otherwise the errno error.
1231 */
1232 errno_t ifnet_get_address_list(ifnet_t interface, ifaddr_t **addresses);
1233
1234 /*!
1235 @function ifnet_get_address_list_family
1236 @discussion Get a list of addresses on the interface. Passing NULL
1237 for the interface will return a list of all addresses. The
1238 addresses will have their reference count bumped so they will
1239 not go away. Calling ifnet_free_address_list will decrement the
1240 refcount and free the array. If you wish to hold on to a
1241 reference to an ifaddr_t, be sure to bump the reference count
1242 before calling ifnet_free_address_list. Unlike
1243 ifnet_get_address_list, this function lets the caller specify
1244 the address family to get a list of only a specific address type.
1245 @param interface The interface.
1246 @param addresses A pointer to a NULL terminated array of ifaddr_ts.
1247 @result 0 on success otherwise the errno error.
1248 */
1249 errno_t ifnet_get_address_list_family(ifnet_t interface, ifaddr_t **addresses, sa_family_t family);
1250
1251 /*!
1252 @function ifnet_free_address_list
1253 @discussion Free a list of addresses returned from
1254 ifnet_get_address_list. Decrements the refcounts and frees the
1255 memory used for the array of references.
1256 @param addresses An array of ifaddr_ts.
1257 */
1258 void ifnet_free_address_list(ifaddr_t *addresses);
1259
1260 /*!
1261 @function ifnet_set_lladdr
1262 @discussion Sets the link-layer address for this interface.
1263 @param interface The interface the link layer address is being
1264 changed on.
1265 @param lladdr A pointer to the raw link layer address (pointer to
1266 the 6 byte ethernet address for ethernet).
1267 @param lladdr_len The length, in bytes, of the link layer address.
1268 */
1269 errno_t ifnet_set_lladdr(ifnet_t interface, const void* lladdr, size_t lladdr_len);
1270
1271 /*!
1272 @function ifnet_lladdr_copy_bytes
1273 @discussion Copies the bytes of the link-layer address in to the
1274 specified buffer.
1275 @param interface The interface to copy the link-layer address from.
1276 @param lladdr The buffer to copy the link-layer address in to.
1277 @param length The length of the buffer. This value must match the
1278 length of the link-layer address.
1279 */
1280 errno_t ifnet_lladdr_copy_bytes(ifnet_t interface, void* lladdr, size_t length);
1281
1282 #ifdef KERNEL_PRIVATE
1283 /*!
1284 @function ifnet_lladdr
1285 @discussion Returns a pointer to the link-layer address.
1286 @param interface The interface the link-layer address is on.
1287 */
1288 void* ifnet_lladdr(ifnet_t interface);
1289 #endif KERNEL_PRIVATE
1290
1291 /*!
1292 @function ifnet_llbroadcast_copy_bytes
1293 @discussion Retrieves the link-layer broadcast address for this
1294 interface.
1295 @param interface The interface.
1296 @param addr A buffer to copy the broadcast address in to.
1297 @param bufferlen The length of the buffer at addr.
1298 @param addr_len On return, the length of the broadcast address.
1299 @param lladdr_len The length, in bytes, of the link layer address.
1300 */
1301 errno_t ifnet_llbroadcast_copy_bytes(ifnet_t interface, void* addr,
1302 size_t bufferlen, size_t* addr_len);
1303
1304 #ifdef KERNEL_PRIVATE
1305 /*!
1306 @function ifnet_set_lladdr_and_type
1307 @discussion Sets the link-layer address as well as the type field in
1308 the sockaddr_dl. Support for setting the type was added for vlan
1309 and bond interfaces.
1310 @param interface The interface the link layer address is being
1311 changed on.
1312 @param lladdr A pointer to the raw link layer address (pointer to
1313 the 6 byte ethernet address for ethernet).
1314 @param lladdr_len The length, in bytes, of the link layer address.
1315 @param type The link-layer address type.
1316 */
1317 errno_t ifnet_set_lladdr_and_type(ifnet_t interface, const void* lladdr, size_t length, u_char type);
1318 #endif KERNEL_PRIVATE
1319
1320 /*!
1321 @function ifnet_add_multicast
1322 @discussion Joins a multicast and returns an ifmultiaddr_t with the
1323 reference count incremented for you. You are responsible for
1324 decrementing the reference count after calling
1325 ifnet_remove_multicast and making sure you no longer have any
1326 references to the multicast.
1327 @param interface The interface.
1328 @param maddr The multicast address to join. Either a physical
1329 address or logical address to be translated to a physical
1330 address.
1331 @param multicast The resulting ifmultiaddr_t multicast address.
1332 @result 0 on success otherwise the errno error.
1333 */
1334 errno_t ifnet_add_multicast(ifnet_t interface, const struct sockaddr *maddr,
1335 ifmultiaddr_t *multicast);
1336
1337 /*!
1338 @function ifnet_remove_multicast
1339 @discussion Causes the interface to leave the multicast group. The
1340 stack keeps track of how many times ifnet_add_multicast has been
1341 called for a given multicast address. The multicast will only be
1342 removed when the number of times ifnet_remove_multicast has been
1343 called matches the number of times ifnet_add_multicast has been
1344 called.
1345
1346 The memory for the multicast address is not actually freed until
1347 the separate reference count has reached zero. Some parts of the
1348 stack may keep a pointer to the multicast even after that
1349 multicast has been removed from the interface.
1350
1351 When an interface is detached, all of the multicasts are
1352 removed. If the interface of the multicast passed in is no
1353 longer attached, this function will gracefully return,
1354 performing no work.
1355
1356 It is the callers responsibility to release the multicast
1357 address after calling this function.
1358 @param multicast The multicast to be removed.
1359 @result 0 on success otherwise the errno error.
1360 */
1361 errno_t ifnet_remove_multicast(ifmultiaddr_t multicast);
1362
1363 /*!
1364 @function ifnet_get_multicast_list
1365 @discussion Retrieves a list of multicast address the interface is
1366 set to receive. This function allocates and returns an array of
1367 references to the various multicast addresses. The multicasts
1368 have their reference counts bumped on your behalf. Calling
1369 ifnet_free_multicast_list will decrement the reference counts
1370 and free the array.
1371 @param interface The interface.
1372 @param multicasts A pointer to a NULL terminated array of references
1373 to the multicast addresses.
1374 @result 0 on success otherwise the errno error.
1375 */
1376 errno_t ifnet_get_multicast_list(ifnet_t interface, ifmultiaddr_t **addresses);
1377
1378 /*!
1379 @function ifnet_free_multicast_list
1380 @discussion Frees a list of multicasts returned by
1381 ifnet_get_multicast_list. Decrements the refcount on each
1382 multicast address and frees the array.
1383 @param multicasts An array of references to the multicast addresses.
1384 @result 0 on success otherwise the errno error.
1385 */
1386 void ifnet_free_multicast_list(ifmultiaddr_t *multicasts);
1387
1388 /*!
1389 @function ifnet_find_by_name
1390 @discussion Find an interface by the name including the unit number.
1391 Caller must call ifnet_release on any non-null interface return
1392 value.
1393 @param name The name of the interface, including any unit number
1394 (i.e. "en0").
1395 @param interface A pointer to an interface reference. This will be
1396 filled in if a matching interface is found.
1397 @result 0 on success otherwise the errno error.
1398 */
1399 errno_t ifnet_find_by_name(const char *ifname, ifnet_t *interface);
1400
1401 /*!
1402 @function ifnet_list_get
1403 @discussion Get a list of attached interfaces. List will be set to
1404 point to an array allocated by ifnet_list_get. The interfaces
1405 are refcounted and the counts will be incremented before the
1406 function returns. The list of interfaces must be freed using
1407 ifnet_list_free.
1408 @param family The interface family (i.e. IFNET_FAMILY_ETHERNET). To
1409 find interfaces of all families, use IFNET_FAMILY_ANY.
1410 @param interfaces A pointer to an array of interface references.
1411 @param count A pointer that will be filled in with the number of
1412 matching interfaces in the array.
1413 @result 0 on success otherwise the errno error.
1414 */
1415 errno_t ifnet_list_get(ifnet_family_t family, ifnet_t **interfaces, u_int32_t *count);
1416
1417 /*!
1418 @function ifnet_list_free
1419 @discussion Free a list of interfaces returned by ifnet_list_get.
1420 Decrements the reference count on each interface and frees the
1421 array of references. If you keep a reference to an interface, be
1422 sure to increment the reference count before calling
1423 ifnet_list_free.
1424 @param interfaces An array of interface references from ifnet_list_get.
1425 */
1426 void ifnet_list_free(ifnet_t *interfaces);
1427
1428 /********************************************************************************************/
1429 /* ifaddr_t accessors */
1430 /********************************************************************************************/
1431
1432 /*!
1433 @function ifaddr_reference
1434 @discussion Increment the reference count of an address tied to an
1435 interface.
1436 @param ifaddr The interface address.
1437 @result 0 upon success
1438 */
1439 errno_t ifaddr_reference(ifaddr_t ifaddr);
1440
1441 /*!
1442 @function ifaddr_release
1443 @discussion Decrements the reference count of and possibly frees an
1444 address tied to an interface.
1445 @param ifaddr The interface address.
1446 @result 0 upon success
1447 */
1448 errno_t ifaddr_release(ifaddr_t ifaddr);
1449
1450 /*!
1451 @function ifaddr_address
1452 @discussion Copies the address out of the ifaddr.
1453 @param ifaddr The interface address.
1454 @param out_addr The sockaddr storage for the address.
1455 @param addr_size The size of the storage for the address.
1456 @result 0 upon success
1457 */
1458 errno_t ifaddr_address(ifaddr_t ifaddr, struct sockaddr *out_addr, u_int32_t addr_size);
1459
1460 /*!
1461 @function ifaddr_address
1462 @discussion Returns the address family of the address.
1463 @param ifaddr The interface address.
1464 @result 0 on failure, address family on success.
1465 */
1466 sa_family_t ifaddr_address_family(ifaddr_t ifaddr);
1467
1468 /*!
1469 @function ifaddr_dstaddress
1470 @discussion Copies the destination address out of the ifaddr.
1471 @param ifaddr The interface address.
1472 @param out_dstaddr The sockaddr storage for the destination address.
1473 @param dstaddr_size The size of the storage for the destination address.
1474 @result 0 upon success
1475 */
1476 errno_t ifaddr_dstaddress(ifaddr_t ifaddr, struct sockaddr *out_dstaddr, u_int32_t dstaddr_size);
1477
1478 /*!
1479 @function ifaddr_netmask
1480 @discussion Copies the netmask out of the ifaddr.
1481 @param ifaddr The interface address.
1482 @param out_netmask The sockaddr storage for the netmask.
1483 @param netmask_size The size of the storage for the netmask.
1484 @result 0 upon success
1485 */
1486 errno_t ifaddr_netmask(ifaddr_t ifaddr, struct sockaddr *out_netmask, u_int32_t netmask_size);
1487
1488 /*!
1489 @function ifaddr_ifnet
1490 @discussion Returns the interface the address is attached to. The
1491 reference is only valid until the ifaddr is released. If you
1492 need to hold a reference to the ifnet for longer than you hold a
1493 reference to the ifaddr, increment the reference using
1494 ifnet_reference.
1495 @param ifaddr The interface address.
1496 @result A reference to the interface the address is attached to.
1497 */
1498 ifnet_t ifaddr_ifnet(ifaddr_t ifaddr);
1499
1500 /*!
1501 @function ifaddr_withaddr
1502 @discussion Returns an interface address with the address specified.
1503 Increments the reference count on the ifaddr before returning to
1504 the caller. Caller is responsible for calling ifaddr_release.
1505 @param address The address to search for.
1506 @result A reference to the interface address.
1507 */
1508 ifaddr_t ifaddr_withaddr(const struct sockaddr* address);
1509
1510 /*!
1511 @function ifaddr_withdstaddr
1512 @discussion Returns an interface address for the interface address
1513 that matches the destination when the netmask is applied.
1514 Increments the reference count on the ifaddr before returning to
1515 the caller. Caller is responsible for calling ifaddr_release.
1516 @param destination The destination to search for.
1517 @result A reference to the interface address.
1518 */
1519 ifaddr_t ifaddr_withdstaddr(const struct sockaddr* destination);
1520
1521 /*!
1522 @function ifaddr_withnet
1523 @discussion Returns an interface address for the interface with the
1524 network described by net. Increments the reference count on the
1525 ifaddr before returning to the caller. Caller is responsible for
1526 calling ifaddr_release.
1527 @param net The network to search for.
1528 @result A reference to the interface address.
1529 */
1530 ifaddr_t ifaddr_withnet(const struct sockaddr* net);
1531
1532 /*!
1533 @function ifaddr_withroute
1534 @discussion Returns an interface address given a destination and
1535 gateway. Increments the reference count on the ifaddr before
1536 returning to the caller. Caller is responsible for calling
1537 ifaddr_release.
1538 @param flags Routing flags. See net/route.h, RTF_GATEWAY etc.
1539 @param destination The destination to search for.
1540 @param gateway A gateway to search for.
1541 @result A reference to the interface address.
1542 */
1543 ifaddr_t ifaddr_withroute(int flags, const struct sockaddr* destination,
1544 const struct sockaddr* gateway);
1545
1546 /*!
1547 @function ifaddr_findbestforaddr
1548 @discussion Finds the best local address assigned to a specific
1549 interface to use when communicating with another address.
1550 Increments the reference count on the ifaddr before returning to
1551 the caller. Caller is responsible for calling ifaddr_release.
1552 @param addr The remote address.
1553 @param interface The local interface.
1554 @result A reference to the interface address.
1555 */
1556 ifaddr_t ifaddr_findbestforaddr(const struct sockaddr *addr, ifnet_t interface);
1557
1558 /********************************************************************************************/
1559 /* ifmultiaddr_t accessors */
1560 /********************************************************************************************/
1561
1562 /*!
1563 @function ifmaddr_reference
1564 @discussion Increment the reference count of an interface multicast
1565 address.
1566 @param ifmaddr The interface multicast address.
1567 @result 0 on success. Only error will be EINVAL if ifmaddr is not valid.
1568 */
1569 errno_t ifmaddr_reference(ifmultiaddr_t ifmaddr);
1570
1571 /*!
1572 @function ifmaddr_release
1573 @discussion Decrement the reference count of an interface multicast
1574 address. If the reference count reaches zero, the ifmultiaddr
1575 will be removed from the interface and the ifmultiaddr will be
1576 freed.
1577 @param ifmaddr The interface multicast address.
1578 @result 0 on success. Only error will be EINVAL if ifmaddr is not valid.
1579 */
1580 errno_t ifmaddr_release(ifmultiaddr_t ifmaddr);
1581
1582 /*!
1583 @function ifmaddr_address
1584 @discussion Copies the multicast address to out_multicast.
1585 @param out_multicast Storage for a sockaddr.
1586 @param addr_size Size of the storage.
1587 @result 0 on success.
1588 */
1589 errno_t ifmaddr_address(ifmultiaddr_t ifmaddr, struct sockaddr *out_multicast, u_int32_t addr_size);
1590
1591 /*!
1592 @function ifmaddr_lladdress
1593 @discussion Copies the link layer multicast address to
1594 out_link_layer_multicast.
1595 @param out_link_layer_multicast Storage for a sockaddr.
1596 @param addr_size Size of the storage.
1597 @result 0 on success.
1598 */
1599 errno_t ifmaddr_lladdress(ifmultiaddr_t ifmaddr, struct sockaddr *out_link_layer_multicast,
1600 u_int32_t addr_size);
1601
1602 /*!
1603 @function ifmaddr_ifnet
1604 @discussion Returns the interface this multicast address is attached
1605 to. The interface reference count is not bumped by this
1606 function. The interface is only valid as long as you don't
1607 release the refernece to the multiast address. If you need to
1608 maintain your pointer to the ifnet, call ifnet_reference
1609 followed by ifnet_release when you're finished.
1610 @param ifmaddr The interface multicast address.
1611 @result A reference to the interface.
1612 */
1613 ifnet_t ifmaddr_ifnet(ifmultiaddr_t ifmaddr);
1614
1615 __END_DECLS
1616
1617 #endif