2 * Copyright (c) 2003-2013, 2015-2018, 2020 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * 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 "SCNetworkConfigurationInternal.h"
39 #include "SCPreferencesInternal.h"
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <net/ethernet.h>
48 #define KERNEL_PRIVATE
50 #include <net/if_var.h>
52 #include <net/if_vlan_var.h>
53 #include <net/if_types.h>
55 /* ---------- VLAN support ---------- */
62 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
64 SC_log(LOG_ERR
, "socket() failed: %s", strerror(errno
));
72 CFMutableArrayRef vlans
;
73 SCPreferencesRef ni_prefs
;
74 SCPreferencesRef prefs
;
75 } addContext
, *addContextRef
;
79 add_configured_interface(const void *key
, const void *value
, void *context
)
81 SCNetworkInterfacePrivateRef interfacePrivate
;
82 addContextRef myContext
= (addContextRef
)context
;
83 SCVLANInterfaceRef vlan
;
84 CFStringRef vlan_if
= (CFStringRef
)key
;
85 CFDictionaryRef vlan_info
= (CFDictionaryRef
)value
;
86 CFStringRef vlan_name
;
87 CFDictionaryRef vlan_options
;
88 SCNetworkInterfaceRef vlan_physical
= NULL
;
89 CFStringRef vlan_physical_if
;
92 vlan_physical_if
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANInterface
);
93 if (!isA_CFString(vlan_physical_if
)) {
94 // if prefs are confused
98 vlan_tag
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANTag
);
99 if (!isA_CFNumber(vlan_tag
)) {
100 // if prefs are confused
104 // create the VLAN interface
105 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
106 assert(vlan
!= NULL
);
108 // set physical interface and tag
109 if (myContext
->ni_prefs
!= NULL
) {
110 vlan_physical
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, myContext
->ni_prefs
,
113 if (vlan_physical
== NULL
) {
114 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
115 kIncludeBondInterfaces
);
117 assert(vlan_physical
!= NULL
);
119 // since we KNOW that the physical interface supported VLANs when
120 // it was first established it's OK to force that state here ...
121 // and this is needed for the case when the interface (e.g. a
122 // dongle) is not currently attached to the system
123 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan_physical
;
124 interfacePrivate
->supportsVLAN
= TRUE
;
126 // and now we associate the physical interface and tag
127 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
128 CFRelease(vlan_physical
);
131 vlan_name
= CFDictionaryGetValue(vlan_info
, kSCPropUserDefinedName
);
132 if (isA_CFString(vlan_name
)) {
133 SCVLANInterfaceSetLocalizedDisplayName(vlan
, vlan_name
);
137 vlan_options
= CFDictionaryGetValue(vlan_info
, kSCPropVirtualNetworkInterfacesVLANOptions
);
138 if (isA_CFDictionary(vlan_options
)) {
139 SCVLANInterfaceSetOptions(vlan
, vlan_options
);
142 // estabish link to the stored configuration
143 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
144 interfacePrivate
->prefs
= CFRetain(myContext
->prefs
);
146 CFArrayAppendValue(myContext
->vlans
, vlan
);
153 static SCVLANInterfaceRef
154 findVLANInterfaceAndTag(SCPreferencesRef prefs
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
158 SCVLANInterfaceRef vlan
= NULL
;
161 vlans
= SCVLANInterfaceCopyAll(prefs
);
163 n
= CFArrayGetCount(vlans
);
164 for (i
= 0; i
< n
; i
++) {
165 SCVLANInterfaceRef config_vlan
;
166 SCNetworkInterfaceRef config_physical
;
167 CFNumberRef config_tag
;
169 config_vlan
= CFArrayGetValueAtIndex(vlans
, i
);
170 config_physical
= SCVLANInterfaceGetPhysicalInterface(config_vlan
);
171 config_tag
= SCVLANInterfaceGetTag(config_vlan
);
173 if ((config_physical
!= NULL
) && (config_tag
!= NULL
)) {
174 if (!CFEqual(physical
, config_physical
)) {
175 // if this VLAN has a different physical interface
179 if (!CFEqual(tag
, config_tag
)) {
180 // if this VLAN has a different tag
184 vlan
= CFRetain(config_vlan
);
195 #pragma mark SCVLANInterface APIs
198 static __inline__
void
199 my_CFDictionaryApplyFunction(CFDictionaryRef theDict
,
200 CFDictionaryApplierFunction applier
,
203 CFAllocatorRef myAllocator
;
204 CFDictionaryRef myDict
;
206 myAllocator
= CFGetAllocator(theDict
);
207 myDict
= CFDictionaryCreateCopy(myAllocator
, theDict
);
208 CFDictionaryApplyFunction(myDict
, applier
, context
);
215 SCVLANInterfaceCopyAll(SCPreferencesRef prefs
)
218 CFDictionaryRef dict
;
219 SCPreferencesRef ni_prefs
;
222 if (__SCPreferencesUsingDefaultPrefs(prefs
)) {
225 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
228 context
.vlans
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
229 context
.ni_prefs
= ni_prefs
;
230 context
.prefs
= prefs
;
232 path
= CFStringCreateWithFormat(NULL
,
235 kSCPrefVirtualNetworkInterfaces
,
236 kSCNetworkInterfaceTypeVLAN
);
237 dict
= SCPreferencesPathGetValue(prefs
, path
);
239 if (isA_CFDictionary(dict
)) {
240 my_CFDictionaryApplyFunction(dict
, add_configured_interface
, &context
);
243 if (ni_prefs
!= NULL
) {
246 return context
.vlans
;
251 addAvailableInterfaces(CFMutableArrayRef available
, CFArrayRef interfaces
,
257 n
= CFArrayGetCount(interfaces
);
258 for (i
= 0; i
< n
; i
++) {
259 SCNetworkInterfaceRef interface
;
260 SCNetworkInterfacePrivateRef interfacePrivate
;
262 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
263 interfacePrivate
= (SCNetworkInterfacePrivateRef
)interface
;
265 if ((excluded
!= NULL
)
266 && CFSetContainsValue(excluded
, interface
)) {
267 // exclude this interface
270 if (interfacePrivate
->supportsVLAN
) {
271 // if this interface is available
272 CFArrayAppendValue(available
, interface
);
281 SCVLANInterfaceCopyAvailablePhysicalInterfaces()
283 CFMutableArrayRef available
;
284 #if !TARGET_OS_IPHONE
285 CFArrayRef bond_interfaces
= NULL
;
286 #endif // !TARGET_OS_IPHONE
287 CFArrayRef bridge_interfaces
= NULL
;
288 CFMutableSetRef excluded
= NULL
;
289 CFArrayRef interfaces
;
290 SCPreferencesRef prefs
;
292 available
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
294 prefs
= SCPreferencesCreate(NULL
, CFSTR("SCVLANInterfaceCopyAvailablePhysicalInterfaces"), NULL
);
296 #if !TARGET_OS_IPHONE
297 bond_interfaces
= SCBondInterfaceCopyAll(prefs
);
298 if (bond_interfaces
!= NULL
) {
299 excluded
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
300 __SCBondInterfaceListCollectMembers(bond_interfaces
, excluded
);
302 #endif // !TARGET_OS_IPHONE
304 bridge_interfaces
= SCBridgeInterfaceCopyAll(prefs
);
305 if (bridge_interfaces
!= NULL
) {
306 if (excluded
== NULL
) {
307 excluded
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
309 __SCBridgeInterfaceListCollectMembers(bridge_interfaces
, excluded
);
315 // add real interfaces that aren't part of a bond or bridge
316 interfaces
= __SCNetworkInterfaceCopyAll_IONetworkInterface(FALSE
);
317 if (interfaces
!= NULL
) {
318 addAvailableInterfaces(available
, interfaces
, excluded
);
319 CFRelease(interfaces
);
322 #if !TARGET_OS_IPHONE
323 // add bond interfaces
324 if (bond_interfaces
!= NULL
) {
325 addAvailableInterfaces(available
, bond_interfaces
, NULL
);
326 CFRelease(bond_interfaces
);
328 #endif // !TARGET_OS_IPHONE
330 // add bridge interfaces
331 if (bridge_interfaces
!= NULL
) {
332 addAvailableInterfaces(available
, bridge_interfaces
, NULL
);
333 CFRelease(bridge_interfaces
);
336 if (excluded
!= NULL
) {
345 _SCVLANInterfaceCopyActive(void)
347 struct ifaddrs
*ifap
;
350 CFMutableArrayRef vlans
= NULL
;
352 if (getifaddrs(&ifap
) == -1) {
353 SC_log(LOG_NOTICE
, "getifaddrs() failed: %s", strerror(errno
));
354 _SCErrorSet(kSCStatusFailed
);
358 s
= inet_dgram_socket();
364 vlans
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
366 for (ifp
= ifap
; ifp
!= NULL
; ifp
= ifp
->ifa_next
) {
367 struct if_data
*if_data
;
369 SCVLANInterfaceRef vlan
;
371 SCNetworkInterfaceRef vlan_physical
;
372 CFStringRef vlan_physical_if
;
373 CFNumberRef vlan_tag
;
374 char vlr_parent
[IFNAMSIZ
];
378 if_data
= (struct if_data
*)ifp
->ifa_data
;
380 || ifp
->ifa_addr
->sa_family
!= AF_LINK
381 || if_data
->ifi_type
!= IFT_L2VLAN
) {
385 memset(&ifr
, 0, sizeof(ifr
));
386 memset(&vreq
, 0, sizeof(vreq
));
387 strlcpy(ifr
.ifr_name
, ifp
->ifa_name
, sizeof(ifr
.ifr_name
));
388 ifr
.ifr_data
= (caddr_t
)&vreq
;
390 if (ioctl(s
, SIOCGIFVLAN
, (caddr_t
)&ifr
) == -1) {
391 SC_log(LOG_NOTICE
, "ioctl(SIOCGIFVLAN) failed: %s", strerror(errno
));
394 _SCErrorSet(kSCStatusFailed
);
398 // create the VLAN interface
399 vlan_if
= CFStringCreateWithCString(NULL
, ifp
->ifa_name
, kCFStringEncodingASCII
);
400 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(NULL
, vlan_if
);
401 assert(vlan
!= NULL
);
404 // set the physical interface and tag
405 strlcpy(vlr_parent
, vreq
.vlr_parent
, sizeof(vlr_parent
));
406 vlan_physical_if
= CFStringCreateWithCString(NULL
, vlr_parent
, kCFStringEncodingASCII
);
407 vlan_physical
= _SCNetworkInterfaceCreateWithBSDName(NULL
, vlan_physical_if
,
408 kIncludeBondInterfaces
);
409 assert(vlan_physical
!= NULL
);
410 CFRelease(vlan_physical_if
);
412 vlr_tag
= vreq
.vlr_tag
;
413 vlan_tag
= CFNumberCreate(NULL
, kCFNumberIntType
, &vlr_tag
);
414 assert(vlan_tag
!= NULL
);
416 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, vlan_physical
, vlan_tag
);
417 CFRelease(vlan_physical
);
421 CFArrayAppendValue(vlans
, vlan
);
436 SCVLANInterfaceCreate(SCPreferencesRef prefs
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
438 CFAllocatorRef allocator
;
440 SCNetworkInterfacePrivateRef interfacePrivate
;
441 SCVLANInterfaceRef vlan
;
444 _SCErrorSet(kSCStatusInvalidArgument
);
448 if (!isA_SCNetworkInterface(physical
)) {
449 _SCErrorSet(kSCStatusInvalidArgument
);
453 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
454 if (!interfacePrivate
->supportsVLAN
) {
455 if (!__SCPreferencesUsingDefaultPrefs(prefs
)) {
456 interfacePrivate
->supportsVLAN
= TRUE
;
458 _SCErrorSet(kSCStatusInvalidArgument
);
463 if (isA_CFNumber(tag
)) {
466 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
467 if ((tag_val
< 1) || (tag_val
> 4094)) {
468 _SCErrorSet(kSCStatusInvalidArgument
);
472 _SCErrorSet(kSCStatusInvalidArgument
);
476 // make sure that physical interface and tag are not used
477 vlan
= findVLANInterfaceAndTag(prefs
, physical
, tag
);
480 _SCErrorSet(kSCStatusKeyExists
);
484 allocator
= CFGetAllocator(prefs
);
486 // create a new VLAN using an unused interface name
487 for (i
= 0; vlan
== NULL
; i
++) {
488 CFDictionaryRef dict
;
493 vlan_if
= CFStringCreateWithFormat(allocator
, NULL
, CFSTR("vlan%ld"), i
);
494 path
= CFStringCreateWithFormat(allocator
,
497 kSCPrefVirtualNetworkInterfaces
,
498 kSCNetworkInterfaceTypeVLAN
,
500 dict
= SCPreferencesPathGetValue(prefs
, path
);
502 // if VLAN interface name not available
508 // add the VLAN to the stored preferences
509 dict
= CFDictionaryCreate(allocator
,
511 &kCFTypeDictionaryKeyCallBacks
,
512 &kCFTypeDictionaryValueCallBacks
);
513 ok
= SCPreferencesPathSetValue(prefs
, path
, dict
);
517 // if the VLAN could not be saved
519 _SCErrorSet(kSCStatusFailed
);
523 // create the SCVLANInterfaceRef
524 vlan
= (SCVLANInterfaceRef
)_SCVLANInterfaceCreatePrivate(allocator
, vlan_if
);
527 // estabish link to the stored configuration
528 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
529 interfacePrivate
->prefs
= CFRetain(prefs
);
531 // set physical interface and tag
532 SCVLANInterfaceSetPhysicalInterfaceAndTag(vlan
, physical
, tag
);
540 SCVLANInterfaceRemove(SCVLANInterfaceRef vlan
)
543 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
547 if (!isA_SCVLANInterface(vlan
)) {
548 _SCErrorSet(kSCStatusInvalidArgument
);
552 if (interfacePrivate
->prefs
== NULL
) {
553 _SCErrorSet(kSCStatusInvalidArgument
);
557 vlan_if
= SCNetworkInterfaceGetBSDName(vlan
);
558 path
= CFStringCreateWithFormat(NULL
,
561 kSCPrefVirtualNetworkInterfaces
,
562 kSCNetworkInterfaceTypeVLAN
,
564 ok
= SCPreferencesPathRemoveValue(interfacePrivate
->prefs
, path
);
571 SCNetworkInterfaceRef
572 SCVLANInterfaceGetPhysicalInterface(SCVLANInterfaceRef vlan
)
574 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
576 if (!isA_SCVLANInterface(vlan
)) {
577 _SCErrorSet(kSCStatusInvalidArgument
);
581 return interfacePrivate
->vlan
.interface
;
586 SCVLANInterfaceGetTag(SCVLANInterfaceRef vlan
)
588 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
590 if (!isA_SCVLANInterface(vlan
)) {
591 _SCErrorSet(kSCStatusInvalidArgument
);
595 return interfacePrivate
->vlan
.tag
;
600 SCVLANInterfaceGetOptions(SCVLANInterfaceRef vlan
)
602 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
604 if (!isA_SCVLANInterface(vlan
)) {
605 _SCErrorSet(kSCStatusInvalidArgument
);
609 return interfacePrivate
->vlan
.options
;
614 SCVLANInterfaceSetPhysicalInterfaceAndTag(SCVLANInterfaceRef vlan
, SCNetworkInterfaceRef physical
, CFNumberRef tag
)
616 SCNetworkInterfacePrivateRef interfacePrivate
;
618 SCPreferencesRef prefs
;
620 if (!isA_SCVLANInterface(vlan
)) {
621 _SCErrorSet(kSCStatusInvalidArgument
);
625 if (!isA_SCNetworkInterface(physical
)) {
626 _SCErrorSet(kSCStatusInvalidArgument
);
630 interfacePrivate
= (SCNetworkInterfacePrivateRef
)physical
;
631 prefs
= interfacePrivate
->prefs
;
633 if (!interfacePrivate
->supportsVLAN
) {
634 if (!__SCPreferencesUsingDefaultPrefs(prefs
)) {
635 interfacePrivate
->supportsVLAN
= TRUE
;
637 _SCErrorSet(kSCStatusInvalidArgument
);
642 if (isA_CFNumber(tag
)) {
645 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
646 if ((tag_val
< 1) || (tag_val
> 4094)) {
647 _SCErrorSet(kSCStatusInvalidArgument
);
651 _SCErrorSet(kSCStatusInvalidArgument
);
655 interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
656 if (interfacePrivate
->prefs
!= NULL
) {
657 SCVLANInterfaceRef config_vlan
;
658 CFDictionaryRef dict
;
659 CFMutableDictionaryRef newDict
;
662 // make sure that physical interface and tag are not used
663 config_vlan
= findVLANInterfaceAndTag(interfacePrivate
->prefs
, physical
, tag
);
664 if (config_vlan
!= NULL
) {
665 if (!CFEqual(vlan
, config_vlan
)) {
666 CFRelease(config_vlan
);
667 _SCErrorSet(kSCStatusKeyExists
);
670 CFRelease(config_vlan
);
673 // set interface/tag in the stored preferences
674 path
= CFStringCreateWithFormat(NULL
,
677 kSCPrefVirtualNetworkInterfaces
,
678 kSCNetworkInterfaceTypeVLAN
,
679 interfacePrivate
->entity_device
);
680 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
681 if (!isA_CFDictionary(dict
)) {
682 // if the prefs are confused
684 _SCErrorSet(kSCStatusFailed
);
688 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
689 CFDictionarySetValue(newDict
,
690 kSCPropVirtualNetworkInterfacesVLANInterface
,
691 SCNetworkInterfaceGetBSDName(physical
));
692 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANTag
, tag
);
693 if (!CFEqual(dict
, newDict
)) {
694 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
701 SCNetworkInterfacePrivateRef newInterface
;
704 // set physical interface
705 newInterface
= __SCNetworkInterfaceCreateCopy(NULL
,
707 interfacePrivate
->prefs
,
708 interfacePrivate
->serviceID
);
709 save
= interfacePrivate
->vlan
.interface
;
710 interfacePrivate
->vlan
.interface
= (SCNetworkInterfaceRef
)newInterface
;
711 if (save
!= NULL
) CFRelease(save
);
714 save
= interfacePrivate
->vlan
.tag
;
715 interfacePrivate
->vlan
.tag
= CFRetain(tag
);
716 if (save
!= NULL
) CFRelease(save
);
724 SCVLANInterfaceSetLocalizedDisplayName(SCVLANInterfaceRef vlan
, CFStringRef newName
)
726 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
729 if (!isA_SCVLANInterface(vlan
)) {
730 _SCErrorSet(kSCStatusInvalidArgument
);
734 if ((newName
!= NULL
) && !isA_CFString(newName
)) {
735 _SCErrorSet(kSCStatusInvalidArgument
);
739 // set name in the stored preferences
740 if (interfacePrivate
->prefs
!= NULL
) {
741 CFDictionaryRef dict
;
742 CFMutableDictionaryRef newDict
;
745 path
= CFStringCreateWithFormat(NULL
,
748 kSCPrefVirtualNetworkInterfaces
,
749 kSCNetworkInterfaceTypeVLAN
,
750 interfacePrivate
->entity_device
);
751 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
752 if (!isA_CFDictionary(dict
)) {
753 // if the prefs are confused
755 _SCErrorSet(kSCStatusFailed
);
759 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
760 if (newName
!= NULL
) {
761 CFDictionarySetValue(newDict
, kSCPropUserDefinedName
, newName
);
763 CFDictionaryRemoveValue(newDict
, kSCPropUserDefinedName
);
765 if (!CFEqual(dict
, newDict
)) {
766 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
772 // set name in the SCVLANInterfaceRef
774 if (interfacePrivate
->localized_name
!= NULL
) {
775 CFRelease(interfacePrivate
->localized_name
);
776 interfacePrivate
->localized_name
= NULL
;
778 if (newName
!= NULL
) {
779 interfacePrivate
->localized_name
= CFStringCreateCopy(NULL
, newName
);
788 SCVLANInterfaceSetOptions(SCVLANInterfaceRef vlan
, CFDictionaryRef newOptions
)
790 SCNetworkInterfacePrivateRef interfacePrivate
= (SCNetworkInterfacePrivateRef
)vlan
;
793 if (!isA_SCVLANInterface(vlan
)) {
794 _SCErrorSet(kSCStatusInvalidArgument
);
798 if ((newOptions
!= NULL
) && !isA_CFDictionary(newOptions
)) {
799 _SCErrorSet(kSCStatusInvalidArgument
);
803 // set options in the stored preferences
804 if (interfacePrivate
->prefs
!= NULL
) {
805 CFDictionaryRef dict
;
806 CFMutableDictionaryRef newDict
;
809 path
= CFStringCreateWithFormat(NULL
,
812 kSCPrefVirtualNetworkInterfaces
,
813 kSCNetworkInterfaceTypeVLAN
,
814 interfacePrivate
->entity_device
);
815 dict
= SCPreferencesPathGetValue(interfacePrivate
->prefs
, path
);
816 if (!isA_CFDictionary(dict
)) {
817 // if the prefs are confused
819 _SCErrorSet(kSCStatusFailed
);
823 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
824 if (newOptions
!= NULL
) {
825 CFDictionarySetValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
, newOptions
);
827 CFDictionaryRemoveValue(newDict
, kSCPropVirtualNetworkInterfacesVLANOptions
);
829 if (!CFEqual(dict
, newDict
)) {
830 ok
= SCPreferencesPathSetValue(interfacePrivate
->prefs
, path
, newDict
);
836 // set options in the SCVLANInterfaceRef
838 if (interfacePrivate
->vlan
.options
!= NULL
) {
839 CFRelease(interfacePrivate
->vlan
.options
);
840 interfacePrivate
->vlan
.options
= NULL
;
842 if (newOptions
!= NULL
) {
843 interfacePrivate
->vlan
.options
= CFDictionaryCreateCopy(NULL
, newOptions
);
852 #pragma mark SCVLANInterface management
856 __vlan_set(int s
, CFStringRef interface_if
, CFStringRef physical_if
, CFNumberRef tag
)
862 memset(&ifr
, 0, sizeof(ifr
));
863 memset(&vreq
, 0, sizeof(vreq
));
866 (void) _SC_cfstring_to_cstring(interface_if
,
868 sizeof(ifr
.ifr_name
),
869 kCFStringEncodingASCII
);
870 ifr
.ifr_data
= (caddr_t
)&vreq
;
872 // physical interface
873 (void) _SC_cfstring_to_cstring(physical_if
,
875 sizeof(vreq
.vlr_parent
),
876 kCFStringEncodingASCII
);
879 CFNumberGetValue(tag
, kCFNumberIntType
, &tag_val
);
880 vreq
.vlr_tag
= tag_val
;
882 // update physical interface and tag
883 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
884 SC_log(LOG_NOTICE
, "ioctl(SIOCSIFVLAN) failed: %s", strerror(errno
));
885 _SCErrorSet(kSCStatusFailed
);
894 __vlan_clear(int s
, CFStringRef interface_if
)
899 memset(&ifr
, 0, sizeof(ifr
));
900 memset(&vreq
, 0, sizeof(vreq
));
903 (void) _SC_cfstring_to_cstring(interface_if
,
905 sizeof(ifr
.ifr_name
),
906 kCFStringEncodingASCII
);
907 ifr
.ifr_data
= (caddr_t
)&vreq
;
909 // clear physical interface
910 memset(&vreq
.vlr_parent
, 0, sizeof(vreq
.vlr_parent
));
915 // update physical interface and tag
916 if (ioctl(s
, SIOCSIFVLAN
, (caddr_t
)&ifr
) == -1) {
917 SC_log(LOG_NOTICE
, "ioctl(SIOCSIFVLAN) failed: %s", strerror(errno
));
918 _SCErrorSet(kSCStatusFailed
);
927 _SCVLANInterfaceUpdateConfiguration(SCPreferencesRef prefs
)
929 CFArrayRef active
= NULL
;
930 CFArrayRef config
= NULL
;
931 CFMutableDictionaryRef devices
= NULL
;
939 _SCErrorSet(kSCStatusInvalidArgument
);
943 /* configured VLANs */
944 config
= SCVLANInterfaceCopyAll(prefs
);
945 nConfig
= (config
!= NULL
) ? CFArrayGetCount(config
) : 0;
947 /* physical interfaces */
948 devices
= CFDictionaryCreateMutable(NULL
,
950 &kCFTypeDictionaryKeyCallBacks
,
951 &kCFTypeDictionaryValueCallBacks
);
954 active
= _SCVLANInterfaceCopyActive();
955 nActive
= (active
!= NULL
) ? CFArrayGetCount(active
) : 0;
957 /* remove any no-longer-configured VLAN interfaces */
958 for (i
= 0; i
< nActive
; i
++) {
959 SCVLANInterfaceRef a_vlan
;
960 CFStringRef a_vlan_if
;
962 Boolean found
= FALSE
;
964 a_vlan
= CFArrayGetValueAtIndex(active
, i
);
965 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
967 for (j
= 0; j
< nConfig
; j
++) {
968 SCVLANInterfaceRef c_vlan
;
969 CFStringRef c_vlan_if
;
971 c_vlan
= CFArrayGetValueAtIndex(config
, j
);
972 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
974 if (CFEqual(a_vlan_if
, c_vlan_if
)) {
981 // remove VLAN interface
983 s
= inet_dgram_socket();
990 if (!__destroyInterface(s
, a_vlan_if
)) {
997 /* create (and update) configured VLAN interfaces */
998 for (i
= 0; i
< nConfig
; i
++) {
999 SCVLANInterfaceRef c_vlan
;
1000 CFStringRef c_vlan_if
;
1001 SCNetworkInterfaceRef c_vlan_physical
;
1002 Boolean found
= FALSE
;
1004 CFBooleanRef supported
;
1006 c_vlan
= CFArrayGetValueAtIndex(config
, i
);
1007 c_vlan_if
= SCNetworkInterfaceGetBSDName(c_vlan
);
1008 c_vlan_physical
= SCVLANInterfaceGetPhysicalInterface(c_vlan
);
1010 if (c_vlan_physical
== NULL
) {
1013 // determine if the physical interface supports VLANs
1014 supported
= CFDictionaryGetValue(devices
, c_vlan_physical
);
1015 if (supported
== NULL
) {
1016 SCNetworkInterfacePrivateRef c_vlan_physicalPrivate
= (SCNetworkInterfacePrivateRef
)c_vlan_physical
;
1018 supported
= c_vlan_physicalPrivate
->supportsVLAN
? kCFBooleanTrue
1020 CFDictionaryAddValue(devices
, c_vlan_physical
, supported
);
1023 for (j
= 0; j
< nActive
; j
++) {
1024 SCVLANInterfaceRef a_vlan
;
1025 CFStringRef a_vlan_if
;
1027 a_vlan
= CFArrayGetValueAtIndex(active
, j
);
1028 a_vlan_if
= SCNetworkInterfaceGetBSDName(a_vlan
);
1030 if (CFEqual(c_vlan_if
, a_vlan_if
)) {
1031 if (!CFEqual(c_vlan
, a_vlan
)) {
1032 // update VLAN interface
1034 s
= inet_dgram_socket();
1042 if (!CFBooleanGetValue(supported
)
1043 || !__vlan_clear(s
, c_vlan_if
)
1044 || !__vlan_set(s
, c_vlan_if
,
1045 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1046 SCVLANInterfaceGetTag(c_vlan
))) {
1047 // something went wrong, try to blow the VLAN away
1048 if (!CFBooleanGetValue(supported
)) {
1049 _SCErrorSet(kSCStatusFailed
);
1051 (void)__destroyInterface(s
, c_vlan_if
);
1061 if (!found
&& CFBooleanGetValue(supported
)) {
1062 // if the physical interface supports VLANs, add new interface
1066 s
= inet_dgram_socket();
1074 created
= __createInterface(s
, c_vlan_if
);
1078 SCNetworkInterfaceGetBSDName(c_vlan_physical
),
1079 SCVLANInterfaceGetTag(c_vlan
))) {
1081 // something went wrong, try to blow the VLAN away
1082 (void)__destroyInterface(s
, c_vlan_if
);
1094 if (active
) CFRelease(active
);
1095 if (config
) CFRelease(config
);
1096 if (devices
) CFRelease(devices
);
1097 if (s
!= -1) (void) close(s
);