2 * Copyright (c) 2016-2018 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 * March 1, 2016 Allan Nathanson <ajn@apple.com>
32 #include <mach/mach.h>
33 #include <mach-o/fat.h>
34 #include <mach-o/loader.h>
36 #include <net/if_types.h>
38 #include <sys/ioctl.h>
39 #include <sys/kern_control.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sys_domain.h>
44 #define SC_LOG_HANDLE __log_QoSMarking()
45 #include <SystemConfiguration/SystemConfiguration.h>
46 #include <SystemConfiguration/SCPrivate.h>
47 #include <SystemConfiguration/SCValidation.h>
49 #import <Foundation/Foundation.h>
50 #import <NetworkExtension/NEPolicySession.h>
51 #import <NEHelperClient.h>
53 // define the QoSMarking.bundle Info.plist key containing [application] bundleIDs to be white-listed
54 #define kQoSMarkingBundleIdentifiersAppleAudioVideoCallsKey CFSTR("QoSMarking_AppleAudioVideoCalls_BundleIDs")
56 // define the QoSMarking.bundle Info.plist key containing paths to be white-listed
57 #define kQoSMarkingExecutablePathsAppleAudioVideoCallsKey CFSTR("QoSMarking_AppleAudioVideoCalls_ExecutablePaths")
59 // define the starting "order" value for any QoS Marking NEPolicy rules
60 #define QOS_MARKING_PRIORITY_BLOCK_AV_APPS 1000
61 #define QOS_MARKING_PRIORITY_BLOCK_AV_PATHS 1500
62 #define QOS_MARKING_PRIORITY_BLOCK_APPS 2000
65 static CFStringRef interfacesKey = NULL;
66 static NSArray * qosMarkingAudioVideoCalls_bundleIDs = nil;
67 static NSArray * qosMarkingAudioVideoCalls_executablePaths = nil;
74 __private_extern__ os_log_t
75 __log_QoSMarking(void)
77 static os_log_t log = NULL;
80 log = os_log_create("com.apple.SystemConfiguration", "QoSMarking");
88 #pragma mark QoSMarking support (system)
92 qosMarkingSetPolicyRestriction(const char *ctl, BOOL yn)
94 int restricted = yn ? 1 : 0;
97 ret = sysctlbyname(ctl, NULL, 0, &restricted, sizeof(restricted));
99 SC_log(LOG_NOTICE, "QoS marking policy: sysctl %s=%d", ctl, restricted);
101 if (errno != ENOENT) {
102 SC_log(LOG_ERR, "sysctlbyname() failed: %s", strerror(errno));
111 qosMarkingSetHavePolicies(BOOL havePolicies)
113 qosMarkingSetPolicyRestriction("net.qos.policy.restricted", havePolicies);
119 qosMarkingSetRestrictAVApps(BOOL restrictApps)
121 qosMarkingSetPolicyRestriction("net.qos.policy.restrict_avapps", restrictApps);
127 #pragma mark QoSMarking support (per-interface)
131 supportsQoSMarking(int s, const char *ifname)
135 bzero(&ifr, sizeof(ifr));
136 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
137 if (ioctl(s, SIOCGIFTYPE, (caddr_t)&ifr) == -1) {
138 SC_log(LOG_NOTICE, "%s: ioctl(SIOCGIFTYPE) failed: %s",
141 ifr.ifr_type.ift_type = 0;
142 ifr.ifr_type.ift_family = IFRTYPE_FAMILY_ANY;
143 ifr.ifr_type.ift_subfamily = IFRTYPE_SUBFAMILY_ANY;
146 #if !TARGET_OS_IPHONE
147 if (ifr.ifr_type.ift_family == IFRTYPE_FAMILY_ETHERNET) {
150 #else // !TARGET_OS_IPHONE
151 if ((ifr.ifr_type.ift_family == IFRTYPE_FAMILY_ETHERNET) &&
152 (ifr.ifr_type.ift_subfamily == IFRTYPE_SUBFAMILY_WIFI)) {
155 #endif // !TARGET_OS_IPHONE
162 qosMarkingSetEnabled(int s, const char *ifname, BOOL enabled)
167 bzero(&ifr, sizeof(ifr));
168 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
169 ifr.ifr_qosmarking_enabled = enabled ? 1 : 0;
170 ret = ioctl(s, SIOCSQOSMARKINGENABLED, &ifr);
172 SC_log(LOG_ERR, "%s: ioctl(SIOCSQOSMARKINGENABLED) failed: %s",
182 #pragma mark QoSMarking Policy support
185 @interface QoSMarkingController : NSObject
187 + (QoSMarkingController *)sharedController;
188 - (void)setInterfaces:(NSArray *)interfaces;
189 - (void)setPolicy:(NSDictionary *)policy forInterface:(NSString *)interface;
194 @interface QoSMarkingController()
198 * An array of network interface names on the system/device
200 @property (nonatomic) NSArray * interfaces;
204 * A dictionary for the maintaining the QoS marking policies.
206 * Key : interface [name]
207 * Value : the NEPolicySession* for the interface
209 @property (nonatomic) NSMutableDictionary * policySessions;
213 * A dictionary for the tracking the QoS marking policies.
215 * Key : interface [name]
216 * Value : the [requested] NSDictionary* "policy" for the interface
218 @property (nonatomic) NSMutableDictionary * requested;
222 * Dictionaries for tracking the "enabled" interfaces with QoS [AV]
225 * Key : interface [name]
226 * Value : the enabled NSDictionary* "policy" for the interface
228 @property (nonatomic) NSMutableDictionary * enabled;
229 @property (nonatomic) NSMutableDictionary * enabledAV;
234 @implementation QoSMarkingController
236 - (NEPolicySession *)createPolicySession
238 return [[NEPolicySession alloc] init];
245 - (BOOL)qosMarkingPolicyEnabled:(NSDictionary *)policy forKey:(NSString *)key
249 enabled = policy[key];
250 if (enabled != nil) {
251 if (![enabled isKindOfClass:[NSNumber class]]) {
252 SC_log(LOG_ERR, "%@ not valid", key);
256 // assume "enabled" if no key
260 return enabled.boolValue;
264 - (BOOL)qosMarkingIsEnabled:(NSDictionary *)policy
266 return [self qosMarkingPolicyEnabled:policy
267 forKey:(NSString *)kSCPropNetQoSMarkingEnabled];
271 - (BOOL)qosMarkingIsAppleAudioVideoCallsEnabled:(NSDictionary *)policy
273 return [self qosMarkingPolicyEnabled:policy
274 forKey:(NSString *)kSCPropNetQoSMarkingAppleAudioVideoCalls];
278 - (NSArray *)qosMarkingWhitelistedAppIdentifiers:(NSDictionary *)policy
282 appIDs = policy[(NSString *)kSCPropNetQoSMarkingWhitelistedAppIdentifiers];
283 if ((appIDs != nil) && ![appIDs isKindOfClass:[NSArray class]]) {
284 SC_log(LOG_ERR, "QoSMarkingWhitelistedAppIdentifier list not valid");
288 for (NSString *appID in appIDs) {
289 if ((appID != nil) &&
290 (![appID isKindOfClass:[NSString class]] || (appID.length == 0))) {
291 SC_log(LOG_ERR, "QoSMarkingWhitelistedAppIdentifier not valid");
303 - (NSUUID *)copyUUIDForSingleArch:(int)fd
305 struct mach_header header;
308 if (read(fd, &header, sizeof(header)) != sizeof(header)) {
312 // Go past the 64 bit header if we have a 64 arch
313 if (header.magic == MH_MAGIC_64) {
314 if (lseek(fd, sizeof(uint32_t), SEEK_CUR) == -1) {
315 SC_log(LOG_ERR, "could not lseek() past 64 bit header");
320 // Find LC_UUID in the load commands
321 for (size_t i = 0; i < header.ncmds; i++) {
322 struct load_command lcmd;
324 if (read(fd, &lcmd, sizeof(lcmd)) != sizeof(lcmd)) {
325 SC_log(LOG_ERR, "could not read() load_command");
329 if (lcmd.cmd == LC_UUID) {
330 struct uuid_command uuid_cmd;
332 if (read(fd, uuid_cmd.uuid, sizeof(uuid_t)) != sizeof(uuid_t)) {
333 SC_log(LOG_ERR, "could not read() uuid_command");
337 uuid = [[NSUUID alloc] initWithUUIDBytes:uuid_cmd.uuid];
340 if (lseek(fd, lcmd.cmdsize - sizeof(lcmd), SEEK_CUR) == -1) {
341 SC_log(LOG_ERR, "could not lseek() past load command");
350 #define MAX_NFAT_ARCH 32
352 - (NSArray *)copyUUIDsForFatBinary:(int)fd
354 struct fat_arch * arches = NULL;
355 mach_msg_type_number_t count;
356 struct fat_header hdr;
357 struct host_basic_info hostinfo;
359 NSMutableArray * uuids = nil;
361 // For a fat architecture, we need find the section that is closet to the host cpu
362 bzero(&hostinfo, sizeof(hostinfo));
363 count = HOST_BASIC_INFO_COUNT;
364 kr = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostinfo, &count);
365 if (kr != KERN_SUCCESS) {
366 SC_log(LOG_ERR, "host_info() failed: %d", kr);
370 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
371 SC_log(LOG_ERR, "could not read() fat_header");
375 // Fat header info are always big-endian
376 hdr.nfat_arch = OSSwapInt32(hdr.nfat_arch);
377 if (hdr.nfat_arch > MAX_NFAT_ARCH) {
378 SC_log(LOG_ERR, "fat_header.nfat_arch (%d) > %d", hdr.nfat_arch, MAX_NFAT_ARCH);
382 arches = (struct fat_arch *)malloc(hdr.nfat_arch * sizeof(struct fat_arch));
383 if (arches == NULL) {
384 // if we could not allocate space for architectures
388 uuids = [[NSMutableArray alloc] init];
390 for (size_t i = 0; i < hdr.nfat_arch; ++i) {
391 struct fat_arch arch;
393 if (read(fd, &arch, sizeof(arch)) != sizeof(arch)) {
394 SC_log(LOG_ERR, "could not read() fat_arch");
397 arch.cputype = (int)OSSwapInt32(arch.cputype);
398 arch.offset = OSSwapInt32(arch.offset);
399 memcpy(&arches[i], &arch, sizeof(arch));
402 for (size_t i = 0; i < hdr.nfat_arch; ++i) {
403 struct fat_arch arch;
406 memcpy(&arch, &arches[i], sizeof(arch));
408 if (arch.offset == 0) {
409 SC_log(LOG_ERR, "invalid offset for arch %d", arch.cputype);
413 if (lseek(fd, arch.offset, SEEK_SET) == -1) {
414 SC_log(LOG_ERR, "could not lseek() to arch %d", arch.cputype);
418 uuid = [self copyUUIDForSingleArch:fd];
420 SC_log(LOG_ERR, "could not get uuid for arch %d", arch.cputype);
424 if (arch.cputype == hostinfo.cpu_type) {
425 [uuids insertObject:uuid atIndex:0];
427 [uuids addObject:uuid];
433 if (arches != NULL) {
436 if (uuids.count == 0) {
442 - (NSArray *)copyUUIDsForExecutable:(const char *)executablePath
446 NSArray * uuids = nil;
448 if (executablePath == NULL) {
452 fd = open(executablePath, O_RDONLY);
454 if (errno != ENOENT) {
455 SC_log(LOG_ERR, "open(\"%s\", O_RDONLY) failed: %s", executablePath, strerror(errno));
461 // Read the magic format to decide which path to take
462 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
463 SC_log(LOG_ERR, "read() no magic format: %s", executablePath);
467 // Rewind to the beginning
468 lseek(fd, 0, SEEK_SET);
471 uuids = [self copyUUIDsForFatBinary:fd];
479 uuid = [self copyUUIDForSingleArch:fd];
481 SC_log(LOG_ERR, "%s: failed to get UUID for single arch", __FUNCTION__);
501 - (void)addWhitelistedPathPolicy:(NSString *)interface forPath:(NSString *)path order:(uint32_t)order
503 NEPolicyCondition * allInterfacesCondition;
504 NEPolicyResult * result;
505 NEPolicyRouteRule * routeRule;
506 NEPolicySession * session;
509 session = _policySessions[interface];
510 if (session == nil) {
511 SC_log(LOG_ERR, "QoS marking policy: %@: no session", interface);
515 // create QoS route rule
516 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionQoSMarking
517 forInterfaceName:interface];
518 result = [NEPolicyResult routeRules:@[ routeRule ]];
520 // create "all interfaces" condition
521 allInterfacesCondition = [NEPolicyCondition allInterfaces];
523 uuids = [self copyUUIDsForExecutable:[path UTF8String]];
524 if ((uuids == nil) || (uuids.count == 0)) {
525 SC_log(LOG_ERR, "QoS marking policy: %@: could not add path \"%@\"",
531 for (NSUUID *uuid in uuids) {
534 NEPolicyCondition * uuidCondition;
536 // create per-app bundleID-->UUID condition
537 uuidCondition = [NEPolicyCondition effectiveApplication:uuid];
539 // create and add policy
540 policy = [[NEPolicy alloc] initWithOrder:order
542 conditions:@[ uuidCondition, allInterfacesCondition ]];
543 policyID = [session addPolicy:policy];
545 SC_log(LOG_NOTICE, "QoS marking policy: %@: %u: whitelist path \"%@\" (%@)",
552 SC_log(LOG_ERR, "QoS marking policy: %@: could not add whitelist policy for path \"%@\" (%@)",
567 - (NSArray *)copyUUIDsForUUIDMapping:(xpc_object_t)mapping
569 NSMutableArray * uuids = nil;
571 if ((mapping != NULL) &&
572 (xpc_get_type(mapping) == XPC_TYPE_ARRAY)) {
573 uuids = [NSMutableArray array];
575 xpc_array_apply(mapping, ^bool(size_t index, xpc_object_t value) {
576 #pragma unused(index)
577 if ((value != NULL) &&
578 (xpc_get_type(value) == XPC_TYPE_UUID)) {
581 uuid = [[NSUUID alloc] initWithUUIDBytes:xpc_uuid_get_bytes(value)];
582 [uuids addObject:uuid];
587 if (uuids.count == 0) {
596 - (NSArray *)copyUUIDsForBundleID:(NSString *)bundleID
599 xpc_object_t uuidsFromHelper;
601 uuidsFromHelper = NEHelperCacheCopyAppUUIDMapping([bundleID UTF8String], NULL);
603 uuids = [self copyUUIDsForUUIDMapping:uuidsFromHelper];
608 - (void)addWhitelistedAppIdentifierPolicy:(NSString *)interface forApp:(NSString *)appBundleID order:(uint32_t)order
610 NEPolicyCondition * allInterfacesCondition;
611 NEPolicyResult * result;
612 NEPolicyRouteRule * routeRule;
613 NEPolicySession * session;
616 if ([appBundleID hasPrefix:@"/"]) {
617 if (_SC_isAppleInternal()) {
618 // special case executable path handling (if internal)
619 [self addWhitelistedPathPolicy:interface forPath:appBundleID order:order];
625 session = _policySessions[interface];
626 if (session == nil) {
627 SC_log(LOG_ERR, "QoS marking policy: %@: no session", interface);
631 // create QoS route rule
632 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionQoSMarking
633 forInterfaceName:interface];
634 result = [NEPolicyResult routeRules:@[ routeRule ]];
636 // create "all interfaces" condition
637 allInterfacesCondition = [NEPolicyCondition allInterfaces];
639 uuids = [self copyUUIDsForBundleID:appBundleID];
640 if ((uuids == nil) || (uuids.count == 0)) {
641 SC_log(LOG_ERR, "QoS marking policy: %@: could not add bundleID \"%@\"",
647 for (NSUUID *uuid in uuids) {
650 NEPolicyCondition * uuidCondition;
652 // create per-app bundleID-->UUID condition
653 uuidCondition = [NEPolicyCondition effectiveApplication:uuid];
655 // create and add policy
656 policy = [[NEPolicy alloc] initWithOrder:order
658 conditions:@[ uuidCondition, allInterfacesCondition ]];
659 policyID = [session addPolicy:policy];
661 SC_log(LOG_NOTICE, "QoS marking policy: %@: %u: whitelist bundleID \"%@\" (%@)",
668 SC_log(LOG_ERR, "QoS marking policy: %@: could not add whitelist policy for bundleID \"%@\" (%@)",
687 _policySessions = [NSMutableDictionary dictionary];
688 _requested = [NSMutableDictionary dictionary];
689 _enabled = [NSMutableDictionary dictionary];
690 _enabledAV = [NSMutableDictionary dictionary];
699 Have QoS Whitelist AppleAVCalls | net.qos.policy. | Interface Interface Interface
700 Profile Enabled Apps(#) Enabled | restricted restrict_avapps | QoS Enabled NECP rules NECP AV rules
701 ======= ======= ========= ============ + ========== =============== + =========== ========== =============
702 1 [N] | 0 0 | [Y] [N] [N]
704 2 [Y] [N] [0] [N] | 0 0 | [N] [N] [N]
705 3 [Y] [N] [0] [Y] | 0 0 | [N] [N] [N]
707 4 [Y] [N] [> 0] [N] | 0 0 | [N] [N] [N]
708 5 [Y] [N] [> 0] [Y] | 0 0 | [N] [N] [N]
710 6 [Y] [Y] [0] [N] | 1 1 | [Y] [N] [N]
711 7 [Y] [Y] [0] [Y] | 1 0 | [Y] [N] [Y]
713 8 [Y] [Y] [> 0] [N] | 1 1 | [Y] [Y] [N]
714 9 [Y] [Y] [> 0] [Y] | 1 0 | [Y] [Y] [Y]
716 Notes (QoSMarking policy) :
717 * If "QoSEnabled" is not present, assume "Y"
718 * If "QoSMarkingAppleAudioVideoCalls" is not present, assume "Y"
719 * If "QoSMarkingWhitelistedAppIdentifiers" is not present (or empty), assume no whitelisted applications
722 * net.qos.policy.restricted should be "1" when NECP policies are present
723 * net.qos.policy.restrict_avapps should be "1" when "QoSMarkingAppleAudioVideoCalls" is "N"
727 - (void)updatePolicy:(NSDictionary *)reqPolicy forInterface:(NSString *)interface
729 // currently enabled settings
730 NSDictionary * nowPolicy = _enabled[interface];
731 BOOL nowDisabled = false;
732 BOOL nowEnabled = false;
733 BOOL nowAppleAV = false;
735 // requested settings
736 BOOL reqDefault = false;
737 BOOL reqDisabled = false;
738 BOOL reqEnabled = false;
739 BOOL reqAppleAV = false;
741 if (nowPolicy != nil) {
742 if ([self qosMarkingIsEnabled:nowPolicy]) {
743 // if we have an enabled QoS marking policy
746 // if we have a disabled QoS marking policy
750 nowAppleAV = [self qosMarkingIsAppleAudioVideoCallsEnabled:nowPolicy];
753 if (reqPolicy != nil) {
754 if ([self qosMarkingIsEnabled:reqPolicy]) {
755 // if QoS marking policy requested
758 // if QoS marking policy present (but disabled)
762 reqAppleAV = [self qosMarkingIsAppleAudioVideoCallsEnabled:reqPolicy];
767 if ((!nowEnabled && reqDefault ) ||
768 ( nowEnabled != reqEnabled ) ||
769 ( nowDisabled != reqDisabled) ||
770 ( nowEnabled && reqEnabled && ![nowPolicy isEqual:reqPolicy])) {
774 // if we are transitioning to enabled or we have a policy
775 // change, ensure that we rebuild policies
778 if ((nowPolicy != nil) && (reqPolicy == nil)) {
779 SC_log(LOG_NOTICE, "QoS marking policy: %@: remove", interface);
782 // if QoS marking was enabled (for this interface), close session
783 [_policySessions removeObjectForKey:interface];
785 // QoS marking policy for this interface is no longer enabled
786 [_enabled removeObjectForKey:interface];
787 [_enabledAV removeObjectForKey:interface];
790 // update QoSMarking enabled (per-interface)
791 s = socket(AF_INET, SOCK_DGRAM, 0);
793 BOOL enable = reqEnabled || reqDefault;
795 SC_log(LOG_NOTICE, "QoS marking policy: %@: %s%s",
797 enable ? "enable" : "disable",
798 reqDefault ? " (default)" : "");
799 qosMarkingSetEnabled(s, interface.UTF8String, enable);
802 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
811 if (nowAppleAV != reqAppleAV) {
815 curAppIDs = [self qosMarkingWhitelistedAppIdentifiers:nowPolicy];
816 reqAppIDs = [self qosMarkingWhitelistedAppIdentifiers:reqPolicy];
817 if (![curAppIDs isEqual:reqAppIDs]) {
824 NEPolicySession * session;
826 // QoS marking being (or still) enabled for this interface
827 if (_enabled.count == 0) {
828 // if we now have a profile requiring us to check NECP policies
829 qosMarkingSetHavePolicies(true);
832 // the QoS marking policy for this interface is now enabled
833 _enabled[interface] = reqPolicy;
835 SC_log(LOG_NOTICE, "QoS marking policy: %@: %s",
837 nowEnabled ? "update" : "add");
839 // prepare [new] per-interface NECP session
841 session = _policySessions[interface];
842 if ((session == nil) && ((reqAppIDs.count > 0) || reqAppleAV)) {
843 // if we need to add NECP policies
844 session = [self createPolicySession];
845 if (session != nil) {
846 _policySessions[interface] = session;
848 SC_log(LOG_ERR, "%@: failed to create policy session", interface);
852 // zap any previously stored policies
853 if (session != nil) {
854 ok = [session removeAllPolicies];
856 SC_log(LOG_ERR, "%@: could not remove policies", interface);
860 // if needed, add policies for any whitelisted applications
861 if ((session != nil) && (reqAppIDs.count > 0)) {
862 order = QOS_MARKING_PRIORITY_BLOCK_APPS;
863 for (NSString *app in reqAppIDs) {
864 [self addWhitelistedAppIdentifierPolicy:interface forApp:app order:order++];
869 if (_enabledAV.count == 0) {
870 // if we are enabling the marking of Apple AV application
871 // then we do not want to restrict handling of traffic that
872 // cannot be handled by NECP
873 qosMarkingSetRestrictAVApps(false);
876 // the QoS [AV] marking policy for this interface is now enabled
877 _enabledAV[interface] = reqPolicy;
879 if (session != nil) {
880 // if needed, add Apple audio/video application policies
882 order = QOS_MARKING_PRIORITY_BLOCK_AV_APPS;
883 for (NSString *app in qosMarkingAudioVideoCalls_bundleIDs) {
884 [self addWhitelistedAppIdentifierPolicy:interface forApp:app order:order++];
887 order = QOS_MARKING_PRIORITY_BLOCK_AV_PATHS;
888 for (NSString *path in qosMarkingAudioVideoCalls_executablePaths) {
889 [self addWhitelistedPathPolicy:interface forPath:path order:order++];
893 // the QoS [AV] marking policy for this interface is no longer enabled
894 [_enabledAV removeObjectForKey:interface];
896 if (_enabledAV.count == 0) {
897 // if we do not (no longer want to) be marking AV then restrict
898 // handling traffic that cannot be handled by NECP
899 qosMarkingSetRestrictAVApps(true);
903 if (session != nil) {
904 ok = [session apply];
906 SC_log(LOG_ERR, "%@: could not apply new policies", interface);
912 // Restore "default" state if no policies
913 if (_enabled.count == 0) {
914 qosMarkingSetRestrictAVApps(false);
915 qosMarkingSetHavePolicies(false);
921 #pragma mark Update QoSMarking Policy Configuration per [SC] changes
924 + (QoSMarkingController *)sharedController
926 static QoSMarkingController * controller;
927 static dispatch_once_t once;
929 dispatch_once(&once, ^{
930 controller = [[QoSMarkingController alloc] init];
937 - (void)setInterfaces:(NSArray *)newInterfaces
939 NSArray * curInterfaces;
942 s = socket(AF_INET, SOCK_DGRAM, 0);
944 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
948 curInterfaces = _interfaces;
949 _interfaces = newInterfaces;
951 for (NSString *interface in newInterfaces) {
952 if (!supportsQoSMarking(s, interface.UTF8String)) {
953 // skip interfaces that do not support QoS marking
957 if (![curInterfaces containsObject:interface]) {
958 NSDictionary * policy;
961 policy = _requested[interface];
962 [_requested removeObjectForKey:interface]; // make this look like a fresh "add"
963 [self setPolicy:policy forInterface:interface]; // and "set" the new policy
972 - (void)setPolicy:(NSDictionary *)policy forInterface:(NSString *)interface
975 if ([_interfaces containsObject:interface]) {
976 // set (update) per-interface policy
977 [self updatePolicy:policy forInterface:interface];
980 // track policy for future changes
981 [_requested setObject:policy forKey:interface];
983 // remove (update) per-interface policy
984 [self updatePolicy:policy forInterface:interface];
986 // track policy for future changes
987 [_requested removeObjectForKey:interface];
997 #pragma mark Update QoS Marking Policy Plugin
1001 * Function: parse_component
1003 * Given a string 'key' and a string prefix 'prefix',
1004 * return the next component in the slash '/' separated
1008 * 1. key = "a/b/c" prefix = "a/"
1010 * 2. key = "a/b/c" prefix = "a/b/"
1013 static CF_RETURNS_RETAINED CFStringRef
1014 parse_component(CFStringRef key, CFStringRef prefix)
1016 CFMutableStringRef comp;
1019 if (!CFStringHasPrefix(key, prefix)) {
1022 comp = CFStringCreateMutableCopy(NULL, 0, key);
1023 CFStringDelete(comp, CFRangeMake(0, CFStringGetLength(prefix)));
1024 range = CFStringFind(comp, CFSTR("/"), 0);
1025 if (range.location == kCFNotFound) {
1028 range.length = CFStringGetLength(comp) - range.location;
1029 CFStringDelete(comp, range);
1036 qosMarkingConfigChangedCallback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *arg)
1039 os_activity_t activity;
1040 CFDictionaryRef changes;
1042 static CFStringRef prefix = NULL;
1044 activity = os_activity_create("processing QoS marking configuration changes",
1045 OS_ACTIVITY_CURRENT,
1046 OS_ACTIVITY_FLAG_DEFAULT);
1047 os_activity_scope(activity);
1049 if (prefix == NULL) {
1050 prefix = SCDynamicStoreKeyCreate(NULL,
1052 kSCDynamicStoreDomainSetup,
1057 changes = SCDynamicStoreCopyMultiple(store, changedKeys, NULL);
1059 n = CFArrayGetCount(changedKeys);
1060 for (CFIndex i = 0; i < n; i++) {
1063 key = CFArrayGetValueAtIndex(changedKeys, i);
1065 if (CFEqual(key, interfacesKey)) {
1066 CFDictionaryRef info;
1068 info = (changes != NULL) ? CFDictionaryGetValue(changes, key) : NULL;
1069 if (isA_CFDictionary(info) != NULL) {
1070 CFArrayRef interfaces;
1072 interfaces = CFDictionaryGetValue(info, kSCPropNetInterfaces);
1073 if (isA_CFArray(interfaces)) {
1075 QoSMarkingController * controller;
1077 controller = [QoSMarkingController sharedController];
1078 [controller setInterfaces:(__bridge NSArray *)interfaces];
1083 CFStringRef interface;
1085 interface = parse_component(key, prefix);
1086 if (interface != NULL) {
1087 CFDictionaryRef policy;
1089 policy = (changes != NULL) ? CFDictionaryGetValue(changes, key) : NULL;
1091 QoSMarkingController * controller;
1093 controller = [QoSMarkingController sharedController];
1094 [controller setPolicy:(__bridge NSDictionary *)policy
1095 forInterface:(__bridge NSString *)interface];
1097 CFRelease(interface);
1102 if (changes != NULL) {
1112 load_QoSMarking(CFBundleRef bundle, Boolean bundleVerbose)
1114 #pragma unused(bundleVerbose)
1115 CFDictionaryRef dict;
1117 CFMutableArrayRef keys;
1119 CFMutableArrayRef patterns;
1120 CFRunLoopSourceRef rls;
1121 SCDynamicStoreRef store;
1123 SC_log(LOG_DEBUG, "load() called");
1124 SC_log(LOG_DEBUG, " bundle ID = %@", CFBundleGetIdentifier(bundle));
1126 // initialize a few globals
1127 interfacesKey = SCDynamicStoreKeyCreateNetworkInterface(NULL,
1128 kSCDynamicStoreDomainState);
1130 dict = CFBundleGetInfoDictionary(bundle);
1131 if (isA_CFDictionary(dict)) {
1132 CFArrayRef bundleIDs;
1135 bundleIDs = CFDictionaryGetValue(dict, kQoSMarkingBundleIdentifiersAppleAudioVideoCallsKey);
1136 bundleIDs = isA_CFArray(bundleIDs);
1137 qosMarkingAudioVideoCalls_bundleIDs = (__bridge NSArray *)bundleIDs;
1139 paths = CFDictionaryGetValue(dict, kQoSMarkingExecutablePathsAppleAudioVideoCallsKey);
1140 paths = isA_CFArray(paths);
1141 qosMarkingAudioVideoCalls_executablePaths = (__bridge NSArray *)paths;
1144 // open a "configd" store to allow cache updates
1145 store = SCDynamicStoreCreate(NULL,
1146 CFSTR("QoS Marking Configuraton plug-in"),
1147 qosMarkingConfigChangedCallback,
1149 if (store == NULL) {
1150 SC_log(LOG_ERR, "SCDynamicStoreCreate() failed: %s", SCErrorString(SCError()));
1154 // establish notification keys and patterns
1155 keys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1156 patterns = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1158 // ...watch for a change in the list of network interfaces
1159 CFArrayAppendValue(keys, interfacesKey);
1161 // ...watch for (per-interface) QoS marking policy changes
1162 key = SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
1163 kSCDynamicStoreDomainSetup,
1165 kSCEntNetQoSMarkingPolicy);
1166 CFArrayAppendValue(patterns, key);
1169 // register the keys/patterns
1170 ok = SCDynamicStoreSetNotificationKeys(store, keys, patterns);
1172 CFRelease(patterns);
1174 SC_log(LOG_NOTICE, "SCDynamicStoreSetNotificationKeys() failed: %s",
1175 SCErrorString(SCError()));
1179 rls = SCDynamicStoreCreateRunLoopSource(NULL, store, 0);
1181 SC_log(LOG_NOTICE, "SCDynamicStoreCreateRunLoopSource() failed: %s",
1182 SCErrorString(SCError()));
1186 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
1191 if (store != NULL) CFRelease(store);
1200 #pragma mark Standalone test code
1204 main(int argc, char **argv)
1207 _sc_verbose = (argc > 1) ? TRUE : FALSE;
1209 load_QoSMarking(CFBundleGetMainBundle(), (argc > 1) ? TRUE : FALSE);