2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 #include "kpi_interface.h"
25 #include <sys/queue.h>
26 #include <sys/param.h> /* for definition of NULL */
27 #include <sys/errno.h>
28 #include <sys/socket.h>
29 #include <sys/kern_event.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/kpi_mbuf.h>
33 #include <net/if_var.h>
34 #include <net/if_dl.h>
36 #include <net/if_types.h>
37 #include <net/if_dl.h>
38 #include <net/if_arp.h>
39 #include <libkern/libkern.h>
40 #include <kern/locks.h>
42 #if IF_LASTCHANGEUPTIME
43 #define TOUCHLASTCHANGE(__if_lastchange) microuptime(__if_lastchange)
45 #define TOUCHLASTCHANGE(__if_lastchange) microtime(__if_lastchange)
48 extern lck_spin_t
*dlil_input_lock
;
51 Temporary work around until we have real reference counting
53 We keep the bits about calling dlil_if_release (which should be
54 called recycle) transparent by calling it from our if_free function
55 pointer. We have to keep the client's original detach function
56 somewhere so we can call it.
62 ifnet_detached_func detach_func
= ifp
->if_kpi_storage
;
67 if (ifp
->if_broadcast
.length
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
68 FREE(ifp
->if_broadcast
.u
.ptr
, M_IFADDR
);
69 ifp
->if_broadcast
.u
.ptr
= NULL
;
77 const struct ifnet_init_params
*init
,
81 struct ifnet
*ifp
= NULL
;
83 if (init
->family
== 0)
85 if (init
->name
== NULL
||
88 if (strlen(init
->name
) >= IFNAMSIZ
)
90 if ((init
->type
& 0xFFFFFF00) != 0 || init
->type
== 0)
93 error
= dlil_if_acquire(init
->family
, init
->uniqueid
, init
->uniqueid_len
, &ifp
);
96 strncpy(ifp
->if_name
, init
->name
, IFNAMSIZ
);
97 ifp
->if_type
= init
->type
;
98 ifp
->if_family
= init
->family
;
99 ifp
->if_unit
= init
->unit
;
100 ifp
->if_output
= init
->output
;
101 ifp
->if_demux
= init
->demux
;
102 ifp
->if_add_proto_u
.kpi
= init
->add_proto
;
103 ifp
->if_del_proto
= init
->del_proto
;
104 ifp
->if_check_multi
= init
->check_multi
;
105 ifp
->if_framer
= init
->framer
;
106 ifp
->if_softc
= init
->softc
;
107 ifp
->if_ioctl
= init
->ioctl
;
108 ifp
->if_set_bpf_tap
= init
->set_bpf_tap
;
109 ifp
->if_free
= ifnet_kpi_free
;
110 ifp
->if_event
= init
->event
;
111 ifp
->if_kpi_storage
= init
->detach
;
112 ifp
->if_eflags
|= IFEF_USEKPI
;
114 if (init
->broadcast_len
&& init
->broadcast_addr
) {
115 if (init
->broadcast_len
> sizeof(ifp
->if_broadcast
.u
.buffer
)) {
116 MALLOC(ifp
->if_broadcast
.u
.ptr
, u_char
*, init
->broadcast_len
, M_IFADDR
, M_NOWAIT
);
117 if (ifp
->if_broadcast
.u
.ptr
== NULL
) {
121 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.ptr
, init
->broadcast_len
);
125 bcopy(init
->broadcast_addr
, ifp
->if_broadcast
.u
.buffer
, init
->broadcast_len
);
127 ifp
->if_broadcast
.length
= init
->broadcast_len
;
130 bzero(&ifp
->if_broadcast
, sizeof(ifp
->if_broadcast
));
135 ifnet_reference(ifp
); // temporary - this should be done in dlil_if_acquire
138 dlil_if_release(ifp
);
144 Note: We should do something here to indicate that we haven't been
145 attached yet. By doing so, we can catch the case in ifnet_release
146 where the reference count reaches zero and call the recycle
147 function. If the interface is attached, the interface will be
148 recycled when the interface's if_free function is called. If the
149 interface is never attached, the if_free function will never be
150 called and the interface will never be recycled.
160 if (interface
== NULL
) return EINVAL
;
161 ifp_reference(interface
);
169 if (interface
== NULL
) return EINVAL
;
170 ifp_release(interface
);
177 const struct sockaddr_dl
*ll_addr
)
179 if (interface
== NULL
) return EINVAL
;
180 if (ll_addr
&& interface
->if_addrlen
== 0) {
181 interface
->if_addrlen
= ll_addr
->sdl_alen
;
183 else if (ll_addr
&& ll_addr
->sdl_alen
!= interface
->if_addrlen
) {
186 return dlil_if_attach_with_address(interface
, ll_addr
);
195 if (interface
== NULL
) return EINVAL
;
197 error
= dlil_if_detach(interface
);
198 if (error
== DLIL_WAIT_FOR_FREE
) error
= 0; /* Client should always wait for detach */
207 return interface
== NULL
? NULL
: interface
->if_softc
;
214 return interface
== NULL
? NULL
: interface
->if_name
;
221 return interface
== NULL
? 0 : interface
->if_family
;
228 return interface
== NULL
? (u_int32_t
)0xffffffff : (u_int32_t
)interface
->if_unit
;
235 return interface
== NULL
? (u_int32_t
)0xffffffff : interface
->if_index
;
246 if (interface
== NULL
) return EINVAL
;
247 lock
= (interface
->if_lock
!= 0);
249 if (lock
) ifnet_lock_exclusive(interface
);
251 /* If we are modifying the up/down state, call if_updown */
252 if (lock
&& (mask
& IFF_UP
) != 0) {
253 if_updown(interface
, (new_flags
& IFF_UP
) == IFF_UP
);
256 interface
->if_flags
= (new_flags
& mask
) | (interface
->if_flags
& ~mask
);
257 if (lock
) ifnet_lock_done(interface
);
266 return interface
== NULL
? 0 : interface
->if_flags
;
277 if (interface
== NULL
) return EINVAL
;
278 lock
= (interface
->if_lock
!= 0);
280 if (lock
) ifnet_lock_exclusive(interface
);
281 interface
->if_eflags
= (new_flags
& mask
) | (interface
->if_eflags
& ~mask
);
282 if (lock
) ifnet_lock_done(interface
);
291 return interface
== NULL
? 0 : interface
->if_eflags
;
294 static const ifnet_offload_t offload_mask
= IFNET_CSUM_IP
| IFNET_CSUM_TCP
|
295 IFNET_CSUM_UDP
| IFNET_CSUM_FRAGMENT
| IFNET_IP_FRAGMENT
|
296 IFNET_CSUM_SUM16
| IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
;
301 ifnet_offload_t offload
)
305 if (interface
== NULL
) return EINVAL
;
306 lock
= (interface
->if_lock
!= 0);
308 if (lock
) ifnet_lock_exclusive(interface
);
309 interface
->if_hwassist
= (offload
& offload_mask
);
310 if (lock
) ifnet_lock_done(interface
);
319 return interface
== NULL
? 0 : (interface
->if_hwassist
& offload_mask
);
323 * Should MIB data store a copy?
326 ifnet_set_link_mib_data(
333 if (interface
== NULL
) return EINVAL
;
334 lock
= (interface
->if_lock
!= 0);
336 if (lock
) ifnet_lock_exclusive(interface
);
337 interface
->if_linkmib
= (void*)mibData
;
338 interface
->if_linkmiblen
= mibLen
;
339 if (lock
) ifnet_lock_done(interface
);
344 ifnet_get_link_mib_data(
352 if (interface
== NULL
) return EINVAL
;
353 lock
= (interface
->if_lock
!= NULL
);
355 if (lock
) ifnet_lock_shared(interface
);
356 if (*mibLen
< interface
->if_linkmiblen
)
358 if (result
== 0 && interface
->if_linkmib
== NULL
)
362 *mibLen
= interface
->if_linkmiblen
;
363 bcopy(interface
->if_linkmib
, mibData
, *mibLen
);
365 if (lock
) ifnet_lock_done(interface
);
371 ifnet_get_link_mib_data_length(
374 return interface
== NULL
? 0 : interface
->if_linkmiblen
;
378 ifnet_attach_protocol(
380 protocol_family_t protocol
,
381 const struct ifnet_attach_proto_param
*proto_details
)
383 if (interface
== NULL
|| protocol
== 0 || proto_details
== NULL
)
385 return dlil_attach_protocol_kpi(interface
, protocol
, proto_details
);
389 ifnet_detach_protocol(
391 protocol_family_t protocol
)
393 if (interface
== NULL
|| protocol
== 0) return EINVAL
;
394 return dlil_detach_protocol(interface
, protocol
);
400 protocol_family_t protocol_family
,
403 const struct sockaddr
*dest
)
405 if (interface
== NULL
|| protocol_family
== 0 || m
== NULL
) {
410 return dlil_output(interface
, protocol_family
, m
, route
, dest
, 0);
416 protocol_family_t protocol_family
,
419 if (interface
== NULL
|| protocol_family
== 0 || m
== NULL
) {
424 return dlil_output(interface
, protocol_family
, m
, NULL
, NULL
, 1);
431 const struct ifnet_stat_increment_param
*stats
)
433 mbuf_t last_packet
= first_packet
;
435 if (interface
== NULL
|| first_packet
== NULL
) {
437 mbuf_freem_list(first_packet
);
441 while (mbuf_nextpkt(last_packet
) != NULL
)
442 last_packet
= mbuf_nextpkt(last_packet
);
443 return dlil_input_with_stats(interface
, first_packet
, last_packet
, stats
);
449 protocol_family_t protocol_family
,
450 u_int32_t ioctl_code
,
453 if (interface
== NULL
|| protocol_family
== 0 || ioctl_code
== 0)
455 return dlil_ioctl(protocol_family
, interface
,
456 ioctl_code
, ioctl_arg
);
462 struct kern_event_msg
* event_ptr
)
464 if (interface
== NULL
|| event_ptr
== NULL
) return EINVAL
;
465 return dlil_event(interface
, event_ptr
);
473 if (interface
== NULL
) return EINVAL
;
474 interface
->if_data
.ifi_mtu
= mtu
;
483 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_mtu
;
493 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_type
;
503 int lock
= (interface
->if_lock
!= 0);
504 if (lock
) ifnet_lock_exclusive(interface
);
505 interface
->if_data
.ifi_typelen
= typelen
;
506 if (lock
) ifnet_lock_done(interface
);
515 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_typelen
;
525 if (interface
== NULL
) return EINVAL
;
526 interface
->if_data
.ifi_addrlen
= addrlen
;
535 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_addrlen
;
544 if (interface
== NULL
) return EINVAL
;
545 interface
->if_data
.ifi_hdrlen
= hdrlen
;
554 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_hdrlen
;
563 if (interface
== NULL
) return EINVAL
;
564 interface
->if_data
.ifi_metric
= metric
;
573 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_metric
;
582 if (interface
== NULL
) return EINVAL
;
583 /* Pin baudrate to 32 bits until we can change the storage size */
584 interface
->if_data
.ifi_baudrate
= baudrate
> 0xFFFFFFFF ? 0xFFFFFFFF : baudrate
;
593 retval
= interface
== NULL
? 0 : interface
->if_data
.ifi_baudrate
;
598 ifnet_stat_increment(
600 const struct ifnet_stat_increment_param
*counts
)
602 if (interface
== NULL
) return EINVAL
;
604 lck_spin_lock(dlil_input_lock
);
606 interface
->if_data
.ifi_ipackets
+= counts
->packets_in
;
607 interface
->if_data
.ifi_ibytes
+= counts
->bytes_in
;
608 interface
->if_data
.ifi_ierrors
+= counts
->errors_in
;
610 interface
->if_data
.ifi_opackets
+= counts
->packets_out
;
611 interface
->if_data
.ifi_obytes
+= counts
->bytes_out
;
612 interface
->if_data
.ifi_oerrors
+= counts
->errors_out
;
614 interface
->if_data
.ifi_collisions
+= counts
->collisions
;
615 interface
->if_data
.ifi_iqdrops
+= counts
->dropped
;
617 /* Touch the last change time. */
618 TOUCHLASTCHANGE(&interface
->if_lastchange
);
620 lck_spin_unlock(dlil_input_lock
);
626 ifnet_stat_increment_in(
628 u_int32_t packets_in
,
632 if (interface
== NULL
) return EINVAL
;
634 lck_spin_lock(dlil_input_lock
);
636 interface
->if_data
.ifi_ipackets
+= packets_in
;
637 interface
->if_data
.ifi_ibytes
+= bytes_in
;
638 interface
->if_data
.ifi_ierrors
+= errors_in
;
640 TOUCHLASTCHANGE(&interface
->if_lastchange
);
642 lck_spin_unlock(dlil_input_lock
);
648 ifnet_stat_increment_out(
650 u_int32_t packets_out
,
652 u_int32_t errors_out
)
654 if (interface
== NULL
) return EINVAL
;
656 lck_spin_lock(dlil_input_lock
);
658 interface
->if_data
.ifi_opackets
+= packets_out
;
659 interface
->if_data
.ifi_obytes
+= bytes_out
;
660 interface
->if_data
.ifi_oerrors
+= errors_out
;
662 TOUCHLASTCHANGE(&interface
->if_lastchange
);
664 lck_spin_unlock(dlil_input_lock
);
672 const struct ifnet_stats_param
*stats
)
674 if (interface
== NULL
) return EINVAL
;
676 lck_spin_lock(dlil_input_lock
);
678 interface
->if_data
.ifi_ipackets
= stats
->packets_in
;
679 interface
->if_data
.ifi_ibytes
= stats
->bytes_in
;
680 interface
->if_data
.ifi_imcasts
= stats
->multicasts_in
;
681 interface
->if_data
.ifi_ierrors
= stats
->errors_in
;
683 interface
->if_data
.ifi_opackets
= stats
->packets_out
;
684 interface
->if_data
.ifi_obytes
= stats
->bytes_out
;
685 interface
->if_data
.ifi_omcasts
= stats
->multicasts_out
;
686 interface
->if_data
.ifi_oerrors
= stats
->errors_out
;
688 interface
->if_data
.ifi_collisions
= stats
->collisions
;
689 interface
->if_data
.ifi_iqdrops
= stats
->dropped
;
690 interface
->if_data
.ifi_noproto
= stats
->no_protocol
;
692 /* Touch the last change time. */
693 TOUCHLASTCHANGE(&interface
->if_lastchange
);
695 lck_spin_unlock(dlil_input_lock
);
703 struct ifnet_stats_param
*stats
)
705 if (interface
== NULL
) return EINVAL
;
707 lck_spin_lock(dlil_input_lock
);
709 stats
->packets_in
= interface
->if_data
.ifi_ipackets
;
710 stats
->bytes_in
= interface
->if_data
.ifi_ibytes
;
711 stats
->multicasts_in
= interface
->if_data
.ifi_imcasts
;
712 stats
->errors_in
= interface
->if_data
.ifi_ierrors
;
714 stats
->packets_out
= interface
->if_data
.ifi_opackets
;
715 stats
->bytes_out
= interface
->if_data
.ifi_obytes
;
716 stats
->multicasts_out
= interface
->if_data
.ifi_omcasts
;
717 stats
->errors_out
= interface
->if_data
.ifi_oerrors
;
719 stats
->collisions
= interface
->if_data
.ifi_collisions
;
720 stats
->dropped
= interface
->if_data
.ifi_iqdrops
;
721 stats
->no_protocol
= interface
->if_data
.ifi_noproto
;
723 lck_spin_unlock(dlil_input_lock
);
729 ifnet_touch_lastchange(
732 if (interface
== NULL
) return EINVAL
;
734 lck_spin_lock(dlil_input_lock
);
735 TOUCHLASTCHANGE(&interface
->if_lastchange
);
736 lck_spin_unlock(dlil_input_lock
);
744 struct timeval
*last_change
)
746 if (interface
== NULL
) return EINVAL
;
748 lck_spin_lock(dlil_input_lock
);
749 *last_change
= interface
->if_data
.ifi_lastchange
;
750 lck_spin_unlock(dlil_input_lock
);
752 #if IF_LASTCHANGEUPTIME
753 /* Crude conversion from uptime to calendar time */
754 last_change
->tv_sec
+= boottime_sec();
761 ifnet_get_address_list(
763 ifaddr_t
**addresses
)
765 if (interface
== NULL
|| addresses
== NULL
) return EINVAL
;
766 return ifnet_get_address_list_family(interface
, addresses
, 0);
770 ifnet_get_address_list_family(
772 ifaddr_t
**addresses
,
779 if (interface
== NULL
|| addresses
== NULL
) return EINVAL
;
782 ifnet_head_lock_shared();
783 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
785 if (interface
&& ifp
!= interface
) continue;
787 ifnet_lock_shared(ifp
);
788 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
789 if (interface
== NULL
|| interface
== ifp
)
792 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
794 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
)
799 else if (interface
!= NULL
) {
800 ifnet_lock_done(ifp
);
804 ifnet_lock_done(ifp
);
807 MALLOC(*addresses
, ifaddr_t
*, sizeof(ifaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
808 if (*addresses
== NULL
) {
813 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
815 if (interface
&& ifp
!= interface
) continue;
817 ifnet_lock_shared(ifp
);
818 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0) {
819 if (interface
== NULL
|| (struct ifnet
*)interface
== ifp
)
822 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
)
824 if (count
+ 1 > cmax
) break;
825 if (family
== 0 || addr
->ifa_addr
->sa_family
== family
) {
826 (*addresses
)[count
] = (ifaddr_t
)addr
;
827 ifaddr_reference((*addresses
)[count
]);
833 ifnet_lock_done(ifp
);
834 if (interface
|| count
== cmax
)
838 (*addresses
)[cmax
] = 0;
844 ifnet_free_address_list(
849 if (addresses
== NULL
) return;
851 for (i
= 0; addresses
[i
] != NULL
; i
++)
853 ifaddr_release(addresses
[i
]);
856 FREE(addresses
, M_TEMP
);
863 if (interface
== NULL
) return NULL
;
864 return LLADDR(SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
));
868 ifnet_llbroadcast_copy_bytes(
874 if (interface
== NULL
|| addr
== NULL
|| out_len
== NULL
) return EINVAL
;
876 *out_len
= interface
->if_broadcast
.length
;
878 if (buffer_len
< interface
->if_broadcast
.length
) {
882 if (interface
->if_broadcast
.length
== 0)
885 if (interface
->if_broadcast
.length
<= sizeof(interface
->if_broadcast
.u
.buffer
)) {
886 bcopy(interface
->if_broadcast
.u
.buffer
, addr
, interface
->if_broadcast
.length
);
889 bcopy(interface
->if_broadcast
.u
.ptr
, addr
, interface
->if_broadcast
.length
);
896 ifnet_lladdr_copy_bytes(
901 struct sockaddr_dl
*sdl
;
902 if (interface
== NULL
|| lladdr
== NULL
) return EINVAL
;
904 sdl
= SDL(interface
->if_addrhead
.tqh_first
->ifa_addr
);
907 if (lladdr_len
!= sdl
->sdl_alen
) {
908 bzero(lladdr
, lladdr_len
);
911 bcopy(LLADDR(sdl
), lladdr
, lladdr_len
);
912 if (bcmp(lladdr
, LLADDR(sdl
), lladdr_len
) == 0 &&
913 lladdr_len
== sdl
->sdl_alen
)
920 ifnet_set_lladdr_internal(
928 struct sockaddr_dl
*sdl
;
931 if (interface
== NULL
) return EINVAL
;
933 if (lladdr_len
!= 0 && (lladdr_len
!= interface
->if_addrlen
|| lladdr
== 0))
936 ifnet_head_lock_shared();
937 ifa
= ifnet_addrs
[interface
->if_index
- 1];
939 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
940 if (lladdr_len
!= 0) {
941 bcopy(lladdr
, LLADDR(sdl
), lladdr_len
);
944 bzero(LLADDR(sdl
), interface
->if_addrlen
);
946 sdl
->sdl_alen
= lladdr_len
;
949 sdl
->sdl_type
= new_type
;
957 /* Generate a kernel event */
959 dlil_post_msg(interface
, KEV_DL_SUBCLASS
,
960 KEV_DL_LINK_ADDRESS_CHANGED
, NULL
, 0);
972 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, 0, 0);
976 ifnet_set_lladdr_and_type(
982 return ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, type
, 1);
988 const struct sockaddr
*maddr
,
989 ifmultiaddr_t
*address
)
991 if (interface
== NULL
|| maddr
== NULL
) return EINVAL
;
992 return if_addmulti(interface
, maddr
, address
);
996 ifnet_remove_multicast(
997 ifmultiaddr_t address
)
999 if (address
== NULL
) return EINVAL
;
1000 return if_delmultiaddr(address
, 0);
1003 errno_t
ifnet_get_multicast_list(ifnet_t interface
, ifmultiaddr_t
**addresses
)
1007 struct ifmultiaddr
*addr
;
1010 if (interface
== NULL
|| addresses
== NULL
)
1013 lock
= (interface
->if_lock
!= 0);
1014 if (lock
) ifnet_lock_shared(interface
);
1015 if ((interface
->if_eflags
& IFEF_DETACHING
) == 0) {
1016 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1022 if (lock
) ifnet_lock_done(interface
);
1026 MALLOC(*addresses
, ifmultiaddr_t
*, sizeof(ifmultiaddr_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
1027 if (*addresses
== NULL
) {
1028 if (lock
) ifnet_lock_done(interface
);
1032 LIST_FOREACH(addr
, &interface
->if_multiaddrs
, ifma_link
)
1034 if (count
+ 1 > cmax
)
1036 (*addresses
)[count
] = (ifmultiaddr_t
)addr
;
1037 ifmaddr_reference((*addresses
)[count
]);
1040 (*addresses
)[cmax
] = 0;
1041 if (lock
) ifnet_lock_done(interface
);
1047 ifnet_free_multicast_list(
1048 ifmultiaddr_t
*addresses
)
1052 if (addresses
== NULL
) return;
1054 for (i
= 0; addresses
[i
] != NULL
; i
++)
1056 ifmaddr_release(addresses
[i
]);
1059 FREE(addresses
, M_TEMP
);
1070 if (ifname
== NULL
) return EINVAL
;
1072 namelen
= strlen(ifname
);
1076 ifnet_head_lock_shared();
1077 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1079 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
1080 struct sockaddr_dl
*ll_addr
;
1082 if (!ifa
|| !ifa
->ifa_addr
)
1085 ll_addr
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1087 if ((ifp
->if_eflags
& IFEF_DETACHING
) == 0 &&
1088 namelen
== ll_addr
->sdl_nlen
&&
1089 (strncmp(ll_addr
->sdl_data
, ifname
, ll_addr
->sdl_nlen
) == 0))
1096 ifnet_reference(*interface
);
1100 return (ifp
== NULL
) ? ENXIO
: 0;
1105 ifnet_family_t family
,
1114 if (list
== NULL
|| count
== NULL
) return EINVAL
;
1116 ifnet_head_lock_shared();
1117 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1119 if (ifp
->if_eflags
& IFEF_DETACHING
) continue;
1120 if (family
== 0 || ifp
->if_family
== family
)
1128 MALLOC(*list
, ifnet_t
*, sizeof(ifnet_t
) * (cmax
+ 1), M_TEMP
, M_NOWAIT
);
1134 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1136 if (ifp
->if_eflags
& IFEF_DETACHING
) continue;
1137 if (*count
+ 1 > cmax
) break;
1138 if (family
== 0 || ((ifnet_family_t
)ifp
->if_family
) == family
)
1140 (*list
)[*count
] = (ifnet_t
)ifp
;
1141 ifnet_reference((*list
)[*count
]);
1145 (*list
)[*count
] = NULL
;
1154 ifnet_t
*interfaces
)
1158 if (interfaces
== NULL
) return;
1160 for (i
= 0; interfaces
[i
]; i
++)
1162 ifnet_release(interfaces
[i
]);
1165 FREE(interfaces
, M_TEMP
);
1168 /****************************************************************************/
1169 /* ifaddr_t accessors */
1170 /****************************************************************************/
1176 if (ifa
== NULL
) return EINVAL
;
1185 if (ifa
== NULL
) return EINVAL
;
1191 ifaddr_address_family(
1194 if (ifa
&& ifa
->ifa_addr
)
1195 return ifa
->ifa_addr
->sa_family
;
1203 struct sockaddr
*out_addr
,
1204 u_int32_t addr_size
)
1208 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1209 if (ifa
->ifa_addr
== NULL
) return ENOTSUP
;
1211 copylen
= (addr_size
>= ifa
->ifa_addr
->sa_len
) ? ifa
->ifa_addr
->sa_len
: addr_size
;
1212 bcopy(ifa
->ifa_addr
, out_addr
, copylen
);
1214 if (ifa
->ifa_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1222 struct sockaddr
*out_addr
,
1223 u_int32_t addr_size
)
1226 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1227 if (ifa
->ifa_dstaddr
== NULL
) return ENOTSUP
;
1229 copylen
= (addr_size
>= ifa
->ifa_dstaddr
->sa_len
) ? ifa
->ifa_dstaddr
->sa_len
: addr_size
;
1230 bcopy(ifa
->ifa_dstaddr
, out_addr
, copylen
);
1232 if (ifa
->ifa_dstaddr
->sa_len
> addr_size
) return EMSGSIZE
;
1240 struct sockaddr
*out_addr
,
1241 u_int32_t addr_size
)
1244 if (ifa
== NULL
|| out_addr
== NULL
) return EINVAL
;
1245 if (ifa
->ifa_netmask
== NULL
) return ENOTSUP
;
1247 copylen
= addr_size
>= ifa
->ifa_netmask
->sa_len
? ifa
->ifa_netmask
->sa_len
: addr_size
;
1248 bcopy(ifa
->ifa_netmask
, out_addr
, copylen
);
1250 if (ifa
->ifa_netmask
->sa_len
> addr_size
) return EMSGSIZE
;
1260 if (ifa
== NULL
) return NULL
;
1263 return (ifnet_t
)ifp
;
1268 const struct sockaddr
* address
)
1270 if (address
== NULL
) return NULL
;
1271 return ifa_ifwithaddr(address
);
1276 const struct sockaddr
* address
)
1278 if (address
== NULL
) return NULL
;
1279 return ifa_ifwithdstaddr(address
);
1284 const struct sockaddr
* net
)
1286 if (net
== NULL
) return NULL
;
1287 return ifa_ifwithnet(net
);
1293 const struct sockaddr
* destination
,
1294 const struct sockaddr
* gateway
)
1296 if (destination
== NULL
|| gateway
== NULL
) return NULL
;
1297 return ifa_ifwithroute(flags
, destination
, gateway
);
1301 ifaddr_findbestforaddr(
1302 const struct sockaddr
*addr
,
1305 if (addr
== NULL
|| interface
== NULL
) return NULL
;
1306 return ifaof_ifpforaddr(addr
, interface
);
1311 ifmultiaddr_t ifmaddr
)
1313 if (ifmaddr
== NULL
) return EINVAL
;
1314 ifma_reference(ifmaddr
);
1320 ifmultiaddr_t ifmaddr
)
1322 if (ifmaddr
== NULL
) return EINVAL
;
1323 ifma_release(ifmaddr
);
1329 ifmultiaddr_t ifmaddr
,
1330 struct sockaddr
*out_addr
,
1331 u_int32_t addr_size
)
1335 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1336 if (ifmaddr
->ifma_addr
== NULL
) return ENOTSUP
;
1338 copylen
= addr_size
>= ifmaddr
->ifma_addr
->sa_len
? ifmaddr
->ifma_addr
->sa_len
: addr_size
;
1339 bcopy(ifmaddr
->ifma_addr
, out_addr
, copylen
);
1341 if (ifmaddr
->ifma_addr
->sa_len
> addr_size
) return EMSGSIZE
;
1348 ifmultiaddr_t ifmaddr
,
1349 struct sockaddr
*out_addr
,
1350 u_int32_t addr_size
)
1352 if (ifmaddr
== NULL
|| out_addr
== NULL
) return EINVAL
;
1353 if (ifmaddr
->ifma_ll
== NULL
) return ENOTSUP
;
1355 return ifmaddr_address(ifmaddr
->ifma_ll
, out_addr
, addr_size
);
1360 ifmultiaddr_t ifmaddr
)
1362 if (ifmaddr
== NULL
|| ifmaddr
->ifma_ifp
== NULL
) return NULL
;
1363 return ifmaddr
->ifma_ifp
;