2 * Copyright (c) 2002-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 * August 5, 2002 Allan Nathanson <ajn@apple.com>
28 * - split code out from eventmon.c
34 #define IP_FORMAT "%d.%d.%d.%d"
35 #define IP_CH(ip, i) (((u_char *)(ip))[i])
36 #define IP_LIST(ip) IP_CH(ip,0),IP_CH(ip,1),IP_CH(ip,2),IP_CH(ip,3)
40 appendAddress(CFMutableDictionaryRef dict
, CFStringRef key
, struct in_addr
*address
)
44 CFMutableArrayRef newAddrs
;
46 addrs
= CFDictionaryGetValue(dict
, key
);
48 newAddrs
= CFArrayCreateMutableCopy(NULL
, 0, addrs
);
50 newAddrs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
53 addr
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR(IP_FORMAT
), IP_LIST(address
));
54 CFArrayAppendValue(newAddrs
, addr
);
57 CFDictionarySetValue(dict
, key
, newAddrs
);
63 static CFMutableDictionaryRef
64 copyIF(CFStringRef key
, CFMutableDictionaryRef oldIFs
, CFMutableDictionaryRef newIFs
)
66 CFDictionaryRef dict
= NULL
;
67 CFMutableDictionaryRef newDict
= NULL
;
69 if (CFDictionaryGetValueIfPresent(newIFs
, key
, (const void **)&dict
)) {
70 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
72 dict
= SCDynamicStoreCopyValue(store
, key
);
74 CFDictionarySetValue(oldIFs
, key
, dict
);
75 if (isA_CFDictionary(dict
)) {
76 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
77 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4Addresses
);
78 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4SubnetMasks
);
79 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4DestAddresses
);
80 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4BroadcastAddresses
);
87 newDict
= CFDictionaryCreateMutable(NULL
,
89 &kCFTypeDictionaryKeyCallBacks
,
90 &kCFTypeDictionaryValueCallBacks
);
98 updateStore(const void *key
, const void *value
, void *context
)
100 CFDictionaryRef dict
;
101 CFDictionaryRef newDict
= (CFDictionaryRef
)value
;
102 CFDictionaryRef oldIFs
= (CFDictionaryRef
)context
;
104 dict
= CFDictionaryGetValue(oldIFs
, key
);
106 if (!dict
|| !CFEqual(dict
, newDict
)) {
107 if (CFDictionaryGetCount(newDict
) > 0) {
108 SC_log(LOG_DEBUG
, "Update interface configuration: %@: %@", key
, newDict
);
109 SCDynamicStoreSetValue(store
, key
, newDict
);
111 CFDictionaryRef oldDict
;
113 oldDict
= SCDynamicStoreCopyValue(store
, key
);
114 if (oldDict
!= NULL
) {
115 SC_log(LOG_DEBUG
, "Update interface configuration: %@: <removed>", key
);
118 SCDynamicStoreRemoveValue(store
, key
);
120 network_changed
= TRUE
;
129 ipv4_interface_update(struct ifaddrs
*ifap
, const char *if_name
)
132 struct ifaddrs
*ifap_temp
= NULL
;
133 CFStringRef interface
;
134 boolean_t interfaceFound
= FALSE
;
135 CFStringRef key
= NULL
;
136 CFMutableDictionaryRef oldIFs
;
137 CFMutableDictionaryRef newDict
= NULL
;
138 CFMutableDictionaryRef newIFs
;
140 oldIFs
= CFDictionaryCreateMutable(NULL
,
142 &kCFTypeDictionaryKeyCallBacks
,
143 &kCFTypeDictionaryValueCallBacks
);
144 newIFs
= CFDictionaryCreateMutable(NULL
,
146 &kCFTypeDictionaryKeyCallBacks
,
147 &kCFTypeDictionaryValueCallBacks
);
150 if (getifaddrs(&ifap_temp
) == -1) {
151 SC_log(LOG_NOTICE
, "getifaddrs() failed: %s", strerror(errno
));
157 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
158 struct sockaddr_in
*sin
;
160 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
161 continue; /* sorry, not interested */
164 /* check if this is the requested interface */
166 if (strncmp(if_name
, ifa
->ifa_name
, IFNAMSIZ
) == 0) {
167 interfaceFound
= TRUE
; /* yes, this is the one I want */
169 continue; /* sorry, not interested */
173 interface
= CFStringCreateWithCString(NULL
, ifa
->ifa_name
, kCFStringEncodingMacRoman
);
174 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
175 kSCDynamicStoreDomainState
,
178 CFRelease(interface
);
180 newDict
= copyIF(key
, oldIFs
, newIFs
);
182 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
183 sin
= (struct sockaddr_in
*)(void *)ifa
->ifa_addr
;
184 appendAddress(newDict
, kSCPropNetIPv4Addresses
, &sin
->sin_addr
);
186 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
187 struct sockaddr_in
*dst
;
189 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
190 dst
= (struct sockaddr_in
*)(void *)ifa
->ifa_dstaddr
;
191 appendAddress(newDict
, kSCPropNetIPv4DestAddresses
, &dst
->sin_addr
);
193 struct sockaddr_in
*brd
;
194 struct sockaddr_in
*msk
;
196 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
197 brd
= (struct sockaddr_in
*)(void *)ifa
->ifa_broadaddr
;
198 appendAddress(newDict
, kSCPropNetIPv4BroadcastAddresses
,&brd
->sin_addr
);
200 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
201 msk
= (struct sockaddr_in
*)(void *)ifa
->ifa_netmask
;
202 appendAddress(newDict
, kSCPropNetIPv4SubnetMasks
, &msk
->sin_addr
);
205 CFDictionarySetValue(newIFs
, key
, newDict
);
210 /* if the last address[es] were removed from the target interface */
211 if (if_name
&& !interfaceFound
) {
212 interface
= CFStringCreateWithCString(NULL
, if_name
, kCFStringEncodingMacRoman
);
213 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
214 kSCDynamicStoreDomainState
,
217 CFRelease(interface
);
219 newDict
= copyIF(key
, oldIFs
, newIFs
);
221 CFDictionarySetValue(newIFs
, key
, newDict
);
226 CFDictionaryApplyFunction(newIFs
, updateStore
, oldIFs
);
230 if (ifap_temp
) freeifaddrs(ifap_temp
);
239 ipv4_arp_collision(const char *if_name
, struct in_addr ip_addr
, int hw_len
, const void * hw_addr
)
241 uint8_t * hw_addr_bytes
= (uint8_t *)hw_addr
;
243 CFStringRef if_name_cf
;
244 CFMutableStringRef key
;
247 if_name_cf
= CFStringCreateWithCString(NULL
, if_name
,
248 kCFStringEncodingASCII
);
249 prefix
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
250 kSCDynamicStoreDomainState
,
252 kSCEntNetIPv4ARPCollision
);
253 key
= CFStringCreateMutableCopy(NULL
, 0, prefix
);
254 CFStringAppendFormat(key
, NULL
, CFSTR("/" IP_FORMAT
),
256 for (i
= 0; i
< hw_len
; i
++) {
257 CFStringAppendFormat(key
, NULL
, CFSTR("%s%02x"),
258 (i
== 0) ? "/" : ":", hw_addr_bytes
[i
]);
260 SC_log(LOG_DEBUG
, "Post ARP collision: %@", key
);
261 SCDynamicStoreNotifyValue(store
, key
);
264 CFRelease(if_name_cf
);
268 #if !TARGET_OS_IPHONE
271 ipv4_port_in_use(uint16_t port
, pid_t req_pid
)
275 key
= SCDynamicStoreKeyCreate(NULL
,
276 CFSTR("%@/%@/Protocol/%@/%@/%d/%d"),
277 kSCDynamicStoreDomainState
,
280 kSCEntNetIPv4PortInUse
,
282 SC_log(LOG_DEBUG
, "Post port-in-use: %@", key
);
283 SCDynamicStoreNotifyValue(store
, key
);
287 #endif /* !TARGET_OS_IPHONE */
290 interface_notify_entity(const char * if_name
, const char * type
, CFStringRef entity
)
292 CFStringRef if_name_cf
;
295 if_name_cf
= CFStringCreateWithCString(NULL
, if_name
,
296 kCFStringEncodingASCII
);
297 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
298 kSCDynamicStoreDomainState
,
301 CFRelease(if_name_cf
);
302 SC_log(LOG_DEBUG
, "Post %s: %@", type
, key
);
303 SCDynamicStoreNotifyValue(store
, key
);
308 __private_extern__
void
309 ipv4_router_arp_failure(const char * if_name
)
311 interface_notify_entity(if_name
, "Router ARP failure", kSCEntNetIPv4RouterARPFailure
);
315 __private_extern__
void
316 ipv4_router_arp_alive(const char * if_name
)
318 interface_notify_entity(if_name
, "Router ARP alive", kSCEntNetIPv4RouterARPAlive
);