2 * Copyright (c) 2003-2011 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
;
352 if ((excluded
!= NULL
)
353 && CFSetContainsValue(excluded
, 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 CFArrayRef bridge_interfaces
= NULL
;
373 CFMutableSetRef excluded
= NULL
;
374 CFArrayRef interfaces
;
375 SCPreferencesRef prefs
;
377 available
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
379 prefs
= SCPreferencesCreate(NULL
, CFSTR("SCVLANInterfaceCopyAvailablePhysicalInterfaces"), NULL
);
381 bond_interfaces
= SCBondInterfaceCopyAll(prefs
);
382 if (bond_interfaces
!= NULL
) {
383 excluded
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
384 __SCBondInterfaceListCollectMembers(bond_interfaces
, excluded
);
387 bridge_interfaces
= SCBridgeInterfaceCopyAll(prefs
);
388 if (bridge_interfaces
!= NULL
) {
389 if (excluded
== NULL
) {
390 excluded
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
392 __SCBridgeInterfaceListCollectMembers(bridge_interfaces
, excluded
);
398 // add real interfaces that aren't part of a bond or bridge
399 interfaces
= __SCNetworkInterfaceCopyAll_IONetworkInterface();
400 if (interfaces
!= NULL
) {
401 addAvailableInterfaces(available
, interfaces
, excluded
);
402 CFRelease(interfaces
);
405 // add bond interfaces
406 if (bond_interfaces
!= NULL
) {
407 addAvailableInterfaces(available
, bond_interfaces
, NULL
);
408 CFRelease(bond_interfaces
);
411 // add bridge interfaces
412 if (bridge_interfaces
!= NULL
) {
413 addAvailableInterfaces(available
, bridge_interfaces
, NULL
);
414 CFRelease(bridge_interfaces
);
417 if (excluded
!= NULL
) {
426 _SCVLANInterfaceCopyActive(void)
428 struct ifaddrs
*ifap
;
431 CFMutableArrayRef vlans
= NULL
;
433 if (getifaddrs(&ifap
) == -1) {
434 SCLog(TRUE
, LOG_ERR
, CFSTR("getifaddrs() failed: %s"), strerror(errno
));
435 _SCErrorSet(kSCStatusFailed
);
439 s
= inet_dgram_socket();
445 vlans
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
447 for (ifp
= ifap
; ifp
!= NULL
; ifp
= ifp
->ifa_next
) {
448 struct if_data
*if_data
;
450 SCVLANInterfaceRef vlan
;
452 SCNetworkInterfaceRef vlan_physical
;
453 CFStringRef vlan_physical_if
;
454 CFNumberRef vlan_tag
;
455 char vlr_parent
[IFNAMSIZ
];
459 if_data
= (struct if_data
*)ifp
->ifa_data
;
461 || ifp
->ifa_addr
->sa_family
!= AF_LINK
462 || if_data
->ifi_type
!= IFT_L2VLAN
) {
466 bzero(&ifr
, sizeof(ifr
));
467 bzero(&vreq
, sizeof(vreq
));
468 strlcpy(ifr
.ifr_name
, ifp
->ifa_name
, sizeof(ifr
.ifr_name
));
469 ifr
.ifr_data
= (caddr_t
)&vreq
;
471 if (ioctl(s
, SIOCGIFVLAN
, (caddr_t
)&ifr
) == -1) {
472 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl() failed: %s"), strerror(errno
));
475 _SCErrorSet(kSCStatusFailed
);
479 // create the VLAN interface
480 vlan_if
= CFStringCreateWithCString(NULL
, ifp
->ifa_name
, kCFStringEncodingASCII
);
481 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
484 // set the physical interface and tag
485 strlcpy(vlr_parent
, vreq
.vlr_parent
, sizeof(vlr_parent
));
486 vlan_physical_if
= CFStringCreateWithCString(NULL
, vlr_parent
, kCFStringEncodingASCII
);
487 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
488 kIncludeBondInterfaces
);
489 CFRelease(vlan_physical_if
);
491 vlr_tag
= vreq
.vlr_tag
;
492 vlan_tag
= CFNumberCreate(NULL
, kCFNumberIntType
, &vlr_tag
);
494 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
495 CFRelease(vlan_physical
);
499 CFArrayAppendValue(vlans
, vlan
);
512 SCVLANInterfaceCreate(SCPreferencesRef prefs
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
514 CFAllocatorRef allocator
;
516 SCNetworkInterfacePrivateRef interfacePrivate
;
517 SCVLANInterfaceRef vlan
;
520 _SCErrorSet(kSCStatusInvalidArgument
);
524 if (!isA_SCNetworkInterface(physical
)) {
525 _SCErrorSet(kSCStatusInvalidArgument
);
529 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
530 if (!interfacePrivate
->supportsVLAN
) {
531 _SCErrorSet(kSCStatusInvalidArgument
);
535 if (isA_CFNumber(tag
)) {
538 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
539 if ((tag_val
< 1) || (tag_val
> 4094)) {
540 _SCErrorSet(kSCStatusInvalidArgument
);
544 _SCErrorSet(kSCStatusInvalidArgument
);
548 // make sure that physical interface and tag are not used
549 vlan
= findVLANInterfaceAndTag(prefs
, physical
, tag
);
552 _SCErrorSet(kSCStatusKeyExists
);
556 allocator
= CFGetAllocator(prefs
);
558 // create a new VLAN using an unused interface name
559 for (i
= 0; vlan
== NULL
; i
++) {
560 CFDictionaryRef dict
;
565 vlan_if
= CFStringCreateWithFormat(allocator
, NULL
, CFSTR("vlan%d"), i
);
566 path
= CFStringCreateWithFormat(allocator
,
569 kSCPrefVirtualNetworkInterfaces
,
570 kSCNetworkInterfaceTypeVLAN
,
572 dict
= SCPreferencesPathGetValue(prefs
, path
);
574 // if VLAN interface name not available
580 // add the VLAN to the stored preferences
581 dict
= CFDictionaryCreate(allocator
,
583 &kCFTypeDictionaryKeyCallBacks
,
584 &kCFTypeDictionaryValueCallBacks
);
585 ok
= SCPreferencesPathSetValue(prefs
, path
, dict
);
589 // if the VLAN could not be saved
591 _SCErrorSet(kSCStatusFailed
);
595 // create the SCVLANInterfaceRef
596 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(allocator
, vlan_if
);
599 // estabish link to the stored configuration
600 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
601 interfacePrivate
->prefs
= CFRetain(prefs
);
603 // set physical interface and tag
604 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, physical
, tag
);
612 SCVLANInterfaceRemove(SCVLANInterfaceRef vlan
)
615 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
619 if (!isA_SCVLANInterface(vlan
)) {
620 _SCErrorSet(kSCStatusInvalidArgument
);
624 if (interfacePrivate
->prefs
== NULL
) {
625 _SCErrorSet(kSCStatusInvalidArgument
);
629 vlan_if
= SCNetworkInterfaceGetBSDName(vlan
);
630 path
= CFStringCreateWithFormat(NULL
,
633 kSCPrefVirtualNetworkInterfaces
,
634 kSCNetworkInterfaceTypeVLAN
,
636 ok
= SCPreferencesPathRemoveValue(interfacePrivate
->prefs
, path
);
643 SCNetworkInterfaceRef
644 SCVLANInterfaceGetPhysicalInterface(SCVLANInterfaceRef vlan
)
646 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
648 if (!isA_SCVLANInterface(vlan
)) {
649 _SCErrorSet(kSCStatusInvalidArgument
);
653 return interfacePrivate
->vlan
.interface
;
658 SCVLANInterfaceGetTag(SCVLANInterfaceRef vlan
)
660 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
662 if (!isA_SCVLANInterface(vlan
)) {
663 _SCErrorSet(kSCStatusInvalidArgument
);
667 return interfacePrivate
->vlan
.tag
;
672 SCVLANInterfaceGetOptions(SCVLANInterfaceRef vlan
)
674 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
676 if (!isA_SCVLANInterface(vlan
)) {
677 _SCErrorSet(kSCStatusInvalidArgument
);
681 return interfacePrivate
->vlan
.options
;
686 SCVLANInterfaceSetPhysicalInterfaceAndTag(SCVLANInterfaceRef vlan
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
688 SCNetworkInterfacePrivateRef interfacePrivate
;
691 if (!isA_SCVLANInterface(vlan
)) {
692 _SCErrorSet(kSCStatusInvalidArgument
);
696 if (!isA_SCNetworkInterface(physical
)) {
697 _SCErrorSet(kSCStatusInvalidArgument
);
701 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
702 if (!interfacePrivate
->supportsVLAN
) {
703 _SCErrorSet(kSCStatusInvalidArgument
);
707 if (isA_CFNumber(tag
)) {
710 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
711 if ((tag_val
< 1) || (tag_val
> 4094)) {
712 _SCErrorSet(kSCStatusInvalidArgument
);
716 _SCErrorSet(kSCStatusInvalidArgument
);
720 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
721 if (interfacePrivate
->prefs
!= NULL
) {
722 SCVLANInterfaceRef config_vlan
;
723 CFDictionaryRef dict
;
724 CFMutableDictionaryRef newDict
;
727 // make sure that physical interface and tag are not used
728 config_vlan
= findVLANInterfaceAndTag(interfacePrivate
->prefs
, physical
, tag
);
729 if (config_vlan
!= NULL
) {
730 if (!CFEqual(vlan
, config_vlan
)) {
731 CFRelease(config_vlan
);
732 _SCErrorSet(kSCStatusKeyExists
);
735 CFRelease(config_vlan
);
738 // set interface/tag in the stored preferences
739 path
= CFStringCreateWithFormat(NULL
,
742 kSCPrefVirtualNetworkInterfaces
,
743 kSCNetworkInterfaceTypeVLAN
,
744 interfacePrivate
->entity_device
);
745 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
746 if (!isA_CFDictionary(dict
)) {
747 // if the prefs are confused
749 _SCErrorSet(kSCStatusFailed
);
753 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
754 CFDictionarySetValue(newDict
,
755 kSCPropVirtualNetworkInterfacesVLANInterface
,
756 SCNetworkInterfaceGetBSDName(physical
));
757 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANTag
, tag
);
758 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
764 SCNetworkInterfacePrivateRef newInterface
;
767 // set physical interface
768 newInterface
= __SCNetworkInterfaceCreateCopy(NULL
,
770 interfacePrivate
->prefs
,
771 interfacePrivate
->serviceID
);
772 save
= interfacePrivate
->vlan
.interface
;
773 interfacePrivate
->vlan
.interface
= (SCNetworkInterfaceRef
)newInterface
;
774 if (save
!= NULL
) CFRelease(save
);
777 save
= interfacePrivate
->vlan
.tag
;
778 interfacePrivate
->vlan
.tag
= CFRetain(tag
);
779 if (save
!= NULL
) CFRelease(save
);
787 SCVLANInterfaceSetLocalizedDisplayName(SCVLANInterfaceRef vlan
, CFStringRef newName
)
789 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
792 if (!isA_SCVLANInterface(vlan
)) {
793 _SCErrorSet(kSCStatusInvalidArgument
);
797 if ((newName
!= NULL
) && !isA_CFString(newName
)) {
798 _SCErrorSet(kSCStatusInvalidArgument
);
802 // set name in the stored preferences
803 if (interfacePrivate
->prefs
!= NULL
) {
804 CFDictionaryRef dict
;
805 CFMutableDictionaryRef newDict
;
808 path
= CFStringCreateWithFormat(NULL
,
811 kSCPrefVirtualNetworkInterfaces
,
812 kSCNetworkInterfaceTypeVLAN
,
813 interfacePrivate
->entity_device
);
814 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
815 if (!isA_CFDictionary(dict
)) {
816 // if the prefs are confused
818 _SCErrorSet(kSCStatusFailed
);
822 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
823 if (newName
!= NULL
) {
824 CFDictionarySetValue(newDict
, kSCPropUserDefinedName
, newName
);
826 CFDictionaryRemoveValue(newDict
, kSCPropUserDefinedName
);
828 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
833 // set name in the SCVLANInterfaceRef
835 if (interfacePrivate
->localized_name
!= NULL
) {
836 CFRelease(interfacePrivate
->localized_name
);
837 interfacePrivate
->localized_name
= NULL
;
839 if (newName
!= NULL
) {
840 interfacePrivate
->localized_name
= CFStringCreateCopy(NULL
, newName
);
849 SCVLANInterfaceSetOptions(SCVLANInterfaceRef vlan
, CFDictionaryRef newOptions
)
851 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
854 if (!isA_SCVLANInterface(vlan
)) {
855 _SCErrorSet(kSCStatusInvalidArgument
);
859 if ((newOptions
!= NULL
) && !isA_CFDictionary(newOptions
)) {
860 _SCErrorSet(kSCStatusInvalidArgument
);
864 // set options in the stored preferences
865 if (interfacePrivate
->prefs
!= NULL
) {
866 CFDictionaryRef dict
;
867 CFMutableDictionaryRef newDict
;
870 path
= CFStringCreateWithFormat(NULL
,
873 kSCPrefVirtualNetworkInterfaces
,
874 kSCNetworkInterfaceTypeVLAN
,
875 interfacePrivate
->entity_device
);
876 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
877 if (!isA_CFDictionary(dict
)) {
878 // if the prefs are confused
880 _SCErrorSet(kSCStatusFailed
);
884 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
885 if (newOptions
!= NULL
) {
886 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
, newOptions
);
888 CFDictionaryRemoveValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
);
890 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
895 // set options in the SCVLANInterfaceRef
897 if (interfacePrivate
->vlan
.options
!= NULL
) {
898 CFRelease(interfacePrivate
->vlan
.options
);
899 interfacePrivate
->vlan
.options
= NULL
;
901 if (newOptions
!= NULL
) {
902 interfacePrivate
->vlan
.options
= CFDictionaryCreateCopy(NULL
, newOptions
);
911 #pragma mark SCVLANInterface management
915 __vlan_set(int s
, CFStringRef interface_if
, CFStringRef physical_if
, CFNumberRef tag
)
921 bzero(&ifr
, sizeof(ifr
));
922 bzero(&vreq
, sizeof(vreq
));
925 (void) _SC_cfstring_to_cstring(interface_if
,
927 sizeof(ifr
.ifr_name
),
928 kCFStringEncodingASCII
);
929 ifr
.ifr_data
= (caddr_t
)&vreq
;
931 // physical interface
932 (void) _SC_cfstring_to_cstring(physical_if
,
934 sizeof(vreq
.vlr_parent
),
935 kCFStringEncodingASCII
);
938 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
939 vreq
.vlr_tag
= tag_val
;
941 // update physical interface and tag
942 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
943 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl(SIOCSIFVLAN) failed: %s"), strerror(errno
));
944 _SCErrorSet(kSCStatusFailed
);
953 __vlan_clear(int s
, CFStringRef interface_if
)
958 bzero(&ifr
, sizeof(ifr
));
959 bzero(&vreq
, sizeof(vreq
));
962 (void) _SC_cfstring_to_cstring(interface_if
,
964 sizeof(ifr
.ifr_name
),
965 kCFStringEncodingASCII
);
966 ifr
.ifr_data
= (caddr_t
)&vreq
;
968 // clear physical interface
969 bzero(&vreq
.vlr_parent
, sizeof(vreq
.vlr_parent
));
974 // update physical interface and tag
975 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
976 SCLog(TRUE
, LOG_ERR
, CFSTR("ioctl(SIOCSIFVLAN) failed: %s"), strerror(errno
));
977 _SCErrorSet(kSCStatusFailed
);
986 _SCVLANInterfaceUpdateConfiguration(SCPreferencesRef prefs
)
988 CFArrayRef active
= NULL
;
989 CFArrayRef config
= NULL
;
990 CFMutableDictionaryRef devices
= NULL
;
998 _SCErrorSet(kSCStatusInvalidArgument
);
1002 /* configured VLANs */
1003 config
= SCVLANInterfaceCopyAll(prefs
);
1004 nConfig
= (config
!= NULL
) ? CFArrayGetCount(config
) : 0;
1006 /* physical interfaces */
1007 devices
= CFDictionaryCreateMutable(NULL
,
1009 &kCFTypeDictionaryKeyCallBacks
,
1010 &kCFTypeDictionaryValueCallBacks
);
1013 active
= _SCVLANInterfaceCopyActive();
1014 nActive
= (active
!= NULL
) ? CFArrayGetCount(active
) : 0;
1016 /* remove any no-longer-configured VLAN interfaces */
1017 for (i
= 0; i
< nActive
; i
++) {
1018 SCVLANInterfaceRef a_vlan
;
1019 CFStringRef a_vlan_if
;
1021 Boolean found
= FALSE
;
1023 a_vlan
= CFArrayGetValueAtIndex(active
, i
);
1024 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
1026 for (j
= 0; j
< nConfig
; j
++) {
1027 SCVLANInterfaceRef c_vlan
;
1028 CFStringRef c_vlan_if
;
1030 c_vlan
= CFArrayGetValueAtIndex(config
, j
);
1031 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
1033 if (CFEqual(a_vlan_if
, c_vlan_if
)) {
1040 // remove VLAN interface
1042 s
= inet_dgram_socket();
1049 if (!__destroyInterface(s
, a_vlan_if
)) {
1056 /* create (and update) configured VLAN interfaces */
1057 for (i
= 0; i
< nConfig
; i
++) {
1058 SCVLANInterfaceRef c_vlan
;
1059 CFStringRef c_vlan_if
;
1060 SCNetworkInterfaceRef c_vlan_physical
;
1061 Boolean found
= FALSE
;
1063 CFBooleanRef supported
;
1065 c_vlan
= CFArrayGetValueAtIndex(config
, i
);
1066 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
1067 c_vlan_physical
= SCVLANInterfaceGetPhysicalInterface(c_vlan
);
1069 if (c_vlan_physical
== NULL
) {
1072 // determine if the physical interface supports VLANs
1073 supported
= CFDictionaryGetValue(devices
, c_vlan_physical
);
1074 if (supported
== NULL
) {
1075 SCNetworkInterfacePrivateRef c_vlan_physicalPrivate
= (SCNetworkInterfacePrivateRef
)c_vlan_physical
;
1077 supported
= c_vlan_physicalPrivate
->supportsVLAN
? kCFBooleanTrue
1079 CFDictionaryAddValue(devices
, c_vlan_physical
, supported
);
1082 for (j
= 0; j
< nActive
; j
++) {
1083 SCVLANInterfaceRef a_vlan
;
1084 CFStringRef a_vlan_if
;
1086 a_vlan
= CFArrayGetValueAtIndex(active
, j
);
1087 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
1089 if (CFEqual(c_vlan_if
, a_vlan_if
)) {
1090 if (!CFEqual(c_vlan
, a_vlan
)) {
1091 // update VLAN interface
1093 s
= inet_dgram_socket();
1101 if (!CFBooleanGetValue(supported
)
1102 || !__vlan_clear(s
, c_vlan_if
)
1103 || !__vlan_set(s
, c_vlan_if
,
1104 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1105 SCVLANInterfaceGetTag(c_vlan
))) {
1106 // something went wrong, try to blow the VLAN away
1107 if (!CFBooleanGetValue(supported
)) {
1108 _SCErrorSet(kSCStatusFailed
);
1110 (void)__destroyInterface(s
, c_vlan_if
);
1120 if (!found
&& CFBooleanGetValue(supported
)) {
1121 // if the physical interface supports VLANs, add new interface
1125 s
= inet_dgram_socket();
1133 created
= __createInterface(s
, c_vlan_if
);
1137 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1138 SCVLANInterfaceGetTag(c_vlan
))) {
1140 // something went wrong, try to blow the VLAN away
1141 (void)__destroyInterface(s
, c_vlan_if
);
1153 if (active
) CFRelease(active
);
1154 if (config
) CFRelease(config
);
1155 if (devices
) CFRelease(devices
);
1156 if (s
!= -1) (void) close(s
);