2 * Copyright (c) 2002-2007, 2010, 2011, 2013, 2015, 2016 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * October 21, 2000 Allan Nathanson <ajn@apple.com>
33 #define KERNEL_PRIVATE
34 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <net/ethernet.h>
39 #include <net/if_vlan_var.h>
40 #include <net/if_media.h>
41 #include <net/if_types.h>
42 #include <netinet/in.h>
43 #include <netinet/ip6.h> // for IPV6_MMTU
45 #include "SCNetworkConfigurationInternal.h" // for __SCNetworkInterfaceCreatePrivate
47 #include <IOKit/IOKitLib.h>
48 #include <IOKit/network/IONetworkInterface.h>
49 #include <IOKit/network/IONetworkController.h>
50 #include "dy_framework.h"
54 #pragma mark Capabilities
57 // the following table needs to keep the capabilitiy names and values
58 // between the <SystemConfiguration/SCSchemaDefinitionsPrivate.h> and
59 // <net/if.h> headers in sync.
61 const CFStringRef
*name
;
64 } capabilityMappings
[] = {
66 { &kSCPropNetEthernetCapabilityRXCSUM
, TRUE
, IFCAP_RXCSUM
}, // can offload checksum on RX
67 { &kSCPropNetEthernetCapabilityTXCSUM
, TRUE
, IFCAP_TXCSUM
}, // can offload checksum on TX
68 { &kSCPropNetEthernetCapabilityVLAN_MTU
, FALSE
, IFCAP_VLAN_MTU
}, // VLAN-compatible MTU
69 { &kSCPropNetEthernetCapabilityVLAN_HWTAGGING
, FALSE
, IFCAP_VLAN_HWTAGGING
}, // hardware VLAN tag support
70 { &kSCPropNetEthernetCapabilityJUMBO_MTU
, FALSE
, IFCAP_JUMBO_MTU
}, // 9000 byte MTU supported
71 { &kSCPropNetEthernetCapabilityTSO
, TRUE
, IFCAP_TSO
}, // can do TCP/TCP6 Segmentation Offload
72 { &kSCPropNetEthernetCapabilityTSO4
, FALSE
, IFCAP_TSO4
}, // can do TCP Segmentation Offload
73 { &kSCPropNetEthernetCapabilityTSO6
, FALSE
, IFCAP_TSO6
}, // can do TCP6 Segmentation Offload
74 { &kSCPropNetEthernetCapabilityLRO
, TRUE
, IFCAP_LRO
}, // can do Large Receive Offload
75 { &kSCPropNetEthernetCapabilityAV
, TRUE
, IFCAP_AV
}, // can do 802.1 AV Bridging
81 findCapability(CFStringRef capability
)
85 for (i
= 0; i
< sizeof(capabilityMappings
) / sizeof(capabilityMappings
[0]); i
++) {
86 if (CFEqual(capability
, *capabilityMappings
[i
].name
)) {
96 __getCapabilities(CFStringRef interfaceName
,
105 bzero((void *)&ifr
, sizeof(ifr
));
106 if (_SC_cfstring_to_cstring(interfaceName
, ifr
.ifr_name
, sizeof(ifr
.ifr_name
), kCFStringEncodingASCII
) == NULL
) {
107 SC_log(LOG_NOTICE
, "could not convert interface name");
108 _SCErrorSet(kSCStatusInvalidArgument
);
112 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
115 SC_log(LOG_ERR
, "socket() failed: %s", strerror(errno
));
119 if (ioctl(sock
, SIOCGIFCAP
, (caddr_t
)&ifr
) == -1) {
126 SC_log(LOG_NOTICE
, "ioctl(SIOCGIFCAP) failed: %s", strerror(errno
));
131 if (current
!= NULL
) *current
= ifr
.ifr_curcap
;
132 if (available
!= NULL
) *available
= ifr
.ifr_reqcap
;
141 if (current
!= NULL
) *current
= 0;
142 if (available
!= NULL
) *available
= 0;
149 __SCNetworkInterfaceCreateCapabilities(SCNetworkInterfaceRef interface
,
151 CFDictionaryRef capability_options
)
153 int cap_available
= 0;
154 int cap_current
= capability_base
;
156 CFStringRef interfaceName
;
158 if (!isA_SCNetworkInterface(interface
)) {
159 _SCErrorSet(kSCStatusInvalidArgument
);
163 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
164 if (interfaceName
== NULL
) {
165 _SCErrorSet(kSCStatusInvalidArgument
);
169 if (!__getCapabilities(interfaceName
,
170 (capability_base
== -1) ? &cap_current
: NULL
,
175 if (cap_available
== 0) {
179 if (capability_options
== NULL
) {
183 for (i
= 0; i
< sizeof(capabilityMappings
) / sizeof(capabilityMappings
[0]); i
++) {
187 if (((cap_available
& capabilityMappings
[i
].val
) != 0) &&
188 capabilityMappings
[i
].readwrite
&&
189 CFDictionaryGetValueIfPresent(capability_options
,
190 *capabilityMappings
[i
].name
,
193 CFNumberGetValue(val
, kCFNumberIntType
, &cap_val
)) {
196 cap_current
|= (cap_available
& capabilityMappings
[i
].val
);
198 cap_current
&= ~capabilityMappings
[i
].val
;
201 // don't process again
202 cap_available
&= ~capabilityMappings
[i
].val
;
213 SCNetworkInterfaceCopyCapability(SCNetworkInterfaceRef interface
,
214 CFStringRef capability
)
217 int cap_available
= 0;
220 CFStringRef interfaceName
;
221 CFTypeRef val
= NULL
;
223 if (!isA_SCNetworkInterface(interface
)) {
224 _SCErrorSet(kSCStatusInvalidArgument
);
228 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
229 if (interfaceName
== NULL
) {
230 _SCErrorSet(kSCStatusInvalidArgument
);
234 if (!__getCapabilities(interfaceName
, &cap_current
, &cap_available
)) {
238 if (capability
== NULL
) {
239 CFMutableDictionaryRef all
= NULL
;
241 // if ALL capabilities requested
242 for (i
= 0; i
< sizeof(capabilityMappings
) / sizeof(capabilityMappings
[0]); i
++) {
243 if ((cap_available
& capabilityMappings
[i
].val
) == capabilityMappings
[i
].val
) {
245 all
= CFDictionaryCreateMutable(NULL
,
247 &kCFTypeDictionaryKeyCallBacks
,
248 &kCFTypeDictionaryValueCallBacks
);
250 cap_val
= ((cap_current
& capabilityMappings
[i
].val
) == capabilityMappings
[i
].val
) ? 1 : 0;
251 val
= CFNumberCreate(NULL
, kCFNumberIntType
, &cap_val
);
252 CFDictionarySetValue(all
, *capabilityMappings
[i
].name
, val
);
254 cap_available
&= ~capabilityMappings
[i
].val
;
260 i
= findCapability(capability
);
261 if (i
== kCFNotFound
) {
262 // if unknown capability
263 _SCErrorSet(kSCStatusInvalidArgument
);
267 if ((cap_available
& capabilityMappings
[i
].val
) != capabilityMappings
[i
].val
) {
268 // if capability not available
269 _SCErrorSet(kSCStatusInvalidArgument
);
273 cap_val
= ((cap_current
& capabilityMappings
[i
].val
) == capabilityMappings
[i
].val
) ? 1 : 0;
274 val
= CFNumberCreate(NULL
, kCFNumberIntType
, &cap_val
);
282 SCNetworkInterfaceSetCapability(SCNetworkInterfaceRef interface
,
283 CFStringRef capability
,
286 int cap_available
= 0;
287 CFDictionaryRef configuration
;
289 CFStringRef interfaceName
;
290 CFMutableDictionaryRef newConfiguration
= NULL
;
293 if (!isA_SCNetworkInterface(interface
)) {
294 _SCErrorSet(kSCStatusInvalidArgument
);
298 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
299 if (interfaceName
== NULL
) {
300 // if no interface name
301 _SCErrorSet(kSCStatusInvalidArgument
);
305 i
= findCapability(capability
);
306 if (i
== kCFNotFound
) {
307 // if unknown capability
308 _SCErrorSet(kSCStatusInvalidArgument
);
312 if (!capabilityMappings
[i
].readwrite
) {
314 _SCErrorSet(kSCStatusInvalidArgument
);
318 if ((newValue
!= NULL
) && !isA_CFNumber(newValue
)) {
319 // all values must (for now) be CFNumber[0 or 1]'s
320 _SCErrorSet(kSCStatusInvalidArgument
);
324 if (!__getCapabilities(interfaceName
, NULL
, &cap_available
)) {
328 if ((cap_available
& capabilityMappings
[i
].val
) == 0) {
329 // if capability not available
330 _SCErrorSet(kSCStatusInvalidArgument
);
334 configuration
= SCNetworkInterfaceGetConfiguration(interface
);
335 if (configuration
== NULL
) {
336 newConfiguration
= CFDictionaryCreateMutable(NULL
,
338 &kCFTypeDictionaryKeyCallBacks
,
339 &kCFTypeDictionaryValueCallBacks
);
341 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
342 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
345 if ((newValue
!= NULL
)) {
346 CFDictionarySetValue(newConfiguration
, capability
, newValue
);
348 CFDictionaryRemoveValue(newConfiguration
, capability
);
349 if (CFDictionaryGetCount(newConfiguration
) == 0) {
350 CFRelease(newConfiguration
);
351 newConfiguration
= NULL
;
355 ok
= SCNetworkInterfaceSetConfiguration(interface
, newConfiguration
);
356 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
363 #pragma mark Media Options
366 static const struct ifmedia_description ifm_subtype_shared_descriptions
[] =
367 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
369 static const struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
370 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
372 static const struct ifmedia_description ifm_subtype_ieee80211_descriptions
[] =
373 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
375 static const struct ifmedia_description ifm_shared_option_descriptions
[] =
376 IFM_SHARED_OPTION_DESCRIPTIONS
;
378 static const struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
379 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
381 static const struct ifmedia_description ifm_subtype_ieee80211_option_descriptions
[] =
382 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
386 __freeMediaList(struct ifmediareq
*ifm
)
388 if (ifm
->ifm_ulist
!= NULL
) CFAllocatorDeallocate(NULL
, ifm
->ifm_ulist
);
389 CFAllocatorDeallocate(NULL
, ifm
);
394 static struct ifmediareq
*
395 __copyMediaList(CFStringRef interfaceName
)
397 struct ifmediareq
*ifm
;
401 ifm
= (struct ifmediareq
*)CFAllocatorAllocate(NULL
, sizeof(struct ifmediareq
), 0);
402 bzero((void *)ifm
, sizeof(*ifm
));
404 if (_SC_cfstring_to_cstring(interfaceName
, ifm
->ifm_name
, sizeof(ifm
->ifm_name
), kCFStringEncodingASCII
) == NULL
) {
405 SC_log(LOG_NOTICE
, "could not convert interface name");
409 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
411 SC_log(LOG_ERR
, "socket() failed: %s", strerror(errno
));
415 if (ioctl(sock
, SIOCGIFMEDIA
, (caddr_t
)ifm
) == -1) {
416 // SC_log(LOG_NOTICE, "ioctl(SIOCGIFMEDIA) failed: %s", strerror(errno));
420 if (ifm
->ifm_count
> 0) {
421 ifm
->ifm_ulist
= (int *)CFAllocatorAllocate(NULL
, ifm
->ifm_count
* sizeof(int), 0);
422 if (ioctl(sock
, SIOCGIFMEDIA
, (caddr_t
)ifm
) == -1) {
423 SC_log(LOG_NOTICE
, "ioctl(SIOCGIFMEDIA) failed: %s", strerror(errno
));
432 if (sock
!= -1) (void)close(sock
);
434 __freeMediaList(ifm
);
436 _SCErrorSet(kSCStatusFailed
);
442 static CFDictionaryRef
443 __createMediaDictionary(int media_options
, Boolean filter
)
445 CFMutableDictionaryRef dict
= NULL
;
447 const struct ifmedia_description
*option_descriptions
= NULL
;
448 CFMutableArrayRef options
= NULL
;
449 const struct ifmedia_description
*subtype_descriptions
= NULL
;
453 ((IFM_SUBTYPE(media_options
) == IFM_NONE
) ||
454 ((IFM_OPTIONS(media_options
) & IFM_LOOP
) != 0))) {
455 return NULL
; /* filter */
458 switch (IFM_TYPE(media_options
)) {
460 option_descriptions
= ifm_subtype_ethernet_option_descriptions
;
461 subtype_descriptions
= ifm_subtype_ethernet_descriptions
;
464 option_descriptions
= ifm_subtype_ieee80211_option_descriptions
;
465 subtype_descriptions
= ifm_subtype_ieee80211_descriptions
;
471 dict
= CFDictionaryCreateMutable(NULL
,
473 &kCFTypeDictionaryKeyCallBacks
,
474 &kCFTypeDictionaryValueCallBacks
);
479 for (i
= 0; !val
&& ifm_subtype_shared_descriptions
[i
].ifmt_string
; i
++) {
480 if (IFM_SUBTYPE(media_options
) == ifm_subtype_shared_descriptions
[i
].ifmt_word
) {
481 val
= CFStringCreateWithCString(NULL
,
482 ifm_subtype_shared_descriptions
[i
].ifmt_string
,
483 kCFStringEncodingASCII
);
488 if (subtype_descriptions
!= NULL
) {
489 for (i
= 0; !val
&& subtype_descriptions
[i
].ifmt_string
; i
++) {
490 if (IFM_SUBTYPE(media_options
) == subtype_descriptions
[i
].ifmt_word
) {
491 val
= CFStringCreateWithCString(NULL
,
492 subtype_descriptions
[i
].ifmt_string
,
493 kCFStringEncodingASCII
);
500 CFDictionaryAddValue(dict
, kSCPropNetEthernetMediaSubType
, val
);
506 options
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
508 for (i
= 0; (IFM_OPTIONS(media_options
) != 0) && (ifm_shared_option_descriptions
[i
].ifmt_string
!= NULL
); i
++) {
509 if ((media_options
& ifm_shared_option_descriptions
[i
].ifmt_word
) != 0) {
510 val
= CFStringCreateWithCString(NULL
,
511 ifm_shared_option_descriptions
[i
].ifmt_string
,
512 kCFStringEncodingASCII
);
513 CFArrayAppendValue(options
, val
);
516 media_options
&= ~ifm_shared_option_descriptions
[i
].ifmt_word
;
520 if (option_descriptions
!= NULL
) {
521 for (i
= 0; (IFM_OPTIONS(media_options
) != 0) && (option_descriptions
[i
].ifmt_string
!= NULL
); i
++) {
522 if ((media_options
& option_descriptions
[i
].ifmt_word
) != 0) {
523 val
= CFStringCreateWithCString(NULL
,
524 option_descriptions
[i
].ifmt_string
,
525 kCFStringEncodingASCII
);
526 CFArrayAppendValue(options
, val
);
529 media_options
&= ~option_descriptions
[i
].ifmt_word
;
534 CFDictionaryAddValue(dict
, kSCPropNetEthernetMediaOptions
, options
);
542 __SCNetworkInterfaceCreateMediaOptions(SCNetworkInterfaceRef interface
, CFDictionaryRef media_options
)
545 struct ifmediareq
*ifm
;
547 CFStringRef interfaceName
;
550 const struct ifmedia_description
*option_descriptions
= NULL
;
553 const struct ifmedia_description
*subtype_descriptions
= NULL
;
556 if (!isA_SCNetworkInterface(interface
)) {
557 _SCErrorSet(kSCStatusInvalidArgument
);
561 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
562 if (interfaceName
== NULL
) {
563 _SCErrorSet(kSCStatusInvalidArgument
);
569 ifm
= __copyMediaList(interfaceName
);
571 if (ifm
->ifm_count
> 0) {
572 ifm_new
= IFM_TYPE(ifm
->ifm_ulist
[0]);
574 __freeMediaList(ifm
);
578 // if we cannot determine the media type for the interface
582 switch (IFM_TYPE(ifm_new
)) {
584 option_descriptions
= ifm_subtype_ethernet_option_descriptions
;
585 subtype_descriptions
= ifm_subtype_ethernet_descriptions
;
588 option_descriptions
= ifm_subtype_ieee80211_option_descriptions
;
589 subtype_descriptions
= ifm_subtype_ieee80211_descriptions
;
595 val
= CFDictionaryGetValue(media_options
, kSCPropNetEthernetMediaSubType
);
596 if (!isA_CFString(val
)) {
600 str
= _SC_cfstring_to_cstring(val
, NULL
, 0, kCFStringEncodingASCII
);
606 for (i
= 0; !match
&& ifm_subtype_shared_descriptions
[i
].ifmt_string
; i
++) {
607 if (strcasecmp(str
, ifm_subtype_shared_descriptions
[i
].ifmt_string
) == 0) {
608 ifm_new
|= ifm_subtype_shared_descriptions
[i
].ifmt_word
;
614 if (subtype_descriptions
!= NULL
) {
615 for (i
= 0; !match
&& subtype_descriptions
[i
].ifmt_string
; i
++) {
616 if (strcasecmp(str
, subtype_descriptions
[i
].ifmt_string
) == 0) {
617 ifm_new
|= subtype_descriptions
[i
].ifmt_word
;
624 CFAllocatorDeallocate(NULL
, str
);
627 return -1; /* if no subtype */
632 options
= CFDictionaryGetValue(media_options
, kSCPropNetEthernetMediaOptions
);
633 if (!isA_CFArray(options
)) {
637 n
= CFArrayGetCount(options
);
638 for (i
= 0; i
< n
; i
++) {
641 val
= CFArrayGetValueAtIndex(options
, i
);
642 if (!isA_CFString(val
)) {
646 str
= _SC_cfstring_to_cstring(val
, NULL
, 0, kCFStringEncodingASCII
);
653 for (j
= 0; !match
&& ifm_shared_option_descriptions
[j
].ifmt_string
; j
++) {
654 if (strcasecmp(str
, ifm_shared_option_descriptions
[j
].ifmt_string
) == 0) {
655 ifm_new
|= ifm_shared_option_descriptions
[j
].ifmt_word
;
661 if (option_descriptions
!= NULL
) {
662 for (j
= 0; !match
&& option_descriptions
[j
].ifmt_string
; j
++) {
663 if (strcasecmp(str
, option_descriptions
[j
].ifmt_string
) == 0) {
664 ifm_new
|= option_descriptions
[j
].ifmt_word
;
671 CFAllocatorDeallocate(NULL
, str
);
674 return -1; /* if no option */
683 SCNetworkInterfaceCopyMediaOptions(SCNetworkInterfaceRef interface
,
684 CFDictionaryRef
*current
,
685 CFDictionaryRef
*active
,
686 CFArrayRef
*available
,
690 struct ifmediareq
*ifm
;
691 CFStringRef interfaceName
;
693 if (!isA_SCNetworkInterface(interface
)) {
694 _SCErrorSet(kSCStatusInvalidArgument
);
698 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
699 if (interfaceName
== NULL
) {
700 _SCErrorSet(kSCStatusInvalidArgument
);
704 ifm
= __copyMediaList(interfaceName
);
709 if (active
!= NULL
) *active
= NULL
;
710 if (current
!= NULL
) *current
= NULL
;
711 if (available
!= NULL
) {
712 CFMutableArrayRef media_options
;
714 media_options
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
715 for (i
= 0; i
< ifm
->ifm_count
; i
++) {
716 CFDictionaryRef options
;
718 options
= __createMediaDictionary(ifm
->ifm_ulist
[i
], filter
);
719 if (options
== NULL
) {
723 if ((active
!= NULL
) && (*active
== NULL
) && (ifm
->ifm_active
== ifm
->ifm_ulist
[i
])) {
724 *active
= CFRetain(options
);
727 if ((current
!= NULL
) && (*current
== NULL
) && (ifm
->ifm_current
== ifm
->ifm_ulist
[i
])) {
728 *current
= CFRetain(options
);
731 if (!CFArrayContainsValue(media_options
, CFRangeMake(0, CFArrayGetCount(media_options
)), options
)) {
732 CFArrayAppendValue(media_options
, options
);
737 *available
= (CFArrayRef
)media_options
;
740 if ((active
!= NULL
) && (*active
== NULL
)) {
741 *active
= __createMediaDictionary(ifm
->ifm_active
, FALSE
);
744 if ((current
!= NULL
) && (*current
== NULL
)) {
745 if ((active
!= NULL
) && (ifm
->ifm_active
== ifm
->ifm_current
)) {
746 if (*active
!= NULL
) *current
= CFRetain(*active
);
748 *current
= __createMediaDictionary(ifm
->ifm_current
, FALSE
);
752 __freeMediaList(ifm
);
758 SCNetworkInterfaceCopyMediaSubTypes(CFArrayRef available
)
762 CFMutableArrayRef subTypes
;
764 if (!isA_CFArray(available
)) {
765 _SCErrorSet(kSCStatusInvalidArgument
);
769 subTypes
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
771 n
= CFArrayGetCount(available
);
772 for (i
= 0; i
< n
; i
++) {
773 CFDictionaryRef options
;
776 options
= CFArrayGetValueAtIndex(available
, i
);
777 if (!isA_CFDictionary(options
)) {
781 subType
= CFDictionaryGetValue(options
, kSCPropNetEthernetMediaSubType
);
782 if (!isA_CFString(subType
)) {
786 if (!CFArrayContainsValue(subTypes
, CFRangeMake(0, CFArrayGetCount(subTypes
)), subType
)) {
787 CFArrayAppendValue(subTypes
, subType
);
791 if (CFArrayGetCount(subTypes
) == 0) {
794 _SCErrorSet(kSCStatusOK
);
802 SCNetworkInterfaceCopyMediaSubTypeOptions(CFArrayRef available
,
807 CFMutableArrayRef subTypeOptions
;
809 if (!isA_CFArray(available
)) {
810 _SCErrorSet(kSCStatusInvalidArgument
);
814 subTypeOptions
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
816 n
= CFArrayGetCount(available
);
817 for (i
= 0; i
< n
; i
++) {
818 CFDictionaryRef options
;
819 CFArrayRef mediaOptions
;
820 CFStringRef mediaSubType
;
822 options
= CFArrayGetValueAtIndex(available
, i
);
823 if (!isA_CFDictionary(options
)) {
827 mediaSubType
= CFDictionaryGetValue(options
, kSCPropNetEthernetMediaSubType
);
828 if (!isA_CFString(mediaSubType
) || !CFEqual(subType
, mediaSubType
)) {
832 mediaOptions
= CFDictionaryGetValue(options
, kSCPropNetEthernetMediaOptions
);
833 if (!isA_CFArray(mediaOptions
)) {
837 if (!CFArrayContainsValue(subTypeOptions
, CFRangeMake(0, CFArrayGetCount(subTypeOptions
)), mediaOptions
)) {
838 CFArrayAppendValue(subTypeOptions
, mediaOptions
);
842 if (CFArrayGetCount(subTypeOptions
) == 0) {
843 CFRelease(subTypeOptions
);
844 subTypeOptions
= NULL
;
845 _SCErrorSet(kSCStatusOK
);
848 return subTypeOptions
;
853 _SCNetworkInterfaceIsPhysicalEthernet(SCNetworkInterfaceRef interface
)
856 struct ifmediareq
*ifm
;
857 CFStringRef interfaceName
;
858 Boolean realEthernet
= FALSE
;
860 if (!isA_SCNetworkInterface(interface
)) {
861 _SCErrorSet(kSCStatusInvalidArgument
);
865 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
866 if (interfaceName
== NULL
) {
867 _SCErrorSet(kSCStatusInvalidArgument
);
871 ifm
= __copyMediaList(interfaceName
);
873 CFStringRef interfaceType
;
875 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
876 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeEthernet
) &&
877 !_SCNetworkInterfaceIsTethered(interface
) &&
878 !_SCNetworkInterfaceIsBluetoothPAN(interface
)) {
879 // if likely physical ethernet interface
884 _SCErrorSet(kSCStatusOK
);
885 if (IFM_TYPE(ifm
->ifm_current
) != IFM_ETHER
) {
888 if (ifm
->ifm_count
== 1
889 && IFM_SUBTYPE(ifm
->ifm_ulist
[0]) == IFM_AUTO
) {
890 /* only support autoselect, not really ethernet */
893 for (i
= 0; i
< ifm
->ifm_count
; i
++) {
894 if ((ifm
->ifm_ulist
[i
] & IFM_FDX
) != 0) {
900 __freeMediaList(ifm
);
901 return (realEthernet
);
905 __getIOMTULimits(char ifr_name
[IFNAMSIZ
],
910 io_iterator_t io_iter
= 0;
911 io_registry_entry_t io_interface
= 0;
912 io_registry_entry_t io_controller
= 0;
914 static mach_port_t masterPort
= MACH_PORT_NULL
;
915 CFMutableDictionaryRef matchingDict
;
917 /* look for a matching interface in the IORegistry */
919 if (masterPort
== MACH_PORT_NULL
) {
920 kr
= IOMasterPort(MACH_PORT_NULL
, &masterPort
);
921 if (kr
!= KERN_SUCCESS
) {
926 matchingDict
= IOBSDNameMatching(masterPort
, 0, ifr_name
);
928 /* Note: IOServiceGetMatchingServices consumes a reference on the 'matchingDict' */
929 kr
= IOServiceGetMatchingServices(masterPort
, matchingDict
, &io_iter
);
930 if ((kr
== KERN_SUCCESS
) && io_iter
) {
931 /* should only have a single match */
932 io_interface
= IOIteratorNext(io_iter
);
934 if (io_iter
) IOObjectRelease(io_iter
);
941 * found an interface, get the interface type
943 num
= IORegistryEntryCreateCFProperty(io_interface
, CFSTR(kIOInterfaceType
), NULL
, kNilOptions
);
945 if (isA_CFNumber(num
)) {
946 CFNumberGetValue(num
, kCFNumberIntType
, &ifType
);
952 * ...and the property we are REALLY interested is in the controller,
953 * which is the parent of the interface object.
955 (void)IORegistryEntryGetParentEntry(io_interface
, kIOServicePlane
, &io_controller
);
956 IOObjectRelease(io_interface
);
958 /* if no matching interface */
965 num
= IORegistryEntryCreateCFProperty(io_controller
, CFSTR(kIOMaxPacketSize
), NULL
, kNilOptions
);
967 if (isA_CFNumber(num
)) {
971 * Get the value and subtract the FCS bytes and Ethernet header
972 * sizes from the maximum frame size reported by the controller
973 * to get the MTU size. The 14 byte media header can be found
974 * in the registry, but not the size for the trailing FCS bytes.
976 CFNumberGetValue(num
, kCFNumberIntType
, &value
);
978 if (ifType
== IFT_ETHER
) {
979 value
-= (ETHER_HDR_LEN
+ ETHER_CRC_LEN
);
982 if (mtu_min
) *mtu_min
= IF_MINMTU
;
983 if (mtu_max
) *mtu_max
= value
;
988 IOObjectRelease(io_controller
);
996 SCNetworkInterfaceCopyMTU(SCNetworkInterfaceRef interface
,
1002 CFStringRef interfaceName
;
1006 if (!isA_SCNetworkInterface(interface
)) {
1007 _SCErrorSet(kSCStatusInvalidArgument
);
1011 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
1012 if (interfaceName
== NULL
) {
1013 _SCErrorSet(kSCStatusInvalidArgument
);
1017 bzero((void *)&ifr
, sizeof(ifr
));
1018 if (_SC_cfstring_to_cstring(interfaceName
, ifr
.ifr_name
, sizeof(ifr
.ifr_name
), kCFStringEncodingASCII
) == NULL
) {
1019 SC_log(LOG_NOTICE
, "could not convert interface name");
1020 _SCErrorSet(kSCStatusInvalidArgument
);
1024 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1027 SC_log(LOG_ERR
, "socket() failed: %s", strerror(errno
));
1031 if (ioctl(sock
, SIOCGIFMTU
, (caddr_t
)&ifr
) == -1) {
1033 // SC_log(LOG_NOTICE, "ioctl(SIOCGIFMTU) failed: %s", strerror(errno));
1037 if (mtu_cur
) *mtu_cur
= ifr
.ifr_mtu
;
1038 if (mtu_min
) *mtu_min
= ifr
.ifr_mtu
;
1039 if (mtu_max
) *mtu_max
= ifr
.ifr_mtu
;
1041 /* get valid MTU range */
1043 if (mtu_min
!= NULL
|| mtu_max
!= NULL
) {
1044 if (ioctl(sock
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
) == 0) {
1045 struct ifdevmtu
* devmtu_p
;
1047 devmtu_p
= &ifr
.ifr_devmtu
;
1048 if (mtu_min
!= NULL
) {
1049 *mtu_min
= (devmtu_p
->ifdm_min
> IF_MINMTU
)
1050 ? devmtu_p
->ifdm_min
: IF_MINMTU
;
1052 if (mtu_max
!= NULL
) {
1053 *mtu_max
= devmtu_p
->ifdm_max
;
1056 ok
= __getIOMTULimits(ifr
.ifr_name
, mtu_min
, mtu_max
);
1058 CFStringRef interfaceType
;
1060 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
1061 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeBridge
)) {
1066 members
= SCBridgeInterfaceGetMemberInterfaces(interface
);
1067 n
= (members
!= NULL
) ? CFArrayGetCount(members
) : 0;
1069 if (mtu_min
) *mtu_min
= IF_MINMTU
;
1070 if (mtu_max
) *mtu_max
= IF_MAXMTU
;
1072 for (i
= 0; i
< n
; i
++) {
1073 SCNetworkInterfaceRef member
;
1077 member
= CFArrayGetValueAtIndex(members
, i
);
1078 ok
= SCNetworkInterfaceCopyMTU(member
, NULL
, &member_mtu_min
, &member_mtu_max
);
1080 if ((mtu_min
!= NULL
) && (*mtu_min
< member_mtu_min
)) {
1081 *mtu_min
= member_mtu_min
; // min MTU needs to be higher
1083 if ((mtu_max
!= NULL
) && (*mtu_max
> member_mtu_max
)) {
1084 *mtu_max
= member_mtu_max
; // max MTU needs to be lower
1092 if (mtu_min
!= NULL
) {
1093 #if IP_MSS > IPV6_MMTU
1094 if (*mtu_min
< IP_MSS
) {
1095 /* bump up the minimum MTU */
1096 *mtu_min
= IP_MSS
/*576*/;
1098 #else // IP_MSS > IPV6_MMTU
1099 if (*mtu_min
< IPV6_MMTU
) {
1100 /* bump up the minimum MTU */
1101 *mtu_min
= IPV6_MMTU
;
1103 #endif // IP_MSS > IPV6_MMTU
1105 if ((mtu_cur
!= NULL
) && (*mtu_min
> *mtu_cur
)) {
1106 /* min must be <= cur */
1107 *mtu_min
= *mtu_cur
;
1110 if ((mtu_max
!= NULL
) && (*mtu_min
> *mtu_max
)) {
1111 /* min must be <= max */
1112 *mtu_min
= *mtu_max
;
1127 SCNetworkInterfaceSetMediaOptions(SCNetworkInterfaceRef interface
,
1128 CFStringRef subtype
,
1131 CFDictionaryRef configuration
;
1132 CFMutableDictionaryRef newConfiguration
= NULL
;
1135 if (!isA_SCNetworkInterface(interface
)) {
1136 _SCErrorSet(kSCStatusInvalidArgument
);
1140 configuration
= SCNetworkInterfaceGetConfiguration(interface
);
1141 if (configuration
== NULL
) {
1142 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1144 &kCFTypeDictionaryKeyCallBacks
,
1145 &kCFTypeDictionaryValueCallBacks
);
1147 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1148 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1151 if (subtype
!= NULL
) {
1152 CFArrayRef available
= NULL
;
1153 CFArrayRef config_options
= options
;
1154 CFArrayRef subtypes
= NULL
;
1155 CFArrayRef subtype_options
= NULL
;
1157 if (options
== NULL
) {
1158 config_options
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
1161 if (!SCNetworkInterfaceCopyMediaOptions(interface
, NULL
, NULL
, &available
, FALSE
)) {
1162 SC_log(LOG_INFO
, "media type / options not available");
1166 if (available
== NULL
) {
1167 _SCErrorSet(kSCStatusInvalidArgument
);
1171 subtypes
= SCNetworkInterfaceCopyMediaSubTypes(available
);
1172 if ((subtypes
== NULL
) ||
1173 !CFArrayContainsValue(subtypes
,
1174 CFRangeMake(0, CFArrayGetCount(subtypes
)),
1176 SC_log(LOG_INFO
, "media type not valid");
1177 _SCErrorSet(kSCStatusInvalidArgument
);
1181 subtype_options
= SCNetworkInterfaceCopyMediaSubTypeOptions(available
, subtype
);
1182 if ((subtype_options
== NULL
) ||
1183 !CFArrayContainsValue(subtype_options
,
1184 CFRangeMake(0, CFArrayGetCount(subtype_options
)),
1186 SC_log(LOG_INFO
, "media options not valid for \"%@\"", subtype
);
1187 _SCErrorSet(kSCStatusInvalidArgument
);
1191 CFDictionarySetValue(newConfiguration
, kSCPropNetEthernetMediaSubType
, subtype
);
1192 CFDictionarySetValue(newConfiguration
,
1193 kSCPropNetEthernetMediaOptions
,
1194 (options
!= NULL
) ? options
: config_options
);
1200 if (available
!= NULL
) CFRelease(available
);
1201 if (subtypes
!= NULL
) CFRelease(subtypes
);
1202 if (subtype_options
!= NULL
) CFRelease(subtype_options
);
1203 if (options
== NULL
) CFRelease(config_options
);
1204 } else if (options
== NULL
) {
1205 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetEthernetMediaSubType
);
1206 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetEthernetMediaOptions
);
1207 if (CFDictionaryGetCount(newConfiguration
) == 0) {
1208 CFRelease(newConfiguration
);
1209 newConfiguration
= NULL
;
1213 SC_log(LOG_INFO
, "media type must be specified with options");
1214 _SCErrorSet(kSCStatusInvalidArgument
);
1218 ok
= SCNetworkInterfaceSetConfiguration(interface
, newConfiguration
);
1221 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1227 SCNetworkInterfaceSetMTU(SCNetworkInterfaceRef interface
,
1230 CFDictionaryRef configuration
;
1233 CFMutableDictionaryRef newConfiguration
= NULL
;
1236 if (!isA_SCNetworkInterface(interface
)) {
1237 _SCErrorSet(kSCStatusInvalidArgument
);
1241 if (!SCNetworkInterfaceCopyMTU(interface
, NULL
, &mtu_min
, &mtu_max
)) {
1242 SC_log(LOG_INFO
, "MTU bounds not available");
1246 configuration
= SCNetworkInterfaceGetConfiguration(interface
);
1247 if (configuration
== NULL
) {
1248 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1250 &kCFTypeDictionaryKeyCallBacks
,
1251 &kCFTypeDictionaryValueCallBacks
);
1253 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1254 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1257 if ((mtu
>= mtu_min
) && (mtu
<= mtu_max
)) {
1260 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &mtu
);
1261 CFDictionarySetValue(newConfiguration
, kSCPropNetEthernetMTU
, num
);
1264 } else if (mtu
== 0) {
1265 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetEthernetMTU
);
1266 if (CFDictionaryGetCount(newConfiguration
) == 0) {
1267 CFRelease(newConfiguration
);
1268 newConfiguration
= NULL
;
1272 SC_log(LOG_INFO
, "MTU out of range");
1273 _SCErrorSet(kSCStatusInvalidArgument
);
1277 ok
= SCNetworkInterfaceSetConfiguration(interface
, newConfiguration
);
1280 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);