2 * Copyright (c) 2014-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 // 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>
39 #include <sys/param.h>
42 #define BACK_TO_MY_MAC CFSTR("BackToMyMac")
43 #define BACK_TO_MY_MAC_DSIDS CFSTR("BackToMyMacDSIDs")
44 #define PREFS_DEFAULT_DIR_PLIST "/Library/Preferences/SystemConfiguration"
45 #define PREFS_DEFAULT_DIR_RELATIVE CFSTR("Library/Preferences/SystemConfiguration/")
46 #define PREFS_DEFAULT_CONFIG_PLIST "preferences.plist"
47 #define NETWORK_INTERFACES_PREFS_PLIST "NetworkInterfaces.plist"
48 #define NUM_MIGRATION_PATHS 2
49 #define PLUGIN_ID CFSTR("System Migration")
50 #define PREFERENCES_PLIST_INDEX 0
51 #define NETWORK_INTERFACES_PLIST_INDEX 1
53 #define kProtocolType CFSTR("Protocol Type")
54 #define kProtocolConfiguration CFSTR("Protocol Configuration")
55 #define kProtocolEnabled CFSTR("Protocol Enabled")
58 const CFStringRef kSCNetworkConfigurationMigrationActionKey
= CFSTR("MigrationActionKey");
59 const CFStringRef kSCNetworkConfigurationRepair
= CFSTR("ConfigurationRepair");
61 static CFDictionaryRef
62 _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(SCPreferencesRef prefs
);
64 static CFDictionaryRef
65 _SCNetworkMigrationCopyMappingBSDNameToBondServices(SCPreferencesRef prefs
);
67 static CFDictionaryRef
68 _SCNetworkMigrationCopyMappingBSDNameToVLANServices(SCPreferencesRef prefs
);
71 _SCNetworkConfigurationIsInterfaceNamerMappable(SCNetworkInterfaceRef interface1
, SCNetworkInterfaceRef interface2
, Boolean bypassActive
);
74 _SCNetworkConfigurationMigrateConfiguration(CFURLRef sourceDir
, CFURLRef targetDir
);
77 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(CFURLRef baseURL
, CFURLRef
*prefs
, CFURLRef
*interfaces
)
79 if (baseURL
!= NULL
) {
82 baseURL
= CFURLCreateFromFileSystemRepresentation(NULL
,
83 (UInt8
*)PREFS_DEFAULT_DIR_PLIST
,
84 sizeof(PREFS_DEFAULT_DIR_PLIST
),
88 *prefs
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
89 (UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
,
90 sizeof(PREFS_DEFAULT_CONFIG_PLIST
) - 1,
94 *interfaces
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
,
95 (UInt8
*)NETWORK_INTERFACES_PREFS_PLIST
,
96 sizeof(NETWORK_INTERFACES_PREFS_PLIST
) - 1,
104 _SCNetworkConfigurationCopyMigrationPaths(CFDictionaryRef options
)
107 CFMutableArrayRef migrationPaths
= NULL
;
110 if (_SC_isInstallEnvironment()) {
113 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(NULL
, &prefs
, &interfaces
);
115 migrationPaths
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
116 CFArrayInsertValueAtIndex(migrationPaths
, PREFERENCES_PLIST_INDEX
, prefs
);
117 CFArrayInsertValueAtIndex(migrationPaths
, NETWORK_INTERFACES_PLIST_INDEX
, interfaces
);
120 CFRelease(interfaces
);
121 return migrationPaths
;
125 _SCNetworkConfigurationRemoveConfigurationFiles(CFURLRef configDir
)
128 char configPathString
[PATH_MAX
];
129 CFURLRef configPathURL
= NULL
;
130 char configNetworkInterfacesPathString
[PATH_MAX
];
131 CFURLRef configNetworkInterfacesPathURL
= NULL
;
133 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(configDir
, &configPathURL
, &configNetworkInterfacesPathURL
);
135 if(!CFURLGetFileSystemRepresentation(configPathURL
,
137 (UInt8
*)configPathString
,
138 sizeof(configPathString
))) {
139 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPathURL
);
141 if (remove(configPathString
) != 0) {
142 SC_log(LOG_INFO
, "remove(\"%s\") failed: %s", configPathString
, strerror(errno
));
145 CFRelease(configPathURL
);
147 if(!CFURLGetFileSystemRepresentation(configNetworkInterfacesPathURL
,
149 (UInt8
*)configNetworkInterfacesPathString
,
150 sizeof(configNetworkInterfacesPathString
))) {
151 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfacesPathURL
);
153 if (remove(configNetworkInterfacesPathString
) != 0) {
154 SC_log(LOG_INFO
, "remove(\"%s\") failed: %s", configNetworkInterfacesPathString
, strerror(errno
));
157 CFRelease(configNetworkInterfacesPathURL
);
163 SCNetworkConfigurationCopyConfigurationFiles(CFURLRef configDir
,
164 CFURLRef targetDir
) // TargetDir needs to exist
167 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
168 char networkInterfacesPathString
[PATH_MAX
];
169 CFURLRef networkInterfacesPathURL
= NULL
;
170 copyfile_state_t networkInterfacesState
;
171 char preferencesPathString
[PATH_MAX
];
172 CFURLRef preferencesPathURL
= NULL
;
173 Boolean removeTargetFiles
= FALSE
;
174 copyfile_state_t state
;
175 Boolean success
= FALSE
;
176 char targetNetworkInterfacesPathString
[PATH_MAX
];
177 CFURLRef targetNetworkInterfacesPathURL
= NULL
;
178 char targetPathString
[PATH_MAX
];
179 CFURLRef targetPathURL
= NULL
;
181 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(targetDir
, &targetPathURL
, &targetNetworkInterfacesPathURL
);
183 if (!CFURLGetFileSystemRepresentation(targetPathURL
,
185 (UInt8
*)targetPathString
,
186 sizeof(targetPathString
))) {
187 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetPathURL
);
190 if (!CFURLGetFileSystemRepresentation(targetNetworkInterfacesPathURL
,
192 (UInt8
*)targetNetworkInterfacesPathString
,
193 sizeof(targetNetworkInterfacesPathString
))) {
194 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetNetworkInterfacesPathURL
);
198 if (configDir
== NULL
) {
199 removeTargetFiles
= TRUE
;
204 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(configDir
, &preferencesPathURL
, &networkInterfacesPathURL
);
206 if (!CFURLGetFileSystemRepresentation(preferencesPathURL
,
208 (UInt8
*)preferencesPathString
,
209 sizeof(preferencesPathString
))) {
210 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", preferencesPathURL
);
213 if (!CFURLGetFileSystemRepresentation(networkInterfacesPathURL
,
215 (UInt8
*)networkInterfacesPathString
,
216 sizeof(networkInterfacesPathString
))) {
217 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", networkInterfacesPathURL
);
221 state
= copyfile_state_alloc();
222 if ((error
= copyfile(preferencesPathString
, targetPathString
, state
, COPYFILE_ALL
)) != 0) {
223 SC_log(LOG_NOTICE
, "copyFile(\"%s\", \"%s\", ...) failed: %s",
224 preferencesPathString
,
227 copyfile_state_free(state
);
228 removeTargetFiles
= TRUE
;
231 copyfile_state_free(state
);
232 (void)chmod(targetPathString
, mode
);
234 networkInterfacesState
= copyfile_state_alloc();
235 if ((error
= copyfile(networkInterfacesPathString
, targetNetworkInterfacesPathString
, networkInterfacesState
, COPYFILE_ALL
)) != 0) {
236 SC_log(LOG_NOTICE
, "copyFile(\"%s\", \"%s\", ...) failed: %s",
237 networkInterfacesPathString
,
238 targetNetworkInterfacesPathString
,
240 copyfile_state_free(networkInterfacesState
);
241 removeTargetFiles
= TRUE
;
244 copyfile_state_free(networkInterfacesState
);
245 (void)chmod(targetNetworkInterfacesPathString
, mode
);
249 if (removeTargetFiles
) {
250 _SCNetworkConfigurationRemoveConfigurationFiles(targetDir
);
252 if (preferencesPathURL
!= NULL
) {
253 CFRelease(preferencesPathURL
);
255 if (networkInterfacesPathURL
!= NULL
) {
256 CFRelease(networkInterfacesPathURL
);
258 if (targetPathURL
!= NULL
) {
259 CFRelease(targetPathURL
);
261 if (targetNetworkInterfacesPathURL
!= NULL
) {
262 CFRelease(targetNetworkInterfacesPathURL
);
268 /* -----------------------------------------------------------------------------
269 Create directories and intermediate directories as required.
270 ----------------------------------------------------------------------------- */
272 _SCNetworkConfigurationMakePathIfNeeded(CFURLRef pathURL
)
277 char thepath
[PATH_MAX
];
280 Boolean success
= FALSE
;
282 if (!CFURLGetFileSystemRepresentation(pathURL
, TRUE
, (UInt8
*)path
, sizeof(path
))) {
283 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", pathURL
);
286 SC_log(LOG_INFO
, "creating path: %s", path
);
288 newmask
= S_IRWXU
| S_IRGRP
| S_IROTH
| S_IXGRP
| S_IXOTH
;
292 strlcpy(thepath
, path
, slen
+1);
296 for(; !success
; c
++){
297 if ((*c
== '/') || (*c
== '\0')){
302 if (mkdir(thepath
, newmask
)){
303 if (errno
== EEXIST
|| errno
== EISDIR
){
304 if (stat(thepath
, &sb
) < 0){
305 SC_log(LOG_ERR
, "stat returned value < 0");
309 SC_log(LOG_ERR
, "received error: %s", strerror(errno
));
319 static SCPreferencesRef
320 __SCNetworkCreateDefaultPref(CFStringRef prefsID
)
322 SCPreferencesRef prefs
;
323 SCNetworkSetRef currentSet
;
326 prefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsID
);
331 currentSet
= SCNetworkSetCopyCurrent(prefs
);
332 if (currentSet
== NULL
) {
333 currentSet
= _SCNetworkSetCreateDefault(prefs
);
335 SCNetworkSetEstablishDefaultConfiguration(currentSet
);
336 CFRelease(currentSet
);
338 model
= SCPreferencesGetValue(prefs
, MODEL
);
340 model
= _SC_hw_model(FALSE
);
341 SCPreferencesSetValue(prefs
, MODEL
, model
);
349 __SCNetworkCreateDefaultNIPrefs(CFStringRef prefsID
)
351 CFMutableArrayRef interfaces
= NULL
;
353 CFArrayRef networkInterfaces
;
354 SCPreferencesRef ni_prefs
;
355 CFComparisonResult res
;
358 networkInterfaces
= __SCNetworkInterfaceCopyAll_IONetworkInterface();
360 if (networkInterfaces
== NULL
) {
361 SC_log(LOG_NOTICE
, "networkInterfaces is NULL");
364 if (prefsID
== NULL
) {
365 prefsID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@/%@"), PREFS_DEFAULT_DIR
, NETWORK_INTERFACES_PREFS
);
371 ni_prefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsID
);
374 if (ni_prefs
== NULL
) {
375 SC_log(LOG_NOTICE
, "ni_prefs is NULL");
379 interfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
381 for (CFIndex idx
= 0; idx
< CFArrayGetCount(networkInterfaces
); idx
++) {
385 SCNetworkInterfaceRef interface
= CFArrayGetValueAtIndex(networkInterfaces
, idx
);
386 CFDictionaryRef interfaceEntity
= __SCNetworkInterfaceCopyStorageEntity(interface
);
388 if (interfaceEntity
== NULL
) {
392 if_type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
393 if_unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
395 if ((if_type
== NULL
) || (if_unit
== NULL
)) {
396 CFRelease(interfaceEntity
);
400 for (idx2
= 0; idx2
< CFArrayGetCount(interfaces
); idx2
++) {
403 CFDictionaryRef dict
= CFArrayGetValueAtIndex(interfaces
, idx2
);
405 db_type
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceType
));
406 db_unit
= CFDictionaryGetValue(dict
, CFSTR(kIOInterfaceUnit
));
407 res
= CFNumberCompare(if_type
, db_type
, NULL
);
408 if (res
== kCFCompareLessThan
409 || (res
== kCFCompareEqualTo
410 && (CFNumberCompare(if_unit
, db_unit
, NULL
) == kCFCompareLessThan
))) {
415 CFArrayInsertValueAtIndex(interfaces
, idx2
, interfaceEntity
);
416 CFRelease(interfaceEntity
);
419 SCPreferencesSetValue(ni_prefs
, INTERFACES
, interfaces
);
421 model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
423 model
= _SC_hw_model(FALSE
);
424 SCPreferencesSetValue(ni_prefs
, MODEL
, model
);
427 if (interfaces
!= NULL
) {
428 CFRelease(interfaces
);
430 if (networkInterfaces
!= NULL
) {
431 CFRelease(networkInterfaces
);
439 * _SCNetworkConfigurationPerformMigration will migrate configuration between source and destination systems
443 _SCNetworkConfigurationPerformMigration(CFURLRef sourceDir
, CFURLRef currentDir
, CFURLRef targetDir
, CFDictionaryRef options
)
445 CFURLRef currentDirConfig
= NULL
;
446 CFURLRef currentSystemPath
= NULL
;
447 Boolean migrationComplete
= FALSE
;
448 CFArrayRef paths
= NULL
;
449 Boolean removeTargetOnFailure
= FALSE
;
450 CFURLRef sourceDirConfig
= NULL
;
451 CFURLRef targetDirConfig
= NULL
;
453 if (_SC_isInstallEnvironment()) {
457 // Both sourceDir and currentDir cannot be NULL because NULL value indicates using current system
458 if (sourceDir
== NULL
&& currentDir
== NULL
) {
459 SC_log(LOG_INFO
, "Both sourceDir and currentDir are NULL");
463 currentSystemPath
= CFURLCreateWithString(NULL
,
467 // if either of the sourceDir or currentDir are NULL, then populate it with current system path
468 if (sourceDir
== NULL
) {
469 sourceDirConfig
= CFRetain(currentSystemPath
);
472 sourceDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
, kCFURLPOSIXPathStyle
, TRUE
, sourceDir
);
475 if (currentDir
!= NULL
) {
476 currentDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
, kCFURLPOSIXPathStyle
, TRUE
, currentDir
);
478 // If the targetDir is not provided then migration will take place in currentDir
479 if (targetDir
== NULL
) {
480 targetDirConfig
= CFRetain(currentSystemPath
);
483 targetDirConfig
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
, kCFURLPOSIXPathStyle
, TRUE
, targetDir
);
485 // Source directory cannot be the same as Target Directory
486 if (CFEqual(sourceDirConfig
, targetDirConfig
)) {
487 SC_log(LOG_INFO
, "Source directory cannot be the same as target directory");
491 if ((currentDirConfig
== NULL
) || !CFEqual(currentDirConfig
, targetDirConfig
)) {
492 if (!_SCNetworkConfigurationMakePathIfNeeded(targetDirConfig
)) {
493 SC_log(LOG_INFO
, "Could not create target directory");
497 if (!SCNetworkConfigurationCopyConfigurationFiles(currentDirConfig
, targetDirConfig
)) {
498 SC_log(LOG_INFO
, "Could not copy configuration files from \"%@\" to \"%@\"",
502 else if (currentDirConfig
!= NULL
) {
503 removeTargetOnFailure
= TRUE
; // Configuration files were copied over to target directory
504 // If migration failed, then we should remove those configuration
505 // files since current directory and target directory are not
510 // If both source and current configurations point to current system, then no migration needs to be done.
511 if ((currentDirConfig
!= NULL
) && CFEqual(sourceDirConfig
, currentDirConfig
)) {
512 SC_log(LOG_INFO
, "No migration needed, source and current configurations point to same path");
513 migrationComplete
= TRUE
;
516 migrationComplete
= _SCNetworkConfigurationMigrateConfiguration(sourceDirConfig
, targetDirConfig
);
518 SC_log(LOG_NOTICE
, "Migration %s", migrationComplete
? "complete" : "failed");
519 if (migrationComplete
) {
520 paths
= _SCNetworkConfigurationCopyMigrationPaths(NULL
);
523 // If migration fails, then remove configuration files from target config if they are
524 // copied from the current directory
525 if (removeTargetOnFailure
) {
526 _SCNetworkConfigurationRemoveConfigurationFiles(targetDirConfig
);
530 if (currentDirConfig
!= NULL
) {
531 CFRelease(currentDirConfig
);
533 if (currentSystemPath
!= NULL
) {
534 CFRelease(currentSystemPath
);
536 if (sourceDirConfig
!= NULL
) {
537 CFRelease(sourceDirConfig
);
539 if (targetDirConfig
!= NULL
) {
540 CFRelease(targetDirConfig
);
546 _SCNetworkConfigurationMigrateIsFilePresent(CFURLRef filePath
)
548 Boolean fileExists
= false;
549 char filePathStr
[PATH_MAX
];
551 struct stat statStruct
= {0, };
553 if (filePath
== NULL
) {
554 SC_log(LOG_DEBUG
, "filePath is NULL");
558 if (!CFURLGetFileSystemRepresentation(filePath
, TRUE
, (UInt8
*) filePathStr
, sizeof(filePathStr
))) {
559 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", filePath
);
563 statResult
= stat(filePathStr
, &statStruct
);
564 if (statResult
== 0) {
572 __SCNetworkConfigurationMigrateConfigurationFilesPresent(CFURLRef baseURL
, CFArrayRef
* migrationPaths
)
574 Boolean configFilesPresent
= FALSE
;
576 CFURLRef filePath
= NULL
;
578 CFMutableArrayRef migrationPathsMutable
= NULL
;
581 if (baseURL
== NULL
) {
582 SC_log(LOG_INFO
, "No base migration URL");
586 _SCNetworkConfigurationCopyMigrationPathsWithBaseURL(baseURL
, &prefs
, &interfaces
);
588 migrationPathsMutable
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
589 CFArrayInsertValueAtIndex(migrationPathsMutable
, PREFERENCES_PLIST_INDEX
, prefs
);
590 CFArrayInsertValueAtIndex(migrationPathsMutable
, NETWORK_INTERFACES_PLIST_INDEX
, interfaces
);
593 CFRelease(interfaces
);
595 *migrationPaths
= migrationPathsMutable
;
597 if ((*migrationPaths
== NULL
) ||
598 ((count
= CFArrayGetCount(*migrationPaths
)) == 0)) {
599 SC_log(LOG_INFO
, "No migration paths");
603 for (CFIndex idx
= 0; idx
< count
; idx
++) {
604 filePath
= CFArrayGetValueAtIndex(*migrationPaths
, idx
);
605 if (_SCNetworkConfigurationMigrateIsFilePresent(filePath
) == FALSE
) {
606 SC_log(LOG_INFO
, "Required migration file not present: %@", filePath
);
611 configFilesPresent
= TRUE
; // all necessary configuration files present
613 return configFilesPresent
;
617 static CFMutableArrayRef
618 _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(SCPreferencesRef ni_prefs
, Boolean isBuiltin
)
621 SCNetworkInterfaceRef interface
;
622 CFArrayRef interfaceList
= NULL
;
623 CFMutableArrayRef resultInterfaceList
= NULL
;
625 interfaceList
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
626 if (interfaceList
== NULL
) {
627 SC_log(LOG_INFO
, "No interfaces");
631 count
= CFArrayGetCount(interfaceList
);
633 resultInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
636 for (CFIndex i
= 0; i
< count
; i
++) {
637 interface
= CFArrayGetValueAtIndex(interfaceList
, i
);
639 if (_SCNetworkInterfaceIsBuiltin(interface
) == isBuiltin
) {
640 CFArrayAppendValue(resultInterfaceList
, interface
);
645 if (interfaceList
!= NULL
) {
646 CFRelease(interfaceList
);
648 return resultInterfaceList
;
651 static CFMutableDictionaryRef
652 _SCNetworkInterfaceStorageCopyMaxUnitPerInterfaceType(SCPreferencesRef ni_prefs
)
654 CFNumberRef cfMaxUnit
;
656 CFArrayRef ifList
= NULL
;
657 SCNetworkInterfaceRef interface
;
658 CFMutableDictionaryRef interfaceTypeToMaxUnitMapping
= NULL
;
662 ifList
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
663 if (ifList
== NULL
) {
664 SC_log(LOG_INFO
, "No interfaces");
668 interfaceTypeToMaxUnitMapping
= CFDictionaryCreateMutable(NULL
, 0,
669 &kCFTypeDictionaryKeyCallBacks
,
670 &kCFTypeDictionaryValueCallBacks
);
671 count
= CFArrayGetCount(ifList
);
672 for (CFIndex idx
= 0; idx
< count
; idx
++) {
674 interface
= CFArrayGetValueAtIndex(ifList
, idx
);
676 if (isA_SCNetworkInterface(interface
) == NULL
) {
680 type
= _SCNetworkInterfaceGetIOInterfaceType(interface
);
681 if (isA_CFNumber(type
) == NULL
) {
682 SC_log(LOG_INFO
, "No interface type");
686 if (!CFDictionaryContainsKey(interfaceTypeToMaxUnitMapping
, type
)) {
688 cfMaxUnit
= CFNumberCreate(NULL
, kCFNumberIntType
, &temp
);
689 CFDictionaryAddValue(interfaceTypeToMaxUnitMapping
, type
, cfMaxUnit
);
690 CFRelease(cfMaxUnit
);
693 if (cfMaxUnit
== NULL
) {
694 cfMaxUnit
= CFDictionaryGetValue(interfaceTypeToMaxUnitMapping
, type
);
697 unit
= _SCNetworkInterfaceGetIOInterfaceUnit(interface
);
699 if (isA_CFNumber(unit
) == NULL
) {
703 if (CFNumberCompare(unit
, cfMaxUnit
, NULL
) == kCFCompareGreaterThan
) {
704 CFDictionarySetValue(interfaceTypeToMaxUnitMapping
, type
, unit
);
707 if (ifList
!= NULL
) {
710 return interfaceTypeToMaxUnitMapping
;
713 static CFMutableDictionaryRef
714 _SCNetworkConfigurationCopyBuiltinMapping (SCPreferencesRef sourcePrefs
, SCPreferencesRef targetPrefs
)
716 CFMutableDictionaryRef builtinMapping
= NULL
;
717 CFIndex sourceBuiltinInterfaceCount
= 0;
718 CFMutableArrayRef sourceBuiltinInterfaces
= NULL
;
719 SCNetworkInterfaceRef sourceInterface
;
720 CFIndex targetBuiltinInterfaceCount
= 0;
721 CFMutableArrayRef targetBuiltinInterfaces
= NULL
;
722 SCNetworkInterfaceRef targetInterface
;
724 sourceBuiltinInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(sourcePrefs
, TRUE
);
725 if (isA_CFArray(sourceBuiltinInterfaces
) == NULL
) {
726 SC_log(LOG_INFO
, "No source built-in interfaces");
729 sourceBuiltinInterfaceCount
= CFArrayGetCount(sourceBuiltinInterfaces
);
731 targetBuiltinInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(targetPrefs
, TRUE
);
732 if (isA_CFArray(targetBuiltinInterfaces
) == NULL
) {
733 SC_log(LOG_INFO
, "No target built-in interfaces");
736 targetBuiltinInterfaceCount
= CFArrayGetCount(targetBuiltinInterfaces
);
738 // Builtin Mapping will try to map all source interfaces into target interfaces
739 for (CFIndex idx
= 0; idx
< sourceBuiltinInterfaceCount
; idx
++) {
740 sourceInterface
= CFArrayGetValueAtIndex(sourceBuiltinInterfaces
, idx
);
741 for (CFIndex idx2
= 0; idx2
< targetBuiltinInterfaceCount
; idx2
++) {
742 targetInterface
= CFArrayGetValueAtIndex(targetBuiltinInterfaces
, idx2
);
744 if (_SCNetworkConfigurationIsInterfaceNamerMappable(sourceInterface
, targetInterface
, FALSE
)) {
745 if (builtinMapping
== NULL
) {
746 builtinMapping
= CFDictionaryCreateMutable(NULL
, 0,
747 &kCFTypeDictionaryKeyCallBacks
,
748 &kCFTypeDictionaryValueCallBacks
);
750 CFDictionaryAddValue(builtinMapping
, sourceInterface
, targetInterface
);
751 CFArrayRemoveValueAtIndex(targetBuiltinInterfaces
, idx2
);
755 targetBuiltinInterfaceCount
= CFArrayGetCount(targetBuiltinInterfaces
);
759 if (sourceBuiltinInterfaces
!= NULL
) {
760 CFRelease(sourceBuiltinInterfaces
);
762 if (targetBuiltinInterfaces
!= NULL
) {
763 CFRelease(targetBuiltinInterfaces
);
765 return builtinMapping
;
768 static CFMutableDictionaryRef
769 _SCNetworkConfigurationCopyExternalInterfaceMapping (SCPreferencesRef sourcePref
, SCPreferencesRef targetPrefs
)
771 CFNumberRef cfMaxTargetUnit
= NULL
;
772 CFNumberRef currentInterfaceUnit
= NULL
;
773 CFMutableDictionaryRef externalMapping
= NULL
;
774 CFMutableDictionaryRef interfaceTypeToMaxUnitMapping
= NULL
;
777 CFIndex sourceExternalInterfaceCount
= 0;
778 CFMutableArrayRef sourceExternalInterfaces
= NULL
;
779 SCNetworkInterfaceRef sourceInterface
= NULL
;
780 CFIndex targetExternalInterfaceCount
= 0;
781 CFMutableArrayRef targetExternalInterfaces
= NULL
;
782 SCNetworkInterfaceRef targetInterface
= NULL
;
785 sourceExternalInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(sourcePref
, FALSE
);
786 if (isA_CFArray(sourceExternalInterfaces
) == NULL
) {
787 SC_log(LOG_INFO
, "No source external interfaces");
790 sourceExternalInterfaceCount
= CFArrayGetCount(sourceExternalInterfaces
);
791 if (sourceExternalInterfaceCount
== 0) {
792 SC_log(LOG_INFO
, "No source external interfaces");
796 targetExternalInterfaces
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(targetPrefs
, FALSE
);
797 if (isA_CFArray(targetExternalInterfaces
) == NULL
) {
798 SC_log(LOG_INFO
, "No target external interfaces");
802 interfaceTypeToMaxUnitMapping
= _SCNetworkInterfaceStorageCopyMaxUnitPerInterfaceType(targetPrefs
);
803 externalMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
805 // Map all external interfaces which exist in both source and target
806 for (CFIndex idx
= 0; idx
< sourceExternalInterfaceCount
; idx
++) {
807 sourceInterface
= CFArrayGetValueAtIndex(sourceExternalInterfaces
, idx
);
808 targetExternalInterfaceCount
= CFArrayGetCount(targetExternalInterfaces
);
809 currentInterfaceUnit
= NULL
;
811 for (CFIndex idx2
= 0; idx2
< targetExternalInterfaceCount
; idx2
++) {
812 targetInterface
= CFArrayGetValueAtIndex(targetExternalInterfaces
, idx2
);
814 if (_SCNetworkConfigurationIsInterfaceNamerMappable(sourceInterface
, targetInterface
, TRUE
)) {
815 CFDictionaryAddValue(externalMapping
, sourceInterface
, targetInterface
);
816 CFArrayRemoveValueAtIndex(targetExternalInterfaces
, idx2
);
821 if (!CFDictionaryContainsKey(externalMapping
, sourceInterface
)) {
822 // Create new mappings for external source interfaces which don't exist in the target
823 type
= _SCNetworkInterfaceGetIOInterfaceType(sourceInterface
);
825 cfMaxTargetUnit
= CFDictionaryGetValue(interfaceTypeToMaxUnitMapping
, type
);
826 if (cfMaxTargetUnit
!= NULL
) {
827 CFNumberGetValue(cfMaxTargetUnit
, kCFNumberIntType
, &maxTargetUnit
);
828 newTargetUnit
= maxTargetUnit
+ 1;
834 cfMaxTargetUnit
= CFNumberCreate(NULL
, kCFNumberIntType
, &newTargetUnit
);
835 CFDictionarySetValue(interfaceTypeToMaxUnitMapping
, type
, cfMaxTargetUnit
);
837 targetInterface
= (SCNetworkInterfaceRef
)__SCNetworkInterfaceCreateCopy(NULL
, sourceInterface
, NULL
, NULL
);
839 SC_log(LOG_DEBUG
, "sourceInterface: %p, target Interface: %p", sourceInterface
, targetInterface
);
841 currentInterfaceUnit
= _SCNetworkInterfaceGetIOInterfaceUnit(targetInterface
);
843 if ((isA_CFNumber(currentInterfaceUnit
) == NULL
) ||
844 !CFEqual(currentInterfaceUnit
, cfMaxTargetUnit
)) {
845 // Update the interface unit
846 __SCNetworkInterfaceSetIOInterfaceUnit(targetInterface
, cfMaxTargetUnit
);
849 CFDictionaryAddValue(externalMapping
, sourceInterface
, targetInterface
);
851 CFRelease(targetInterface
);
852 targetInterface
= NULL
;
853 CFRelease(cfMaxTargetUnit
);
854 cfMaxTargetUnit
= NULL
;
858 if (sourceExternalInterfaces
!= NULL
) {
859 CFRelease(sourceExternalInterfaces
);
861 if (targetExternalInterfaces
!= NULL
) {
862 CFRelease(targetExternalInterfaces
);
864 if (interfaceTypeToMaxUnitMapping
!= NULL
) {
865 CFRelease(interfaceTypeToMaxUnitMapping
);
867 return externalMapping
;
870 __SCNetworkConfigurationInterfaceNameIsEquiv(CFStringRef interfaceName1
, CFStringRef interfaceName2
);
873 _SCNetworkConfigurationIsInterfaceNamerMappable(SCNetworkInterfaceRef interface1
, SCNetworkInterfaceRef interface2
, Boolean bypassActive
)
875 Boolean interface1IsBuiltin
;
876 CFStringRef interface1Prefix
;
877 CFStringRef interface1Type
;
878 CFStringRef interface1UserDefinedName
;
879 Boolean interface2IsBuiltin
;
880 CFStringRef interface2Prefix
;
881 CFStringRef interface2Type
;
882 CFStringRef interface2UserDefinedName
;
884 if (interface1
== interface2
) {
885 // No work needs to be done
888 interface1IsBuiltin
= _SCNetworkInterfaceIsBuiltin(interface1
);
889 interface2IsBuiltin
= _SCNetworkInterfaceIsBuiltin(interface2
);
891 interface1UserDefinedName
= SCNetworkInterfaceGetLocalizedDisplayName(interface1
);
892 interface2UserDefinedName
= SCNetworkInterfaceGetLocalizedDisplayName(interface2
);
894 interface1Type
= SCNetworkInterfaceGetInterfaceType(interface1
);
895 interface2Type
= SCNetworkInterfaceGetInterfaceType(interface2
);
897 interface1Prefix
= _SCNetworkInterfaceGetIOInterfaceNamePrefix(interface1
);
898 interface2Prefix
= _SCNetworkInterfaceGetIOInterfaceNamePrefix(interface2
);
900 // Check if have same builtin values.
901 // Check if User Defined name matches
902 // Check if SCNetwork Interface Type matches
904 if (interface1IsBuiltin
!= interface2IsBuiltin
) {
908 if (!_SC_CFEqual(interface1Type
, interface2Type
)) {
912 if (!_SC_CFEqual(interface1Prefix
, interface2Prefix
)) {
916 if (!_SC_CFEqual(interface1UserDefinedName
, interface2UserDefinedName
)) {
917 // Checking if we have a mismatch because of the name Ethernet and Ethernet 1
918 // Checking if we have a mismatch because of the name Airport and WiFi
919 if (interface1IsBuiltin
&&
920 interface2IsBuiltin
&&
921 __SCNetworkConfigurationInterfaceNameIsEquiv(interface1UserDefinedName
, interface2UserDefinedName
)) {
930 __SCNetworkConfigurationInterfaceNameIsEquiv(CFStringRef interfaceName1
, CFStringRef interfaceName2
)
932 CFStringRef interfaceArray
[] = { CFSTR("iPhone"), CFSTR("iPad"), CFSTR("iPod"), CFSTR("AppleTV") };
933 const int interfaceCount
= sizeof(interfaceArray
) / sizeof(CFStringRef
);
934 CFStringRef portSuffix
= CFSTR(", Port 1");
936 if ((isA_CFString(interfaceName1
) != NULL
) &&
937 (isA_CFString(interfaceName2
) != NULL
)) {
938 if (!CFEqual(interfaceName1
, interfaceName2
)) {
939 // Check if we are looking at the WiFi interface
940 if ((CFEqual(interfaceName1
, CFSTR("AirPort")) ||
941 (CFEqual(interfaceName1
, CFSTR("Wi-Fi")))) &&
942 (CFEqual(interfaceName2
, CFSTR("AirPort")) ||
943 (CFEqual(interfaceName2
, CFSTR("Wi-Fi"))))) {
947 if (((CFEqual(interfaceName1
, CFSTR("Ethernet"))) ||
948 (CFEqual(interfaceName1
, CFSTR("Ethernet 1")))) &&
949 ((CFEqual(interfaceName2
, CFSTR("Ethernet"))) ||
950 (CFEqual(interfaceName2
, CFSTR("Ethernet 1"))))) {
954 if ((CFStringHasSuffix(interfaceName1
, portSuffix
) &&
955 (CFStringCompareWithOptions(interfaceName1
, interfaceName2
, CFRangeMake(0, (CFStringGetLength(interfaceName1
) - CFStringGetLength(portSuffix
))), 0) == kCFCompareEqualTo
)) ||
956 (CFStringHasSuffix(interfaceName2
, portSuffix
) &&
957 (CFStringCompareWithOptions(interfaceName2
, interfaceName1
, CFRangeMake(0, (CFStringGetLength(interfaceName2
) - CFStringGetLength(portSuffix
))), 0) == kCFCompareEqualTo
))) {
961 for (CFIndex idx
= 0; idx
< interfaceCount
; idx
++) {
962 CFStringRef tempInterfaceName
= interfaceArray
[idx
];
963 if ((CFEqual(interfaceName1
, tempInterfaceName
) ||
964 __SCNetworkInterfaceMatchesName(interfaceName1
, tempInterfaceName
)) &&
965 (CFEqual(interfaceName2
, tempInterfaceName
) ||
966 __SCNetworkInterfaceMatchesName(interfaceName2
, tempInterfaceName
))) {
980 CFDictionaryRef interfaceMapping
;
981 CFMutableArrayRef interfacesMissingServices
;
982 } SCNetworkConfigurationMissingServiceContext
;
985 CFDictionaryRef bsdNameToBridgeServices
; // Mapping of BSD Name to SCBridgeInterfaceRef
986 CFDictionaryRef bsdNameToBondServices
; // Mapping of BSD Name to SCBondInterfaceRef
987 CFDictionaryRef bsdNameToVLANServices
; // Mapping of BSD Name to SCVLANInterfaceRef
988 CFDictionaryRef interfaceMapping
;
990 CFMutableArrayRef interfaceToBeRemoved
; // SCNetworkInterfaceRef. Services containing the interface will be removed
991 CFMutableArrayRef interfaceToBeReplaced
;// SCNetworkInterfaceRef. Services containing the interface will be replaced with default service
992 CFMutableArrayRef interfacePreserveServiceInformation
; // SCNetworkInterfaceRef. Services containing the interface will be replaced with new service which has same configuration as the current service with issue.
993 CFMutableDictionaryRef bsdNameServiceProtocolPreserveMapping
;
994 SCPreferencesRef prefs
;
996 } SCNetworkConfigurationValidityContext
;
999 _SCNetworkConfigurationValidateInterface (const void *key
, const void *value
, void *context
)
1001 CFStringRef bsdName
= (CFStringRef
)key
;
1002 SCNetworkConfigurationValidityContext
*ctx
= (SCNetworkConfigurationValidityContext
*)context
;
1003 CFDictionaryRef bsdNameToBridgeServices
= ctx
->bsdNameToBridgeServices
;
1004 CFDictionaryRef bsdNameToBondServices
= ctx
->bsdNameToBondServices
;
1005 CFDictionaryRef bsdNameToVLANServices
= ctx
->bsdNameToVLANServices
;
1006 SCNetworkInterfaceRef interface
= NULL
;
1007 CFDictionaryRef interfaceMapping
= ctx
->interfaceMapping
;
1008 CFStringRef interfaceUserDefinedName
= NULL
;
1009 Boolean repair
= ctx
->repair
;
1010 SCNetworkInterfaceRef serviceInterface
= (SCNetworkInterfaceRef
)value
;
1011 CFStringRef serviceInterfaceUserDefinedName
= NULL
;
1012 CFMutableArrayRef interfaceToBeRemoved
= ctx
->interfaceToBeRemoved
;
1013 CFMutableArrayRef interfaceToBeReplaced
= ctx
->interfaceToBeReplaced
;
1014 CFMutableArrayRef interfacePreserveServiceInformation
= ctx
->interfacePreserveServiceInformation
;
1016 // No work needs to be done if we have already made determination that configuration somewhere is not valid,
1017 // or we don't intend to repair invalid configuration.
1018 if ((*ctx
->isValid
== FALSE
) && !repair
) {
1022 // There is no interface present for the service
1023 interface
= CFDictionaryGetValue(interfaceMapping
, bsdName
);
1024 if (interface
== NULL
) {
1025 if ((((bsdNameToBridgeServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToBridgeServices
, bsdName
))) &&
1026 (((bsdNameToBondServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToBondServices
, bsdName
))) &&
1027 (((bsdNameToVLANServices
!= NULL
) && !CFDictionaryContainsKey(bsdNameToVLANServices
, bsdName
)))) {
1028 // Not a virtual interface
1029 SC_log(LOG_INFO
, "No real interface with BSD name (%@) for service", bsdName
);
1032 CFArrayAppendValue(interfaceToBeRemoved
, serviceInterface
);
1034 *ctx
->isValid
= FALSE
;
1039 // Need to compare between both SCNetworkInterfaceRefs
1040 interfaceUserDefinedName
= __SCNetworkInterfaceGetUserDefinedName(interface
);
1041 serviceInterfaceUserDefinedName
= __SCNetworkInterfaceGetUserDefinedName(serviceInterface
);
1043 if (!__SCNetworkConfigurationInterfaceNameIsEquiv(interfaceUserDefinedName
, serviceInterfaceUserDefinedName
)) {
1044 SC_log(LOG_INFO
, "Interface user defined name (%@) doesn't match service/interface user defined name: %@",
1045 interfaceUserDefinedName
,
1046 serviceInterfaceUserDefinedName
);
1047 *ctx
->isValid
= FALSE
;
1048 // Check if the service interface name is set to localized key
1049 if (isA_CFArray(interfacePreserveServiceInformation
) != NULL
&&
1050 __SCNetworkInterfaceMatchesName(interfaceUserDefinedName
, serviceInterfaceUserDefinedName
)) {
1051 SC_log(LOG_NOTICE
, "serviceInterfaceUserDefinedName: %@ is the localized key for interface name: %@", serviceInterfaceUserDefinedName
, interfaceUserDefinedName
);
1052 CFArrayAppendValue(interfacePreserveServiceInformation
, serviceInterface
);
1054 // Add service interface to the interfaceToBeReplaced list
1055 if (isA_CFArray(interfaceToBeReplaced
) != NULL
) {
1056 CFArrayAppendValue(interfaceToBeReplaced
, interface
);
1058 if (isA_CFArray(interfaceToBeRemoved
) != NULL
) {
1059 CFArrayAppendValue(interfaceToBeRemoved
, serviceInterface
);
1066 _SCNetworkConfigurationCollectMissingService(const void *key
, const void *value
, void *context
)
1068 CFStringRef bsdName
= (CFStringRef
)key
;
1069 SCNetworkConfigurationMissingServiceContext
*ctx
= (SCNetworkConfigurationMissingServiceContext
*)context
;
1070 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)value
;
1071 CFMutableArrayRef interfacesMissingServices
= ctx
->interfacesMissingServices
;
1072 CFDictionaryRef serviceInterfaceMapping
= ctx
->interfaceMapping
;
1074 if ((isA_SCNetworkInterface(interface
) == NULL
) ||
1075 !_SCNetworkInterfaceIsBuiltin(interface
)) {
1079 // Check if services have mapping for the BSD name of the interface
1080 if (!CFDictionaryContainsKey(serviceInterfaceMapping
, bsdName
)) {
1081 CFArrayAppendValue(interfacesMissingServices
, interface
); // Adding interface since the corresponding service seems to be missing
1086 _SCNetworkConfigurationCreateBuiltinInterfaceServices(SCPreferencesRef pref
,
1087 SCPreferencesRef ni_pref
)
1089 SCNetworkConfigurationMissingServiceContext context
;
1090 SCNetworkInterfaceRef interface
= NULL
;
1091 CFArrayRef interfaces
= NULL
;
1092 CFMutableArrayRef interfacesWithoutService
= NULL
;
1093 CFDictionaryRef mappingBSDNameToInterface
= NULL
;
1094 CFDictionaryRef mappingServiceBSDNameToInterface
= NULL
;
1095 CFIndex missingServiceCount
= 0;
1096 Boolean success
= FALSE
;
1098 interfaces
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_pref
);
1099 if (interfaces
== NULL
) {
1100 SC_log(LOG_NOTICE
, "No interfaces");
1104 mappingBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1105 CFRelease(interfaces
);
1106 if (isA_CFDictionary(mappingBSDNameToInterface
) == NULL
) {
1110 interfaces
= __SCNetworkServiceCopyAllInterfaces(pref
);
1111 if (interfaces
== NULL
) {
1112 SC_log(LOG_INFO
, "No interfaces");
1115 mappingServiceBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1116 CFRelease(interfaces
);
1117 if (isA_CFDictionary(mappingServiceBSDNameToInterface
) == NULL
) {
1121 interfacesWithoutService
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1123 context
.interfaceMapping
= mappingServiceBSDNameToInterface
;
1124 context
.interfacesMissingServices
= interfacesWithoutService
;
1126 CFDictionaryApplyFunction(mappingBSDNameToInterface
, _SCNetworkConfigurationCollectMissingService
, &context
);
1127 missingServiceCount
= CFArrayGetCount(interfacesWithoutService
);
1131 for (CFIndex idx
= 0; idx
< missingServiceCount
; idx
++) {
1132 interface
= CFArrayGetValueAtIndex(interfacesWithoutService
, idx
);
1134 if (!__SCNetworkServiceCreate(pref
, interface
, NULL
)) {
1135 SC_log(LOG_INFO
, "Could not add service for interface: %@", interface
);
1140 if (mappingBSDNameToInterface
!= NULL
) {
1141 CFRelease(mappingBSDNameToInterface
);
1143 if (mappingServiceBSDNameToInterface
!= NULL
) {
1144 CFRelease(mappingServiceBSDNameToInterface
);
1146 if (interfacesWithoutService
!= NULL
) {
1147 CFRelease(interfacesWithoutService
);
1154 add_service(const void *value
, void *context
)
1156 SCNetworkConfigurationValidityContext
*ctx
= (SCNetworkConfigurationValidityContext
*)context
;
1157 SCNetworkSetRef currentSet
= NULL
;
1159 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)value
;
1160 CFDictionaryRef bsdNameServiceProtocolMapping
= ctx
->bsdNameServiceProtocolPreserveMapping
;
1161 SCPreferencesRef prefs
= ctx
->prefs
;
1162 SCNetworkServiceRef service
;
1163 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1164 CFArrayRef protocolArray
= NULL
;
1166 if (isA_CFString(bsdName
)) {
1167 protocolArray
= CFDictionaryGetValue(bsdNameServiceProtocolMapping
, bsdName
);
1169 service
= SCNetworkServiceCreate(prefs
, interface
);
1170 if (service
== NULL
) {
1171 SC_log(LOG_INFO
, "Could not create new service");
1175 if (!SCNetworkServiceEstablishDefaultConfiguration(service
)) {
1176 SCNetworkServiceRemove(service
);
1177 SC_log(LOG_INFO
, "SCNetworkServiceEstablishDefaultConfiguration() failed");
1181 if (protocolArray
!= NULL
) {
1182 CFIndex protocolArrayCount
= CFArrayGetCount(protocolArray
);
1184 for (CFIndex idx
= 0; idx
< protocolArrayCount
; idx
++) {
1185 CFDictionaryRef protocolInfo
= CFArrayGetValueAtIndex(protocolArray
, idx
);
1186 CFDictionaryRef configuration
= CFDictionaryGetValue(protocolInfo
, kProtocolConfiguration
);
1187 CFStringRef protocolType
= CFDictionaryGetValue(protocolInfo
, kProtocolType
);
1188 CFBooleanRef cfEnabled
= CFDictionaryGetValue(protocolInfo
, kProtocolEnabled
);
1190 enabled
= CFBooleanGetValue(cfEnabled
);
1194 __SCNetworkServiceAddProtocolToService(service
, protocolType
, configuration
, enabled
);
1198 // Add Service to current set
1199 currentSet
= SCNetworkSetCopyCurrent(prefs
);
1200 if (currentSet
== NULL
) {
1201 SCNetworkServiceRemove(service
);
1202 SC_log(LOG_INFO
, "Could not find current set");
1206 if (!SCNetworkSetAddService(currentSet
, service
)) {
1207 SCNetworkServiceRemove(service
);
1208 SC_log(LOG_INFO
, "Could not add service to current set");
1212 if (service
!= NULL
) {
1215 if (currentSet
!= NULL
) {
1216 CFRelease(currentSet
);
1221 create_bsd_name_service_protocol_mapping(const void *value
, void *context
)
1223 SCNetworkConfigurationValidityContext
*ctx
= (SCNetworkConfigurationValidityContext
*)context
;
1224 CFArrayRef interfacePreserveServiceInformation
= ctx
->interfacePreserveServiceInformation
;
1225 CFMutableDictionaryRef bsdNameServiceProtocolMapping
= ctx
->bsdNameServiceProtocolPreserveMapping
;
1226 SCNetworkInterfaceRef interface
;
1227 SCNetworkServiceRef service
= (SCNetworkServiceRef
)value
;
1229 interface
= SCNetworkServiceGetInterface(service
);
1231 if (CFArrayContainsValue(interfacePreserveServiceInformation
, CFRangeMake(0, CFArrayGetCount(interfacePreserveServiceInformation
)), interface
)) {
1232 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1233 if (isA_CFString(bsdName
)) {
1235 CFArrayRef protocols
= SCNetworkServiceCopyProtocols(service
);
1236 if (protocols
!= NULL
) {
1237 CFMutableArrayRef protocolArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1238 CFIndex protocolCount
= CFArrayGetCount(protocols
);
1240 for (CFIndex idx
= 0; idx
< protocolCount
; idx
++) {
1241 SCNetworkProtocolRef protocol
= CFArrayGetValueAtIndex(protocols
, idx
);
1242 CFDictionaryRef configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1243 CFStringRef protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1244 Boolean enabled
= SCNetworkProtocolGetEnabled(protocol
);
1246 if (configuration
== NULL
|| protocolType
== NULL
) {
1249 CFMutableDictionaryRef protocolInfo
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
1251 CFDictionaryAddValue(protocolInfo
, kProtocolType
, protocolType
);
1252 CFDictionaryAddValue(protocolInfo
, kProtocolConfiguration
, configuration
);
1253 CFDictionaryAddValue(protocolInfo
, kProtocolEnabled
, enabled
? kCFBooleanTrue
: kCFBooleanFalse
);
1254 CFArrayAppendValue(protocolArray
, protocolInfo
);
1255 CFRelease(protocolInfo
);
1257 CFDictionaryAddValue(bsdNameServiceProtocolMapping
, bsdName
, protocolArray
);
1258 CFRelease(protocols
);
1259 CFRelease(protocolArray
);
1267 remove_service(const void *value
, void *context
)
1269 SCNetworkConfigurationValidityContext
*ctx
= (SCNetworkConfigurationValidityContext
*)context
;
1270 SCNetworkInterfaceRef interface
;
1271 SCNetworkServiceRef service
= (SCNetworkServiceRef
)value
;
1272 CFArrayRef toBeRemoved
= ctx
->interfaceToBeRemoved
;
1274 interface
= SCNetworkServiceGetInterface(service
);
1276 if (CFArrayContainsValue(toBeRemoved
, CFRangeMake(0, CFArrayGetCount(toBeRemoved
)), interface
)) {
1277 SCNetworkServiceRemove(service
);
1282 _SCNetworkConfigurationSaveOldConfiguration(SCPreferencesRef prefs
)
1284 CFAbsoluteTime absoluteTime
;
1285 CFCalendarRef currentCalendar
;
1288 CFStringRef keyList
[] = {
1291 kSCPrefNetworkServices
,
1294 kSCPrefVirtualNetworkInterfaces
1296 CFIndex keyListCount
;
1302 currentCalendar
= CFCalendarCopyCurrent();
1303 absoluteTime
= CFAbsoluteTimeGetCurrent();
1305 if (!CFCalendarDecomposeAbsoluteTime(currentCalendar
, absoluteTime
, "yMdHms",
1306 &year
, &month
, &day
, &hour
, &minute
, &second
)) {
1307 SC_log(LOG_INFO
, "CFCalendarDecomposeAbsoluteTime() failed");
1309 keyListCount
= (CFIndex
)sizeof(keyList
)/sizeof(CFStringRef
);
1311 for (CFIndex idx
= 0; idx
< keyListCount
; idx
++) {
1313 CFTypeRef value
= SCPreferencesGetValue(prefs
, keyList
[idx
]);
1315 if (value
!= NULL
) {
1316 newKey
= CFStringCreateWithFormat(NULL
, NULL
,
1317 CFSTR("%d-%d-%d %d:%d:%d : %@"),
1318 year
, month
, day
, hour
,
1319 minute
, second
, keyList
[idx
]);
1320 SCPreferencesSetValue(prefs
, newKey
, value
);
1324 CFRelease(currentCalendar
);
1328 _SCNetworkConfigurationRepairUsingPreferences(SCPreferencesRef prefs
,
1329 SCNetworkConfigurationValidityContext
*context
)
1331 CFIndex removeCount
;
1332 CFIndex replaceCount
;
1333 CFArrayRef serviceList
;
1334 CFArrayRef interfaceToBeRemoved
= context
->interfaceToBeRemoved
;
1335 CFArrayRef interfaceToBeReplaced
= context
->interfaceToBeReplaced
;
1337 removeCount
= CFArrayGetCount(interfaceToBeRemoved
);
1338 replaceCount
= CFArrayGetCount(interfaceToBeReplaced
);
1339 if (removeCount
== 0 &&
1340 replaceCount
== 0) {
1341 // We don't have any information to repair
1344 // Backup current preferences before making changes
1345 _SCNetworkConfigurationSaveOldConfiguration(prefs
);
1347 serviceList
= SCNetworkServiceCopyAll(prefs
);
1348 CFArrayApplyFunction(serviceList
, CFRangeMake(0, CFArrayGetCount(serviceList
)), create_bsd_name_service_protocol_mapping
, context
);
1349 CFArrayApplyFunction(serviceList
, CFRangeMake(0, CFArrayGetCount(serviceList
)), remove_service
, (void*)context
);
1350 CFArrayApplyFunction(interfaceToBeReplaced
, CFRangeMake(0, replaceCount
), add_service
, (void*)context
);
1351 CFRelease(serviceList
);
1356 validate_bridge(const void *value
, void *context
)
1358 SCBridgeInterfaceRef bridge
= (SCBridgeInterfaceRef
) value
;
1359 CFArrayRef memberInterfaces
= SCBridgeInterfaceGetMemberInterfaces(bridge
);
1360 CFMutableArrayRef memberInterfacesMutable
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1361 SCPreferencesRef ni_prefs
= (SCPreferencesRef
)context
;
1363 for (CFIndex idx
= 0; idx
< CFArrayGetCount(memberInterfaces
); idx
++) {
1364 CFStringRef bsdName
;
1365 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)CFArrayGetValueAtIndex(memberInterfaces
, idx
);
1366 SCNetworkInterfaceRef memberInterface
;
1368 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1369 if (bsdName
== NULL
) {
1373 // Check if member interface is present
1374 memberInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, bsdName
);
1375 if (memberInterface
!= NULL
) {
1376 CFArrayAppendValue(memberInterfacesMutable
, memberInterface
);
1377 CFRelease(memberInterface
);
1381 if (CFArrayGetCount(memberInterfacesMutable
) == 0) {
1382 SC_log(LOG_INFO
, "Removing invalid bridge configuration: %@", bridge
);
1383 SCBridgeInterfaceRemove(bridge
);
1386 SCBridgeInterfaceSetMemberInterfaces(bridge
, memberInterfacesMutable
);
1388 CFRelease(memberInterfacesMutable
);
1390 #if !TARGET_OS_IPHONE
1392 validate_bond(const void *value
, void *context
)
1394 SCBondInterfaceRef bond
= (SCBondInterfaceRef
)value
;
1395 CFArrayRef memberInterfaces
= SCBondInterfaceGetMemberInterfaces(bond
);
1396 CFMutableArrayRef memberInterfacesMutable
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1397 SCPreferencesRef ni_prefs
= (SCPreferencesRef
)context
;
1399 for (CFIndex idx
= 0; idx
< CFArrayGetCount(memberInterfaces
); idx
++) {
1400 CFStringRef bsdName
;
1401 SCNetworkInterfaceRef interface
= (SCNetworkInterfaceRef
)CFArrayGetValueAtIndex(memberInterfaces
, idx
);
1402 SCNetworkInterfaceRef memberInterface
;
1404 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
1405 if (bsdName
== NULL
) {
1409 // Check if member interface is present
1410 memberInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, bsdName
);
1411 if (memberInterface
!= NULL
) {
1412 CFArrayAppendValue(memberInterfacesMutable
, memberInterface
);
1413 CFRelease(memberInterface
);
1417 if (CFArrayGetCount(memberInterfacesMutable
) == 0) {
1418 SC_log(LOG_INFO
, "Removing invalid bond configuration: %@", bond
);
1419 SCBondInterfaceRemove(bond
);
1422 SCBondInterfaceSetMemberInterfaces(bond
, memberInterfacesMutable
);
1424 CFRelease(memberInterfacesMutable
);
1429 validate_vlan(const void *value
, void *context
)
1431 CFStringRef bsdName
;
1432 SCNetworkInterfaceRef interface
;
1433 Boolean isValid
= TRUE
;
1434 SCPreferencesRef ni_prefs
= (SCPreferencesRef
)context
;
1435 SCNetworkInterfaceRef physicalInterface
;
1436 SCVLANInterfaceRef vlan
= (SCVLANInterfaceRef
)value
;
1438 physicalInterface
= SCVLANInterfaceGetPhysicalInterface(vlan
);
1439 bsdName
= SCNetworkInterfaceGetBSDName(physicalInterface
);
1441 if (bsdName
== NULL
) {
1446 // Check if the physical interface is present
1447 interface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, bsdName
);
1448 if (interface
== NULL
) {
1452 CFRelease(interface
);
1456 SC_log(LOG_INFO
, "Removing invalid VLAN configuration: %@", vlan
);
1457 SCVLANInterfaceRemove(vlan
);
1462 _SCNetworkConfigurationCheckValidityWithPreferences(SCPreferencesRef prefs
,
1463 SCPreferencesRef ni_prefs
,
1464 CFDictionaryRef options
)
1466 CFArrayRef allServices
= NULL
;
1467 CFArrayRef allSets
= NULL
;
1468 CFDictionaryRef bsdNameToBridgeServices
= NULL
;
1469 CFDictionaryRef bsdNameToBondServices
= NULL
;
1470 CFDictionaryRef bsdNameToVLANServices
= NULL
;
1471 SCNetworkConfigurationValidityContext context
;
1472 CFArrayRef interfaces
= NULL
;
1473 CFMutableArrayRef interfaceToBeRemoved
= NULL
;
1474 CFMutableArrayRef interfaceToBeReplaced
= NULL
;
1475 CFMutableArrayRef interfacePreserveServiceInformation
= NULL
;
1476 CFMutableDictionaryRef bsdNameServiceProtocolPreserveMapping
= NULL
;
1477 Boolean isValid
= TRUE
;
1478 CFDictionaryRef mappingBSDNameToInterface
= NULL
;
1479 CFDictionaryRef mappingServiceBSDNameToInterface
= NULL
;
1480 CFStringRef model
= NULL
;
1481 CFStringRef ni_model
= NULL
;
1482 Boolean repairConfiguration
= FALSE
;
1483 Boolean revertLimitNetworkConfiguration
= FALSE
;
1484 CFArrayRef setServiceOrder
= NULL
;
1485 CFArrayRef setServices
= NULL
;
1487 if ((isA_CFDictionary(options
) != NULL
)) {
1488 CFBooleanRef repair
= CFDictionaryGetValue(options
, kSCNetworkConfigurationRepair
);
1489 if (isA_CFBoolean(repair
) != NULL
) {
1490 repairConfiguration
= CFBooleanGetValue(repair
);
1493 if (!__SCPreferencesGetLimitSCNetworkConfiguration(prefs
)) {
1494 __SCPreferencesSetLimitSCNetworkConfiguration(prefs
, TRUE
);
1495 revertLimitNetworkConfiguration
= TRUE
;
1499 Check the validity by:
1500 - Comparing if the models are the same
1502 model
= SCPreferencesGetValue(prefs
, MODEL
);
1503 ni_model
= SCPreferencesGetValue(ni_prefs
, MODEL
);
1505 if (isA_CFString(model
) == NULL
|| isA_CFString(ni_model
) == NULL
|| CFStringCompare(model
, ni_model
, 0) != kCFCompareEqualTo
) {
1507 SC_log(LOG_INFO
, "Model names do not match in preferences.plist and NetworkInterfaces.plist");
1512 - Comparing if the interfaces names mentioned in NetworkInterfaces.plist and preferences.plist match
1515 __SCNetworkInterfaceCreateMappingUsingBSDName(SCPreferencesRef prefs);
1517 interfaces
= __SCNetworkInterfaceCopyStoredWithPreferences(ni_prefs
);
1518 if (isA_CFArray(interfaces
) == NULL
) {
1519 SC_log(LOG_NOTICE
, "No interfaces");
1523 mappingBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1524 CFRelease(interfaces
);
1525 if (isA_CFDictionary(mappingBSDNameToInterface
) == NULL
) {
1527 SC_log(LOG_INFO
, "No BSD name to interface mapping");
1531 interfaces
= __SCNetworkServiceCopyAllInterfaces(prefs
);
1532 if (isA_CFArray(interfaces
) == NULL
) {
1534 SC_log(LOG_INFO
, "No interfaces");
1537 mappingServiceBSDNameToInterface
= __SCNetworkInterfaceCreateMappingUsingBSDName(interfaces
);
1538 CFRelease(interfaces
);
1539 if (isA_CFDictionary(mappingServiceBSDNameToInterface
) == NULL
) {
1541 SC_log(LOG_INFO
, "No Service BSD name to interface mapping");
1545 if (repairConfiguration
) {
1546 interfaceToBeRemoved
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1547 interfaceToBeReplaced
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1548 interfacePreserveServiceInformation
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1549 bsdNameServiceProtocolPreserveMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
1550 #if !TARGET_OS_IPHONE
1551 bsdNameToBridgeServices
= _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(prefs
);
1552 bsdNameToBondServices
= _SCNetworkMigrationCopyMappingBSDNameToBondServices(prefs
);
1553 bsdNameToVLANServices
= _SCNetworkMigrationCopyMappingBSDNameToVLANServices(prefs
);
1556 context
.interfaceMapping
= mappingBSDNameToInterface
;
1557 context
.isValid
= &isValid
;
1558 context
.interfaceToBeRemoved
= interfaceToBeRemoved
;
1559 context
.interfaceToBeReplaced
= interfaceToBeReplaced
;
1560 context
.interfacePreserveServiceInformation
= interfacePreserveServiceInformation
;
1561 context
.bsdNameToBridgeServices
= bsdNameToBridgeServices
;
1562 context
.bsdNameToBondServices
= bsdNameToBondServices
;
1563 context
.bsdNameToVLANServices
= bsdNameToVLANServices
;
1564 context
.repair
= repairConfiguration
;
1565 context
.prefs
= prefs
;
1566 context
.bsdNameServiceProtocolPreserveMapping
= bsdNameServiceProtocolPreserveMapping
;
1568 CFDictionaryApplyFunction(mappingServiceBSDNameToInterface
, _SCNetworkConfigurationValidateInterface
, &context
);
1571 SC_log(LOG_INFO
, "mismatch between interface names in NetworkInterfaces.plist and preferences.plist");
1572 if (repairConfiguration
) {
1573 isValid
= _SCNetworkConfigurationRepairUsingPreferences(prefs
, &context
);
1577 // Save the changes if repair fixed an invalid configuration
1578 if (!SCPreferencesCommitChanges(prefs
)) {
1579 SC_log(LOG_INFO
, "SCPreferencesCommitChanges() failed");
1587 - Check if all the network services mentioned in the SCNetworkSet are actually present in the SCNetworkService array
1590 allServices
= SCNetworkServiceCopyAll(prefs
);
1591 if (isA_CFArray(allServices
) == NULL
) {
1593 SC_log(LOG_INFO
, "No services");
1597 allSets
= SCNetworkSetCopyAll(prefs
);
1598 if (isA_CFArray(allSets
) == NULL
) {
1600 SC_log(LOG_INFO
, "No sets");
1604 for (CFIndex idx
= 0; ((idx
< CFArrayGetCount(allSets
)) && isValid
); idx
++) {
1605 SCNetworkSetRef set
= CFArrayGetValueAtIndex(allSets
, idx
);
1607 if (isA_SCNetworkSet(set
) == NULL
) {
1608 SC_log(LOG_INFO
, "No set");
1611 setServices
= SCNetworkSetCopyServices(set
);
1612 if (setServices
== NULL
) {
1613 SC_log(LOG_INFO
, "No services");
1616 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(setServices
); idx2
++) {
1617 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(setServices
, idx2
);
1619 if (!CFArrayContainsValue(allServices
, CFRangeMake(0, CFArrayGetCount(allServices
)), service
)) {
1621 SC_log(LOG_INFO
, "All network services in the network set are not present in SCNetworkService array");
1630 - Check if service IDs in service order do exist in the SET
1632 setServiceOrder
= SCNetworkSetGetServiceOrder(set
);
1633 if (setServiceOrder
!= NULL
) {
1634 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(setServiceOrder
); idx2
++) {
1635 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(setServiceOrder
, idx2
);
1636 if (!CFArrayContainsValue(setServiceOrder
, CFRangeMake(0, CFArrayGetCount(setServiceOrder
)), service
) &&
1637 !CFArrayContainsValue(allServices
, CFRangeMake(0, CFArrayGetCount(allServices
)), service
)) {
1638 SC_log(LOG_INFO
, "Service: %@ is not present in the service order for set %@", service
, set
);
1643 if (setServices
!= NULL
) {
1644 CFRelease(setServices
);
1650 - Check if the virtual network interfaces have valid member interfaces
1652 CFArrayRef bridges
= SCBridgeInterfaceCopyAll(prefs
);
1653 if (bridges
!= NULL
) {
1654 CFArrayApplyFunction(bridges
, CFRangeMake(0, CFArrayGetCount(bridges
)), validate_bridge
, (void*)ni_prefs
);
1657 #if !TARGET_OS_IPHONE
1658 CFArrayRef bonds
= SCBondInterfaceCopyAll(prefs
);
1659 if (bonds
!= NULL
) {
1660 CFArrayApplyFunction(bonds
, CFRangeMake(0, CFArrayGetCount(bonds
)), validate_bond
, (void*)ni_prefs
);
1664 CFArrayRef vlans
= SCVLANInterfaceCopyAll(prefs
);
1665 if (vlans
!= NULL
) {
1666 CFArrayApplyFunction(vlans
, CFRangeMake(0, CFArrayGetCount(vlans
)), validate_vlan
, (void*)ni_prefs
);
1672 if (mappingBSDNameToInterface
!= NULL
) {
1673 CFRelease(mappingBSDNameToInterface
);
1675 if (mappingServiceBSDNameToInterface
!= NULL
) {
1676 CFRelease(mappingServiceBSDNameToInterface
);
1678 if (allServices
!= NULL
) {
1679 CFRelease(allServices
);
1681 if (allSets
!= NULL
) {
1684 #if !TARGET_OS_IPHONE
1685 if (bsdNameToBridgeServices
!= NULL
) {
1686 CFRelease(bsdNameToBridgeServices
);
1688 if (bsdNameToBondServices
!= NULL
) {
1689 CFRelease(bsdNameToBondServices
);
1691 if (bsdNameToVLANServices
!= NULL
) {
1692 CFRelease(bsdNameToVLANServices
);
1695 if (setServices
!= NULL
) {
1696 CFRelease(setServices
);
1698 if (interfaceToBeRemoved
!= NULL
) {
1699 CFRelease(interfaceToBeRemoved
);
1701 if (interfaceToBeReplaced
!= NULL
) {
1702 CFRelease(interfaceToBeReplaced
);
1704 if (interfacePreserveServiceInformation
!= NULL
) {
1705 CFRelease(interfacePreserveServiceInformation
);
1707 if (bsdNameServiceProtocolPreserveMapping
!= NULL
) {
1708 CFRelease(bsdNameServiceProtocolPreserveMapping
);
1710 if (revertLimitNetworkConfiguration
) {
1711 __SCPreferencesSetLimitSCNetworkConfiguration(prefs
, FALSE
);
1717 _SCNetworkConfigurationCheckValidity(CFURLRef configDir
, CFDictionaryRef options
)
1719 CFURLRef baseURL
= NULL
;
1720 CFURLRef configNetworkInterfaceFile
= NULL
;
1721 CFStringRef configNetworkInterfaceFileString
= NULL
;
1722 SCPreferencesRef configNetworkInterfacePref
= NULL
;
1723 SCPreferencesRef configPref
= NULL
;
1724 CFURLRef configPreferenceFile
= NULL
;
1725 CFStringRef configPreferencesFileString
= NULL
;
1726 Boolean isValid
= FALSE
;
1727 char networkInterfaceStr
[PATH_MAX
];
1728 char prefsStr
[PATH_MAX
];
1730 if (configDir
== NULL
) {
1731 SC_log(LOG_INFO
, "Migration files not found in directory: %@",
1732 (configDir
== NULL
) ? CFSTR("NULL") : CFURLGetString(configDir
));
1735 baseURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
,
1736 kCFURLPOSIXPathStyle
, TRUE
, configDir
);
1738 configPreferenceFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
, sizeof(PREFS_DEFAULT_CONFIG_PLIST
), FALSE
, baseURL
);
1739 configNetworkInterfaceFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)NETWORK_INTERFACES_PREFS_PLIST
, sizeof(NETWORK_INTERFACES_PREFS_PLIST
), FALSE
, baseURL
);
1741 if (!CFURLGetFileSystemRepresentation(configPreferenceFile
, TRUE
, (UInt8
*)prefsStr
, sizeof(prefsStr
))) {
1742 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPreferenceFile
);
1745 if (!CFURLGetFileSystemRepresentation(configNetworkInterfaceFile
, TRUE
, (UInt8
*)networkInterfaceStr
, sizeof(networkInterfaceStr
))) {
1746 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfaceFile
);
1750 configPreferencesFileString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsStr
);
1751 configNetworkInterfaceFileString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceStr
);
1753 configPref
= SCPreferencesCreate(NULL
,
1755 configPreferencesFileString
);
1757 configNetworkInterfacePref
= SCPreferencesCreate(NULL
,
1759 configNetworkInterfaceFileString
);
1760 if ((configPref
== NULL
) || (configNetworkInterfacePref
== NULL
)) {
1764 // This function compares preferences.plist and NetworkInterfaces.plist and verifies if the values are correct
1765 // Checking interface mismatch for validity
1766 isValid
= _SCNetworkConfigurationCheckValidityWithPreferences(configPref
, configNetworkInterfacePref
, options
);
1769 if (baseURL
!= NULL
) {
1772 if (configPreferencesFileString
!= NULL
) {
1773 CFRelease(configPreferencesFileString
);
1775 if (configNetworkInterfaceFileString
!= NULL
) {
1776 CFRelease(configNetworkInterfaceFileString
);
1778 if (configPref
!= NULL
) {
1779 CFRelease(configPref
);
1781 if (configNetworkInterfacePref
!= NULL
) {
1782 CFRelease(configNetworkInterfacePref
);
1784 if (configPreferenceFile
!= NULL
) {
1785 CFRelease(configPreferenceFile
);
1787 if (configNetworkInterfaceFile
!= NULL
) {
1788 CFRelease(configNetworkInterfaceFile
);
1795 CFMutableArrayRef externalInterfaceList
;
1796 CFMutableArrayRef networkInterfaceList
;
1797 Boolean foundNewInterfaces
;
1798 } SCExternalMappingContext
;
1801 _SCNetworkConfigurationCollectInterfaceStorageEntity(const void *key
, const void *value
, void *context
)
1803 SCExternalMappingContext
* ctx
= context
;
1804 CFDictionaryRef interface_entity
= NULL
;
1805 SCNetworkInterfaceRef targetInterface
= (SCNetworkInterfaceRef
)value
;
1807 if (CFArrayContainsValue(ctx
->externalInterfaceList
, CFRangeMake(0, CFArrayGetCount(ctx
->externalInterfaceList
)), targetInterface
)) {
1808 SC_log(LOG_INFO
, "Target interface (%@) already exists, not adding to NetworkInterfaces.plist", targetInterface
);
1809 return; // If the target interface already exists then do not add it to NetworkInterfaces.plist
1811 ctx
->foundNewInterfaces
= TRUE
;
1812 interface_entity
= __SCNetworkInterfaceCopyStorageEntity(targetInterface
);
1814 if (interface_entity
!= NULL
) {
1815 CFArrayAppendValue(ctx
->networkInterfaceList
, interface_entity
);
1816 CFRelease(interface_entity
);
1820 static CFArrayRef
// CFDictionaryRef
1821 _SCNetworkMigrationCreateNetworkInterfaceArray(SCPreferencesRef ni_prefs
, CFDictionaryRef externalMapping
, Boolean
*hasNewInterface
)
1823 SCExternalMappingContext context
;
1825 CFMutableArrayRef externalInterfaceList
= NULL
;
1826 CFArrayRef if_list
= NULL
;
1827 CFDictionaryRef interface_entity
= NULL
;
1828 CFMutableArrayRef networkInterfaceList
= NULL
;
1830 if (ni_prefs
== NULL
) {
1831 SC_log(LOG_INFO
, "No NetworkInterfaces.plist");
1835 if_list
= SCPreferencesGetValue(ni_prefs
, INTERFACES
);
1837 if ((isA_CFArray(if_list
) == NULL
) ||
1838 ((count
= CFArrayGetCount(if_list
)) == 0)) {
1839 SC_log(LOG_INFO
, "No interfaces");
1843 networkInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1845 // Keep the same builtin and external interfaces
1846 for (CFIndex idx
= 0; idx
< count
; idx
++) {
1847 interface_entity
= CFArrayGetValueAtIndex(if_list
, idx
);
1848 if (isA_CFDictionary(interface_entity
) == NULL
) {
1851 CFArrayAppendValue(networkInterfaceList
, interface_entity
);
1854 if (isA_CFDictionary(externalMapping
) == NULL
) {
1855 SC_log(LOG_INFO
, "No external mapping");
1858 // Add any new external interfaces found
1859 externalInterfaceList
= _SCNetworkInterfaceCopyInterfacesFilteredByBuiltinWithPreferences(ni_prefs
, FALSE
);
1860 context
.externalInterfaceList
= externalInterfaceList
;
1861 context
.networkInterfaceList
= networkInterfaceList
;
1862 context
.foundNewInterfaces
= FALSE
;
1864 CFDictionaryApplyFunction(externalMapping
, _SCNetworkConfigurationCollectInterfaceStorageEntity
, &context
);
1866 if (hasNewInterface
!= NULL
) {
1867 *hasNewInterface
= context
.foundNewInterfaces
;
1870 if (externalInterfaceList
!= NULL
) {
1871 CFRelease(externalInterfaceList
);
1873 return networkInterfaceList
;
1877 SCNetworkMigrationMapSourceToTargetName(const void *key
, const void *value
, void *context
)
1879 SCNetworkInterfaceRef interfaceKey
= (SCNetworkInterfaceRef
)key
;
1880 SCNetworkInterfaceRef interfaceValue
= (SCNetworkInterfaceRef
)value
;
1881 CFMutableDictionaryRef mapping
= (CFMutableDictionaryRef
)context
;
1882 CFStringRef sourceBSDName
= NULL
;
1883 CFStringRef targetBSDName
= NULL
;
1885 sourceBSDName
= SCNetworkInterfaceGetBSDName(interfaceKey
);
1886 if (isA_CFString(sourceBSDName
) == NULL
) {
1890 targetBSDName
= SCNetworkInterfaceGetBSDName(interfaceValue
);
1891 if (isA_CFString(targetBSDName
) == NULL
) {
1895 if (!CFDictionaryContainsKey(mapping
, sourceBSDName
)) {
1896 CFDictionaryAddValue(mapping
, sourceBSDName
, targetBSDName
);
1901 static CFDictionaryRef
1902 _SCNetworkMigrationCreateBSDNameMapping(CFDictionaryRef internalMapping
, CFDictionaryRef externalMapping
)
1904 CFMutableDictionaryRef bsdNameMapping
= CFDictionaryCreateMutable(NULL
, 0,
1905 &kCFTypeDictionaryKeyCallBacks
,
1906 &kCFTypeDictionaryValueCallBacks
);
1908 if ((internalMapping
== NULL
) && externalMapping
== NULL
) {
1912 if (internalMapping
!= NULL
) {
1913 CFDictionaryApplyFunction(internalMapping
, SCNetworkMigrationMapSourceToTargetName
, bsdNameMapping
);
1916 if (externalMapping
!= NULL
) {
1917 CFDictionaryApplyFunction(externalMapping
, SCNetworkMigrationMapSourceToTargetName
, bsdNameMapping
);
1921 return bsdNameMapping
;
1925 CFMutableArrayRef mutableServiceArray
;
1926 SCPreferencesRef prefs
;
1927 } SCNetworkServiceArrayCopyContext
;
1929 static CFDictionaryRef
1930 _SCNetworkMigrationCreateServiceSetMapping(SCPreferencesRef prefs
)
1932 CFMutableDictionaryRef serviceSetMapping
= CFDictionaryCreateMutable(NULL
, 0,
1933 &kCFTypeDictionaryKeyCallBacks
,
1934 &kCFTypeDictionaryValueCallBacks
);
1935 SCNetworkServiceRef service
= NULL
;
1936 CFArrayRef services
= NULL
;
1937 CFMutableArrayRef setList
= NULL
;
1938 CFArrayRef sets
= NULL
;
1940 services
= SCNetworkServiceCopyAll(prefs
);
1941 if (services
== NULL
) {
1944 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
1945 service
= CFArrayGetValueAtIndex(services
, idx
);
1947 if (!CFDictionaryContainsKey(serviceSetMapping
, service
)) {
1948 setList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1949 CFDictionaryAddValue(serviceSetMapping
, service
, setList
);
1953 CFRelease(services
);
1955 sets
= SCNetworkSetCopyAll(prefs
);
1960 for (CFIndex idx
= 0; idx
< CFArrayGetCount(sets
); idx
++) {
1961 SCNetworkSetRef set
= CFArrayGetValueAtIndex(sets
, idx
);
1962 services
= SCNetworkSetCopyServices(set
);
1964 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(services
); idx2
++) {
1965 service
= CFArrayGetValueAtIndex(services
, idx2
);
1966 setList
= (CFMutableArrayRef
)CFDictionaryGetValue(serviceSetMapping
, service
);
1967 if (setList
!= NULL
) {
1968 CFArrayAppendValue(setList
, set
);
1971 CFRelease(services
);
1978 return serviceSetMapping
;
1981 static CFDictionaryRef
1982 _SCNetworkMigrationCreateSetMapping(SCPreferencesRef sourcePrefs
,
1983 SCPreferencesRef targetPrefs
)
1985 SCNetworkSetRef currentSourceSet
= NULL
;
1986 CFMutableDictionaryRef setMapping
= NULL
;
1987 CFStringRef setName
;
1988 CFArrayRef sourceSets
= NULL
;
1989 CFIndex targetCount
;
1990 SCNetworkSetRef targetSet
;
1991 CFArrayRef targetSets
= NULL
;
1992 CFMutableArrayRef targetSetsMutable
= NULL
;
1994 sourceSets
= SCNetworkSetCopyAll(sourcePrefs
);
1995 targetSets
= SCNetworkSetCopyAll(targetPrefs
);
1997 if (sourceSets
== NULL
||
1998 targetSets
== NULL
) {
2001 targetSetsMutable
= CFArrayCreateMutableCopy(NULL
, 0, targetSets
);
2002 targetCount
= CFArrayGetCount(targetSetsMutable
);
2004 setMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2006 currentSourceSet
= SCNetworkSetCopyCurrent(sourcePrefs
);
2008 // Mapping the current source set to the first target set, and setting it as current set
2009 if (currentSourceSet
!= NULL
) {
2010 if (targetCount
> 0) {
2011 targetSet
= CFArrayGetValueAtIndex(targetSetsMutable
, 0);
2012 CFRetain(targetSet
);
2013 CFArrayRemoveValueAtIndex(targetSetsMutable
, 0);
2015 setName
= SCNetworkSetGetName(currentSourceSet
);
2016 SCNetworkSetSetName(targetSet
, setName
);
2017 CFDictionaryAddValue(setMapping
, currentSourceSet
, targetSet
);
2018 SCNetworkSetSetCurrent(targetSet
);
2019 CFRelease(targetSet
);
2022 SC_log(LOG_ERR
, "Number of sets in the target should be at least 1, but is found to be %ld", targetCount
);
2027 for (CFIndex idx
= 0; idx
< CFArrayGetCount(sourceSets
); idx
++) {
2028 SCNetworkSetRef sourceSet
= CFArrayGetValueAtIndex(sourceSets
, idx
);
2030 if ((currentSourceSet
!= NULL
) && CFEqual(sourceSet
, currentSourceSet
)) {
2034 targetCount
= CFArrayGetCount(targetSetsMutable
);
2035 setName
= SCNetworkSetGetName(sourceSet
);
2037 if (targetCount
> 0) {
2038 targetSet
= CFArrayGetValueAtIndex(targetSetsMutable
, 0);
2039 CFRetain(targetSet
);
2040 CFArrayRemoveValueAtIndex(targetSetsMutable
, 0);
2043 targetSet
= SCNetworkSetCreate(targetPrefs
);
2045 SCNetworkSetSetName(targetSet
, setName
);
2046 CFDictionaryAddValue(setMapping
, sourceSet
, targetSet
);
2048 CFRelease(targetSet
);
2052 if (sourceSets
!= NULL
) {
2053 CFRelease(sourceSets
);
2055 if (targetSets
!= NULL
) {
2056 CFRelease(targetSets
);
2058 if (targetSetsMutable
!= NULL
) {
2059 CFRelease(targetSetsMutable
);
2061 if (currentSourceSet
!= NULL
) {
2062 CFRelease(currentSourceSet
);
2067 // This function finds the mapping between source and target preferences (SCNetworkServicesRef -> SCNetworkServicesRef)
2068 // If there is no mapping found between source and target preferences, then the CFBooleanRef value indicating no value is found is stored (SCNetworkServicesRef -> kCFBooleanFalse)
2069 static CFDictionaryRef
2070 _SCNetworkMigrationCreateServiceMappingUsingBSDMapping(SCPreferencesRef sourcePrefs
,
2071 SCPreferencesRef targetPrefs
,
2072 CFDictionaryRef bsdNameMapping
)
2074 CFStringRef bsdNameMapTarget
= NULL
;
2075 CFMutableDictionaryRef serviceMapping
= NULL
; // Mapping of services between source and target configurations
2076 CFStringRef sourceBSDName
= NULL
;
2077 CFIndex sourceCount
= 0;
2078 SCNetworkInterfaceRef sourceInterface
= NULL
;
2079 CFStringRef sourceInterfaceSubType
= NULL
; // Check interface type and subtype to be able to transfer VPN
2080 CFStringRef sourceInterfaceType
= NULL
;
2081 CFArrayRef sourceSCNetworkServices
= NULL
;
2082 CFMutableArrayRef sourceSCNetworkServicesMutable
= NULL
; // Source SCNetworkServiceRef mutable array
2083 SCNetworkServiceRef sourceService
= NULL
;
2084 CFStringRef targetBSDName
= NULL
;
2085 CFIndex targetCount
= 0; // Count of Source and Target Services
2086 SCNetworkInterfaceRef targetInterface
= NULL
;
2087 CFStringRef targetInterfaceSubType
= NULL
; // services during migration
2088 CFStringRef targetInterfaceType
= NULL
;
2089 CFArrayRef targetSCNetworkServices
= NULL
;
2090 CFMutableArrayRef targetSCNetworkServicesMutable
= NULL
; // Target SCNetworkServiceRef mutable array
2091 SCNetworkServiceRef targetService
= NULL
;
2093 // We need BSD Mapping to successfully create service mapping
2094 if (bsdNameMapping
== NULL
) {
2095 SC_log(LOG_INFO
, "No BSD name mapping");
2098 sourceSCNetworkServices
= SCNetworkServiceCopyAll(sourcePrefs
);
2099 if (isA_CFArray(sourceSCNetworkServices
) == NULL
) {
2100 SC_log(LOG_INFO
, "No source network services");
2103 targetSCNetworkServices
= SCNetworkServiceCopyAll(targetPrefs
);
2104 if (isA_CFArray(targetSCNetworkServices
) == NULL
) {
2105 SC_log(LOG_INFO
, "No target network services");
2109 sourceCount
= CFArrayGetCount(sourceSCNetworkServices
);
2111 sourceSCNetworkServicesMutable
= CFArrayCreateMutableCopy(NULL
, 0, sourceSCNetworkServices
);
2112 targetSCNetworkServicesMutable
= CFArrayCreateMutableCopy(NULL
, 0, targetSCNetworkServices
);
2114 serviceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2116 for (CFIndex idx
= 0; idx
< sourceCount
; idx
++) {
2117 sourceBSDName
= NULL
;
2118 sourceService
= NULL
;
2119 sourceInterface
= NULL
;
2120 sourceInterfaceType
= NULL
;
2121 sourceInterfaceSubType
= NULL
;
2122 bsdNameMapTarget
= NULL
;
2124 targetCount
= CFArrayGetCount(targetSCNetworkServicesMutable
);
2125 sourceService
= (SCNetworkServiceRef
) CFArrayGetValueAtIndex(sourceSCNetworkServicesMutable
, idx
);
2127 sourceInterface
= SCNetworkServiceGetInterface(sourceService
);
2128 if (sourceInterface
== NULL
) {
2129 SC_log(LOG_INFO
, "No source interface");
2133 sourceInterfaceType
= __SCNetworkInterfaceGetEntityType(sourceInterface
);
2134 if ((isA_CFString(sourceInterfaceType
) != NULL
) &&
2135 (CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeVPN
) ||
2136 CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypePPP
))) {
2137 sourceInterfaceSubType
= __SCNetworkInterfaceGetEntitySubType(sourceInterface
);
2138 if (isA_CFString(sourceInterfaceSubType
) == NULL
) {
2139 SC_log(LOG_INFO
, "No source interface SubType");
2143 else if ((isA_CFString(sourceInterfaceType
) &&
2144 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeIPSec
) &&
2145 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceType6to4
) &&
2146 !CFEqual(sourceInterfaceType
, kSCValNetInterfaceTypeLoopback
)) ||
2147 !isA_CFString(sourceInterfaceType
)) {
2148 sourceBSDName
= SCNetworkInterfaceGetBSDName(sourceInterface
);
2149 if (!isA_CFString(sourceBSDName
) ||
2150 !CFDictionaryContainsKey(bsdNameMapping
, sourceBSDName
)) {
2151 SC_log(LOG_INFO
, "No BSD name mapping for %@",
2152 (sourceBSDName
== NULL
) ? CFSTR("NULL") : sourceBSDName
);
2156 bsdNameMapTarget
= CFDictionaryGetValue(bsdNameMapping
, sourceBSDName
);
2157 if (isA_CFString(bsdNameMapTarget
) == NULL
) {
2158 SC_log(LOG_INFO
, "No BSD name mapping target");
2162 // Find the bsd name in target service
2163 for (CFIndex idx2
= 0; idx2
< targetCount
; idx2
++) {
2164 targetService
= NULL
;
2165 targetInterface
= NULL
;
2166 targetBSDName
= NULL
;
2167 targetInterfaceType
= NULL
;
2168 targetInterfaceSubType
= NULL
;
2170 targetService
= (SCNetworkServiceRef
) CFArrayGetValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2172 targetInterface
= SCNetworkServiceGetInterface(targetService
);
2173 if (targetInterface
== NULL
) {
2174 SC_log(LOG_INFO
, "No target interface");
2177 SC_log(LOG_INFO
, "targetInterface: %@", targetInterface
);
2178 if (sourceBSDName
!= NULL
) {
2179 targetBSDName
= SCNetworkInterfaceGetBSDName(targetInterface
);
2180 if (isA_CFString(targetBSDName
) == NULL
) {
2181 SC_log(LOG_INFO
, "No target BSD name");
2185 if (CFEqual(targetBSDName
, bsdNameMapTarget
)) {
2186 SC_log(LOG_INFO
, "Removing target BSD name: %@", targetBSDName
);
2187 CFDictionaryAddValue(serviceMapping
, sourceService
, targetService
);
2188 CFArrayRemoveValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2193 // Source Interface Type should be VPN
2194 targetInterfaceType
= __SCNetworkInterfaceGetEntityType(targetInterface
);
2195 if ((isA_CFString(targetInterfaceType
) == NULL
) ||
2196 (!CFEqual(targetInterfaceType
, kSCValNetInterfaceTypeVPN
) &&
2197 !CFEqual(targetInterfaceType
, kSCValNetInterfaceTypePPP
))) {
2198 SC_log(LOG_INFO
, "Unexpected target interface type: %@",
2199 (targetInterfaceType
!= NULL
) ? targetInterfaceType
: CFSTR("NULL"));
2202 targetInterfaceSubType
= __SCNetworkInterfaceGetEntitySubType(targetInterface
);
2203 if (isA_CFString(targetInterfaceSubType
) == NULL
) {
2204 SC_log(LOG_INFO
, "No target interface SubType");
2208 // Check if the target interface type and the target interface sub type match
2209 if (CFEqual(targetInterfaceType
, sourceInterfaceType
) &&
2210 CFEqual(targetInterfaceSubType
, sourceInterfaceSubType
)) {
2211 SC_log(LOG_INFO
, "Removing target BSD Name: %@ for VPN", targetBSDName
);
2212 CFDictionaryAddValue(serviceMapping
, sourceService
, targetService
);
2213 CFArrayRemoveValueAtIndex(targetSCNetworkServicesMutable
, idx2
);
2218 // Check if sourceService has found a mapping or not, if not the create a NULL mapping to indicate
2219 // the this service needs to be added and not replaced
2220 if (!CFDictionaryContainsKey(serviceMapping
, sourceService
)) {
2221 SC_log(LOG_INFO
, "Service needs to be added: %@", sourceService
);
2222 CFDictionaryAddValue(serviceMapping
, sourceService
, kCFBooleanFalse
);
2226 if (sourceSCNetworkServices
!= NULL
) {
2227 CFRelease(sourceSCNetworkServices
);
2229 if (targetSCNetworkServices
!= NULL
) {
2230 CFRelease(targetSCNetworkServices
);
2232 if (sourceSCNetworkServicesMutable
!= NULL
) {
2233 CFRelease(sourceSCNetworkServicesMutable
);
2235 if (targetSCNetworkServicesMutable
!= NULL
) {
2236 CFRelease(targetSCNetworkServicesMutable
);
2238 return serviceMapping
;
2242 SCPreferencesRef targetPrefs
;
2243 CFDictionaryRef bsdMapping
;
2244 CFDictionaryRef setMapping
;
2245 CFDictionaryRef serviceSetMapping
;
2246 } ServiceMigrationContext
;
2249 // SCNetworkServiceRef: if target service needs replacement
2250 // CFBooleanRef: if target service is not present
2252 ServiceMigrationAddOrReplace(const void *key
, const void *value
, void *context
)
2254 CFDictionaryRef bsdMapping
= NULL
;
2255 ServiceMigrationContext
*ctx
= (ServiceMigrationContext
*)context
;
2256 CFDictionaryRef setMapping
;
2257 CFDictionaryRef sourceServiceSetMapping
;
2258 SCNetworkServiceRef sourceService
= (SCNetworkServiceRef
)key
;
2259 SCPreferencesRef targetPrefs
= NULL
;
2260 SCNetworkServiceRef targetService
= (SCNetworkServiceRef
)value
;
2262 targetPrefs
= ctx
->targetPrefs
;
2263 bsdMapping
= ctx
->bsdMapping
;
2264 setMapping
= ctx
->setMapping
;
2265 sourceServiceSetMapping
= ctx
->serviceSetMapping
;
2267 if ((setMapping
!= NULL
|| sourceServiceSetMapping
!= NULL
)) {
2268 if (isA_SCNetworkService(targetService
) != NULL
) {
2269 SC_log(LOG_INFO
, "Removing target service: %@", targetService
);
2270 SCNetworkServiceRemove(targetService
);
2273 SC_log(LOG_INFO
, "Adding service: %@", sourceService
);
2274 if (__SCNetworkServiceMigrateNew(targetPrefs
, sourceService
, bsdMapping
, setMapping
, sourceServiceSetMapping
) == FALSE
) {
2275 SC_log(LOG_INFO
, "Could not add service: %@", sourceService
);
2280 _SCNetworkMigrationDoServiceMigration(SCPreferencesRef sourcePrefs
, SCPreferencesRef targetPrefs
,
2281 CFDictionaryRef serviceMapping
, CFDictionaryRef bsdMapping
,
2282 CFDictionaryRef setMapping
, CFDictionaryRef serviceSetMapping
)
2284 ServiceMigrationContext context
;
2285 Boolean success
= FALSE
;
2287 if ((sourcePrefs
== NULL
) ||
2288 (targetPrefs
== NULL
) ||
2289 (isA_CFDictionary(serviceMapping
) == NULL
) ||
2290 (isA_CFDictionary(bsdMapping
) == NULL
)) {
2291 SC_log(LOG_INFO
, "No sourcePrefs, targetPrefs, serviceMapping, or bsdMapping");
2294 context
.targetPrefs
= targetPrefs
;
2295 context
.bsdMapping
= bsdMapping
;
2296 context
.setMapping
= setMapping
;
2297 context
.serviceSetMapping
= serviceSetMapping
;
2299 CFDictionaryApplyFunction(serviceMapping
, ServiceMigrationAddOrReplace
, &context
);
2307 _SCNetworkMigrationDoSystemMigration(SCPreferencesRef sourcePrefs
, SCPreferencesRef targetPrefs
)
2309 CFStringEncoding nameEncoding
;
2310 CFStringRef computerName
;
2311 CFStringRef hostname
;
2312 CFStringRef localHostname
;
2313 CFDictionaryRef btmm
= NULL
;
2314 CFDictionaryRef btmmDSID
= NULL
;
2315 CFStringRef btmmDSIDPath
;
2316 CFStringRef btmmPath
;
2319 if ((sourcePrefs
== NULL
) ||
2320 (targetPrefs
== NULL
)) {
2324 hostname
= SCPreferencesGetHostName(sourcePrefs
);
2325 if (hostname
!= NULL
) {
2326 SCPreferencesSetHostName(targetPrefs
, hostname
);
2329 localHostname
= _SCPreferencesCopyLocalHostName(sourcePrefs
);
2330 if (localHostname
!= NULL
) {
2331 SCPreferencesSetLocalHostName(targetPrefs
, localHostname
);
2332 CFRelease(localHostname
);
2335 computerName
= _SCPreferencesCopyComputerName(sourcePrefs
, &nameEncoding
);
2337 if (computerName
!= NULL
) {
2338 SCPreferencesSetComputerName(targetPrefs
, computerName
, nameEncoding
);
2339 CFRelease(computerName
);
2342 btmmPath
= CFStringCreateWithFormat(NULL
, NULL
,
2347 btmm
= SCPreferencesPathGetValue(sourcePrefs
, btmmPath
);
2350 SCPreferencesPathSetValue(targetPrefs
, btmmPath
, btmm
);
2352 CFRelease(btmmPath
);
2354 btmmDSIDPath
= CFStringCreateWithFormat(NULL
, NULL
,
2358 BACK_TO_MY_MAC_DSIDS
);
2360 btmmDSID
= SCPreferencesPathGetValue(sourcePrefs
, btmmDSIDPath
);
2361 if (btmmDSID
!= NULL
) {
2362 SCPreferencesPathSetValue(targetPrefs
, btmmDSIDPath
, btmmDSID
);
2364 CFRelease(btmmDSIDPath
);
2368 #if !TARGET_OS_IPHONE
2371 CFMutableArrayRef interfaceList
;
2372 SCPreferencesRef ni_prefs
;
2373 CFDictionaryRef bsdMapping
;
2374 } SCVirtualInterfaceMemberListContext
;
2377 SCPreferencesRef prefs
;
2378 SCPreferencesRef ni_prefs
;
2379 CFDictionaryRef bsdMapping
;
2380 CFDictionaryRef virtualBSDMapping
;
2381 CFDictionaryRef mappingBSDNameToService
;
2382 CFDictionaryRef setMapping
;
2383 CFDictionaryRef serviceSetMapping
;
2384 } SCVirtualInterfaceContext
;
2387 add_virtual_interface(const void *value
, void *context
)
2389 SCVirtualInterfaceMemberListContext
*ctx
= (SCVirtualInterfaceMemberListContext
*)context
;
2390 CFMutableArrayRef interfaceList
= ctx
->interfaceList
;
2391 CFDictionaryRef bsdMapping
= ctx
->bsdMapping
;
2392 CFStringRef oldInterfaceBSDName
= (CFStringRef
)value
;
2393 SCNetworkInterfaceRef newInterface
;
2394 CFStringRef newInterfaceBSDName
;
2396 SC_log(LOG_INFO
, "old BSD interface name: %@", oldInterfaceBSDName
);
2398 newInterfaceBSDName
= CFDictionaryGetValue(bsdMapping
, oldInterfaceBSDName
);
2399 if (newInterfaceBSDName
== NULL
) {
2402 SC_log(LOG_INFO
, "new BSD interface name: %@", newInterfaceBSDName
);
2404 newInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ctx
->ni_prefs
, newInterfaceBSDName
);
2405 if (newInterface
!= NULL
) {
2406 SC_log(LOG_INFO
, "Adding interface to interfaceList: %@", newInterface
);
2407 CFArrayAppendValue(interfaceList
, newInterface
);
2408 CFRelease(newInterface
);
2414 add_target_bridge(const void *key
, const void *value
, void *context
)
2416 CFStringRef bridgeName
;
2417 CFDictionaryRef bridgeOptions
;
2418 SCVirtualInterfaceContext
*ctx
= (SCVirtualInterfaceContext
*)context
;
2419 CFDictionaryRef bridgeBSDNameMapping
= ctx
->virtualBSDMapping
;
2420 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
2421 SCVirtualInterfaceMemberListContext memberListContext
;
2422 CFMutableArrayRef newInterfaceList
;
2423 SCBridgeInterfaceRef newBridge
;
2424 SCBridgeInterfaceRef oldBridge
= (SCBridgeInterfaceRef
)key
;
2425 CFStringRef oldBSDName
;
2426 CFArrayRef oldInterfaceList
= (CFArrayRef
)value
;
2427 CFArrayRef oldServiceList
;
2428 SCPreferencesRef prefs
= ctx
->prefs
;
2429 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
2430 CFDictionaryRef setMapping
= ctx
->setMapping
;
2432 newInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2434 memberListContext
.bsdMapping
= ctx
->bsdMapping
;
2435 memberListContext
.interfaceList
= newInterfaceList
;
2436 memberListContext
.ni_prefs
= ctx
->ni_prefs
;
2438 CFArrayApplyFunction(oldInterfaceList
, CFRangeMake(0, CFArrayGetCount(oldInterfaceList
)), add_virtual_interface
, &memberListContext
);
2440 newBridge
= SCBridgeInterfaceCreate(prefs
);
2442 if (!__SCBridgeInterfaceSetMemberInterfaces(newBridge
, newInterfaceList
)) {
2443 SC_log(LOG_INFO
, "__SCBridgeInterfaceSetMemberInterfaces() failed");
2445 CFRelease(newInterfaceList
);
2447 bridgeOptions
= SCBridgeInterfaceGetOptions(oldBridge
);
2448 if (bridgeOptions
!= NULL
) {
2449 SCBridgeInterfaceSetOptions(newBridge
, bridgeOptions
);
2452 bridgeName
= SCNetworkInterfaceGetLocalizedDisplayName(oldBridge
);
2454 if (bridgeName
!= NULL
) {
2455 SCBridgeInterfaceSetLocalizedDisplayName(newBridge
, bridgeName
);
2458 oldBSDName
= SCNetworkInterfaceGetBSDName(oldBridge
);
2459 if (oldBSDName
== NULL
) {
2463 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
2464 if (oldServiceList
== NULL
) {
2468 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
2469 SCNetworkServiceRef oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
2470 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, bridgeBSDNameMapping
, setMapping
, serviceSetMapping
)) {
2471 SC_log(LOG_INFO
, "Could not migrate Bridge service: %@", oldService
);
2475 CFRelease(newBridge
);
2479 _SCNetworkMigrationRemoveBridgeServices(SCPreferencesRef prefs
)
2481 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2483 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2484 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2485 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2486 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2488 if ((bsdName
!= NULL
) &&
2489 (SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBridge
)) {
2490 SC_log(LOG_INFO
, "Removing service: %@", service
);
2491 SCNetworkServiceRemove(service
);
2494 CFRelease(services
);
2498 static CFDictionaryRef
2499 _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(SCPreferencesRef prefs
)
2501 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2502 CFMutableDictionaryRef bridgeServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2504 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2505 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2506 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2507 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2509 if ((bsdName
!= NULL
) &&
2510 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBridge
) {
2511 CFMutableArrayRef serviceList
;
2512 if (!CFDictionaryContainsKey(bridgeServices
, bsdName
)) {
2513 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2514 CFDictionaryAddValue(bridgeServices
, bsdName
, serviceList
);
2515 CFRelease(serviceList
);
2517 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(bridgeServices
, bsdName
);
2518 CFArrayAppendValue(serviceList
, service
);
2521 CFRelease(services
);
2522 return bridgeServices
;
2527 _SCNetworkMigrationDoBridgeMigration (SCPreferencesRef sourcePrefs
,
2528 SCPreferencesRef sourceNIPrefs
,
2529 SCPreferencesRef targetPrefs
,
2530 SCPreferencesRef targetNIPrefs
,
2531 CFDictionaryRef bsdMapping
,
2532 CFDictionaryRef setMapping
,
2533 CFDictionaryRef serviceSetMapping
)
2535 CFArrayRef allSourceBridges
;
2536 CFArrayRef allTargetBridges
;
2537 SCBridgeInterfaceRef bridge
;
2538 CFMutableDictionaryRef bridgeInterfaceMapping
= NULL
;
2539 CFMutableDictionaryRef bridgeMapping
;
2540 CFDictionaryRef bsdNameToBridgeServices
;
2541 SCVirtualInterfaceContext context
;
2543 Boolean success
= FALSE
;
2545 allSourceBridges
= SCBridgeInterfaceCopyAll(sourcePrefs
);
2546 allTargetBridges
= SCBridgeInterfaceCopyAll(targetPrefs
);
2548 bsdNameToBridgeServices
= _SCNetworkMigrationCopyMappingBSDNameToBridgeServices(sourcePrefs
);
2550 bridgeInterfaceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2551 bridgeMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2553 // Create Bridge Interface Mapping
2554 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceBridges
); idx
++) {
2555 bridge
= CFArrayGetValueAtIndex(allSourceBridges
, idx
);
2556 CFArrayRef bridgeMembers
= SCBridgeInterfaceGetMemberInterfaces(bridge
);
2557 CFMutableArrayRef interfaceList
;
2559 interfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2560 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(bridgeMembers
); idx2
++) {
2561 CFStringRef interfaceName
= NULL
;
2562 SCNetworkInterfaceRef interface
= NULL
;
2564 interface
= CFArrayGetValueAtIndex(bridgeMembers
, idx2
);
2565 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
2567 if (CFDictionaryContainsKey(bsdMapping
, interfaceName
)) {
2568 CFStringRef bridgeNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("bridge%ld"), count
);
2569 CFDictionaryAddValue(bridgeMapping
, interfaceName
, bridgeNewName
);
2570 CFArrayAppendValue(interfaceList
, interfaceName
);
2571 CFRelease(bridgeNewName
);
2575 if (CFArrayGetCount(interfaceList
) > 0) {
2576 CFDictionaryAddValue(bridgeInterfaceMapping
, bridge
, interfaceList
);
2578 CFRelease(interfaceList
);
2580 // Remove bridge services from target
2581 _SCNetworkMigrationRemoveBridgeServices(targetPrefs
);
2583 // Remove Target Bridges
2584 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetBridges
); idx
++) {
2585 bridge
= CFArrayGetValueAtIndex(allTargetBridges
, idx
);
2586 if (!SCBridgeInterfaceRemove(bridge
)) {
2587 SC_log(LOG_INFO
, "SCBridgeInterfaceRemove() failed: %@", bridge
);
2592 context
.prefs
= targetPrefs
;
2593 context
.ni_prefs
= targetNIPrefs
;
2594 context
.bsdMapping
= bsdMapping
;
2595 context
.virtualBSDMapping
= bridgeMapping
;
2596 context
.mappingBSDNameToService
= bsdNameToBridgeServices
;
2597 context
.setMapping
= setMapping
;
2598 context
.serviceSetMapping
= serviceSetMapping
;
2600 // Add Bridge configurations at the target using mapping
2601 CFDictionaryApplyFunction(bridgeInterfaceMapping
, add_target_bridge
, &context
);
2605 CFRelease(allSourceBridges
);
2606 CFRelease(allTargetBridges
);
2607 CFRelease(bridgeInterfaceMapping
);
2608 CFRelease(bridgeMapping
);
2609 CFRelease(bsdNameToBridgeServices
);
2615 add_target_bond(const void *key
, const void *value
, void *context
)
2617 CFNumberRef bondMode
;
2618 CFStringRef bondName
;
2619 CFDictionaryRef bondOptions
;
2620 SCVirtualInterfaceContext
*ctx
= (SCVirtualInterfaceContext
*)context
;
2621 CFDictionaryRef bondBSDNameMapping
= ctx
->virtualBSDMapping
;
2622 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
2623 SCVirtualInterfaceMemberListContext memberListContext
;
2624 CFMutableArrayRef newInterfaceList
;
2625 SCBondInterfaceRef newBond
;
2626 SCBondInterfaceRef oldBond
= (SCBondInterfaceRef
)key
;
2627 CFStringRef oldBSDName
;
2628 CFArrayRef oldInterfaceList
= (CFArrayRef
)value
;
2629 CFArrayRef oldServiceList
;
2630 SCPreferencesRef prefs
= ctx
->prefs
;
2631 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
2632 CFDictionaryRef setMapping
= ctx
->setMapping
;
2634 newInterfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2636 memberListContext
.bsdMapping
= ctx
->bsdMapping
;
2637 memberListContext
.interfaceList
= newInterfaceList
;
2638 memberListContext
.ni_prefs
= ctx
->ni_prefs
;
2640 CFArrayApplyFunction(oldInterfaceList
, CFRangeMake(0, CFArrayGetCount(oldInterfaceList
)), add_virtual_interface
, &memberListContext
);
2642 newBond
= SCBondInterfaceCreate(prefs
);
2643 if (!__SCBondInterfaceSetMemberInterfaces(newBond
, newInterfaceList
)) {
2644 SC_log(LOG_INFO
, "__SCBondInterfaceSetMemberInterfaces() failed");
2646 CFRelease(newInterfaceList
);
2648 bondOptions
= SCBondInterfaceGetOptions(oldBond
);
2649 if (bondOptions
!= NULL
) {
2650 SCBondInterfaceSetOptions(newBond
, bondOptions
);
2653 bondName
= SCNetworkInterfaceGetLocalizedDisplayName(oldBond
);
2654 if (bondName
!= NULL
) {
2655 SCBondInterfaceSetLocalizedDisplayName(newBond
, bondName
);
2658 bondMode
= SCBondInterfaceGetMode(oldBond
);
2659 if (bondMode
!= NULL
) {
2660 SCBondInterfaceSetMode(newBond
, bondMode
);
2662 oldBSDName
= SCNetworkInterfaceGetBSDName(oldBond
);
2663 if (oldBSDName
== NULL
) {
2667 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
2668 if (oldServiceList
== NULL
) {
2672 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
2673 SCNetworkServiceRef oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
2674 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, bondBSDNameMapping
, setMapping
, serviceSetMapping
)) {
2675 SC_log(LOG_INFO
, "Could not migrate Bond service: %@", oldService
);
2683 _SCNetworkMigrationRemoveBondServices(SCPreferencesRef prefs
)
2685 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2687 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2688 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2689 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2690 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2692 if ((bsdName
!= NULL
) &&
2693 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBond
) {
2694 SCNetworkServiceRemove(service
);
2697 CFRelease(services
);
2701 static CFDictionaryRef
2702 _SCNetworkMigrationCopyMappingBSDNameToBondServices(SCPreferencesRef prefs
)
2704 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2705 CFMutableDictionaryRef bondServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2707 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2708 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2709 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2710 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2712 if ((bsdName
!= NULL
) &&
2713 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeBond
) {
2714 CFMutableArrayRef serviceList
;
2715 if (!CFDictionaryContainsKey(bondServices
, bsdName
)) {
2716 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2717 CFDictionaryAddValue(bondServices
, bsdName
, serviceList
);
2718 CFRelease(serviceList
);
2720 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(bondServices
, bsdName
);
2721 CFArrayAppendValue(serviceList
, service
);
2724 CFRelease(services
);
2725 return bondServices
;
2730 _SCNetworkMigrationDoBondMigration (SCPreferencesRef sourcePrefs
,
2731 SCPreferencesRef sourceNIPrefs
,
2732 SCPreferencesRef targetPrefs
,
2733 SCPreferencesRef targetNIPrefs
,
2734 CFDictionaryRef bsdMapping
,
2735 CFDictionaryRef setMapping
,
2736 CFDictionaryRef serviceSetMapping
)
2738 CFArrayRef allSourceBonds
;
2739 CFArrayRef allTargetBonds
;
2740 SCBondInterfaceRef bond
;
2741 CFMutableDictionaryRef bondInterfaceMapping
= NULL
;
2742 CFMutableDictionaryRef bondMapping
;
2743 CFDictionaryRef bsdNameToBondServices
;
2744 SCVirtualInterfaceContext context
;
2746 Boolean success
= FALSE
;
2748 allSourceBonds
= SCBondInterfaceCopyAll(sourcePrefs
);
2749 allTargetBonds
= SCBondInterfaceCopyAll(targetPrefs
);
2751 bsdNameToBondServices
= _SCNetworkMigrationCopyMappingBSDNameToBondServices(sourcePrefs
);
2753 bondInterfaceMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2754 bondMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2755 // Create Bond Interface mapping
2756 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceBonds
); idx
++) {
2757 bond
= CFArrayGetValueAtIndex(allSourceBonds
, idx
);
2758 CFArrayRef bondMembers
= SCBondInterfaceGetMemberInterfaces(bond
);
2759 CFMutableArrayRef interfaceList
;
2761 interfaceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2762 for (CFIndex idx2
= 0; idx2
< CFArrayGetCount(bondMembers
); idx2
++) {
2763 CFStringRef interfaceName
;
2764 SCNetworkInterfaceRef interface
;
2766 interface
= CFArrayGetValueAtIndex(bondMembers
, idx2
);
2767 interfaceName
= SCNetworkInterfaceGetBSDName(interface
);
2769 if (CFDictionaryContainsKey(bsdMapping
, interfaceName
)) {
2770 CFStringRef bondNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("bond%ld"), count
);
2771 CFDictionaryAddValue(bondMapping
, interfaceName
, bondNewName
);
2772 CFArrayAppendValue(interfaceList
, interfaceName
);
2773 CFRelease(bondNewName
);
2777 if (CFArrayGetCount(interfaceList
) > 0) {
2778 CFDictionaryAddValue(bondInterfaceMapping
, bond
, interfaceList
);
2780 CFRelease(interfaceList
);
2782 // Remove bond services from target
2783 _SCNetworkMigrationRemoveBondServices(targetPrefs
);
2785 // Remove Target Bonds
2786 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetBonds
); idx
++) {
2787 bond
= CFArrayGetValueAtIndex(allTargetBonds
, idx
);
2788 if (!SCBondInterfaceRemove(bond
)) {
2789 SC_log(LOG_INFO
, "SCBondInterfaceRemove() failed: %@", bond
);
2794 context
.prefs
= targetPrefs
;
2795 context
.ni_prefs
= targetNIPrefs
;
2796 context
.bsdMapping
= bsdMapping
;
2797 context
.virtualBSDMapping
= bondMapping
;
2798 context
.mappingBSDNameToService
= bsdNameToBondServices
;
2799 context
.setMapping
= setMapping
;
2800 context
.serviceSetMapping
= serviceSetMapping
;
2802 // Add Bond configurations at the target using mapping
2803 CFDictionaryApplyFunction(bondInterfaceMapping
, add_target_bond
, &context
);
2807 CFRelease(allSourceBonds
);
2808 CFRelease(allTargetBonds
);
2809 CFRelease(bondInterfaceMapping
);
2810 CFRelease(bondMapping
);
2811 CFRelease(bsdNameToBondServices
);
2816 add_target_vlan(const void *value
, void *context
)
2818 CFDictionaryRef bsdMapping
;
2819 SCVirtualInterfaceContext
*ctx
= (SCVirtualInterfaceContext
*)context
;
2820 CFDictionaryRef bsdNameToServiceMapping
= ctx
->mappingBSDNameToService
;
2821 SCPreferencesRef prefs
= ctx
->prefs
;
2822 SCVLANInterfaceRef newVLAN
= NULL
;
2823 SCNetworkInterfaceRef newPhysicalInterface
= NULL
;
2824 CFStringRef newPhysicalInterfaceName
;
2825 SCVLANInterfaceRef oldVLAN
= (SCVLANInterfaceRef
)value
;
2826 CFStringRef oldBSDName
;
2827 SCNetworkInterfaceRef oldPhysicalInterface
;
2828 CFStringRef oldPhysicalInterfaceName
;
2829 SCNetworkServiceRef oldService
;
2830 CFArrayRef oldServiceList
;
2831 CFDictionaryRef serviceSetMapping
= ctx
->serviceSetMapping
;
2832 CFDictionaryRef setMapping
= ctx
->setMapping
;
2833 CFDictionaryRef vlanBSDMapping
= ctx
->virtualBSDMapping
;
2834 CFNumberRef vlanTag
;
2835 CFStringRef vlanName
;
2836 CFDictionaryRef vlanOptions
;
2838 bsdMapping
= ctx
->bsdMapping
;
2840 oldPhysicalInterface
= SCVLANInterfaceGetPhysicalInterface(oldVLAN
);
2841 if (oldPhysicalInterface
== NULL
) {
2842 SC_log(LOG_INFO
, "No old VLAN physical interface");
2846 oldPhysicalInterfaceName
= SCNetworkInterfaceGetBSDName(oldPhysicalInterface
);
2847 if (oldPhysicalInterfaceName
== NULL
) {
2848 SC_log(LOG_INFO
, "No old VLAN physical interface name");
2852 newPhysicalInterfaceName
= CFDictionaryGetValue(bsdMapping
, oldPhysicalInterfaceName
);
2853 if (newPhysicalInterfaceName
== NULL
) {
2854 SC_log(LOG_INFO
, "No new VLAN physical interface name");
2857 newPhysicalInterface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ctx
->ni_prefs
, newPhysicalInterfaceName
);
2858 if (newPhysicalInterface
== NULL
) {
2859 SC_log(LOG_INFO
, "Could not create new VLAN physical interface");
2863 vlanTag
= SCVLANInterfaceGetTag(oldVLAN
);
2864 if (vlanTag
== NULL
) {
2865 SC_log(LOG_INFO
, "No old VLAN interface tag");
2869 newVLAN
= SCVLANInterfaceCreate(prefs
, newPhysicalInterface
, vlanTag
);
2870 if (newVLAN
== NULL
) {
2871 SC_log(LOG_INFO
, "Could not create new VLAN interface");
2875 vlanName
= SCNetworkInterfaceGetLocalizedDisplayName(oldVLAN
);
2876 if (vlanName
!= NULL
) {
2877 SCVLANInterfaceSetLocalizedDisplayName(newVLAN
, vlanName
);
2880 vlanOptions
= SCVLANInterfaceGetOptions(oldVLAN
);
2881 if (vlanOptions
!= NULL
) {
2882 SCVLANInterfaceSetOptions(newVLAN
, vlanOptions
);
2884 oldBSDName
= SCNetworkInterfaceGetBSDName(oldVLAN
);
2886 if (oldBSDName
== NULL
) {
2890 oldServiceList
= CFDictionaryGetValue(bsdNameToServiceMapping
, oldBSDName
);
2891 if (oldServiceList
== NULL
) {
2895 for (CFIndex idx
= 0; idx
< CFArrayGetCount(oldServiceList
); idx
++) {
2896 oldService
= CFArrayGetValueAtIndex(oldServiceList
, idx
);
2897 if (!__SCNetworkServiceMigrateNew(prefs
, oldService
, vlanBSDMapping
, setMapping
, serviceSetMapping
)) {
2898 SC_log(LOG_INFO
, "Could not migrate VLAN service: %@", oldService
);
2903 if (newPhysicalInterface
!= NULL
) {
2904 CFRelease(newPhysicalInterface
);
2906 if (newVLAN
!= NULL
) {
2912 _SCNetworkMigrationRemoveVLANServices(SCPreferencesRef prefs
)
2914 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2916 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2917 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2918 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2919 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2921 if ((bsdName
!= NULL
) &&
2922 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeVLAN
) {
2923 SCNetworkServiceRemove(service
);
2927 CFRelease(services
);
2931 static CFDictionaryRef
2932 _SCNetworkMigrationCopyMappingBSDNameToVLANServices(SCPreferencesRef prefs
)
2934 CFArrayRef services
= SCNetworkServiceCopyAll(prefs
);
2935 CFMutableDictionaryRef vlanServices
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2937 for (CFIndex idx
= 0; idx
< CFArrayGetCount(services
); idx
++) {
2938 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(services
, idx
);
2939 SCNetworkInterfaceRef interface
= SCNetworkServiceGetInterface(service
);
2940 CFStringRef bsdName
= SCNetworkInterfaceGetBSDName(interface
);
2942 if ((bsdName
!= NULL
) &&
2943 SCNetworkInterfaceGetInterfaceType(interface
) == kSCNetworkInterfaceTypeVLAN
) {
2944 CFMutableArrayRef serviceList
;
2945 if (!CFDictionaryContainsKey(vlanServices
, bsdName
)) {
2946 serviceList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2947 CFDictionaryAddValue(vlanServices
, bsdName
, serviceList
);
2948 CFRelease(serviceList
);
2950 serviceList
= (CFMutableArrayRef
)CFDictionaryGetValue(vlanServices
, bsdName
);
2951 CFArrayAppendValue(serviceList
, service
);
2954 CFRelease(services
);
2955 return vlanServices
;
2959 _SCNetworkMigrationDoVLANMigration (SCPreferencesRef sourcePrefs
,
2960 SCPreferencesRef sourceNIPrefs
,
2961 SCPreferencesRef targetPrefs
,
2962 SCPreferencesRef targetNIPrefs
,
2963 CFDictionaryRef bsdMapping
,
2964 CFDictionaryRef setMapping
,
2965 CFDictionaryRef serviceSetMapping
)
2967 CFArrayRef allSourceVLAN
;
2968 CFArrayRef allTargetVLAN
;
2969 SCVirtualInterfaceContext context
;
2971 Boolean success
= FALSE
;
2972 SCVLANInterfaceRef vlan
;
2973 CFMutableArrayRef vlanList
;
2974 CFMutableDictionaryRef vlanMapping
;
2975 CFDictionaryRef bsdNameToVLANServices
;
2977 allSourceVLAN
= SCVLANInterfaceCopyAll(sourcePrefs
);
2978 allTargetVLAN
= SCVLANInterfaceCopyAll(targetPrefs
);
2980 bsdNameToVLANServices
= _SCNetworkMigrationCopyMappingBSDNameToVLANServices(sourcePrefs
);
2982 vlanList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
2983 vlanMapping
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
2985 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allSourceVLAN
); idx
++) {
2986 vlan
= CFArrayGetValueAtIndex(allSourceVLAN
, idx
);
2987 CFStringRef vlanBSDName
= SCNetworkInterfaceGetBSDName(vlan
);
2988 SCNetworkInterfaceRef physicalInterface
= SCVLANInterfaceGetPhysicalInterface(vlan
);
2989 CFStringRef physicalInterfaceName
;
2991 SC_log(LOG_DEBUG
, "physical VLAN interface: %@", physicalInterface
);
2993 physicalInterfaceName
= SCNetworkInterfaceGetBSDName(physicalInterface
);
2994 SC_log(LOG_DEBUG
, "physical VLAN interface name: %@", physicalInterfaceName
);
2996 // Add VLAN to be migrated if the mapping between interfaces exists
2997 if (CFDictionaryContainsKey(bsdMapping
, physicalInterfaceName
)) {
2998 CFStringRef vlanNewName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("vlan%ld"), count
);
2999 CFDictionaryAddValue(vlanMapping
, vlanBSDName
, vlanNewName
);
3000 CFArrayAppendValue(vlanList
, vlan
);
3001 CFRelease(vlanNewName
);
3005 // Remove vlan services from target
3006 _SCNetworkMigrationRemoveVLANServices(targetPrefs
);
3008 // Remove Target VLANs
3009 for (CFIndex idx
= 0; idx
< CFArrayGetCount(allTargetVLAN
); idx
++) {
3010 vlan
= CFArrayGetValueAtIndex(allTargetVLAN
, idx
);
3011 if (!SCVLANInterfaceRemove(vlan
)) {
3012 SC_log(LOG_INFO
, "SCVLANInterfaceRemove() failed: %@", vlan
);
3017 context
.prefs
= targetPrefs
;
3018 context
.ni_prefs
= targetNIPrefs
;
3019 context
.bsdMapping
= bsdMapping
;
3020 context
.virtualBSDMapping
= vlanMapping
;
3021 context
.mappingBSDNameToService
= bsdNameToVLANServices
;
3022 context
.setMapping
= setMapping
;
3023 context
.serviceSetMapping
= serviceSetMapping
;
3025 // Add VLAN configurations at the target using vlanList
3026 CFArrayApplyFunction(vlanList
, CFRangeMake(0, CFArrayGetCount(vlanList
)), add_target_vlan
, &context
);
3030 CFRelease(allSourceVLAN
);
3031 CFRelease(allTargetVLAN
);
3032 CFRelease(vlanList
);
3033 CFRelease(vlanMapping
);
3034 CFRelease(bsdNameToVLANServices
);
3039 _SCNetworkMigrationDoVirtualNetworkInterfaceMigration(SCPreferencesRef sourcePrefs
,
3040 SCPreferencesRef sourceNIPrefs
,
3041 SCPreferencesRef targetPrefs
,
3042 SCPreferencesRef targetNIPrefs
,
3043 CFDictionaryRef bsdMapping
,
3044 CFDictionaryRef setMapping
,
3045 CFDictionaryRef serviceSetMapping
)
3048 if (!_SCNetworkMigrationDoBridgeMigration(sourcePrefs
, sourceNIPrefs
,
3049 targetPrefs
, targetNIPrefs
,
3050 bsdMapping
, setMapping
, serviceSetMapping
)) {
3051 SC_log(LOG_INFO
, "Bridge migration failed");
3055 if (!_SCNetworkMigrationDoBondMigration(sourcePrefs
, sourceNIPrefs
,
3056 targetPrefs
, targetNIPrefs
,
3057 bsdMapping
, setMapping
, serviceSetMapping
)) {
3058 SC_log(LOG_INFO
, "Bond migration failed");
3062 if (!_SCNetworkMigrationDoVLANMigration(sourcePrefs
, sourceNIPrefs
,
3063 targetPrefs
, targetNIPrefs
,
3064 bsdMapping
, setMapping
, serviceSetMapping
)) {
3065 SC_log(LOG_INFO
, "VLAN migration failed");
3072 SCPreferencesRef prefs
;
3073 CFArrayRef serviceOrder
;
3074 CFMutableArrayRef serviceListMutable
;
3076 } migrated_service_context
;
3079 create_migrated_order(const void *value
, void *context
)
3081 migrated_service_context
*ctx
= (migrated_service_context
*)context
;
3082 CFMutableArrayRef migratedServiceOrder
= ctx
->serviceListMutable
;
3083 CFArrayRef targetServiceOrder
= ctx
->serviceOrder
;
3084 CFStringRef migratedServiceID
= (CFStringRef
)value
;
3085 Boolean
*success
= ctx
->success
;
3087 if (*success
== FALSE
) {
3090 // Preserving the service order in the source configuration for the services
3091 // which were migrated into the target configuration
3092 for (CFIndex idx
= 0; idx
< CFArrayGetCount(targetServiceOrder
); idx
++) {
3093 CFStringRef targetServiceID
= CFArrayGetValueAtIndex(targetServiceOrder
, idx
);
3094 if (CFEqual(migratedServiceID
, targetServiceID
)) {
3095 CFArrayAppendValue(migratedServiceOrder
, migratedServiceID
);
3102 create_non_migrated_service_list(const void *value
, void *context
)
3104 migrated_service_context
*ctx
= (migrated_service_context
*)context
;
3105 CFArrayRef migratedServiceOrder
= ctx
->serviceOrder
;
3106 CFMutableArrayRef nonMigratedService
= ctx
->serviceListMutable
;
3107 SCPreferencesRef prefs
= ctx
->prefs
;
3108 SCNetworkServiceRef service
;
3109 Boolean
*success
= ctx
->success
;
3110 CFStringRef targetServiceID
= (CFStringRef
)value
;
3112 if (*success
== FALSE
) {
3115 // Adding all services not present in migratedServiceOrder into nonMigrated service
3116 for (CFIndex idx
= 0; idx
< CFArrayGetCount(migratedServiceOrder
); idx
++) {
3117 CFStringRef migratedServiceID
= CFArrayGetValueAtIndex(migratedServiceOrder
, idx
);
3119 if (CFEqual(targetServiceID
, migratedServiceID
)) {
3123 service
= SCNetworkServiceCopy(prefs
, targetServiceID
);
3124 if (service
== NULL
) {
3129 CFArrayAppendValue(nonMigratedService
, service
);
3134 preserve_service_order(const void *key
, const void *value
, void *context
)
3136 migrated_service_context migrated_context
;
3137 CFMutableArrayRef migratedServiceOrder
;
3138 migrated_service_context non_migrated_context
;
3139 CFMutableArrayRef nonMigratedServices
;
3140 SCNetworkSetRef sourceSet
= (SCNetworkSetRef
)key
;
3141 CFArrayRef sourceServiceOrder
= NULL
;
3142 Boolean
*success
= (Boolean
*)context
;
3143 SCNetworkSetRef targetSet
= (SCNetworkSetRef
)value
;
3144 SCNetworkSetPrivateRef targetPrivate
= (SCNetworkSetPrivateRef
)targetSet
;
3145 CFArrayRef targetServiceOrder
= NULL
;
3147 if (*success
== FALSE
) {
3150 migratedServiceOrder
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3151 nonMigratedServices
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3153 sourceServiceOrder
= SCNetworkSetGetServiceOrder(sourceSet
);
3154 if (sourceServiceOrder
== NULL
) {
3157 targetServiceOrder
= SCNetworkSetGetServiceOrder(targetSet
);
3158 if (targetServiceOrder
== NULL
) {
3162 migrated_context
.prefs
= NULL
;
3163 migrated_context
.serviceOrder
= targetServiceOrder
;
3164 migrated_context
.serviceListMutable
= migratedServiceOrder
;
3165 migrated_context
.success
= success
;
3167 // Creating a list of service IDs which were migrated in the target set
3168 // while maintaining the service order or the source set
3169 CFArrayApplyFunction(sourceServiceOrder
, CFRangeMake(0, CFArrayGetCount(sourceServiceOrder
)), create_migrated_order
, &migrated_context
);
3171 if (*success
== FALSE
) {
3175 non_migrated_context
.prefs
= targetPrivate
->prefs
;
3176 non_migrated_context
.serviceOrder
= migratedServiceOrder
;
3177 non_migrated_context
.serviceListMutable
= nonMigratedServices
;
3178 non_migrated_context
.success
= success
;
3180 // Creating a list of all the services which were not migrated from the source set to the
3182 CFArrayApplyFunction(targetServiceOrder
, CFRangeMake(0, CFArrayGetCount(targetServiceOrder
)), create_non_migrated_service_list
, &non_migrated_context
);
3184 // Remove non migrated service
3185 for (CFIndex idx
= 0; idx
< CFArrayGetCount(nonMigratedServices
); idx
++) {
3186 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(nonMigratedServices
, idx
);
3187 SCNetworkSetRemoveService(targetSet
, service
);
3189 // Set migrated service order
3190 SCNetworkSetSetServiceOrder(targetSet
, migratedServiceOrder
);
3192 // Add non migrated services
3193 for (CFIndex idx
= 0; idx
< CFArrayGetCount(nonMigratedServices
); idx
++) {
3194 SCNetworkServiceRef service
= CFArrayGetValueAtIndex(nonMigratedServices
, idx
);
3195 (void)SCNetworkSetAddService(targetSet
, service
);
3199 CFRelease(migratedServiceOrder
);
3200 CFRelease(nonMigratedServices
);
3206 _SCNetworkMigrationDoServiceOrderMigration(SCPreferencesRef sourcePrefs
,
3207 SCPreferencesRef targetPrefs
,
3208 CFDictionaryRef setMapping
)
3210 Boolean success
= TRUE
;
3212 if (isA_CFDictionary(setMapping
) == NULL
) {
3217 CFDictionaryApplyFunction(setMapping
, preserve_service_order
, &success
);
3223 // This is a function that looks at source and target network configuration
3224 // and determines what network configurations can be transferred from source to
3227 _SCNetworkConfigurationMigrateConfiguration(CFURLRef sourceDir
, CFURLRef targetDir
)
3229 CFDictionaryRef bsdNameMapping
= NULL
; // Mapping between BSD name and SCNetworkInterfaceRef to help with mapping services
3230 CFMutableDictionaryRef builtinMapping
= NULL
; // Mapping between builtin interfaces between source and target configurations: (SCNetworkInterfaceRef -> SCNetworkInterfaceRef)
3231 CFMutableDictionaryRef externalMapping
= NULL
; // Mapping between external interfaces between source and target configurations: (SCNetworkInterfaceRef -> SCNetworkInterfaceRef)
3232 Boolean migrationSuccess
= FALSE
;
3233 CFArrayRef newTargetNetworkInterfaceEntity
= NULL
; // Array of Interface Entity which used to create new target interfaces created during migration
3234 CFDictionaryRef serviceMapping
= NULL
; // Mapping between services of source to target. (SCNetworkServicesRef -> SCNetworkServicesRef)
3235 CFDictionaryRef setMapping
= NULL
;
3236 CFDictionaryRef sourceServiceSetMapping
= NULL
;
3237 CFArrayRef sourceConfigurationFiles
= NULL
; // Path to the source configuration files which need to be migrated
3238 CFStringRef sourceModel
= NULL
;
3239 CFURLRef sourceNetworkInterfaceFile
= NULL
; // Source CFURLRef for preferences.plist and NetworkInterfaces.plist
3240 char sourceNetworkInterfaceFileStr
[PATH_MAX
];
3241 CFStringRef sourceNetworkInterfaceFileString
= NULL
; // Source CFStringRef for preferences.plist and NetworkInterfaces.plist
3242 SCPreferencesRef sourceNetworkInterfacePrefs
= NULL
; // Source SCPreferencesRef for preferences.plist and NetworkInterfaces.plist
3243 CFURLRef sourcePreferencesFile
= NULL
;
3244 char sourcePreferencesFileStr
[PATH_MAX
];
3245 CFStringRef sourcePreferencesFileString
= NULL
;
3246 SCPreferencesRef sourcePrefs
= NULL
;
3247 CFArrayRef targetConfigurationFiles
= NULL
; // Path to the target configuration files where migration will take place to
3248 Boolean targetConfigurationFilesPresent
;
3249 CFStringRef targetModel
= NULL
;
3250 CFURLRef targetNetworkInterfaceFile
= NULL
; // Target CFURLRef for preferences.plist and NetworkInterfaces.plist
3251 char targetNetworkInterfaceFileStr
[PATH_MAX
];
3252 CFStringRef targetNetworkInterfaceFileString
= NULL
; // Target CFStringRef for preferences.plist and NetworkInterfaces.plist
3253 SCPreferencesRef targetNetworkInterfacePrefs
= NULL
; // Target SCPreferencesRef for preferences.plist and NetworkInterfaces.plist
3254 CFURLRef targetPreferencesFile
= NULL
;
3255 char targetPreferencesFileStr
[PATH_MAX
];
3256 CFStringRef targetPreferencesFileString
= NULL
;
3257 SCPreferencesRef targetPrefs
= NULL
;
3258 Boolean isUpgradeScenario
= FALSE
;
3259 CFMutableDictionaryRef validityOptions
= NULL
;
3261 // Check if configuration files exist in sourceDir
3262 if (!__SCNetworkConfigurationMigrateConfigurationFilesPresent(sourceDir
, &sourceConfigurationFiles
)) {
3263 SC_log(LOG_INFO
, "sourceDir: (%@) doesn't contain configuration files", sourceDir
);
3267 sourcePreferencesFile
= CFArrayGetValueAtIndex(sourceConfigurationFiles
, PREFERENCES_PLIST_INDEX
);
3268 if (!CFURLGetFileSystemRepresentation(sourcePreferencesFile
, TRUE
, (UInt8
*)sourcePreferencesFileStr
, sizeof(sourcePreferencesFileStr
))) {
3269 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", sourcePreferencesFile
);
3273 sourceNetworkInterfaceFile
= CFArrayGetValueAtIndex(sourceConfigurationFiles
, NETWORK_INTERFACES_PLIST_INDEX
);
3274 if (!CFURLGetFileSystemRepresentation(sourceNetworkInterfaceFile
, TRUE
, (UInt8
*)sourceNetworkInterfaceFileStr
, sizeof(sourceNetworkInterfaceFileStr
))) {
3275 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", sourceNetworkInterfaceFile
);
3279 sourcePreferencesFileString
= CFStringCreateWithCString(NULL
, sourcePreferencesFileStr
, kCFStringEncodingUTF8
);
3280 sourceNetworkInterfaceFileString
= CFStringCreateWithCString(NULL
, sourceNetworkInterfaceFileStr
, kCFStringEncodingUTF8
);
3282 sourcePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, sourcePreferencesFileString
);
3283 sourceNetworkInterfacePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, sourceNetworkInterfaceFileString
);
3284 if ((sourcePrefs
== NULL
) || (sourceNetworkInterfacePrefs
== NULL
)) {
3288 targetConfigurationFilesPresent
= __SCNetworkConfigurationMigrateConfigurationFilesPresent(targetDir
, &targetConfigurationFiles
);
3289 if (!targetConfigurationFilesPresent
) {
3290 if (targetConfigurationFiles
== NULL
) {
3291 SC_log(LOG_DEBUG
, "targetConfigurationFiles is NULL");
3294 SC_log(LOG_INFO
, "targetDir: (%@) doesn't contain configuration files ... Need to create default configuration", targetDir
);
3297 targetPreferencesFile
= CFArrayGetValueAtIndex(targetConfigurationFiles
, PREFERENCES_PLIST_INDEX
);
3298 if (!CFURLGetFileSystemRepresentation(targetPreferencesFile
, TRUE
, (UInt8
*)targetPreferencesFileStr
, sizeof(targetPreferencesFileStr
))) {
3299 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetPreferencesFile
);
3302 targetNetworkInterfaceFile
= CFArrayGetValueAtIndex(targetConfigurationFiles
, NETWORK_INTERFACES_PLIST_INDEX
);
3303 if (!CFURLGetFileSystemRepresentation(targetNetworkInterfaceFile
, TRUE
, (UInt8
*)targetNetworkInterfaceFileStr
, sizeof(targetNetworkInterfaceFileStr
))) {
3304 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", targetNetworkInterfaceFile
);
3308 targetPreferencesFileString
= CFStringCreateWithCString(NULL
, targetPreferencesFileStr
, kCFStringEncodingUTF8
);
3309 targetNetworkInterfaceFileString
= CFStringCreateWithCString(NULL
, targetNetworkInterfaceFileStr
, kCFStringEncodingUTF8
);
3311 if (targetConfigurationFilesPresent
) {
3312 targetPrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, targetPreferencesFileString
);
3313 targetNetworkInterfacePrefs
= SCPreferencesCreate(NULL
, PLUGIN_ID
, targetNetworkInterfaceFileString
);
3314 if ((targetPrefs
== NULL
) || (targetNetworkInterfacePrefs
== NULL
)) {
3319 targetPrefs
= __SCNetworkCreateDefaultPref(targetPreferencesFileString
);
3320 targetNetworkInterfacePrefs
= __SCNetworkCreateDefaultNIPrefs(targetNetworkInterfaceFileString
);
3322 if (targetPrefs
== NULL
||
3323 targetNetworkInterfacePrefs
== NULL
) {
3324 SC_log(LOG_DEBUG
, "Could not create default configuration");
3328 validityOptions
= CFDictionaryCreateMutable(NULL
, 0,
3329 &kCFTypeDictionaryKeyCallBacks
,
3330 &kCFTypeDictionaryValueCallBacks
);
3331 CFDictionaryAddValue(validityOptions
, kSCNetworkConfigurationRepair
, kCFBooleanTrue
);
3333 SC_log(LOG_DEBUG
, "sourcePreferenceFileString: %@\n"
3334 "sourceNetworkInterfaceFileString:%@\n"
3335 "targetPreferencesFileString:%@\n"
3336 "targetNetworkInterfaceFileString:%@",
3337 sourcePreferencesFileString
,
3338 sourceNetworkInterfaceFileString
,
3339 targetPreferencesFileString
,
3340 targetNetworkInterfaceFileString
);
3342 // Setting Bypass Interface to avoid looking at system interfaces
3343 __SCPreferencesSetLimitSCNetworkConfiguration(sourcePrefs
, TRUE
);
3344 __SCPreferencesSetLimitSCNetworkConfiguration(targetPrefs
, TRUE
);
3346 sourceModel
= SCPreferencesGetValue(sourcePrefs
, MODEL
);
3347 targetModel
= SCPreferencesGetValue(targetPrefs
, MODEL
);
3349 isUpgradeScenario
= (isA_CFString(sourceModel
) && isA_CFString(targetModel
) && CFStringCompare(sourceModel
, targetModel
, 0) == kCFCompareEqualTo
);
3351 // Create services for builtin interfaces at source if they don't exist
3352 (void)_SCNetworkConfigurationCreateBuiltinInterfaceServices(sourcePrefs
, sourceNetworkInterfacePrefs
);
3353 // Checking validity of the source and destination preferences before continuing
3354 if (!_SCNetworkConfigurationCheckValidityWithPreferences(sourcePrefs
,
3355 sourceNetworkInterfacePrefs
,
3357 SC_log(LOG_INFO
, "Source configuration not valid");
3358 goto skipServiceMigration
;
3360 // Only call this function if configuration files were not created by default
3361 if (targetConfigurationFilesPresent
) {
3362 // Create services for builtin interfaces at target if they don't exist
3363 (void)_SCNetworkConfigurationCreateBuiltinInterfaceServices(targetPrefs
, targetNetworkInterfacePrefs
);
3364 if (!_SCNetworkConfigurationCheckValidityWithPreferences(targetPrefs
,
3365 targetNetworkInterfacePrefs
,
3367 SC_log(LOG_INFO
, "Target configuration not valid");
3368 goto skipServiceMigration
;
3371 SC_log(LOG_DEBUG
, "This is %san upgrade scenario", isUpgradeScenario
? "" : "not ");
3372 // Upgrade scenario, source and target models match
3373 if (isUpgradeScenario
) {
3374 Boolean foundNewInterface
= FALSE
;
3375 // Create SCPreferences to copy the target prefs
3376 SCPreferencesRef upgradeSourcePrefs
= SCPreferencesCreate(NULL
, CFSTR("Upgrade Source Prefs"), NULL
);
3377 SCPreferencesRef upgradeSourceNIPrefs
= SCPreferencesCreate(NULL
, CFSTR("Upgrade Source NI Prefs"), NULL
);
3379 // Content of target prefs
3380 CFDictionaryRef targetPrefsContent
= SCPreferencesPathGetValue(targetPrefs
, CFSTR("/"));
3381 CFDictionaryRef targetNIPrefsContent
= SCPreferencesPathGetValue(targetNetworkInterfacePrefs
, CFSTR("/"));
3383 // Backing up the target prefs into source prefs
3384 SCPreferencesPathSetValue(upgradeSourcePrefs
, CFSTR("/"), targetPrefsContent
);
3385 SCPreferencesPathSetValue(upgradeSourceNIPrefs
, CFSTR("/"), targetNIPrefsContent
);
3387 // Copying content from the source prefs
3388 CFDictionaryRef sourcePrefsContent
= SCPreferencesPathGetValue(sourcePrefs
, CFSTR("/"));
3389 CFDictionaryRef sourceNIPreferencesContent
= SCPreferencesPathGetValue(sourceNetworkInterfacePrefs
, CFSTR("/"));
3391 // Setting the contents of the source prefs into the target prefs
3392 SCPreferencesPathSetValue(targetPrefs
, CFSTR("/"), sourcePrefsContent
);
3393 SCPreferencesPathSetValue(targetNetworkInterfacePrefs
, CFSTR("/"), sourceNIPreferencesContent
);
3395 // Getting the mapping of the non builtin interfaces between source and target
3396 externalMapping
= _SCNetworkConfigurationCopyExternalInterfaceMapping(upgradeSourceNIPrefs
, targetNetworkInterfacePrefs
);
3398 newTargetNetworkInterfaceEntity
= _SCNetworkMigrationCreateNetworkInterfaceArray(targetNetworkInterfacePrefs
, externalMapping
, &foundNewInterface
);
3399 if (foundNewInterface
) {
3400 if (isA_CFArray(newTargetNetworkInterfaceEntity
) == NULL
) {
3401 SC_log(LOG_INFO
, "newTargetNetworkInterfaceEntity is NULL or not of correct type");
3402 CFRelease(upgradeSourcePrefs
);
3403 CFRelease(upgradeSourceNIPrefs
);
3406 // Write new interface mapping to NetworkInterfaces.plist
3407 if (!__SCNetworkInterfaceSaveStoredWithPreferences(targetNetworkInterfacePrefs
, newTargetNetworkInterfaceEntity
)) {
3408 SC_log(LOG_INFO
, "SCNetworkInterfaceSaveStoreWithPreferences: failed to update NetworkInterfaces.plist");
3409 CFRelease(upgradeSourcePrefs
);
3410 CFRelease(upgradeSourceNIPrefs
);
3414 // Create BSD Name Mapping to facilitate mapping of services
3415 bsdNameMapping
= _SCNetworkMigrationCreateBSDNameMapping(NULL
, externalMapping
);
3417 serviceMapping
= _SCNetworkMigrationCreateServiceMappingUsingBSDMapping(upgradeSourcePrefs
, targetPrefs
, bsdNameMapping
);
3419 _SCNetworkMigrationDoServiceMigration(upgradeSourcePrefs
, targetPrefs
,
3420 serviceMapping
, bsdNameMapping
,
3423 CFRelease(upgradeSourcePrefs
);
3424 CFRelease(upgradeSourceNIPrefs
);
3427 builtinMapping
= _SCNetworkConfigurationCopyBuiltinMapping(sourceNetworkInterfacePrefs
, targetNetworkInterfacePrefs
);
3428 externalMapping
= _SCNetworkConfigurationCopyExternalInterfaceMapping(sourceNetworkInterfacePrefs
, targetNetworkInterfacePrefs
);
3430 newTargetNetworkInterfaceEntity
= _SCNetworkMigrationCreateNetworkInterfaceArray(targetNetworkInterfacePrefs
, externalMapping
, NULL
);
3431 if (isA_CFArray(newTargetNetworkInterfaceEntity
) == NULL
) {
3432 SC_log(LOG_INFO
, "newTargetNetworkInterfaceEntity is NULL or not of correct type");
3435 // Write new interface mapping to NetworkInterfaces.plist
3436 if (!__SCNetworkInterfaceSaveStoredWithPreferences(targetNetworkInterfacePrefs
, newTargetNetworkInterfaceEntity
)) {
3437 SC_log(LOG_INFO
, "SCNetworkInterfaceSaveStoreWithPreferences: failed to update NetworkInterfaces.plist");
3440 // Create BSD Name Mapping to facilitate mapping of services
3441 bsdNameMapping
= _SCNetworkMigrationCreateBSDNameMapping(builtinMapping
, externalMapping
);
3443 if (isA_CFDictionary(bsdNameMapping
) == NULL
) {
3444 SC_log(LOG_INFO
, "BSD name mapping is NULL");
3447 SC_log(LOG_DEBUG
, "BSD Name Mapping: %@", bsdNameMapping
);
3448 serviceMapping
= _SCNetworkMigrationCreateServiceMappingUsingBSDMapping(sourcePrefs
, targetPrefs
, bsdNameMapping
);
3450 if (isA_CFDictionary(serviceMapping
) == NULL
) {
3451 SC_log(LOG_INFO
, "Service mapping is NULL");
3454 SC_log(LOG_NOTICE
, "Service mapping: %@", serviceMapping
);
3456 setMapping
= _SCNetworkMigrationCreateSetMapping(sourcePrefs
, targetPrefs
);
3457 sourceServiceSetMapping
= _SCNetworkMigrationCreateServiceSetMapping(sourcePrefs
);
3459 // Perform the migration of services
3460 if (!_SCNetworkMigrationDoServiceMigration(sourcePrefs
, targetPrefs
,
3461 serviceMapping
, bsdNameMapping
,
3462 setMapping
, sourceServiceSetMapping
)) {
3463 SC_log(LOG_INFO
, "SCNetworkMigrationDoServiceMigration: failed to complete successfully");
3467 #if !TARGET_OS_IPHONE
3468 // Migrating Virtual Network Interface
3469 if (!_SCNetworkMigrationDoVirtualNetworkInterfaceMigration(sourcePrefs
, sourceNetworkInterfacePrefs
,
3470 targetPrefs
, targetNetworkInterfacePrefs
,
3471 bsdNameMapping
, setMapping
, sourceServiceSetMapping
)) {
3472 SC_log(LOG_INFO
, "_SCNetworkMigrationDoVirtualNetworkInterfaceMigration: failed to complete successfully");
3475 // Migrate Service Order
3476 if (!_SCNetworkMigrationDoServiceOrderMigration(sourcePrefs
, targetPrefs
, setMapping
)) {
3477 SC_log(LOG_INFO
, "_SCNetworkMigrationDoServiceOrderMigration: failed to complete successfully");
3481 skipServiceMigration
:
3482 // Migrating System Information
3483 if (!isUpgradeScenario
) {
3484 if (!_SCNetworkMigrationDoSystemMigration(sourcePrefs
, targetPrefs
)) {
3485 SC_log(LOG_INFO
, "_SCNetworkMigrationDoSystemMigration: failed to complete successfully");
3488 if (!_SCNetworkConfigurationCheckValidityWithPreferences(targetPrefs
, targetNetworkInterfacePrefs
, validityOptions
)) {
3489 SC_log(LOG_INFO
, "Migrated configuration not valid");
3492 if (!SCPreferencesCommitChanges(targetPrefs
)) {
3493 SC_log(LOG_INFO
, "SCPreferencesCommitChanges(target preferences.plist) failed: %s", SCErrorString(SCError()));
3497 if (!SCPreferencesCommitChanges(targetNetworkInterfacePrefs
)) {
3498 SC_log(LOG_INFO
, "SCPreferencesCommitChanges(target NetworkInterfaces.plist) failed: %s", SCErrorString(SCError()));
3501 migrationSuccess
= TRUE
;
3504 if (setMapping
!= NULL
) {
3505 CFRelease(setMapping
);
3507 if (sourceServiceSetMapping
!= NULL
) {
3508 CFRelease(sourceServiceSetMapping
);
3510 if (sourceConfigurationFiles
!= NULL
) {
3511 CFRelease(sourceConfigurationFiles
);
3513 if (targetConfigurationFiles
!= NULL
) {
3514 CFRelease(targetConfigurationFiles
);
3516 if (sourcePreferencesFileString
!= NULL
) {
3517 CFRelease(sourcePreferencesFileString
);
3519 if (sourceNetworkInterfaceFileString
!= NULL
) {
3520 CFRelease(sourceNetworkInterfaceFileString
);
3522 if (targetPreferencesFileString
!= NULL
) {
3523 CFRelease(targetPreferencesFileString
);
3525 if (targetNetworkInterfaceFileString
!= NULL
) {
3526 CFRelease(targetNetworkInterfaceFileString
);
3528 if (newTargetNetworkInterfaceEntity
!= NULL
) {
3529 CFRelease(newTargetNetworkInterfaceEntity
);
3531 if (builtinMapping
!= NULL
) {
3532 CFRelease(builtinMapping
);
3534 if (externalMapping
!= NULL
) {
3535 CFRelease(externalMapping
);
3537 if (bsdNameMapping
!= NULL
) {
3538 CFRelease(bsdNameMapping
);
3540 if (serviceMapping
!= NULL
) {
3541 CFRelease(serviceMapping
);
3543 if (targetPrefs
!= NULL
) {
3544 CFRelease(targetPrefs
);
3546 if (sourcePrefs
!= NULL
) {
3547 CFRelease(sourcePrefs
);
3549 if (sourceNetworkInterfacePrefs
!= NULL
) {
3550 CFRelease(sourceNetworkInterfacePrefs
);
3552 if (targetNetworkInterfacePrefs
!= NULL
) {
3553 CFRelease(targetNetworkInterfacePrefs
);
3555 if (validityOptions
!= NULL
) {
3556 CFRelease(validityOptions
);
3558 return migrationSuccess
;
3564 _SCNetworkMigrationAreServicesIdentical(SCPreferencesRef configPref
, SCPreferencesRef expectedConfigPref
)
3566 const void * expected_vals_q
[N_QUICK
];
3567 const void ** expected_vals
= expected_vals_q
;
3568 CFMutableArrayRef expectedServiceArray
= NULL
;
3569 CFIndex expectedServiceArrayCount
= 0;
3570 CFDictionaryRef expectedServiceDict
= NULL
;
3571 CFIndex expectedServiceDictCount
= 0;
3572 CFDictionaryRef expectedServiceEntity
= 0;
3573 Boolean foundMatch
= FALSE
;
3574 CFMutableArrayRef serviceArray
= NULL
;
3575 CFIndex serviceArrayCount
= 0;
3576 CFDictionaryRef serviceDict
= NULL
;
3577 CFIndex serviceDictCount
= 0;
3578 CFDictionaryRef serviceEntity
= NULL
;
3579 Boolean success
= FALSE
;
3580 const void * vals_q
[N_QUICK
];
3581 const void ** vals
= vals_q
;
3583 serviceDict
= SCPreferencesGetValue(configPref
, kSCPrefNetworkServices
);
3584 if (isA_CFDictionary(serviceDict
) == NULL
) {
3587 serviceDictCount
= CFDictionaryGetCount(serviceDict
);
3589 expectedServiceDict
= SCPreferencesGetValue(expectedConfigPref
, kSCPrefNetworkServices
);
3590 if (isA_CFDictionary(expectedServiceDict
) == NULL
) {
3593 expectedServiceDictCount
= CFDictionaryGetCount(expectedServiceDict
);
3595 if (serviceDictCount
!= expectedServiceDictCount
) {
3599 if (serviceDictCount
> (sizeof(vals_q
) / sizeof(CFTypeRef
))) {
3600 vals
= CFAllocatorAllocate(NULL
, serviceDictCount
* sizeof(CFPropertyListRef
), 0);
3603 CFDictionaryGetKeysAndValues(serviceDict
, NULL
, vals
);
3604 serviceArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3606 for (CFIndex idx
=0; idx
< serviceDictCount
; idx
++) {
3607 serviceEntity
= vals
[idx
];
3608 if (!isA_CFDictionary(serviceEntity
)) {
3611 CFArrayAppendValue(serviceArray
, serviceEntity
);
3614 serviceArrayCount
= CFArrayGetCount(serviceArray
);
3616 if (expectedServiceDictCount
> (sizeof(expected_vals_q
) / sizeof(CFTypeRef
))) {
3617 expected_vals
= CFAllocatorAllocate(NULL
, expectedServiceDictCount
, 0);
3620 CFDictionaryGetKeysAndValues(expectedServiceDict
, NULL
, expected_vals
);
3621 expectedServiceArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3623 for (CFIndex idx
= 0; idx
< expectedServiceDictCount
; idx
++) {
3624 serviceEntity
= expected_vals
[idx
];
3625 if (!isA_CFDictionary(serviceEntity
)) {
3628 CFArrayAppendValue(expectedServiceArray
, serviceEntity
);
3630 expectedServiceArrayCount
= CFArrayGetCount(expectedServiceArray
);
3632 if (serviceArrayCount
!= expectedServiceArrayCount
) {
3636 for (CFIndex idx
= 0; idx
< expectedServiceArrayCount
; idx
++) {
3638 expectedServiceEntity
= CFArrayGetValueAtIndex(expectedServiceArray
, idx
);
3639 serviceArrayCount
= CFArrayGetCount(serviceArray
);
3641 for (CFIndex idx2
= 0; idx2
< serviceArrayCount
; idx2
++) {
3642 serviceEntity
= CFArrayGetValueAtIndex(serviceArray
, idx2
);
3644 if (CFEqual(expectedServiceEntity
, serviceEntity
)) {
3655 success
= foundMatch
;
3657 if (vals
!= vals_q
) {
3658 CFAllocatorDeallocate(NULL
, vals
);
3660 if (expected_vals
!= expected_vals_q
) {
3661 CFAllocatorDeallocate(NULL
, expected_vals
);
3667 _SCNetworkMigrationAreNetworkInterfaceConfigurationsIdentical (SCPreferencesRef configNetworkInterfacePref
, SCPreferencesRef expectedNetworkInterfacePref
)
3669 CFDictionaryRef expectedInterfaceEntity
= NULL
;
3670 CFArrayRef expectedInterfaceList
= NULL
;
3671 CFIndex expectedInterfaceListCount
;
3672 Boolean foundMatch
= FALSE
;
3673 CFDictionaryRef interfaceEntity
= NULL
;
3674 CFArrayRef interfaceList
= NULL
;
3675 CFIndex interfaceListCount
;
3676 CFMutableArrayRef interfaceListMutable
= NULL
;
3677 Boolean success
= FALSE
;
3679 interfaceList
= SCPreferencesGetValue(configNetworkInterfacePref
, INTERFACES
);
3680 if (isA_CFArray(interfaceList
) == NULL
) {
3683 interfaceListMutable
= CFArrayCreateMutableCopy(NULL
, 0, interfaceList
);
3684 interfaceListCount
= CFArrayGetCount(interfaceListMutable
);
3686 expectedInterfaceList
= SCPreferencesGetValue(expectedNetworkInterfacePref
, INTERFACES
);
3687 if (isA_CFArray(expectedInterfaceList
) == NULL
) {
3690 expectedInterfaceListCount
= CFArrayGetCount(expectedInterfaceList
);
3692 if (interfaceListCount
!= expectedInterfaceListCount
) {
3696 for (CFIndex idx
= 0; idx
< expectedInterfaceListCount
; idx
++) {
3698 expectedInterfaceEntity
= CFArrayGetValueAtIndex(expectedInterfaceList
, idx
);
3699 interfaceListCount
= CFArrayGetCount(interfaceListMutable
);
3701 for (CFIndex idx2
= 0; idx2
< interfaceListCount
; idx2
++) {
3702 interfaceEntity
= CFArrayGetValueAtIndex(interfaceList
, idx2
);
3703 if (CFEqual(expectedInterfaceEntity
, interfaceEntity
)) {
3712 success
= foundMatch
;
3715 if (interfaceListMutable
!= NULL
) {
3716 CFRelease(interfaceListMutable
);
3723 _SCNetworkMigrationAreConfigurationsIdentical (CFURLRef configurationURL
,
3724 CFURLRef expectedConfigurationURL
)
3726 CFURLRef baseConfigURL
= NULL
;
3727 CFURLRef baseExpectedConfigURL
= NULL
;
3728 CFURLRef configPreferencesURL
= NULL
;
3729 CFURLRef configNetworkInterfacesURL
= NULL
;
3730 SCPreferencesRef configPref
= NULL
;
3731 SCPreferencesRef configNetworkInterfacePref
= NULL
;
3732 SCPreferencesRef expectedConfigPref
= NULL
;
3733 SCPreferencesRef expectedNetworkInterfacePref
= NULL
;
3734 CFURLRef expectedNetworkInterfaceURL
= NULL
;
3735 CFURLRef expectedPreferencesURL
= NULL
;
3736 Boolean isIdentical
= FALSE
;
3737 CFStringRef networkInterfaceConfigString
= NULL
;
3738 CFStringRef networkInterfaceExpectedString
= NULL
;
3739 CFStringRef prefsConfigString
= NULL
;
3740 CFStringRef prefsExpectedString
= NULL
;
3741 char networkInterfaceConfigStr
[PATH_MAX
];
3742 char networkInterfaceExpectedStr
[PATH_MAX
];
3743 char prefsConfigStr
[PATH_MAX
];
3744 char prefsExpectedStr
[PATH_MAX
];
3746 if (configurationURL
== NULL
||
3747 expectedConfigurationURL
== NULL
) {
3750 baseConfigURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
, kCFURLPOSIXPathStyle
, TRUE
, configurationURL
);
3751 configPreferencesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*) PREFS_DEFAULT_CONFIG_PLIST
, sizeof(PREFS_DEFAULT_CONFIG_PLIST
), FALSE
, baseConfigURL
);
3753 if (!CFURLResourceIsReachable(configPreferencesURL
, NULL
)) {
3754 SC_log(LOG_INFO
, "No preferences.plist file");
3758 configNetworkInterfacesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)NETWORK_INTERFACES_PREFS_PLIST
, sizeof(NETWORK_INTERFACES_PREFS_PLIST
), FALSE
, baseConfigURL
);
3760 if (!CFURLResourceIsReachable(configNetworkInterfacesURL
, NULL
)) {
3761 SC_log(LOG_INFO
, "No NetworkInterfaces.plist file");
3765 if (!CFURLGetFileSystemRepresentation(configPreferencesURL
, TRUE
, (UInt8
*)prefsConfigStr
, sizeof(prefsConfigStr
))) {
3766 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configPreferencesURL
);
3769 if (!CFURLGetFileSystemRepresentation(configNetworkInterfacesURL
, TRUE
, (UInt8
*)networkInterfaceConfigStr
, sizeof(networkInterfaceConfigStr
))) {
3770 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", configNetworkInterfacesURL
);
3774 baseExpectedConfigURL
= CFURLCreateWithFileSystemPathRelativeToBase(NULL
, PREFS_DEFAULT_DIR_RELATIVE
, kCFURLPOSIXPathStyle
, TRUE
, expectedConfigurationURL
);
3775 expectedPreferencesURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)PREFS_DEFAULT_CONFIG_PLIST
, sizeof(PREFS_DEFAULT_CONFIG_PLIST
), FALSE
, baseExpectedConfigURL
);
3777 if (!CFURLResourceIsReachable(expectedPreferencesURL
, NULL
)) {
3778 SC_log(LOG_INFO
, "No expected preferences.plist file");
3782 expectedNetworkInterfaceURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)NETWORK_INTERFACES_PREFS_PLIST
, sizeof(NETWORK_INTERFACES_PREFS_PLIST
), FALSE
, baseExpectedConfigURL
);
3784 if (!CFURLResourceIsReachable(expectedNetworkInterfaceURL
, NULL
)) {
3785 SC_log(LOG_INFO
, "No expected NetworkInterfaces.plist file");
3789 if (!CFURLGetFileSystemRepresentation(expectedPreferencesURL
, TRUE
, (UInt8
*)prefsExpectedStr
, sizeof(prefsExpectedStr
))) {
3790 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", expectedPreferencesURL
);
3793 if (!CFURLGetFileSystemRepresentation(expectedNetworkInterfaceURL
, TRUE
, (UInt8
*)networkInterfaceExpectedStr
, sizeof(networkInterfaceExpectedStr
))) {
3794 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", expectedNetworkInterfaceURL
);
3798 prefsConfigString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsConfigStr
);
3799 networkInterfaceConfigString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceConfigStr
);
3800 prefsExpectedString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), prefsExpectedStr
);
3801 networkInterfaceExpectedString
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), networkInterfaceExpectedStr
);
3803 configPref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsConfigString
);
3804 expectedConfigPref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, prefsExpectedString
);
3805 configNetworkInterfacePref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, networkInterfaceConfigString
);
3806 expectedNetworkInterfacePref
= SCPreferencesCreate(NULL
, PLUGIN_ID
, networkInterfaceExpectedString
);
3808 if (configPref
== NULL
||
3809 expectedConfigPref
== NULL
||
3810 configNetworkInterfacePref
== NULL
||
3811 expectedNetworkInterfacePref
== NULL
) {
3812 SC_log(LOG_INFO
, "One of the preferences is NULL");
3813 isIdentical
= FALSE
;
3816 isIdentical
= (_SCNetworkMigrationAreServicesIdentical(configPref
, expectedConfigPref
) &&
3817 _SCNetworkMigrationAreNetworkInterfaceConfigurationsIdentical(configNetworkInterfacePref
, expectedNetworkInterfacePref
));
3819 if (baseConfigURL
!= NULL
) {
3820 CFRelease(baseConfigURL
);
3822 if (configPreferencesURL
!= NULL
) {
3823 CFRelease(configPreferencesURL
);
3825 if (configNetworkInterfacesURL
!= NULL
) {
3826 CFRelease(configNetworkInterfacesURL
);
3828 if (baseExpectedConfigURL
!= NULL
) {
3829 CFRelease(baseExpectedConfigURL
);
3831 if (expectedPreferencesURL
!= NULL
) {
3832 CFRelease(expectedPreferencesURL
);
3834 if (expectedNetworkInterfaceURL
!= NULL
) {
3835 CFRelease(expectedNetworkInterfaceURL
);
3837 if (prefsConfigString
!= NULL
) {
3838 CFRelease(prefsConfigString
);
3840 if (networkInterfaceConfigString
!= NULL
) {
3841 CFRelease(networkInterfaceConfigString
);
3843 if (prefsExpectedString
!= NULL
) {
3844 CFRelease(prefsExpectedString
);
3846 if (networkInterfaceExpectedString
!= NULL
) {
3847 CFRelease(networkInterfaceExpectedString
);
3849 if (configPref
!= NULL
) {
3850 CFRelease(configPref
);
3852 if (expectedConfigPref
!= NULL
) {
3853 CFRelease(expectedConfigPref
);
3855 if (configNetworkInterfacePref
!= NULL
) {
3856 CFRelease(configNetworkInterfacePref
);
3858 if (expectedNetworkInterfacePref
!= NULL
) {
3859 CFRelease(expectedNetworkInterfacePref
);
3865 _SCNetworkConfigurationCopyMigrationRemovePaths (CFArrayRef targetPaths
,
3868 CFURLRef affectedURL
;
3869 char filePath
[PATH_MAX
];
3870 CFURLRef targetFile
;
3871 CFMutableArrayRef toBeRemoved
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
3873 for (CFIndex idx
= 0; idx
< CFArrayGetCount(targetPaths
); idx
++) {
3874 affectedURL
= CFArrayGetValueAtIndex(targetPaths
, idx
);
3876 if (!CFURLGetFileSystemRepresentation(affectedURL
, TRUE
, (UInt8
*)filePath
, sizeof(filePath
))) {
3877 SC_log(LOG_NOTICE
, "Cannot get file system representation for url: %@", affectedURL
);
3880 targetFile
= CFURLCreateFromFileSystemRepresentationRelativeToBase(NULL
, (const UInt8
*)filePath
,
3881 strnlen(filePath
, sizeof(filePath
)), FALSE
, targetDir
);
3883 if (!CFURLResourceIsReachable(targetFile
, NULL
)) {
3884 CFArrayAppendValue(toBeRemoved
, affectedURL
);
3886 CFRelease(targetFile
);
3888 // If number of files to be removed is 0, return NULL
3889 if (CFArrayGetCount(toBeRemoved
) == 0) {
3890 CFRelease(toBeRemoved
);