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 #if IF_LASTCHANGEUPTIME
51 #define TOUCHLASTCHANGE(__if_lastchange) microuptime(__if_lastchange)
53 #define TOUCHLASTCHANGE(__if_lastchange) microtime(__if_lastchange)
56 extern struct dlil_threading_info
*dlil_lo_thread_ptr
;
57 extern int dlil_multithreaded_input
;
60 Temporary work around until we have real reference counting
62 We keep the bits about calling dlil_if_release (which should be
63 called recycle) transparent by calling it from our if_free function
64 pointer. We have to keep the client's original detach function
65 somewhere so we can call it.
71 ifnet_detached_func detach_func
= ifp
->if_kpi_storage
;
76 if (ifp
->if_broadcast
.length
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
77 FREE(ifp
->if_broadcast
.u
.ptr
, M_IFADDR
);
78 ifp
->if_broadcast
.u
.ptr
= NULL
;
84 static __inline__
void*
85 _cast_non_const(const void * ptr
) {
97 const struct ifnet_init_params
*init
,
101 struct ifnet
*ifp
= NULL
;
103 if (init
->family
== 0)
105 if (init
->name
== NULL
||
106 init
->output
== NULL
)
108 if (strlen(init
->name
) >= IFNAMSIZ
)
110 if ((init
->type
& 0xFFFFFF00) != 0 || init
->type
== 0)
113 error
= dlil_if_acquire(init
->family
, init
->uniqueid
, init
->uniqueid_len
, &ifp
);
117 * Cast ifp->if_name as non const. dlil_if_acquire sets it up
118 * to point to storage of at least IFNAMSIZ bytes. It is safe
121 strncpy(_cast_non_const(ifp
->if_name
), init
->name
, IFNAMSIZ
);
122 ifp
->if_type
= init
->type
;
123 ifp
->if_family
= init
->family
;
124 ifp
->if_unit
= init
->unit
;
125 ifp
->if_output
= init
->output
;
126 ifp
->if_demux
= init
->demux
;
127 ifp
->if_add_proto
= init
->add_proto
;
128 ifp
->if_del_proto
= init
->del_proto
;
129 ifp
->if_check_multi
= init
->check_multi
;
130 ifp
->if_framer
= init
->framer
;
131 ifp
->if_softc
= init
->softc
;
132 ifp
->if_ioctl
= init
->ioctl
;
133 ifp
->if_set_bpf_tap
= init
->set_bpf_tap
;
134 ifp
->if_free
= ifnet_kpi_free
;
135 ifp
->if_event
= init
->event
;
136 ifp
->if_kpi_storage
= init
->detach
;
137 ifp
->if_eflags
|= IFEF_USEKPI
;
139 if (init
->broadcast_len
&& init
->broadcast_addr
) {
140 if (init
->broadcast_len
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
141 MALLOC(ifp
->if_broadcast
.u
.ptr
, u_char
*, init
->broadcast_len
, M_IFADDR
, M_NOWAIT
);
142 if (ifp
->if_broadcast
.u
.ptr
== NULL
) {
146 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.ptr
, init
->broadcast_len
);
150 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.buffer
, init
->broadcast_len
);
152 ifp
->if_broadcast
.length
= init
->broadcast_len
;
155 bzero(&ifp
->if_broadcast
, sizeof(ifp
->if_broadcast
));
160 ifnet_reference(ifp
); // temporary - this should be done in dlil_if_acquire
163 dlil_if_release(ifp
);
169 Note: We should do something here to indicate that we haven't been
170 attached yet. By doing so, we can catch the case in ifnet_release
171 where the reference count reaches zero and call the recycle
172 function. If the interface is attached, the interface will be
173 recycled when the interface's if_free function is called. If the
174 interface is never attached, the if_free function will never be
175 called and the interface will never be recycled.
187 if (ifp
== NULL
) return EINVAL
;
189 oldval
= OSIncrementAtomic((SInt32
*)&ifp
->if_refcnt
);
200 if (ifp
== NULL
) return EINVAL
;
202 oldval
= OSDecrementAtomic((SInt32
*)&ifp
->if_refcnt
);
204 panic("ifnet_release - refcount decremented past zero!");
213 return interface
== NULL
? NULL
: interface
->if_softc
;
220 return interface
== NULL
? NULL
: interface
->if_name
;
227 return interface
== NULL
? 0 : interface
->if_family
;
234 return interface
== NULL
? (u_int32_t
)0xffffffff : (u_int32_t
)interface
->if_unit
;
241 return interface
== NULL
? (u_int32_t
)0xffffffff : interface
->if_index
;
252 if (interface
== NULL
) return EINVAL
;
253 lock
= (interface
->if_lock
!= 0);
255 if (lock
) ifnet_lock_exclusive(interface
);
257 /* If we are modifying the up/down state, call if_updown */
258 if (lock
&& (mask
& IFF_UP
) != 0) {
259 if_updown(interface
, (new_flags
& IFF_UP
) == IFF_UP
);
262 interface
->if_flags
= (new_flags
& mask
) | (interface
->if_flags
& ~mask
);
263 if (lock
) ifnet_lock_done(interface
);
272 return interface
== NULL
? 0 : interface
->if_flags
;
283 if (interface
== NULL
) return EINVAL
;
284 lock
= (interface
->if_lock
!= 0);
286 if (lock
) ifnet_lock_exclusive(interface
);
287 interface
->if_eflags
= (new_flags
& mask
) | (interface
->if_eflags
& ~mask
);
288 if (lock
) ifnet_lock_done(interface
);
297 return interface
== NULL
? 0 : interface
->if_eflags
;
300 static const ifnet_offload_t offload_mask
= IFNET_CSUM_IP
| IFNET_CSUM_TCP
|
301 IFNET_CSUM_UDP
| IFNET_CSUM_FRAGMENT
| IFNET_IP_FRAGMENT
|
302 IFNET_CSUM_SUM16
| IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
|
308 ifnet_offload_t offload
)
312 if (interface
== NULL
) return EINVAL
;
313 lock
= (interface
->if_lock
!= 0);
315 if (lock
) ifnet_lock_exclusive(interface
);
316 interface
->if_hwassist
= (offload
& offload_mask
);
317 if (lock
) ifnet_lock_done(interface
);
326 return interface
== NULL
? 0 : (interface
->if_hwassist
& offload_mask
);
330 * Should MIB data store a copy?
333 ifnet_set_link_mib_data(
340 if (interface
== NULL
) return EINVAL
;
341 lock
= (interface
->if_lock
!= 0);
343 if (lock
) ifnet_lock_exclusive(interface
);
344 interface
->if_linkmib
= (void*)mibData
;
345 interface
->if_linkmiblen
= mibLen
;
346 if (lock
) ifnet_lock_done(interface
);
351 ifnet_get_link_mib_data(
359 if (interface
== NULL
) return EINVAL
;
360 lock
= (interface
->if_lock
!= NULL
);
362 if (lock
) ifnet_lock_shared(interface
);
363 if (*mibLen
< interface
->if_linkmiblen
)
365 if (result
== 0 && interface
->if_linkmib
== NULL
)
369 *mibLen
= interface
->if_linkmiblen
;
370 bcopy(interface
->if_linkmib
, mibData
, *mibLen
);
372 if (lock
) ifnet_lock_done(interface
);
378 ifnet_get_link_mib_data_length(
381 return interface
== NULL
? 0 : interface
->if_linkmiblen
;
387 protocol_family_t protocol_family
,
390 const struct sockaddr
*dest
)
392 if (interface
== NULL
|| protocol_family
== 0 || m
== NULL
) {
397 return dlil_output(interface
, protocol_family
, m
, route
, dest
, 0);
403 protocol_family_t protocol_family
,
406 if (interface
== NULL
|| m
== NULL
) {
411 return dlil_output(interface
, protocol_family
, m
, NULL
, NULL
, 1);
419 if (interface
== NULL
) return EINVAL
;
420 interface
->if_data
.ifi_mtu
= mtu
;
429 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_mtu
;
439 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_type
;
449 int lock
= (interface
->if_lock
!= 0);
450 if (lock
) ifnet_lock_exclusive(interface
);
451 interface
->if_data
.ifi_typelen
= typelen
;
452 if (lock
) ifnet_lock_done(interface
);
461 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_typelen
;
471 if (interface
== NULL
) return EINVAL
;
472 interface
->if_data
.ifi_addrlen
= addrlen
;
481 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_addrlen
;
490 if (interface
== NULL
) return EINVAL
;
491 interface
->if_data
.ifi_hdrlen
= hdrlen
;
500 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_hdrlen
;
509 if (interface
== NULL
) return EINVAL
;
510 interface
->if_data
.ifi_metric
= metric
;
519 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_metric
;
528 if (interface
== NULL
) return EINVAL
;
529 /* Pin baudrate to 32 bits until we can change the storage size */
530 interface
->if_data
.ifi_baudrate
= baudrate
> 0xFFFFFFFF ? 0xFFFFFFFF : baudrate
;
539 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_baudrate
;
544 ifnet_stat_increment(
546 const struct ifnet_stat_increment_param
*counts
)
548 struct dlil_threading_info
*thread
;
549 if (interface
== NULL
) return EINVAL
;
551 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
552 thread
= dlil_lo_thread_ptr
;
554 lck_mtx_lock(thread
->input_lck
);
556 interface
->if_data
.ifi_ipackets
+= counts
->packets_in
;
557 interface
->if_data
.ifi_ibytes
+= counts
->bytes_in
;
558 interface
->if_data
.ifi_ierrors
+= counts
->errors_in
;
560 interface
->if_data
.ifi_opackets
+= counts
->packets_out
;
561 interface
->if_data
.ifi_obytes
+= counts
->bytes_out
;
562 interface
->if_data
.ifi_oerrors
+= counts
->errors_out
;
564 interface
->if_data
.ifi_collisions
+= counts
->collisions
;
565 interface
->if_data
.ifi_iqdrops
+= counts
->dropped
;
567 /* Touch the last change time. */
568 TOUCHLASTCHANGE(&interface
->if_lastchange
);
570 lck_mtx_unlock(thread
->input_lck
);
576 ifnet_stat_increment_in(
578 u_int32_t packets_in
,
582 struct dlil_threading_info
*thread
;
584 if (interface
== NULL
) return EINVAL
;
586 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
587 thread
= dlil_lo_thread_ptr
;
589 lck_mtx_lock(thread
->input_lck
);
591 interface
->if_data
.ifi_ipackets
+= packets_in
;
592 interface
->if_data
.ifi_ibytes
+= bytes_in
;
593 interface
->if_data
.ifi_ierrors
+= errors_in
;
595 TOUCHLASTCHANGE(&interface
->if_lastchange
);
597 lck_mtx_unlock(thread
->input_lck
);
603 ifnet_stat_increment_out(
605 u_int32_t packets_out
,
607 u_int32_t errors_out
)
609 struct dlil_threading_info
*thread
;
610 if (interface
== NULL
) return EINVAL
;
612 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
613 thread
= dlil_lo_thread_ptr
;
615 lck_mtx_lock(thread
->input_lck
);
617 interface
->if_data
.ifi_opackets
+= packets_out
;
618 interface
->if_data
.ifi_obytes
+= bytes_out
;
619 interface
->if_data
.ifi_oerrors
+= errors_out
;
621 TOUCHLASTCHANGE(&interface
->if_lastchange
);
623 lck_mtx_unlock(thread
->input_lck
);
631 const struct ifnet_stats_param
*stats
)
633 struct dlil_threading_info
*thread
;
635 if (interface
== NULL
) return EINVAL
;
637 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
638 thread
= dlil_lo_thread_ptr
;
640 lck_mtx_lock(thread
->input_lck
);
642 interface
->if_data
.ifi_ipackets
= stats
->packets_in
;
643 interface
->if_data
.ifi_ibytes
= stats
->bytes_in
;
644 interface
->if_data
.ifi_imcasts
= stats
->multicasts_in
;
645 interface
->if_data
.ifi_ierrors
= stats
->errors_in
;
647 interface
->if_data
.ifi_opackets
= stats
->packets_out
;
648 interface
->if_data
.ifi_obytes
= stats
->bytes_out
;
649 interface
->if_data
.ifi_omcasts
= stats
->multicasts_out
;
650 interface
->if_data
.ifi_oerrors
= stats
->errors_out
;
652 interface
->if_data
.ifi_collisions
= stats
->collisions
;
653 interface
->if_data
.ifi_iqdrops
= stats
->dropped
;
654 interface
->if_data
.ifi_noproto
= stats
->no_protocol
;
656 /* Touch the last change time. */
657 TOUCHLASTCHANGE(&interface
->if_lastchange
);
659 lck_mtx_unlock(thread
->input_lck
);
667 struct ifnet_stats_param
*stats
)
669 struct dlil_threading_info
*thread
;
671 if (interface
== NULL
) return EINVAL
;
673 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
674 thread
= dlil_lo_thread_ptr
;
676 lck_mtx_lock(thread
->input_lck
);
678 stats
->packets_in
= interface
->if_data
.ifi_ipackets
;
679 stats
->bytes_in
= interface
->if_data
.ifi_ibytes
;
680 stats
->multicasts_in
= interface
->if_data
.ifi_imcasts
;
681 stats
->errors_in
= interface
->if_data
.ifi_ierrors
;
683 stats
->packets_out
= interface
->if_data
.ifi_opackets
;
684 stats
->bytes_out
= interface
->if_data
.ifi_obytes
;
685 stats
->multicasts_out
= interface
->if_data
.ifi_omcasts
;
686 stats
->errors_out
= interface
->if_data
.ifi_oerrors
;
688 stats
->collisions
= interface
->if_data
.ifi_collisions
;
689 stats
->dropped
= interface
->if_data
.ifi_iqdrops
;
690 stats
->no_protocol
= interface
->if_data
.ifi_noproto
;
692 lck_mtx_unlock(thread
->input_lck
);
698 ifnet_touch_lastchange(
701 struct dlil_threading_info
*thread
;
703 if (interface
== NULL
) return EINVAL
;
705 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
706 thread
= dlil_lo_thread_ptr
;
708 lck_mtx_lock(thread
->input_lck
);
710 TOUCHLASTCHANGE(&interface
->if_lastchange
);
712 lck_mtx_unlock(thread
->input_lck
);
720 struct timeval
*last_change
)
722 struct dlil_threading_info
*thread
;
724 if (interface
== NULL
) return EINVAL
;
726 if ((thread
= interface
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
727 thread
= dlil_lo_thread_ptr
;
729 lck_mtx_lock(thread
->input_lck
);
731 *last_change
= interface
->if_data
.ifi_lastchange
;
733 lck_mtx_unlock(thread
->input_lck
);
735 #if IF_LASTCHANGEUPTIME
736 /* Crude conversion from uptime to calendar time */
737 last_change
->tv_sec
+= boottime_sec();
744 ifnet_get_address_list(
746 ifaddr_t
**addresses
)
748 if (addresses
== NULL
) return EINVAL
;
749 return ifnet_get_address_list_family(interface
, addresses
, 0);
753 ifnet_get_address_list_family(
755 ifaddr_t
**addresses
,
762 if (addresses
== NULL
) return EINVAL
;
765 ifnet_head_lock_shared();
766 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
768 if (interface
&& ifp
!= interface
) continue;
770 ifnet_lock_shared(ifp
);
771 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
772 if (interface
== NULL
|| interface
== ifp
)
775 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
777 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
)
782 else if (interface
!= NULL
) {
783 ifnet_lock_done(ifp
);
787 ifnet_lock_done(ifp
);
790 MALLOC(*addresses
, ifaddr_t
*, sizeof(ifaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
791 if (*addresses
== NULL
) {
796 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
798 if (interface
&& ifp
!= interface
) continue;
800 ifnet_lock_shared(ifp
);
801 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
802 if (interface
== NULL
|| (struct ifnet
*)interface
== ifp
)
805 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
807 if (count
+ 1 > cmax
) break;
808 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
) {
809 (*addresses
)[count
] = (ifaddr_t
)addr
;
810 ifaddr_reference((*addresses
)[count
]);
816 ifnet_lock_done(ifp
);
817 if (interface
|| count
== cmax
)
821 (*addresses
)[cmax
] = 0;
827 ifnet_free_address_list(
832 if (addresses
== NULL
) return;
834 for (i
= 0; addresses
[i
] != NULL
; i
++)
836 ifaddr_release(addresses
[i
]);
839 FREE(addresses
, M_TEMP
);
846 if (interface
== NULL
) return NULL
;
847 return LLADDR(SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
));
851 ifnet_llbroadcast_copy_bytes(
857 if (interface
== NULL
|| addr
== NULL
|| out_len
== NULL
) return EINVAL
;
859 *out_len
= interface
->if_broadcast
.length
;
861 if (buffer_len
< interface
->if_broadcast
.length
) {
865 if (interface
->if_broadcast
.length
== 0)
868 if (interface
->if_broadcast
.length
<= sizeof(interface
->if_broadcast
.u
.buffer
)) {
869 bcopy(interface
->if_broadcast
.u
.buffer
, addr
, interface
->if_broadcast
.length
);
872 bcopy(interface
->if_broadcast
.u
.ptr
, addr
, interface
->if_broadcast
.length
);
879 ifnet_lladdr_copy_bytes(
884 struct sockaddr_dl
*sdl
;
885 if (interface
== NULL
|| lladdr
== NULL
) return EINVAL
;
887 sdl
= SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
);
890 if (lladdr_len
!= sdl
->sdl_alen
) {
891 bzero(lladdr
, lladdr_len
);
894 bcopy(LLADDR(sdl
), lladdr
, lladdr_len
);
895 if (bcmp(lladdr
, LLADDR(sdl
), lladdr_len
) == 0 &&
896 lladdr_len
== sdl
->sdl_alen
)
903 ifnet_set_lladdr_internal(
911 struct sockaddr_dl
*sdl
;
914 if (interface
== NULL
) return EINVAL
;
916 if (lladdr_len
!= 0 && (lladdr_len
!= interface
->if_addrlen
|| lladdr
== 0))
919 ifnet_head_lock_shared();
920 ifa
= ifnet_addrs
[interface
->if_index
- 1];
922 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
923 if (lladdr_len
!= 0) {
924 bcopy(lladdr
, LLADDR(sdl
), lladdr_len
);
927 bzero(LLADDR(sdl
), interface
->if_addrlen
);
929 sdl
->sdl_alen
= lladdr_len
;
932 sdl
->sdl_type
= new_type
;
940 /* Generate a kernel event */
942 dlil_post_msg(interface
, KEV_DL_SUBCLASS
,
943 KEV_DL_LINK_ADDRESS_CHANGED
, NULL
, 0);
955 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, 0, 0);
959 ifnet_set_lladdr_and_type(
965 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, type
, 1);
971 const struct sockaddr
*maddr
,
972 ifmultiaddr_t
*address
)
974 if (interface
== NULL
|| maddr
== NULL
) return EINVAL
;
975 return if_addmulti(interface
, maddr
, address
);
979 ifnet_remove_multicast(
980 ifmultiaddr_t address
)
982 if (address
== NULL
) return EINVAL
;
983 return if_delmultiaddr(address
, 0);
986 errno_t
ifnet_get_multicast_list(ifnet_t interface
, ifmultiaddr_t
**addresses
)
990 struct ifmultiaddr
*addr
;
993 if (interface
== NULL
|| addresses
== NULL
)
996 lock
= (interface
->if_lock
!= 0);
997 if (lock
) ifnet_lock_shared(interface
);
998 if ((interface
->if_eflags
& IFEF_DETACHING
) == 0) {
999 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1005 if (lock
) ifnet_lock_done(interface
);
1009 MALLOC(*addresses
, ifmultiaddr_t
*, sizeof(ifmultiaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
1010 if (*addresses
== NULL
) {
1011 if (lock
) ifnet_lock_done(interface
);
1015 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1017 if (count
+ 1 > cmax
)
1019 (*addresses
)[count
] = (ifmultiaddr_t
)addr
;
1020 ifmaddr_reference((*addresses
)[count
]);
1023 (*addresses
)[cmax
] = 0;
1024 if (lock
) ifnet_lock_done(interface
);
1030 ifnet_free_multicast_list(
1031 ifmultiaddr_t
*addresses
)
1035 if (addresses
== NULL
) return;
1037 for (i
= 0; addresses
[i
] != NULL
; i
++)
1039 ifmaddr_release(addresses
[i
]);
1042 FREE(addresses
, M_TEMP
);
1053 if (ifname
== NULL
) return EINVAL
;
1055 namelen
= strlen(ifname
);
1059 ifnet_head_lock_shared();
1060 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1062 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
1063 struct sockaddr_dl
*ll_addr
;
1065 if (!ifa
|| !ifa
->ifa_addr
)
1068 ll_addr
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1070 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0 &&
1071 namelen
== ll_addr
->sdl_nlen
&&
1072 (strncmp(ll_addr
->sdl_data
, ifname
, ll_addr
->sdl_nlen
) == 0))
1079 ifnet_reference(*interface
);
1083 return (ifp
== NULL
) ? ENXIO
: 0;
1088 ifnet_family_t family
,
1097 if (list
== NULL
|| count
== NULL
) return EINVAL
;
1099 ifnet_head_lock_shared();
1100 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1102 if (ifp
->if_eflags
& IFEF_DETACHING
) continue;
1103 if (family
== 0 || ifp
->if_family
== family
)
1111 MALLOC(*list
, ifnet_t
*, sizeof(ifnet_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
1117 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1119 if (ifp
->if_eflags
& IFEF_DETACHING
) continue;
1120 if (*count
+ 1 > cmax
) break;
1121 if (family
== 0 || ((ifnet_family_t
)ifp
->if_family
) == family
)
1123 (*list
)[*count
] = (ifnet_t
)ifp
;
1124 ifnet_reference((*list
)[*count
]);
1128 (*list
)[*count
] = NULL
;
1137 ifnet_t
*interfaces
)
1141 if (interfaces
== NULL
) return;
1143 for (i
= 0; interfaces
[i
]; i
++)
1145 ifnet_release(interfaces
[i
]);
1148 FREE(interfaces
, M_TEMP
);
1151 /****************************************************************************/
1152 /* ifaddr_t accessors */
1153 /****************************************************************************/
1159 if (ifa
== NULL
) return EINVAL
;
1168 if (ifa
== NULL
) return EINVAL
;
1174 ifaddr_address_family(
1177 if (ifa
&& ifa
->ifa_addr
)
1178 return ifa
->ifa_addr
->sa_family
;
1186 struct sockaddr
*out_addr
,
1187 u_int32_t addr_size
)
1191 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1192 if (ifa
->ifa_addr
== NULL
) return ENOTSUP
;
1194 copylen
= (addr_size
>= ifa
->ifa_addr
->sa_len
) ? ifa
->ifa_addr
->sa_len
: addr_size
;
1195 bcopy(ifa
->ifa_addr
, out_addr
, copylen
);
1197 if (ifa
->ifa_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1205 struct sockaddr
*out_addr
,
1206 u_int32_t addr_size
)
1209 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1210 if (ifa
->ifa_dstaddr
== NULL
) return ENOTSUP
;
1212 copylen
= (addr_size
>= ifa
->ifa_dstaddr
->sa_len
) ? ifa
->ifa_dstaddr
->sa_len
: addr_size
;
1213 bcopy(ifa
->ifa_dstaddr
, out_addr
, copylen
);
1215 if (ifa
->ifa_dstaddr
->sa_len
> addr_size
) return EMSGSIZE
;
1223 struct sockaddr
*out_addr
,
1224 u_int32_t addr_size
)
1227 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1228 if (ifa
->ifa_netmask
== NULL
) return ENOTSUP
;
1230 copylen
= addr_size
>= ifa
->ifa_netmask
->sa_len
? ifa
->ifa_netmask
->sa_len
: addr_size
;
1231 bcopy(ifa
->ifa_netmask
, out_addr
, copylen
);
1233 if (ifa
->ifa_netmask
->sa_len
> addr_size
) return EMSGSIZE
;
1243 if (ifa
== NULL
) return NULL
;
1246 return (ifnet_t
)ifp
;
1251 const struct sockaddr
* address
)
1253 if (address
== NULL
) return NULL
;
1254 return ifa_ifwithaddr(address
);
1259 const struct sockaddr
* address
)
1261 if (address
== NULL
) return NULL
;
1262 return ifa_ifwithdstaddr(address
);
1267 const struct sockaddr
* net
)
1269 if (net
== NULL
) return NULL
;
1270 return ifa_ifwithnet(net
);
1276 const struct sockaddr
* destination
,
1277 const struct sockaddr
* gateway
)
1279 if (destination
== NULL
|| gateway
== NULL
) return NULL
;
1280 return ifa_ifwithroute(flags
, destination
, gateway
);
1284 ifaddr_findbestforaddr(
1285 const struct sockaddr
*addr
,
1288 if (addr
== NULL
|| interface
== NULL
) return NULL
;
1289 return ifaof_ifpforaddr(addr
, interface
);
1294 ifmultiaddr_t ifmaddr
)
1296 if (ifmaddr
== NULL
) return EINVAL
;
1297 ifma_reference(ifmaddr
);
1303 ifmultiaddr_t ifmaddr
)
1305 if (ifmaddr
== NULL
) return EINVAL
;
1306 ifma_release(ifmaddr
);
1312 ifmultiaddr_t ifmaddr
,
1313 struct sockaddr
*out_addr
,
1314 u_int32_t addr_size
)
1318 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1319 if (ifmaddr
->ifma_addr
== NULL
) return ENOTSUP
;
1321 copylen
= addr_size
>= ifmaddr
->ifma_addr
->sa_len
? ifmaddr
->ifma_addr
->sa_len
: addr_size
;
1322 bcopy(ifmaddr
->ifma_addr
, out_addr
, copylen
);
1324 if (ifmaddr
->ifma_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1331 ifmultiaddr_t ifmaddr
,
1332 struct sockaddr
*out_addr
,
1333 u_int32_t addr_size
)
1335 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1336 if (ifmaddr
->ifma_ll
== NULL
) return ENOTSUP
;
1338 return ifmaddr_address(ifmaddr
->ifma_ll
, out_addr
, addr_size
);
1343 ifmultiaddr_t ifmaddr
)
1345 if (ifmaddr
== NULL
|| ifmaddr
->ifma_ifp
== NULL
) return NULL
;
1346 return ifmaddr
->ifma_ifp
;