2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * November 28, 2005 Allan Nathanson <ajn@apple.com>
30 * November 14, 2003 Allan Nathanson <ajn@apple.com>
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <CoreFoundation/CFRuntime.h>
38 #include <SystemConfiguration/SystemConfiguration.h>
39 #include "SCNetworkConfigurationInternal.h"
40 #include <SystemConfiguration/SCValidation.h>
41 #include <SystemConfiguration/SCPrivate.h>
46 #include <sys/types.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <net/ethernet.h>
50 #define KERNEL_PRIVATE
52 #include <net/if_var.h>
54 #include <net/if_vlan_var.h>
55 #include <net/if_types.h>
57 /* ---------- VLAN support ---------- */
64 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
66 SCLog(TRUE
, LOG_ERR
, CFSTR("socket() failed: %s"), strerror(errno
));
74 CFMutableArrayRef vlans
;
75 SCPreferencesRef prefs
;
76 } addContext
, *addContextRef
;
80 add_configured_interface(const void *key
, const void *value
, void *context
)
82 SCNetworkInterfacePrivateRef interfacePrivate
;
83 addContextRef myContext
= (addContextRef
)context
;
84 SCVLANInterfaceRef vlan
;
85 CFStringRef vlan_if
= (CFStringRef
)key
;
86 CFDictionaryRef vlan_info
= (CFDictionaryRef
)value
;
87 CFStringRef vlan_name
;
88 CFDictionaryRef vlan_options
;
89 SCNetworkInterfaceRef vlan_physical
;
90 CFStringRef vlan_physical_if
;
93 vlan_physical_if
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANInterface
);
94 if (!isA_CFString(vlan_physical_if
)) {
95 // if prefs are confused
99 vlan_tag
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANTag
);
100 if (!isA_CFNumber(vlan_tag
)) {
101 // if prefs are confused
105 // create the VLAN interface
106 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
108 // set physical interface and tag
109 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
110 kIncludeBondInterfaces
);
111 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
112 CFRelease(vlan_physical
);
115 vlan_name
= CFDictionaryGetValue(vlan_info
, kSCPropUserDefinedName
);
116 if (isA_CFString(vlan_name
)) {
117 SCVLANInterfaceSetLocalizedDisplayName(vlan
, vlan_name
);
121 vlan_options
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANOptions
);
122 if (isA_CFDictionary(vlan_options
)) {
123 SCVLANInterfaceSetOptions(vlan
, vlan_options
);
126 // estabish link to the stored configuration
127 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
128 interfacePrivate
->prefs
= CFRetain(myContext
->prefs
);
130 CFArrayAppendValue(myContext
->vlans
, vlan
);
138 add_legacy_configuration(addContextRef myContext
)
142 SCPreferencesRef prefs
;
145 #define VLAN_PREFERENCES_ID CFSTR("VirtualNetworkInterfaces.plist")
146 #define VLAN_PREFERENCES_VLANS CFSTR("VLANs")
147 #define __kVLANInterface_interface CFSTR("interface") // e.g. vlan0, vlan1, ...
148 #define __kVLANInterface_device CFSTR("device") // e.g. en0, en1, ...
149 #define __kVLANInterface_tag CFSTR("tag") // e.g. 1 <= tag <= 4094
150 #define __kVLANInterface_options CFSTR("options") // e.g. UserDefinedName
152 prefs
= SCPreferencesCreate(NULL
, CFSTR("SCVLANInterfaceCopyAll"), VLAN_PREFERENCES_ID
);
157 vlans
= SCPreferencesGetValue(prefs
, VLAN_PREFERENCES_VLANS
);
158 if ((vlans
!= NULL
) && !isA_CFArray(vlans
)) {
159 CFRelease(prefs
); // if the prefs are confused
163 n
= (vlans
!= NULL
) ? CFArrayGetCount(vlans
) : 0;
164 for (i
= 0; i
< n
; i
++) {
165 CFDictionaryRef dict
;
166 SCNetworkInterfacePrivateRef interfacePrivate
;
168 CFDictionaryRef options
;
170 SCVLANInterfaceRef vlan
;
172 CFDictionaryRef vlan_dict
;
173 SCNetworkInterfaceRef vlan_physical
;
174 CFStringRef vlan_physical_if
;
175 CFNumberRef vlan_tag
;
177 vlan_dict
= CFArrayGetValueAtIndex(vlans
, i
);
178 if (!isA_CFDictionary(vlan_dict
)) {
179 continue; // if the prefs are confused
182 vlan_if
= CFDictionaryGetValue(vlan_dict
, __kVLANInterface_interface
);
183 if (!isA_CFString(vlan_if
)) {
184 continue; // if the prefs are confused
187 vlan_physical_if
= CFDictionaryGetValue(vlan_dict
, __kVLANInterface_device
);
188 if (!isA_CFString(vlan_physical_if
)) {
189 continue; // if the prefs are confused
192 vlan_tag
= CFDictionaryGetValue(vlan_dict
, __kVLANInterface_tag
);
193 if (!isA_CFNumber(vlan_tag
)) {
194 continue; // if the prefs are confused
197 // check if this VLAN interface has already been allocated
198 path
= CFStringCreateWithFormat(NULL
,
201 kSCPrefVirtualNetworkInterfaces
,
202 kSCNetworkInterfaceTypeVLAN
,
204 dict
= SCPreferencesPathGetValue(myContext
->prefs
, path
);
206 // if VLAN interface name not available
211 // add a placeholder for the VLAN in the stored preferences
212 dict
= CFDictionaryCreate(NULL
,
214 &kCFTypeDictionaryKeyCallBacks
,
215 &kCFTypeDictionaryValueCallBacks
);
216 ok
= SCPreferencesPathSetValue(myContext
->prefs
, path
, dict
);
220 // if the VLAN could not be saved
224 // create the VLAN interface
225 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
227 // estabish link to the stored configuration
228 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
229 interfacePrivate
->prefs
= CFRetain(myContext
->prefs
);
231 // set the interface and tag (which updates the stored preferences)
232 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
233 kIncludeBondInterfaces
);
234 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
235 CFRelease(vlan_physical
);
237 // set display name (which updates the stored preferences)
238 options
= CFDictionaryGetValue(vlan_dict
, __kVLANInterface_options
);
239 if (isA_CFDictionary(options
)) {
240 CFStringRef vlan_name
;
242 vlan_name
= CFDictionaryGetValue(options
, CFSTR("VLAN Name"));
243 if (isA_CFString(vlan_name
)) {
244 SCVLANInterfaceSetLocalizedDisplayName(vlan
, vlan_name
);
248 CFArrayAppendValue(myContext
->vlans
, vlan
);
257 static SCVLANInterfaceRef
258 findVLANInterfaceAndTag(SCPreferencesRef prefs
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
262 SCVLANInterfaceRef vlan
= NULL
;
265 vlans
= SCVLANInterfaceCopyAll(prefs
);
267 n
= CFArrayGetCount(vlans
);
268 for (i
= 0; i
< n
; i
++) {
269 SCVLANInterfaceRef config_vlan
;
270 SCNetworkInterfaceRef config_physical
;
271 CFNumberRef config_tag
;
273 config_vlan
= CFArrayGetValueAtIndex(vlans
, i
);
274 config_physical
= SCVLANInterfaceGetPhysicalInterface(config_vlan
);
275 config_tag
= SCVLANInterfaceGetTag(config_vlan
);
277 if ((config_physical
!= NULL
) && (config_tag
!= NULL
)) {
278 if (!CFEqual(physical
, config_physical
)) {
279 // if this VLAN has a different physical interface
283 if (!CFEqual(tag
, config_tag
)) {
284 // if this VLAN has a different tag
288 vlan
= CFRetain(config_vlan
);
299 #pragma mark SCVLANInterface APIs
303 SCVLANInterfaceCopyAll(SCPreferencesRef prefs
)
306 CFDictionaryRef dict
;
309 context
.vlans
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
310 context
.prefs
= prefs
;
312 path
= CFStringCreateWithFormat(NULL
,
315 kSCPrefVirtualNetworkInterfaces
,
316 kSCNetworkInterfaceTypeVLAN
);
317 dict
= SCPreferencesPathGetValue(prefs
, path
);
318 if (isA_CFDictionary(dict
)) {
319 CFDictionaryApplyFunction(dict
, add_configured_interface
, &context
);
321 // no VLAN configuration, upgrade from legacy configuration
322 dict
= CFDictionaryCreate(NULL
,
324 &kCFTypeDictionaryKeyCallBacks
,
325 &kCFTypeDictionaryValueCallBacks
);
326 (void) SCPreferencesPathSetValue(prefs
, path
, dict
);
329 add_legacy_configuration(&context
);
333 return context
.vlans
;
338 addAvailableInterfaces(CFMutableArrayRef available
, CFArrayRef interfaces
,
344 n
= CFArrayGetCount(interfaces
);
345 for (i
= 0; i
< n
; i
++) {
346 SCNetworkInterfaceRef interface
;
347 SCNetworkInterfacePrivateRef interfacePrivate
;
349 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
350 interfacePrivate
= (SCNetworkInterfacePrivateRef
)interface
;
353 && CFSetContainsValue(exclude
, interface
)) {
354 // exclude this interface
357 if (interfacePrivate
->supportsVLAN
) {
358 // if this interface is available
359 CFArrayAppendValue(available
, interface
);
368 SCVLANInterfaceCopyAvailablePhysicalInterfaces()
370 CFMutableArrayRef available
;
371 CFArrayRef bond_interfaces
= NULL
;
372 CFMutableSetRef exclude
= NULL
;
373 CFArrayRef interfaces
;
374 SCPreferencesRef prefs
;
376 available
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
378 prefs
= SCPreferencesCreate(NULL
, CFSTR("SCVLANInterfaceCopyAvailablePhysicalInterfaces"), NULL
);
380 bond_interfaces
= SCBondInterfaceCopyAll(prefs
);
382 if (bond_interfaces
!= NULL
) {
383 exclude
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
384 __SCBondInterfaceListCopyMembers(bond_interfaces
, exclude
);
388 // add real interfaces that aren't part of a bond
389 interfaces
= __SCNetworkInterfaceCopyAll_IONetworkInterface();
390 if (interfaces
!= NULL
) {
391 addAvailableInterfaces(available
, interfaces
, exclude
);
392 CFRelease(interfaces
);
395 // add bond interfaces
396 if (bond_interfaces
!= NULL
) {
397 addAvailableInterfaces(available
, bond_interfaces
, NULL
);
398 CFRelease(bond_interfaces
);
400 if (exclude
!= NULL
) {
409 _SCVLANInterfaceCopyActive(void)
411 struct ifaddrs
*ifap
;
414 CFMutableArrayRef vlans
= NULL
;
416 if (getifaddrs(&ifap
) == -1) {
417 SCLog(TRUE
, LOG_ERR
, CFSTR("getifaddrs() failed: %s"), strerror(errno
));
418 _SCErrorSet(kSCStatusFailed
);
422 s
= inet_dgram_socket();
428 vlans
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
430 for (ifp
= ifap
; ifp
!= NULL
; ifp
= ifp
->ifa_next
) {
431 struct if_data
*if_data
;
433 SCVLANInterfaceRef vlan
;
435 SCNetworkInterfaceRef vlan_physical
;
436 CFStringRef vlan_physical_if
;
437 CFNumberRef vlan_tag
;
438 char vlr_parent
[IFNAMSIZ
+ 1];
442 if_data
= (struct if_data
*)ifp
->ifa_data
;
444 || ifp
->ifa_addr
->sa_family
!= AF_LINK
445 || if_data
->ifi_type
!= IFT_L2VLAN
) {
449 bzero(&ifr
, sizeof(ifr
));
450 bzero(&vreq
, sizeof(vreq
));
451 strncpy(ifr
.ifr_name
, ifp
->ifa_name
, sizeof(ifr
.ifr_name
));
452 ifr
.ifr_data
= (caddr_t
)&vreq
;
454 if (ioctl(s
, SIOCGIFVLAN
, (caddr_t
)&ifr
) == -1) {
455 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl() failed: %s"), strerror(errno
));
458 _SCErrorSet(kSCStatusFailed
);
462 // create the VLAN interface
463 vlan_if
= CFStringCreateWithCString(NULL
, ifp
->ifa_name
, kCFStringEncodingASCII
);
464 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
467 // set the physical interface and tag
468 bzero(&vlr_parent
, sizeof(vlr_parent
));
469 bcopy(vreq
.vlr_parent
, vlr_parent
, IFNAMSIZ
);
470 vlan_physical_if
= CFStringCreateWithCString(NULL
, vlr_parent
, kCFStringEncodingASCII
);
471 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
472 kIncludeBondInterfaces
);
473 CFRelease(vlan_physical_if
);
475 vlr_tag
= vreq
.vlr_tag
;
476 vlan_tag
= CFNumberCreate(NULL
, kCFNumberIntType
, &vlr_tag
);
478 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
479 CFRelease(vlan_physical
);
483 CFArrayAppendValue(vlans
, vlan
);
496 SCVLANInterfaceCreate(SCPreferencesRef prefs
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
498 CFAllocatorRef allocator
;
500 SCNetworkInterfacePrivateRef interfacePrivate
;
501 SCVLANInterfaceRef vlan
;
504 _SCErrorSet(kSCStatusInvalidArgument
);
508 if (!isA_SCNetworkInterface(physical
)) {
509 _SCErrorSet(kSCStatusInvalidArgument
);
513 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
514 if (!interfacePrivate
->supportsVLAN
) {
515 _SCErrorSet(kSCStatusInvalidArgument
);
519 if (isA_CFNumber(tag
)) {
522 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
523 if ((tag_val
< 1) || (tag_val
> 4094)) {
524 _SCErrorSet(kSCStatusInvalidArgument
);
528 _SCErrorSet(kSCStatusInvalidArgument
);
532 // make sure that physical interface and tag are not used
533 vlan
= findVLANInterfaceAndTag(prefs
, physical
, tag
);
536 _SCErrorSet(kSCStatusKeyExists
);
540 allocator
= CFGetAllocator(prefs
);
542 // create a new VLAN using an unused interface name
543 for (i
= 0; vlan
== NULL
; i
++) {
544 CFDictionaryRef dict
;
549 vlan_if
= CFStringCreateWithFormat(allocator
, NULL
, CFSTR("vlan%d"), i
);
550 path
= CFStringCreateWithFormat(allocator
,
553 kSCPrefVirtualNetworkInterfaces
,
554 kSCNetworkInterfaceTypeVLAN
,
556 dict
= SCPreferencesPathGetValue(prefs
, path
);
558 // if VLAN interface name not available
564 // add the VLAN to the stored preferences
565 dict
= CFDictionaryCreate(allocator
,
567 &kCFTypeDictionaryKeyCallBacks
,
568 &kCFTypeDictionaryValueCallBacks
);
569 ok
= SCPreferencesPathSetValue(prefs
, path
, dict
);
573 // if the VLAN could not be saved
575 _SCErrorSet(kSCStatusFailed
);
579 // create the SCVLANInterfaceRef
580 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(allocator
, vlan_if
);
583 // estabish link to the stored configuration
584 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
585 interfacePrivate
->prefs
= CFRetain(prefs
);
587 // set physical interface and tag
588 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, physical
, tag
);
596 SCVLANInterfaceRemove(SCVLANInterfaceRef vlan
)
599 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
603 if (!isA_SCVLANInterface(vlan
)) {
604 _SCErrorSet(kSCStatusInvalidArgument
);
608 if (interfacePrivate
->prefs
== NULL
) {
609 _SCErrorSet(kSCStatusInvalidArgument
);
613 vlan_if
= SCNetworkInterfaceGetBSDName(vlan
);
614 path
= CFStringCreateWithFormat(NULL
,
617 kSCPrefVirtualNetworkInterfaces
,
618 kSCNetworkInterfaceTypeVLAN
,
620 ok
= SCPreferencesPathRemoveValue(interfacePrivate
->prefs
, path
);
627 SCNetworkInterfaceRef
628 SCVLANInterfaceGetPhysicalInterface(SCVLANInterfaceRef vlan
)
630 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
632 if (!isA_SCVLANInterface(vlan
)) {
633 _SCErrorSet(kSCStatusInvalidArgument
);
637 return interfacePrivate
->vlan
.interface
;
642 SCVLANInterfaceGetTag(SCVLANInterfaceRef vlan
)
644 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
646 if (!isA_SCVLANInterface(vlan
)) {
647 _SCErrorSet(kSCStatusInvalidArgument
);
651 return interfacePrivate
->vlan
.tag
;
656 SCVLANInterfaceGetOptions(SCVLANInterfaceRef vlan
)
658 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
660 if (!isA_SCVLANInterface(vlan
)) {
661 _SCErrorSet(kSCStatusInvalidArgument
);
665 return interfacePrivate
->vlan
.options
;
670 SCVLANInterfaceSetPhysicalInterfaceAndTag(SCVLANInterfaceRef vlan
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
672 SCNetworkInterfacePrivateRef interfacePrivate
;
675 if (!isA_SCVLANInterface(vlan
)) {
676 _SCErrorSet(kSCStatusInvalidArgument
);
680 if (!isA_SCNetworkInterface(physical
)) {
681 _SCErrorSet(kSCStatusInvalidArgument
);
685 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
686 if (!interfacePrivate
->supportsVLAN
) {
687 _SCErrorSet(kSCStatusInvalidArgument
);
691 if (isA_CFNumber(tag
)) {
694 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
695 if ((tag_val
< 1) || (tag_val
> 4094)) {
696 _SCErrorSet(kSCStatusInvalidArgument
);
700 _SCErrorSet(kSCStatusInvalidArgument
);
704 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
705 if (interfacePrivate
->prefs
!= NULL
) {
706 SCVLANInterfaceRef config_vlan
;
707 CFDictionaryRef dict
;
708 CFMutableDictionaryRef newDict
;
711 // make sure that physical interface and tag are not used
712 config_vlan
= findVLANInterfaceAndTag(interfacePrivate
->prefs
, physical
, tag
);
713 if (config_vlan
!= NULL
) {
714 if (!CFEqual(vlan
, config_vlan
)) {
715 CFRelease(config_vlan
);
716 _SCErrorSet(kSCStatusKeyExists
);
719 CFRelease(config_vlan
);
722 // set interface/tag in the stored preferences
723 path
= CFStringCreateWithFormat(NULL
,
726 kSCPrefVirtualNetworkInterfaces
,
727 kSCNetworkInterfaceTypeVLAN
,
728 interfacePrivate
->entity_device
);
729 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
730 if (!isA_CFDictionary(dict
)) {
731 // if the prefs are confused
733 _SCErrorSet(kSCStatusFailed
);
737 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
738 CFDictionarySetValue(newDict
,
739 kSCPropVirtualNetworkInterfacesVLANInterface
,
740 SCNetworkInterfaceGetBSDName(physical
));
741 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANTag
, tag
);
742 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
748 SCNetworkInterfacePrivateRef newInterface
;
751 // set physical interface
752 newInterface
= __SCNetworkInterfaceCreateCopy(NULL
,
754 interfacePrivate
->prefs
,
755 interfacePrivate
->serviceID
);
756 save
= interfacePrivate
->vlan
.interface
;
757 interfacePrivate
->vlan
.interface
= (SCNetworkInterfaceRef
)newInterface
;
758 if (save
!= NULL
) CFRelease(save
);
761 save
= interfacePrivate
->vlan
.tag
;
762 interfacePrivate
->vlan
.tag
= CFRetain(tag
);
763 if (save
!= NULL
) CFRelease(save
);
771 SCVLANInterfaceSetLocalizedDisplayName(SCVLANInterfaceRef vlan
, CFStringRef newName
)
773 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
776 if (!isA_SCVLANInterface(vlan
)) {
777 _SCErrorSet(kSCStatusInvalidArgument
);
781 if ((newName
!= NULL
) && !isA_CFString(newName
)) {
782 _SCErrorSet(kSCStatusInvalidArgument
);
786 // set name in the stored preferences
787 if (interfacePrivate
->prefs
!= NULL
) {
788 CFDictionaryRef dict
;
789 CFMutableDictionaryRef newDict
;
792 path
= CFStringCreateWithFormat(NULL
,
795 kSCPrefVirtualNetworkInterfaces
,
796 kSCNetworkInterfaceTypeVLAN
,
797 interfacePrivate
->entity_device
);
798 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
799 if (!isA_CFDictionary(dict
)) {
800 // if the prefs are confused
802 _SCErrorSet(kSCStatusFailed
);
806 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
807 if (newName
!= NULL
) {
808 CFDictionarySetValue(newDict
, kSCPropUserDefinedName
, newName
);
810 CFDictionaryRemoveValue(newDict
, kSCPropUserDefinedName
);
812 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
817 // set name in the SCVLANInterfaceRef
819 if (interfacePrivate
->localized_name
!= NULL
) {
820 CFRelease(interfacePrivate
->localized_name
);
821 interfacePrivate
->localized_name
= NULL
;
823 if (newName
!= NULL
) {
824 interfacePrivate
->localized_name
= CFStringCreateCopy(NULL
, newName
);
833 SCVLANInterfaceSetOptions(SCVLANInterfaceRef vlan
, CFDictionaryRef newOptions
)
835 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
838 if (!isA_SCVLANInterface(vlan
)) {
839 _SCErrorSet(kSCStatusInvalidArgument
);
843 if ((newOptions
!= NULL
) && !isA_CFDictionary(newOptions
)) {
844 _SCErrorSet(kSCStatusInvalidArgument
);
848 // set options in the stored preferences
849 if (interfacePrivate
->prefs
!= NULL
) {
850 CFDictionaryRef dict
;
851 CFMutableDictionaryRef newDict
;
854 path
= CFStringCreateWithFormat(NULL
,
857 kSCPrefVirtualNetworkInterfaces
,
858 kSCNetworkInterfaceTypeVLAN
,
859 interfacePrivate
->entity_device
);
860 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
861 if (!isA_CFDictionary(dict
)) {
862 // if the prefs are confused
864 _SCErrorSet(kSCStatusFailed
);
868 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
869 if (newOptions
!= NULL
) {
870 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
, newOptions
);
872 CFDictionaryRemoveValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
);
874 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
879 // set options in the SCVLANInterfaceRef
881 if (interfacePrivate
->vlan
.options
!= NULL
) {
882 CFRelease(interfacePrivate
->vlan
.options
);
883 interfacePrivate
->vlan
.options
= NULL
;
885 if (newOptions
!= NULL
) {
886 interfacePrivate
->vlan
.options
= CFDictionaryCreateCopy(NULL
, newOptions
);
895 #pragma mark SCVLANInterface management
899 __vlan_set(int s
, CFStringRef interface_if
, CFStringRef physical_if
, CFNumberRef tag
)
905 bzero(&ifr
, sizeof(ifr
));
906 bzero(&vreq
, sizeof(vreq
));
909 (void) _SC_cfstring_to_cstring(interface_if
,
911 sizeof(ifr
.ifr_name
),
912 kCFStringEncodingASCII
);
913 ifr
.ifr_data
= (caddr_t
)&vreq
;
915 // physical interface
916 (void) _SC_cfstring_to_cstring(physical_if
,
918 sizeof(vreq
.vlr_parent
),
919 kCFStringEncodingASCII
);
922 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
923 vreq
.vlr_tag
= tag_val
;
925 // update physical interface and tag
926 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
927 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl(SIOCSIFVLAN) failed: %s"), strerror(errno
));
928 _SCErrorSet(kSCStatusFailed
);
937 __vlan_clear(int s
, CFStringRef interface_if
)
942 bzero(&ifr
, sizeof(ifr
));
943 bzero(&vreq
, sizeof(vreq
));
946 (void) _SC_cfstring_to_cstring(interface_if
,
948 sizeof(ifr
.ifr_name
),
949 kCFStringEncodingASCII
);
950 ifr
.ifr_data
= (caddr_t
)&vreq
;
952 // clear physical interface
953 bzero(&vreq
.vlr_parent
, sizeof(vreq
.vlr_parent
));
958 // update physical interface and tag
959 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
960 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl(SIOCSIFVLAN) failed: %s"), strerror(errno
));
961 _SCErrorSet(kSCStatusFailed
);
970 _SCVLANInterfaceUpdateConfiguration(SCPreferencesRef prefs
)
972 CFArrayRef active
= NULL
;
973 CFArrayRef config
= NULL
;
974 CFMutableDictionaryRef devices
= NULL
;
982 _SCErrorSet(kSCStatusInvalidArgument
);
986 /* configured VLANs */
987 config
= SCVLANInterfaceCopyAll(prefs
);
988 nConfig
= CFArrayGetCount(config
);
990 /* physical interfaces */
991 devices
= CFDictionaryCreateMutable(NULL
,
993 &kCFTypeDictionaryKeyCallBacks
,
994 &kCFTypeDictionaryValueCallBacks
);
997 active
= _SCVLANInterfaceCopyActive();
998 nActive
= CFArrayGetCount(active
);
1000 /* remove any no-longer-configured VLAN interfaces */
1001 for (i
= 0; i
< nActive
; i
++) {
1002 SCVLANInterfaceRef a_vlan
;
1003 CFStringRef a_vlan_if
;
1005 Boolean found
= FALSE
;
1007 a_vlan
= CFArrayGetValueAtIndex(active
, i
);
1008 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
1010 for (j
= 0; j
< nConfig
; j
++) {
1011 SCVLANInterfaceRef c_vlan
;
1012 CFStringRef c_vlan_if
;
1014 c_vlan
= CFArrayGetValueAtIndex(config
, j
);
1015 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
1017 if (CFEqual(a_vlan_if
, c_vlan_if
)) {
1024 // remove VLAN interface
1026 s
= inet_dgram_socket();
1033 if (!__destroyInterface(s
, a_vlan_if
)) {
1040 /* create (and update) configured VLAN interfaces */
1041 for (i
= 0; i
< nConfig
; i
++) {
1042 SCVLANInterfaceRef c_vlan
;
1043 CFStringRef c_vlan_if
;
1044 SCNetworkInterfaceRef c_vlan_physical
;
1045 Boolean found
= FALSE
;
1047 CFBooleanRef supported
;
1049 c_vlan
= CFArrayGetValueAtIndex(config
, i
);
1050 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
1051 c_vlan_physical
= SCVLANInterfaceGetPhysicalInterface(c_vlan
);
1053 if (c_vlan_physical
== NULL
) {
1056 // determine if the physical interface supports VLANs
1057 supported
= CFDictionaryGetValue(devices
, c_vlan_physical
);
1058 if (supported
== NULL
) {
1059 SCNetworkInterfacePrivateRef c_vlan_physicalPrivate
= (SCNetworkInterfacePrivateRef
)c_vlan_physical
;
1061 supported
= c_vlan_physicalPrivate
->supportsVLAN
? kCFBooleanTrue
1063 CFDictionaryAddValue(devices
, c_vlan_physical
, supported
);
1066 for (j
= 0; j
< nActive
; j
++) {
1067 SCVLANInterfaceRef a_vlan
;
1068 CFStringRef a_vlan_if
;
1070 a_vlan
= CFArrayGetValueAtIndex(active
, j
);
1071 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
1073 if (CFEqual(c_vlan_if
, a_vlan_if
)) {
1074 if (!CFEqual(c_vlan
, a_vlan
)) {
1075 // update VLAN interface
1077 s
= inet_dgram_socket();
1085 if (!CFBooleanGetValue(supported
)
1086 || !__vlan_clear(s
, c_vlan_if
)
1087 || !__vlan_set(s
, c_vlan_if
,
1088 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1089 SCVLANInterfaceGetTag(c_vlan
))) {
1090 // something went wrong, try to blow the VLAN away
1091 if (!CFBooleanGetValue(supported
)) {
1092 _SCErrorSet(kSCStatusFailed
);
1094 (void)__destroyInterface(s
, c_vlan_if
);
1104 if (!found
&& CFBooleanGetValue(supported
)) {
1105 // if the physical interface supports VLANs, add new interface
1109 s
= inet_dgram_socket();
1117 created
= __createInterface(s
, c_vlan_if
);
1121 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1122 SCVLANInterfaceGetTag(c_vlan
))) {
1124 // something went wrong, try to blow the VLAN away
1125 (void)__destroyInterface(s
, c_vlan_if
);
1137 if (active
) CFRelease(active
);
1138 if (config
) CFRelease(config
);
1139 if (devices
) CFRelease(devices
);
1140 if (s
!= -1) (void) close(s
);