2 * Copyright (c) 2001-2020 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 * May 20, 2006 Joe Liu <joe.liu@apple.com>
28 * Allan Nathanson <ajn@apple.com>
29 * - register interface by entryID (and not path)
31 * November 6, 2006 Allan Nathanson <ajn@apple.com>
32 * Dan Markarian <markarian@apple.com>
33 * Dieter Siegmund <dieter@apple.com>
34 * - updated code to name interfaces quicker (without need for
35 * calling IOKitWaitQuiet).
37 * October 3, 2003 Allan Nathanson <ajn@apple.com>
38 * - sort new interfaces by IOKit path (rather than MAC address) to
39 * help facilitate a more predictable interface-->name mapping for
40 * like hardware configurations.
42 * June 23, 2001 Allan Nathanson <ajn@apple.com>
43 * - update to public SystemConfiguration.framework APIs
45 * January 23, 2001 Dieter Siegmund <dieter@apple.com>
51 * - module that receives IOKit Network Interface messages
52 * and names any interface that currently does not have a name
53 * - uses Interface Type and MACAddress as the unique identifying
54 * keys; any interface that doesn't contain both of these properties
55 * is ignored and not processed
56 * - stores the Interface Type, MACAddress, and Unit in permanent storage
57 * to give persistent interface names
60 #include <TargetConditionals.h>
69 #endif // TARGET_OS_IPHONE
70 #include <sys/ioctl.h>
72 #include <sys/sysctl.h>
73 #include <sys/param.h>
74 #include <mach/mach.h>
75 #include <net/ethernet.h>
76 #include <net/if_types.h>
79 #include <CommonCrypto/CommonDigest.h>
81 #include <CoreFoundation/CoreFoundation.h>
83 #include <SystemConfiguration/SystemConfiguration.h>
84 #include <SystemConfiguration/SCDPlugin.h>
85 #include <SystemConfiguration/SCPrivate.h>
86 #include <SystemConfiguration/SCValidation.h>
87 #include "SCNetworkConfigurationInternal.h"
88 #include "SCPreferencesInternal.h"
89 #include "plugin_shared.h"
91 #include "InterfaceNamerControlPrefs.h"
92 #endif // !TARGET_OS_IPHONE
94 #ifdef TEST_INTERFACE_ASSIGNMENT
95 #undef INTERFACES_DEFAULT_CONFIG
96 #define INTERFACES_DEFAULT_CONFIG CFSTR("/tmp/ifnamer-test-NetworkInterfaces.plist")
97 #endif // TEST_INTERFACE_ASSIGNMENT
99 #include <IOKit/IOKitLib.h>
100 #include <IOKit/IOKitLibPrivate.h>
101 #include <IOKit/IOKitKeysPrivate.h>
102 #include <IOKit/IOBSD.h>
103 #include <IOKit/IOMessage.h>
104 #include <IOKit/network/IONetworkController.h>
105 #include <IOKit/network/IONetworkInterface.h>
106 #include <IOKit/network/IONetworkStack.h>
107 #include <IOKit/usb/USB.h>
109 #ifdef kIONetworkStackUserCommandKey
110 #define USE_REGISTRY_ENTRY_ID
113 #ifndef USE_REGISTRY_ENTRY_ID
114 // from <IOKit/network/IONetworkStack.h>
115 #define kIONetworkStackUserCommandKey "IONetworkStackUserCommand"
117 kRegisterInterfaceWithFixedUnit
= 0,
119 kRegisterAllInterfaces
121 #endif // !USE_REGISTRY_ENTRY_ID
123 #define MY_PLUGIN_NAME "InterfaceNamer"
124 #define MY_PLUGIN_ID CFSTR("com.apple.SystemConfiguration." MY_PLUGIN_NAME)
126 #define WAIT_STACK_TIMEOUT_KEY "WaitStackTimeout"
127 #define WAIT_STACK_TIMEOUT_DEFAULT 300.0
129 #define WAIT_QUIET_TIMEOUT_KEY "WaitQuietTimeout"
130 #define WAIT_QUIET_TIMEOUT_DEFAULT 240.0
134 * "IONetworkStack" connect object used to "name" an interface.
136 static io_connect_t S_connect
= MACH_PORT_NULL
;
140 * An array of CFDictionary's representing the interfaces
141 * that have been identified and [need to be] named.
143 static CFMutableArrayRef S_dblist
= NULL
;
147 * An array of SCNetworkInterface's representing the
148 * interfaces that have been identified.
150 static CFMutableArrayRef S_iflist
= NULL
;
154 * IOServiceAddMatchingNotification object used to watch for
155 * new network interfaces.
157 static io_iterator_t S_iter
= MACH_PORT_NULL
;
159 #if !TARGET_OS_IPHONE
162 * An array of CFData(WatchedInfo) objects representing those
163 * interfaces that have been connected to the system while
166 static CFMutableArrayRef S_locked
= NULL
;
167 #endif // !TARGET_OS_IPHONE
171 * notification object for receiving IOKit notifications of
172 * new devices or state changes.
174 static IONotificationPortRef S_notify
= NULL
;
178 * An array of CFData(WatchedInfo) objects representing those
179 * pre-configured interfaces that have been connected to the
182 static CFMutableArrayRef S_preconfigured
= NULL
;
184 /* S_prev_active_list
185 * An array of CFDictionary's representing the previously
188 static CFMutableArrayRef S_prev_active_list
= NULL
;
192 * IOServiceAddInterestNotification object used to watch for
193 * IOKit matching to quiesce.
195 static io_object_t S_quiet
= MACH_PORT_NULL
;
199 * IOServiceAddMatchingNotification object used to watch for
200 * the availability of the "IONetworkStack" object.
202 static io_iterator_t S_stack
= MACH_PORT_NULL
;
206 * A dictionary containing Information about each network
207 * interface. For now, the key is the BSD name and the
208 * value is a CFNumber noting how long (in milliseconds)
209 * it took for the interface to be recognized/named.
211 static CFMutableDictionaryRef S_state
= NULL
;
215 * S_trustedHostAttached
217 * Note: this global must only be updated on trustRequired_queue()
219 static Boolean S_trustedHostAttached
= FALSE
;
223 * Note: this global must only be updated on trustRequired_queue()
225 static CFIndex S_trustedHostCount
= 0;
229 * An array of CFData(WatchedInfo) objects representing those
230 * interfaces that require [lockdownd] trust.
232 static CFMutableArrayRef S_trustRequired
= NULL
;
233 #endif // TARGET_OS_IPHONE
237 * CFRunLoopTimer tracking how long we are willing to wait
238 * for IOKit matching to quiesce (IOKitWaitQuiet).
241 * time to wait for the IONetworkStack object to appear before timeout
244 * time to wait for the IOKit to quiesce (after the IONetworkStack is
247 static CFRunLoopTimerRef S_timer
= NULL
;
248 static double S_stack_timeout
= WAIT_STACK_TIMEOUT_DEFAULT
;
249 static double S_quiet_timeout
= WAIT_QUIET_TIMEOUT_DEFAULT
;
252 * Virtual network interface configuration
253 * S_prefs : SCPreferences to configuration
254 * S_bonds : most recently actived Bond configuration
255 * S_bridges : most recently actived Bridge configuration
256 * S_vlans : most recently actived VLAN configuration
258 static SCPreferencesRef S_prefs
= NULL
;
259 static CFArrayRef S_bonds
= NULL
;
260 static CFArrayRef S_bridges
= NULL
;
261 static CFArrayRef S_vlans
= NULL
;
268 __log_InterfaceNamer(void)
270 static os_log_t log
= NULL
;
273 log
= os_log_create("com.apple.SystemConfiguration", "InterfaceNamer");
281 addTimestamp(CFMutableDictionaryRef dict
, CFStringRef key
)
286 now
= CFAbsoluteTimeGetCurrent();
287 val
= CFNumberCreate(NULL
, kCFNumberDoubleType
, &now
);
288 CFDictionaryAddValue(dict
, key
, val
);
293 static CFComparisonResult
294 if_unit_compare(const void *val1
, const void *val2
, void *context
)
296 #pragma unused(context)
297 CFComparisonResult res
;
303 type1
= CFDictionaryGetValue((CFDictionaryRef
)val1
,
304 CFSTR(kIOInterfaceType
));
305 type2
= CFDictionaryGetValue((CFDictionaryRef
)val2
,
306 CFSTR(kIOInterfaceType
));
307 res
= CFNumberCompare(type1
, type2
, NULL
);
308 if (res
!= kCFCompareEqualTo
) {
311 unit1
= CFDictionaryGetValue((CFDictionaryRef
)val1
,
312 CFSTR(kIOInterfaceUnit
));
313 unit2
= CFDictionaryGetValue((CFDictionaryRef
)val2
,
314 CFSTR(kIOInterfaceUnit
));
315 return (CFNumberCompare(unit1
, unit2
, NULL
));
319 writeInterfaceListForModel(SCPreferencesRef prefs
, CFStringRef old_model
)
322 CFPropertyListRef plist
;
323 SCPreferencesRef savedPrefs
;
324 CFStringRef savedPrefsID
;
326 savedPrefsID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@-%@"),
327 INTERFACES_DEFAULT_CONFIG
,
329 savedPrefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":writeInterfaceListForModel"), savedPrefsID
);
330 CFRelease(savedPrefsID
);
331 if (savedPrefs
== NULL
) {
332 SC_log(LOG_NOTICE
, "SCPreferencesCreate(\"NetworkInterfaces-<model>.plist\") failed: %s", SCErrorString(SCError()));
336 plist
= SCPreferencesPathGetValue(prefs
, CFSTR("/"));
337 ok
= SCPreferencesPathSetValue(savedPrefs
, CFSTR("/"), plist
);
339 SC_log(LOG_NOTICE
, "SCPreferencesPathSetValue() failed: %s", SCErrorString(SCError()));
342 ok
= SCPreferencesCommitChanges(savedPrefs
);
343 CFRelease(savedPrefs
);
345 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges(\"NetworkInterfaces-<model>.plist\") failed: %s", SCErrorString(SCError()));
352 writeInterfaceList(CFArrayRef if_list
)
355 CFStringRef new_model
;
356 SCPreferencesRef ni_prefs
;
357 CFStringRef old_model
;
359 if (!isA_CFArray(if_list
)) {
363 ni_prefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":writeInterfaceList"), INTERFACES_DEFAULT_CONFIG
);
364 if (ni_prefs
== NULL
) {
365 SC_log(LOG_NOTICE
, "SCPreferencesCreate(\"NetworkInterfaces.plist\") failed: %s", SCErrorString(SCError()));
369 cur_list
= SCPreferencesGetValue(ni_prefs
, INTERFACES
);
370 if (_SC_CFEqual(cur_list
, if_list
)) {
374 old_model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
375 new_model
= _SC_hw_model(FALSE
);
376 if ((old_model
!= NULL
) && !_SC_CFEqual(old_model
, new_model
)) {
378 if ((old_model
!= NULL
) && (cur_list
!= NULL
)) {
379 SC_log(LOG_NOTICE
, "Hardware model changed\n"
380 " created on \"%@\"\n"
385 // save the interface list that was created on "other" hardware
386 writeInterfaceListForModel(ni_prefs
, old_model
);
389 SCPreferencesSetValue(ni_prefs
, MODEL
, new_model
);
392 SCPreferencesSetValue(ni_prefs
, INTERFACES
, if_list
);
394 if (cur_list
== NULL
) {
395 const int new_version
= NETWORK_CONFIGURATION_VERSION
;
398 version
= CFNumberCreate(NULL
, kCFNumberIntType
, &new_version
);
399 SCPreferencesSetValue(ni_prefs
, kSCPrefVersion
, version
);
403 if (!SCPreferencesCommitChanges(ni_prefs
)) {
404 if (SCError() != EROFS
) {
405 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges() failed: %s", SCErrorString(SCError()));
416 static CF_RETURNS_RETAINED CFMutableArrayRef
419 CFMutableArrayRef db_list
= NULL
;
421 SCPreferencesRef ni_prefs
;
422 CFStringRef old_model
;
424 ni_prefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":readInterfaceList"), INTERFACES_DEFAULT_CONFIG
);
425 if (ni_prefs
== NULL
) {
426 SC_log(LOG_NOTICE
, "SCPreferencesCreate() failed: %s", SCErrorString(SCError()));
430 if_list
= SCPreferencesGetValue(ni_prefs
, INTERFACES
);
431 if_list
= isA_CFArray(if_list
);
433 old_model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
434 if (old_model
!= NULL
) {
435 CFStringRef new_model
;
437 new_model
= _SC_hw_model(FALSE
);
438 if (!_SC_CFEqual(old_model
, new_model
)) {
440 * If the interface list was created on other hardware then
447 if (if_list
!= NULL
) {
448 CFIndex n
= CFArrayGetCount(if_list
);
450 db_list
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
451 for (CFIndex i
= 0; i
< n
; i
++) {
452 CFDictionaryRef dict
;
454 dict
= CFArrayGetValueAtIndex(if_list
, i
);
455 if (isA_CFDictionary(dict
) &&
456 CFDictionaryContainsKey(dict
, CFSTR(kIOInterfaceType
)) &&
457 CFDictionaryContainsKey(dict
, CFSTR(kIOInterfaceUnit
)) &&
458 CFDictionaryContainsKey(dict
, CFSTR(kIOMACAddress
))) {
459 CFArrayAppendValue(db_list
, dict
);
464 if (db_list
!= NULL
) {
465 CFIndex n
= CFArrayGetCount(db_list
);
468 CFArraySortValues(db_list
, CFRangeMake(0, n
), if_unit_compare
, NULL
);
476 static CF_RETURNS_RETAINED CFMutableArrayRef
477 previouslyActiveInterfaces()
479 CFMutableArrayRef active
;
482 if (S_dblist
== NULL
) {
486 active
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
488 n
= CFArrayGetCount(S_dblist
);
489 for (CFIndex i
= 0; i
< n
; i
++) {
490 CFDictionaryRef if_dict
;
492 if_dict
= CFArrayGetValueAtIndex(S_dblist
, i
);
493 if (CFDictionaryContainsKey(if_dict
, CFSTR(kSCNetworkInterfaceActive
))) {
494 CFMutableDictionaryRef new_dict
;
496 new_dict
= CFDictionaryCreateMutableCopy(NULL
, 0, if_dict
);
497 CFDictionaryRemoveValue(new_dict
, CFSTR(kSCNetworkInterfaceActive
));
498 CFArraySetValueAtIndex(S_dblist
, i
, new_dict
);
499 CFArrayAppendValue(active
, new_dict
);
508 updateInterfaces(void);
515 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@" MY_PLUGIN_NAME
), kSCDynamicStoreDomainPlugin
);
516 (void)SCDynamicStoreSetValue(NULL
, key
, S_state
);
522 #if !TARGET_OS_IPHONE
524 updateBondInterfaceConfiguration(SCPreferencesRef prefs
)
526 CFArrayRef interfaces
;
528 interfaces
= SCBondInterfaceCopyAll(prefs
);
529 if ((interfaces
!= NULL
) && (CFArrayGetCount(interfaces
) == 0)) {
530 CFRelease(interfaces
);
534 if (_SC_CFEqual(S_bonds
, interfaces
)) {
536 if (interfaces
!= NULL
) CFRelease(interfaces
);
540 if (S_bonds
!= NULL
) CFRelease(S_bonds
);
541 S_bonds
= interfaces
;
543 if (!_SCBondInterfaceUpdateConfiguration(prefs
)) {
544 SC_log(LOG_NOTICE
, "_SCBondInterfaceUpdateConfiguration() failed: %s",
545 SCErrorString(SCError()));
550 #endif // !TARGET_OS_IPHONE
553 updateBridgeInterfaceConfiguration(SCPreferencesRef prefs
)
555 CFArrayRef interfaces
;
557 interfaces
= SCBridgeInterfaceCopyAll(prefs
);
558 if ((interfaces
!= NULL
) && (CFArrayGetCount(interfaces
) == 0)) {
559 CFRelease(interfaces
);
563 if (_SC_CFEqual(S_bridges
, interfaces
)) {
565 if (interfaces
!= NULL
) CFRelease(interfaces
);
569 if (S_bridges
!= NULL
) CFRelease(S_bridges
);
570 S_bridges
= interfaces
;
572 if (!_SCBridgeInterfaceUpdateConfiguration(prefs
)) {
573 SC_log(LOG_NOTICE
, "_SCBridgeInterfaceUpdateConfiguration() failed: %s",
574 SCErrorString(SCError()));
581 updateVLANInterfaceConfiguration(SCPreferencesRef prefs
)
583 CFArrayRef interfaces
;
585 interfaces
= SCVLANInterfaceCopyAll(prefs
);
586 if ((interfaces
!= NULL
) && (CFArrayGetCount(interfaces
) == 0)) {
587 CFRelease(interfaces
);
591 if (_SC_CFEqual(S_vlans
, interfaces
)) {
593 if (interfaces
!= NULL
) CFRelease(interfaces
);
597 if (S_vlans
!= NULL
) CFRelease(S_vlans
);
598 S_vlans
= interfaces
;
600 if (!_SCVLANInterfaceUpdateConfiguration(prefs
)) {
601 SC_log(LOG_NOTICE
, "_SCVLANInterfaceUpdateConfiguration() failed: %s",
602 SCErrorString(SCError()));
609 updateVirtualNetworkInterfaceConfiguration(SCPreferencesRef prefs
,
610 SCPreferencesNotification notificationType
,
614 if ((notificationType
& kSCPreferencesNotificationApply
) != kSCPreferencesNotificationApply
) {
619 // if a new interface has been "named"
621 if (S_bonds
!= NULL
) {
625 if (S_bridges
!= NULL
) {
626 CFRelease(S_bridges
);
629 if (S_vlans
!= NULL
) {
635 #if !TARGET_OS_IPHONE
636 updateBondInterfaceConfiguration (prefs
);
637 #endif // !TARGET_OS_IPHONE
638 updateBridgeInterfaceConfiguration(prefs
);
639 updateVLANInterfaceConfiguration (prefs
);
641 // we are finished with current prefs, wait for changes
642 SCPreferencesSynchronize(prefs
);
650 updateBTPANInformation(const void *value
, void *context
)
652 #pragma unused(context)
654 CFDictionaryRef dict
= (CFDictionaryRef
)value
;
656 CFDictionaryRef info
;
659 if_name
= CFDictionaryGetValue(dict
, CFSTR(kIOBSDNameKey
));
660 if (!isA_CFString(if_name
)) {
665 info
= CFDictionaryGetValue(dict
, CFSTR(kSCNetworkInterfaceInfo
));
666 if (!isA_CFDictionary(info
)) {
667 // if no SCNetworkInterface info
671 name
= CFDictionaryGetValue(info
, kSCPropUserDefinedName
);
672 if (!isA_CFString(name
) || !CFEqual(name
, CFSTR(BT_PAN_NAME
))) {
673 // if not BT-PAN interface
677 CFDictionaryAddValue(S_state
, kInterfaceNamerKey_BT_PAN_Name
, if_name
);
679 addr
= CFDictionaryGetValue(dict
, CFSTR(kIOMACAddress
));
680 if (isA_CFData(addr
)) {
681 CFDictionaryAddValue(S_state
, kInterfaceNamerKey_BT_PAN_Mac
, addr
);
686 #endif // TARGET_OS_OSX
688 static CFDictionaryRef
689 createInterfaceDict(SCNetworkInterfaceRef interface
, CFArrayRef matchingMACs
)
691 CFMutableDictionaryRef new_if
;
694 new_if
= CFDictionaryCreateMutable(NULL
,
696 &kCFTypeDictionaryKeyCallBacks
,
697 &kCFTypeDictionaryValueCallBacks
);
699 val
= _SCNetworkInterfaceCopyInterfaceInfo(interface
);
701 CFDictionarySetValue(new_if
, CFSTR(kSCNetworkInterfaceInfo
), val
);
705 val
= _SCNetworkInterfaceGetIOPath(interface
);
707 CFDictionarySetValue(new_if
, CFSTR(kIOPathMatchKey
), val
);
710 val
= _SCNetworkInterfaceGetIOInterfaceNamePrefix(interface
);
712 CFDictionarySetValue(new_if
, CFSTR(kIOInterfaceNamePrefix
), val
);
715 val
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
717 CFDictionarySetValue(new_if
, CFSTR(kIOInterfaceType
), val
);
720 val
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
722 CFDictionarySetValue(new_if
, CFSTR(kIOInterfaceUnit
), val
);
725 val
= _SCNetworkInterfaceGetHardwareAddress(interface
);
727 CFDictionarySetValue(new_if
, CFSTR(kIOMACAddress
), val
);
730 val
= SCNetworkInterfaceGetBSDName(interface
);
732 CFDictionarySetValue(new_if
, CFSTR(kIOBSDNameKey
), val
);
735 val
= SCNetworkInterfaceGetInterfaceType(interface
);
737 CFDictionarySetValue(new_if
, CFSTR(kSCNetworkInterfaceType
), val
);
740 CFDictionarySetValue(new_if
,
742 _SCNetworkInterfaceIsBuiltin(interface
) ? kCFBooleanTrue
: kCFBooleanFalse
);
744 if (_SCNetworkInterfaceIsHiddenConfiguration(interface
)) {
745 CFDictionarySetValue(new_if
, kSCNetworkInterfaceHiddenConfigurationKey
, kCFBooleanTrue
);
748 CFDictionarySetValue(new_if
, CFSTR(kSCNetworkInterfaceActive
), kCFBooleanTrue
);
750 if (matchingMACs
!= NULL
) {
751 CFDictionarySetValue(new_if
, CFSTR(kSCNetworkInterfaceMatchingMACs
), matchingMACs
);
757 static CFDictionaryRef
758 lookupInterfaceByAddress(CFArrayRef db_list
, SCNetworkInterfaceRef interface
, CFIndex
* where
)
764 if (db_list
== NULL
) {
767 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
768 addr
= _SCNetworkInterfaceGetHardwareAddress(interface
);
769 if (type
== NULL
|| addr
== NULL
) {
773 n
= CFArrayGetCount(db_list
);
774 for (CFIndex i
= 0; i
< n
; i
++) {
776 CFDictionaryRef dict
= CFArrayGetValueAtIndex(db_list
, i
);
779 t
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
780 a
= CFDictionaryGetValue(dict
, CFSTR(kIOMACAddress
));
781 if (t
== NULL
|| a
== NULL
)
784 if (CFEqual(type
, t
) && CFEqual(addr
, a
)) {
794 static CFDictionaryRef
795 lookupInterfaceByName(CFArrayRef db_list
, CFStringRef bsdName
, CFIndex
* where
)
799 if (db_list
== NULL
) {
803 n
= CFArrayGetCount(db_list
);
804 for (CFIndex i
= 0; i
< n
; i
++) {
805 CFDictionaryRef dict
= CFArrayGetValueAtIndex(db_list
, i
);
808 name
= CFDictionaryGetValue(dict
, CFSTR(kIOBSDNameKey
));
809 if (_SC_CFEqual(name
, bsdName
)) {
819 static CFDictionaryRef
820 lookupInterfaceByUnit(CFArrayRef db_list
, SCNetworkInterfaceRef interface
, CFIndex
* where
)
826 if (db_list
== NULL
) {
829 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
830 unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
831 if (type
== NULL
|| unit
== NULL
) {
835 n
= CFArrayGetCount(db_list
);
836 for (CFIndex i
= 0; i
< n
; i
++) {
837 CFDictionaryRef dict
= CFArrayGetValueAtIndex(db_list
, i
);
841 t
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
842 u
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceUnit
));
843 if (t
== NULL
|| u
== NULL
) {
847 if (CFEqual(type
, t
) && CFEqual(unit
, u
)) {
857 CFDictionaryRef match_info
;
858 CFStringRef match_type
;
859 CFBooleanRef match_builtin
;
860 CFMutableArrayRef matches
;
861 } matchContext
, *matchContextRef
;
863 static CF_RETURNS_RETAINED CFDictionaryRef
864 thinInterfaceInfo(CFDictionaryRef info
)
869 if (CFDictionaryGetValueIfPresent(info
, CFSTR(kUSBVendorID
), (const void **)&num
)
871 && CFNumberGetValue(num
, kCFNumberIntType
, &vid
)
872 && (vid
== kIOUSBAppleVendorID
)) {
873 CFMutableDictionaryRef thin
;
875 // if this is an Apple USB device than we trust that
876 // the non-localized name will be correct.
877 thin
= CFDictionaryCreateMutableCopy(NULL
, 0, info
);
878 CFDictionaryRemoveValue(thin
, CFSTR(kUSBProductString
));
879 CFDictionaryRemoveValue(thin
, CFSTR(kUSBVendorID
));
880 CFDictionaryRemoveValue(thin
, CFSTR(kUSBProductID
));
884 return CFRetain(info
);
888 matchInterfaceInfo(CFDictionaryRef known_info
, CFDictionaryRef match_info
)
892 match
= _SC_CFEqual(known_info
, match_info
);
894 isA_CFDictionary(known_info
) &&
895 isA_CFDictionary(match_info
)) {
897 // if not an exact match, try thinning
898 known_info
= thinInterfaceInfo(known_info
);
899 match_info
= thinInterfaceInfo(match_info
);
900 match
= _SC_CFEqual(known_info
, match_info
);
901 if (known_info
!= NULL
) CFRelease(known_info
);
902 if (match_info
!= NULL
) CFRelease(match_info
);
909 matchKnown(const void *value
, void *context
)
911 CFDictionaryRef known_dict
= (CFDictionaryRef
)value
;
912 matchContextRef match_context
= (matchContextRef
)context
;
914 // match interface type
916 CFStringRef known_type
;
918 known_type
= CFDictionaryGetValue(known_dict
, CFSTR(kSCNetworkInterfaceType
));
919 if (!_SC_CFEqual(known_type
, match_context
->match_type
)) {
924 // match SCNetworkInterfaceInfo
926 CFDictionaryRef known_info
;
928 known_info
= CFDictionaryGetValue(known_dict
, CFSTR(kSCNetworkInterfaceInfo
));
929 if (!matchInterfaceInfo(known_info
, match_context
->match_info
)) {
934 // if requested, match [non-]builtin
935 if (match_context
->match_builtin
!= NULL
) {
936 CFBooleanRef known_builtin
;
938 known_builtin
= CFDictionaryGetValue(known_dict
, CFSTR(kIOBuiltin
));
939 if (!isA_CFBoolean(known_builtin
)) {
940 known_builtin
= kCFBooleanFalse
;
942 if (!_SC_CFEqual(known_builtin
, match_context
->match_builtin
)) {
947 // if we have a match
948 if (match_context
->matches
== NULL
) {
949 match_context
->matches
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
951 CFArrayAppendValue(match_context
->matches
, known_dict
);
957 matchUnnamed(const void *value
, void *context
)
959 SCNetworkInterfaceRef known_if
= (SCNetworkInterfaceRef
)value
;
960 matchContextRef match_context
= (matchContextRef
)context
;
962 if (match_context
->matches
== NULL
) {
966 // match interface type
968 CFStringRef known_type
;
970 known_type
= SCNetworkInterfaceGetInterfaceType(known_if
);
971 if (!_SC_CFEqual(known_type
, match_context
->match_type
)) {
976 // match SCNetworkInterfaceInfo
978 CFDictionaryRef known_info
;
981 known_info
= _SCNetworkInterfaceCopyInterfaceInfo(known_if
);
982 match
= matchInterfaceInfo(known_info
, match_context
->match_info
);
983 if (known_info
!= NULL
) CFRelease(known_info
);
989 // if requested, match [non-]builtin
990 if (match_context
->match_builtin
!= NULL
) {
991 CFBooleanRef known_builtin
;
993 known_builtin
= _SCNetworkInterfaceIsBuiltin(known_if
) ? kCFBooleanTrue
995 if (!_SC_CFEqual(known_builtin
, match_context
->match_builtin
)) {
1000 // if we have a match
1001 CFRelease(match_context
->matches
);
1002 match_context
->matches
= NULL
;
1008 interfaceExists(CFStringRef prefix
, CFNumberRef unit
)
1010 Boolean found
= FALSE
;
1011 CFDictionaryRef match_dict
;
1012 CFStringRef match_keys
[2];
1013 CFTypeRef match_vals
[2];
1014 CFDictionaryRef matching
;
1018 io_registry_entry_t entry
= MACH_PORT_NULL
;
1019 io_iterator_t iterator
= MACH_PORT_NULL
;
1021 mach_port_t masterPort
= MACH_PORT_NULL
;
1023 kr
= IOMasterPort(bootstrap_port
, &masterPort
);
1024 if (kr
!= KERN_SUCCESS
) {
1025 SC_log(LOG_ERR
, "IOMasterPort returned 0x%x", kr
);
1029 // look for kIONetworkInterface with matching prefix and unit
1030 match_keys
[0] = CFSTR(kIOInterfaceNamePrefix
);
1031 match_vals
[0] = prefix
;
1032 match_keys
[1] = CFSTR(kIOInterfaceUnit
);
1033 match_vals
[1] = unit
;
1034 match_dict
= CFDictionaryCreate(NULL
,
1035 (const void **)match_keys
,
1036 (const void **)match_vals
,
1038 &kCFTypeDictionaryKeyCallBacks
,
1039 &kCFTypeDictionaryValueCallBacks
);
1041 match_keys
[0] = CFSTR(kIOProviderClassKey
);
1042 match_vals
[0] = CFSTR(kIONetworkInterfaceClass
);
1043 match_keys
[1] = CFSTR(kIOPropertyMatchKey
);
1044 match_vals
[1] = match_dict
;
1045 matching
= CFDictionaryCreate(NULL
,
1046 (const void **)match_keys
,
1047 (const void **)match_vals
,
1048 sizeof(match_keys
)/sizeof(match_keys
[0]),
1049 &kCFTypeDictionaryKeyCallBacks
,
1050 &kCFTypeDictionaryValueCallBacks
);
1051 CFRelease(match_dict
);
1053 // note: the "matching" dictionary will be consumed by the following
1054 kr
= IOServiceGetMatchingServices(masterPort
, matching
, &iterator
);
1055 if ((kr
!= kIOReturnSuccess
) || (iterator
== MACH_PORT_NULL
)) {
1060 entry
= IOIteratorNext(iterator
);
1061 if (entry
== MACH_PORT_NULL
) {
1069 if (masterPort
!= MACH_PORT_NULL
) {
1070 mach_port_deallocate(mach_task_self(), masterPort
);
1072 if (entry
!= MACH_PORT_NULL
) {
1073 IOObjectRelease(entry
);
1075 if (iterator
!= MACH_PORT_NULL
) {
1076 IOObjectRelease(iterator
);
1083 * lookupMatchingInterface
1085 * Looks at the interfaces that have already been [or need to be] named with
1086 * the goal of allowing a system using a single network interface/adaptor of
1087 * a given type (vendor, model, ...) to not care about the specific adaptor
1088 * that is used (i.e. swapping dongle's is OK). Once a system has had more
1089 * than one interface/adaptor connected at the same time than we assume that
1090 * the network configuration is being setup for multi-homing that should be
1093 * If no matches are found or if more than one match is found, return NULL.
1094 * If a single match is found, return the match.
1096 static CFDictionaryRef
1097 lookupMatchingInterface(SCNetworkInterfaceRef interface
,
1098 CFArrayRef db_list
, // already named
1099 CFArrayRef if_list
, // to be named
1100 CFIndex if_list_index
,
1101 CFBooleanRef builtin
)
1103 CFStringRef if_type
;
1104 CFDictionaryRef match
= NULL
;
1105 matchContext match_context
;
1107 if_type
= SCNetworkInterfaceGetInterfaceType(interface
);
1108 if (if_type
== NULL
) {
1112 match_context
.match_type
= if_type
;
1113 match_context
.match_info
= _SCNetworkInterfaceCopyInterfaceInfo(interface
);
1114 match_context
.match_builtin
= builtin
;
1115 match_context
.matches
= NULL
;
1117 // check for matches to interfaces that have already been named
1118 // ... and append each match that we find to match_context.matches
1119 if (db_list
!= NULL
) {
1120 CFArrayApplyFunction(db_list
,
1121 CFRangeMake(0, CFArrayGetCount(db_list
)),
1126 // check for matches to interfaces that will be named
1127 // ... and CFRelease match_context.matches if we find another network
1128 // interface of the same type that also needs to be named
1129 if (if_list
!= NULL
) {
1130 CFIndex if_list_count
;
1132 if_list_count
= CFArrayGetCount(if_list
);
1133 if (if_list_index
< if_list_count
) {
1134 CFArrayApplyFunction(if_list
,
1135 CFRangeMake(if_list_index
, if_list_count
- if_list_index
),
1141 // check if we have a single match
1142 if (match_context
.matches
!= NULL
) {
1143 if (CFArrayGetCount(match_context
.matches
) == 1) {
1144 match
= CFArrayGetValueAtIndex(match_context
.matches
, 0);
1146 CFRelease(match_context
.matches
);
1149 if (match
!= NULL
) {
1150 Boolean active
= TRUE
;
1153 name
= CFDictionaryGetValue(match
, CFSTR(kIOBSDNameKey
));
1154 if (isA_CFString(name
)) {
1158 prefix
= CFDictionaryGetValue(match
, CFSTR(kIOInterfaceNamePrefix
));
1159 unit
= CFDictionaryGetValue(match
, CFSTR(kIOInterfaceUnit
));
1160 if (isA_CFString(prefix
) && isA_CFNumber(unit
)) {
1161 if (!interfaceExists(prefix
, unit
)) {
1172 if (match_context
.match_info
!= NULL
) CFRelease(match_context
.match_info
);
1177 insertInterface(CFMutableArrayRef db_list
, SCNetworkInterfaceRef interface
, CFDictionaryRef db_dict_match
)
1180 CFDictionaryRef if_dict
;
1181 CFStringRef if_name
;
1182 CFNumberRef if_type
;
1183 CFNumberRef if_unit
;
1184 CFArrayRef matchingMACs
= NULL
;
1185 CFIndex n
= CFArrayGetCount(db_list
);
1186 CFComparisonResult res
;
1188 if_name
= SCNetworkInterfaceGetBSDName(interface
);
1189 if (if_name
!= NULL
) {
1190 addTimestamp(S_state
, if_name
);
1193 if (!_SCNetworkInterfaceIsBuiltin(interface
) && (db_dict_match
!= NULL
)) {
1197 matchingMACs
= CFDictionaryGetValue(db_dict_match
, CFSTR(kSCNetworkInterfaceMatchingMACs
));
1198 if (matchingMACs
!= NULL
) {
1199 CFRetain(matchingMACs
);
1202 addr_old
= CFDictionaryGetValue(db_dict_match
, CFSTR(kIOMACAddress
));
1203 addr_cur
= _SCNetworkInterfaceGetHardwareAddress(interface
);
1204 if ((addr_old
!= NULL
) && (addr_cur
!= NULL
) && !CFEqual(addr_old
, addr_cur
)) {
1205 CFMutableArrayRef matching_new
;
1207 // if MAC address changed, add previous MAC to history
1208 if (matchingMACs
!= NULL
) {
1209 matching_new
= CFArrayCreateMutableCopy(NULL
, 0, matchingMACs
);
1210 CFRelease(matchingMACs
);
1212 // remove duplicates of the now current MAC from history
1213 i
= CFArrayGetFirstIndexOfValue(matching_new
, CFRangeMake(0, CFArrayGetCount(matching_new
)), addr_cur
);
1214 if (i
!= kCFNotFound
) {
1215 CFArrayRemoveValueAtIndex(matching_new
, i
);
1218 // remove duplicates of the previous MAC from history before re-inserting
1219 i
= CFArrayGetFirstIndexOfValue(matching_new
, CFRangeMake(0, CFArrayGetCount(matching_new
)), addr_old
);
1220 if (i
!= kCFNotFound
) {
1221 CFArrayRemoveValueAtIndex(matching_new
, i
);
1224 matching_new
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1226 CFArrayInsertValueAtIndex(matching_new
, 0, addr_old
);
1228 // limit history size
1229 #define MATCHING_HISTORY_MAXLEN 32
1230 for (i
= CFArrayGetCount(matching_new
); i
> MATCHING_HISTORY_MAXLEN
; i
--) {
1231 CFArrayRemoveValueAtIndex(matching_new
, i
- 1);
1234 matchingMACs
= matching_new
;
1238 if_dict
= createInterfaceDict(interface
, matchingMACs
);
1239 if (matchingMACs
!= NULL
) {
1240 CFRelease(matchingMACs
);
1243 if_type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
1244 if_unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
1245 if ((if_type
== NULL
) || (if_unit
== NULL
)) {
1250 for (i
= 0; i
< n
; i
++) {
1251 CFNumberRef db_type
;
1252 CFNumberRef db_unit
;
1253 CFDictionaryRef dict
= CFArrayGetValueAtIndex(db_list
, i
);
1255 db_type
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
1256 db_unit
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceUnit
));
1257 res
= CFNumberCompare(if_type
, db_type
, NULL
);
1258 if (res
== kCFCompareLessThan
1259 || (res
== kCFCompareEqualTo
1260 && (CFNumberCompare(if_unit
, db_unit
, NULL
)
1261 == kCFCompareLessThan
))) {
1262 CFArrayInsertValueAtIndex(db_list
, i
, if_dict
);
1268 CFArrayAppendValue(S_dblist
, if_dict
);
1271 updateBTPANInformation(if_dict
, NULL
);
1272 #endif // TARGET_OS_OSX
1279 removeInterface(CFMutableArrayRef db_list
, SCNetworkInterfaceRef interface
, CFDictionaryRef
*matched
)
1281 CFDictionaryRef db_dict
;
1285 // remove any dict that has our type/addr
1287 db_dict
= lookupInterfaceByAddress(db_list
, interface
, &where
);
1288 if (db_dict
== NULL
) {
1291 if ((matched
!= NULL
) && (*matched
== NULL
)) {
1292 *matched
= CFRetain(db_dict
);
1294 CFArrayRemoveValueAtIndex(db_list
, where
);
1298 // remove any dict that has the same type/unit
1300 db_dict
= lookupInterfaceByUnit(db_list
, interface
, &where
);
1301 if (db_dict
== NULL
) {
1304 if ((matched
!= NULL
) && (*matched
== NULL
)) {
1305 *matched
= CFRetain(db_dict
);
1307 CFArrayRemoveValueAtIndex(db_list
, where
);
1312 SC_log(LOG_ERR
, "Multiple interfaces removed from database (n = %d, %@)", n
, interface
);
1319 replaceInterface(SCNetworkInterfaceRef interface
)
1321 CFDictionaryRef matched
= NULL
;
1323 if (S_dblist
== NULL
) {
1324 S_dblist
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1326 // remove any matching interfaces
1327 removeInterface(S_dblist
, interface
, &matched
);
1330 // [re-]insert the new interface
1331 insertInterface(S_dblist
, interface
, matched
);
1333 if (matched
!= NULL
) {
1341 getNextUnitForType(CFNumberRef if_type
, int requested
)
1345 if (S_dblist
== NULL
) {
1349 n
= CFArrayGetCount(S_dblist
);
1350 for (CFIndex i
= 0; i
< n
; i
++) {
1351 CFDictionaryRef dict
= CFArrayGetValueAtIndex(S_dblist
, i
);
1354 type
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
1355 if (CFEqual(type
, if_type
)) {
1359 unit
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceUnit
));
1360 if (!isA_CFNumber(unit
) ||
1361 !CFNumberGetValue(unit
, kCFNumberIntType
, &u
)) {
1365 if (u
< requested
) {
1366 // if we have not yet found our starting unit #
1370 if (u
== requested
) {
1371 // our starting (or now proposed) unit # is "in use" so
1372 // let's keep searching
1377 // we've found a unit # gap ... so let's re-assign it!
1385 * Function: ensureInterfaceHasUnit
1387 * Ensure that the SCNetworkInterfaceRef has a unit number. If it doesn't,
1388 * release the interface and return NULL.
1390 static SCNetworkInterfaceRef
1391 ensureInterfaceHasUnit(SCNetworkInterfaceRef net_if
)
1394 && _SCNetworkInterfaceGetIOInterfaceUnit(net_if
) == NULL
) {
1401 #ifdef USE_REGISTRY_ENTRY_ID
1402 static kern_return_t
1403 registerInterfaceWithIORegistryEntryID(io_connect_t connect
,
1409 CFMutableDictionaryRef dict
;
1413 dict
= CFDictionaryCreateMutable(NULL
, 0,
1414 &kCFTypeDictionaryKeyCallBacks
,
1415 &kCFTypeDictionaryValueCallBacks
);
1416 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &command
);
1417 CFDictionarySetValue(dict
, CFSTR(kIONetworkStackUserCommandKey
), num
);
1419 data
= CFDataCreate(NULL
, (void *) &entryID
, sizeof(entryID
));
1420 CFDictionarySetValue(dict
, CFSTR(kIORegistryEntryIDKey
), data
);
1422 CFDictionarySetValue(dict
, CFSTR(kIOInterfaceUnit
), unit
);
1423 kr
= IOConnectSetCFProperties(connect
, dict
);
1428 static SCNetworkInterfaceRef
1429 copyInterfaceForIORegistryEntryID(uint64_t entryID
)
1431 io_registry_entry_t entry
= MACH_PORT_NULL
;
1432 SCNetworkInterfaceRef interface
= NULL
;
1433 io_iterator_t iterator
= MACH_PORT_NULL
;
1435 mach_port_t masterPort
= MACH_PORT_NULL
;
1437 kr
= IOMasterPort(bootstrap_port
, &masterPort
);
1438 if (kr
!= KERN_SUCCESS
) {
1439 SC_log(LOG_ERR
, "IOMasterPort returned 0x%x", kr
);
1443 kr
= IOServiceGetMatchingServices(masterPort
,
1444 IORegistryEntryIDMatching(entryID
),
1446 if ((kr
!= KERN_SUCCESS
) || (iterator
== MACH_PORT_NULL
)) {
1447 SC_log(LOG_NOTICE
, "IOServiceGetMatchingServices(0x%llx) returned 0x%x/%d",
1454 entry
= IOIteratorNext(iterator
);
1455 if (entry
== MACH_PORT_NULL
) {
1456 SC_log(LOG_NOTICE
, "IORegistryEntryIDMatching(0x%llx) failed", entryID
);
1460 interface
= _SCNetworkInterfaceCreateWithIONetworkInterfaceObject(entry
);
1463 if (masterPort
!= MACH_PORT_NULL
) {
1464 mach_port_deallocate(mach_task_self(), masterPort
);
1466 if (entry
!= MACH_PORT_NULL
) {
1467 IOObjectRelease(entry
);
1469 if (iterator
!= MACH_PORT_NULL
) {
1470 IOObjectRelease(iterator
);
1475 static SCNetworkInterfaceRef
1476 copyNamedInterfaceForIORegistryEntryID(uint64_t entryID
)
1478 SCNetworkInterfaceRef net_if
;
1480 net_if
= copyInterfaceForIORegistryEntryID(entryID
);
1481 return (ensureInterfaceHasUnit(net_if
));
1484 #else // USE_REGISTRY_ENTRY_ID
1486 * Function: registerInterface
1488 * Register a single interface with the given service path to the
1489 * data link layer (BSD), using the specified unit number.
1491 static kern_return_t
1492 registerInterfaceWithIOServicePath(io_connect_t connect
,
1497 CFMutableDictionaryRef dict
;
1501 dict
= CFDictionaryCreateMutable(NULL
, 0,
1502 &kCFTypeDictionaryKeyCallBacks
,
1503 &kCFTypeDictionaryValueCallBacks
);
1504 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &command
);
1505 CFDictionarySetValue(dict
, CFSTR(kIONetworkStackUserCommandKey
), num
);
1507 CFDictionarySetValue(dict
, CFSTR(kIOPathMatchKey
), path
);
1508 CFDictionarySetValue(dict
, CFSTR(kIOInterfaceUnit
), unit
);
1509 kr
= IOConnectSetCFProperties(connect
, dict
);
1514 static SCNetworkInterfaceRef
1515 copyInterfaceForIOKitPath(CFStringRef if_path
)
1517 io_registry_entry_t entry
= MACH_PORT_NULL
;
1518 SCNetworkInterfaceRef interface
= NULL
;
1520 mach_port_t masterPort
= MACH_PORT_NULL
;
1523 kr
= IOMasterPort(bootstrap_port
, &masterPort
);
1524 if (kr
!= KERN_SUCCESS
) {
1525 SC_log(LOG_ERR
, "IOMasterPort returned 0x%x", kr
);
1528 _SC_cfstring_to_cstring(if_path
, path
, sizeof(path
), kCFStringEncodingASCII
);
1529 entry
= IORegistryEntryFromPath(masterPort
, path
);
1530 if (entry
== MACH_PORT_NULL
) {
1531 SC_log(LOG_NOTICE
, "IORegistryEntryFromPath(%@) failed", if_path
);
1535 interface
= _SCNetworkInterfaceCreateWithIONetworkInterfaceObject(entry
);
1538 if (masterPort
!= MACH_PORT_NULL
) {
1539 mach_port_deallocate(mach_task_self(), masterPort
);
1541 if (entry
!= MACH_PORT_NULL
) {
1542 IOObjectRelease(entry
);
1548 static SCNetworkInterfaceRef
1549 copyNamedInterfaceForIOKitPath(CFStringRef if_path
)
1551 SCNetworkInterfaceRef net_if
;
1553 net_if
= copyInterfaceForIOKitPath(if_path
);
1554 return (ensureInterfaceHasUnit(net_if
));
1557 #endif // USE_REGISTRY_ENTRY_ID
1560 displayInterface(SCNetworkInterfaceRef interface
)
1567 name
= SCNetworkInterfaceGetBSDName(interface
);
1568 unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
1569 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
1570 addr
= SCNetworkInterfaceGetHardwareAddressString(interface
);
1572 SC_log(LOG_INFO
, " %s%@%sType: %@, %s%@%sMAC address: %@",
1573 (name
!= NULL
) ? "BSD Name: " : "",
1574 (name
!= NULL
) ? name
: CFSTR(""),
1575 (name
!= NULL
) ? ", " : "",
1577 (unit
!= NULL
) ? "Unit: " : "",
1578 (unit
!= NULL
) ? (CFTypeRef
)unit
: (CFTypeRef
)CFSTR(""),
1579 (unit
!= NULL
) ? ", " : "",
1580 (addr
!= NULL
) ? addr
: CFSTR("?"));
1584 builtinAvailable(SCNetworkInterfaceRef interface
, // new interface
1585 CFNumberRef if_unit
) // desired unit
1588 CFNumberRef if_type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
1591 n
= (S_dblist
!= NULL
) ? CFArrayGetCount(S_dblist
) : 0;
1592 for (i
= 0; i
< n
; i
++) {
1593 CFStringRef if_path
;
1594 CFDictionaryRef known_dict
= CFArrayGetValueAtIndex(S_dblist
, i
);
1595 CFStringRef known_path
;
1596 CFNumberRef known_type
;
1597 CFNumberRef known_unit
;
1599 known_type
= CFDictionaryGetValue(known_dict
, CFSTR(kIOInterfaceType
));
1600 if (!_SC_CFEqual(if_type
, known_type
)) {
1601 continue; // if not the same interface type
1604 known_unit
= CFDictionaryGetValue(known_dict
, CFSTR(kIOInterfaceUnit
));
1605 if (!_SC_CFEqual(if_unit
, known_unit
)) {
1606 continue; // if not the same interface unit
1609 if_path
= _SCNetworkInterfaceGetIOPath(interface
);
1610 known_path
= CFDictionaryGetValue(known_dict
, CFSTR(kIOPathMatchKey
));
1611 if (!_SC_CFEqual(if_path
, known_path
)) {
1612 // if different IORegistry path
1616 // if same type, same unit, same path
1620 // if interface type/unit not found
1625 builtinCount(CFArrayRef if_list
, CFIndex last
, CFNumberRef if_type
)
1630 for (i
= 0; i
< last
; i
++) {
1631 SCNetworkInterfaceRef builtin_if
;
1632 CFNumberRef builtin_type
;
1634 builtin_if
= CFArrayGetValueAtIndex(if_list
, i
);
1635 builtin_type
= _SCNetworkInterfaceGetIOInterfaceType(builtin_if
);
1636 if (CFEqual(if_type
, builtin_type
)) {
1637 if (_SCNetworkInterfaceIsBuiltin(builtin_if
)) {
1638 n
++; // if built-in interface
1648 #pragma mark Internet Sharing configuration support
1653 static SCPreferencesRef nat_configuration
= NULL
; // com.apple.nat.plist
1654 static SCPreferencesRef nat_preferences
= NULL
; // preferences.plist
1658 sharingConfigurationClose(void)
1660 if (nat_configuration
!= NULL
) {
1661 CFRelease(nat_configuration
);
1662 nat_configuration
= NULL
;
1665 if (nat_preferences
!= NULL
) {
1666 CFRelease(nat_preferences
);
1667 nat_preferences
= NULL
;
1675 sharingConfigurationUsesInterface(CFStringRef bsdName
, Boolean keepOpen
)
1677 CFDictionaryRef config
;
1678 Boolean isShared
= FALSE
;
1680 if (nat_configuration
== NULL
) {
1681 nat_configuration
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":sharingConfigurationUsesInterface"), CFSTR("com.apple.nat.plist"));
1682 if (nat_configuration
== NULL
) {
1687 config
= SCPreferencesGetValue(nat_configuration
, CFSTR("NAT"));
1688 if (isA_CFDictionary(config
)) {
1689 CFBooleanRef bVal
= NULL
;
1690 Boolean enabled
= FALSE
;
1691 CFStringRef sharedFromServiceID
= NULL
;
1692 CFArrayRef sharedToInterfaces
= NULL
;
1694 if (CFDictionaryGetValueIfPresent(config
,
1696 (const void **)&bVal
) &&
1697 isA_CFBoolean(bVal
)) {
1698 enabled
= CFBooleanGetValue(bVal
);
1702 CFDictionaryGetValueIfPresent(config
,
1703 CFSTR("SharingDevices"),
1704 (const void **)&sharedToInterfaces
) &&
1705 isA_CFArray(sharedToInterfaces
)) {
1708 // if "To computers using" interfaces configured
1709 n
= CFArrayGetCount(sharedToInterfaces
);
1710 for (CFIndex i
= 0; i
< n
; i
++) {
1711 CFStringRef sharedToInterface_bsdName
;
1713 sharedToInterface_bsdName
= CFArrayGetValueAtIndex(sharedToInterfaces
, i
);
1714 if (_SC_CFEqual(bsdName
, sharedToInterface_bsdName
)) {
1723 CFDictionaryGetValueIfPresent(config
,
1724 CFSTR("PrimaryService"),
1725 (const void **)&sharedFromServiceID
) &&
1726 isA_CFString(sharedFromServiceID
)) {
1727 if (nat_preferences
== NULL
) {
1728 nat_preferences
= SCPreferencesCreateCompanion(nat_configuration
, NULL
);
1730 if (nat_preferences
!= NULL
) {
1731 SCNetworkServiceRef sharedFromService
;
1733 // if "Share your connection from" service configured
1734 sharedFromService
= SCNetworkServiceCopy(nat_preferences
, sharedFromServiceID
);
1735 if (sharedFromService
!= NULL
) {
1736 CFStringRef sharedFromService_bsdName
;
1737 SCNetworkInterfaceRef sharedFromService_interface
;
1739 sharedFromService_interface
= SCNetworkServiceGetInterface(sharedFromService
);
1740 sharedFromService_bsdName
= SCNetworkInterfaceGetBSDName(sharedFromService_interface
);
1741 isShared
= _SC_CFEqual(bsdName
, sharedFromService_bsdName
);
1742 CFRelease(sharedFromService
);
1749 sharingConfigurationClose();
1755 #endif // TARGET_OS_OSX
1759 #pragma mark Interface monitoring (e.g. watch for "detach")
1762 typedef struct WatchedInfo
*WatchedInfoRef
;
1764 typedef void (*InterfaceUpdateCallBack
) (
1766 natural_t messageType
,
1767 void *messageArgument
1771 SCNetworkInterfaceRef interface
;
1772 io_service_t interface_node
;
1773 io_object_t notification
;
1774 InterfaceUpdateCallBack callback
;
1778 watcherRelease(CFDataRef watched
);
1781 updateWatchedInterface(void *refCon
, io_service_t service
, natural_t messageType
, void *messageArgument
)
1783 #pragma unused(service)
1784 #pragma unused(messageArgument)
1785 switch (messageType
) {
1786 case kIOMessageServiceIsTerminated
: { // if [watched] interface yanked
1787 SCNetworkInterfaceRef remove
;
1788 CFDataRef watched
= (CFDataRef
)refCon
;
1789 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
1791 remove
= watchedInfo
->interface
;
1792 if (_SCNetworkInterfaceIsBuiltin(remove
)) {
1793 // if built-in, keep
1795 } else if (!_SCNetworkInterfaceIsApplePreconfigured(remove
)) {
1796 // if not pre-configured, keep
1799 // if not built-in *and* pre-configured
1804 if (remove
!= NULL
) {
1805 CFStringRef bsdName
;
1807 bsdName
= SCNetworkInterfaceGetBSDName(remove
);
1808 if ((bsdName
!= NULL
) && sharingConfigurationUsesInterface(bsdName
, FALSE
)) {
1809 // if referenced in the Internet Sharing configuration, keep
1814 #endif // TARGET_OS_OSX
1817 watchedInfo
->callback(watched
, messageType
, messageArgument
);
1818 watcherRelease(watched
);
1821 if (remove
!= NULL
) {
1822 SC_log(LOG_INFO
, "Interface released unit %@ (from database)",
1823 _SCNetworkInterfaceGetIOInterfaceUnit(remove
));
1824 removeInterface(S_dblist
, remove
, NULL
);
1827 // update the DB with the [remaining] interfaces that have been named
1828 writeInterfaceList(S_dblist
);
1842 watcherCreate(SCNetworkInterfaceRef interface
, InterfaceUpdateCallBack callback
)
1845 io_service_t interface_node
;
1847 CFDictionaryRef matching
;
1848 CFMutableDataRef watched
;
1849 WatchedInfo
*watchedInfo
;
1851 // get the IORegistry node
1852 entryID
= _SCNetworkInterfaceGetIORegistryEntryID(interface
);
1853 matching
= IORegistryEntryIDMatching(entryID
);
1854 interface_node
= IOServiceGetMatchingService(kIOMasterPortDefault
, matching
);
1855 if (interface_node
== MACH_PORT_NULL
) {
1856 // interface no longer present
1860 // create [locked/trusted] interface watcher
1861 watched
= CFDataCreateMutable(NULL
, sizeof(WatchedInfo
));
1862 CFDataSetLength(watched
, sizeof(WatchedInfo
));
1863 watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
1864 memset(watchedInfo
, 0, sizeof(*watchedInfo
));
1867 watchedInfo
->interface
= CFRetain(interface
);
1869 // ... and the interface node
1870 watchedInfo
->interface_node
= interface_node
;
1872 // ... and set the callback
1873 watchedInfo
->callback
= callback
;
1875 kr
= IOServiceAddInterestNotification(S_notify
, // IONotificationPortRef
1876 watchedInfo
->interface_node
, // io_service_t
1877 kIOGeneralInterest
, // interestType
1878 updateWatchedInterface
, // IOServiceInterestCallback
1879 (void *)watched
, // refCon
1880 &watchedInfo
->notification
); // notification
1881 if (kr
!= KERN_SUCCESS
) {
1883 "IOServiceAddInterestNotification() failed, kr = 0x%x",
1885 watcherRelease(watched
);
1894 watcherRelease(CFDataRef watched
)
1896 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
1899 if (watchedInfo
->notification
!= IO_OBJECT_NULL
) {
1900 IOObjectRelease(watchedInfo
->notification
);
1901 watchedInfo
->notification
= IO_OBJECT_NULL
;
1904 // release interface node
1905 if (watchedInfo
->interface_node
!= IO_OBJECT_NULL
) {
1906 IOObjectRelease(watchedInfo
->interface_node
);
1907 watchedInfo
->interface_node
= IO_OBJECT_NULL
;
1910 // release interface
1911 if (watchedInfo
->interface
!= NULL
) {
1912 CFRelease(watchedInfo
->interface
);
1913 watchedInfo
->interface
= NULL
;
1920 isWatchedInterface(CFArrayRef watchedInterfaces
, SCNetworkInterfaceRef interface
)
1924 n
= (watchedInterfaces
!= NULL
) ? CFArrayGetCount(watchedInterfaces
) : 0;
1925 for (CFIndex i
= 0; i
< n
; i
++) {
1926 CFDataRef watched
= CFArrayGetValueAtIndex(watchedInterfaces
, i
);
1927 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
1929 if (CFEqual((watchedInfo
->interface
), interface
)) {
1939 #pragma mark Locked device support [macOS]
1942 #if !TARGET_OS_IPHONE
1948 n
= (S_locked
!= NULL
) ? CFArrayGetCount(S_locked
) : 0;
1950 CFMutableArrayRef locked
;
1952 locked
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1954 for (CFIndex i
= 0; i
< n
; i
++) {
1959 CFDataRef watched
= CFArrayGetValueAtIndex(S_locked
, i
);
1960 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
1962 name
= SCNetworkInterfaceGetLocalizedDisplayName(watchedInfo
->interface
);
1963 addr
= SCNetworkInterfaceGetHardwareAddressString(watchedInfo
->interface
);
1964 path
= _SCNetworkInterfaceGetIOPath(watchedInfo
->interface
);
1965 str
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@: %@: %@"),
1966 (name
!= NULL
) ? name
: CFSTR("?"),
1967 (addr
!= NULL
) ? addr
: CFSTR("?"),
1969 CFArrayAppendValue(locked
, str
);
1973 CFDictionarySetValue(S_state
, kInterfaceNamerKey_LockedInterfaces
, locked
);
1976 CFDictionaryRemoveValue(S_state
, kInterfaceNamerKey_LockedInterfaces
);
1985 blockNewInterfaces()
1987 static boolean_t allow
= TRUE
;
1988 static dispatch_once_t once
;
1990 dispatch_once(&once
, ^{
1991 allow
= InterfaceNamerControlPrefsAllowNewInterfaces();
2000 CFArrayRef console_sessions
;
2001 boolean_t locked
= FALSE
;
2002 io_registry_entry_t root
;
2004 root
= IORegistryGetRootEntry(kIOMasterPortDefault
);
2005 console_sessions
= IORegistryEntryCreateCFProperty(root
,
2006 CFSTR(kIOConsoleUsersKey
),
2009 if (isA_CFArray(console_sessions
)) {
2012 n
= CFArrayGetCount(console_sessions
);
2013 for (CFIndex i
= 0; i
< n
; i
++) {
2014 CFBooleanRef isLocked
;
2015 CFBooleanRef isLoginDone
;
2016 CFBooleanRef onConsole
;
2017 CFDictionaryRef session
;
2019 session
= CFArrayGetValueAtIndex(console_sessions
, i
);
2020 if (!isA_CFDictionary(session
)) {
2021 // if not dictionary
2025 if (!CFDictionaryGetValueIfPresent(session
,
2026 CFSTR(kIOConsoleSessionOnConsoleKey
),
2027 (const void **)&onConsole
) ||
2028 !isA_CFBoolean(onConsole
) ||
2029 !CFBooleanGetValue(onConsole
)) {
2030 // if not "on console" session
2035 CFDictionaryGetValueIfPresent(session
,
2036 CFSTR(kIOConsoleSessionLoginDoneKey
),
2037 (const void **)&isLoginDone
) &&
2038 isA_CFBoolean(isLoginDone
) &&
2039 !CFBooleanGetValue(isLoginDone
)) {
2041 SC_log(LOG_INFO
, "multiple sessions, console @ loginwindow");
2046 if (CFDictionaryGetValueIfPresent(session
,
2047 CFSTR(kIOConsoleSessionScreenIsLockedKey
),
2048 (const void **)&isLocked
) &&
2049 isA_CFBoolean(isLocked
) &&
2050 CFBooleanGetValue(isLocked
)) {
2052 SC_log(LOG_INFO
, "console screen locked");
2059 SC_log(LOG_INFO
, "console not locked");
2063 if (console_sessions
!= NULL
) {
2064 CFRelease(console_sessions
);
2066 IOObjectRelease(root
);
2071 //#define ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2072 #ifdef ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2074 static CFUserNotificationRef userNotification
;
2075 static CFRunLoopSourceRef userRls
;
2078 lockedNotification_remove(void)
2080 if (userRls
!= NULL
) {
2081 CFRunLoopSourceInvalidate(userRls
);
2085 if (userNotification
!= NULL
) {
2088 status
= CFUserNotificationCancel(userNotification
);
2091 "CFUserNotificationCancel() failed, status=%d",
2094 CFRelease(userNotification
);
2095 userNotification
= NULL
;
2101 #define MY_ICON_PATH "/System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns"
2104 lockedNotification_reply(CFUserNotificationRef userNotification
, CFOptionFlags response_flags
)
2106 #pragma unused(userNotification)
2110 n
= (S_locked
!= NULL
) ? CFArrayGetCount(S_locked
) : 0;
2111 for (CFIndex i
= 0; i
< n
; i
++) {
2112 CFDataRef watched
= CFArrayGetValueAtIndex(S_locked
, i
);
2113 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2115 // process user response
2116 switch (response_flags
& 0x3) {
2117 case kCFUserNotificationDefaultResponse
: {
2118 // if OK'd, [re-]process new interfaces
2120 SC_log(LOG_INFO
, "Reprocessing %ld [locked] interface%s", n
, n
== 1 ? "" : "s");
2122 if (S_iflist
== NULL
) {
2123 S_iflist
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2127 // add the interface to those newly discovered
2128 CFArrayAppendValue(S_iflist
, watchedInfo
->interface
);
2132 // if cancelled, ignore [remaining] new interfaces
2133 SC_log(LOG_INFO
, "[locked] interface ignored");
2134 SC_log(LOG_INFO
, " path = %@", _SCNetworkInterfaceGetIOPath(watchedInfo
->interface
));
2140 watcherRelease(watched
);
2143 if (S_locked
!= NULL
) {
2144 CFRelease(S_locked
);
2148 lockedNotification_remove();
2150 if (S_iflist
!= NULL
) {
2158 lockedNotification_add(void)
2161 CFMutableDictionaryRef dict
;
2163 CFMutableArrayRef message
;
2165 CFURLRef url
= NULL
;
2167 n
= (S_locked
!= NULL
) ? CFArrayGetCount(S_locked
) : 0;
2169 // no locked interfaces, no notification needed
2173 dict
= CFDictionaryCreateMutable(NULL
,
2175 &kCFTypeDictionaryKeyCallBacks
,
2176 &kCFTypeDictionaryValueCallBacks
);
2178 // set localization URL
2179 bundle
= _SC_CFBundleGet();
2180 if (bundle
!= NULL
) {
2181 url
= CFBundleCopyBundleURL(bundle
);
2185 CFDictionarySetValue(dict
, kCFUserNotificationLocalizationURLKey
, url
);
2188 SC_log(LOG_ERR
, "can't find bundle");
2193 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
2194 (const UInt8
*)MY_ICON_PATH
,
2195 sizeof(MY_ICON_PATH
) - 1,
2198 CFDictionarySetValue(dict
, kCFUserNotificationIconURLKey
, url
);
2203 CFDictionarySetValue(dict
,
2204 kCFUserNotificationAlertHeaderKey
,
2205 (n
== 1) ? CFSTR("LOCKED_SINGLE_INTERFACE_HEADER")
2206 : CFSTR("LOCKED_MULTIPLE_INTERFACES_HEADER"));
2209 message
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2210 CFArrayAppendValue(message
,
2211 (n
== 1) ? CFSTR("LOCKED_SINGLE_INTERFACE_MESSAGE")
2212 : CFSTR("LOCKED_MULTIPLE_INTERFACES_MESSAGE"));
2213 for (CFIndex i
= 0; i
< n
; i
++) {
2216 CFDataRef watched
= CFArrayGetValueAtIndex(S_locked
, i
);
2217 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2219 name
= SCNetworkInterfaceGetLocalizedDisplayName(watchedInfo
->interface
);
2220 str
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("\r\t%@"), name
);
2221 CFArrayAppendValue(message
, str
);
2224 CFDictionarySetValue(dict
, kCFUserNotificationAlertMessageKey
, message
);
2228 CFDictionarySetValue(dict
,
2229 kCFUserNotificationDefaultButtonTitleKey
,
2230 CFSTR("LOCKED_INTERFACES_IGNORE"));
2231 CFDictionarySetValue(dict
,
2232 kCFUserNotificationAlternateButtonTitleKey
,
2233 (n
== 1) ? CFSTR("LOCKED_SINGLE_INTERFACE_ADD")
2234 : CFSTR("LOCKED_MULTIPLE_INTERFACES_ADD"));
2236 // create and post notification
2237 userNotification
= CFUserNotificationCreate(NULL
,
2239 kCFUserNotificationNoteAlertLevel
,
2242 if (userNotification
== NULL
) {
2243 SC_log(LOG_ERR
, "CFUserNotificationCreate() failed: %d", (int)error
);
2247 // establish callback
2248 userRls
= CFUserNotificationCreateRunLoopSource(NULL
,
2250 lockedNotification_reply
,
2252 if (userRls
== NULL
) {
2253 SC_log(LOG_ERR
, "CFUserNotificationCreateRunLoopSource() failed");
2254 CFRelease(userNotification
);
2255 userNotification
= NULL
;
2258 CFRunLoopAddSource(CFRunLoopGetCurrent(), userRls
, kCFRunLoopDefaultMode
);
2262 if (dict
!= NULL
) CFRelease(dict
);
2267 lockedNotification_update(void)
2269 // if present, remove current notification
2270 lockedNotification_remove();
2272 // post notification (if needed)
2273 lockedNotification_add();
2278 #endif // ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2281 lockedInterfaceUpdated(CFDataRef watched
, natural_t messageType
, void *messageArgument
)
2283 #pragma unused(messageArgument)
2284 Boolean updated
= FALSE
;
2285 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2287 switch (messageType
) {
2288 case kIOMessageServiceIsTerminated
: { // if [locked] interface yanked
2289 SC_log(LOG_INFO
, "[locked] interface removed");
2290 SC_log(LOG_INFO
, " path = %@", _SCNetworkInterfaceGetIOPath(watchedInfo
->interface
));
2292 if (S_locked
!= NULL
) {
2294 CFIndex n
= CFArrayGetCount(S_locked
);
2296 i
= CFArrayGetFirstIndexOfValue(S_locked
, CFRangeMake(0, n
), watched
);
2297 if (i
!= kCFNotFound
) {
2298 CFArrayRemoveValueAtIndex(S_locked
, i
);
2299 if (CFArrayGetCount(S_locked
) == 0) {
2300 CFRelease(S_locked
);
2315 #ifdef ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2316 // update user notification after interface removed
2317 lockedNotification_update();
2318 #endif // ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2320 // post info about interfaces not added because the console is locked
2328 watchLockedInterface(SCNetworkInterfaceRef interface
)
2330 Boolean updated
= FALSE
;
2333 watched
= watcherCreate(interface
, lockedInterfaceUpdated
);
2334 if (watched
!= NULL
) {
2335 SC_log(LOG_INFO
, "watching [locked] interface");
2336 SC_log(LOG_INFO
, " path = %@", _SCNetworkInterfaceGetIOPath(interface
));
2338 if (S_locked
== NULL
) {
2339 S_locked
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2341 CFArrayAppendValue(S_locked
, watched
);
2346 // post info about interfaces not added because the console is locked
2349 #ifdef ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2350 // post/update user notification with new interface
2351 lockedNotification_update();
2352 #endif // ENABLE_LOCKED_CONSOLE_INTERFACE_NOTIFICATIONS
2357 #endif // !TARGET_OS_IPHONE
2361 #pragma mark Trust required support [iOS]
2364 #if TARGET_OS_IPHONE
2366 #include <SoftLinking/WeakLinking.h>
2367 WEAK_LINK_FORCE_IMPORT(lockdown_is_host_trusted
);
2368 WEAK_LINK_FORCE_IMPORT(kLockdownNotificationHostAttached
);
2369 WEAK_LINK_FORCE_IMPORT(kLockdownNotificationHostDetached
);
2370 WEAK_LINK_FORCE_IMPORT(kLockdownNotificationTrustedHostAttached
);
2371 WEAK_LINK_FORCE_IMPORT(kLockdownNotificationTrustedPTPAttached
);
2376 Boolean haveLibrary
;
2378 haveLibrary
= ((lockdown_is_host_trusted
!= NULL
) &&
2379 (&kLockdownNotificationHostAttached
!= NULL
) &&
2380 (&kLockdownNotificationHostDetached
!= NULL
) &&
2381 (&kLockdownNotificationTrustedHostAttached
!= NULL
) &&
2382 (&kLockdownNotificationTrustedPTPAttached
!= NULL
)
2392 n
= (S_trustRequired
!= NULL
) ? CFArrayGetCount(S_trustRequired
) : 0;
2393 if ((n
> 0) && !S_trustedHostAttached
) {
2394 CFMutableArrayRef excluded
;
2396 // if we have interfaces that require not [yet] granted "trust".
2398 excluded
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2400 for (CFIndex i
= 0; i
< n
; i
++) {
2401 CFStringRef bsdName
;
2402 CFDataRef watched
= CFArrayGetValueAtIndex(S_trustRequired
, i
);
2403 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2405 bsdName
= SCNetworkInterfaceGetBSDName(watchedInfo
->interface
);
2406 if (bsdName
== NULL
) {
2407 SC_log(LOG_NOTICE
, "[trust required] interface w/no BSD name not excluded");
2408 SC_log(LOG_NOTICE
, " interface = %@", watchedInfo
->interface
);
2411 CFArrayAppendValue(excluded
, bsdName
);
2414 CFDictionarySetValue(S_state
, kInterfaceNamerKey_ExcludedInterfaces
, excluded
);
2415 CFRelease(excluded
);
2417 CFDictionaryRemoveValue(S_state
, kInterfaceNamerKey_ExcludedInterfaces
);
2425 static dispatch_queue_t
2426 trustRequired_queue()
2428 static dispatch_once_t once
;
2429 static dispatch_queue_t q
;
2431 dispatch_once(&once
, ^{
2432 q
= dispatch_queue_create("Trust Required queue", NULL
);
2439 // runs on "Trust Required" dispatch queue
2441 trustRequiredNotification_update(CFRunLoopRef rl
, CFStringRef reason
)
2443 Boolean changed
= FALSE
;
2444 CFStringRef error
= NULL
;
2449 * determine whether the device has "trusted" the host (or other device)
2451 trusted
= lockdown_is_host_trusted(MY_PLUGIN_ID
, NULL
, &error
);
2452 n
= (S_trustRequired
!= NULL
) ? CFArrayGetCount(S_trustRequired
) : 0;
2453 if ((S_trustedHostCount
!= n
) || (S_trustedHostAttached
!= trusted
)) {
2457 SC_log(LOG_INFO
, "%@, trusted = %s%s%@, %ld interface%s)%s",
2459 trusted
? "Yes" : "No",
2460 (error
!= NULL
) ? ", error = " : "",
2461 (error
!= NULL
) ? error
: CFSTR(""),
2463 (n
== 1) ? "" : "s",
2464 changed
? " *" : "");
2467 S_trustedHostAttached
= trusted
;
2468 S_trustedHostCount
= n
;
2469 CFRunLoopPerformBlock(rl
, kCFRunLoopDefaultMode
, ^{
2472 CFRunLoopWakeUp(rl
);
2475 if (error
!= NULL
) {
2483 trustRequiredInterfaceUpdated(CFDataRef watched
, natural_t messageType
, void *messageArgument
)
2485 #pragma unused(messageArgument)
2486 Boolean updated
= FALSE
;
2487 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2489 switch (messageType
) {
2490 case kIOMessageServiceIsTerminated
: { // if [locked] interface yanked
2491 SC_log(LOG_INFO
, "[trust required] interface removed");
2492 SC_log(LOG_INFO
, " path = %@", _SCNetworkInterfaceGetIOPath(watchedInfo
->interface
));
2494 if (S_trustRequired
!= NULL
) {
2496 CFIndex n
= CFArrayGetCount(S_trustRequired
);
2498 i
= CFArrayGetFirstIndexOfValue(S_trustRequired
, CFRangeMake(0, n
), watched
);
2499 if (i
!= kCFNotFound
) {
2500 CFArrayRemoveValueAtIndex(S_trustRequired
, i
);
2501 if (CFArrayGetCount(S_trustRequired
) == 0) {
2502 CFRelease(S_trustRequired
);
2503 S_trustRequired
= NULL
;
2517 CFRunLoopRef rl
= CFRunLoopGetCurrent();
2520 dispatch_async(trustRequired_queue(), ^{
2521 trustRequiredNotification_update(rl
, CFSTR("TrustRequired interface removed"));
2530 watchTrustedStatus(CFStringRef notification
, CFStringRef reason
)
2533 int notify_token
= -1;
2535 CFRunLoopRef rl
= CFRunLoopGetCurrent();
2537 key
= CFStringGetCStringPtr(notification
, kCFStringEncodingUTF8
);
2538 assert(key
!= NULL
);
2542 ret
= notify_register_dispatch(key
,
2544 trustRequired_queue(),
2546 #pragma unused(token)
2547 trustRequiredNotification_update(rl
, reason
);
2549 if (ret
!= NOTIFY_STATUS_OK
) {
2550 SC_log(LOG_ERR
, "notify_register_dispatch(%@) failed: %u", notification
, ret
);
2559 updateTrustRequiredInterfaces(CFArrayRef interfaces
)
2562 Boolean updated
= FALSE
;
2564 n
= (interfaces
!= NULL
) ? CFArrayGetCount(interfaces
) : 0;
2565 for (CFIndex i
= 0; i
< n
; i
++) {
2566 SCNetworkInterfaceRef interface
;
2568 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
2569 if (_SCNetworkInterfaceIsTrustRequired(interface
) &&
2570 !isWatchedInterface(S_trustRequired
, interface
)) {
2573 watched
= watcherCreate(interface
, trustRequiredInterfaceUpdated
);
2574 if (watched
!= NULL
) {
2575 CFStringRef bsdName
;
2577 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2578 if (bsdName
!= NULL
) {
2579 SC_log(LOG_INFO
, "watching [trust required] interface: %@", bsdName
);
2581 SC_log(LOG_INFO
, "watching [trust required] interface w/no BSD name");
2582 SC_log(LOG_INFO
, " interface = %@", interface
);
2585 if (S_trustRequired
== NULL
) {
2586 S_trustRequired
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2588 CFArrayAppendValue(S_trustRequired
, watched
);
2595 static dispatch_once_t once
;
2596 CFRunLoopRef rl
= CFRunLoopGetCurrent();
2598 dispatch_once(&once
, ^{
2599 // watch for "Host attached"
2600 watchTrustedStatus(kLockdownNotificationHostAttached
,
2601 CFSTR("Host attached"));
2603 // watch for "Host detached"
2604 watchTrustedStatus(kLockdownNotificationHostDetached
,
2605 CFSTR("Host detached"));
2607 // watch for "Trusted host attached"
2608 watchTrustedStatus(kLockdownNotificationTrustedHostAttached
,
2609 CFSTR("Trusted Host attached"));
2611 // watch for "Trusted PDP attached"
2612 watchTrustedStatus(kLockdownNotificationTrustedPTPAttached
,
2613 CFSTR("Trusted PTP attached"));
2617 dispatch_async(trustRequired_queue(), ^{
2618 trustRequiredNotification_update(rl
, CFSTR("TrustRequired interface added"));
2625 #endif // TARGET_OS_IPHONE
2629 #pragma mark Pre-configured interface support
2633 sharePreconfigured()
2637 n
= (S_preconfigured
!= NULL
) ? CFArrayGetCount(S_preconfigured
) : 0;
2639 CFMutableArrayRef preconfigured
;
2641 preconfigured
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2643 for (CFIndex i
= 0; i
< n
; i
++) {
2644 CFStringRef bsdName
;
2645 CFDataRef watched
= CFArrayGetValueAtIndex(S_preconfigured
, i
);
2646 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2648 bsdName
= SCNetworkInterfaceGetBSDName(watchedInfo
->interface
);
2649 if (bsdName
== NULL
) {
2650 SC_log(LOG_NOTICE
, "pre-configured interface w/no BSD name");
2651 SC_log(LOG_NOTICE
, " interface = %@", watchedInfo
->interface
);
2654 CFArrayAppendValue(preconfigured
, bsdName
);
2657 CFDictionarySetValue(S_state
, kInterfaceNamerKey_PreConfiguredInterfaces
, preconfigured
);
2658 CFRelease(preconfigured
);
2660 CFDictionaryRemoveValue(S_state
, kInterfaceNamerKey_PreConfiguredInterfaces
);
2669 preconfiguredInterfaceUpdated(CFDataRef watched
, natural_t messageType
, void *messageArgument
)
2671 Boolean updated
= FALSE
;
2672 WatchedInfo
*watchedInfo
= (WatchedInfo
*)(void *)CFDataGetBytePtr(watched
);
2674 #pragma unused(messageArgument)
2675 switch (messageType
) {
2676 case kIOMessageServiceIsTerminated
: { // if [locked] interface yanked
2677 CFStringRef bsdName
;
2679 bsdName
= SCNetworkInterfaceGetBSDName(watchedInfo
->interface
);
2680 if (bsdName
!= NULL
) {
2681 SC_log(LOG_INFO
, "[pre-configured] interface removed: %@", bsdName
);
2683 SC_log(LOG_INFO
, "[pre-configured] interface w/no BSD name removed");
2684 SC_log(LOG_INFO
, " interface = %@", watchedInfo
->interface
);
2687 if (S_preconfigured
!= NULL
) {
2689 CFIndex n
= CFArrayGetCount(S_preconfigured
);
2691 i
= CFArrayGetFirstIndexOfValue(S_preconfigured
, CFRangeMake(0, n
), watched
);
2692 if (i
!= kCFNotFound
) {
2693 CFArrayRemoveValueAtIndex(S_preconfigured
, i
);
2694 if (CFArrayGetCount(S_preconfigured
) == 0) {
2695 CFRelease(S_preconfigured
);
2696 S_preconfigured
= NULL
;
2710 sharePreconfigured();
2717 updatePreConfiguredInterfaces(CFArrayRef interfaces
)
2720 Boolean updated
= FALSE
;
2722 n
= (interfaces
!= NULL
) ? CFArrayGetCount(interfaces
) : 0;
2723 for (CFIndex i
= 0; i
< n
; i
++) {
2724 SCNetworkInterfaceRef interface
;
2726 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
2727 if (_SCNetworkInterfaceIsApplePreconfigured(interface
) &&
2728 !isWatchedInterface(S_preconfigured
, interface
)) {
2731 watched
= watcherCreate(interface
, preconfiguredInterfaceUpdated
);
2732 if (watched
!= NULL
) {
2733 CFStringRef bsdName
;
2735 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2736 if (bsdName
!= NULL
) {
2737 SC_log(LOG_INFO
, "watching [pre-configured] interface: %@", bsdName
);
2739 SC_log(LOG_INFO
, "watching [pre-configured] interface w/no BSD name");
2740 SC_log(LOG_INFO
, " interface = %@", interface
);
2743 if (S_preconfigured
== NULL
) {
2744 S_preconfigured
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2746 CFArrayAppendValue(S_preconfigured
, watched
);
2753 sharePreconfigured();
2761 #pragma mark Interface naming
2764 static __inline__ boolean_t
2767 return (S_quiet
== MACH_PORT_NULL
);
2771 wasPreviouslyUsedInterface(CFDictionaryRef dbdict
, SCNetworkInterfaceRef interface
)
2773 CFArrayRef matchingMACs
;
2775 matchingMACs
= CFDictionaryGetValue(dbdict
, CFSTR(kSCNetworkInterfaceMatchingMACs
));
2776 if (matchingMACs
!= NULL
) {
2779 addr
= _SCNetworkInterfaceGetHardwareAddress(interface
);
2781 if (CFArrayContainsValue(matchingMACs
,
2782 CFRangeMake(0, CFArrayGetCount(matchingMACs
)),
2793 nameInterfaces(CFMutableArrayRef if_list
)
2796 CFIndex n
= CFArrayGetCount(if_list
);
2798 for (i
= 0; i
< n
; i
++) {
2800 SCNetworkInterfaceRef interface
;
2801 SCNetworkInterfaceRef new_interface
;
2807 interface
= CFArrayGetValueAtIndex(if_list
, i
);
2808 path
= _SCNetworkInterfaceGetIOPath(interface
);
2809 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
2810 unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
2811 entryID
= _SCNetworkInterfaceGetIORegistryEntryID(interface
);
2814 CFStringRef if_name
;
2816 if_name
= SCNetworkInterfaceGetBSDName(interface
);
2817 if ((if_name
== NULL
) || !CFDictionaryContainsKey(S_state
, if_name
)) {
2818 SC_log(LOG_INFO
, "Interface already has a unit number");
2819 displayInterface(interface
);
2822 // update the list of interfaces that were previously named
2823 if ((S_prev_active_list
!= NULL
)
2824 && lookupInterfaceByAddress(S_prev_active_list
, interface
, &where
) != NULL
) {
2825 CFArrayRemoveValueAtIndex(S_prev_active_list
, where
);
2828 replaceInterface(interface
);
2830 CFDictionaryRef dbdict
;
2831 boolean_t is_builtin
;
2835 dbdict
= lookupInterfaceByAddress(S_dblist
, interface
, NULL
);
2836 if (dbdict
!= NULL
) {
2837 unit
= CFDictionaryGetValue(dbdict
, CFSTR(kIOInterfaceUnit
));
2840 SC_log(LOG_INFO
, "Interface assigned unit %@ (from database)", unit
);
2843 if ((dbdict
== NULL
) && !isQuiet()) {
2844 // if new interface, wait until quiet before naming
2845 addTimestamp(S_state
, path
);
2849 is_builtin
= _SCNetworkInterfaceIsBuiltin(interface
);
2851 if (dbdict
== NULL
) {
2852 dbdict
= lookupMatchingInterface(interface
,
2856 is_builtin
? kCFBooleanTrue
: kCFBooleanFalse
);
2858 if ((dbdict
!= NULL
) && wasPreviouslyUsedInterface(dbdict
, interface
)) {
2859 unit
= CFDictionaryGetValue(dbdict
, CFSTR(kIOInterfaceUnit
));
2862 SC_log(LOG_INFO
, "Interface assigned unit %@ (updating database w/previously used interface)", unit
);
2865 #if !TARGET_OS_IPHONE
2866 if ((unit
== NULL
) &&
2869 blockNewInterfaces() &&
2870 !_SCNetworkInterfaceIsApplePreconfigured(interface
) &&
2871 isConsoleLocked()) {
2874 // if new (but matching) interface and console locked, ignore
2875 addr
= SCNetworkInterfaceGetHardwareAddressString(interface
);
2876 SC_log(LOG_NOTICE
, "Console locked, network interface* ignored");
2877 SC_log(LOG_INFO
, " path = %@, addr = %@",
2879 (addr
!= NULL
) ? addr
: CFSTR("?"));
2880 watchLockedInterface(interface
);
2883 #endif // !TARGET_OS_IPHONE
2885 if ((unit
== NULL
) && (dbdict
!= NULL
)) {
2886 unit
= CFDictionaryGetValue(dbdict
, CFSTR(kIOInterfaceUnit
));
2889 SC_log(LOG_INFO
, "Interface assigned unit %@ (updating database w/new interface)", unit
);
2893 if ((dbdict
!= NULL
) && (S_prev_active_list
!= NULL
)) {
2894 // update the list of interfaces that were previously named
2895 where
= CFArrayGetFirstIndexOfValue(S_prev_active_list
,
2896 CFRangeMake(0, CFArrayGetCount(S_prev_active_list
)),
2898 if (where
!= kCFNotFound
) {
2899 CFArrayRemoveValueAtIndex(S_prev_active_list
, where
);
2903 if (dbdict
== NULL
) {
2907 // built-in interface, try to use the reserved slots
2908 next_unit
= builtinCount(if_list
, i
, type
);
2910 // But, before claiming a reserved slot we check to see if the
2911 // slot had previously been used. If so, and if the slot had been
2912 // assigned to the same type of interface, then we will perform a
2913 // replacement (e.g. assume that this was a board swap). But, if
2914 // the new interface is a different type then we assume that the
2915 // built-in configuration has changed and allocate a new unit from
2916 // the non-reserved slots.
2918 unit
= CFNumberCreate(NULL
, kCFNumberIntType
, &next_unit
);
2919 if (!builtinAvailable(interface
, unit
)) {
2920 // if [built-in] unit not available
2921 SC_log(LOG_INFO
, "Interface not assigned [built-in] unit %@", unit
);
2927 #if !TARGET_OS_IPHONE
2930 blockNewInterfaces() &&
2931 !_SCNetworkInterfaceIsApplePreconfigured(interface
) &&
2932 isConsoleLocked()) {
2935 // if new interface and console locked, ignore
2936 addr
= SCNetworkInterfaceGetHardwareAddressString(interface
);
2937 SC_log(LOG_NOTICE
, "Console locked, network interface ignored");
2938 SC_log(LOG_INFO
, " path = %@, addr = %@",
2940 (addr
!= NULL
) ? addr
: CFSTR("?"));
2941 watchLockedInterface(interface
);
2944 #endif // !TARGET_OS_IPHONE
2947 // not built-in (or built-in unit not available), allocate from
2948 // the non-reserved slots
2949 next_unit
= builtinCount(if_list
, n
, type
);
2950 next_unit
= getNextUnitForType(type
, next_unit
);
2951 unit
= CFNumberCreate(NULL
, kCFNumberIntType
, &next_unit
);
2954 SC_log(LOG_INFO
, "Interface assigned unit %@ (%s)",
2956 is_builtin
? "built-in" : "next available");
2961 #ifdef USE_REGISTRY_ENTRY_ID
2962 kr
= registerInterfaceWithIORegistryEntryID(S_connect
,
2965 (dbdict
== NULL
) ? kIONetworkStackRegisterInterfaceWithLowestUnit
2966 : kIONetworkStackRegisterInterfaceWithUnit
);
2967 new_interface
= copyNamedInterfaceForIORegistryEntryID(entryID
);
2968 #else // USE_REGISTRY_ENTRY_ID
2969 kr
= registerInterfaceWithIOServicePath(S_connect
,
2972 (dbdict
== NULL
) ? kRegisterInterface
2973 : kRegisterInterfaceWithFixedUnit
);
2974 new_interface
= copyNamedInterfaceForIOKitPath(path
);
2975 #endif // USE_REGISTRY_ENTRY_ID
2976 if (new_interface
== NULL
) {
2977 const char *signature
;
2979 signature
= (dbdict
== NULL
) ? "failed to name new interface"
2980 : "failed to name known interface";
2982 SC_log(LOG_NOTICE
, "%s, kr=0x%x\n"
2992 displayInterface(interface
);
2994 if ((dbdict
!= NULL
) && (retries
++ < 5)) {
2995 usleep(50 * 1000); // sleep 50ms between attempts
3000 CFNumberRef new_unit
;
3003 SC_log(LOG_INFO
, "%s interface named after %d %s\n"
3006 (dbdict
== NULL
) ? "New" : "Known",
3008 (retries
== 1) ? "try" : "tries",
3012 #ifdef SHOW_NAMING_FAILURE
3013 str
= CFStringCreateWithFormat(NULL
,
3015 CFSTR("\"%s\" interface named after %d %s, unit = %@"),
3016 (dbdict
== NULL
) ? "New" : "Known",
3018 (retries
== 1) ? "try" : "tries",
3020 CFUserNotificationDisplayNotice(0,
3021 kCFUserNotificationStopAlertLevel
,
3026 CFSTR("Please report repeated failures."),
3029 #endif // SHOW_NAMING_FAILURE
3032 new_unit
= _SCNetworkInterfaceGetIOInterfaceUnit(new_interface
);
3033 if (!CFEqual(unit
, new_unit
)) {
3034 SC_log(LOG_INFO
, "interface type %@ assigned unit %@ instead of %@",
3035 type
, new_unit
, unit
);
3038 displayInterface(new_interface
);
3040 // update if_list (with the interface name & unit)
3041 CFArraySetValueAtIndex(if_list
, i
, new_interface
);
3042 CFRelease(new_interface
);
3043 interface
= new_interface
; // if_list holds the reference
3045 if (is_builtin
&& (S_prev_active_list
!= NULL
)) {
3048 // update the list of [built-in] interfaces that were previously named
3049 if (lookupInterfaceByUnit(S_prev_active_list
, interface
, &where
) != NULL
) {
3050 SC_log(LOG_DEBUG
, " and updated database (new address)");
3051 CFArrayRemoveValueAtIndex(S_prev_active_list
, where
);
3054 replaceInterface(interface
);
3062 #if !TARGET_OS_IPHONE
3064 #define INSTALL_ENVIRONMENT "__OSINSTALL_ENVIRONMENT"
3069 static Boolean isRecovery
= FALSE
;
3070 static dispatch_once_t once
;
3073 * We check to see if the __OSINSTALL_ENVIRONMENT env var is present. If
3074 * so, then we are most likely booted into the Recovery OS with no [Aqua]
3075 * "SCMonitor" [UserEventAgent] plugin.
3077 dispatch_once(&once
, ^{
3078 if (getenv(INSTALL_ENVIRONMENT
) != NULL
) {
3088 updateNetworkConfiguration(CFArrayRef if_list
)
3090 Boolean do_commit
= FALSE
;
3093 SCPreferencesRef prefs
= NULL
;
3094 SCNetworkSetRef set
= NULL
;
3096 prefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":updateNetworkConfiguration"), NULL
);
3097 if (prefs
== NULL
) {
3098 SC_log(LOG_NOTICE
, "SCPreferencesCreate() failed: %s", SCErrorString(SCError()));
3102 set
= SCNetworkSetCopyCurrent(prefs
);
3104 SC_log(LOG_INFO
, "No current set, adding default");
3105 set
= _SCNetworkSetCreateDefault(prefs
);
3107 SC_log(LOG_NOTICE
, "_SCNetworkSetCreateDefault() failed: %s", SCErrorString(SCError()));
3112 n
= (if_list
!= NULL
) ? CFArrayGetCount(if_list
) : 0;
3113 for (i
= 0; i
< n
; i
++) {
3114 SCNetworkInterfaceRef interface
;
3116 interface
= CFArrayGetValueAtIndex(if_list
, i
);
3117 if (SCNetworkSetEstablishDefaultInterfaceConfiguration(set
, interface
)) {
3118 SC_log(LOG_INFO
, "adding default configuration for %@",
3119 SCNetworkInterfaceGetBSDName(interface
));
3127 ok
= SCPreferencesCommitChanges(prefs
);
3129 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges() failed: %s", SCErrorString(SCError()));
3133 ok
= SCPreferencesApplyChanges(prefs
);
3135 SC_log(LOG_NOTICE
, "SCPreferencesApplyChanges() failed: %s", SCErrorString(SCError()));
3147 if (prefs
!= NULL
) {
3154 #endif // !TARGET_OS_IPHONE
3157 upgradeNetworkConfiguration()
3159 static dispatch_once_t once
;
3162 * Once, per start of InterfaceNamer, we check/ensure that the
3163 * configuration has been upgraded.
3165 * Note: this check should not be performed until we know that
3166 * the __wait_for_IOKit_to_quiesce() conditions have been
3170 dispatch_once(&once
, ^{
3171 SCPreferencesRef ni_prefs
;
3174 // save the [current] DB with the interfaces that have been named
3175 writeInterfaceList(S_dblist
);
3177 // upgrade the configuration
3178 ni_prefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":upgradeNetworkConfiguration"), INTERFACES_DEFAULT_CONFIG
);
3179 if (ni_prefs
== NULL
) {
3180 SC_log(LOG_NOTICE
, "SCPreferencesCreate() failed: %s", SCErrorString(SCError()));
3183 updated
= __SCNetworkConfigurationUpgrade(NULL
, &ni_prefs
, TRUE
);
3184 CFRelease(ni_prefs
);
3187 // re-read list of previously named network interfaces
3188 if (S_dblist
!= NULL
) {
3189 CFRelease(S_dblist
);
3191 S_dblist
= readInterfaceList();
3193 addTimestamp(S_state
, CFSTR("*UPGRADED*"));
3194 SC_log(LOG_INFO
, "network configuration upgraded");
3203 removeInactiveInterfaces(void)
3208 * remove any previous interfaces that were built-in,
3209 * were active, and were hidden (pre-configured) that
3210 * are no longer plugged in.
3213 if ((S_dblist
== NULL
) || (S_prev_active_list
== NULL
)) {
3217 n
= CFArrayGetCount(S_prev_active_list
);
3218 for (CFIndex i
= n
- 1; i
>= 0; i
--) {
3219 CFBooleanRef builtin
;
3220 CFBooleanRef hidden
;
3221 CFDictionaryRef if_dict
;
3222 CFDictionaryRef info
;
3226 if_dict
= CFArrayGetValueAtIndex(S_prev_active_list
, i
);
3228 // Note: keep the following logic in sync with _SCNetworkInterfaceIsApplePreconfigured()
3230 name
= CFDictionaryGetValue(if_dict
, CFSTR(kIOBSDNameKey
));
3231 if (!isA_CFString(name
)) {
3236 hidden
= CFDictionaryGetValue(if_dict
, kSCNetworkInterfaceHiddenConfigurationKey
);
3237 if (!isA_CFBoolean(hidden
) || !CFBooleanGetValue(hidden
)) {
3242 builtin
= CFDictionaryGetValue(if_dict
, CFSTR(kIOBuiltin
));
3243 if (isA_CFBoolean(builtin
) && CFBooleanGetValue(builtin
)) {
3244 // if [hidden] builtin
3248 info
= CFDictionaryGetValue(if_dict
, CFSTR(kSCNetworkInterfaceInfo
));
3249 if (isA_CFDictionary(info
)) {
3253 if (CFDictionaryGetValueIfPresent(info
, CFSTR(kUSBVendorID
), (const void **)&vidNum
) &&
3254 isA_CFNumber(vidNum
) &&
3255 CFNumberGetValue(vidNum
, kCFNumberIntType
, &vid
) &&
3256 (vid
== kIOUSBAppleVendorID
)) {
3257 // if [hidden] Apple interface
3260 if (sharingConfigurationUsesInterface(name
, TRUE
)) {
3261 // do not remove interfaces referenced in the sharing configuration
3264 #endif // TARGET_OS_OSX
3274 SC_log(LOG_INFO
, "Removing no-longer-active \"hidden\" interface: %@", name
);
3276 if (lookupInterfaceByName(S_dblist
, name
, &where
) != NULL
) {
3277 // remove from the list of interfaces we know about
3278 CFArrayRemoveValueAtIndex(S_dblist
, where
);
3279 // remove from the previously active list
3280 CFArrayRemoveValueAtIndex(S_prev_active_list
, i
);
3285 sharingConfigurationClose();
3286 #endif // TARGET_OS_OSX
3292 reportInactiveInterfaces(void)
3297 * report any previous interfaces that are not [yet] active
3300 if (S_prev_active_list
== NULL
) {
3304 n
= CFArrayGetCount(S_prev_active_list
);
3306 SC_log(LOG_INFO
, "Interface%s not [yet] active",
3307 (n
> 1) ? "s" : "");
3309 for (CFIndex i
= 0; i
< n
; i
++) {
3310 CFDictionaryRef if_dict
;
3315 if_dict
= CFArrayGetValueAtIndex(S_prev_active_list
, i
);
3316 name
= CFDictionaryGetValue(if_dict
, CFSTR(kIOBSDNameKey
));
3317 type
= CFDictionaryGetValue(if_dict
, CFSTR(kIOInterfaceType
));
3318 unit
= CFDictionaryGetValue(if_dict
, CFSTR(kIOInterfaceUnit
));
3319 SC_log(LOG_INFO
, " %s%@%sType: %@, Unit: %@",
3320 (name
!= NULL
) ? "BSD Name: " : "",
3321 (name
!= NULL
) ? name
: CFSTR(""),
3322 (name
!= NULL
) ? ", " : "",
3331 updateInterfaces(void)
3333 if (S_connect
== MACH_PORT_NULL
) {
3334 // if we don't have the "IONetworkStack" connect object
3338 if (S_iflist
!= NULL
) {
3341 n
= CFArrayGetCount(S_iflist
);
3343 CFArraySortValues(S_iflist
, CFRangeMake(0, n
), _SCNetworkInterfaceCompare
, NULL
);
3345 nameInterfaces(S_iflist
);
3349 * Update the list of [Apple] pre-configured interfaces
3351 updatePreConfiguredInterfaces(S_iflist
);
3353 #if TARGET_OS_IPHONE
3355 * Update the list of "trust required" interfaces
3357 if (haveLockdown()) {
3358 updateTrustRequiredInterfaces(S_iflist
);
3360 #endif // TARGET_OS_IPHONE
3364 * The registry [matching] has quiesced
3367 // remove any inactive interfaces
3368 removeInactiveInterfaces();
3370 // save the DB with the interfaces that have been named
3371 writeInterfaceList(S_dblist
);
3373 // update the VLAN/BOND configuration
3374 updateVirtualNetworkInterfaceConfiguration(NULL
, kSCPreferencesNotificationApply
, NULL
);
3376 #if !TARGET_OS_IPHONE
3377 if (isRecoveryOS()) {
3379 * We are most likely booted into the Recovery OS with no "SCMonitor"
3380 * UserEventAgent plugin running so let's make sure we update the
3381 * network configuration for new interfaces.
3383 updateNetworkConfiguration(S_iflist
);
3385 #endif // !TARGET_OS_IPHONE
3387 // tell everyone that we've finished (at least for now)
3390 // log those interfaces which are no longer present in
3391 // the HW config (or have yet to show up).
3392 reportInactiveInterfaces();
3394 if (S_prev_active_list
!= NULL
) {
3395 CFRelease(S_prev_active_list
);
3396 S_prev_active_list
= NULL
;
3399 if (S_iflist
!= NULL
) {
3400 CFRelease(S_iflist
);
3404 if ((S_prev_active_list
!= NULL
) && (CFArrayGetCount(S_prev_active_list
) == 0)) {
3406 * if we've named all of the interfaces that
3407 * were used during the previous boot.
3409 addTimestamp(S_state
, kInterfaceNamerKey_Complete
);
3410 SC_log(LOG_INFO
, "last boot interfaces have been named");
3412 CFRelease(S_prev_active_list
);
3413 S_prev_active_list
= NULL
;
3421 interfaceArrivalCallback(void *refcon
, io_iterator_t iter
)
3423 #pragma unused(refcon)
3426 while ((obj
= IOIteratorNext(iter
)) != MACH_PORT_NULL
) {
3427 SCNetworkInterfaceRef interface
;
3429 interface
= _SCNetworkInterfaceCreateWithIONetworkInterfaceObject(obj
);
3430 if (interface
!= NULL
) {
3431 if (S_iflist
== NULL
) {
3432 S_iflist
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3434 CFArrayAppendValue(S_iflist
, interface
);
3435 CFRelease(interface
);
3437 IOObjectRelease(obj
);
3446 * Function: stackCallback
3448 * Get a reference to the single IONetworkStack object instance in
3449 * the kernel. Naming requests must be sent to this object, which is
3450 * attached as a client to all network interface objects in the system.
3452 * Call IOObjectRelease on the returned object.
3455 stackCallback(void *refcon
, io_iterator_t iter
)
3457 #pragma unused(refcon)
3461 stack
= IOIteratorNext(iter
);
3462 if (stack
== MACH_PORT_NULL
) {
3466 kr
= IOServiceOpen(stack
, mach_task_self(), 0, &S_connect
);
3467 if (kr
!= KERN_SUCCESS
) {
3468 SC_log(LOG_ERR
, "IOServiceOpen returned 0x%x", kr
);
3472 addTimestamp(S_state
, CFSTR("*STACK*"));
3473 SC_log(LOG_INFO
, "IONetworkStack found");
3475 if (S_stack
!= MACH_PORT_NULL
) {
3476 IOObjectRelease(S_stack
);
3477 S_stack
= MACH_PORT_NULL
;
3480 if ((S_timer
!= NULL
) && CFRunLoopTimerIsValid(S_timer
)) {
3481 // With the IONetworkStack object now available we can
3482 // reset (shorten?) the time we are willing to wait for
3483 // IOKit to quiesce.
3484 CFRunLoopTimerSetNextFireDate(S_timer
,
3485 CFAbsoluteTimeGetCurrent() + S_quiet_timeout
);
3492 if (stack
!= MACH_PORT_NULL
) {
3493 IOObjectRelease(stack
);
3500 quietCallback(void *refcon
,
3501 io_service_t service
,
3502 natural_t messageType
,
3503 void *messageArgument
)
3505 #pragma unused(refcon)
3506 #pragma unused(service)
3507 if (messageArgument
!= NULL
) {
3512 if (messageType
== kIOMessageServiceBusyStateChange
) {
3513 addTimestamp(S_state
, kInterfaceNamerKey_Quiet
);
3514 SC_log(LOG_INFO
, "IOKit quiet");
3517 if (S_connect
== MACH_PORT_NULL
) {
3518 SC_log(LOG_ERR
, "No network stack object");
3522 if (S_quiet
!= MACH_PORT_NULL
) {
3523 IOObjectRelease(S_quiet
);
3524 S_quiet
= MACH_PORT_NULL
;
3527 if (S_timer
!= NULL
) {
3528 CFRunLoopTimerInvalidate(S_timer
);
3533 // grab (and name) any additional interfaces.
3534 interfaceArrivalCallback((void *)S_notify
, S_iter
);
3536 if (messageType
== kIOMessageServiceBusyStateChange
) {
3537 addTimestamp(S_state
, CFSTR("*QUIET&NAMED*"));
3539 upgradeNetworkConfiguration();
3546 iterateRegistryBusy(io_iterator_t iterator
, CFArrayRef nodes
, int *count
)
3548 kern_return_t kr
= kIOReturnSuccess
;;
3551 while ((kr
== kIOReturnSuccess
) &&
3552 ((obj
= IOIteratorNext(iterator
)) != MACH_PORT_NULL
)) {
3553 uint64_t accumulated_busy_time
;
3554 uint32_t busy_state
;
3557 CFMutableArrayRef newNodes
;
3559 CFMutableStringRef str
= NULL
;
3561 if (nodes
== NULL
) {
3562 newNodes
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3564 newNodes
= CFArrayCreateMutableCopy(NULL
, 0, nodes
);
3566 assert(newNodes
!= NULL
);
3568 kr
= IORegistryEntryGetName(obj
, name
);
3569 if (kr
!= kIOReturnSuccess
) {
3570 SC_log(LOG_NOTICE
, "IORegistryEntryGetName() returned 0x%x", kr
);
3574 str
= CFStringCreateMutable(NULL
, 0);
3575 CFStringAppendCString(str
, name
, kCFStringEncodingUTF8
);
3577 kr
= IORegistryEntryGetLocationInPlane(obj
, kIOServicePlane
, location
);
3579 case kIOReturnSuccess
:
3580 CFStringAppendCString(str
, "@", kCFStringEncodingUTF8
);
3581 CFStringAppendCString(str
, location
, kCFStringEncodingUTF8
);
3583 case kIOReturnNotFound
:
3586 SC_log(LOG_NOTICE
, "IORegistryEntryGetLocationInPlane() returned 0x%x", kr
);
3591 CFArrayAppendValue(newNodes
, str
);
3594 kr
= IOServiceGetBusyStateAndTime(obj
, &state
, &busy_state
, &accumulated_busy_time
);
3595 if (kr
!= kIOReturnSuccess
) {
3596 SC_log(LOG_NOTICE
, "IOServiceGetBusyStateAndTime() returned 0x%x", kr
);
3600 #ifdef TEST_SNAPSHOT
3603 #endif // TEST_SNAPSHOT
3605 if (busy_state
!= 0) {
3608 if ((*count
)++ == 0) {
3609 SC_log(LOG_ERR
, "Busy services :");
3612 path
= CFStringCreateByCombiningStrings(NULL
, newNodes
, CFSTR("/"));
3613 SC_log(LOG_ERR
, " %@ [%s%s%s%d, %lld ms]",
3615 (state
& kIOServiceRegisteredState
) ? "" : "!registered, ",
3616 (state
& kIOServiceMatchedState
) ? "" : "!matched, ",
3617 (state
& kIOServiceInactiveState
) ? "inactive, " : "",
3619 accumulated_busy_time
/ kMillisecondScale
);
3623 kr
= IORegistryIteratorEnterEntry(iterator
);
3624 if (kr
!= kIOReturnSuccess
) {
3625 SC_log(LOG_NOTICE
, "IORegistryIteratorEnterEntry() returned 0x%x", kr
);
3629 iterateRegistryBusy(iterator
, newNodes
, count
);
3631 kr
= IORegistryIteratorExitEntry(iterator
);
3632 if (kr
!= kIOReturnSuccess
) {
3633 SC_log(LOG_NOTICE
, "IORegistryIteratorExitEntry() returned 0x%x", kr
);
3638 CFRelease(newNodes
);
3639 IOObjectRelease(obj
);
3649 io_iterator_t iterator
= MACH_PORT_NULL
;
3652 kr
= IORegistryCreateIterator(kIOMasterPortDefault
,
3656 if (kr
!= kIOReturnSuccess
) {
3657 SC_log(LOG_NOTICE
, "IORegistryCreateIterator() returned 0x%x", kr
);
3661 iterateRegistryBusy(iterator
, NULL
, &count
);
3663 SC_log(LOG_ERR
, "w/no busy services");
3666 IOObjectRelease(iterator
);
3670 timerCallback(CFRunLoopTimerRef timer
, void *info
)
3672 #pragma unused(timer)
3673 #pragma unused(info)
3674 // We've been waiting for IOKit to quiesce and it just
3675 // hasn't happenned. Time to just move on!
3676 addTimestamp(S_state
, kInterfaceNamerKey_Timeout
);
3679 SC_log(LOG_ERR
, "timed out waiting for IOKit to quiesce");
3682 quietCallback((void *)S_notify
, MACH_PORT_NULL
, 0, NULL
);
3684 addTimestamp(S_state
, CFSTR("*TIMEOUT&NAMED*"));
3686 upgradeNetworkConfiguration();
3692 setup_IOKit(CFBundleRef bundle
)
3694 #pragma unused(bundle)
3697 mach_port_t masterPort
= MACH_PORT_NULL
;
3699 io_object_t root
= MACH_PORT_NULL
;
3701 // read DB of previously named network interfaces
3702 S_dblist
= readInterfaceList();
3704 // get interfaces that were named during the last boot
3705 S_prev_active_list
= previouslyActiveInterfaces();
3707 // track how long we've waited to see each interface.
3708 S_state
= CFDictionaryCreateMutable(NULL
,
3710 &kCFTypeDictionaryKeyCallBacks
,
3711 &kCFTypeDictionaryValueCallBacks
);
3712 addTimestamp(S_state
, CFSTR("*START*"));
3714 // Creates and returns a notification object for receiving IOKit
3715 // notifications of new devices or state changes.
3716 kr
= IOMasterPort(bootstrap_port
, &masterPort
);
3717 if (kr
!= KERN_SUCCESS
) {
3718 SC_log(LOG_ERR
, "IOMasterPort returned 0x%x", kr
);
3722 S_notify
= IONotificationPortCreate(masterPort
);
3723 if (S_notify
== NULL
) {
3724 SC_log(LOG_ERR
, "IONotificationPortCreate failed");
3728 // watch IOKit matching activity
3729 root
= IORegistryEntryFromPath(masterPort
, kIOServicePlane
":/");
3730 if (root
== MACH_PORT_NULL
) {
3731 SC_log(LOG_ERR
, "IORegistryEntryFromPath failed");
3735 kr
= IOServiceAddInterestNotification(S_notify
,
3739 (void *)S_notify
, // refCon
3740 &S_quiet
); // notification
3741 if (kr
!= KERN_SUCCESS
) {
3742 SC_log(LOG_ERR
, "IOServiceAddInterestNotification returned 0x%x", kr
);
3746 kr
= IOServiceGetBusyState(root
, &busy
);
3747 if (kr
!= KERN_SUCCESS
) {
3748 SC_log(LOG_ERR
, "IOServiceGetBusyState returned 0x%x", kr
);
3752 // add a timer so we don't wait forever for IOKit to quiesce
3753 S_timer
= CFRunLoopTimerCreate(NULL
,
3754 CFAbsoluteTimeGetCurrent() + S_stack_timeout
,
3760 if (S_timer
== NULL
) {
3761 SC_log(LOG_ERR
, "CFRunLoopTimerCreate failed");
3765 CFRunLoopAddTimer(CFRunLoopGetCurrent(), S_timer
, kCFRunLoopDefaultMode
);
3767 // watch for the introduction of the IONetworkStack
3768 kr
= IOServiceAddMatchingNotification(S_notify
,
3769 kIOFirstMatchNotification
,
3770 IOServiceMatching("IONetworkStack"),
3772 (void *)S_notify
, // refCon
3773 &S_stack
); // notification
3774 if (kr
!= KERN_SUCCESS
) {
3775 SC_log(LOG_ERR
, "IOServiceAddMatchingNotification returned 0x%x", kr
);
3779 // check and see if the stack is already available and arm the
3780 // notification for its introduction.
3781 stackCallback((void *)S_notify
, S_stack
);
3783 // watch for the introduction of new network interfaces
3784 kr
= IOServiceAddMatchingNotification(S_notify
,
3785 kIOFirstMatchNotification
,
3786 IOServiceMatching("IONetworkInterface"),
3787 &interfaceArrivalCallback
,
3788 (void *)S_notify
, // refCon
3789 &S_iter
); // notification
3790 if (kr
!= KERN_SUCCESS
) {
3791 SC_log(LOG_ERR
, "IOServiceAddMatchingNotification returned 0x%x", kr
);
3795 // Get the current list of matches and arm the notification for
3796 // future interface arrivals.
3797 interfaceArrivalCallback((void *)S_notify
, S_iter
);
3799 // Check if IOKit has already quiesced.
3800 quietCallback((void *)S_notify
,
3802 kIOMessageServiceBusyStateChange
,
3803 (void *)(uintptr_t)busy
);
3805 CFRunLoopAddSource(CFRunLoopGetCurrent(),
3806 IONotificationPortGetRunLoopSource(S_notify
),
3807 kCFRunLoopDefaultMode
);
3809 #ifdef WAIT_PREVIOUS_BOOT_INTERFACES_OR_QUIET
3811 * Start the wheels turning until we've named all of
3812 * the interfaces that were used during the previous
3813 * boot, until IOKit [matching] has quiesced, or
3814 * until we've waited long enough.
3816 CFRunLoopAddTimer(CFRunLoopGetCurrent(), S_timer
, MY_PLUGIN_ID
);
3817 CFRunLoopAddSource(CFRunLoopGetCurrent(),
3818 IONotificationPortGetRunLoopSource(S_notify
),
3820 while (S_prev_active_list
!= NULL
) {
3823 rlStatus
= CFRunLoopRunInMode(MY_PLUGIN_ID
, 1.0e10
, TRUE
);
3825 #endif /* WAIT_PREVIOUS_BOOT_INTERFACES_OR_QUIET */
3828 if (S_dblist
!= NULL
) {
3829 // apply special handling for the BT-PAN interface (if present)
3830 CFArrayApplyFunction(S_dblist
,
3831 CFRangeMake(0, CFArrayGetCount(S_dblist
)),
3832 updateBTPANInformation
,
3835 #endif // TARGET_OS_OSX
3840 if (root
!= MACH_PORT_NULL
) {
3841 IOObjectRelease(root
);
3843 if (masterPort
!= MACH_PORT_NULL
) {
3844 mach_port_deallocate(mach_task_self(), masterPort
);
3851 setup_Virtual(CFBundleRef bundle
)
3853 #pragma unused(bundle)
3854 // open a SCPreferences session
3855 S_prefs
= SCPreferencesCreate(NULL
, CFSTR(MY_PLUGIN_NAME
":setup_Virtual"), NULL
);
3856 if (S_prefs
== NULL
) {
3857 SC_log(LOG_ERR
, "SCPreferencesCreate() failed: %s",
3858 SCErrorString(SCError()));
3862 // register for change notifications.
3863 if (!SCPreferencesSetCallback(S_prefs
, updateVirtualNetworkInterfaceConfiguration
, NULL
)) {
3864 SC_log(LOG_ERR
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
3870 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
)) {
3871 if (SCError() != kSCStatusNoStoreServer
) {
3872 SC_log(LOG_ERR
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
3882 exec_InterfaceNamer(void *arg
)
3884 CFBundleRef bundle
= (CFBundleRef
)arg
;
3885 CFDictionaryRef dict
;
3887 pthread_setname_np(MY_PLUGIN_NAME
" thread");
3889 dict
= CFBundleGetInfoDictionary(bundle
);
3890 if (isA_CFDictionary(dict
)) {
3893 num
= CFDictionaryGetValue(dict
, CFSTR(WAIT_STACK_TIMEOUT_KEY
));
3895 if (!isA_CFNumber(num
) ||
3896 !CFNumberGetValue(num
, kCFNumberDoubleType
, &S_stack_timeout
) ||
3897 (S_stack_timeout
<= 0.0)) {
3898 SC_log(LOG_NOTICE
, WAIT_STACK_TIMEOUT_KEY
" value error");
3899 S_stack_timeout
= WAIT_STACK_TIMEOUT_DEFAULT
;
3903 num
= CFDictionaryGetValue(dict
, CFSTR(WAIT_QUIET_TIMEOUT_KEY
));
3905 if (!isA_CFNumber(num
) ||
3906 !CFNumberGetValue(num
, kCFNumberDoubleType
, &S_quiet_timeout
) ||
3907 (S_quiet_timeout
<= 0.0)) {
3908 SC_log(LOG_NOTICE
, WAIT_QUIET_TIMEOUT_KEY
" value error");
3909 S_quiet_timeout
= WAIT_QUIET_TIMEOUT_DEFAULT
;
3914 // setup virtual network interface monitoring
3915 if (!setup_Virtual(bundle
)) {
3919 // setup [IOKit] network interface monitoring
3920 if (!setup_IOKit(bundle
)) {
3927 if (S_connect
!= MACH_PORT_NULL
) {
3928 IOServiceClose(S_connect
);
3929 S_connect
= MACH_PORT_NULL
;
3931 if (S_dblist
!= NULL
) {
3932 CFRelease(S_dblist
);
3935 if (S_iter
!= MACH_PORT_NULL
) {
3936 IOObjectRelease(S_iter
);
3937 S_iter
= MACH_PORT_NULL
;
3939 if (S_notify
!= MACH_PORT_NULL
) {
3940 IONotificationPortDestroy(S_notify
);
3942 if (S_quiet
!= MACH_PORT_NULL
) {
3943 IOObjectRelease(S_quiet
);
3944 S_quiet
= MACH_PORT_NULL
;
3946 if (S_stack
!= MACH_PORT_NULL
) {
3947 IOObjectRelease(S_stack
);
3948 S_stack
= MACH_PORT_NULL
;
3950 if (S_state
!= NULL
) {
3954 if (S_timer
!= NULL
) {
3955 CFRunLoopTimerInvalidate(S_timer
);
3969 load_InterfaceNamer(CFBundleRef bundle
, Boolean bundleVerbose
)
3971 #pragma unused(bundleVerbose)
3972 pthread_attr_t tattr
;
3975 CFRetain(bundle
); // released in exec_InterfaceNamer
3977 pthread_attr_init(&tattr
);
3978 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
3979 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
3980 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
3981 pthread_create(&tid
, &tattr
, exec_InterfaceNamer
, bundle
);
3982 pthread_attr_destroy(&tattr
);
3987 //------------------------------------------------------------------------
3989 #ifdef TEST_INTERFACE_ASSIGNMENT
3991 main(int argc
, char ** argv
)
3993 #pragma unused(argv)
3995 CFMutableArrayRef interfaces
;
3996 CFArrayRef interfaces_all
;
3999 _sc_log
= kSCLogDestinationFile
;
4000 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
4002 bundle
= CFBundleGetMainBundle();
4003 CFRetain(bundle
); // released in exec_InterfaceNamer
4006 setup_IOKit(bundle
);
4008 // but, when running this test we know that the IORegistry has already quiesced
4009 IOObjectRelease(S_quiet
);
4010 S_quiet
= MACH_PORT_NULL
;
4012 // collect the interfaces
4013 interfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
4014 interfaces_all
= SCNetworkInterfaceCopyAll();
4015 n
= CFArrayGetCount(interfaces_all
);
4016 for (CFIndex i
= 0; i
< n
; i
++) {
4017 SCNetworkInterfaceRef interface
= CFArrayGetValueAtIndex(interfaces_all
, i
);
4018 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)interface
;
4020 if (interfacePrivate
->type
== NULL
) {
4021 // skip interfaces with a kIOInterfaceType property
4025 if (interfacePrivate
->unit
!= NULL
) {
4026 // remove any already assigned unit #
4027 CFRelease(interfacePrivate
->unit
);
4028 interfacePrivate
->unit
= NULL
;
4031 CFArrayAppendValue(interfaces
, interface
);
4033 CFRelease(interfaces_all
);
4034 SC_log(LOG_INFO
, "interfaces = %@", interfaces
);
4036 // exercise the interface naming assignments
4037 nameInterfaces(interfaces
);
4038 CFRelease(interfaces
);
4045 #ifdef TEST_SNAPSHOT
4047 main(int argc
, char ** argv
)
4049 _sc_log
= kSCLogDestinationFile
;
4050 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
4057 #endif /* TEST_SNAPSHOT */