2 * Copyright (c) 2014-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 // SCNetworkMigration.c
27 // Created by Ashish Kulkarni on 11/19/13.
32 #include <CoreFoundation/CoreFoundation.h>
33 #include "SCNetworkConfigurationInternal.h"
34 #include "SCPreferencesInternal.h"
35 #include <IOKit/network/IONetworkInterface.h>
36 #include <IOKit/network/IONetworkController.h>
37 #if !TARGET_OS_SIMULATOR
38 #include <IOKit/usb/USB.h>
39 #endif // !TARGET_OS_SIMULATOR
42 #include <sys/param.h>
45 #define BACK_TO_MY_MAC CFSTR("BackToMyMac")
46 #define BACK_TO_MY_MAC_DSIDS CFSTR("BackToMyMacDSIDs")
47 #define NUM_MIGRATION_PATHS 2
48 #define PLUGIN_ID CFSTR("System Migration")
49 #define PREFERENCES_PLIST_INDEX 0
50 #define NETWORK_INTERFACES_PLIST_INDEX 1
52 #define kProtocolType CFSTR("Protocol Type")
53 #define kProtocolConfiguration CFSTR("Protocol Configuration")
54 #define kProtocolEnabled CFSTR("Protocol Enabled")
57 const CFStringRef kSCNetworkConfigurationMigrationActionKey
= CFSTR("MigrationActionKey");
58 const CFStringRef kSCNetworkConfigurationRepair
= CFSTR("ConfigurationRepair");
59 const CFStringRef kSCNetworkConfigurationRepairModel
= CFSTR("ConfigurationRepairModel");
63 logInterfaces(int level
, const char *description
, CFArrayRef interfaces
, Boolean detailed
)
65 CFMutableArrayRef interfaceNames
= NULL
;
68 SC_log(level
, "%s = ", description
);
70 interfaceNames
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
73 for (CFIndex i
= 0, n
= CFArrayGetCount(interfaces
); i
< n
; i
++) {
75 SCNetworkInterfaceRef interface
;
76 CFStringRef userDefinedName
= NULL
;
78 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
79 if (isA_SCNetworkInterface(interface
)) {
80 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
81 if (bsdName
== NULL
) {
84 userDefinedName
= __SCNetworkInterfaceGetUserDefinedName(interface
);
85 } else if (isA_CFDictionary(interface
)) {
88 bsdName
= CFDictionaryGetValue((CFDictionaryRef
)interface
, CFSTR(kSCNetworkInterfaceBSDName
));
89 if (bsdName
== NULL
) {
93 info
= CFDictionaryGetValue((CFDictionaryRef
)interface
, CFSTR(kSCNetworkInterfaceInfo
));
95 userDefinedName
= CFDictionaryGetValue(info
, kSCPropUserDefinedName
);
101 if (interfaceNames
== NULL
) {
102 SC_log(level
, " %@ (%@)",
104 userDefinedName
!= NULL
? userDefinedName
: CFSTR("?"));
106 CFArrayAppendValue(interfaceNames
, bsdName
);
110 if (interfaceNames
!= NULL
) {
113 list
= CFStringCreateByCombiningStrings(NULL
, interfaceNames
, CFSTR(", "));
114 SC_log(level
, "%s = %@", description
, list
);
117 CFRelease(interfaceNames
);
132 * Logs a single key/value of a mapping dictionary
135 logMapping_one(const void *key
, const void *value
, void *context
)
137 #pragma unused(context)
138 logMappingContext
* mapping_context
= (logMappingContext
*)context
;
139 CFTypeRef mapping_key
= NULL
;
140 CFTypeRef mapping_value
= NULL
;
141 Boolean mapping_value_retained
= FALSE
;
143 if (mapping_context
->name
!= NULL
) {
144 SC_log(mapping_context
->level
, "%s =", mapping_context
->name
);
145 mapping_context
->name
= NULL
;
148 if (isA_SCNetworkService(key
)) {
149 mapping_key
= SCNetworkServiceGetServiceID(key
);
150 } else if (isA_SCNetworkSet(key
)) {
151 mapping_key
= SCNetworkSetGetSetID(key
);
152 } else if (isA_SCNetworkInterface(key
)) {
153 mapping_key
= SCNetworkInterfaceGetBSDName(key
);
158 if (isA_SCNetworkService(value
)) {
159 mapping_value
= SCNetworkServiceGetServiceID(value
);
160 } else if (isA_SCNetworkSet(value
)) {
161 mapping_value
= SCNetworkSetGetSetID(value
);
162 } else if (isA_SCNetworkInterface(value
)) {
164 CFStringRef userDefinedName
;
166 bsdName
= SCNetworkInterfaceGetBSDName(value
);
167 userDefinedName
= __SCNetworkInterfaceGetUserDefinedName(value
);
168 mapping_value
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@ (%@)"),
170 userDefinedName
!= NULL
? userDefinedName
: CFSTR("?"));
171 mapping_value_retained
= TRUE
;
172 } else if (isA_CFBoolean(value
)) {
173 mapping_value
= CFSTR("None");
174 } else if (isA_CFArray(value
)) {
175 CFIndex n
= CFArrayGetCount(value
);
176 CFMutableStringRef str
= CFStringCreateMutable(NULL
, 0);
178 CFStringAppendFormat(str
, NULL
, CFSTR("( "));
179 for (CFIndex i
= 0; i
< n
; i
++) {
182 val
= CFArrayGetValueAtIndex(value
, i
);
183 if (isA_SCNetworkSet(val
)) {
184 val
= SCNetworkSetGetSetID(val
);
186 CFStringAppendFormat(str
, NULL
, CFSTR("%s%@"),
192 CFStringAppendFormat(str
, NULL
, CFSTR(" )"));
194 mapping_value_retained
= TRUE
;
196 mapping_value
= value
;
199 SC_log(mapping_context
->level
, " %@ --> %@", mapping_key
, mapping_value
);
201 if (mapping_value_retained
) {
202 CFRelease(mapping_value
);
210 * Generates a log of the mappings between :
212 * bsdNameServiceProtocolPreserveMapping
213 * SCNetworkService/SCNetworkInterface/bsdName
214 * --> array<info about protocols in the associated service>
216 * mappingServiceBSDNameToInterface
217 * SCNetworkInterface/bsdName
218 * --> SCNetworkInterface
221 * [source]interface BSD name
222 * --> [target]interface BSD name
224 * externalMapping (like bsdNameMapping but only for "external" interfaces)
226 * serviceMapping (matching on SCNetworkService/SCNetworkInterface/bsdName)
227 * [source]SCNetworkService
228 * --> [target]SCNetworkService
230 * setMapping (matching on the SCNetworkSet "name")
231 * [source]SCNetworkSet
232 * --> [target]SCNetworkSet
236 * --> array<SCNetworkSet's containing the service>
239 logMapping(int level
, CFDictionaryRef mapping
, const char *name
)
241 logMappingContext mappingContext
= { .level
= level
, .name
= name
};
243 CFDictionaryApplyFunction(mapping
, logMapping_one
, &mappingContext
);
247 #if !TARGET_OS_IPHONE
248 static CFDictionaryRef
249 _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(SCPreferencesRef prefs
);
251 static CFDictionaryRef
252 _SCNetworkMigrationCopyMappingBSDNameToBondServices(SCPreferencesRef prefs
);
254 static CFDictionaryRef
255 _SCNetworkMigrationCopyMappingBSDNameToVLANServices(SCPreferencesRef prefs
);
256 #endif // !TARGET_OS_IPHONE
259 _SCNetworkConfigurationIsInterfaceNamerMappable(SCNetworkInterfaceRef interface1
, SCNetworkInterfaceRef interface2
, Boolean bypassActive
);
262 _SCNetworkConfigurationMigrateConfiguration(CFURLRef sourceDir
, CFURLRef targetDir
);
265 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(CFURLRef baseURL
, CFURLRef
*prefs
, CFURLRef
*interfaces
)
267 if (baseURL
!= NULL
) {
270 baseURL
= CFURLCreateFromFileSystemRepresentation(NULL
,
271 (UInt8
*)PREFS_DEFAULT_DIR_PATH
,
272 sizeof(PREFS_DEFAULT_DIR_PATH
) - 1,
276 *prefs
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
277 (UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
,
278 sizeof(PREFS_DEFAULT_CONFIG_PLIST
) - 1,
282 *interfaces
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
283 (UInt8
*)INTERFACES_DEFAULT_CONFIG_PLIST
,
284 sizeof(INTERFACES_DEFAULT_CONFIG_PLIST
) - 1,
292 _SCNetworkConfigurationCopyMigrationPaths(CFDictionaryRef options
)
294 #pragma unused(options)
296 CFMutableArrayRef migrationPaths
= NULL
;
299 migrationPaths
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
300 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(NULL
, &prefs
, &interfaces
);
301 CFArrayAppendValue(migrationPaths
, prefs
);
302 CFArrayAppendValue(migrationPaths
, interfaces
);
304 CFRelease(interfaces
);
306 return migrationPaths
;
310 _SCNetworkConfigurationRemoveConfigurationFiles(CFURLRef configDir
)
313 char configPathString
[PATH_MAX
];
314 CFURLRef configPathURL
= NULL
;
315 char configNetworkInterfacesPathString
[PATH_MAX
];
316 CFURLRef configNetworkInterfacesPathURL
= NULL
;
318 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(configDir
, &configPathURL
, &configNetworkInterfacesPathURL
);
320 if(!CFURLGetFileSystemRepresentation(configPathURL
,
322 (UInt8
*)configPathString
,
323 sizeof(configPathString
))) {
324 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPathURL
);
326 if ((remove(configPathString
) != 0) && (errno
!= ENOENT
)) {
327 SC_log(LOG_NOTICE
, "remove(\"%s\") failed: %s", configPathString
, strerror(errno
));
330 CFRelease(configPathURL
);
332 if(!CFURLGetFileSystemRepresentation(configNetworkInterfacesPathURL
,
334 (UInt8
*)configNetworkInterfacesPathString
,
335 sizeof(configNetworkInterfacesPathString
))) {
336 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfacesPathURL
);
338 if ((remove(configNetworkInterfacesPathString
) != 0) && (errno
!= ENOENT
)) {
339 SC_log(LOG_NOTICE
, "remove(\"%s\") failed: %s", configNetworkInterfacesPathString
, strerror(errno
));
342 CFRelease(configNetworkInterfacesPathURL
);
348 SCNetworkConfigurationCopyConfigurationFiles(CFURLRef configDir
,
349 CFURLRef targetDir
) // TargetDir needs to exist
352 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
353 char networkInterfacesPathString
[PATH_MAX
];
354 CFURLRef networkInterfacesPathURL
= NULL
;
355 copyfile_state_t networkInterfacesState
;
356 char preferencesPathString
[PATH_MAX
];
357 CFURLRef preferencesPathURL
= NULL
;
358 Boolean removeTargetFiles
= FALSE
;
359 copyfile_state_t state
;
360 Boolean success
= FALSE
;
361 char targetNetworkInterfacesPathString
[PATH_MAX
];
362 CFURLRef targetNetworkInterfacesPathURL
= NULL
;
363 char targetPathString
[PATH_MAX
];
364 CFURLRef targetPathURL
= NULL
;
366 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(targetDir
, &targetPathURL
, &targetNetworkInterfacesPathURL
);
368 if (!CFURLGetFileSystemRepresentation(targetPathURL
,
370 (UInt8
*)targetPathString
,
371 sizeof(targetPathString
))) {
372 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetPathURL
);
375 if (!CFURLGetFileSystemRepresentation(targetNetworkInterfacesPathURL
,
377 (UInt8
*)targetNetworkInterfacesPathString
,
378 sizeof(targetNetworkInterfacesPathString
))) {
379 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetNetworkInterfacesPathURL
);
383 if (configDir
== NULL
) {
384 removeTargetFiles
= TRUE
;
389 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(configDir
, &preferencesPathURL
, &networkInterfacesPathURL
);
391 if (!CFURLGetFileSystemRepresentation(preferencesPathURL
,
393 (UInt8
*)preferencesPathString
,
394 sizeof(preferencesPathString
))) {
395 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", preferencesPathURL
);
398 if (!CFURLGetFileSystemRepresentation(networkInterfacesPathURL
,
400 (UInt8
*)networkInterfacesPathString
,
401 sizeof(networkInterfacesPathString
))) {
402 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", networkInterfacesPathURL
);
406 state
= copyfile_state_alloc();
407 error
= copyfile(preferencesPathString
, targetPathString
, state
, COPYFILE_ALL
);
409 SC_log(LOG_NOTICE
, "copyFile(\"%s\", \"%s\", ...) failed: %s",
410 preferencesPathString
,
413 copyfile_state_free(state
);
414 removeTargetFiles
= TRUE
;
417 copyfile_state_free(state
);
418 (void)chmod(targetPathString
, mode
);
420 networkInterfacesState
= copyfile_state_alloc();
421 error
= copyfile(networkInterfacesPathString
, targetNetworkInterfacesPathString
, networkInterfacesState
, COPYFILE_ALL
);
423 SC_log(LOG_NOTICE
, "copyFile(\"%s\", \"%s\", ...) failed: %s",
424 networkInterfacesPathString
,
425 targetNetworkInterfacesPathString
,
427 copyfile_state_free(networkInterfacesState
);
428 removeTargetFiles
= TRUE
;
431 copyfile_state_free(networkInterfacesState
);
432 (void)chmod(targetNetworkInterfacesPathString
, mode
);
436 if (removeTargetFiles
) {
437 _SCNetworkConfigurationRemoveConfigurationFiles(targetDir
);
439 if (preferencesPathURL
!= NULL
) {
440 CFRelease(preferencesPathURL
);
442 if (networkInterfacesPathURL
!= NULL
) {
443 CFRelease(networkInterfacesPathURL
);
445 if (targetPathURL
!= NULL
) {
446 CFRelease(targetPathURL
);
448 if (targetNetworkInterfacesPathURL
!= NULL
) {
449 CFRelease(targetNetworkInterfacesPathURL
);
455 /* -----------------------------------------------------------------------------
456 Create directories and intermediate directories as required.
457 ----------------------------------------------------------------------------- */
459 _SCNetworkConfigurationMakePathIfNeeded(CFURLRef pathURL
)
463 Boolean success
= FALSE
;
465 if (!CFURLGetFileSystemRepresentation(pathURL
, TRUE
, (UInt8
*)path
, sizeof(path
))) {
466 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", pathURL
);
470 SC_log(LOG_INFO
, "creating path: %s", path
);
474 c
++; // skip leading /
476 for(; !success
; c
++) {
477 if ((*c
== '/') || (*c
== '\0')){
483 if (mkdir(path
, (S_IRWXU
| S_IRGRP
| S_IROTH
| S_IXGRP
| S_IXOTH
)) != 0) {
484 if ((errno
!= EEXIST
) && (errno
!= EISDIR
)) {
485 SC_log(LOG_NOTICE
, "mkdir(%s) failed: %s", path
, strerror(errno
));
497 __SCNetworkPopulateDefaultPrefs(SCPreferencesRef prefs
)
499 SCNetworkSetRef currentSet
;
504 "Populating preferences.plist"
508 currentSet
= SCNetworkSetCopyCurrent(prefs
);
509 if (currentSet
== NULL
) {
510 currentSet
= _SCNetworkSetCreateDefault(prefs
);
512 SCNetworkSetEstablishDefaultConfiguration(currentSet
);
513 CFRelease(currentSet
);
515 model
= SCPreferencesGetValue(prefs
, MODEL
);
517 model
= _SC_hw_model(FALSE
);
518 SCPreferencesSetValue(prefs
, MODEL
, model
);
521 version
= SCPreferencesGetValue(prefs
, kSCPrefVersion
);
522 if (version
== NULL
) {
523 const int new_version
= NETWORK_CONFIGURATION_VERSION
;
525 version
= CFNumberCreate(NULL
, kCFNumberIntType
, &new_version
);
526 SCPreferencesSetValue(prefs
, kSCPrefVersion
, version
);
534 __SCNetworkPopulateDefaultNIPrefs(SCPreferencesRef ni_prefs
)
536 CFMutableArrayRef interfaces
= NULL
;
538 CFArrayRef networkInterfaces
;
539 CFComparisonResult res
;
542 interfaces
= (CFMutableArrayRef
)SCPreferencesGetValue(ni_prefs
, INTERFACES
);
543 if (isA_CFArray(interfaces
)) {
544 // if already populated
549 "Populating NetworkInterfaces.plist"
553 networkInterfaces
= __SCNetworkInterfaceCopyAll_IONetworkInterface(TRUE
);
554 if (networkInterfaces
== NULL
) {
555 SC_log(LOG_NOTICE
, "Cannot populate NetworkInterfaces.plist, no network interfaces");
559 interfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
561 for (CFIndex idx
= 0; idx
< CFArrayGetCount(networkInterfaces
); idx
++) {
565 SCNetworkInterfaceRef interface
= CFArrayGetValueAtIndex(networkInterfaces
, idx
);
566 CFDictionaryRef interfaceEntity
;
568 interfaceEntity
= __SCNetworkInterfaceCopyStorageEntity(interface
);
569 if (interfaceEntity
== NULL
) {
573 if_type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
574 if_unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
575 if ((if_type
== NULL
) || (if_unit
== NULL
)) {
576 CFRelease(interfaceEntity
);
580 for (idx2
= 0; idx2
< CFArrayGetCount(interfaces
); idx2
++) {
583 CFDictionaryRef dict
= CFArrayGetValueAtIndex(interfaces
, idx2
);
585 db_type
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
586 db_unit
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceUnit
));
587 res
= CFNumberCompare(if_type
, db_type
, NULL
);
588 if (res
== kCFCompareLessThan
589 || (res
== kCFCompareEqualTo
590 && (CFNumberCompare(if_unit
, db_unit
, NULL
) == kCFCompareLessThan
))) {
595 CFArrayInsertValueAtIndex(interfaces
, idx2
, interfaceEntity
);
596 CFRelease(interfaceEntity
);
600 SCPreferencesSetValue(ni_prefs
, INTERFACES
, interfaces
);
601 CFRelease(interfaces
);
603 model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
605 model
= _SC_hw_model(FALSE
);
606 SCPreferencesSetValue(ni_prefs
, MODEL
, model
);
609 version
= SCPreferencesGetValue(ni_prefs
, kSCPrefVersion
);
610 if (version
== NULL
) {
611 const int new_version
= NETWORK_CONFIGURATION_VERSION
;
613 version
= CFNumberCreate(NULL
, kCFNumberIntType
, &new_version
);
614 SCPreferencesSetValue(ni_prefs
, kSCPrefVersion
, version
);
618 CFRelease(networkInterfaces
);
625 * _SCNetworkConfigurationPerformMigration will migrate configuration between source and destination systems
629 _SCNetworkConfigurationPerformMigration(CFURLRef sourceDir
, CFURLRef currentDir
, CFURLRef targetDir
, CFDictionaryRef options
)
631 #pragma unused(options)
632 CFURLRef currentDirConfig
= NULL
;
633 CFURLRef currentSystemPath
= NULL
;
634 Boolean migrationComplete
= FALSE
;
635 CFArrayRef paths
= NULL
;
636 Boolean removeTargetOnFailure
= FALSE
;
637 CFURLRef sourceDirConfig
= NULL
;
638 CFURLRef targetDirConfig
= NULL
;
641 "Perform Migration%s"
646 _SC_isInstallEnvironment() ? " (INSTALLER ENVIRONMENT)" : "",
650 options
!= NULL
? options
: (CFDictionaryRef
)CFSTR("None"));
652 if ((sourceDir
!= NULL
) && !CFURLHasDirectoryPath(sourceDir
)) {
653 SC_log(LOG_NOTICE
, "Migration source is not a directory: %@", sourceDir
);
657 if ((currentDir
!= NULL
) && !CFURLHasDirectoryPath(currentDir
)) {
658 SC_log(LOG_NOTICE
, "Migration current is not a directory: %@", currentDir
);
662 if ((targetDir
!= NULL
) && !CFURLHasDirectoryPath(targetDir
)) {
663 SC_log(LOG_NOTICE
, "Migration target is not a directory: %@", targetDir
);
667 // Both sourceDir and currentDir cannot be NULL because NULL value indicates using current system
668 if (sourceDir
== NULL
&& currentDir
== NULL
) {
669 SC_log(LOG_NOTICE
, "Both migration source and current are NULL");
673 currentSystemPath
= CFURLCreateWithString(NULL
,
677 // if either of the sourceDir or currentDir are NULL, then populate it with current system path
678 if (sourceDir
== NULL
) {
679 sourceDirConfig
= CFRetain(currentSystemPath
);
681 sourceDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
682 PREFS_DEFAULT_DIR_RELATIVE
,
683 kCFURLPOSIXPathStyle
,
688 if (currentDir
!= NULL
) {
689 currentDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
690 PREFS_DEFAULT_DIR_RELATIVE
,
691 kCFURLPOSIXPathStyle
,
695 // If the targetDir is not provided then migration will take place in currentDir
696 if (targetDir
== NULL
) {
697 targetDirConfig
= CFRetain(currentSystemPath
);
699 targetDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
700 PREFS_DEFAULT_DIR_RELATIVE
,
701 kCFURLPOSIXPathStyle
,
705 // Source directory cannot be the same as target directory
706 if (CFEqual(sourceDirConfig
, targetDirConfig
)) {
707 SC_log(LOG_NOTICE
, "Source directory cannot be the same as target directory");
711 if ((currentDirConfig
== NULL
) || !CFEqual(currentDirConfig
, targetDirConfig
)) {
712 if (!_SCNetworkConfigurationMakePathIfNeeded(targetDirConfig
)) {
713 SC_log(LOG_NOTICE
, "Could not create target directory");
717 if (!SCNetworkConfigurationCopyConfigurationFiles(currentDirConfig
, targetDirConfig
)) {
718 SC_log(LOG_NOTICE
, "Could not copy configuration files from \"%@\" to \"%@\"",
721 } else if (currentDirConfig
!= NULL
) {
722 removeTargetOnFailure
= TRUE
; // Configuration files were copied over to target directory
723 // If migration failed, then we should remove those configuration
724 // files since current directory and target directory are not
729 // If both source and current configurations point to current system, then no migration needs to be done.
730 if ((currentDirConfig
!= NULL
) && CFEqual(sourceDirConfig
, currentDirConfig
)) {
731 SC_log(LOG_NOTICE
, "No migration needed, source and current configurations have the same path");
732 migrationComplete
= TRUE
;
734 migrationComplete
= _SCNetworkConfigurationMigrateConfiguration(sourceDirConfig
, targetDirConfig
);
736 if (migrationComplete
) {
737 SC_log(LOG_NOTICE
, "Migration complete");
738 paths
= _SCNetworkConfigurationCopyMigrationPaths(NULL
);
740 SC_log(LOG_NOTICE
, "Migration failed: %s", SCErrorString(SCError()));
742 // If migration fails, then remove configuration files from target config if they were
743 // copied from the current directory
744 if (removeTargetOnFailure
) {
745 _SCNetworkConfigurationRemoveConfigurationFiles(targetDirConfig
);
749 if (currentDirConfig
!= NULL
) {
750 CFRelease(currentDirConfig
);
752 if (currentSystemPath
!= NULL
) {
753 CFRelease(currentSystemPath
);
755 if (sourceDirConfig
!= NULL
) {
756 CFRelease(sourceDirConfig
);
758 if (targetDirConfig
!= NULL
) {
759 CFRelease(targetDirConfig
);
766 _SCNetworkConfigurationMigrateIsFilePresent(CFURLRef filePath
)
768 Boolean fileExists
= false;
769 char filePathStr
[PATH_MAX
];
771 struct stat statStruct
= { 0, };
773 if (filePath
== NULL
) {
774 SC_log(LOG_NOTICE
, "_SCNetworkConfigurationMigrateIsFilePresent: No path");
778 if (!CFURLGetFileSystemRepresentation(filePath
, TRUE
, (UInt8
*) filePathStr
, sizeof(filePathStr
))) {
779 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", filePath
);
783 statResult
= stat(filePathStr
, &statStruct
);
784 if (statResult
== 0) {
785 SCPreferencesRef prefs
;
788 if (statStruct
.st_size
== 0) {
789 SC_log(LOG_INFO
, "_SCNetworkConfigurationMigrateIsFilePresent: empty .plist: %@", filePath
); // REMOVE
793 prefsID
= CFStringCreateWithCString(NULL
, filePathStr
, kCFStringEncodingUTF8
);
794 prefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsID
);
797 SC_log(LOG_NOTICE
, "_SCNetworkConfigurationMigrateIsFilePresent: bad .plist: %@", filePath
);
801 if (!__SCPreferencesIsEmpty(prefs
)) {
802 // if non-empty .plist
805 SC_log(LOG_NOTICE
, "_SCNetworkConfigurationMigrateIsFilePresent: effectively empty .plist: %@", filePath
);
815 __SCNetworkConfigurationMigrateConfigurationFilesPresent(CFURLRef baseURL
, CFArrayRef
* migrationPaths
, Boolean expected
)
817 Boolean configFilesPresent
= FALSE
;
819 CFURLRef filePath
= NULL
;
821 CFMutableArrayRef migrationPathsMutable
= NULL
;
824 if (baseURL
== NULL
) {
825 SC_log(LOG_NOTICE
, "No base migration URL");
829 migrationPathsMutable
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
830 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(baseURL
, &prefs
, &interfaces
);
831 CFArrayAppendValue(migrationPathsMutable
, prefs
);
832 CFArrayAppendValue(migrationPathsMutable
, interfaces
);
834 CFRelease(interfaces
);
836 *migrationPaths
= migrationPathsMutable
;
838 count
= CFArrayGetCount(*migrationPaths
);
839 for (CFIndex idx
= 0; idx
< count
; idx
++) {
842 filePath
= CFArrayGetValueAtIndex(*migrationPaths
, idx
);
843 present
= _SCNetworkConfigurationMigrateIsFilePresent(filePath
);
846 SC_log(LOG_INFO
, "Expected migration file not present: %@", filePath
);
852 configFilesPresent
= TRUE
; // all necessary configuration files present
854 return configFilesPresent
;
858 static CFMutableArrayRef
859 _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(SCPreferencesRef ni_prefs
, Boolean isBuiltin
)
862 SCNetworkInterfaceRef interface
;
863 CFArrayRef interfaceList
= NULL
;
864 SCPreferencesRef prefs
= NULL
;
865 CFMutableArrayRef resultInterfaceList
= NULL
;
867 interfaceList
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
868 if (interfaceList
== NULL
) {
872 count
= CFArrayGetCount(interfaceList
);
874 resultInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
875 prefs
= SCPreferencesCreateCompanion(ni_prefs
, NULL
);
878 for (CFIndex i
= 0; i
< count
; i
++) {
879 interface
= CFArrayGetValueAtIndex(interfaceList
, i
);
880 if (_SCNetworkInterfaceIsBuiltin(interface
) == isBuiltin
) {
881 SCNetworkInterfacePrivateRef newInterface
;
883 newInterface
= __SCNetworkInterfaceCreateCopy(NULL
, interface
, prefs
, NULL
);
884 CFArrayAppendValue(resultInterfaceList
, newInterface
);
885 CFRelease(newInterface
);
890 if (interfaceList
!= NULL
) {
891 CFRelease(interfaceList
);
896 return resultInterfaceList
;
899 static CFMutableDictionaryRef
900 _SCNetworkInterfaceStorageCopyMaxUnitPerInterfaceType(SCPreferencesRef ni_prefs
)
902 CFNumberRef cfMaxUnit
;
904 CFArrayRef ifList
= NULL
;
905 SCNetworkInterfaceRef interface
;
906 CFMutableDictionaryRef interfaceTypeToMaxUnitMapping
= NULL
;
910 ifList
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
911 if (ifList
== NULL
) {
912 SC_log(LOG_INFO
, "No interfaces");
916 interfaceTypeToMaxUnitMapping
= CFDictionaryCreateMutable(NULL
, 0,
917 &kCFTypeDictionaryKeyCallBacks
,
918 &kCFTypeDictionaryValueCallBacks
);
919 count
= CFArrayGetCount(ifList
);
920 for (CFIndex idx
= 0; idx
< count
; idx
++) {
922 interface
= CFArrayGetValueAtIndex(ifList
, idx
);
924 if (!isA_SCNetworkInterface(interface
)) {
928 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
929 if (!isA_CFNumber(type
)) {
930 SC_log(LOG_INFO
, "No interface type");
934 if (!CFDictionaryContainsKey(interfaceTypeToMaxUnitMapping
, type
)) {
936 cfMaxUnit
= CFNumberCreate(NULL
, kCFNumberIntType
, &temp
);
937 CFDictionaryAddValue(interfaceTypeToMaxUnitMapping
, type
, cfMaxUnit
);
938 CFRelease(cfMaxUnit
);
941 if (cfMaxUnit
== NULL
) {
942 cfMaxUnit
= CFDictionaryGetValue(interfaceTypeToMaxUnitMapping
, type
);
945 unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
946 if (!isA_CFNumber(unit
)) {
950 if (CFNumberCompare(unit
, cfMaxUnit
, NULL
) == kCFCompareGreaterThan
) {
951 CFDictionarySetValue(interfaceTypeToMaxUnitMapping
, type
, unit
);
954 if (ifList
!= NULL
) {
957 return interfaceTypeToMaxUnitMapping
;
960 static CFMutableDictionaryRef
961 _SCNetworkConfigurationCopyBuiltinMapping (SCPreferencesRef sourcePrefs
, SCPreferencesRef targetPrefs
)
963 CFMutableDictionaryRef builtinMapping
= NULL
;
964 CFIndex sourceBuiltinInterfaceCount
= 0;
965 CFMutableArrayRef sourceBuiltinInterfaces
= NULL
;
966 SCNetworkInterfaceRef sourceInterface
;
967 CFIndex targetBuiltinInterfaceCount
= 0;
968 CFMutableArrayRef targetBuiltinInterfaces
= NULL
;
969 SCNetworkInterfaceRef targetInterface
;
971 sourceBuiltinInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(sourcePrefs
, TRUE
);
972 if (!isA_CFArray(sourceBuiltinInterfaces
)) {
973 SC_log(LOG_INFO
, "No source built-in interfaces");
976 sourceBuiltinInterfaceCount
= CFArrayGetCount(sourceBuiltinInterfaces
);
978 targetBuiltinInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(targetPrefs
, TRUE
);
979 if (!isA_CFArray(targetBuiltinInterfaces
)) {
980 SC_log(LOG_INFO
, "No target built-in interfaces");
984 // Builtin Mapping will try to map all source interfaces into target interfaces
985 for (CFIndex idx
= 0; idx
< sourceBuiltinInterfaceCount
; idx
++) {
986 Boolean matched
= FALSE
;
988 sourceInterface
= CFArrayGetValueAtIndex(sourceBuiltinInterfaces
, idx
);
989 targetBuiltinInterfaceCount
= CFArrayGetCount(targetBuiltinInterfaces
);
991 for (CFIndex idx2
= 0; idx2
< targetBuiltinInterfaceCount
; idx2
++) {
992 CFDataRef sourceHardwareAddress
;
993 CFDataRef targetHardwareAddress
;
995 targetInterface
= CFArrayGetValueAtIndex(targetBuiltinInterfaces
, idx2
);
996 sourceHardwareAddress
= _SCNetworkInterfaceGetHardwareAddress(sourceInterface
);
997 targetHardwareAddress
= _SCNetworkInterfaceGetHardwareAddress(targetInterface
);
998 if (_SC_CFEqual(sourceHardwareAddress
, targetHardwareAddress
)) {
999 if (builtinMapping
== NULL
) {
1000 builtinMapping
= CFDictionaryCreateMutable(NULL
, 0,
1001 &kCFTypeDictionaryKeyCallBacks
,
1002 &kCFTypeDictionaryValueCallBacks
);
1004 CFDictionaryAddValue(builtinMapping
, sourceInterface
, targetInterface
);
1005 CFArrayRemoveValueAtIndex(targetBuiltinInterfaces
, idx2
);
1011 // proceed to next source interface
1015 for (CFIndex idx2
= 0; idx2
< targetBuiltinInterfaceCount
; idx2
++) {
1016 targetInterface
= CFArrayGetValueAtIndex(targetBuiltinInterfaces
, idx2
);
1018 if (_SCNetworkConfigurationIsInterfaceNamerMappable(sourceInterface
, targetInterface
, FALSE
)) {
1019 if (builtinMapping
== NULL
) {
1020 builtinMapping
= CFDictionaryCreateMutable(NULL
, 0,
1021 &kCFTypeDictionaryKeyCallBacks
,
1022 &kCFTypeDictionaryValueCallBacks
);
1024 CFDictionaryAddValue(builtinMapping
, sourceInterface
, targetInterface
);
1025 CFArrayRemoveValueAtIndex(targetBuiltinInterfaces
, idx2
);
1032 if (sourceBuiltinInterfaces
!= NULL
) {
1033 CFRelease(sourceBuiltinInterfaces
);
1035 if (targetBuiltinInterfaces
!= NULL
) {
1036 CFRelease(targetBuiltinInterfaces
);
1038 return builtinMapping
;
1042 isExcludedExternalInterface(SCNetworkInterfaceRef interface
)
1044 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)interface
;
1045 #if !TARGET_OS_SIMULATOR
1047 #endif // !TARGET_OS_SIMULATOR
1049 if (_SCNetworkInterfaceIsHiddenInterface(interface
)) {
1053 #if !TARGET_OS_SIMULATOR
1054 if (!isA_CFNumber(interfacePrivate
->usb
.vid
) ||
1055 !CFNumberGetValue(interfacePrivate
->usb
.vid
, kCFNumberIntType
, &vid
) ||
1056 (vid
!= kIOUSBAppleVendorID
)) {
1057 // if not "Apple" interface
1060 #endif // !TARGET_OS_SIMULATOR
1062 if (_SC_CFEqual(interfacePrivate
->name
, CFSTR("iBridge"))) {
1063 // exclude "Apple T2 Controller"
1070 static CFMutableDictionaryRef
1071 _SCNetworkConfigurationCopyExternalInterfaceMapping (SCPreferencesRef sourceNIPrefs
, SCPreferencesRef targetNIPrefs
)
1073 CFNumberRef cfMaxTargetUnit
= NULL
;
1074 CFNumberRef currentInterfaceUnit
= NULL
;
1075 CFMutableDictionaryRef externalMapping
= NULL
;
1076 CFMutableDictionaryRef interfaceTypeToMaxUnitMapping
= NULL
;
1079 CFIndex sourceExternalInterfaceCount
= 0;
1080 CFMutableArrayRef sourceExternalInterfaces
= NULL
;
1081 SCNetworkInterfaceRef sourceInterface
= NULL
;
1082 CFIndex targetExternalInterfaceCount
= 0;
1083 CFMutableArrayRef targetExternalInterfaces
= NULL
;
1084 SCNetworkInterfaceRef targetInterface
= NULL
;
1085 SCPreferencesRef targetPrefs
= NULL
;
1088 sourceExternalInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(sourceNIPrefs
, FALSE
);
1089 if (!isA_CFArray(sourceExternalInterfaces
)) {
1090 SC_log(LOG_INFO
, "No source external interfaces");
1093 sourceExternalInterfaceCount
= CFArrayGetCount(sourceExternalInterfaces
);
1094 if (sourceExternalInterfaceCount
== 0) {
1095 SC_log(LOG_INFO
, "No source external interfaces");
1099 targetExternalInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(targetNIPrefs
, FALSE
);
1100 if (!isA_CFArray(targetExternalInterfaces
)) {
1101 SC_log(LOG_INFO
, "No target external interfaces");
1105 interfaceTypeToMaxUnitMapping
= _SCNetworkInterfaceStorageCopyMaxUnitPerInterfaceType(targetNIPrefs
);
1106 externalMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
1107 targetPrefs
= SCPreferencesCreateCompanion(targetNIPrefs
, NULL
);
1109 // Map all external interfaces which exist in both source and target
1110 for (CFIndex idx
= 0; idx
< sourceExternalInterfaceCount
; idx
++) {
1111 Boolean matched
= FALSE
;
1113 sourceInterface
= CFArrayGetValueAtIndex(sourceExternalInterfaces
, idx
);
1114 targetExternalInterfaceCount
= CFArrayGetCount(targetExternalInterfaces
);
1115 currentInterfaceUnit
= NULL
;
1117 for (CFIndex idx2
= 0; idx2
< targetExternalInterfaceCount
; idx2
++) {
1118 CFDataRef sourceHardwareAddress
;
1119 CFDataRef targetHardwareAddress
;
1121 targetInterface
= CFArrayGetValueAtIndex(targetExternalInterfaces
, idx2
);
1122 sourceHardwareAddress
= _SCNetworkInterfaceGetHardwareAddress(sourceInterface
);
1123 targetHardwareAddress
= _SCNetworkInterfaceGetHardwareAddress(targetInterface
);
1124 if (_SC_CFEqual(sourceHardwareAddress
, targetHardwareAddress
)) {
1125 CFDictionaryAddValue(externalMapping
, sourceInterface
, targetInterface
);
1126 CFArrayRemoveValueAtIndex(targetExternalInterfaces
, idx2
);
1132 // proceed to next source interface
1136 for (CFIndex idx2
= 0; idx2
< targetExternalInterfaceCount
; idx2
++) {
1137 targetInterface
= CFArrayGetValueAtIndex(targetExternalInterfaces
, idx2
);
1139 if (_SCNetworkConfigurationIsInterfaceNamerMappable(sourceInterface
, targetInterface
, TRUE
)) {
1140 CFDictionaryAddValue(externalMapping
, sourceInterface
, targetInterface
);
1141 CFArrayRemoveValueAtIndex(targetExternalInterfaces
, idx2
);
1147 // proceed to next source interface
1151 // Before we create mapping for external source interfaces that do not exist
1152 // in the target we need check for and handle some exclusions.
1153 if (isExcludedExternalInterface(sourceInterface
)) {
1158 // Create new mappings for external source interfaces which don't exist in the target
1159 type
= _SCNetworkInterfaceGetIOInterfaceType(sourceInterface
);
1161 cfMaxTargetUnit
= CFDictionaryGetValue(interfaceTypeToMaxUnitMapping
, type
);
1162 if (cfMaxTargetUnit
!= NULL
) {
1163 CFNumberGetValue(cfMaxTargetUnit
, kCFNumberIntType
, &maxTargetUnit
);
1164 newTargetUnit
= maxTargetUnit
+ 1;
1169 cfMaxTargetUnit
= CFNumberCreate(NULL
, kCFNumberIntType
, &newTargetUnit
);
1170 CFDictionarySetValue(interfaceTypeToMaxUnitMapping
, type
, cfMaxTargetUnit
);
1172 targetInterface
= (SCNetworkInterfaceRef
)__SCNetworkInterfaceCreateCopy(NULL
,
1177 currentInterfaceUnit
= _SCNetworkInterfaceGetIOInterfaceUnit(targetInterface
);
1178 if (!isA_CFNumber(currentInterfaceUnit
) ||
1179 !CFEqual(currentInterfaceUnit
, cfMaxTargetUnit
)) {
1180 // Update the interface unit
1181 __SCNetworkInterfaceSetIOInterfaceUnit(targetInterface
, cfMaxTargetUnit
);
1184 CFDictionaryAddValue(externalMapping
, sourceInterface
, targetInterface
);
1186 CFRelease(targetInterface
);
1187 targetInterface
= NULL
;
1188 CFRelease(cfMaxTargetUnit
);
1189 cfMaxTargetUnit
= NULL
;
1193 if (sourceExternalInterfaces
!= NULL
) {
1194 CFRelease(sourceExternalInterfaces
);
1196 if (targetExternalInterfaces
!= NULL
) {
1197 CFRelease(targetExternalInterfaces
);
1199 if (interfaceTypeToMaxUnitMapping
!= NULL
) {
1200 CFRelease(interfaceTypeToMaxUnitMapping
);
1202 if (targetPrefs
!= NULL
) {
1203 CFRelease(targetPrefs
);
1205 return externalMapping
;
1208 __SCNetworkConfigurationInterfaceNameIsEquiv(CFStringRef interfaceName1
, CFStringRef interfaceName2
);
1211 _SCNetworkConfigurationIsInterfaceNamerMappable(SCNetworkInterfaceRef interface1
, SCNetworkInterfaceRef interface2
, Boolean bypassActive
)
1213 #pragma unused(bypassActive)
1214 Boolean interface1IsBuiltin
;
1215 CFStringRef interface1Prefix
;
1216 CFStringRef interface1Type
;
1217 CFStringRef interface1UserDefinedName
;
1218 Boolean interface2IsBuiltin
;
1219 CFStringRef interface2Prefix
;
1220 CFStringRef interface2Type
;
1221 CFStringRef interface2UserDefinedName
;
1223 if (interface1
== interface2
) {
1224 // No work needs to be done
1227 interface1IsBuiltin
= _SCNetworkInterfaceIsBuiltin(interface1
);
1228 interface2IsBuiltin
= _SCNetworkInterfaceIsBuiltin(interface2
);
1230 interface1UserDefinedName
= SCNetworkInterfaceGetLocalizedDisplayName(interface1
);
1231 interface2UserDefinedName
= SCNetworkInterfaceGetLocalizedDisplayName(interface2
);
1233 interface1Type
= SCNetworkInterfaceGetInterfaceType(interface1
);
1234 interface2Type
= SCNetworkInterfaceGetInterfaceType(interface2
);
1236 interface1Prefix
= _SCNetworkInterfaceGetIOInterfaceNamePrefix(interface1
);
1237 interface2Prefix
= _SCNetworkInterfaceGetIOInterfaceNamePrefix(interface2
);
1239 // Check if have same builtin values.
1240 // Check if User Defined name matches
1241 // Check if SCNetwork Interface Type matches
1243 if (interface1IsBuiltin
!= interface2IsBuiltin
) {
1247 if (!_SC_CFEqual(interface1Type
, interface2Type
)) {
1251 if (!_SC_CFEqual(interface1Prefix
, interface2Prefix
)) {
1255 if (!_SC_CFEqual(interface1UserDefinedName
, interface2UserDefinedName
)) {
1256 // Checking if we have a mismatch because of the name Ethernet and Ethernet 1
1257 // Checking if we have a mismatch because of the name Airport and WiFi
1258 if (interface1IsBuiltin
&&
1259 interface2IsBuiltin
&&
1260 __SCNetworkConfigurationInterfaceNameIsEquiv(interface1UserDefinedName
, interface2UserDefinedName
)) {
1269 __SCNetworkConfigurationInterfaceNameIsEquiv(CFStringRef interfaceName1
, CFStringRef interfaceName2
)
1271 CFStringRef interfaceArray
[] = { CFSTR("iPhone"), CFSTR("iPad"), CFSTR("iPod"), CFSTR("AppleTV") };
1272 const int interfaceCount
= sizeof(interfaceArray
) / sizeof(CFStringRef
);
1273 CFStringRef portSuffix
= CFSTR(", Port 1");
1275 if ((isA_CFString(interfaceName1
)) && (isA_CFString(interfaceName2
))) {
1276 if (!CFEqual(interfaceName1
, interfaceName2
)) {
1277 // Check if we are looking at the WiFi interface
1278 if ((CFEqual(interfaceName1
, CFSTR("AirPort")) ||
1279 (CFEqual(interfaceName1
, CFSTR("Wi-Fi")))) &&
1280 (CFEqual(interfaceName2
, CFSTR("AirPort")) ||
1281 (CFEqual(interfaceName2
, CFSTR("Wi-Fi"))))) {
1285 if (((CFEqual(interfaceName1
, CFSTR("Ethernet"))) ||
1286 (CFEqual(interfaceName1
, CFSTR("Ethernet 1")))) &&
1287 ((CFEqual(interfaceName2
, CFSTR("Ethernet"))) ||
1288 (CFEqual(interfaceName2
, CFSTR("Ethernet 1"))))) {
1292 if ((CFStringHasSuffix(interfaceName1
, portSuffix
) &&
1293 (CFStringCompareWithOptions(interfaceName1
, interfaceName2
, CFRangeMake(0, (CFStringGetLength(interfaceName1
) - CFStringGetLength(portSuffix
))), 0) == kCFCompareEqualTo
)) ||
1294 (CFStringHasSuffix(interfaceName2
, portSuffix
) &&
1295 (CFStringCompareWithOptions(interfaceName2
, interfaceName1
, CFRangeMake(0, (CFStringGetLength(interfaceName2
) - CFStringGetLength(portSuffix
))), 0) == kCFCompareEqualTo
))) {
1299 for (CFIndex idx
= 0; idx
< interfaceCount
; idx
++) {
1300 CFStringRef tempInterfaceName
= interfaceArray
[idx
];
1301 if ((CFEqual(interfaceName1
, tempInterfaceName
) ||
1302 __SCNetworkInterfaceMatchesName(interfaceName1
, tempInterfaceName
)) &&
1303 (CFEqual(interfaceName2
, tempInterfaceName
) ||
1304 __SCNetworkInterfaceMatchesName(interfaceName2
, tempInterfaceName
))) {
1317 CFDictionaryRef interfaceMapping
;
1318 CFMutableArrayRef interfacesMissingServices
;
1319 } missingServiceContext
;
1322 CFDictionaryRef bsdNameToBridgeServices
; // Mapping of BSD Name to SCBridgeInterfaceRef
1323 CFDictionaryRef bsdNameToBondServices
; // Mapping of BSD Name to SCBondInterfaceRef
1324 CFDictionaryRef bsdNameToVLANServices
; // Mapping of BSD Name to SCVLANInterfaceRef
1325 CFDictionaryRef interfaceMapping
;
1327 CFMutableArrayRef interfaceToBeRemoved
; // SCNetworkInterfaceRef. Services containing the interface will be removed
1328 CFMutableArrayRef interfaceToBeReplaced
; // SCNetworkInterfaceRef. Services containing the interface will be replaced with default service
1329 CFMutableArrayRef interfacePreserveServiceInformation
; // SCNetworkInterfaceRef. Services containing the interface will be replaced with new service which has same configuration as the current service with issue.
1330 CFMutableDictionaryRef bsdNameServiceProtocolPreserveMapping
;
1331 SCPreferencesRef prefs
;
1336 _SCNetworkConfigurationValidateInterface(const void *key
, const void *value
, void *context
)
1338 CFStringRef bsdName
= (CFStringRef
)key
;
1339 validityContext
*ctx
= (validityContext
*)context
;
1340 CFDictionaryRef bsdNameToBridgeServices
= ctx
->bsdNameToBridgeServices
;
1341 CFDictionaryRef bsdNameToBondServices
= ctx
->bsdNameToBondServices
;
1342 CFDictionaryRef bsdNameToVLANServices
= ctx
->bsdNameToVLANServices
;
1343 SCNetworkInterfaceRef interface
= NULL
;
1344 CFDictionaryRef interfaceMapping
= ctx
->interfaceMapping
;
1345 CFStringRef interfaceUserDefinedName
= NULL
;
1346 Boolean repair
= ctx
->repair
;
1347 SCNetworkInterfaceRef serviceInterface
= (SCNetworkInterfaceRef
)value
;
1348 CFStringRef serviceInterfaceUserDefinedName
= NULL
;
1349 CFMutableArrayRef interfaceToBeRemoved
= ctx
->interfaceToBeRemoved
;
1350 CFMutableArrayRef interfaceToBeReplaced
= ctx
->interfaceToBeReplaced
;
1351 CFMutableArrayRef interfacePreserveServiceInformation
= ctx
->interfacePreserveServiceInformation
;
1353 // No work needs to be done if we have already made determination that configuration somewhere is not valid,
1354 // or we don't intend to repair invalid configuration.
1355 if ((*ctx
->isValid
== FALSE
) && !repair
) {
1359 // There is no interface present for the service
1360 interface
= CFDictionaryGetValue(interfaceMapping
, bsdName
);
1361 if (interface
== NULL
) {
1362 if (((bsdNameToBridgeServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToBridgeServices
, bsdName
)) &&
1363 ((bsdNameToBondServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToBondServices
, bsdName
)) &&
1364 ((bsdNameToVLANServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToVLANServices
, bsdName
))) {
1365 // Not a virtual interface
1367 "No interface with BSD name \"%@\" present for service",
1371 CFArrayAppendValue(interfaceToBeRemoved
, serviceInterface
);
1373 *ctx
->isValid
= FALSE
;
1378 // Need to compare between both SCNetworkInterfaceRefs
1379 interfaceUserDefinedName
= __SCNetworkInterfaceGetUserDefinedName(interface
);
1380 serviceInterfaceUserDefinedName
= __SCNetworkInterfaceGetUserDefinedName(serviceInterface
);
1382 if (!__SCNetworkConfigurationInterfaceNameIsEquiv(interfaceUserDefinedName
, serviceInterfaceUserDefinedName
)) {
1384 "Interface user defined name \"%@\" doesn't match service/interface user defined name \"%@\"",
1385 interfaceUserDefinedName
,
1386 serviceInterfaceUserDefinedName
);
1387 *ctx
->isValid
= FALSE
;
1388 // Check if the service interface name is set to localized key
1389 if (isA_CFArray(interfacePreserveServiceInformation
) != NULL
&&
1390 __SCNetworkInterfaceMatchesName(interfaceUserDefinedName
, serviceInterfaceUserDefinedName
)) {
1392 "serviceInterfaceUserDefinedName: \"%@\" is the localized key for interface name \"%@\"",
1393 serviceInterfaceUserDefinedName
,
1394 interfaceUserDefinedName
);
1395 CFArrayAppendValue(interfacePreserveServiceInformation
, serviceInterface
);
1397 // Add service interface to the interfaceToBeReplaced list
1398 if (isA_CFArray(interfaceToBeReplaced
) != NULL
) {
1399 CFArrayAppendValue(interfaceToBeReplaced
, interface
);
1401 if (isA_CFArray(interfaceToBeRemoved
) != NULL
) {
1402 CFArrayAppendValue(interfaceToBeRemoved
, serviceInterface
);
1409 _SCNetworkConfigurationCollectMissingService(const void *key
, const void *value
, void *context
)
1411 CFStringRef bsdName
= (CFStringRef
)key
;
1412 missingServiceContext
*ctx
= (missingServiceContext
*)context
;
1413 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)value
;
1414 CFMutableArrayRef interfacesMissingServices
= ctx
->interfacesMissingServices
;
1415 CFDictionaryRef serviceInterfaceMapping
= ctx
->interfaceMapping
;
1417 if (!isA_SCNetworkInterface(interface
) ||
1418 !_SCNetworkInterfaceIsBuiltin(interface
)) {
1422 // Check if services have mapping for the BSD name of the interface
1423 if (!CFDictionaryContainsKey(serviceInterfaceMapping
, bsdName
)) {
1424 CFArrayAppendValue(interfacesMissingServices
, interface
); // Adding interface since the corresponding service seems to be missing
1429 _SCNetworkConfigurationCreateBuiltinInterfaceServices(SCPreferencesRef pref
,
1430 SCPreferencesRef ni_pref
)
1432 missingServiceContext context
;
1433 SCNetworkInterfaceRef interface
= NULL
;
1434 CFArrayRef interfaces
= NULL
;
1435 CFMutableArrayRef interfacesWithoutService
= NULL
;
1436 CFDictionaryRef mappingBSDNameToInterface
= NULL
;
1437 CFDictionaryRef mappingServiceBSDNameToInterface
= NULL
;
1438 CFIndex missingServiceCount
= 0;
1439 Boolean success
= FALSE
;
1441 interfaces
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_pref
);
1442 if (interfaces
== NULL
) {
1443 SC_log(LOG_NOTICE
, "No interfaces");
1447 mappingBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1448 CFRelease(interfaces
);
1449 if (!isA_CFDictionary(mappingBSDNameToInterface
)) {
1453 interfaces
= __SCNetworkServiceCopyAllInterfaces(pref
);
1454 if (interfaces
== NULL
) {
1455 SC_log(LOG_NOTICE
, "No [service] interfaces");
1458 mappingServiceBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1459 CFRelease(interfaces
);
1460 if (!isA_CFDictionary(mappingServiceBSDNameToInterface
)) {
1464 interfacesWithoutService
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1466 context
.interfaceMapping
= mappingServiceBSDNameToInterface
;
1467 context
.interfacesMissingServices
= interfacesWithoutService
;
1469 CFDictionaryApplyFunction(mappingBSDNameToInterface
, _SCNetworkConfigurationCollectMissingService
, &context
);
1470 missingServiceCount
= CFArrayGetCount(interfacesWithoutService
);
1474 for (CFIndex idx
= 0; idx
< missingServiceCount
; idx
++) {
1475 interface
= CFArrayGetValueAtIndex(interfacesWithoutService
, idx
);
1477 if (__SCNetworkInterfaceIsMember(pref
, interface
)) {
1478 // if the interface is a member of a bond or bridge
1482 if (!__SCNetworkServiceCreate(pref
, interface
, NULL
)) {
1483 SC_log(LOG_NOTICE
, "Could not create service for interface: %@", interface
);
1488 if (mappingBSDNameToInterface
!= NULL
) {
1489 CFRelease(mappingBSDNameToInterface
);
1491 if (mappingServiceBSDNameToInterface
!= NULL
) {
1492 CFRelease(mappingServiceBSDNameToInterface
);
1494 if (interfacesWithoutService
!= NULL
) {
1495 CFRelease(interfacesWithoutService
);
1502 add_service(const void *value
, void *context
)
1504 validityContext
*ctx
= (validityContext
*)context
;
1505 SCNetworkSetRef currentSet
= NULL
;
1507 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)value
;
1508 CFDictionaryRef bsdNameServiceProtocolMapping
= ctx
->bsdNameServiceProtocolPreserveMapping
;
1509 SCPreferencesRef prefs
= ctx
->prefs
;
1510 SCNetworkServiceRef service
;
1511 CFStringRef serviceID
;
1512 CFStringRef serviceName
;
1513 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1514 CFArrayRef protocolArray
= NULL
;
1516 if (isA_CFString(bsdName
)) {
1517 protocolArray
= CFDictionaryGetValue(bsdNameServiceProtocolMapping
, bsdName
);
1519 service
= SCNetworkServiceCreate(prefs
, interface
);
1520 if (service
== NULL
) {
1521 SC_log(LOG_NOTICE
, "Could not create new service");
1525 if (!SCNetworkServiceEstablishDefaultConfiguration(service
)) {
1526 SC_log(LOG_NOTICE
, "SCNetworkServiceEstablishDefaultConfiguration() failed");
1527 SCNetworkServiceRemove(service
);
1531 if (protocolArray
!= NULL
) {
1532 CFIndex protocolArrayCount
= CFArrayGetCount(protocolArray
);
1534 for (CFIndex idx
= 0; idx
< protocolArrayCount
; idx
++) {
1535 CFDictionaryRef protocolInfo
= CFArrayGetValueAtIndex(protocolArray
, idx
);
1536 CFDictionaryRef configuration
= CFDictionaryGetValue(protocolInfo
, kProtocolConfiguration
);
1537 CFStringRef protocolType
= CFDictionaryGetValue(protocolInfo
, kProtocolType
);
1538 CFBooleanRef cfEnabled
= CFDictionaryGetValue(protocolInfo
, kProtocolEnabled
);
1540 enabled
= CFBooleanGetValue(cfEnabled
);
1544 __SCNetworkServiceAddProtocolToService(service
, protocolType
, configuration
, enabled
);
1548 // Add service to current set
1549 currentSet
= SCNetworkSetCopyCurrent(prefs
);
1550 if (currentSet
== NULL
) {
1551 SC_log(LOG_NOTICE
, "Could not find current set");
1552 SCNetworkServiceRemove(service
);
1556 if (!SCNetworkSetAddService(currentSet
, service
)) {
1557 SC_log(LOG_NOTICE
, "Could not add service to current set");
1558 SCNetworkServiceRemove(service
);
1562 serviceID
= SCNetworkServiceGetServiceID(service
);
1563 serviceName
= SCNetworkServiceGetName(service
);
1564 if (serviceName
== NULL
) serviceName
= CFSTR("");
1565 SC_log(LOG_INFO
, "Adding service : %@ (%@%s%@)",
1568 bsdName
!= NULL
? ", " : "",
1569 bsdName
!= NULL
? bsdName
: CFSTR(""));
1573 if (service
!= NULL
) {
1576 if (currentSet
!= NULL
) {
1577 CFRelease(currentSet
);
1582 create_bsd_name_service_protocol_mapping(const void *value
, void *context
)
1584 validityContext
*ctx
= (validityContext
*)context
;
1585 CFArrayRef interfacePreserveServiceInformation
= ctx
->interfacePreserveServiceInformation
;
1586 CFMutableDictionaryRef bsdNameServiceProtocolMapping
= ctx
->bsdNameServiceProtocolPreserveMapping
;
1587 SCNetworkInterfaceRef interface
;
1588 SCNetworkServiceRef service
= (SCNetworkServiceRef
)value
;
1590 interface
= SCNetworkServiceGetInterface(service
);
1592 if (CFArrayContainsValue(interfacePreserveServiceInformation
, CFRangeMake(0, CFArrayGetCount(interfacePreserveServiceInformation
)), interface
)) {
1593 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1594 if (isA_CFString(bsdName
)) {
1596 CFArrayRef protocols
= SCNetworkServiceCopyProtocols(service
);
1597 if (protocols
!= NULL
) {
1598 CFMutableArrayRef protocolArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1599 CFIndex protocolCount
= CFArrayGetCount(protocols
);
1601 for (CFIndex idx
= 0; idx
< protocolCount
; idx
++) {
1602 SCNetworkProtocolRef protocol
= CFArrayGetValueAtIndex(protocols
, idx
);
1603 CFDictionaryRef configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1604 CFStringRef protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1605 Boolean enabled
= SCNetworkProtocolGetEnabled(protocol
);
1607 if (configuration
== NULL
|| protocolType
== NULL
) {
1610 CFMutableDictionaryRef protocolInfo
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
1612 CFDictionaryAddValue(protocolInfo
, kProtocolType
, protocolType
);
1613 CFDictionaryAddValue(protocolInfo
, kProtocolConfiguration
, configuration
);
1614 CFDictionaryAddValue(protocolInfo
, kProtocolEnabled
, enabled
? kCFBooleanTrue
: kCFBooleanFalse
);
1615 CFArrayAppendValue(protocolArray
, protocolInfo
);
1616 CFRelease(protocolInfo
);
1618 CFDictionaryAddValue(bsdNameServiceProtocolMapping
, bsdName
, protocolArray
);
1619 CFRelease(protocols
);
1620 CFRelease(protocolArray
);
1628 remove_service(const void *value
, void *context
)
1630 validityContext
*ctx
= (validityContext
*)context
;
1631 SCNetworkInterfaceRef interface
;
1632 SCNetworkServiceRef service
= (SCNetworkServiceRef
)value
;
1633 CFArrayRef toBeRemoved
= ctx
->interfaceToBeRemoved
;
1635 interface
= SCNetworkServiceGetInterface(service
);
1636 if (CFArrayContainsValue(toBeRemoved
, CFRangeMake(0, CFArrayGetCount(toBeRemoved
)), interface
)) {
1637 CFStringRef bsdName
;
1638 CFStringRef serviceID
;
1639 CFStringRef serviceName
;
1641 serviceID
= SCNetworkServiceGetServiceID(service
);
1642 serviceName
= SCNetworkServiceGetName(service
);
1643 if (serviceName
== NULL
) serviceName
= CFSTR("");
1644 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1645 SC_log(LOG_INFO
, "Removing service : %@ (%@%s%@)",
1648 bsdName
!= NULL
? ", " : "",
1649 bsdName
!= NULL
? bsdName
: CFSTR(""));
1651 SCNetworkServiceRemove(service
);
1656 _SCNetworkConfigurationRepairUsingPreferences(SCPreferencesRef prefs
,
1657 SCPreferencesRef ni_prefs
,
1658 validityContext
*context
)
1660 CFIndex removeCount
;
1661 CFIndex replaceCount
;
1662 CFArrayRef serviceList
;
1663 CFArrayRef interfaceToBeRemoved
= context
->interfaceToBeRemoved
;
1664 CFArrayRef interfaceToBeReplaced
= context
->interfaceToBeReplaced
;
1666 removeCount
= CFArrayGetCount(interfaceToBeRemoved
);
1667 replaceCount
= CFArrayGetCount(interfaceToBeReplaced
);
1668 if ((removeCount
== 0) && (replaceCount
== 0)) {
1669 // We don't have any information to repair
1673 // Backup current preferences before making changes
1674 __SCNetworkConfigurationBackup(prefs
, CFSTR("pre-repair"), prefs
);
1675 __SCNetworkConfigurationBackup(ni_prefs
, CFSTR("pre-repair"), prefs
);
1677 __SCNetworkConfigurationReport(LOG_DEBUG
, "pre-repair", prefs
, ni_prefs
);
1678 if (interfaceToBeRemoved
!= NULL
) {
1679 logInterfaces(LOG_DEBUG
, "Interfaces to be removed", interfaceToBeRemoved
, FALSE
);
1681 if (interfaceToBeReplaced
!= NULL
) {
1682 logInterfaces(LOG_DEBUG
, "Interfaces to be replaced", interfaceToBeReplaced
, FALSE
);
1685 serviceList
= SCNetworkServiceCopyAll(prefs
);
1686 CFArrayApplyFunction(serviceList
, CFRangeMake(0, CFArrayGetCount(serviceList
)), create_bsd_name_service_protocol_mapping
, context
);
1687 if (context
->bsdNameServiceProtocolPreserveMapping
!= NULL
) {
1688 logMapping(LOG_DEBUG
, context
->bsdNameServiceProtocolPreserveMapping
, "BSD name / Service Protocol mapping");
1691 CFArrayApplyFunction(serviceList
, CFRangeMake(0, CFArrayGetCount(serviceList
)), remove_service
, (void*)context
);
1692 CFArrayApplyFunction(interfaceToBeReplaced
, CFRangeMake(0, replaceCount
), add_service
, (void*)context
);
1693 CFRelease(serviceList
);
1695 __SCNetworkConfigurationReport(LOG_DEBUG
, "post-repair", prefs
, ni_prefs
);
1701 validate_bridge(const void *value
, void *context
)
1703 SCBridgeInterfaceRef bridge
= (SCBridgeInterfaceRef
)value
;
1704 CFArrayRef memberInterfaces
= SCBridgeInterfaceGetMemberInterfaces(bridge
);
1705 CFMutableArrayRef memberInterfacesMutable
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1706 SCPreferencesRef ni_prefs
= (SCPreferencesRef
)context
;
1708 for (CFIndex idx
= 0; idx
< CFArrayGetCount(memberInterfaces
); idx
++) {
1709 CFStringRef bsdName
;
1710 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)CFArrayGetValueAtIndex(memberInterfaces
, idx
);
1711 SCNetworkInterfaceRef memberInterface
;
1713 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1714 if (bsdName
== NULL
) {
1718 // Check if member interface is present
1719 memberInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, bsdName
);
1720 if (memberInterface
!= NULL
) {
1721 CFArrayAppendValue(memberInterfacesMutable
, memberInterface
);
1722 CFRelease(memberInterface
);
1726 if (CFArrayGetCount(memberInterfacesMutable
) == 0) {
1727 SC_log(LOG_NOTICE
, "Removing bridge w/no member interfaces: %@", bridge
);
1728 SCBridgeInterfaceRemove(bridge
);
1730 SCBridgeInterfaceSetMemberInterfaces(bridge
, memberInterfacesMutable
);
1732 CFRelease(memberInterfacesMutable
);
1735 #if !TARGET_OS_IPHONE
1737 validate_bond(const void *value
, void *context
)
1739 SCBondInterfaceRef bond
= (SCBondInterfaceRef
)value
;
1740 CFArrayRef memberInterfaces
= SCBondInterfaceGetMemberInterfaces(bond
);
1741 CFMutableArrayRef memberInterfacesMutable
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1742 SCPreferencesRef ni_prefs
= (SCPreferencesRef
)context
;
1744 for (CFIndex idx
= 0; idx
< CFArrayGetCount(memberInterfaces
); idx
++) {
1745 CFStringRef bsdName
;
1746 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)CFArrayGetValueAtIndex(memberInterfaces
, idx
);
1747 SCNetworkInterfaceRef memberInterface
;
1749 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1750 if (bsdName
== NULL
) {
1754 // Check if member interface is present
1755 memberInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, bsdName
);
1756 if (memberInterface
!= NULL
) {
1757 CFArrayAppendValue(memberInterfacesMutable
, memberInterface
);
1758 CFRelease(memberInterface
);
1762 if (CFArrayGetCount(memberInterfacesMutable
) == 0) {
1763 SC_log(LOG_NOTICE
, "Removing bond w/no member interfaces: %@", bond
);
1764 SCBondInterfaceRemove(bond
);
1766 SCBondInterfaceSetMemberInterfaces(bond
, memberInterfacesMutable
);
1768 CFRelease(memberInterfacesMutable
);
1770 #endif // !TARGET_OS_IPHONE
1773 SCPreferencesRef prefs
;
1774 SCPreferencesRef ni_prefs
;
1775 #if !TARGET_OS_IPHONE
1776 CFArrayRef bondInterfaces
;
1777 #endif // !TARGET_OS_IPHONE
1778 CFArrayRef bridgeInterfaces
;
1779 } validate_prefs_context
;
1781 static validate_prefs_context
*
1782 validate_prefs_context_create(SCPreferencesRef prefs
, SCPreferencesRef ni_prefs
)
1784 validate_prefs_context
*context
;
1786 context
= calloc(1, sizeof(validate_prefs_context
));
1787 context
->prefs
= prefs
;
1788 context
->ni_prefs
= ni_prefs
;
1789 #if !TARGET_OS_IPHONE
1790 context
->bondInterfaces
= (CFArrayRef
)kCFNull
;
1791 #endif // !TARGET_OS_IPHONE
1792 context
->bridgeInterfaces
= (CFArrayRef
)kCFNull
;
1797 validate_prefs_context_release(validate_prefs_context
*context
)
1799 #if !TARGET_OS_IPHONE
1800 if ((context
->bondInterfaces
!= NULL
) && (context
->bondInterfaces
!= (CFArrayRef
)kCFNull
)) {
1801 CFRelease(context
->bondInterfaces
);
1803 #endif // !TARGET_OS_IPHONE
1804 if ((context
->bridgeInterfaces
!= NULL
) && (context
->bridgeInterfaces
!= (CFArrayRef
)kCFNull
)) {
1805 CFRelease(context
->bridgeInterfaces
);
1811 interfaceArrayContainsBSDName(CFArrayRef interfaces
, CFStringRef bsdName
)
1813 Boolean match
= FALSE
;
1814 CFIndex n
= CFArrayGetCount(interfaces
);
1816 for (CFIndex i
= 0; i
< n
; i
++) {
1817 SCNetworkInterfaceRef interface
;
1818 CFStringRef matchName
;
1820 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
1821 matchName
= SCNetworkInterfaceGetBSDName(interface
);
1822 if (_SC_CFEqual(bsdName
, matchName
)) {
1832 validate_vlan(const void *value
, void *context
)
1834 CFStringRef bsdName
;
1835 SCNetworkInterfaceRef interface
;
1836 Boolean isValid
= FALSE
;
1837 SCNetworkInterfaceRef physicalInterface
;
1838 validate_prefs_context
*prefs
= (validate_prefs_context
*)context
;
1839 SCVLANInterfaceRef vlan
= (SCVLANInterfaceRef
)value
;
1841 physicalInterface
= SCVLANInterfaceGetPhysicalInterface(vlan
);
1842 bsdName
= SCNetworkInterfaceGetBSDName(physicalInterface
);
1843 if (bsdName
== NULL
) {
1847 // Check if the physical interface is present
1848 interface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, prefs
->ni_prefs
, bsdName
);
1849 if (interface
!= NULL
) {
1850 CFRelease(interface
);
1855 #if !TARGET_OS_IPHONE
1856 if (prefs
->bondInterfaces
== (CFArrayRef
)kCFNull
) {
1857 prefs
->bondInterfaces
= SCBondInterfaceCopyAll(prefs
->prefs
);
1859 if (prefs
->bondInterfaces
!= NULL
) {
1860 isValid
= interfaceArrayContainsBSDName(prefs
->bondInterfaces
, bsdName
);
1865 #endif // !TARGET_OS_IPHONE
1867 if (prefs
->bridgeInterfaces
== (CFArrayRef
)kCFNull
) {
1868 prefs
->bridgeInterfaces
= SCBridgeInterfaceCopyAll(prefs
->prefs
);
1870 if (prefs
->bridgeInterfaces
!= NULL
) {
1871 isValid
= interfaceArrayContainsBSDName(prefs
->bridgeInterfaces
, bsdName
);
1879 SC_log(LOG_NOTICE
, "Removing VLAN w/no physical interface: %@", vlan
);
1880 SCVLANInterfaceRemove(vlan
);
1885 _SCNetworkConfigurationCheckValidityWithPreferences(SCPreferencesRef prefs
,
1886 SCPreferencesRef ni_prefs
,
1887 CFDictionaryRef options
)
1889 CFArrayRef allServices
= NULL
;
1890 CFArrayRef allSets
= NULL
;
1891 CFDictionaryRef bsdNameToBridgeServices
= NULL
;
1892 CFDictionaryRef bsdNameToBondServices
= NULL
;
1893 CFDictionaryRef bsdNameToVLANServices
= NULL
;
1894 validityContext context
;
1895 CFArrayRef interfaces
= NULL
;
1896 CFMutableArrayRef interfaceToBeRemoved
= NULL
;
1897 CFMutableArrayRef interfaceToBeReplaced
= NULL
;
1898 CFMutableArrayRef interfacePreserveServiceInformation
= NULL
;
1899 CFMutableDictionaryRef bsdNameServiceProtocolPreserveMapping
= NULL
;
1900 Boolean isValid
= TRUE
;
1901 CFDictionaryRef mappingBSDNameToInterface
= NULL
;
1902 CFDictionaryRef mappingServiceBSDNameToInterface
= NULL
;
1903 CFStringRef model
= NULL
;
1904 CFStringRef ni_model
= NULL
;
1905 Boolean repairConfiguration
= FALSE
;
1906 Boolean repairModel
= FALSE
;
1907 Boolean revertBypassSystemInterfaces
= FALSE
;
1908 CFArrayRef setServiceOrder
= NULL
;
1909 CFArrayRef setServices
= NULL
;
1911 if ((isA_CFDictionary(options
) != NULL
)) {
1914 val
= CFDictionaryGetValue(options
, kSCNetworkConfigurationRepair
);
1915 if (isA_CFBoolean(val
) != NULL
) {
1916 repairConfiguration
= CFBooleanGetValue(val
);
1919 val
= CFDictionaryGetValue(options
, kSCNetworkConfigurationRepairModel
);
1920 if (isA_CFBoolean(val
) != NULL
) {
1921 repairModel
= CFBooleanGetValue(val
);
1926 "Configuration validity check%s%s%s"
1929 repairModel
? ", w/repair-model" : "",
1930 repairConfiguration
? ", w/repair-configuration" : "",
1931 _SCNetworkConfigurationBypassSystemInterfaces(prefs
) ? ", bypass system interfaces" : "",
1935 if (!_SCNetworkConfigurationBypassSystemInterfaces(prefs
)) {
1936 _SCNetworkConfigurationSetBypassSystemInterfaces(prefs
, TRUE
);
1937 revertBypassSystemInterfaces
= TRUE
;
1941 Check the validity by:
1942 - Comparing if the models are the same
1944 model
= SCPreferencesGetValue(prefs
, MODEL
);
1945 ni_model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
1946 if (!isA_CFString(model
)) {
1947 if (repairModel
&& isA_CFString(ni_model
)) {
1948 SC_log(LOG_INFO
, " updating \"Model\" property in preferences.plist");
1949 SCPreferencesSetValue(prefs
, MODEL
, ni_model
); // have prefs model match ni_prefs
1952 SC_log(LOG_INFO
, " no \"Model\" property in preferences.plist");
1955 if (!isA_CFString(ni_model
)) {
1956 if (repairModel
&& isA_CFString(model
)) {
1957 SC_log(LOG_INFO
, " updating \"Model\" property in NetworkInterfaces.plist");
1958 SCPreferencesSetValue(ni_prefs
, MODEL
, model
); // have ni_prefs model match prefs
1961 SC_log(LOG_INFO
, " no \"Model\" property in NetworkInterfaces.plist");
1964 if (isA_CFString(model
) && isA_CFString(ni_model
) && !CFEqual(model
, ni_model
)) {
1967 "Configuration validity check: model names do not match!"
1976 - Comparing if the interfaces names mentioned in NetworkInterfaces.plist and preferences.plist match
1979 __SCNetworkInterfaceCreateMappingUsingBSDName(SCPreferencesRef prefs);
1981 interfaces
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
1982 if (interfaces
== NULL
) {
1984 "Configuration validity check: no network interfaces!"
1990 mappingBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1991 CFRelease(interfaces
);
1992 if (!isA_CFDictionary(mappingBSDNameToInterface
)) {
1995 "Configuration validity check: no BSD name to network interface mapping!"
2001 interfaces
= __SCNetworkServiceCopyAllInterfaces(prefs
);
2002 if (interfaces
== NULL
) {
2005 "Configuration validity check: no service interfaces!"
2010 logInterfaces(LOG_DEBUG
, "interfaces", interfaces
, TRUE
);
2013 mappingServiceBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
2014 CFRelease(interfaces
);
2015 if (mappingServiceBSDNameToInterface
!= NULL
) {
2016 logMapping(LOG_DEBUG
, mappingServiceBSDNameToInterface
, "mappingServiceBSDNameToInterface");
2020 "Configuration validity check: no BSD name to service interface mapping!"
2026 if (repairConfiguration
) {
2027 interfaceToBeRemoved
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2028 interfaceToBeReplaced
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2029 interfacePreserveServiceInformation
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2030 bsdNameServiceProtocolPreserveMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2031 #if !TARGET_OS_IPHONE
2032 bsdNameToBridgeServices
= _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(prefs
);
2033 bsdNameToBondServices
= _SCNetworkMigrationCopyMappingBSDNameToBondServices(prefs
);
2034 bsdNameToVLANServices
= _SCNetworkMigrationCopyMappingBSDNameToVLANServices(prefs
);
2035 #endif // !TARGET_OS_IPHONE
2038 context
.interfaceMapping
= mappingBSDNameToInterface
;
2039 context
.isValid
= &isValid
;
2040 context
.interfaceToBeRemoved
= interfaceToBeRemoved
;
2041 context
.interfaceToBeReplaced
= interfaceToBeReplaced
;
2042 context
.interfacePreserveServiceInformation
= interfacePreserveServiceInformation
;
2043 context
.bsdNameToBridgeServices
= bsdNameToBridgeServices
;
2044 context
.bsdNameToBondServices
= bsdNameToBondServices
;
2045 context
.bsdNameToVLANServices
= bsdNameToVLANServices
;
2046 context
.repair
= repairConfiguration
;
2047 context
.prefs
= prefs
;
2048 context
.bsdNameServiceProtocolPreserveMapping
= bsdNameServiceProtocolPreserveMapping
;
2049 CFDictionaryApplyFunction(mappingServiceBSDNameToInterface
, _SCNetworkConfigurationValidateInterface
, &context
);
2053 "Configuration validity check: mismatched interface names between NetworkInterfaces.plist and preferences.plist!"
2058 if (repairConfiguration
) {
2059 isValid
= _SCNetworkConfigurationRepairUsingPreferences(prefs
, ni_prefs
, &context
);
2063 // Save the changes if repair fixed an invalid configuration
2064 if (!SCPreferencesCommitChanges(prefs
)) {
2065 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges() failed");
2073 - Check if all the network services mentioned in the SCNetworkSet are actually present in the SCNetworkService array
2075 allServices
= SCNetworkServiceCopyAll(prefs
);
2076 if (!isA_CFArray(allServices
)) {
2079 "Configuration validity check: no services!"
2085 allSets
= SCNetworkSetCopyAll(prefs
);
2086 if (!isA_CFArray(allSets
)) {
2089 "Configuration validity check: no sets!"
2095 for (CFIndex idx
= 0; ((idx
< CFArrayGetCount(allSets
)) && isValid
); idx
++) {
2096 SCNetworkSetRef set
= CFArrayGetValueAtIndex(allSets
, idx
);
2098 setServices
= SCNetworkSetCopyServices(set
);
2099 if (setServices
== NULL
) {
2101 "Configuration validity check: set w/no services!"
2108 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(setServices
); idx2
++) {
2109 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(setServices
, idx2
);
2111 if (!CFArrayContainsValue(allServices
, CFRangeMake(0, CFArrayGetCount(allServices
)), service
)) {
2114 "All network services in the network set are not present in SCNetworkService array");
2123 - Check if service IDs in service order do exist in the SET
2125 setServiceOrder
= SCNetworkSetGetServiceOrder(set
);
2126 if (setServiceOrder
!= NULL
) {
2127 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(setServiceOrder
); idx2
++) {
2128 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(setServiceOrder
, idx2
);
2129 if (!CFArrayContainsValue(setServiceOrder
, CFRangeMake(0, CFArrayGetCount(setServiceOrder
)), service
) &&
2130 !CFArrayContainsValue(allServices
, CFRangeMake(0, CFArrayGetCount(allServices
)), service
)) {
2132 "Service: %@ is not present in the service order for set %@",
2139 if (setServices
!= NULL
) {
2140 CFRelease(setServices
);
2146 - Check if the virtual network interfaces have valid member interfaces
2148 CFArrayRef bridges
= SCBridgeInterfaceCopyAll(prefs
);
2149 if (bridges
!= NULL
) {
2150 CFArrayApplyFunction(bridges
, CFRangeMake(0, CFArrayGetCount(bridges
)), validate_bridge
, (void*)ni_prefs
);
2153 #if !TARGET_OS_IPHONE
2154 CFArrayRef bonds
= SCBondInterfaceCopyAll(prefs
);
2155 if (bonds
!= NULL
) {
2156 CFArrayApplyFunction(bonds
, CFRangeMake(0, CFArrayGetCount(bonds
)), validate_bond
, (void*)ni_prefs
);
2159 #endif // !TARGET_OS_IPHONE
2160 CFArrayRef vlans
= SCVLANInterfaceCopyAll(prefs
);
2161 if (vlans
!= NULL
) {
2162 validate_prefs_context
*validate_prefs
;
2164 validate_prefs
= validate_prefs_context_create(prefs
, ni_prefs
);
2165 CFArrayApplyFunction(vlans
, CFRangeMake(0, CFArrayGetCount(vlans
)), validate_vlan
, (void*)validate_prefs
);
2166 validate_prefs_context_release(validate_prefs
);
2172 if (mappingBSDNameToInterface
!= NULL
) {
2173 CFRelease(mappingBSDNameToInterface
);
2175 if (mappingServiceBSDNameToInterface
!= NULL
) {
2176 CFRelease(mappingServiceBSDNameToInterface
);
2178 if (allServices
!= NULL
) {
2179 CFRelease(allServices
);
2181 if (allSets
!= NULL
) {
2184 #if !TARGET_OS_IPHONE
2185 if (bsdNameToBridgeServices
!= NULL
) {
2186 CFRelease(bsdNameToBridgeServices
);
2188 if (bsdNameToBondServices
!= NULL
) {
2189 CFRelease(bsdNameToBondServices
);
2191 if (bsdNameToVLANServices
!= NULL
) {
2192 CFRelease(bsdNameToVLANServices
);
2194 #endif // !TARGET_OS_IPHONE
2195 if (setServices
!= NULL
) {
2196 CFRelease(setServices
);
2198 if (interfaceToBeRemoved
!= NULL
) {
2199 CFRelease(interfaceToBeRemoved
);
2201 if (interfaceToBeReplaced
!= NULL
) {
2202 CFRelease(interfaceToBeReplaced
);
2204 if (interfacePreserveServiceInformation
!= NULL
) {
2205 CFRelease(interfacePreserveServiceInformation
);
2207 if (bsdNameServiceProtocolPreserveMapping
!= NULL
) {
2208 CFRelease(bsdNameServiceProtocolPreserveMapping
);
2210 if (revertBypassSystemInterfaces
) {
2211 _SCNetworkConfigurationSetBypassSystemInterfaces(prefs
, FALSE
);
2214 SC_log(LOG_INFO
, " configuration is %svalid", isValid
? "" : "not ");
2219 _SCNetworkConfigurationCheckValidity(CFURLRef configDir
, CFDictionaryRef options
)
2221 CFURLRef baseURL
= NULL
;
2222 CFURLRef configNetworkInterfaceFile
= NULL
;
2223 CFStringRef configNetworkInterfaceFileString
= NULL
;
2224 SCPreferencesRef configNetworkInterfacePref
= NULL
;
2225 SCPreferencesRef configPref
= NULL
;
2226 CFURLRef configPreferenceFile
= NULL
;
2227 CFStringRef configPreferencesFileString
= NULL
;
2228 Boolean isValid
= FALSE
;
2229 char networkInterfaceStr
[PATH_MAX
];
2230 char prefsStr
[PATH_MAX
];
2232 if (configDir
== NULL
) {
2233 SC_log(LOG_NOTICE
, "Migration files not found in directory: %@",
2234 (configDir
== NULL
) ? CFSTR("NULL") : CFURLGetString(configDir
));
2237 baseURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
2238 PREFS_DEFAULT_DIR_RELATIVE
,
2239 kCFURLPOSIXPathStyle
,
2243 configPreferenceFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
2244 (const UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
,
2245 sizeof(PREFS_DEFAULT_CONFIG_PLIST
),
2248 configNetworkInterfaceFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
2249 (const UInt8
*)INTERFACES_DEFAULT_CONFIG_PLIST
,
2250 sizeof(INTERFACES_DEFAULT_CONFIG_PLIST
),
2254 if (!CFURLGetFileSystemRepresentation(configPreferenceFile
, TRUE
, (UInt8
*)prefsStr
, sizeof(prefsStr
))) {
2255 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPreferenceFile
);
2258 if (!CFURLGetFileSystemRepresentation(configNetworkInterfaceFile
, TRUE
, (UInt8
*)networkInterfaceStr
, sizeof(networkInterfaceStr
))) {
2259 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfaceFile
);
2263 configPreferencesFileString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsStr
);
2264 configNetworkInterfaceFileString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceStr
);
2266 configPref
= SCPreferencesCreate(NULL
,
2268 configPreferencesFileString
);
2270 configNetworkInterfacePref
= SCPreferencesCreate(NULL
,
2272 configNetworkInterfaceFileString
);
2273 if ((configPref
== NULL
) || (configNetworkInterfacePref
== NULL
)) {
2277 // This function compares preferences.plist and NetworkInterfaces.plist and verifies if the values are correct
2278 // Checking interface mismatch for validity
2279 isValid
= _SCNetworkConfigurationCheckValidityWithPreferences(configPref
, configNetworkInterfacePref
, options
);
2282 if (baseURL
!= NULL
) {
2285 if (configPreferencesFileString
!= NULL
) {
2286 CFRelease(configPreferencesFileString
);
2288 if (configNetworkInterfaceFileString
!= NULL
) {
2289 CFRelease(configNetworkInterfaceFileString
);
2291 if (configPref
!= NULL
) {
2292 CFRelease(configPref
);
2294 if (configNetworkInterfacePref
!= NULL
) {
2295 CFRelease(configNetworkInterfacePref
);
2297 if (configPreferenceFile
!= NULL
) {
2298 CFRelease(configPreferenceFile
);
2300 if (configNetworkInterfaceFile
!= NULL
) {
2301 CFRelease(configNetworkInterfaceFile
);
2308 CFMutableArrayRef externalInterfaceList
;
2309 CFMutableArrayRef networkInterfaceList
;
2310 Boolean foundNewInterfaces
;
2311 } externalMappingContext
;
2314 _SCNetworkConfigurationCollectInterfaceStorageEntity(const void *key
, const void *value
, void *context
)
2317 externalMappingContext
*ctx
= context
;
2318 CFDictionaryRef interface_entity
;
2320 SCNetworkInterfaceRef targetInterface
= (SCNetworkInterfaceRef
)value
;
2322 matchIndex
= CFArrayGetFirstIndexOfValue(ctx
->externalInterfaceList
,
2323 CFRangeMake(0, CFArrayGetCount(ctx
->externalInterfaceList
)),
2325 if (matchIndex
!= kCFNotFound
) {
2326 SCNetworkInterfaceRef matchInterface
;
2327 CFStringRef matchName
;
2328 CFStringRef targetName
;
2330 matchInterface
= CFArrayGetValueAtIndex(ctx
->externalInterfaceList
, matchIndex
);
2331 matchName
= __SCNetworkInterfaceGetUserDefinedName(matchInterface
);
2332 targetName
= __SCNetworkInterfaceGetUserDefinedName(targetInterface
);
2333 if (_SC_CFEqual(matchName
, targetName
)) {
2334 // the target interface already exists; do not add it to NetworkInterfaces.plist
2336 "Target interface already exists, not updating NetworkInterfaces.plist"
2341 // the target interface differs from the NetworkInterfaces.plist interface; replace it
2343 "Current interface morphed, replacing"
2346 CFArrayRemoveValueAtIndex(ctx
->externalInterfaceList
, matchIndex
);
2350 ctx
->foundNewInterfaces
= TRUE
;
2351 interface_entity
= __SCNetworkInterfaceCopyStorageEntity(targetInterface
);
2352 if (interface_entity
!= NULL
) {
2354 "adding network interface entity"
2357 CFArrayAppendValue(ctx
->networkInterfaceList
, interface_entity
);
2358 CFRelease(interface_entity
);
2362 static CFArrayRef
// CFDictionaryRef
2363 _SCNetworkMigrationCreateNetworkInterfaceArray(SCPreferencesRef ni_prefs
, CFDictionaryRef externalMapping
, Boolean
*hasNewInterface
)
2365 externalMappingContext context
;
2367 CFMutableArrayRef externalInterfaceList
= NULL
;
2368 CFArrayRef if_list
= NULL
;
2369 CFDictionaryRef interface_entity
= NULL
;
2370 CFMutableArrayRef networkInterfaceList
= NULL
;
2372 if (ni_prefs
== NULL
) {
2373 SC_log(LOG_NOTICE
, "No NetworkInterfaces.plist");
2377 if_list
= SCPreferencesGetValue(ni_prefs
, INTERFACES
);
2378 if (!isA_CFArray(if_list
) ||
2379 ((count
= CFArrayGetCount(if_list
)) == 0)) {
2380 SC_log(LOG_NOTICE
, "No interfaces");
2384 networkInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2386 // Keep the same builtin and external interfaces
2387 for (CFIndex idx
= 0; idx
< count
; idx
++) {
2388 interface_entity
= CFArrayGetValueAtIndex(if_list
, idx
);
2389 if (!isA_CFDictionary(interface_entity
)) {
2392 CFArrayAppendValue(networkInterfaceList
, interface_entity
);
2395 if (!isA_CFDictionary(externalMapping
)) {
2396 // if no external mapping
2400 // Add any new external interfaces found
2401 externalInterfaceList
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(ni_prefs
, FALSE
);
2402 context
.externalInterfaceList
= externalInterfaceList
;
2403 context
.networkInterfaceList
= networkInterfaceList
;
2404 context
.foundNewInterfaces
= FALSE
;
2406 SC_log(LOG_DEBUG
, "Updating network interface list");
2407 logInterfaces(LOG_DEBUG
, " externalInterfaceList", externalInterfaceList
, FALSE
);
2408 logInterfaces(LOG_DEBUG
, " networkInterfaceList" , networkInterfaceList
, FALSE
);
2410 CFDictionaryApplyFunction(externalMapping
, _SCNetworkConfigurationCollectInterfaceStorageEntity
, &context
);
2412 if (hasNewInterface
!= NULL
) {
2413 *hasNewInterface
= context
.foundNewInterfaces
;
2416 if (externalInterfaceList
!= NULL
) {
2417 CFRelease(externalInterfaceList
);
2419 return networkInterfaceList
;
2423 SCNetworkMigrationMapSourceToTargetName(const void *key
, const void *value
, void *context
)
2425 SCNetworkInterfaceRef interfaceKey
= (SCNetworkInterfaceRef
)key
;
2426 SCNetworkInterfaceRef interfaceValue
= (SCNetworkInterfaceRef
)value
;
2427 CFMutableDictionaryRef mapping
= (CFMutableDictionaryRef
)context
;
2428 CFStringRef sourceBSDName
= NULL
;
2429 CFStringRef targetBSDName
= NULL
;
2431 sourceBSDName
= SCNetworkInterfaceGetBSDName(interfaceKey
);
2432 if (!isA_CFString(sourceBSDName
)) {
2436 targetBSDName
= SCNetworkInterfaceGetBSDName(interfaceValue
);
2437 if (!isA_CFString(targetBSDName
)) {
2441 if (!CFDictionaryContainsKey(mapping
, sourceBSDName
)) {
2442 CFDictionaryAddValue(mapping
, sourceBSDName
, targetBSDName
);
2447 static CFDictionaryRef
2448 _SCNetworkMigrationCreateBSDNameMapping(CFDictionaryRef internalMapping
, CFDictionaryRef externalMapping
)
2450 CFMutableDictionaryRef bsdNameMapping
= CFDictionaryCreateMutable(NULL
, 0,
2451 &kCFTypeDictionaryKeyCallBacks
,
2452 &kCFTypeDictionaryValueCallBacks
);
2454 if ((internalMapping
== NULL
) && (externalMapping
== NULL
)) {
2458 if (internalMapping
!= NULL
) {
2459 CFDictionaryApplyFunction(internalMapping
, SCNetworkMigrationMapSourceToTargetName
, bsdNameMapping
);
2462 if (externalMapping
!= NULL
) {
2463 CFDictionaryApplyFunction(externalMapping
, SCNetworkMigrationMapSourceToTargetName
, bsdNameMapping
);
2467 return bsdNameMapping
;
2470 static CFDictionaryRef
2471 _SCNetworkMigrationCreateServiceSetMapping(SCPreferencesRef prefs
)
2473 SCNetworkServiceRef service
;
2474 CFMutableDictionaryRef serviceSetMapping
;
2475 CFArrayRef services
= NULL
;
2476 CFMutableArrayRef setList
= NULL
;
2477 CFArrayRef sets
= NULL
;
2479 serviceSetMapping
= CFDictionaryCreateMutable(NULL
, 0,
2480 &kCFTypeDictionaryKeyCallBacks
,
2481 &kCFTypeDictionaryValueCallBacks
);
2482 services
= SCNetworkServiceCopyAll(prefs
);
2483 if (services
== NULL
) {
2486 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2487 service
= CFArrayGetValueAtIndex(services
, idx
);
2488 setList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2489 CFDictionaryAddValue(serviceSetMapping
, service
, setList
);
2492 CFRelease(services
);
2494 sets
= SCNetworkSetCopyAll(prefs
);
2499 for (CFIndex idx
= 0; idx
< CFArrayGetCount(sets
); idx
++) {
2500 SCNetworkSetRef set
;
2502 set
= CFArrayGetValueAtIndex(sets
, idx
);
2503 services
= SCNetworkSetCopyServices(set
);
2504 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(services
); idx2
++) {
2505 service
= CFArrayGetValueAtIndex(services
, idx2
);
2506 setList
= (CFMutableArrayRef
)CFDictionaryGetValue(serviceSetMapping
, service
);
2507 if (setList
!= NULL
) {
2508 CFArrayAppendValue(setList
, set
);
2511 CFRelease(services
);
2518 return serviceSetMapping
;
2521 static CFDictionaryRef
2522 _SCNetworkMigrationCreateSetMapping(SCPreferencesRef sourcePrefs
,
2523 SCPreferencesRef targetPrefs
)
2525 SCNetworkSetRef currentSourceSet
= NULL
;
2526 CFMutableDictionaryRef setMapping
= NULL
;
2527 CFStringRef setName
;
2528 CFArrayRef sourceSets
= NULL
;
2529 CFIndex targetCount
;
2530 SCNetworkSetRef targetSet
;
2531 CFArrayRef targetSets
= NULL
;
2532 CFMutableArrayRef targetSetsMutable
= NULL
;
2534 sourceSets
= SCNetworkSetCopyAll(sourcePrefs
);
2535 if (sourceSets
== NULL
) {
2539 targetSets
= SCNetworkSetCopyAll(targetPrefs
);
2540 if (targetSets
== NULL
) {
2543 targetSetsMutable
= CFArrayCreateMutableCopy(NULL
, 0, targetSets
);
2544 targetCount
= CFArrayGetCount(targetSetsMutable
);
2546 setMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2548 currentSourceSet
= SCNetworkSetCopyCurrent(sourcePrefs
);
2550 // Mapping the current source set to the first target set, and setting it as current set
2551 if (currentSourceSet
!= NULL
) {
2552 if (targetCount
> 0) {
2553 targetSet
= CFArrayGetValueAtIndex(targetSetsMutable
, 0);
2554 CFRetain(targetSet
);
2555 CFArrayRemoveValueAtIndex(targetSetsMutable
, 0);
2557 setName
= SCNetworkSetGetName(currentSourceSet
);
2558 SCNetworkSetSetName(targetSet
, setName
);
2559 CFDictionaryAddValue(setMapping
, currentSourceSet
, targetSet
);
2560 SCNetworkSetSetCurrent(targetSet
);
2561 CFRelease(targetSet
);
2564 SC_log(LOG_NOTICE
, "Number of sets in the target should be at least 1, but is found to be %ld", targetCount
);
2569 for (CFIndex idx
= 0; idx
< CFArrayGetCount(sourceSets
); idx
++) {
2570 SCNetworkSetRef sourceSet
= CFArrayGetValueAtIndex(sourceSets
, idx
);
2572 if ((currentSourceSet
!= NULL
) && CFEqual(sourceSet
, currentSourceSet
)) {
2576 targetCount
= CFArrayGetCount(targetSetsMutable
);
2577 setName
= SCNetworkSetGetName(sourceSet
);
2579 if (targetCount
> 0) {
2580 targetSet
= CFArrayGetValueAtIndex(targetSetsMutable
, 0);
2581 CFRetain(targetSet
);
2582 CFArrayRemoveValueAtIndex(targetSetsMutable
, 0);
2584 targetSet
= SCNetworkSetCreate(targetPrefs
);
2586 SCNetworkSetSetName(targetSet
, setName
);
2587 CFDictionaryAddValue(setMapping
, sourceSet
, targetSet
);
2589 CFRelease(targetSet
);
2594 if (sourceSets
!= NULL
) {
2595 CFRelease(sourceSets
);
2597 if (targetSets
!= NULL
) {
2598 CFRelease(targetSets
);
2600 if (targetSetsMutable
!= NULL
) {
2601 CFRelease(targetSetsMutable
);
2603 if (currentSourceSet
!= NULL
) {
2604 CFRelease(currentSourceSet
);
2610 // This function finds the mapping between source and target preferences (SCNetworkServicesRef -> SCNetworkServicesRef)
2611 // If there is no mapping found between source and target preferences, then the CFBooleanRef value indicating no value is found is stored (SCNetworkServicesRef -> kCFBooleanFalse)
2612 static CFDictionaryRef
2613 _SCNetworkMigrationCreateServiceMappingUsingBSDNameMapping(SCPreferencesRef sourcePrefs
,
2614 SCPreferencesRef targetPrefs
,
2615 CFDictionaryRef bsdNameMapping
)
2617 CFMutableDictionaryRef serviceMapping
= NULL
; // Mapping of services between source and target configurations
2618 CFStringRef sourceBSDName
= NULL
;
2619 CFIndex sourceCount
= 0;
2620 SCNetworkInterfaceRef sourceInterface
= NULL
;
2621 CFStringRef sourceInterfaceSubType
= NULL
; // Check interface type and subtype to be able to transfer VPN
2622 CFStringRef sourceInterfaceType
= NULL
;
2623 CFArrayRef sourceSCNetworkServices
= NULL
;
2624 CFMutableArrayRef sourceSCNetworkServicesMutable
= NULL
; // Source SCNetworkServiceRef mutable array
2625 SCNetworkServiceRef sourceService
= NULL
;
2626 CFStringRef targetBSDName
= NULL
;
2627 CFStringRef targetBSDNameMapped
= NULL
;
2628 CFIndex targetCount
= 0; // Count of Source and Target Services
2629 SCNetworkInterfaceRef targetInterface
= NULL
;
2630 CFStringRef targetInterfaceSubType
= NULL
; // services during migration
2631 CFStringRef targetInterfaceType
= NULL
;
2632 CFArrayRef targetSCNetworkServices
= NULL
;
2633 CFMutableArrayRef targetSCNetworkServicesMutable
= NULL
; // Target SCNetworkServiceRef mutable array
2634 SCNetworkServiceRef targetService
= NULL
;
2636 // We need BSD Mapping to successfully create service mapping
2637 if (bsdNameMapping
== NULL
) {
2638 SC_log(LOG_NOTICE
, "No BSD name mappings");
2641 sourceSCNetworkServices
= SCNetworkServiceCopyAll(sourcePrefs
);
2642 if (!isA_CFArray(sourceSCNetworkServices
)) {
2643 SC_log(LOG_NOTICE
, "No source network services");
2646 targetSCNetworkServices
= SCNetworkServiceCopyAll(targetPrefs
);
2647 if (!isA_CFArray(targetSCNetworkServices
)) {
2648 SC_log(LOG_NOTICE
, "No target network services");
2652 sourceCount
= CFArrayGetCount(sourceSCNetworkServices
);
2654 sourceSCNetworkServicesMutable
= CFArrayCreateMutableCopy(NULL
, 0, sourceSCNetworkServices
);
2655 targetSCNetworkServicesMutable
= CFArrayCreateMutableCopy(NULL
, 0, targetSCNetworkServices
);
2657 serviceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2659 for (CFIndex idx
= 0; idx
< sourceCount
; idx
++) {
2660 sourceBSDName
= NULL
;
2661 sourceService
= NULL
;
2662 sourceInterface
= NULL
;
2663 sourceInterfaceType
= NULL
;
2664 sourceInterfaceSubType
= NULL
;
2665 targetBSDNameMapped
= NULL
;
2667 targetCount
= CFArrayGetCount(targetSCNetworkServicesMutable
);
2668 sourceService
= (SCNetworkServiceRef
) CFArrayGetValueAtIndex(sourceSCNetworkServicesMutable
, idx
);
2670 sourceInterface
= SCNetworkServiceGetInterface(sourceService
);
2671 if (sourceInterface
== NULL
) {
2672 SC_log(LOG_NOTICE
, "source service with no interface, should never happen");
2676 sourceInterfaceType
= __SCNetworkInterfaceGetEntityType(sourceInterface
);
2677 if ((isA_CFString(sourceInterfaceType
) != NULL
) &&
2678 (CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeVPN
) ||
2679 CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypePPP
))) {
2680 sourceInterfaceSubType
= __SCNetworkInterfaceGetEntitySubType(sourceInterface
);
2681 if (!isA_CFString(sourceInterfaceSubType
)) {
2682 SC_log(LOG_NOTICE
, "source service with VPN/PPP interface missing SubType");
2686 else if ((isA_CFString(sourceInterfaceType
) &&
2687 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeIPSec
) &&
2688 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceType6to4
) &&
2689 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeLoopback
)) ||
2690 !isA_CFString(sourceInterfaceType
)) {
2691 sourceBSDName
= SCNetworkInterfaceGetBSDName(sourceInterface
);
2692 if (!isA_CFString(sourceBSDName
) ||
2693 !CFDictionaryGetValueIfPresent(bsdNameMapping
,
2695 (const void **)&targetBSDNameMapped
) ||
2696 !isA_CFString(targetBSDNameMapped
)) {
2697 SC_log(LOG_INFO
, "No BSD name mapping for %@",
2698 (sourceBSDName
!= NULL
) ? sourceBSDName
: CFSTR("NULL"));
2702 // Find the bsd name in target service
2703 for (CFIndex idx2
= 0; idx2
< targetCount
; idx2
++) {
2704 targetService
= NULL
;
2705 targetInterface
= NULL
;
2706 targetBSDName
= NULL
;
2707 targetInterfaceType
= NULL
;
2708 targetInterfaceSubType
= NULL
;
2710 targetService
= (SCNetworkServiceRef
) CFArrayGetValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2712 targetInterface
= SCNetworkServiceGetInterface(targetService
);
2713 if (targetInterface
== NULL
) {
2714 SC_log(LOG_NOTICE
, "target service with no interface, should never happen");
2717 if (sourceBSDName
!= NULL
) {
2718 targetBSDName
= SCNetworkInterfaceGetBSDName(targetInterface
);
2719 if (!isA_CFString(targetBSDName
)) {
2720 // source interface w/BSD interface, target w/o BSD interface
2724 if (CFEqual(targetBSDName
, targetBSDNameMapped
)) {
2725 // SC_log(LOG_NOTICE, "Removing target BSD name: %@", targetBSDName);
2726 CFDictionaryAddValue(serviceMapping
, sourceService
, targetService
);
2727 CFArrayRemoveValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2731 // Source Interface Type should be VPN
2732 targetInterfaceType
= __SCNetworkInterfaceGetEntityType(targetInterface
);
2733 if ((!isA_CFString(targetInterfaceType
)) ||
2734 (!CFEqual(targetInterfaceType
, kSCValNetInterfaceTypeVPN
) &&
2735 !CFEqual(targetInterfaceType
, kSCValNetInterfaceTypePPP
))) {
2736 SC_log(LOG_NOTICE
, "Unexpected target interface type: %@", targetInterface
);
2739 targetInterfaceSubType
= __SCNetworkInterfaceGetEntitySubType(targetInterface
);
2740 if (!isA_CFString(targetInterfaceSubType
)) {
2741 SC_log(LOG_NOTICE
, "No target interface SubType: %@", targetInterface
);
2745 // Check if the target interface type and the target interface sub type match
2746 if (CFEqual(targetInterfaceType
, sourceInterfaceType
) &&
2747 CFEqual(targetInterfaceSubType
, sourceInterfaceSubType
)) {
2748 // SC_log(LOG_NOTICE, "Removing target BSD Name: %@ for VPN", targetBSDName);
2749 CFDictionaryAddValue(serviceMapping
, sourceService
, targetService
);
2750 CFArrayRemoveValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2755 // Check if sourceService has found a mapping or not, if not the create a NULL mapping to indicate
2756 // the this service needs to be added and not replaced
2757 if (!CFDictionaryContainsKey(serviceMapping
, sourceService
)) {
2758 CFDictionaryAddValue(serviceMapping
, sourceService
, kCFBooleanFalse
);
2764 if (sourceSCNetworkServices
!= NULL
) {
2765 CFRelease(sourceSCNetworkServices
);
2767 if (targetSCNetworkServices
!= NULL
) {
2768 CFRelease(targetSCNetworkServices
);
2770 if (sourceSCNetworkServicesMutable
!= NULL
) {
2771 CFRelease(sourceSCNetworkServicesMutable
);
2773 if (targetSCNetworkServicesMutable
!= NULL
) {
2774 CFRelease(targetSCNetworkServicesMutable
);
2777 return serviceMapping
;
2781 SCPreferencesRef targetPrefs
;
2782 CFDictionaryRef bsdNameMapping
;
2783 CFDictionaryRef setMapping
;
2784 CFDictionaryRef serviceSetMapping
;
2785 } serviceMigrationContext
;
2788 // SCNetworkServiceRef: if target service needs replacement
2789 // CFBooleanRef: if target service is not present
2791 ServiceMigrationAddOrReplace(const void *key
, const void *value
, void *context
)
2793 CFDictionaryRef bsdNameMapping
= NULL
;
2794 serviceMigrationContext
*ctx
= (serviceMigrationContext
*)context
;
2795 CFDictionaryRef setMapping
;
2796 CFDictionaryRef sourceServiceSetMapping
;
2797 SCNetworkServiceRef sourceService
= (SCNetworkServiceRef
)key
;
2798 SCPreferencesRef targetPrefs
= NULL
;
2799 SCNetworkServiceRef targetService
= (SCNetworkServiceRef
)value
;
2801 targetPrefs
= ctx
->targetPrefs
;
2802 bsdNameMapping
= ctx
->bsdNameMapping
;
2803 setMapping
= ctx
->setMapping
;
2804 sourceServiceSetMapping
= ctx
->serviceSetMapping
;
2806 if ((setMapping
!= NULL
) || (sourceServiceSetMapping
!= NULL
)) {
2807 if (isA_SCNetworkService(targetService
)) {
2808 (void)SCNetworkServiceGetName(targetService
); // ensures that the service name will be logged
2809 SC_log(LOG_INFO
, "Removing [target] service: %@", targetService
);
2810 SCNetworkServiceRemove(targetService
);
2814 (void)SCNetworkServiceGetName(sourceService
); // ensures that the service name will be logged
2815 SC_log(LOG_INFO
, "Adding [source] service: %@", sourceService
);
2817 if (!__SCNetworkServiceMigrateNew(targetPrefs
, sourceService
, bsdNameMapping
, setMapping
, sourceServiceSetMapping
)) {
2818 (void)SCNetworkServiceGetName(sourceService
); // ensures that the service name will be logged
2819 SC_log(LOG_INFO
, "*** adding [source] service failed: %@", sourceService
);
2824 _SCNetworkMigrationDoServiceMigration(SCPreferencesRef sourcePrefs
,
2825 SCPreferencesRef targetPrefs
,
2826 CFDictionaryRef serviceMapping
,
2827 CFDictionaryRef bsdNameMapping
,
2828 CFDictionaryRef setMapping
,
2829 CFDictionaryRef serviceSetMapping
)
2831 serviceMigrationContext context
;
2832 Boolean success
= FALSE
;
2834 if ((sourcePrefs
== NULL
) ||
2835 (targetPrefs
== NULL
) ||
2836 !isA_CFDictionary(serviceMapping
) ||
2837 !isA_CFDictionary(bsdNameMapping
)) {
2838 SC_log(LOG_INFO
, "No sourcePrefs, targetPrefs, serviceMapping, or bsdNameMapping");
2842 if (bsdNameMapping
!= NULL
) {
2843 logMapping(LOG_INFO
, bsdNameMapping
, "BSD name mapping");
2845 SC_log(LOG_NOTICE
, "No BSD name mapping");
2848 if (serviceMapping
!= NULL
) {
2849 logMapping(LOG_INFO
, serviceMapping
, "SCNetworkService mapping");
2851 SC_log(LOG_NOTICE
, "No SCNetworkService mapping");
2854 if (setMapping
!= NULL
) {
2855 logMapping(LOG_INFO
, setMapping
, "SCNetworkSet mapping");
2857 SC_log(LOG_NOTICE
, "No SCNetworkSet mapping");
2860 if (serviceSetMapping
!= NULL
) {
2861 logMapping(LOG_INFO
, serviceSetMapping
, "SCNetworkService/SCNetworkSet mapping");
2863 SC_log(LOG_NOTICE
, "No SCNetworkService/SCNetworkSet mapping");
2866 context
.targetPrefs
= targetPrefs
;
2867 context
.bsdNameMapping
= bsdNameMapping
;
2868 context
.setMapping
= setMapping
;
2869 context
.serviceSetMapping
= serviceSetMapping
;
2870 CFDictionaryApplyFunction(serviceMapping
, ServiceMigrationAddOrReplace
, &context
);
2878 _SCNetworkMigrationDoSystemMigration(SCPreferencesRef sourcePrefs
, SCPreferencesRef targetPrefs
)
2880 CFDictionaryRef btmm
;
2881 CFDictionaryRef btmmDSID
;
2882 CFStringRef btmmDSIDPath
;
2883 CFStringRef btmmPath
;
2884 CFStringRef computerName
;
2885 CFStringRef hostname
;
2886 CFStringRef localHostname
;
2887 CFStringEncoding nameEncoding
;
2889 SC_log(LOG_NOTICE
, "Copying \"system\" settings");
2891 if ((sourcePrefs
== NULL
) ||
2892 (targetPrefs
== NULL
)) {
2896 hostname
= SCPreferencesGetHostName(sourcePrefs
);
2897 if (hostname
!= NULL
) {
2898 SCPreferencesSetHostName(targetPrefs
, hostname
);
2899 SC_log(LOG_NOTICE
, " copied HostName");
2902 localHostname
= _SCPreferencesCopyLocalHostName(sourcePrefs
);
2903 if (localHostname
!= NULL
) {
2904 SCPreferencesSetLocalHostName(targetPrefs
, localHostname
);
2905 CFRelease(localHostname
);
2906 SC_log(LOG_NOTICE
, " copied LocalHostName");
2909 computerName
= _SCPreferencesCopyComputerName(sourcePrefs
, &nameEncoding
);
2910 if (computerName
!= NULL
) {
2911 SCPreferencesSetComputerName(targetPrefs
, computerName
, nameEncoding
);
2912 CFRelease(computerName
);
2913 SC_log(LOG_NOTICE
, " copied ComputerName");
2916 btmmPath
= CFStringCreateWithFormat(NULL
, NULL
,
2921 btmm
= SCPreferencesPathGetValue(sourcePrefs
, btmmPath
);
2923 SCPreferencesPathSetValue(targetPrefs
, btmmPath
, btmm
);
2925 CFRelease(btmmPath
);
2927 btmmDSIDPath
= CFStringCreateWithFormat(NULL
, NULL
,
2931 BACK_TO_MY_MAC_DSIDS
);
2932 btmmDSID
= SCPreferencesPathGetValue(sourcePrefs
, btmmDSIDPath
);
2933 if (btmmDSID
!= NULL
) {
2934 SCPreferencesPathSetValue(targetPrefs
, btmmDSIDPath
, btmmDSID
);
2936 CFRelease(btmmDSIDPath
);
2941 #if !TARGET_OS_IPHONE
2944 CFMutableArrayRef interfaceList
;
2945 SCPreferencesRef ni_prefs
;
2946 CFDictionaryRef bsdNameMapping
;
2947 } virtualInterfaceMemberListContext
;
2950 SCPreferencesRef prefs
;
2951 SCPreferencesRef ni_prefs
;
2952 CFDictionaryRef bsdNameMapping
;
2953 CFDictionaryRef virtualBSDNameMapping
;
2954 CFDictionaryRef mappingBSDNameToService
;
2955 CFDictionaryRef setMapping
;
2956 CFDictionaryRef serviceSetMapping
;
2957 } virtualInterfaceContext
;
2960 add_virtual_interface(const void *value
, void *context
)
2962 virtualInterfaceMemberListContext
*ctx
= (virtualInterfaceMemberListContext
*)context
;
2963 CFDictionaryRef bsdNameMapping
= ctx
->bsdNameMapping
;
2964 CFMutableArrayRef interfaceList
= ctx
->interfaceList
;
2965 CFStringRef oldInterfaceBSDName
= (CFStringRef
)value
;
2966 SCNetworkInterfaceRef newInterface
;
2967 CFStringRef newInterfaceBSDName
;
2969 SC_log(LOG_INFO
, "old BSD interface name: %@", oldInterfaceBSDName
);
2971 newInterfaceBSDName
= CFDictionaryGetValue(bsdNameMapping
, oldInterfaceBSDName
);
2972 if (newInterfaceBSDName
== NULL
) {
2975 SC_log(LOG_INFO
, "new BSD interface name: %@", newInterfaceBSDName
);
2977 newInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ctx
->ni_prefs
, newInterfaceBSDName
);
2978 if (newInterface
!= NULL
) {
2979 SC_log(LOG_INFO
, "adding interface to interfaceList: %@", newInterface
);
2980 CFArrayAppendValue(interfaceList
, newInterface
);
2981 CFRelease(newInterface
);
2987 add_target_bridge(const void *key
, const void *value
, void *context
)
2989 CFStringRef bridgeName
;
2990 CFDictionaryRef bridgeOptions
;
2991 virtualInterfaceContext
*ctx
= (virtualInterfaceContext
*)context
;
2992 CFDictionaryRef bridgeBSDNameMapping
= ctx
->virtualBSDNameMapping
;
2993 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
2994 virtualInterfaceMemberListContext memberListContext
;
2995 CFMutableArrayRef newInterfaceList
;
2996 SCBridgeInterfaceRef newBridge
;
2997 SCBridgeInterfaceRef oldBridge
= (SCBridgeInterfaceRef
)key
;
2998 CFStringRef oldBSDName
;
2999 CFArrayRef oldInterfaceList
= (CFArrayRef
)value
;
3000 CFArrayRef oldServiceList
;
3001 SCPreferencesRef prefs
= ctx
->prefs
;
3002 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
3003 CFDictionaryRef setMapping
= ctx
->setMapping
;
3005 newInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3007 memberListContext
.bsdNameMapping
= ctx
->bsdNameMapping
;
3008 memberListContext
.interfaceList
= newInterfaceList
;
3009 memberListContext
.ni_prefs
= ctx
->ni_prefs
;
3011 CFArrayApplyFunction(oldInterfaceList
, CFRangeMake(0, CFArrayGetCount(oldInterfaceList
)), add_virtual_interface
, &memberListContext
);
3013 newBridge
= SCBridgeInterfaceCreate(prefs
);
3015 if (!__SCBridgeInterfaceSetMemberInterfaces(newBridge
, newInterfaceList
)) {
3016 SC_log(LOG_NOTICE
, "__SCBridgeInterfaceSetMemberInterfaces() failed");
3018 CFRelease(newInterfaceList
);
3020 bridgeOptions
= SCBridgeInterfaceGetOptions(oldBridge
);
3021 if (bridgeOptions
!= NULL
) {
3022 SCBridgeInterfaceSetOptions(newBridge
, bridgeOptions
);
3025 bridgeName
= SCNetworkInterfaceGetLocalizedDisplayName(oldBridge
);
3027 if (bridgeName
!= NULL
) {
3028 SCBridgeInterfaceSetLocalizedDisplayName(newBridge
, bridgeName
);
3031 oldBSDName
= SCNetworkInterfaceGetBSDName(oldBridge
);
3032 if (oldBSDName
== NULL
) {
3036 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
3037 if (oldServiceList
== NULL
) {
3041 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
3042 SCNetworkServiceRef oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
3043 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, bridgeBSDNameMapping
, setMapping
, serviceSetMapping
)) {
3044 SC_log(LOG_NOTICE
, "could not migrate bridge service: %@", oldService
);
3048 CFRelease(newBridge
);
3052 _SCNetworkMigrationRemoveBridgeServices(SCPreferencesRef prefs
)
3054 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3056 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3057 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3058 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3059 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3061 if ((bsdName
!= NULL
) &&
3062 (SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBridge
)) {
3063 SC_log(LOG_INFO
, "removing service: %@", service
);
3064 SCNetworkServiceRemove(service
);
3067 CFRelease(services
);
3071 static CFDictionaryRef
3072 _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(SCPreferencesRef prefs
)
3074 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3075 CFMutableDictionaryRef bridgeServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3077 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3078 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3079 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3080 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3082 if ((bsdName
!= NULL
) &&
3083 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBridge
) {
3084 CFMutableArrayRef serviceList
;
3085 if (!CFDictionaryContainsKey(bridgeServices
, bsdName
)) {
3086 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3087 CFDictionaryAddValue(bridgeServices
, bsdName
, serviceList
);
3088 CFRelease(serviceList
);
3090 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(bridgeServices
, bsdName
);
3091 CFArrayAppendValue(serviceList
, service
);
3094 CFRelease(services
);
3095 return bridgeServices
;
3100 _SCNetworkMigrationDoBridgeMigration(SCPreferencesRef sourcePrefs
,
3101 SCPreferencesRef sourceNIPrefs
,
3102 SCPreferencesRef targetPrefs
,
3103 SCPreferencesRef targetNIPrefs
,
3104 CFDictionaryRef bsdNameMapping
,
3105 CFDictionaryRef setMapping
,
3106 CFDictionaryRef serviceSetMapping
)
3108 #pragma unused(sourceNIPrefs)
3109 CFArrayRef allSourceBridges
;
3110 CFArrayRef allTargetBridges
;
3111 SCBridgeInterfaceRef bridge
;
3112 CFMutableDictionaryRef bridgeInterfaceMapping
= NULL
;
3113 CFMutableDictionaryRef bridgeMapping
;
3114 CFDictionaryRef bsdNameToBridgeServices
;
3115 virtualInterfaceContext context
;
3117 Boolean success
= FALSE
;
3119 allSourceBridges
= SCBridgeInterfaceCopyAll(sourcePrefs
);
3120 allTargetBridges
= SCBridgeInterfaceCopyAll(targetPrefs
);
3122 bsdNameToBridgeServices
= _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(sourcePrefs
);
3124 bridgeInterfaceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3125 bridgeMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3127 // Create Bridge Interface Mapping
3128 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceBridges
); idx
++) {
3129 bridge
= CFArrayGetValueAtIndex(allSourceBridges
, idx
);
3130 CFArrayRef bridgeMembers
= SCBridgeInterfaceGetMemberInterfaces(bridge
);
3131 CFMutableArrayRef interfaceList
;
3133 interfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3134 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(bridgeMembers
); idx2
++) {
3135 CFStringRef interfaceName
= NULL
;
3136 SCNetworkInterfaceRef interface
= NULL
;
3138 interface
= CFArrayGetValueAtIndex(bridgeMembers
, idx2
);
3139 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
3141 if (CFDictionaryContainsKey(bsdNameMapping
, interfaceName
)) {
3142 CFStringRef bridgeNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("bridge%ld"), count
);
3143 CFDictionaryAddValue(bridgeMapping
, interfaceName
, bridgeNewName
);
3144 CFArrayAppendValue(interfaceList
, interfaceName
);
3145 CFRelease(bridgeNewName
);
3149 if (CFArrayGetCount(interfaceList
) > 0) {
3150 CFDictionaryAddValue(bridgeInterfaceMapping
, bridge
, interfaceList
);
3152 CFRelease(interfaceList
);
3154 // Remove bridge services from target
3155 _SCNetworkMigrationRemoveBridgeServices(targetPrefs
);
3157 // Remove Target Bridges
3158 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetBridges
); idx
++) {
3159 bridge
= CFArrayGetValueAtIndex(allTargetBridges
, idx
);
3160 if (!SCBridgeInterfaceRemove(bridge
)) {
3161 SC_log(LOG_NOTICE
, "SCBridgeInterfaceRemove() failed: %@", bridge
);
3166 context
.prefs
= targetPrefs
;
3167 context
.ni_prefs
= targetNIPrefs
;
3168 context
.bsdNameMapping
= bsdNameMapping
;
3169 context
.virtualBSDNameMapping
= bridgeMapping
;
3170 context
.mappingBSDNameToService
= bsdNameToBridgeServices
;
3171 context
.setMapping
= setMapping
;
3172 context
.serviceSetMapping
= serviceSetMapping
;
3174 // Add Bridge configurations at the target using mapping
3175 CFDictionaryApplyFunction(bridgeInterfaceMapping
, add_target_bridge
, &context
);
3179 CFRelease(allSourceBridges
);
3180 CFRelease(allTargetBridges
);
3181 CFRelease(bridgeInterfaceMapping
);
3182 CFRelease(bridgeMapping
);
3183 CFRelease(bsdNameToBridgeServices
);
3189 add_target_bond(const void *key
, const void *value
, void *context
)
3191 CFNumberRef bondMode
;
3192 CFStringRef bondName
;
3193 CFDictionaryRef bondOptions
;
3194 virtualInterfaceContext
*ctx
= (virtualInterfaceContext
*)context
;
3195 CFDictionaryRef bondBSDNameMapping
= ctx
->virtualBSDNameMapping
;
3196 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
3197 virtualInterfaceMemberListContext memberListContext
;
3198 CFMutableArrayRef newInterfaceList
;
3199 SCBondInterfaceRef newBond
;
3200 SCBondInterfaceRef oldBond
= (SCBondInterfaceRef
)key
;
3201 CFStringRef oldBSDName
;
3202 CFArrayRef oldInterfaceList
= (CFArrayRef
)value
;
3203 CFArrayRef oldServiceList
;
3204 SCPreferencesRef prefs
= ctx
->prefs
;
3205 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
3206 CFDictionaryRef setMapping
= ctx
->setMapping
;
3208 newInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3210 memberListContext
.bsdNameMapping
= ctx
->bsdNameMapping
;
3211 memberListContext
.interfaceList
= newInterfaceList
;
3212 memberListContext
.ni_prefs
= ctx
->ni_prefs
;
3214 CFArrayApplyFunction(oldInterfaceList
, CFRangeMake(0, CFArrayGetCount(oldInterfaceList
)), add_virtual_interface
, &memberListContext
);
3216 newBond
= SCBondInterfaceCreate(prefs
);
3217 if (!__SCBondInterfaceSetMemberInterfaces(newBond
, newInterfaceList
)) {
3218 SC_log(LOG_NOTICE
, "__SCBondInterfaceSetMemberInterfaces() failed");
3220 CFRelease(newInterfaceList
);
3222 bondOptions
= SCBondInterfaceGetOptions(oldBond
);
3223 if (bondOptions
!= NULL
) {
3224 SCBondInterfaceSetOptions(newBond
, bondOptions
);
3227 bondName
= SCNetworkInterfaceGetLocalizedDisplayName(oldBond
);
3228 if (bondName
!= NULL
) {
3229 SCBondInterfaceSetLocalizedDisplayName(newBond
, bondName
);
3232 bondMode
= SCBondInterfaceGetMode(oldBond
);
3233 if (bondMode
!= NULL
) {
3234 SCBondInterfaceSetMode(newBond
, bondMode
);
3236 oldBSDName
= SCNetworkInterfaceGetBSDName(oldBond
);
3237 if (oldBSDName
== NULL
) {
3241 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
3242 if (oldServiceList
== NULL
) {
3246 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
3247 SCNetworkServiceRef oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
3248 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, bondBSDNameMapping
, setMapping
, serviceSetMapping
)) {
3249 SC_log(LOG_NOTICE
, "could not migrate bond service: %@", oldService
);
3257 _SCNetworkMigrationRemoveBondServices(SCPreferencesRef prefs
)
3259 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3261 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3262 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3263 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3264 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3266 if ((bsdName
!= NULL
) &&
3267 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBond
) {
3268 SCNetworkServiceRemove(service
);
3271 CFRelease(services
);
3275 static CFDictionaryRef
3276 _SCNetworkMigrationCopyMappingBSDNameToBondServices(SCPreferencesRef prefs
)
3278 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3279 CFMutableDictionaryRef bondServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3281 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3282 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3283 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3284 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3286 if ((bsdName
!= NULL
) &&
3287 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBond
) {
3288 CFMutableArrayRef serviceList
;
3289 if (!CFDictionaryContainsKey(bondServices
, bsdName
)) {
3290 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3291 CFDictionaryAddValue(bondServices
, bsdName
, serviceList
);
3292 CFRelease(serviceList
);
3294 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(bondServices
, bsdName
);
3295 CFArrayAppendValue(serviceList
, service
);
3298 CFRelease(services
);
3299 return bondServices
;
3304 _SCNetworkMigrationDoBondMigration(SCPreferencesRef sourcePrefs
,
3305 SCPreferencesRef sourceNIPrefs
,
3306 SCPreferencesRef targetPrefs
,
3307 SCPreferencesRef targetNIPrefs
,
3308 CFDictionaryRef bsdNameMapping
,
3309 CFDictionaryRef setMapping
,
3310 CFDictionaryRef serviceSetMapping
)
3312 #pragma unused(sourceNIPrefs)
3313 CFArrayRef allSourceBonds
;
3314 CFArrayRef allTargetBonds
;
3315 SCBondInterfaceRef bond
;
3316 CFMutableDictionaryRef bondInterfaceMapping
= NULL
;
3317 CFMutableDictionaryRef bondMapping
;
3318 CFDictionaryRef bsdNameToBondServices
;
3319 virtualInterfaceContext context
;
3321 Boolean success
= FALSE
;
3323 allSourceBonds
= SCBondInterfaceCopyAll(sourcePrefs
);
3324 allTargetBonds
= SCBondInterfaceCopyAll(targetPrefs
);
3326 bsdNameToBondServices
= _SCNetworkMigrationCopyMappingBSDNameToBondServices(sourcePrefs
);
3328 bondInterfaceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3329 bondMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3330 // Create Bond Interface mapping
3331 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceBonds
); idx
++) {
3332 bond
= CFArrayGetValueAtIndex(allSourceBonds
, idx
);
3333 CFArrayRef bondMembers
= SCBondInterfaceGetMemberInterfaces(bond
);
3334 CFMutableArrayRef interfaceList
;
3336 interfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3337 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(bondMembers
); idx2
++) {
3338 CFStringRef interfaceName
;
3339 SCNetworkInterfaceRef interface
;
3341 interface
= CFArrayGetValueAtIndex(bondMembers
, idx2
);
3342 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
3344 if (CFDictionaryContainsKey(bsdNameMapping
, interfaceName
)) {
3345 CFStringRef bondNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("bond%ld"), count
);
3346 CFDictionaryAddValue(bondMapping
, interfaceName
, bondNewName
);
3347 CFArrayAppendValue(interfaceList
, interfaceName
);
3348 CFRelease(bondNewName
);
3352 if (CFArrayGetCount(interfaceList
) > 0) {
3353 CFDictionaryAddValue(bondInterfaceMapping
, bond
, interfaceList
);
3355 CFRelease(interfaceList
);
3357 // Remove bond services from target
3358 _SCNetworkMigrationRemoveBondServices(targetPrefs
);
3360 // Remove Target Bonds
3361 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetBonds
); idx
++) {
3362 bond
= CFArrayGetValueAtIndex(allTargetBonds
, idx
);
3363 if (!SCBondInterfaceRemove(bond
)) {
3364 SC_log(LOG_NOTICE
, "SCBondInterfaceRemove() failed: %@", bond
);
3369 context
.prefs
= targetPrefs
;
3370 context
.ni_prefs
= targetNIPrefs
;
3371 context
.bsdNameMapping
= bsdNameMapping
;
3372 context
.virtualBSDNameMapping
= bondMapping
;
3373 context
.mappingBSDNameToService
= bsdNameToBondServices
;
3374 context
.setMapping
= setMapping
;
3375 context
.serviceSetMapping
= serviceSetMapping
;
3377 // Add Bond configurations at the target using mapping
3378 CFDictionaryApplyFunction(bondInterfaceMapping
, add_target_bond
, &context
);
3382 CFRelease(allSourceBonds
);
3383 CFRelease(allTargetBonds
);
3384 CFRelease(bondInterfaceMapping
);
3385 CFRelease(bondMapping
);
3386 CFRelease(bsdNameToBondServices
);
3391 add_target_vlan(const void *value
, void *context
)
3393 CFDictionaryRef bsdNameMapping
;
3394 virtualInterfaceContext
*ctx
= (virtualInterfaceContext
*)context
;
3395 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
3396 SCPreferencesRef prefs
= ctx
->prefs
;
3397 SCVLANInterfaceRef newVLAN
= NULL
;
3398 SCNetworkInterfaceRef newPhysicalInterface
= NULL
;
3399 CFStringRef newPhysicalInterfaceName
;
3400 SCVLANInterfaceRef oldVLAN
= (SCVLANInterfaceRef
)value
;
3401 CFStringRef oldBSDName
;
3402 SCNetworkInterfaceRef oldPhysicalInterface
;
3403 CFStringRef oldPhysicalInterfaceName
;
3404 SCNetworkServiceRef oldService
;
3405 CFArrayRef oldServiceList
;
3406 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
3407 CFDictionaryRef setMapping
= ctx
->setMapping
;
3408 CFDictionaryRef vlanBSDNameMapping
= ctx
->virtualBSDNameMapping
;
3409 CFNumberRef vlanTag
;
3410 CFStringRef vlanName
;
3411 CFDictionaryRef vlanOptions
;
3413 bsdNameMapping
= ctx
->bsdNameMapping
;
3415 oldPhysicalInterface
= SCVLANInterfaceGetPhysicalInterface(oldVLAN
);
3416 if (oldPhysicalInterface
== NULL
) {
3417 SC_log(LOG_NOTICE
, "No old VLAN physical interface");
3421 oldPhysicalInterfaceName
= SCNetworkInterfaceGetBSDName(oldPhysicalInterface
);
3422 if (oldPhysicalInterfaceName
== NULL
) {
3423 SC_log(LOG_NOTICE
, "No old VLAN physical interface name");
3427 newPhysicalInterfaceName
= CFDictionaryGetValue(bsdNameMapping
, oldPhysicalInterfaceName
);
3428 if (newPhysicalInterfaceName
== NULL
) {
3429 SC_log(LOG_NOTICE
, "No new VLAN physical interface name");
3432 newPhysicalInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ctx
->ni_prefs
, newPhysicalInterfaceName
);
3433 if (newPhysicalInterface
== NULL
) {
3434 SC_log(LOG_NOTICE
, "Could not create new VLAN physical interface");
3438 vlanTag
= SCVLANInterfaceGetTag(oldVLAN
);
3439 if (vlanTag
== NULL
) {
3440 SC_log(LOG_NOTICE
, "No old VLAN interface tag");
3444 newVLAN
= SCVLANInterfaceCreate(prefs
, newPhysicalInterface
, vlanTag
);
3445 if (newVLAN
== NULL
) {
3446 SC_log(LOG_NOTICE
, "Could not create new VLAN interface");
3450 vlanName
= SCNetworkInterfaceGetLocalizedDisplayName(oldVLAN
);
3451 if (vlanName
!= NULL
) {
3452 SCVLANInterfaceSetLocalizedDisplayName(newVLAN
, vlanName
);
3455 vlanOptions
= SCVLANInterfaceGetOptions(oldVLAN
);
3456 if (vlanOptions
!= NULL
) {
3457 SCVLANInterfaceSetOptions(newVLAN
, vlanOptions
);
3459 oldBSDName
= SCNetworkInterfaceGetBSDName(oldVLAN
);
3461 if (oldBSDName
== NULL
) {
3465 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
3466 if (oldServiceList
== NULL
) {
3470 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
3471 oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
3472 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, vlanBSDNameMapping
, setMapping
, serviceSetMapping
)) {
3473 SC_log(LOG_NOTICE
, "Could not migrate VLAN service: %@", oldService
);
3478 if (newPhysicalInterface
!= NULL
) {
3479 CFRelease(newPhysicalInterface
);
3481 if (newVLAN
!= NULL
) {
3487 _SCNetworkMigrationRemoveVLANServices(SCPreferencesRef prefs
)
3489 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3491 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3492 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3493 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3494 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3496 if ((bsdName
!= NULL
) &&
3497 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeVLAN
) {
3498 SCNetworkServiceRemove(service
);
3502 CFRelease(services
);
3506 static CFDictionaryRef
3507 _SCNetworkMigrationCopyMappingBSDNameToVLANServices(SCPreferencesRef prefs
)
3509 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
3510 CFMutableDictionaryRef vlanServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3512 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
3513 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
3514 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
3515 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3517 if ((bsdName
!= NULL
) &&
3518 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeVLAN
) {
3519 CFMutableArrayRef serviceList
;
3520 if (!CFDictionaryContainsKey(vlanServices
, bsdName
)) {
3521 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3522 CFDictionaryAddValue(vlanServices
, bsdName
, serviceList
);
3523 CFRelease(serviceList
);
3525 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(vlanServices
, bsdName
);
3526 CFArrayAppendValue(serviceList
, service
);
3529 CFRelease(services
);
3530 return vlanServices
;
3534 _SCNetworkMigrationDoVLANMigration(SCPreferencesRef sourcePrefs
,
3535 SCPreferencesRef sourceNIPrefs
,
3536 SCPreferencesRef targetPrefs
,
3537 SCPreferencesRef targetNIPrefs
,
3538 CFDictionaryRef bsdNameMapping
,
3539 CFDictionaryRef setMapping
,
3540 CFDictionaryRef serviceSetMapping
)
3542 #pragma unused(sourceNIPrefs)
3543 CFArrayRef allSourceVLAN
;
3544 CFArrayRef allTargetVLAN
;
3545 virtualInterfaceContext context
;
3547 Boolean success
= FALSE
;
3548 SCVLANInterfaceRef vlan
;
3549 CFMutableArrayRef vlanList
;
3550 CFMutableDictionaryRef vlanMapping
;
3551 CFDictionaryRef bsdNameToVLANServices
;
3553 allSourceVLAN
= SCVLANInterfaceCopyAll(sourcePrefs
);
3554 allTargetVLAN
= SCVLANInterfaceCopyAll(targetPrefs
);
3556 bsdNameToVLANServices
= _SCNetworkMigrationCopyMappingBSDNameToVLANServices(sourcePrefs
);
3558 vlanList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3559 vlanMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
3561 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceVLAN
); idx
++) {
3562 vlan
= CFArrayGetValueAtIndex(allSourceVLAN
, idx
);
3563 CFStringRef vlanBSDName
= SCNetworkInterfaceGetBSDName(vlan
);
3564 SCNetworkInterfaceRef physicalInterface
= SCVLANInterfaceGetPhysicalInterface(vlan
);
3565 CFStringRef physicalInterfaceName
= SCNetworkInterfaceGetBSDName(physicalInterface
);
3567 // Add VLAN to be migrated if the mapping between interfaces exists
3568 if (CFDictionaryContainsKey(bsdNameMapping
, physicalInterfaceName
)) {
3569 CFStringRef vlanNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("vlan%ld"), count
);
3570 CFDictionaryAddValue(vlanMapping
, vlanBSDName
, vlanNewName
);
3571 CFArrayAppendValue(vlanList
, vlan
);
3572 CFRelease(vlanNewName
);
3576 // Remove vlan services from target
3577 _SCNetworkMigrationRemoveVLANServices(targetPrefs
);
3579 // Remove Target VLANs
3580 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetVLAN
); idx
++) {
3581 vlan
= CFArrayGetValueAtIndex(allTargetVLAN
, idx
);
3582 if (!SCVLANInterfaceRemove(vlan
)) {
3583 SC_log(LOG_NOTICE
, "SCVLANInterfaceRemove() failed: %@", vlan
);
3588 context
.prefs
= targetPrefs
;
3589 context
.ni_prefs
= targetNIPrefs
;
3590 context
.bsdNameMapping
= bsdNameMapping
;
3591 context
.virtualBSDNameMapping
= vlanMapping
;
3592 context
.mappingBSDNameToService
= bsdNameToVLANServices
;
3593 context
.setMapping
= setMapping
;
3594 context
.serviceSetMapping
= serviceSetMapping
;
3596 // Add VLAN configurations at the target using vlanList
3597 CFArrayApplyFunction(vlanList
, CFRangeMake(0, CFArrayGetCount(vlanList
)), add_target_vlan
, &context
);
3601 CFRelease(allSourceVLAN
);
3602 CFRelease(allTargetVLAN
);
3603 CFRelease(vlanList
);
3604 CFRelease(vlanMapping
);
3605 CFRelease(bsdNameToVLANServices
);
3610 _SCNetworkMigrationDoVirtualNetworkInterfaceMigration(SCPreferencesRef sourcePrefs
,
3611 SCPreferencesRef sourceNIPrefs
,
3612 SCPreferencesRef targetPrefs
,
3613 SCPreferencesRef targetNIPrefs
,
3614 CFDictionaryRef bsdNameMapping
,
3615 CFDictionaryRef setMapping
,
3616 CFDictionaryRef serviceSetMapping
)
3619 if (!_SCNetworkMigrationDoBridgeMigration(sourcePrefs
, sourceNIPrefs
,
3620 targetPrefs
, targetNIPrefs
,
3621 bsdNameMapping
, setMapping
, serviceSetMapping
)) {
3622 SC_log(LOG_NOTICE
, "Bridge migration failed");
3626 if (!_SCNetworkMigrationDoBondMigration(sourcePrefs
, sourceNIPrefs
,
3627 targetPrefs
, targetNIPrefs
,
3628 bsdNameMapping
, setMapping
, serviceSetMapping
)) {
3629 SC_log(LOG_NOTICE
, "Bond migration failed");
3633 if (!_SCNetworkMigrationDoVLANMigration(sourcePrefs
, sourceNIPrefs
,
3634 targetPrefs
, targetNIPrefs
,
3635 bsdNameMapping
, setMapping
, serviceSetMapping
)) {
3636 SC_log(LOG_NOTICE
, "VLAN migration failed");
3640 #endif // !TARGET_OS_IPHONE
3643 SCPreferencesRef prefs
;
3644 CFArrayRef serviceOrder
;
3645 CFMutableArrayRef serviceOrderMutable
;
3647 } migrated_service_context
;
3650 create_migrated_order(const void *value
, void *context
)
3652 migrated_service_context
*ctx
= (migrated_service_context
*)context
;
3653 CFMutableArrayRef migratedServiceOrder
= ctx
->serviceOrderMutable
;
3654 CFArrayRef targetServiceOrder
= ctx
->serviceOrder
;
3655 CFStringRef migratedServiceID
= (CFStringRef
)value
;
3656 Boolean
*success
= ctx
->success
;
3658 if (*success
== FALSE
) {
3662 // Preserving the service order in the source configuration for the services
3663 // which were migrated into the target configuration
3664 for (CFIndex idx
= 0; idx
< CFArrayGetCount(targetServiceOrder
); idx
++) {
3665 CFStringRef targetServiceID
= CFArrayGetValueAtIndex(targetServiceOrder
, idx
);
3666 if (CFEqual(migratedServiceID
, targetServiceID
)) {
3667 CFArrayAppendValue(migratedServiceOrder
, migratedServiceID
);
3674 SCPreferencesRef prefs
;
3675 CFMutableArrayRef serviceListMutable
;
3676 CFArrayRef serviceOrder
;
3678 } non_migrated_service_context
;
3681 create_non_migrated_service_list(const void *value
, void *context
)
3683 non_migrated_service_context
*ctx
= (non_migrated_service_context
*)context
;
3684 CFArrayRef migratedServiceOrder
= ctx
->serviceOrder
;
3685 CFMutableArrayRef nonMigratedService
= ctx
->serviceListMutable
;
3686 SCPreferencesRef prefs
= ctx
->prefs
;
3687 SCNetworkServiceRef service
;
3688 Boolean
*success
= ctx
->success
;
3689 CFStringRef targetServiceID
= (CFStringRef
)value
;
3691 if (*success
== FALSE
) {
3695 // Adding all services not present in migratedServiceOrder into nonMigrated service
3696 if (CFArrayGetFirstIndexOfValue(migratedServiceOrder
,
3697 CFRangeMake(0, CFArrayGetCount(migratedServiceOrder
)),
3698 targetServiceID
) != kCFNotFound
) {
3699 // if service already present
3703 service
= SCNetworkServiceCopy(prefs
, targetServiceID
);
3704 if (service
== NULL
) {
3709 CFArrayAppendValue(nonMigratedService
, service
);
3714 preserve_service_order(const void *key
, const void *value
, void *context
)
3716 migrated_service_context migrated_context
;
3717 CFMutableArrayRef migratedServiceOrder
;
3718 non_migrated_service_context non_migrated_context
;
3719 CFMutableArrayRef nonMigratedServices
;
3720 SCNetworkSetRef sourceSet
= (SCNetworkSetRef
)key
;
3721 CFArrayRef sourceServiceOrder
= NULL
;
3722 Boolean
*success
= (Boolean
*)context
;
3723 SCNetworkSetRef targetSet
= (SCNetworkSetRef
)value
;
3724 SCNetworkSetPrivateRef targetPrivate
= (SCNetworkSetPrivateRef
)targetSet
;
3725 CFArrayRef targetServiceOrder
= NULL
;
3727 if (*success
== FALSE
) {
3731 migratedServiceOrder
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3732 nonMigratedServices
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3734 sourceServiceOrder
= SCNetworkSetGetServiceOrder(sourceSet
);
3735 if (sourceServiceOrder
== NULL
) {
3738 targetServiceOrder
= SCNetworkSetGetServiceOrder(targetSet
);
3739 if (targetServiceOrder
== NULL
) {
3743 migrated_context
.prefs
= NULL
;
3744 migrated_context
.serviceOrder
= targetServiceOrder
;
3745 migrated_context
.serviceOrderMutable
= migratedServiceOrder
;
3746 migrated_context
.success
= success
;
3748 // Creating a list of service IDs which were migrated in the target set
3749 // while maintaining the service order of the source set
3750 CFArrayApplyFunction(sourceServiceOrder
, CFRangeMake(0, CFArrayGetCount(sourceServiceOrder
)), create_migrated_order
, &migrated_context
);
3751 if (*success
== FALSE
) {
3755 non_migrated_context
.prefs
= targetPrivate
->prefs
;
3756 non_migrated_context
.serviceOrder
= migratedServiceOrder
;
3757 non_migrated_context
.serviceListMutable
= nonMigratedServices
;
3758 non_migrated_context
.success
= success
;
3760 // Creating a list of all the services which were not migrated from the
3761 // source set to the target set
3762 CFArrayApplyFunction(targetServiceOrder
, CFRangeMake(0, CFArrayGetCount(targetServiceOrder
)), create_non_migrated_service_list
, &non_migrated_context
);
3764 // Remove non migrated service
3765 for (CFIndex idx
= 0; idx
< CFArrayGetCount(nonMigratedServices
); idx
++) {
3766 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(nonMigratedServices
, idx
);
3767 SCNetworkSetRemoveService(targetSet
, service
);
3770 // Set migrated service order
3771 SCNetworkSetSetServiceOrder(targetSet
, migratedServiceOrder
);
3773 // Add non migrated services
3774 for (CFIndex idx
= 0; idx
< CFArrayGetCount(nonMigratedServices
); idx
++) {
3776 SCNetworkServiceRef service
;
3778 service
= CFArrayGetValueAtIndex(nonMigratedServices
, idx
);
3779 ok
= SCNetworkSetAddService(targetSet
, service
);
3781 CFStringRef bsdName
;
3782 SCNetworkInterfaceRef interface
;
3784 interface
= SCNetworkServiceGetInterface(service
);
3785 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
3786 SC_log(LOG_NOTICE
, "preserve_service_order(): could not add service: %s"
3787 "\n serviceID = %@, interface = %@",
3788 SCErrorString(SCError()),
3789 SCNetworkServiceGetServiceID(service
),
3790 bsdName
!= NULL
? bsdName
: CFSTR("?"));
3795 CFRelease(migratedServiceOrder
);
3796 CFRelease(nonMigratedServices
);
3802 _SCNetworkMigrationDoServiceOrderMigration(SCPreferencesRef sourcePrefs
,
3803 SCPreferencesRef targetPrefs
,
3804 CFDictionaryRef setMapping
)
3806 #pragma unused(sourcePrefs)
3807 #pragma unused(targetPrefs)
3808 Boolean success
= TRUE
;
3810 if (!isA_CFDictionary(setMapping
)) {
3814 CFDictionaryApplyFunction(setMapping
, preserve_service_order
, &success
);
3819 // This is a function that looks at source and target network configuration
3820 // and determines what network configurations can be transferred from source to
3823 _SCNetworkConfigurationMigrateConfiguration(CFURLRef sourceDir
, CFURLRef targetDir
)
3825 CFDictionaryRef bsdNameMapping
= NULL
; // Mapping between BSD name and SCNetworkInterfaceRef to help with mapping services
3826 CFMutableDictionaryRef builtinMapping
= NULL
; // Mapping between builtin interfaces between source and target configurations: (SCNetworkInterfaceRef -> SCNetworkInterfaceRef)
3827 CFMutableDictionaryRef externalMapping
= NULL
; // Mapping between external interfaces between source and target configurations: (SCNetworkInterfaceRef -> SCNetworkInterfaceRef)
3828 Boolean migrationSuccess
= FALSE
;
3829 CFArrayRef newTargetNetworkInterfaceEntities
= NULL
; // Array of Interface Entity which used to create new target interfaces created during migration
3830 CFDictionaryRef serviceMapping
= NULL
; // Mapping between services of source to target. (SCNetworkServicesRef -> SCNetworkServicesRef)
3831 CFDictionaryRef setMapping
= NULL
;
3832 CFDictionaryRef sourceServiceSetMapping
= NULL
;
3833 CFArrayRef sourceConfigurationFiles
= NULL
; // Path to the source configuration files which need to be migrated
3834 CFStringRef sourceModel
= NULL
;
3835 CFURLRef sourceNetworkInterfaceFile
= NULL
; // Source CFURLRef for preferences.plist and NetworkInterfaces.plist
3836 char sourceNetworkInterfaceFileStr
[PATH_MAX
];
3837 CFStringRef sourceNetworkInterfaceFileString
= NULL
; // Source CFStringRef for preferences.plist and NetworkInterfaces.plist
3838 SCPreferencesRef sourceNetworkInterfacePrefs
= NULL
; // Source SCPreferencesRef for preferences.plist and NetworkInterfaces.plist
3839 CFURLRef sourcePreferencesFile
= NULL
;
3840 char sourcePreferencesFileStr
[PATH_MAX
];
3841 CFStringRef sourcePreferencesFileString
= NULL
;
3842 SCPreferencesRef sourcePrefs
= NULL
;
3844 CFArrayRef targetConfigurationFiles
= NULL
; // Path to the target configuration files where migration will take place to
3845 Boolean targetConfigurationFilesPresent
;
3846 CFStringRef targetModel
= NULL
;
3847 CFURLRef targetNetworkInterfaceFile
= NULL
; // Target CFURLRef for preferences.plist and NetworkInterfaces.plist
3848 char targetNetworkInterfaceFileStr
[PATH_MAX
];
3849 CFStringRef targetNetworkInterfaceFileString
= NULL
; // Target CFStringRef for preferences.plist and NetworkInterfaces.plist
3850 SCPreferencesRef targetNetworkInterfacePrefs
= NULL
; // Target SCPreferencesRef for preferences.plist and NetworkInterfaces.plist
3851 CFURLRef targetPreferencesFile
= NULL
;
3852 char targetPreferencesFileStr
[PATH_MAX
];
3853 CFStringRef targetPreferencesFileString
= NULL
;
3854 SCPreferencesRef targetPrefs
= NULL
;
3855 Boolean isUpgradeScenario
= FALSE
;
3856 CFMutableDictionaryRef validityOptions
= NULL
;
3858 // Check if configuration files exist in sourceDir
3859 if (!__SCNetworkConfigurationMigrateConfigurationFilesPresent(sourceDir
, &sourceConfigurationFiles
, TRUE
)) {
3860 SC_log(LOG_NOTICE
, "sourceDir: (%@) missing configuration files", sourceDir
);
3864 sourcePreferencesFile
= CFArrayGetValueAtIndex(sourceConfigurationFiles
, PREFERENCES_PLIST_INDEX
);
3865 if (!CFURLGetFileSystemRepresentation(sourcePreferencesFile
, TRUE
, (UInt8
*)sourcePreferencesFileStr
, sizeof(sourcePreferencesFileStr
))) {
3866 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", sourcePreferencesFile
);
3870 sourceNetworkInterfaceFile
= CFArrayGetValueAtIndex(sourceConfigurationFiles
, NETWORK_INTERFACES_PLIST_INDEX
);
3871 if (!CFURLGetFileSystemRepresentation(sourceNetworkInterfaceFile
, TRUE
, (UInt8
*)sourceNetworkInterfaceFileStr
, sizeof(sourceNetworkInterfaceFileStr
))) {
3872 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", sourceNetworkInterfaceFile
);
3876 sourcePreferencesFileString
= CFStringCreateWithCString(NULL
, sourcePreferencesFileStr
, kCFStringEncodingUTF8
);
3877 sourceNetworkInterfaceFileString
= CFStringCreateWithCString(NULL
, sourceNetworkInterfaceFileStr
, kCFStringEncodingUTF8
);
3879 sourcePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, sourcePreferencesFileString
);
3880 sourceNetworkInterfacePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, sourceNetworkInterfaceFileString
);
3881 if ((sourcePrefs
== NULL
) || (sourceNetworkInterfacePrefs
== NULL
)) {
3885 targetConfigurationFilesPresent
= __SCNetworkConfigurationMigrateConfigurationFilesPresent(targetDir
, &targetConfigurationFiles
, FALSE
);
3886 if (!targetConfigurationFilesPresent
) {
3887 // if the expected configuration files are not present in the target directory
3888 if (targetConfigurationFiles
== NULL
) {
3889 // but we don't know what files are needed (no target URL)
3890 SC_log(LOG_NOTICE
, "targetConfigurationFiles is NULL");
3895 targetPreferencesFile
= CFArrayGetValueAtIndex(targetConfigurationFiles
, PREFERENCES_PLIST_INDEX
);
3896 if (!CFURLGetFileSystemRepresentation(targetPreferencesFile
, TRUE
, (UInt8
*)targetPreferencesFileStr
, sizeof(targetPreferencesFileStr
))) {
3897 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetPreferencesFile
);
3900 targetNetworkInterfaceFile
= CFArrayGetValueAtIndex(targetConfigurationFiles
, NETWORK_INTERFACES_PLIST_INDEX
);
3901 if (!CFURLGetFileSystemRepresentation(targetNetworkInterfaceFile
, TRUE
, (UInt8
*)targetNetworkInterfaceFileStr
, sizeof(targetNetworkInterfaceFileStr
))) {
3902 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetNetworkInterfaceFile
);
3906 targetPreferencesFileString
= CFStringCreateWithCString(NULL
, targetPreferencesFileStr
, kCFStringEncodingUTF8
);
3907 targetNetworkInterfaceFileString
= CFStringCreateWithCString(NULL
, targetNetworkInterfaceFileStr
, kCFStringEncodingUTF8
);
3909 if (targetConfigurationFilesPresent
) {
3910 targetPrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, targetPreferencesFileString
);
3911 targetNetworkInterfacePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, targetNetworkInterfaceFileString
);
3912 if ((targetPrefs
== NULL
) || (targetNetworkInterfacePrefs
== NULL
)) {
3913 SC_log(LOG_NOTICE
, "Could not open target prefs/ni_prefs");
3917 // create and populate a new preferences.plist
3918 targetPrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, targetPreferencesFileString
);
3919 __SCNetworkPopulateDefaultPrefs(targetPrefs
);
3921 // create and populate a new NetworkInterfaces.plist
3922 targetNetworkInterfacePrefs
= SCPreferencesCreateCompanion(targetPrefs
, INTERFACES_DEFAULT_CONFIG
);
3923 __SCNetworkPopulateDefaultNIPrefs(targetNetworkInterfacePrefs
);
3925 validityOptions
= CFDictionaryCreateMutable(NULL
, 0,
3926 &kCFTypeDictionaryKeyCallBacks
,
3927 &kCFTypeDictionaryValueCallBacks
);
3928 CFDictionaryAddValue(validityOptions
, kSCNetworkConfigurationRepair
, kCFBooleanTrue
);
3930 // Setting Bypass Interface to avoid looking at system interfaces
3931 _SCNetworkConfigurationSetBypassSystemInterfaces(sourcePrefs
, TRUE
);
3932 _SCNetworkConfigurationSetBypassSystemInterfaces(targetPrefs
, TRUE
);
3934 sourceModel
= SCPreferencesGetValue(sourcePrefs
, MODEL
);
3935 targetModel
= SCPreferencesGetValue(targetPrefs
, MODEL
);
3936 isUpgradeScenario
= (isA_CFString(sourceModel
) && isA_CFString(targetModel
) && CFEqual(sourceModel
, targetModel
));
3937 if (isUpgradeScenario
) {
3938 SC_log(LOG_NOTICE
, "Upgrading network configuration");
3940 SC_log(LOG_NOTICE
, "Migrating network configuration");
3945 "\n sourceNetworkInterfacePrefs = %@"
3946 "\n targetPrefs = %@%s"
3947 "\n targetNetworkInterfacePrefs = %@%s",
3949 sourceNetworkInterfacePrefs
,
3951 targetConfigurationFilesPresent
? "" : ", new",
3952 targetNetworkInterfacePrefs
,
3953 targetConfigurationFilesPresent
? "" : ", new");
3955 // create backup of migration source
3956 suffix
= CFStringCreateWithFormat(NULL
, NULL
,
3957 CFSTR("pre-%s-source"),
3958 isUpgradeScenario
? "upgrade" : "migration");
3959 __SCNetworkConfigurationBackup(sourcePrefs
, suffix
, targetPrefs
);
3960 __SCNetworkConfigurationBackup(sourceNetworkInterfacePrefs
, suffix
, targetPrefs
);
3963 // create backup of migration target
3964 suffix
= CFStringCreateWithFormat(NULL
, NULL
,
3965 CFSTR("pre-%s-%starget"),
3966 isUpgradeScenario
? "upgrade" : "migration",
3967 targetConfigurationFilesPresent
? "" : "new-");
3968 __SCNetworkConfigurationBackup(targetPrefs
, suffix
, targetPrefs
);
3969 __SCNetworkConfigurationBackup(targetNetworkInterfacePrefs
, suffix
, targetPrefs
);
3972 // log what we're starting with
3973 __SCNetworkConfigurationReport(LOG_NOTICE
, "Source", sourcePrefs
, sourceNetworkInterfacePrefs
);
3974 __SCNetworkConfigurationReport(LOG_NOTICE
, "Target", targetPrefs
, targetNetworkInterfacePrefs
);
3976 // Create services for builtin interfaces at source if they don't exist
3977 (void)_SCNetworkConfigurationCreateBuiltinInterfaceServices(sourcePrefs
, sourceNetworkInterfacePrefs
);
3978 // Checking validity of the source and destination preferences before continuing
3979 if (!_SCNetworkConfigurationCheckValidityWithPreferences(sourcePrefs
,
3980 sourceNetworkInterfacePrefs
,
3982 SC_log(LOG_NOTICE
, "Source configuration not valid");
3983 goto skipServiceMigration
;
3985 // Only call this function if configuration files were not created by default
3986 if (targetConfigurationFilesPresent
) {
3987 // Create services for builtin interfaces at target if they don't exist
3988 (void)_SCNetworkConfigurationCreateBuiltinInterfaceServices(targetPrefs
, targetNetworkInterfacePrefs
);
3989 if (!_SCNetworkConfigurationCheckValidityWithPreferences(targetPrefs
,
3990 targetNetworkInterfacePrefs
,
3992 SC_log(LOG_NOTICE
, "Target configuration not valid");
3993 goto skipServiceMigration
;
3996 // Upgrade scenario, source and target models match
3997 if (isUpgradeScenario
) {
3998 Boolean foundNewInterfaces
= FALSE
;
4000 // Create SCPreferences to copy the target prefs
4001 SCPreferencesRef upgradeSourcePrefs
= SCPreferencesCreate(NULL
, CFSTR("Upgrade Source Prefs"), NULL
);
4002 SCPreferencesRef upgradeSourceNIPrefs
= SCPreferencesCreate(NULL
, CFSTR("Upgrade Source NI Prefs"), INTERFACES_DEFAULT_CONFIG
);
4004 // Setting Bypass Interface to avoid looking at system interfaces
4005 _SCNetworkConfigurationSetBypassSystemInterfaces(upgradeSourcePrefs
, TRUE
);
4008 "Upgrading network configuration:"
4009 "\n upgradeSourcePrefs [temp] = %@"
4010 "\n upgradeSourceNIPrefs [temp] = %@"
4011 "\n Copying target --> upgrade, source --> target",
4013 upgradeSourceNIPrefs
);
4015 // Content of target prefs
4016 CFDictionaryRef targetPrefsContent
= SCPreferencesPathGetValue(targetPrefs
, CFSTR("/"));
4017 CFDictionaryRef targetNIPrefsContent
= SCPreferencesPathGetValue(targetNetworkInterfacePrefs
, CFSTR("/"));
4019 // Backing up the target prefs into source prefs
4020 SCPreferencesPathSetValue(upgradeSourcePrefs
, CFSTR("/"), targetPrefsContent
);
4021 SCPreferencesPathSetValue(upgradeSourceNIPrefs
, CFSTR("/"), targetNIPrefsContent
);
4023 // Copying content from the source prefs
4024 CFDictionaryRef sourcePrefsContent
= SCPreferencesPathGetValue(sourcePrefs
, CFSTR("/"));
4025 CFDictionaryRef sourceNIPreferencesContent
= SCPreferencesPathGetValue(sourceNetworkInterfacePrefs
, CFSTR("/"));
4027 // Setting the contents of the source prefs into the target prefs
4028 SCPreferencesPathSetValue(targetPrefs
, CFSTR("/"), sourcePrefsContent
);
4029 SCPreferencesPathSetValue(targetNetworkInterfacePrefs
, CFSTR("/"), sourceNIPreferencesContent
);
4031 // Getting the mapping of the non builtin interfaces between source and target
4032 externalMapping
= _SCNetworkConfigurationCopyExternalInterfaceMapping(upgradeSourceNIPrefs
, targetNetworkInterfacePrefs
);
4033 if (externalMapping
!= NULL
) {
4034 logMapping(LOG_INFO
, externalMapping
, "Upgrading, external interface mapping");
4037 newTargetNetworkInterfaceEntities
= _SCNetworkMigrationCreateNetworkInterfaceArray(targetNetworkInterfacePrefs
, externalMapping
, &foundNewInterfaces
);
4038 SC_log(LOG_INFO
, "Upgrading, %s new interfaces", foundNewInterfaces
? "found" : "no");
4040 if (foundNewInterfaces
) {
4041 if (newTargetNetworkInterfaceEntities
== NULL
) {
4042 SC_log(LOG_NOTICE
, "Upgrading, failed w/no new interface list");
4043 CFRelease(upgradeSourcePrefs
);
4044 CFRelease(upgradeSourceNIPrefs
);
4048 // add new interface mapping to NetworkInterfaces.plist
4049 if (!__SCNetworkInterfaceSaveStoredWithPreferences(targetNetworkInterfacePrefs
, newTargetNetworkInterfaceEntities
)) {
4050 SC_log(LOG_NOTICE
, "Upgrading, failed to update NetworkInterfaces.plist");
4051 CFRelease(upgradeSourcePrefs
);
4052 CFRelease(upgradeSourceNIPrefs
);
4056 // create BSD name mapping to facilitate mapping of services
4057 bsdNameMapping
= _SCNetworkMigrationCreateBSDNameMapping(NULL
, externalMapping
);
4059 serviceMapping
= _SCNetworkMigrationCreateServiceMappingUsingBSDNameMapping(upgradeSourcePrefs
, targetPrefs
, bsdNameMapping
);
4061 setMapping
= _SCNetworkMigrationCreateSetMapping(upgradeSourcePrefs
, targetPrefs
);
4063 sourceServiceSetMapping
= _SCNetworkMigrationCreateServiceSetMapping(upgradeSourcePrefs
);
4065 _SCNetworkMigrationDoServiceMigration(upgradeSourcePrefs
,
4070 sourceServiceSetMapping
);
4072 CFRelease(upgradeSourcePrefs
);
4073 CFRelease(upgradeSourceNIPrefs
);
4075 builtinMapping
= _SCNetworkConfigurationCopyBuiltinMapping(sourceNetworkInterfacePrefs
, targetNetworkInterfacePrefs
);
4077 externalMapping
= _SCNetworkConfigurationCopyExternalInterfaceMapping(sourceNetworkInterfacePrefs
, targetNetworkInterfacePrefs
);
4079 newTargetNetworkInterfaceEntities
= _SCNetworkMigrationCreateNetworkInterfaceArray(targetNetworkInterfacePrefs
, externalMapping
, NULL
);
4080 if (newTargetNetworkInterfaceEntities
== NULL
) {
4081 SC_log(LOG_NOTICE
, "Migrating, failed w/no new interface list");
4085 // Write new interface mapping to NetworkInterfaces.plist
4086 if (!__SCNetworkInterfaceSaveStoredWithPreferences(targetNetworkInterfacePrefs
, newTargetNetworkInterfaceEntities
)) {
4087 SC_log(LOG_NOTICE
, "Migrating, failed to update NetworkInterfaces.plist");
4091 // create BSD name mapping to facilitate mapping of services
4092 bsdNameMapping
= _SCNetworkMigrationCreateBSDNameMapping(builtinMapping
, externalMapping
);
4094 serviceMapping
= _SCNetworkMigrationCreateServiceMappingUsingBSDNameMapping(sourcePrefs
, targetPrefs
, bsdNameMapping
);
4095 if (serviceMapping
== NULL
) {
4099 setMapping
= _SCNetworkMigrationCreateSetMapping(sourcePrefs
, targetPrefs
);
4101 sourceServiceSetMapping
= _SCNetworkMigrationCreateServiceSetMapping(sourcePrefs
);
4103 if (!_SCNetworkMigrationDoServiceMigration(sourcePrefs
,
4108 sourceServiceSetMapping
)) {
4109 SC_log(LOG_NOTICE
, "SCNetworkMigrationDoServiceMigration(): service migration failed");
4112 #if !TARGET_OS_IPHONE
4113 // Migrating Virtual Network Interfaces
4114 if (!_SCNetworkMigrationDoVirtualNetworkInterfaceMigration(sourcePrefs
, sourceNetworkInterfacePrefs
,
4115 targetPrefs
, targetNetworkInterfacePrefs
,
4116 bsdNameMapping
, setMapping
, sourceServiceSetMapping
)) {
4117 SC_log(LOG_NOTICE
, "SCNetworkMigrationDoServiceMigration(): virtual interface migration failed");
4119 #endif // !TARGET_OS_IPHONE
4120 // Migrate Service Order
4121 if (!_SCNetworkMigrationDoServiceOrderMigration(sourcePrefs
, targetPrefs
, setMapping
)) {
4122 SC_log(LOG_NOTICE
, "SCNetworkMigrationDoServiceMigration(): service order migration failed");
4126 skipServiceMigration
:
4127 // Migrating System Information
4128 if (!isUpgradeScenario
) {
4129 if (!_SCNetworkMigrationDoSystemMigration(sourcePrefs
, targetPrefs
)) {
4130 SC_log(LOG_NOTICE
, "SCNetworkMigrationDoServiceMigration(): system setting migration failed");
4133 CFDictionaryAddValue(validityOptions
, kSCNetworkConfigurationRepairModel
, kCFBooleanTrue
);
4134 if (!_SCNetworkConfigurationCheckValidityWithPreferences(targetPrefs
, targetNetworkInterfacePrefs
, validityOptions
)) {
4136 "%s configuration not valid",
4137 isUpgradeScenario
? "Upgraded" : "Migrated");
4140 if (!SCPreferencesCommitChanges(targetPrefs
)) {
4141 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges(target preferences.plist) failed: %s", SCErrorString(SCError()));
4145 if (!SCPreferencesCommitChanges(targetNetworkInterfacePrefs
)) {
4146 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges(target NetworkInterfaces.plist) failed: %s", SCErrorString(SCError()));
4149 migrationSuccess
= TRUE
;
4151 __SCNetworkConfigurationReport(LOG_NOTICE
, isUpgradeScenario
? "Upgraded" : "Migrated", targetPrefs
, targetNetworkInterfacePrefs
);
4154 if (setMapping
!= NULL
) {
4155 CFRelease(setMapping
);
4157 if (sourceServiceSetMapping
!= NULL
) {
4158 CFRelease(sourceServiceSetMapping
);
4160 if (sourceConfigurationFiles
!= NULL
) {
4161 CFRelease(sourceConfigurationFiles
);
4163 if (targetConfigurationFiles
!= NULL
) {
4164 CFRelease(targetConfigurationFiles
);
4166 if (sourcePreferencesFileString
!= NULL
) {
4167 CFRelease(sourcePreferencesFileString
);
4169 if (sourceNetworkInterfaceFileString
!= NULL
) {
4170 CFRelease(sourceNetworkInterfaceFileString
);
4172 if (targetPreferencesFileString
!= NULL
) {
4173 CFRelease(targetPreferencesFileString
);
4175 if (targetNetworkInterfaceFileString
!= NULL
) {
4176 CFRelease(targetNetworkInterfaceFileString
);
4178 if (newTargetNetworkInterfaceEntities
!= NULL
) {
4179 CFRelease(newTargetNetworkInterfaceEntities
);
4181 if (builtinMapping
!= NULL
) {
4182 CFRelease(builtinMapping
);
4184 if (externalMapping
!= NULL
) {
4185 CFRelease(externalMapping
);
4187 if (bsdNameMapping
!= NULL
) {
4188 CFRelease(bsdNameMapping
);
4190 if (serviceMapping
!= NULL
) {
4191 CFRelease(serviceMapping
);
4193 if (targetPrefs
!= NULL
) {
4194 CFRelease(targetPrefs
);
4196 if (sourcePrefs
!= NULL
) {
4197 CFRelease(sourcePrefs
);
4199 if (sourceNetworkInterfacePrefs
!= NULL
) {
4200 CFRelease(sourceNetworkInterfacePrefs
);
4202 if (targetNetworkInterfacePrefs
!= NULL
) {
4203 CFRelease(targetNetworkInterfacePrefs
);
4205 if (validityOptions
!= NULL
) {
4206 CFRelease(validityOptions
);
4208 return migrationSuccess
;
4214 _SCNetworkMigrationAreServicesIdentical(SCPreferencesRef configPref
, SCPreferencesRef expectedConfigPref
)
4216 const void * expected_vals_q
[N_QUICK
];
4217 const void ** expected_vals
= expected_vals_q
;
4218 CFMutableArrayRef expectedServiceArray
= NULL
;
4219 CFIndex expectedServiceArrayCount
= 0;
4220 CFDictionaryRef expectedServiceDict
= NULL
;
4221 size_t expectedServiceDictCount
= 0;
4222 CFDictionaryRef expectedServiceEntity
= 0;
4223 Boolean foundMatch
= FALSE
;
4224 CFMutableArrayRef serviceArray
= NULL
;
4225 CFIndex serviceArrayCount
= 0;
4226 CFDictionaryRef serviceDict
= NULL
;
4227 size_t serviceDictCount
= 0;
4228 CFDictionaryRef serviceEntity
= NULL
;
4229 Boolean success
= FALSE
;
4230 const void * vals_q
[N_QUICK
];
4231 const void ** vals
= vals_q
;
4233 serviceDict
= SCPreferencesGetValue(configPref
, kSCPrefNetworkServices
);
4234 if (!isA_CFDictionary(serviceDict
)) {
4237 serviceDictCount
= CFDictionaryGetCount(serviceDict
);
4239 expectedServiceDict
= SCPreferencesGetValue(expectedConfigPref
, kSCPrefNetworkServices
);
4240 if (!isA_CFDictionary(expectedServiceDict
)) {
4243 expectedServiceDictCount
= CFDictionaryGetCount(expectedServiceDict
);
4245 if (serviceDictCount
!= expectedServiceDictCount
) {
4249 if (serviceDictCount
> (sizeof(vals_q
) / sizeof(CFTypeRef
))) {
4250 vals
= CFAllocatorAllocate(NULL
, serviceDictCount
* sizeof(CFPropertyListRef
), 0);
4253 CFDictionaryGetKeysAndValues(serviceDict
, NULL
, vals
);
4254 serviceArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
4256 for (size_t idx
=0; idx
< serviceDictCount
; idx
++) {
4257 serviceEntity
= vals
[idx
];
4258 if (!isA_CFDictionary(serviceEntity
)) {
4261 CFArrayAppendValue(serviceArray
, serviceEntity
);
4264 serviceArrayCount
= CFArrayGetCount(serviceArray
);
4266 if (expectedServiceDictCount
> (sizeof(expected_vals_q
) / sizeof(CFTypeRef
))) {
4267 expected_vals
= CFAllocatorAllocate(NULL
, expectedServiceDictCount
, 0);
4270 CFDictionaryGetKeysAndValues(expectedServiceDict
, NULL
, expected_vals
);
4271 expectedServiceArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
4273 for (size_t idx
= 0; idx
< expectedServiceDictCount
; idx
++) {
4274 serviceEntity
= expected_vals
[idx
];
4275 if (!isA_CFDictionary(serviceEntity
)) {
4278 CFArrayAppendValue(expectedServiceArray
, serviceEntity
);
4280 expectedServiceArrayCount
= CFArrayGetCount(expectedServiceArray
);
4282 if (serviceArrayCount
!= expectedServiceArrayCount
) {
4286 for (CFIndex idx
= 0; idx
< expectedServiceArrayCount
; idx
++) {
4288 expectedServiceEntity
= CFArrayGetValueAtIndex(expectedServiceArray
, idx
);
4289 serviceArrayCount
= CFArrayGetCount(serviceArray
);
4291 for (CFIndex idx2
= 0; idx2
< serviceArrayCount
; idx2
++) {
4292 serviceEntity
= CFArrayGetValueAtIndex(serviceArray
, idx2
);
4294 if (CFEqual(expectedServiceEntity
, serviceEntity
)) {
4305 success
= foundMatch
;
4307 if (vals
!= vals_q
) {
4308 CFAllocatorDeallocate(NULL
, vals
);
4310 if (expected_vals
!= expected_vals_q
) {
4311 CFAllocatorDeallocate(NULL
, expected_vals
);
4317 _SCNetworkMigrationAreNetworkInterfaceConfigurationsIdentical (SCPreferencesRef configNetworkInterfacePref
, SCPreferencesRef expectedNetworkInterfacePref
)
4319 CFDictionaryRef expectedInterfaceEntity
= NULL
;
4320 CFArrayRef expectedInterfaceList
= NULL
;
4321 CFIndex expectedInterfaceListCount
;
4322 Boolean foundMatch
= FALSE
;
4323 CFDictionaryRef interfaceEntity
= NULL
;
4324 CFArrayRef interfaceList
= NULL
;
4325 CFIndex interfaceListCount
;
4326 CFMutableArrayRef interfaceListMutable
= NULL
;
4327 Boolean success
= FALSE
;
4329 interfaceList
= SCPreferencesGetValue(configNetworkInterfacePref
, INTERFACES
);
4330 if (!isA_CFArray(interfaceList
)) {
4333 interfaceListMutable
= CFArrayCreateMutableCopy(NULL
, 0, interfaceList
);
4334 interfaceListCount
= CFArrayGetCount(interfaceListMutable
);
4336 expectedInterfaceList
= SCPreferencesGetValue(expectedNetworkInterfacePref
, INTERFACES
);
4337 if (!isA_CFArray(expectedInterfaceList
)) {
4340 expectedInterfaceListCount
= CFArrayGetCount(expectedInterfaceList
);
4342 if (interfaceListCount
!= expectedInterfaceListCount
) {
4346 for (CFIndex idx
= 0; idx
< expectedInterfaceListCount
; idx
++) {
4348 expectedInterfaceEntity
= CFArrayGetValueAtIndex(expectedInterfaceList
, idx
);
4349 interfaceListCount
= CFArrayGetCount(interfaceListMutable
);
4351 for (CFIndex idx2
= 0; idx2
< interfaceListCount
; idx2
++) {
4352 interfaceEntity
= CFArrayGetValueAtIndex(interfaceList
, idx2
);
4353 if (CFEqual(expectedInterfaceEntity
, interfaceEntity
)) {
4362 success
= foundMatch
;
4365 if (interfaceListMutable
!= NULL
) {
4366 CFRelease(interfaceListMutable
);
4373 _SCNetworkMigrationAreConfigurationsIdentical (CFURLRef configurationURL
,
4374 CFURLRef expectedConfigurationURL
)
4376 CFURLRef baseConfigURL
= NULL
;
4377 CFURLRef baseExpectedConfigURL
= NULL
;
4378 CFURLRef configPreferencesURL
= NULL
;
4379 CFURLRef configNetworkInterfacesURL
= NULL
;
4380 SCPreferencesRef configPref
= NULL
;
4381 SCPreferencesRef configNetworkInterfacePref
= NULL
;
4382 SCPreferencesRef expectedConfigPref
= NULL
;
4383 SCPreferencesRef expectedNetworkInterfacePref
= NULL
;
4384 CFURLRef expectedNetworkInterfaceURL
= NULL
;
4385 CFURLRef expectedPreferencesURL
= NULL
;
4386 Boolean isIdentical
= FALSE
;
4387 CFStringRef networkInterfaceConfigString
= NULL
;
4388 CFStringRef networkInterfaceExpectedString
= NULL
;
4389 CFStringRef prefsConfigString
= NULL
;
4390 CFStringRef prefsExpectedString
= NULL
;
4391 char networkInterfaceConfigStr
[PATH_MAX
];
4392 char networkInterfaceExpectedStr
[PATH_MAX
];
4393 char prefsConfigStr
[PATH_MAX
];
4394 char prefsExpectedStr
[PATH_MAX
];
4396 if (configurationURL
== NULL
||
4397 expectedConfigurationURL
== NULL
) {
4400 baseConfigURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
4401 PREFS_DEFAULT_DIR_RELATIVE
,
4402 kCFURLPOSIXPathStyle
,
4405 configPreferencesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
4406 (const UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
,
4407 sizeof(PREFS_DEFAULT_CONFIG_PLIST
),
4410 if (!CFURLResourceIsReachable(configPreferencesURL
, NULL
)) {
4411 SC_log(LOG_NOTICE
, "No preferences.plist file: %@", configPreferencesURL
);
4415 configNetworkInterfacesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
4416 (const UInt8
*)INTERFACES_DEFAULT_CONFIG_PLIST
,
4417 sizeof(INTERFACES_DEFAULT_CONFIG_PLIST
),
4420 if (!CFURLResourceIsReachable(configNetworkInterfacesURL
, NULL
)) {
4421 SC_log(LOG_NOTICE
, "No NetworkInterfaces.plist file: %@", configNetworkInterfacesURL
);
4425 if (!CFURLGetFileSystemRepresentation(configPreferencesURL
, TRUE
, (UInt8
*)prefsConfigStr
, sizeof(prefsConfigStr
))) {
4426 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPreferencesURL
);
4429 if (!CFURLGetFileSystemRepresentation(configNetworkInterfacesURL
, TRUE
, (UInt8
*)networkInterfaceConfigStr
, sizeof(networkInterfaceConfigStr
))) {
4430 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfacesURL
);
4434 baseExpectedConfigURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
,
4435 PREFS_DEFAULT_DIR_RELATIVE
,
4436 kCFURLPOSIXPathStyle
,
4438 expectedConfigurationURL
);
4439 expectedPreferencesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
4440 (const UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
,
4441 sizeof(PREFS_DEFAULT_CONFIG_PLIST
),
4443 baseExpectedConfigURL
);
4445 if (!CFURLResourceIsReachable(expectedPreferencesURL
, NULL
)) {
4446 SC_log(LOG_NOTICE
, "No expected preferences.plist file");
4450 expectedNetworkInterfaceURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
4451 (const UInt8
*)INTERFACES_DEFAULT_CONFIG_PLIST
,
4452 sizeof(INTERFACES_DEFAULT_CONFIG_PLIST
),
4454 baseExpectedConfigURL
);
4456 if (!CFURLResourceIsReachable(expectedNetworkInterfaceURL
, NULL
)) {
4457 SC_log(LOG_NOTICE
, "No expected NetworkInterfaces.plist file");
4461 if (!CFURLGetFileSystemRepresentation(expectedPreferencesURL
, TRUE
, (UInt8
*)prefsExpectedStr
, sizeof(prefsExpectedStr
))) {
4462 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", expectedPreferencesURL
);
4465 if (!CFURLGetFileSystemRepresentation(expectedNetworkInterfaceURL
, TRUE
, (UInt8
*)networkInterfaceExpectedStr
, sizeof(networkInterfaceExpectedStr
))) {
4466 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", expectedNetworkInterfaceURL
);
4470 prefsConfigString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsConfigStr
);
4471 networkInterfaceConfigString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceConfigStr
);
4472 prefsExpectedString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsExpectedStr
);
4473 networkInterfaceExpectedString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceExpectedStr
);
4475 configPref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsConfigString
);
4476 expectedConfigPref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsExpectedString
);
4477 configNetworkInterfacePref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, networkInterfaceConfigString
);
4478 expectedNetworkInterfacePref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, networkInterfaceExpectedString
);
4480 if (configPref
== NULL
||
4481 expectedConfigPref
== NULL
||
4482 configNetworkInterfacePref
== NULL
||
4483 expectedNetworkInterfacePref
== NULL
) {
4484 SC_log(LOG_NOTICE
, "One of the preferences is NULL");
4485 isIdentical
= FALSE
;
4487 isIdentical
= (_SCNetworkMigrationAreServicesIdentical(configPref
, expectedConfigPref
) &&
4488 _SCNetworkMigrationAreNetworkInterfaceConfigurationsIdentical(configNetworkInterfacePref
, expectedNetworkInterfacePref
));
4490 if (baseConfigURL
!= NULL
) {
4491 CFRelease(baseConfigURL
);
4493 if (configPreferencesURL
!= NULL
) {
4494 CFRelease(configPreferencesURL
);
4496 if (configNetworkInterfacesURL
!= NULL
) {
4497 CFRelease(configNetworkInterfacesURL
);
4499 if (baseExpectedConfigURL
!= NULL
) {
4500 CFRelease(baseExpectedConfigURL
);
4502 if (expectedPreferencesURL
!= NULL
) {
4503 CFRelease(expectedPreferencesURL
);
4505 if (expectedNetworkInterfaceURL
!= NULL
) {
4506 CFRelease(expectedNetworkInterfaceURL
);
4508 if (prefsConfigString
!= NULL
) {
4509 CFRelease(prefsConfigString
);
4511 if (networkInterfaceConfigString
!= NULL
) {
4512 CFRelease(networkInterfaceConfigString
);
4514 if (prefsExpectedString
!= NULL
) {
4515 CFRelease(prefsExpectedString
);
4517 if (networkInterfaceExpectedString
!= NULL
) {
4518 CFRelease(networkInterfaceExpectedString
);
4520 if (configPref
!= NULL
) {
4521 CFRelease(configPref
);
4523 if (expectedConfigPref
!= NULL
) {
4524 CFRelease(expectedConfigPref
);
4526 if (configNetworkInterfacePref
!= NULL
) {
4527 CFRelease(configNetworkInterfacePref
);
4529 if (expectedNetworkInterfacePref
!= NULL
) {
4530 CFRelease(expectedNetworkInterfacePref
);
4536 _SCNetworkConfigurationCopyMigrationRemovePaths (CFArrayRef targetPaths
,
4539 CFURLRef affectedURL
;
4540 char filePath
[PATH_MAX
];
4541 CFURLRef targetFile
;
4542 CFMutableArrayRef toBeRemoved
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
4544 for (CFIndex idx
= 0; idx
< CFArrayGetCount(targetPaths
); idx
++) {
4545 affectedURL
= CFArrayGetValueAtIndex(targetPaths
, idx
);
4547 if (!CFURLGetFileSystemRepresentation(affectedURL
, TRUE
, (UInt8
*)filePath
, sizeof(filePath
))) {
4548 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", affectedURL
);
4551 targetFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
4552 (const UInt8
*)filePath
,
4553 strnlen(filePath
, sizeof(filePath
)),
4557 if (!CFURLResourceIsReachable(targetFile
, NULL
)) {
4558 CFArrayAppendValue(toBeRemoved
, affectedURL
);
4560 CFRelease(targetFile
);
4562 // If number of files to be removed is 0, return NULL
4563 if (CFArrayGetCount(toBeRemoved
) == 0) {
4564 CFRelease(toBeRemoved
);