2 * Copyright (c) 2002-2015 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
35 #ifndef kSCEntNetIPv4ARPCollision
36 #define kSCEntNetIPv4ARPCollision CFSTR("IPv4ARPCollision")
37 #endif /* kSCEntNetIPv4ARPCollision */
40 #ifndef kSCEntNetIPv4PortInUse
41 #define kSCEntNetIPv4PortInUse CFSTR("PortInUse")
42 #endif /* kSCEntNetIPv4PortInUse */
43 #endif /* !TARGET_OS_IPHONE */
45 #define IP_FORMAT "%d.%d.%d.%d"
46 #define IP_CH(ip, i) (((u_char *)(ip))[i])
47 #define IP_LIST(ip) IP_CH(ip,0),IP_CH(ip,1),IP_CH(ip,2),IP_CH(ip,3)
51 appendAddress(CFMutableDictionaryRef dict
, CFStringRef key
, struct in_addr
*address
)
55 CFMutableArrayRef newAddrs
;
57 addrs
= CFDictionaryGetValue(dict
, key
);
59 newAddrs
= CFArrayCreateMutableCopy(NULL
, 0, addrs
);
61 newAddrs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
64 addr
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR(IP_FORMAT
), IP_LIST(address
));
65 CFArrayAppendValue(newAddrs
, addr
);
68 CFDictionarySetValue(dict
, key
, newAddrs
);
74 static CFMutableDictionaryRef
75 copyIF(CFStringRef key
, CFMutableDictionaryRef oldIFs
, CFMutableDictionaryRef newIFs
)
77 CFDictionaryRef dict
= NULL
;
78 CFMutableDictionaryRef newDict
= NULL
;
80 if (CFDictionaryGetValueIfPresent(newIFs
, key
, (const void **)&dict
)) {
81 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
83 dict
= cache_SCDynamicStoreCopyValue(store
, key
);
85 CFDictionarySetValue(oldIFs
, key
, dict
);
86 if (isA_CFDictionary(dict
)) {
87 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
88 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4Addresses
);
89 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4SubnetMasks
);
90 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4DestAddresses
);
91 CFDictionaryRemoveValue(newDict
, kSCPropNetIPv4BroadcastAddresses
);
98 newDict
= CFDictionaryCreateMutable(NULL
,
100 &kCFTypeDictionaryKeyCallBacks
,
101 &kCFTypeDictionaryValueCallBacks
);
109 updateStore(const void *key
, const void *value
, void *context
)
111 CFDictionaryRef dict
;
112 CFDictionaryRef newDict
= (CFDictionaryRef
)value
;
113 CFDictionaryRef oldIFs
= (CFDictionaryRef
)context
;
115 dict
= CFDictionaryGetValue(oldIFs
, key
);
117 if (!dict
|| !CFEqual(dict
, newDict
)) {
118 if (CFDictionaryGetCount(newDict
) > 0) {
119 SC_log(LOG_DEBUG
, "Update interface configuration: %@: %@", key
, newDict
);
120 cache_SCDynamicStoreSetValue(store
, key
, newDict
);
122 SC_log(LOG_DEBUG
, "Update interface configuration: %@: <removed>", key
);
123 cache_SCDynamicStoreRemoveValue(store
, key
);
125 network_changed
= TRUE
;
134 ipv4_interface_update(struct ifaddrs
*ifap
, const char *if_name
)
137 struct ifaddrs
*ifap_temp
= NULL
;
138 CFStringRef interface
;
139 boolean_t interfaceFound
= FALSE
;
140 CFStringRef key
= NULL
;
141 CFMutableDictionaryRef oldIFs
;
142 CFMutableDictionaryRef newDict
= NULL
;
143 CFMutableDictionaryRef newIFs
;
145 oldIFs
= CFDictionaryCreateMutable(NULL
,
147 &kCFTypeDictionaryKeyCallBacks
,
148 &kCFTypeDictionaryValueCallBacks
);
149 newIFs
= CFDictionaryCreateMutable(NULL
,
151 &kCFTypeDictionaryKeyCallBacks
,
152 &kCFTypeDictionaryValueCallBacks
);
155 if (getifaddrs(&ifap_temp
) == -1) {
156 SC_log(LOG_NOTICE
, "getifaddrs() failed: %s", strerror(errno
));
162 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
163 struct sockaddr_in
*sin
;
165 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
166 continue; /* sorry, not interested */
169 /* check if this is the requested interface */
171 if (strncmp(if_name
, ifa
->ifa_name
, IFNAMSIZ
) == 0) {
172 interfaceFound
= TRUE
; /* yes, this is the one I want */
174 continue; /* sorry, not interested */
178 interface
= CFStringCreateWithCString(NULL
, ifa
->ifa_name
, kCFStringEncodingMacRoman
);
179 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
180 kSCDynamicStoreDomainState
,
183 CFRelease(interface
);
185 newDict
= copyIF(key
, oldIFs
, newIFs
);
187 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
188 sin
= (struct sockaddr_in
*)(void *)ifa
->ifa_addr
;
189 appendAddress(newDict
, kSCPropNetIPv4Addresses
, &sin
->sin_addr
);
191 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
192 struct sockaddr_in
*dst
;
194 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
195 dst
= (struct sockaddr_in
*)(void *)ifa
->ifa_dstaddr
;
196 appendAddress(newDict
, kSCPropNetIPv4DestAddresses
, &dst
->sin_addr
);
198 struct sockaddr_in
*brd
;
199 struct sockaddr_in
*msk
;
201 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
202 brd
= (struct sockaddr_in
*)(void *)ifa
->ifa_broadaddr
;
203 appendAddress(newDict
, kSCPropNetIPv4BroadcastAddresses
,&brd
->sin_addr
);
205 /* ALIGN: cast ok, this should be aligned (getifaddrs). */
206 msk
= (struct sockaddr_in
*)(void *)ifa
->ifa_netmask
;
207 appendAddress(newDict
, kSCPropNetIPv4SubnetMasks
, &msk
->sin_addr
);
210 CFDictionarySetValue(newIFs
, key
, newDict
);
215 /* if the last address[es] were removed from the target interface */
216 if (if_name
&& !interfaceFound
) {
217 interface
= CFStringCreateWithCString(NULL
, if_name
, kCFStringEncodingMacRoman
);
218 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
219 kSCDynamicStoreDomainState
,
222 CFRelease(interface
);
224 newDict
= copyIF(key
, oldIFs
, newIFs
);
226 CFDictionarySetValue(newIFs
, key
, newDict
);
231 CFDictionaryApplyFunction(newIFs
, updateStore
, oldIFs
);
235 if (ifap_temp
) freeifaddrs(ifap_temp
);
244 ipv4_arp_collision(const char *if_name
, struct in_addr ip_addr
, int hw_len
, const void * hw_addr
)
246 uint8_t * hw_addr_bytes
= (uint8_t *)hw_addr
;
248 CFStringRef if_name_cf
;
249 CFMutableStringRef key
;
252 if_name_cf
= CFStringCreateWithCString(NULL
, if_name
,
253 kCFStringEncodingASCII
);
254 prefix
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
255 kSCDynamicStoreDomainState
,
257 kSCEntNetIPv4ARPCollision
);
258 key
= CFStringCreateMutableCopy(NULL
, 0, prefix
);
259 CFStringAppendFormat(key
, NULL
, CFSTR("/" IP_FORMAT
),
261 for (i
= 0; i
< hw_len
; i
++) {
262 CFStringAppendFormat(key
, NULL
, CFSTR("%s%02x"),
263 (i
== 0) ? "/" : ":", hw_addr_bytes
[i
]);
265 SC_log(LOG_DEBUG
, "Post ARP collision: %@", key
);
266 cache_SCDynamicStoreNotifyValue(store
, key
);
269 CFRelease(if_name_cf
);
273 #if !TARGET_OS_IPHONE
276 ipv4_port_in_use(uint16_t port
, pid_t req_pid
)
280 key
= SCDynamicStoreKeyCreate(NULL
,
281 CFSTR("%@/%@/Protocol/%@/%@/%d/%d"),
282 kSCDynamicStoreDomainState
,
285 kSCEntNetIPv4PortInUse
,
287 SC_log(LOG_DEBUG
, "Post port-in-use: %@", key
);
288 cache_SCDynamicStoreNotifyValue(store
, key
);
292 #endif /* !TARGET_OS_IPHONE */
295 interface_notify_entity(const char * if_name
, const char * type
, CFStringRef entity
)
297 CFStringRef if_name_cf
;
300 if_name_cf
= CFStringCreateWithCString(NULL
, if_name
,
301 kCFStringEncodingASCII
);
302 key
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
303 kSCDynamicStoreDomainState
,
306 CFRelease(if_name_cf
);
307 SC_log(LOG_DEBUG
, "Post %s: %@", type
, key
);
308 cache_SCDynamicStoreNotifyValue(store
, key
);
313 __private_extern__
void
314 ipv4_router_arp_failure(const char * if_name
)
316 interface_notify_entity(if_name
, "Router ARP failure", kSCEntNetIPv4RouterARPFailure
);
320 __private_extern__
void
321 ipv4_router_arp_alive(const char * if_name
)
323 interface_notify_entity(if_name
, "Router ARP alive", kSCEntNetIPv4RouterARPAlive
);