Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802/* * Copyright (c) 2004-2019 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */
/* * Modification History * * November 28, 2005 Allan Nathanson <ajn@apple.com> * - public API * * July 22, 2004 Allan Nathanson <ajn@apple.com> * - initial revision */
#include <CoreFoundation/CoreFoundation.h>#include <CoreFoundation/CFRuntime.h>
#include "SCNetworkConfigurationInternal.h"#include "SCPreferencesInternal.h"
#include <ifaddrs.h>#include <pthread.h>#include <unistd.h>#include <sys/types.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <sys/sysctl.h>#include <net/ethernet.h>#define KERNEL_PRIVATE#include <net/if.h>#include <net/if_var.h>#undef KERNEL_PRIVATE#include <net/if_bond_var.h>#include <net/if_types.h>#include <net/if_media.h>#include <net/route.h>
/* ---------- Bond support ---------- */
static intinet_dgram_socket(){ int s;
s = socket(AF_INET, SOCK_DGRAM, 0); if (s == -1) { SC_log(LOG_ERR, "socket() failed: %s", strerror(errno)); }
return s;}
static intsiocgifxmedia(int s, const char * ifname, int * status, int * active){ struct ifmediareq ifmr;
*status = 0; *active = 0; memset(&ifmr, 0, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); if (ioctl(s, SIOCGIFXMEDIA, &ifmr) == -1) { return (-1); } if (ifmr.ifm_count != 0) { *status = ifmr.ifm_status; *active = ifmr.ifm_active; } return (0);}
static struct if_bond_status_req *if_bond_status_req_copy(int s, const char * ifname){ void * buf = NULL; struct if_bond_req ibr; struct if_bond_status_req * ibsr_p; struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); memset((char *)&ibr, 0, sizeof(ibr)); ibr.ibr_op = IF_BOND_OP_GET_STATUS; ibsr_p = &ibr.ibr_ibru.ibru_status; ibsr_p->ibsr_version = IF_BOND_STATUS_REQ_VERSION; ifr.ifr_data = (caddr_t)&ibr;
/* how many of them are there? */ if (ioctl(s, SIOCGIFBOND, (caddr_t)&ifr) == -1) { goto failed; } buf = malloc(sizeof(struct if_bond_status) * ibsr_p->ibsr_total + sizeof(*ibsr_p)); if (buf == NULL) { goto failed; } if (ibsr_p->ibsr_total == 0) { goto done; } ibsr_p->ibsr_count = ibsr_p->ibsr_total; ibsr_p->ibsr_buffer = buf + sizeof(*ibsr_p);
/* get the list */ if (ioctl(s, SIOCGIFBOND, (caddr_t)&ifr) == -1) { goto failed; } done: (*(struct if_bond_status_req *)buf) = *ibsr_p; return ((struct if_bond_status_req *)buf);
failed: if (buf != NULL) { free(buf); } return (NULL);}
static voidadd_interface(CFMutableArrayRef *interfaces, CFStringRef if_name, SCPreferencesRef ni_prefs){ SCNetworkInterfaceRef interface = NULL;
if (*interfaces == NULL) { *interfaces = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); } if (ni_prefs != NULL) { interface = __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL, ni_prefs, if_name); } if (interface == NULL) { interface = _SCNetworkInterfaceCreateWithBSDName(NULL, if_name, kIncludeNoVirtualInterfaces); }
if (interface != NULL) { CFArrayAppendValue(*interfaces, interface); CFRelease(interface); }}
static Boolean_SCBondInterfaceSetMode(SCBondInterfaceRef bond, CFNumberRef mode);
typedef struct { CFMutableArrayRef bonds; SCPreferencesRef ni_prefs; SCPreferencesRef prefs;} addContext, *addContextRef;
static voidadd_configured_interface(const void *key, const void *value, void *context){ SCBondInterfaceRef bond; CFStringRef bond_if = (CFStringRef)key; CFDictionaryRef bond_info = (CFDictionaryRef)value; CFDictionaryRef bond_options; CFIndex i; CFArrayRef interfaces; SCNetworkInterfacePrivateRef interfacePrivate; CFMutableArrayRef members = NULL; CFNumberRef mode; addContextRef myContext = (addContextRef)context; CFStringRef name; CFIndex n;
// create the bond interface bond = (SCBondInterfaceRef)_SCBondInterfaceCreatePrivate(NULL, bond_if);
// add member interfaces interfaces = CFDictionaryGetValue(bond_info, kSCPropVirtualNetworkInterfacesBondInterfaces); n = isA_CFArray(interfaces) ? CFArrayGetCount(interfaces) : 0; for (i = 0; i < n; i++) { CFStringRef member;
member = CFArrayGetValueAtIndex(interfaces, i); if (isA_CFString(member)) { add_interface(&members, member, myContext->ni_prefs); } } if (members != NULL) { __SCBondInterfaceSetMemberInterfaces(bond, members); CFRelease(members); }
// set display name name = CFDictionaryGetValue(bond_info, kSCPropUserDefinedName); if (isA_CFString(name)) { SCBondInterfaceSetLocalizedDisplayName(bond, name); }
// set options bond_options = CFDictionaryGetValue(bond_info, kSCPropVirtualNetworkInterfacesBondOptions); if (isA_CFDictionary(bond_options)) { SCBondInterfaceSetOptions(bond, bond_options); }
// set the mode mode = CFDictionaryGetValue(bond_info, kSCPropVirtualNetworkInterfacesBondMode); _SCBondInterfaceSetMode(bond, isA_CFNumber(mode));
// estabish link to the stored configuration interfacePrivate = (SCNetworkInterfacePrivateRef)bond; interfacePrivate->prefs = CFRetain(myContext->prefs);
CFArrayAppendValue(myContext->bonds, bond); CFRelease(bond);
return;}
#pragma mark -#pragma mark SCBondInterface APIs
static __inline__ voidmy_CFDictionaryApplyFunction(CFDictionaryRef theDict, CFDictionaryApplierFunction applier, void *context){ CFAllocatorRef myAllocator; CFDictionaryRef myDict;
myAllocator = CFGetAllocator(theDict); myDict = CFDictionaryCreateCopy(myAllocator, theDict); CFDictionaryApplyFunction(myDict, applier, context); CFRelease(myDict); return;}
CFArrayRefSCBondInterfaceCopyAll(SCPreferencesRef prefs){ addContext context; CFDictionaryRef dict; SCPreferencesRef ni_prefs; CFStringRef path;
if (__SCPreferencesUsingDefaultPrefs(prefs)) { ni_prefs = NULL; } else { ni_prefs = __SCPreferencesCreateNIPrefsFromPrefs(prefs); }
context.bonds = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); context.prefs = prefs; context.ni_prefs = ni_prefs;
path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond); dict = SCPreferencesPathGetValue(prefs, path); CFRelease(path); if (isA_CFDictionary(dict)) { my_CFDictionaryApplyFunction(dict, add_configured_interface, &context); }
if (ni_prefs != NULL) { CFRelease(ni_prefs); } return context.bonds;}
__private_extern__ void__SCBondInterfaceListCollectMembers(CFArrayRef interfaces, CFMutableSetRef set){ CFIndex i; CFIndex n;
n = CFArrayGetCount(interfaces); for (i = 0; i < n; i++) { SCBondInterfaceRef bondInterface; CFArrayRef members;
bondInterface = CFArrayGetValueAtIndex(interfaces, i); members = SCBondInterfaceGetMemberInterfaces(bondInterface); if (members != NULL) { CFIndex j; CFIndex n_members;
// exclude the member interfaces of this bond n_members = CFArrayGetCount(members); for (j = 0; j < n_members; j++) { SCNetworkInterfaceRef member;
member = CFArrayGetValueAtIndex(members, j); CFSetAddValue(set, member); } }
} return;}
CFArrayRef /* of SCNetworkInterfaceRef's */SCBondInterfaceCopyAvailableMemberInterfaces(SCPreferencesRef prefs){ CFMutableArrayRef available; CFMutableSetRef excluded; CFArrayRef interfaces;
available = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); excluded = CFSetCreateMutable (NULL, 0, &kCFTypeSetCallBacks);
// exclude Bond [member] interfaces interfaces = SCBondInterfaceCopyAll(prefs); if (interfaces != NULL) { __SCBondInterfaceListCollectMembers(interfaces, excluded); CFRelease(interfaces); }
// exclude Bridge [member] interfaces interfaces = SCBridgeInterfaceCopyAll(prefs); if (interfaces != NULL) { __SCBridgeInterfaceListCollectMembers(interfaces, excluded); CFRelease(interfaces); }
// exclude VLAN [physical] interfaces interfaces = SCVLANInterfaceCopyAll(prefs); if (interfaces != NULL) { CFIndex i; CFIndex n;
n = CFArrayGetCount(interfaces); for (i = 0; i < n; i++) { SCVLANInterfaceRef vlanInterface; SCNetworkInterfaceRef physical;
// exclude the physical interface of this VLAN vlanInterface = CFArrayGetValueAtIndex(interfaces, i); physical = SCVLANInterfaceGetPhysicalInterface(vlanInterface); CFSetAddValue(excluded, physical); } CFRelease(interfaces); }
// identify available interfaces interfaces = __SCNetworkInterfaceCopyAll_IONetworkInterface(FALSE); if (interfaces != NULL) { CFIndex i; CFIndex n;
n = CFArrayGetCount(interfaces); for (i = 0; i < n; i++) { SCNetworkInterfaceRef interface; SCNetworkInterfacePrivateRef interfacePrivate;
interface = CFArrayGetValueAtIndex(interfaces, i); interfacePrivate = (SCNetworkInterfacePrivateRef)interface;
if (!interfacePrivate->supportsBond) { // if this interface is not available continue; }
if (CFSetContainsValue(excluded, interface)) { // if excluded continue; }
CFArrayAppendValue(available, interface); } CFRelease(interfaces); }
CFRelease(excluded);
return available;}
CFArrayRef_SCBondInterfaceCopyActive(void){ struct ifaddrs *ifap; struct ifaddrs *ifp; int s; CFMutableArrayRef bonds = NULL;
if (getifaddrs(&ifap) == -1) { _SCErrorSet(errno); SC_log(LOG_NOTICE, "getifaddrs() failed: %s", strerror(errno)); return NULL; }
s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); goto done; }
bonds = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
for (ifp = ifap; ifp != NULL; ifp = ifp->ifa_next) { SCBondInterfaceRef bond; CFStringRef bond_if; struct if_bond_status_req *ibsr_p; struct if_data *if_data; int int_val; CFNumberRef mode; CFMutableArrayRef members = NULL;
if_data = (struct if_data *)ifp->ifa_data; if (if_data == NULL || ifp->ifa_addr->sa_family != AF_LINK || if_data->ifi_type != IFT_IEEE8023ADLAG) { continue; }
ibsr_p = if_bond_status_req_copy(s, ifp->ifa_name); if (ibsr_p == NULL) { if (errno == EBUSY) { continue; } _SCErrorSet(errno); SC_log(LOG_NOTICE, "if_bond_status_req_copy(%s) failed: %s", ifp->ifa_name, strerror(errno)); CFRelease(bonds); bonds = NULL; goto done; }
// create the bond interface bond_if = CFStringCreateWithCString(NULL, ifp->ifa_name, kCFStringEncodingASCII); bond = (SCBondInterfaceRef)_SCBondInterfaceCreatePrivate(NULL, bond_if); CFRelease(bond_if);
// set the mode int_val = ibsr_p->ibsr_mode; mode = CFNumberCreate(NULL, kCFNumberIntType, &int_val); assert(mode != NULL); _SCBondInterfaceSetMode(bond, mode); CFRelease(mode);
// add member interfaces if (ibsr_p->ibsr_total > 0) { int i; struct if_bond_status * ibs_p;
// iterate over each member interface ibs_p = (struct if_bond_status *)ibsr_p->ibsr_buffer; for (i = 0; i < ibsr_p->ibsr_total; i++) { CFStringRef member;
member = CFStringCreateWithCString(NULL, ibs_p[i].ibs_if_name, kCFStringEncodingASCII); add_interface(&members, member, NULL); CFRelease(member); } } free(ibsr_p);
if (members != NULL) { __SCBondInterfaceSetMemberInterfaces(bond, members); CFRelease(members); }
// add bond CFArrayAppendValue(bonds, bond); CFRelease(bond); }
done :
if (s != -1) { (void) close(s); } freeifaddrs(ifap); return bonds;}
SCBondInterfaceRefSCBondInterfaceCreate(SCPreferencesRef prefs){ CFAllocatorRef allocator; SCBondInterfaceRef bond = NULL; CFIndex i;
if (prefs == NULL) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; }
allocator = CFGetAllocator(prefs);
// create a new bond using an unused interface name for (i = 0; bond == NULL; i++) { CFDictionaryRef dict; CFStringRef bond_if; SCNetworkInterfacePrivateRef interfacePrivate; CFMutableDictionaryRef newDict; CFArrayRef newInterfaces; Boolean ok; CFStringRef path;
bond_if = CFStringCreateWithFormat(allocator, NULL, CFSTR("bond%ld"), i); path = CFStringCreateWithFormat(allocator, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, bond_if); dict = SCPreferencesPathGetValue(prefs, path); if (dict != NULL) { // if bond interface name not available CFRelease(path); CFRelease(bond_if); continue; }
// add the bond to the stored preferences newDict = CFDictionaryCreateMutable(allocator, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); newInterfaces = CFArrayCreate(allocator, NULL, 0, &kCFTypeArrayCallBacks); CFDictionaryAddValue(newDict, kSCPropVirtualNetworkInterfacesBondInterfaces, newInterfaces); CFRelease(newInterfaces); ok = SCPreferencesPathSetValue(prefs, path, newDict); CFRelease(newDict); CFRelease(path); if (!ok) { // if the bond could not be saved CFRelease(bond_if); break; }
// create the SCBondInterfaceRef bond = (SCBondInterfaceRef)_SCBondInterfaceCreatePrivate(allocator, bond_if); CFRelease(bond_if);
// estabish link to the stored configuration interfacePrivate = (SCNetworkInterfacePrivateRef)bond; interfacePrivate->prefs = CFRetain(prefs); }
return bond;}
BooleanSCBondInterfaceRemove(SCBondInterfaceRef bond){ CFStringRef bond_if; SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; Boolean ok; CFStringRef path;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if (interfacePrivate->prefs == NULL) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
bond_if = SCNetworkInterfaceGetBSDName(bond); path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, bond_if); ok = SCPreferencesPathRemoveValue(interfacePrivate->prefs, path); CFRelease(path);
return ok;}
CFArrayRefSCBondInterfaceGetMemberInterfaces(SCBondInterfaceRef bond){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; }
return interfacePrivate->bond.interfaces;}
CFDictionaryRefSCBondInterfaceGetOptions(SCBondInterfaceRef bond){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; }
return interfacePrivate->bond.options;}
__private_extern__Boolean__SCBondInterfaceSetMemberInterfaces(SCBondInterfaceRef bond, CFArrayRef members){ CFIndex i; SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; CFIndex n; CFMutableArrayRef newMembers; Boolean ok = TRUE;
n = (members != NULL) ? CFArrayGetCount(members) : 0;
// set member interfaces in the stored preferences if (interfacePrivate->prefs != NULL) { CFDictionaryRef dict; CFMutableDictionaryRef newDict; CFStringRef path;
path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, interfacePrivate->entity_device); dict = SCPreferencesPathGetValue(interfacePrivate->prefs, path); if (!isA_CFDictionary(dict)) { // if the prefs are confused CFRelease(path); _SCErrorSet(kSCStatusFailed); return FALSE; }
newMembers = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); for (i = 0; i < n; i++) { SCNetworkInterfaceRef interface; CFStringRef memberName;
interface = CFArrayGetValueAtIndex(members, i); memberName = SCNetworkInterfaceGetBSDName(interface); CFArrayAppendValue(newMembers, memberName); }
newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); CFDictionarySetValue(newDict, kSCPropVirtualNetworkInterfacesBondInterfaces, newMembers); CFRelease(newMembers); if (!CFEqual(dict, newDict)) { ok = SCPreferencesPathSetValue(interfacePrivate->prefs, path, newDict); } CFRelease(newDict); CFRelease(path); }
if (ok) { newMembers = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); for (i = 0; i < n; i++) { SCNetworkInterfaceRef member; SCNetworkInterfacePrivateRef newMember;
member = CFArrayGetValueAtIndex(members, i); newMember = __SCNetworkInterfaceCreateCopy(NULL, member, interfacePrivate->prefs, interfacePrivate->serviceID); CFArrayAppendValue(newMembers, newMember); CFRelease(newMember); } CFRelease(interfacePrivate->bond.interfaces); interfacePrivate->bond.interfaces = newMembers; }
return ok;}
BooleanSCBondInterfaceSetMemberInterfaces(SCBondInterfaceRef bond, CFArrayRef members){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; Boolean ok; int sc_status = kSCStatusOK;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if ((members != NULL) && !isA_CFArray(members)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if (interfacePrivate->prefs != NULL) { CFArrayRef available; CFArrayRef current; CFIndex i; CFIndex n_available; CFIndex n_current; CFIndex n_members; CFArrayRef services = NULL;
current = SCBondInterfaceGetMemberInterfaces(bond); n_current = (current != NULL) ? CFArrayGetCount(current) : 0;
available = SCBondInterfaceCopyAvailableMemberInterfaces(interfacePrivate->prefs); n_available = (available != NULL) ? CFArrayGetCount(available) : 0;
n_members = (members != NULL) ? CFArrayGetCount(members) : 0; for (i = 0; i < n_members; i++) { SCNetworkInterfaceRef member;
member = CFArrayGetValueAtIndex(members, i);
if ((current != NULL) && CFArrayContainsValue(current, CFRangeMake(0, n_current), member)) { // current members are allowed continue; }
if ((available != NULL) && CFArrayContainsValue(available, CFRangeMake(0, n_available), member)) { // available members are allowed but cannot be associated // with any other network services.
if (services == NULL) { services = __SCNetworkServiceCopyAllEnabled(interfacePrivate->prefs); } if ((services != NULL) && __SCNetworkServiceExistsForInterface(services, member)) { sc_status = kSCStatusKeyExists; break; }
// if available continue; }
// if member not allowed sc_status = kSCStatusInvalidArgument; break; }
if (available != NULL) CFRelease(available); if (services != NULL) CFRelease(services); }
if (sc_status != kSCStatusOK) { _SCErrorSet(sc_status); return FALSE; }
ok = __SCBondInterfaceSetMemberInterfaces(bond, members); return ok;}
BooleanSCBondInterfaceSetLocalizedDisplayName(SCBondInterfaceRef bond, CFStringRef newName){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; Boolean ok = TRUE;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if ((newName != NULL) && !isA_CFString(newName)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
// set name in the stored preferences if (interfacePrivate->prefs != NULL) { CFDictionaryRef dict; CFMutableDictionaryRef newDict; CFStringRef path;
path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, interfacePrivate->entity_device); dict = SCPreferencesPathGetValue(interfacePrivate->prefs, path); if (!isA_CFDictionary(dict)) { // if the prefs are confused CFRelease(path); _SCErrorSet(kSCStatusFailed); return FALSE; }
newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); if (newName != NULL) { CFDictionarySetValue(newDict, kSCPropUserDefinedName, newName); } else { CFDictionaryRemoveValue(newDict, kSCPropUserDefinedName); } if (!CFEqual(dict, newDict)) { ok = SCPreferencesPathSetValue(interfacePrivate->prefs, path, newDict); } CFRelease(newDict); CFRelease(path); }
// set name in the SCBondInterfaceRef if (ok) { if (interfacePrivate->localized_name != NULL) { CFRelease(interfacePrivate->localized_name); interfacePrivate->localized_name = NULL; } if (newName != NULL) { interfacePrivate->localized_name = CFStringCreateCopy(NULL, newName); } }
return ok;}
BooleanSCBondInterfaceSetOptions(SCBondInterfaceRef bond, CFDictionaryRef newOptions){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; Boolean ok = TRUE;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if ((newOptions != NULL) && !isA_CFDictionary(newOptions)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
// set options in the stored preferences if (interfacePrivate->prefs != NULL) { CFDictionaryRef dict; CFMutableDictionaryRef newDict; CFStringRef path;
path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, interfacePrivate->entity_device); dict = SCPreferencesPathGetValue(interfacePrivate->prefs, path); if (!isA_CFDictionary(dict)) { // if the prefs are confused CFRelease(path); _SCErrorSet(kSCStatusFailed); return FALSE; }
newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); if (newOptions != NULL) { CFDictionarySetValue(newDict, kSCPropVirtualNetworkInterfacesBondOptions, newOptions); } else { CFDictionaryRemoveValue(newDict, kSCPropVirtualNetworkInterfacesBondOptions); } if (!CFEqual(dict, newDict)) { ok = SCPreferencesPathSetValue(interfacePrivate->prefs, path, newDict); } CFRelease(newDict); CFRelease(path); }
// set options in the SCBondInterfaceRef if (ok) { if (interfacePrivate->bond.options != NULL) { CFRelease(interfacePrivate->bond.options); interfacePrivate->bond.options = NULL; } if (newOptions != NULL) { interfacePrivate->bond.options = CFDictionaryCreateCopy(NULL, newOptions); } }
return ok;}
static Boolean_SCBondInterfaceSetMode(SCBondInterfaceRef bond, CFNumberRef mode){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond; Boolean needs_release = FALSE; Boolean ok = TRUE;
assert(bond != NULL);
if (mode == NULL) { int mode_num = IF_BOND_MODE_LACP;
mode = CFNumberCreate(NULL, kCFNumberIntType, &mode_num); needs_release = TRUE; }
// set mode in the stored preferences if (interfacePrivate->prefs != NULL) { CFDictionaryRef dict; CFMutableDictionaryRef newDict; CFStringRef path;
path = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/%@/%@"), kSCPrefVirtualNetworkInterfaces, kSCNetworkInterfaceTypeBond, interfacePrivate->entity_device); dict = SCPreferencesPathGetValue(interfacePrivate->prefs, path); if (!isA_CFDictionary(dict)) { // if the prefs are confused CFRelease(path); _SCErrorSet(kSCStatusFailed); ok = FALSE; goto done; } newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); CFDictionarySetValue(newDict, kSCPropVirtualNetworkInterfacesBondMode, mode); if (!CFEqual(dict, newDict)) { ok = SCPreferencesPathSetValue(interfacePrivate->prefs, path, newDict); } CFRelease(newDict); CFRelease(path); }
if (ok) { CFRetain(mode); if (interfacePrivate->bond.mode != NULL) { CFRelease(interfacePrivate->bond.mode); } interfacePrivate->bond.mode = mode; }
done :
if (needs_release) CFRelease(mode); return ok;}
BooleanSCBondInterfaceSetMode(SCBondInterfaceRef bond, CFNumberRef mode){ int mode_num;
if (!isA_SCBondInterface(bond) || !isA_CFNumber(mode)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if (!CFNumberGetValue(mode, kCFNumberIntType, &mode_num)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
switch (mode_num) { case IF_BOND_MODE_LACP: case IF_BOND_MODE_STATIC: break; default: _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
return (_SCBondInterfaceSetMode(bond, mode));}
CFNumberRefSCBondInterfaceGetMode(SCBondInterfaceRef bond){ SCNetworkInterfacePrivateRef interfacePrivate = (SCNetworkInterfacePrivateRef)bond;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; } return (interfacePrivate->bond.mode);}
#pragma mark -#pragma mark SCBondStatus APIs
typedef struct {
// base CFType information CFRuntimeBase cfBase;
// bond status SCBondInterfaceRef bond; CFDictionaryRef status_bond;
// member interfaces and status CFArrayRef interfaces; // of SCNetworkInterfaceRef's CFDictionaryRef status_interfaces; // key = interface, val = interface status)
} SCBondStatusPrivate, * SCBondStatusPrivateRef;
const CFStringRef kSCBondStatusDeviceAggregationStatus = CFSTR("AggregationStatus");const CFStringRef kSCBondStatusDeviceCollecting = CFSTR("Collecting");const CFStringRef kSCBondStatusDeviceDistributing = CFSTR("Distributing");
static CFStringRef __SCBondStatusCopyDescription (CFTypeRef cf);static void __SCBondStatusDeallocate (CFTypeRef cf);static Boolean __SCBondStatusEqual (CFTypeRef cf1, CFTypeRef cf2);
static const CFRuntimeClass __SCBondStatusClass = { 0, // version "BondStatus", // className NULL, // init NULL, // copy __SCBondStatusDeallocate, // dealloc __SCBondStatusEqual, // equal NULL, // hash NULL, // copyFormattingDesc __SCBondStatusCopyDescription // copyDebugDesc};
static CFTypeID __kSCBondStatusTypeID = _kCFRuntimeNotATypeID;
static pthread_once_t bondStatus_init = PTHREAD_ONCE_INIT;
static CFStringRef__SCBondStatusCopyDescription(CFTypeRef cf){ CFAllocatorRef allocator = CFGetAllocator(cf); CFMutableStringRef result; SCBondStatusPrivateRef statusPrivate = (SCBondStatusPrivateRef)cf;
result = CFStringCreateMutable(allocator, 0); CFStringAppendFormat(result, NULL, CFSTR("<SCBondStatus %p [%p]> {"), cf, allocator); CFStringAppendFormat(result, NULL, CFSTR(" bond = %@"), statusPrivate->bond); CFStringAppendFormat(result, NULL, CFSTR(", interface = %@"), statusPrivate->status_bond); CFStringAppendFormat(result, NULL, CFSTR(", members = %@"), statusPrivate->status_interfaces); CFStringAppendFormat(result, NULL, CFSTR(" }"));
return result;}
static void__SCBondStatusDeallocate(CFTypeRef cf){ SCBondStatusPrivateRef statusPrivate = (SCBondStatusPrivateRef)cf;
/* release resources */
CFRelease(statusPrivate->bond); CFRelease(statusPrivate->status_bond); if (statusPrivate->interfaces != NULL) CFRelease(statusPrivate->interfaces); CFRelease(statusPrivate->status_interfaces); return;}
static Boolean__SCBondStatusEqual(CFTypeRef cf1, CFTypeRef cf2){ SCBondStatusPrivateRef status1 = (SCBondStatusPrivateRef)cf1; SCBondStatusPrivateRef status2 = (SCBondStatusPrivateRef)cf2;
if (status1 == status2) return TRUE;
if (!CFEqual(status1->bond, status2->bond)) return FALSE; // if not the same bond
if (!CFEqual(status1->status_bond, status2->status_bond)) return FALSE; // if not the same interface status
if (!CFEqual(status1->status_interfaces, status2->status_interfaces)) return FALSE; // if not the same status of the member interfaces
return TRUE;}
static void__SCBondStatusInitialize(void){ __kSCBondStatusTypeID = _CFRuntimeRegisterClass(&__SCBondStatusClass); return;}
static SCBondStatusRef__SCBondStatusCreatePrivate(CFAllocatorRef __nullable allocator, SCBondInterfaceRef bond, CFDictionaryRef status_bond, CFDictionaryRef status_interfaces){ SCBondStatusPrivateRef statusPrivate; uint32_t size;
/* initialize runtime */ pthread_once(&bondStatus_init, __SCBondStatusInitialize);
/* allocate bond */ size = sizeof(SCBondStatusPrivate) - sizeof(CFRuntimeBase); statusPrivate = (SCBondStatusPrivateRef)_CFRuntimeCreateInstance(allocator, __kSCBondStatusTypeID, size, NULL); if (statusPrivate == NULL) { return NULL; }
/* initialize non-zero/NULL members */ statusPrivate->bond = CFRetain(bond); statusPrivate->status_bond = CFDictionaryCreateCopy(NULL, status_bond); statusPrivate->status_interfaces = CFDictionaryCreateCopy(NULL, status_interfaces);
return (SCBondStatusRef)statusPrivate;}
static __inline__ CFTypeRefisA_SCBondStatus(CFTypeRef obj){ return (isA_CFType(obj, SCBondStatusGetTypeID()));}
CFTypeIDSCBondStatusGetTypeID(){ pthread_once(&bondStatus_init, __SCBondStatusInitialize); /* initialize runtime */ return __kSCBondStatusTypeID;}
#define N_QUICK 16
CFArrayRef /* of SCNetworkInterfaceRef's */SCBondStatusGetMemberInterfaces(SCBondStatusRef bondStatus){ SCBondStatusPrivateRef statusPrivate = (SCBondStatusPrivateRef)bondStatus;
if (!isA_SCBondStatus(bondStatus)) { return NULL; }
if (statusPrivate->interfaces == NULL) { const void * keys_q[N_QUICK]; const void ** keys = keys_q; CFIndex n;
n = CFDictionaryGetCount(statusPrivate->status_interfaces); if (n > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) { keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0); } CFDictionaryGetKeysAndValues(statusPrivate->status_interfaces, keys, NULL); statusPrivate->interfaces = CFArrayCreate(NULL, keys, n, &kCFTypeArrayCallBacks); if (keys != keys_q) { CFAllocatorDeallocate(NULL, keys); } }
return statusPrivate->interfaces;}
CFDictionaryRefSCBondStatusGetInterfaceStatus(SCBondStatusRef bondStatus, SCNetworkInterfaceRef interface){ CFDictionaryRef status = NULL; SCBondStatusPrivateRef statusPrivate = (SCBondStatusPrivateRef)bondStatus;
if (!isA_SCBondStatus(bondStatus)) { return NULL; }
if (interface == NULL) { // return status of the bond status = statusPrivate->status_bond; } else { // return status of the member interface status = CFDictionaryGetValue(statusPrivate->status_interfaces, interface); }
return status;}
SCBondStatusRefSCBondInterfaceCopyStatus(SCBondInterfaceRef bond){ int bond_if_active; int bond_if_status; CFIndex i; struct if_bond_status_req *ibsr_p = NULL; char if_name[IFNAMSIZ]; CFIndex n; CFNumberRef num; int s; struct if_bond_status *scan_p; SCBondStatusRef status = NULL; CFMutableDictionaryRef status_bond; CFMutableDictionaryRef status_interfaces;
if (!isA_SCBondInterface(bond)) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; }
s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); goto done; }
_SC_cfstring_to_cstring(SCNetworkInterfaceGetBSDName(bond), if_name, sizeof(if_name), kCFStringEncodingASCII); if (siocgifxmedia(s, if_name, &bond_if_status, &bond_if_active) == -1) { _SCErrorSet(errno); switch (errno) { case EBUSY : case ENXIO : break; default : SC_log(LOG_NOTICE, "siocgifxmedia(%s) failed: %s", if_name, strerror(errno)); } goto done; } ibsr_p = if_bond_status_req_copy(s, if_name); if (ibsr_p == NULL) { _SCErrorSet(errno); goto done; }
status_bond = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
status_interfaces = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); n = ibsr_p->ibsr_total; for (i = 0, scan_p = (struct if_bond_status *)ibsr_p->ibsr_buffer; i < n; i++, scan_p++) { int collecting = 0; int distributing = 0; SCNetworkInterfaceRef interface; CFStringRef interface_name; struct if_bond_partner_state * ps; CFMutableDictionaryRef status_interface; int status_val;
ps = &scan_p->ibs_partner_state;
if (lacp_actor_partner_state_in_sync(scan_p->ibs_state)) { /* we're in-sync */ status_val = kSCBondStatusOK; if (lacp_actor_partner_state_in_sync(ps->ibps_state)) { /* partner is also in-sync */ if (lacp_actor_partner_state_collecting(scan_p->ibs_state) && lacp_actor_partner_state_distributing(ps->ibps_state)) { /* we're able to collect (receive) frames */ collecting = 1; } if (lacp_actor_partner_state_distributing(scan_p->ibs_state) && lacp_actor_partner_state_collecting(ps->ibps_state)) { /* we're able to distribute (transmit) frames */ distributing = 1; } } } else { int active = 0; int status = 0; static lacp_system zeroes = { {0, 0, 0, 0, 0, 0}};
if (siocgifxmedia(s, scan_p->ibs_if_name, &status, &active) == -1) { switch (errno) { case EBUSY : case ENXIO : break; default : SC_log(LOG_NOTICE, "siocgifxmedia(%s) failed: %s", if_name, strerror(errno)); break; } } if (((status & IFM_AVALID) == 0) || ((status & IFM_ACTIVE) == 0) || ((active & IFM_FDX ) == 0)) { /* link down or not full-duplex */ status_val = kSCBondStatusLinkInvalid; } else if ((ps->ibps_system_priority == 0) && (bcmp(&zeroes, &ps->ibps_system, sizeof(zeroes)) == 0)) { /* no one on the other end of the link */ status_val = kSCBondStatusNoPartner; } else if (active != bond_if_active) { /* the link speed was different */ status_val = kSCBondStatusLinkInvalid; } else { /* partner is not in the active group */ status_val = kSCBondStatusNotInActiveGroup; } }
// interface strlcpy(if_name, scan_p->ibs_if_name, sizeof(if_name)); interface_name = CFStringCreateWithCString(NULL, if_name, kCFStringEncodingASCII); interface = _SCNetworkInterfaceCreateWithBSDName(NULL, interface_name, kIncludeNoVirtualInterfaces); CFRelease(interface_name);
// interface status status_interface = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); num = CFNumberCreate(NULL, kCFNumberIntType, &status_val); CFDictionarySetValue(status_interface, kSCBondStatusDeviceAggregationStatus, num); CFRelease(num); num = CFNumberCreate(NULL, kCFNumberIntType, &collecting); CFDictionarySetValue(status_interface, kSCBondStatusDeviceCollecting, num); CFRelease(num); num = CFNumberCreate(NULL, kCFNumberIntType, &distributing); CFDictionarySetValue(status_interface, kSCBondStatusDeviceDistributing, num); CFRelease(num);
CFDictionarySetValue(status_interfaces, interface, status_interface); CFRelease(interface); CFRelease(status_interface); }
status = __SCBondStatusCreatePrivate(NULL, bond, status_bond, status_interfaces); CFRelease(status_bond); CFRelease(status_interfaces);
done:
if (s != -1) { close(s); } if (ibsr_p != NULL) { free(ibsr_p); } return (SCBondStatusRef)status;}
#pragma mark -#pragma mark SCBondInterface management
static Boolean__bond_set_mode(int s, CFStringRef bond_if, CFNumberRef mode){ struct if_bond_req breq; struct ifreq ifr; int mode_num;
mode_num = IF_BOND_MODE_LACP; if (mode != NULL) { CFNumberGetValue(mode, kCFNumberIntType, &mode_num); }
// bond interface memset(&ifr, 0, sizeof(ifr)); (void) _SC_cfstring_to_cstring(bond_if, ifr.ifr_name, sizeof(ifr.ifr_name), kCFStringEncodingASCII); ifr.ifr_data = (caddr_t)&breq; memset(&breq, 0, sizeof(breq)); breq.ibr_op = IF_BOND_OP_SET_MODE; breq.ibr_ibru.ibru_int_val = mode_num; if (ioctl(s, SIOCSIFBOND, (caddr_t)&ifr) == -1) { _SCErrorSet(errno); SC_log(LOG_ERR, "could not set mode to %@ on bond \"%@\": %s", mode, bond_if, strerror(errno)); return FALSE; }
return TRUE;}
static Boolean__bond_add_interface(int s, CFStringRef bond_if, CFStringRef interface_if){ struct if_bond_req breq; struct ifreq ifr;
// bond interface memset(&ifr, 0, sizeof(ifr)); (void) _SC_cfstring_to_cstring(bond_if, ifr.ifr_name, sizeof(ifr.ifr_name), kCFStringEncodingASCII); ifr.ifr_data = (caddr_t)&breq;
// new bond member memset(&breq, 0, sizeof(breq)); breq.ibr_op = IF_BOND_OP_ADD_INTERFACE; (void) _SC_cfstring_to_cstring(interface_if, breq.ibr_ibru.ibru_if_name, sizeof(breq.ibr_ibru.ibru_if_name), kCFStringEncodingASCII);
// add new bond member if (ioctl(s, SIOCSIFBOND, (caddr_t)&ifr) == -1) { _SCErrorSet(errno); SC_log(LOG_ERR, "could not add interface \"%@\" to bond \"%@\": %s", interface_if, bond_if, strerror(errno)); return FALSE; }
return TRUE;}
static Boolean__bond_remove_interface(int s, CFStringRef bond_if, CFStringRef interface_if){ struct if_bond_req breq; struct ifreq ifr;
// bond interface memset(&ifr, 0, sizeof(ifr)); (void) _SC_cfstring_to_cstring(bond_if, ifr.ifr_name, sizeof(ifr.ifr_name), kCFStringEncodingASCII); ifr.ifr_data = (caddr_t)&breq;
// bond member to remove memset(&breq, 0, sizeof(breq)); breq.ibr_op = IF_BOND_OP_REMOVE_INTERFACE; (void) _SC_cfstring_to_cstring(interface_if, breq.ibr_ibru.ibru_if_name, sizeof(breq.ibr_ibru.ibru_if_name), kCFStringEncodingASCII);
// remove bond member if (ioctl(s, SIOCSIFBOND, (caddr_t)&ifr) == -1) { _SCErrorSet(errno); SC_log(LOG_ERR, "could not remove interface \"%@\" from bond \"%@\": %s", interface_if, bond_if, strerror(errno)); return FALSE; }
return TRUE;}
Boolean_SCBondInterfaceUpdateConfiguration(SCPreferencesRef prefs){ CFArrayRef active = NULL; CFArrayRef config = NULL; CFIndex i; CFIndex nActive; CFIndex nConfig; Boolean ok = TRUE; int s = -1;
if (prefs == NULL) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
/* configured Bonds */ config = SCBondInterfaceCopyAll(prefs); nConfig = (config != NULL) ? CFArrayGetCount(config) : 0;
/* active Bonds */ active = _SCBondInterfaceCopyActive(); nActive = (active != NULL) ? CFArrayGetCount(active) : 0;
/* * remove any no-longer-configured bond interfaces and * any devices associated with a bond that are no longer * associated with a bond. */ for (i = 0; i < nActive; i++) { SCBondInterfaceRef a_bond; CFStringRef a_bond_if; CFIndex j; Boolean found = FALSE;
a_bond = CFArrayGetValueAtIndex(active, i); a_bond_if = SCNetworkInterfaceGetBSDName(a_bond);
for (j = 0; j < nConfig; j++) { SCBondInterfaceRef c_bond; CFStringRef c_bond_if;
c_bond = CFArrayGetValueAtIndex(config, j); c_bond_if = SCNetworkInterfaceGetBSDName(c_bond);
if (CFEqual(a_bond_if, c_bond_if)) { CFIndex a; CFArrayRef a_bond_interfaces; CFIndex a_count; CFArrayRef c_bond_interfaces; CFIndex c_count;
c_bond_interfaces = SCBondInterfaceGetMemberInterfaces(c_bond); c_count = (c_bond_interfaces != NULL) ? CFArrayGetCount(c_bond_interfaces) : 0;
a_bond_interfaces = SCBondInterfaceGetMemberInterfaces(a_bond); a_count = (a_bond_interfaces != NULL) ? CFArrayGetCount(a_bond_interfaces) : 0;
for (a = 0; a < a_count; a++) { SCNetworkInterfaceRef a_interface; CFStringRef a_interface_if;
a_interface = CFArrayGetValueAtIndex(a_bond_interfaces, a); if ((c_count == 0) || !CFArrayContainsValue(c_bond_interfaces, CFRangeMake(0, c_count), a_interface)) { /* * if this device is no longer part * of the bond. */ if (s == -1) { s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); ok = FALSE; goto done; } }
a_interface_if = SCNetworkInterfaceGetBSDName(a_interface); if (!__bond_remove_interface(s, a_bond_if, a_interface_if)) { ok = FALSE; } } }
found = TRUE; break; } }
if (!found) { /* * if this interface is no longer configured */ if (s == -1) { s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); ok = FALSE; goto done; } }
if (!__destroyInterface(s, a_bond_if)) { _SCErrorSet(errno); ok = FALSE; } } }
/* * add any newly-configured bond interfaces and add any * devices that should now be associated with the bond. */ for (i = 0; i < nConfig; i++) { CFNumberRef c_bond_mode; SCBondInterfaceRef c_bond; CFArrayRef c_bond_interfaces; CFStringRef c_bond_if; CFIndex c_count; Boolean found = FALSE; CFIndex j;
c_bond = CFArrayGetValueAtIndex(config, i); c_bond_if = SCNetworkInterfaceGetBSDName(c_bond); c_bond_interfaces = SCBondInterfaceGetMemberInterfaces(c_bond); c_bond_mode = SCBondInterfaceGetMode(c_bond); c_count = (c_bond_interfaces != NULL) ? CFArrayGetCount(c_bond_interfaces) : 0;
for (j = 0; j < nActive; j++) { SCBondInterfaceRef a_bond; CFArrayRef a_bond_interfaces; CFNumberRef a_bond_mode; CFStringRef a_bond_if; CFIndex a_count;
a_bond = CFArrayGetValueAtIndex(active, j); a_bond_if = SCNetworkInterfaceGetBSDName(a_bond); a_bond_interfaces = SCBondInterfaceGetMemberInterfaces(a_bond); a_bond_mode = SCBondInterfaceGetMode(a_bond); a_count = (a_bond_interfaces != NULL) ? CFArrayGetCount(a_bond_interfaces) : 0;
if (CFEqual(c_bond_if, a_bond_if)) { CFIndex c; Boolean if_list_change = FALSE; Boolean mode_change = FALSE;
found = TRUE;
if (!_SC_CFEqual(a_bond_mode, c_bond_mode)) { mode_change = TRUE; }
if (!_SC_CFEqual(c_bond_interfaces, a_bond_interfaces)) { if_list_change = TRUE; } if (!mode_change && !if_list_change) { break; // if no change } if (s == -1) { s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); ok = FALSE; goto done; } } if (mode_change) { __bond_set_mode(s, a_bond_if, c_bond_mode); } if (!if_list_change) { break; // no if list changes }
/* * ensure that the first device of the bond matches, if * not then we remove all current devices and add them * back in the preferred order. */ if ((c_count > 0) && (a_count > 0) && !CFEqual(CFArrayGetValueAtIndex(c_bond_interfaces, 0), CFArrayGetValueAtIndex(a_bond_interfaces, 0))) { CFIndex a;
for (a = 0; a < a_count; a++) { SCNetworkInterfaceRef a_interface; CFStringRef a_interface_if;
a_interface = CFArrayGetValueAtIndex(a_bond_interfaces, a); if (!CFArrayContainsValue(c_bond_interfaces, CFRangeMake(0, c_count), a_interface)) { continue; // if already removed }
a_interface_if = SCNetworkInterfaceGetBSDName(a_interface); if (!__bond_remove_interface(s, a_bond_if, a_interface_if)) { ok = FALSE; } }
a_count = 0; // all active devices have been removed }
/* * add any devices which are not currently associated * with the bond interface. */ for (c = 0; c < c_count; c++) { SCNetworkInterfaceRef c_interface; SCNetworkInterfacePrivateRef c_interfacePrivate; CFStringRef c_interface_if;
c_interface = CFArrayGetValueAtIndex(c_bond_interfaces, c); if ((a_count == 0) || !CFArrayContainsValue(a_bond_interfaces, CFRangeMake(0, a_count), c_interface)) { /* * check if this member interface can be added to a bond. */ c_interfacePrivate = (SCNetworkInterfacePrivateRef)c_interface; if (!c_interfacePrivate->supportsBond) { // if member not supported continue; }
/* * if this member interface is not currently part of the bond. */ c_interface_if = SCNetworkInterfaceGetBSDName(c_interface); if (!__bond_add_interface(s, c_bond_if, c_interface_if)) { // if member could not be added ok = FALSE; } } }
break; } }
if (!found) { CFIndex c;
if (s == -1) { s = inet_dgram_socket(); if (s == -1) { _SCErrorSet(errno); ok = FALSE; goto done; } }
/* * establish the new bond interface. */ if (!__createInterface(s, c_bond_if)) { _SCErrorSet(errno); ok = FALSE; continue; }
/* set the mode */ __bond_set_mode(s, c_bond_if, c_bond_mode);
/* * add the member interfaces */ for (c = 0; c < c_count; c++) { SCNetworkInterfaceRef c_interface; SCNetworkInterfacePrivateRef c_interfacePrivate; CFStringRef c_interface_if;
c_interface = CFArrayGetValueAtIndex(c_bond_interfaces, c); c_interfacePrivate = (SCNetworkInterfacePrivateRef)c_interface; if (!c_interfacePrivate->supportsBond) { // if member not supported continue; }
c_interface_if = SCNetworkInterfaceGetBSDName(c_interface); if (!__bond_add_interface(s, c_bond_if, c_interface_if)) { // if member could not be added ok = FALSE; } } }
}
done :
if (active != NULL) CFRelease(active); if (config != NULL) CFRelease(config); if (s != -1) (void) close(s);
return ok;}