2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include "kpi_interface.h"
31 #include <sys/queue.h>
32 #include <sys/param.h> /* for definition of NULL */
33 #include <kern/debug.h> /* for panic */
34 #include <sys/errno.h>
35 #include <sys/socket.h>
36 #include <sys/kern_event.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/kpi_mbuf.h>
40 #include <net/if_var.h>
41 #include <net/if_dl.h>
43 #include <net/if_types.h>
44 #include <net/if_dl.h>
45 #include <net/if_arp.h>
46 #include <libkern/libkern.h>
47 #include <libkern/OSAtomic.h>
48 #include <kern/locks.h>
50 #include "net/net_str_id.h"
52 #if IF_LASTCHANGEUPTIME
53 #define TOUCHLASTCHANGE(__if_lastchange) microuptime(__if_lastchange)
55 #define TOUCHLASTCHANGE(__if_lastchange) microtime(__if_lastchange)
58 extern struct dlil_threading_info
*dlil_lo_thread_ptr
;
59 extern int dlil_multithreaded_input
;
62 ifnet_list_get_common(ifnet_family_t
, boolean_t
, ifnet_t
**, u_int32_t
*);
65 Temporary work around until we have real reference counting
67 We keep the bits about calling dlil_if_release (which should be
68 called recycle) transparent by calling it from our if_free function
69 pointer. We have to keep the client's original detach function
70 somewhere so we can call it.
76 ifnet_detached_func detach_func
= ifp
->if_kpi_storage
;
81 if (ifp
->if_broadcast
.length
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
82 FREE(ifp
->if_broadcast
.u
.ptr
, M_IFADDR
);
83 ifp
->if_broadcast
.u
.ptr
= NULL
;
89 static __inline__
void*
90 _cast_non_const(const void * ptr
) {
102 const struct ifnet_init_params
*init
,
106 struct ifnet
*ifp
= NULL
;
108 if (init
->family
== 0)
110 if (init
->name
== NULL
||
111 init
->output
== NULL
)
113 if (strlen(init
->name
) >= IFNAMSIZ
)
115 if ((init
->type
& 0xFFFFFF00) != 0 || init
->type
== 0)
118 error
= dlil_if_acquire(init
->family
, init
->uniqueid
, init
->uniqueid_len
, &ifp
);
122 * Cast ifp->if_name as non const. dlil_if_acquire sets it up
123 * to point to storage of at least IFNAMSIZ bytes. It is safe
126 strncpy(_cast_non_const(ifp
->if_name
), init
->name
, IFNAMSIZ
);
127 ifp
->if_type
= init
->type
;
128 ifp
->if_family
= init
->family
;
129 ifp
->if_unit
= init
->unit
;
130 ifp
->if_output
= init
->output
;
131 ifp
->if_demux
= init
->demux
;
132 ifp
->if_add_proto
= init
->add_proto
;
133 ifp
->if_del_proto
= init
->del_proto
;
134 ifp
->if_check_multi
= init
->check_multi
;
135 ifp
->if_framer
= init
->framer
;
136 ifp
->if_softc
= init
->softc
;
137 ifp
->if_ioctl
= init
->ioctl
;
138 ifp
->if_set_bpf_tap
= init
->set_bpf_tap
;
139 ifp
->if_free
= ifnet_kpi_free
;
140 ifp
->if_event
= init
->event
;
141 ifp
->if_kpi_storage
= init
->detach
;
142 ifp
->if_eflags
|= IFEF_USEKPI
;
144 if (init
->broadcast_len
&& init
->broadcast_addr
) {
145 if (init
->broadcast_len
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
146 MALLOC(ifp
->if_broadcast
.u
.ptr
, u_char
*, init
->broadcast_len
, M_IFADDR
, M_NOWAIT
);
147 if (ifp
->if_broadcast
.u
.ptr
== NULL
) {
151 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.ptr
, init
->broadcast_len
);
155 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.buffer
, init
->broadcast_len
);
157 ifp
->if_broadcast
.length
= init
->broadcast_len
;
160 bzero(&ifp
->if_broadcast
, sizeof(ifp
->if_broadcast
));
165 ifnet_reference(ifp
); // temporary - this should be done in dlil_if_acquire
168 dlil_if_release(ifp
);
174 Note: We should do something here to indicate that we haven't been
175 attached yet. By doing so, we can catch the case in ifnet_release
176 where the reference count reaches zero and call the recycle
177 function. If the interface is attached, the interface will be
178 recycled when the interface's if_free function is called. If the
179 interface is never attached, the if_free function will never be
180 called and the interface will never be recycled.
192 if (ifp
== NULL
) return EINVAL
;
194 oldval
= OSIncrementAtomic(&ifp
->if_refcnt
);
205 if (ifp
== NULL
) return EINVAL
;
207 oldval
= OSDecrementAtomic(&ifp
->if_refcnt
);
209 panic("ifnet_release - refcount decremented past zero!");
215 ifnet_interface_family_find(const char *module_string
, ifnet_family_t
*family_id
)
217 if (module_string
== NULL
|| family_id
== NULL
)
219 return net_str_id_find_internal(module_string
, family_id
, NSI_IF_FAM_ID
, 1);
227 return interface
== NULL
? NULL
: interface
->if_softc
;
234 return interface
== NULL
? NULL
: interface
->if_name
;
241 return interface
== NULL
? 0 : interface
->if_family
;
248 return interface
== NULL
? (u_int32_t
)0xffffffff : (u_int32_t
)interface
->if_unit
;
255 return interface
== NULL
? (u_int32_t
)0xffffffff : interface
->if_index
;
266 if (interface
== NULL
) return EINVAL
;
267 lock
= (interface
->if_lock
!= 0);
269 if (lock
) ifnet_lock_exclusive(interface
);
271 /* If we are modifying the up/down state, call if_updown */
272 if (lock
&& (mask
& IFF_UP
) != 0) {
273 if_updown(interface
, (new_flags
& IFF_UP
) == IFF_UP
);
276 interface
->if_flags
= (new_flags
& mask
) | (interface
->if_flags
& ~mask
);
277 if (lock
) ifnet_lock_done(interface
);
286 return interface
== NULL
? 0 : interface
->if_flags
;
297 if (interface
== NULL
) return EINVAL
;
298 lock
= (interface
->if_lock
!= 0);
300 if (lock
) ifnet_lock_exclusive(interface
);
301 interface
->if_eflags
= (new_flags
& mask
) | (interface
->if_eflags
& ~mask
);
302 if (lock
) ifnet_lock_done(interface
);
311 return interface
== NULL
? 0 : interface
->if_eflags
;
314 static const ifnet_offload_t offload_mask
= IFNET_CSUM_IP
| IFNET_CSUM_TCP
|
315 IFNET_CSUM_UDP
| IFNET_CSUM_FRAGMENT
| IFNET_IP_FRAGMENT
|
316 IFNET_CSUM_SUM16
| IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
|
317 IFNET_MULTIPAGES
| IFNET_TSO_IPV4
| IFNET_TSO_IPV6
;
322 ifnet_offload_t offload
)
326 if (interface
== NULL
) return EINVAL
;
327 lock
= (interface
->if_lock
!= 0);
329 if (lock
) ifnet_lock_exclusive(interface
);
330 interface
->if_hwassist
= (offload
& offload_mask
);
331 if (lock
) ifnet_lock_done(interface
);
340 return interface
== NULL
? 0 : (interface
->if_hwassist
& offload_mask
);
351 if (interface
== NULL
) return EINVAL
;
353 if (mtuLen
< interface
->if_mtu
)
360 if (interface
->if_hwassist
& IFNET_TSO_IPV4
)
361 interface
->if_tso_v4_mtu
= mtuLen
;
367 if (interface
->if_hwassist
& IFNET_TSO_IPV6
)
368 interface
->if_tso_v6_mtu
= mtuLen
;
374 error
= EPROTONOSUPPORT
;
388 if (interface
== NULL
|| mtuLen
== NULL
) return EINVAL
;
393 if (interface
->if_hwassist
& IFNET_TSO_IPV4
)
394 *mtuLen
= interface
->if_tso_v4_mtu
;
400 if (interface
->if_hwassist
& IFNET_TSO_IPV6
)
401 *mtuLen
= interface
->if_tso_v6_mtu
;
406 error
= EPROTONOSUPPORT
;
413 ifnet_set_wake_flags(ifnet_t interface
, u_int32_t properties
, u_int32_t mask
)
416 struct kev_msg ev_msg
;
417 struct net_event_data ev_data
;
419 if (interface
== NULL
)
422 /* Do not accept wacky values */
423 if ((properties
& mask
) & ~IF_WAKE_VALID_FLAGS
)
426 lock
= (interface
->if_lock
!= 0);
429 ifnet_lock_exclusive(interface
);
431 interface
->if_wake_properties
= (properties
& mask
) | (interface
->if_wake_properties
& ~mask
);
434 ifnet_lock_done(interface
);
436 (void) ifnet_touch_lastchange(interface
);
438 /* Notify application of the change */
439 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
440 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
441 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
443 ev_msg
.event_code
= KEV_DL_WAKEFLAGS_CHANGED
;
444 strlcpy(&ev_data
.if_name
[0], interface
->if_name
, IFNAMSIZ
);
445 ev_data
.if_family
= interface
->if_family
;
446 ev_data
.if_unit
= (u_int32_t
) interface
->if_unit
;
447 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
448 ev_msg
.dv
[0].data_ptr
= &ev_data
;
449 ev_msg
.dv
[1].data_length
= 0;
450 kev_post_msg(&ev_msg
);
456 ifnet_get_wake_flags(ifnet_t interface
)
458 return interface
== NULL
? 0 : interface
->if_wake_properties
;
465 * Should MIB data store a copy?
468 ifnet_set_link_mib_data(
475 if (interface
== NULL
) return EINVAL
;
476 lock
= (interface
->if_lock
!= 0);
478 if (lock
) ifnet_lock_exclusive(interface
);
479 interface
->if_linkmib
= (void*)mibData
;
480 interface
->if_linkmiblen
= mibLen
;
481 if (lock
) ifnet_lock_done(interface
);
486 ifnet_get_link_mib_data(
494 if (interface
== NULL
) return EINVAL
;
495 lock
= (interface
->if_lock
!= NULL
);
497 if (lock
) ifnet_lock_shared(interface
);
498 if (*mibLen
< interface
->if_linkmiblen
)
500 if (result
== 0 && interface
->if_linkmib
== NULL
)
504 *mibLen
= interface
->if_linkmiblen
;
505 bcopy(interface
->if_linkmib
, mibData
, *mibLen
);
507 if (lock
) ifnet_lock_done(interface
);
513 ifnet_get_link_mib_data_length(
516 return interface
== NULL
? 0 : interface
->if_linkmiblen
;
522 protocol_family_t protocol_family
,
525 const struct sockaddr
*dest
)
527 if (interface
== NULL
|| protocol_family
== 0 || m
== NULL
) {
532 return dlil_output(interface
, protocol_family
, m
, route
, dest
, 0);
538 protocol_family_t protocol_family
,
541 if (interface
== NULL
|| m
== NULL
) {
546 return dlil_output(interface
, protocol_family
, m
, NULL
, NULL
, 1);
554 if (interface
== NULL
) return EINVAL
;
555 interface
->if_data
.ifi_mtu
= mtu
;
564 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_mtu
;
574 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_type
;
584 int lock
= (interface
->if_lock
!= 0);
585 if (lock
) ifnet_lock_exclusive(interface
);
586 interface
->if_data
.ifi_typelen
= typelen
;
587 if (lock
) ifnet_lock_done(interface
);
596 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_typelen
;
606 if (interface
== NULL
) return EINVAL
;
607 interface
->if_data
.ifi_addrlen
= addrlen
;
616 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_addrlen
;
625 if (interface
== NULL
) return EINVAL
;
626 interface
->if_data
.ifi_hdrlen
= hdrlen
;
635 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_hdrlen
;
644 if (interface
== NULL
) return EINVAL
;
645 interface
->if_data
.ifi_metric
= metric
;
654 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_metric
;
663 if (interface
== NULL
) return EINVAL
;
664 /* Pin baudrate to 32 bits until we can change the storage size */
665 interface
->if_data
.ifi_baudrate
= baudrate
> 0xFFFFFFFF ? 0xFFFFFFFF : baudrate
;
674 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_baudrate
;
679 ifnet_stat_increment(
681 const struct ifnet_stat_increment_param
*counts
)
683 struct dlil_threading_info
*thread
;
684 if (interface
== NULL
) return EINVAL
;
686 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
687 thread
= dlil_lo_thread_ptr
;
689 lck_mtx_lock(thread
->input_lck
);
691 interface
->if_data
.ifi_ipackets
+= counts
->packets_in
;
692 interface
->if_data
.ifi_ibytes
+= counts
->bytes_in
;
693 interface
->if_data
.ifi_ierrors
+= counts
->errors_in
;
695 interface
->if_data
.ifi_opackets
+= counts
->packets_out
;
696 interface
->if_data
.ifi_obytes
+= counts
->bytes_out
;
697 interface
->if_data
.ifi_oerrors
+= counts
->errors_out
;
699 interface
->if_data
.ifi_collisions
+= counts
->collisions
;
700 interface
->if_data
.ifi_iqdrops
+= counts
->dropped
;
702 /* Touch the last change time. */
703 TOUCHLASTCHANGE(&interface
->if_lastchange
);
705 lck_mtx_unlock(thread
->input_lck
);
711 ifnet_stat_increment_in(
713 u_int32_t packets_in
,
717 struct dlil_threading_info
*thread
;
719 if (interface
== NULL
) return EINVAL
;
721 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
722 thread
= dlil_lo_thread_ptr
;
724 lck_mtx_lock(thread
->input_lck
);
726 interface
->if_data
.ifi_ipackets
+= packets_in
;
727 interface
->if_data
.ifi_ibytes
+= bytes_in
;
728 interface
->if_data
.ifi_ierrors
+= errors_in
;
730 TOUCHLASTCHANGE(&interface
->if_lastchange
);
732 lck_mtx_unlock(thread
->input_lck
);
738 ifnet_stat_increment_out(
740 u_int32_t packets_out
,
742 u_int32_t errors_out
)
744 struct dlil_threading_info
*thread
;
745 if (interface
== NULL
) return EINVAL
;
747 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
748 thread
= dlil_lo_thread_ptr
;
750 lck_mtx_lock(thread
->input_lck
);
752 interface
->if_data
.ifi_opackets
+= packets_out
;
753 interface
->if_data
.ifi_obytes
+= bytes_out
;
754 interface
->if_data
.ifi_oerrors
+= errors_out
;
756 TOUCHLASTCHANGE(&interface
->if_lastchange
);
758 lck_mtx_unlock(thread
->input_lck
);
766 const struct ifnet_stats_param
*stats
)
768 struct dlil_threading_info
*thread
;
770 if (interface
== NULL
) return EINVAL
;
772 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
773 thread
= dlil_lo_thread_ptr
;
775 lck_mtx_lock(thread
->input_lck
);
777 interface
->if_data
.ifi_ipackets
= stats
->packets_in
;
778 interface
->if_data
.ifi_ibytes
= stats
->bytes_in
;
779 interface
->if_data
.ifi_imcasts
= stats
->multicasts_in
;
780 interface
->if_data
.ifi_ierrors
= stats
->errors_in
;
782 interface
->if_data
.ifi_opackets
= stats
->packets_out
;
783 interface
->if_data
.ifi_obytes
= stats
->bytes_out
;
784 interface
->if_data
.ifi_omcasts
= stats
->multicasts_out
;
785 interface
->if_data
.ifi_oerrors
= stats
->errors_out
;
787 interface
->if_data
.ifi_collisions
= stats
->collisions
;
788 interface
->if_data
.ifi_iqdrops
= stats
->dropped
;
789 interface
->if_data
.ifi_noproto
= stats
->no_protocol
;
791 /* Touch the last change time. */
792 TOUCHLASTCHANGE(&interface
->if_lastchange
);
794 lck_mtx_unlock(thread
->input_lck
);
802 struct ifnet_stats_param
*stats
)
804 struct dlil_threading_info
*thread
;
806 if (interface
== NULL
) return EINVAL
;
808 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
809 thread
= dlil_lo_thread_ptr
;
811 lck_mtx_lock(thread
->input_lck
);
813 stats
->packets_in
= interface
->if_data
.ifi_ipackets
;
814 stats
->bytes_in
= interface
->if_data
.ifi_ibytes
;
815 stats
->multicasts_in
= interface
->if_data
.ifi_imcasts
;
816 stats
->errors_in
= interface
->if_data
.ifi_ierrors
;
818 stats
->packets_out
= interface
->if_data
.ifi_opackets
;
819 stats
->bytes_out
= interface
->if_data
.ifi_obytes
;
820 stats
->multicasts_out
= interface
->if_data
.ifi_omcasts
;
821 stats
->errors_out
= interface
->if_data
.ifi_oerrors
;
823 stats
->collisions
= interface
->if_data
.ifi_collisions
;
824 stats
->dropped
= interface
->if_data
.ifi_iqdrops
;
825 stats
->no_protocol
= interface
->if_data
.ifi_noproto
;
827 lck_mtx_unlock(thread
->input_lck
);
833 ifnet_touch_lastchange(
836 struct dlil_threading_info
*thread
;
838 if (interface
== NULL
) return EINVAL
;
840 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
841 thread
= dlil_lo_thread_ptr
;
843 lck_mtx_lock(thread
->input_lck
);
845 TOUCHLASTCHANGE(&interface
->if_lastchange
);
847 lck_mtx_unlock(thread
->input_lck
);
855 struct timeval
*last_change
)
857 struct dlil_threading_info
*thread
;
859 if (interface
== NULL
) return EINVAL
;
861 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
862 thread
= dlil_lo_thread_ptr
;
864 lck_mtx_lock(thread
->input_lck
);
866 *last_change
= interface
->if_data
.ifi_lastchange
;
868 lck_mtx_unlock(thread
->input_lck
);
870 #if IF_LASTCHANGEUPTIME
871 /* Crude conversion from uptime to calendar time */
872 last_change
->tv_sec
+= boottime_sec();
879 ifnet_get_address_list(
881 ifaddr_t
**addresses
)
883 if (addresses
== NULL
) return EINVAL
;
884 return ifnet_get_address_list_family(interface
, addresses
, 0);
888 ifnet_get_address_list_family(
890 ifaddr_t
**addresses
,
897 if (addresses
== NULL
) return EINVAL
;
900 ifnet_head_lock_shared();
901 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
903 if (interface
&& ifp
!= interface
) continue;
905 ifnet_lock_shared(ifp
);
906 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
907 if (interface
== NULL
|| interface
== ifp
)
910 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
912 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
)
917 else if (interface
!= NULL
) {
918 ifnet_lock_done(ifp
);
922 ifnet_lock_done(ifp
);
925 MALLOC(*addresses
, ifaddr_t
*, sizeof(ifaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
926 if (*addresses
== NULL
) {
931 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
933 if (interface
&& ifp
!= interface
) continue;
935 ifnet_lock_shared(ifp
);
936 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
937 if (interface
== NULL
|| (struct ifnet
*)interface
== ifp
)
940 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
942 if (count
+ 1 > cmax
) break;
943 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
) {
944 (*addresses
)[count
] = (ifaddr_t
)addr
;
945 ifaddr_reference((*addresses
)[count
]);
951 ifnet_lock_done(ifp
);
952 if (interface
|| count
== cmax
)
956 (*addresses
)[cmax
] = 0;
962 ifnet_free_address_list(
967 if (addresses
== NULL
) return;
969 for (i
= 0; addresses
[i
] != NULL
; i
++)
971 ifaddr_release(addresses
[i
]);
974 FREE(addresses
, M_TEMP
);
981 if (interface
== NULL
) return NULL
;
982 return LLADDR(SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
));
986 ifnet_llbroadcast_copy_bytes(
992 if (interface
== NULL
|| addr
== NULL
|| out_len
== NULL
) return EINVAL
;
994 *out_len
= interface
->if_broadcast
.length
;
996 if (buffer_len
< interface
->if_broadcast
.length
) {
1000 if (interface
->if_broadcast
.length
== 0)
1003 if (interface
->if_broadcast
.length
<= sizeof(interface
->if_broadcast
.u
.buffer
)) {
1004 bcopy(interface
->if_broadcast
.u
.buffer
, addr
, interface
->if_broadcast
.length
);
1007 bcopy(interface
->if_broadcast
.u
.ptr
, addr
, interface
->if_broadcast
.length
);
1014 ifnet_lladdr_copy_bytes(
1019 struct sockaddr_dl
*sdl
;
1020 if (interface
== NULL
|| lladdr
== NULL
) return EINVAL
;
1022 sdl
= SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
);
1025 if (lladdr_len
!= sdl
->sdl_alen
) {
1026 bzero(lladdr
, lladdr_len
);
1029 bcopy(LLADDR(sdl
), lladdr
, lladdr_len
);
1030 if (bcmp(lladdr
, LLADDR(sdl
), lladdr_len
) == 0 &&
1031 lladdr_len
== sdl
->sdl_alen
)
1038 ifnet_set_lladdr_internal(
1046 struct sockaddr_dl
*sdl
;
1049 if (interface
== NULL
) return EINVAL
;
1051 if (lladdr_len
!= 0 && (lladdr_len
!= interface
->if_addrlen
|| lladdr
== 0))
1054 ifnet_head_lock_shared();
1055 ifa
= ifnet_addrs
[interface
->if_index
- 1];
1057 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1058 if (lladdr_len
!= 0) {
1059 bcopy(lladdr
, LLADDR(sdl
), lladdr_len
);
1062 bzero(LLADDR(sdl
), interface
->if_addrlen
);
1064 sdl
->sdl_alen
= lladdr_len
;
1067 sdl
->sdl_type
= new_type
;
1075 /* Generate a kernel event */
1077 dlil_post_msg(interface
, KEV_DL_SUBCLASS
,
1078 KEV_DL_LINK_ADDRESS_CHANGED
, NULL
, 0);
1090 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, 0, 0);
1094 ifnet_set_lladdr_and_type(
1100 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, type
, 1);
1104 ifnet_add_multicast(
1106 const struct sockaddr
*maddr
,
1107 ifmultiaddr_t
*address
)
1109 if (interface
== NULL
|| maddr
== NULL
) return EINVAL
;
1110 return if_addmulti(interface
, maddr
, address
);
1114 ifnet_remove_multicast(
1115 ifmultiaddr_t address
)
1117 if (address
== NULL
) return EINVAL
;
1118 return if_delmultiaddr(address
, 0);
1121 errno_t
ifnet_get_multicast_list(ifnet_t interface
, ifmultiaddr_t
**addresses
)
1125 struct ifmultiaddr
*addr
;
1128 if (interface
== NULL
|| addresses
== NULL
)
1131 lock
= (interface
->if_lock
!= 0);
1132 if (lock
) ifnet_lock_shared(interface
);
1133 if ((interface
->if_eflags
& IFEF_DETACHING
) == 0) {
1134 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1140 if (lock
) ifnet_lock_done(interface
);
1144 MALLOC(*addresses
, ifmultiaddr_t
*, sizeof(ifmultiaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
1145 if (*addresses
== NULL
) {
1146 if (lock
) ifnet_lock_done(interface
);
1150 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1152 if (count
+ 1 > cmax
)
1154 (*addresses
)[count
] = (ifmultiaddr_t
)addr
;
1155 ifmaddr_reference((*addresses
)[count
]);
1158 (*addresses
)[cmax
] = 0;
1159 if (lock
) ifnet_lock_done(interface
);
1165 ifnet_free_multicast_list(
1166 ifmultiaddr_t
*addresses
)
1170 if (addresses
== NULL
) return;
1172 for (i
= 0; addresses
[i
] != NULL
; i
++)
1174 ifmaddr_release(addresses
[i
]);
1177 FREE(addresses
, M_TEMP
);
1188 if (ifname
== NULL
) return EINVAL
;
1190 namelen
= strlen(ifname
);
1194 ifnet_head_lock_shared();
1195 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1197 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
1198 struct sockaddr_dl
*ll_addr
;
1200 if (!ifa
|| !ifa
->ifa_addr
)
1203 ll_addr
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1205 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0 &&
1206 namelen
== ll_addr
->sdl_nlen
&&
1207 (strncmp(ll_addr
->sdl_data
, ifname
, ll_addr
->sdl_nlen
) == 0))
1214 ifnet_reference(*interface
);
1218 return (ifp
== NULL
) ? ENXIO
: 0;
1222 ifnet_list_get(ifnet_family_t family
, ifnet_t
**list
, u_int32_t
*count
)
1224 return (ifnet_list_get_common(family
, FALSE
, list
, count
));
1227 __private_extern__ errno_t
1228 ifnet_list_get_all(ifnet_family_t family
, ifnet_t
**list
, u_int32_t
*count
)
1230 return (ifnet_list_get_common(family
, TRUE
, list
, count
));
1234 ifnet_list_get_common(ifnet_family_t family
, boolean_t get_all
, ifnet_t
**list
,
1242 if (list
== NULL
|| count
== NULL
)
1245 ifnet_head_lock_shared();
1246 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1247 if ((ifp
->if_eflags
& IFEF_DETACHING
) && !get_all
)
1249 if (family
== IFNET_FAMILY_ANY
|| ifp
->if_family
== family
)
1257 MALLOC(*list
, ifnet_t
*, sizeof(ifnet_t
) * (cmax
+ 1),
1264 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1265 if ((ifp
->if_eflags
& IFEF_DETACHING
) && !get_all
)
1267 if (*count
+ 1 > cmax
)
1269 if (family
== IFNET_FAMILY_ANY
||
1270 ((ifnet_family_t
)ifp
->if_family
) == family
) {
1271 (*list
)[*count
] = (ifnet_t
)ifp
;
1272 ifnet_reference((*list
)[*count
]);
1276 (*list
)[*count
] = NULL
;
1284 ifnet_list_free(ifnet_t
*interfaces
)
1288 if (interfaces
== NULL
)
1291 for (i
= 0; interfaces
[i
]; i
++) {
1292 ifnet_release(interfaces
[i
]);
1295 FREE(interfaces
, M_TEMP
);
1298 /****************************************************************************/
1299 /* ifaddr_t accessors */
1300 /****************************************************************************/
1306 if (ifa
== NULL
) return EINVAL
;
1315 if (ifa
== NULL
) return EINVAL
;
1321 ifaddr_address_family(
1324 if (ifa
&& ifa
->ifa_addr
)
1325 return ifa
->ifa_addr
->sa_family
;
1333 struct sockaddr
*out_addr
,
1334 u_int32_t addr_size
)
1338 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1339 if (ifa
->ifa_addr
== NULL
) return ENOTSUP
;
1341 copylen
= (addr_size
>= ifa
->ifa_addr
->sa_len
) ? ifa
->ifa_addr
->sa_len
: addr_size
;
1342 bcopy(ifa
->ifa_addr
, out_addr
, copylen
);
1344 if (ifa
->ifa_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1352 struct sockaddr
*out_addr
,
1353 u_int32_t addr_size
)
1356 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1357 if (ifa
->ifa_dstaddr
== NULL
) return ENOTSUP
;
1359 copylen
= (addr_size
>= ifa
->ifa_dstaddr
->sa_len
) ? ifa
->ifa_dstaddr
->sa_len
: addr_size
;
1360 bcopy(ifa
->ifa_dstaddr
, out_addr
, copylen
);
1362 if (ifa
->ifa_dstaddr
->sa_len
> addr_size
) return EMSGSIZE
;
1370 struct sockaddr
*out_addr
,
1371 u_int32_t addr_size
)
1374 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1375 if (ifa
->ifa_netmask
== NULL
) return ENOTSUP
;
1377 copylen
= addr_size
>= ifa
->ifa_netmask
->sa_len
? ifa
->ifa_netmask
->sa_len
: addr_size
;
1378 bcopy(ifa
->ifa_netmask
, out_addr
, copylen
);
1380 if (ifa
->ifa_netmask
->sa_len
> addr_size
) return EMSGSIZE
;
1390 if (ifa
== NULL
) return NULL
;
1393 return (ifnet_t
)ifp
;
1398 const struct sockaddr
* address
)
1400 if (address
== NULL
) return NULL
;
1401 return ifa_ifwithaddr(address
);
1406 const struct sockaddr
* address
)
1408 if (address
== NULL
) return NULL
;
1409 return ifa_ifwithdstaddr(address
);
1414 const struct sockaddr
* net
)
1416 if (net
== NULL
) return NULL
;
1417 return ifa_ifwithnet(net
);
1423 const struct sockaddr
* destination
,
1424 const struct sockaddr
* gateway
)
1426 if (destination
== NULL
|| gateway
== NULL
) return NULL
;
1427 return ifa_ifwithroute(flags
, destination
, gateway
);
1431 ifaddr_findbestforaddr(
1432 const struct sockaddr
*addr
,
1435 if (addr
== NULL
|| interface
== NULL
) return NULL
;
1436 return ifaof_ifpforaddr(addr
, interface
);
1441 ifmultiaddr_t ifmaddr
)
1443 if (ifmaddr
== NULL
) return EINVAL
;
1444 ifma_reference(ifmaddr
);
1450 ifmultiaddr_t ifmaddr
)
1452 if (ifmaddr
== NULL
) return EINVAL
;
1453 ifma_release(ifmaddr
);
1459 ifmultiaddr_t ifmaddr
,
1460 struct sockaddr
*out_addr
,
1461 u_int32_t addr_size
)
1465 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1466 if (ifmaddr
->ifma_addr
== NULL
) return ENOTSUP
;
1468 copylen
= addr_size
>= ifmaddr
->ifma_addr
->sa_len
? ifmaddr
->ifma_addr
->sa_len
: addr_size
;
1469 bcopy(ifmaddr
->ifma_addr
, out_addr
, copylen
);
1471 if (ifmaddr
->ifma_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1478 ifmultiaddr_t ifmaddr
,
1479 struct sockaddr
*out_addr
,
1480 u_int32_t addr_size
)
1482 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1483 if (ifmaddr
->ifma_ll
== NULL
) return ENOTSUP
;
1485 return ifmaddr_address(ifmaddr
->ifma_ll
, out_addr
, addr_size
);
1490 ifmultiaddr_t ifmaddr
)
1492 if (ifmaddr
== NULL
|| ifmaddr
->ifma_ifp
== NULL
) return NULL
;
1493 return ifmaddr
->ifma_ifp
;