]> git.saurik.com Git - apple/configd.git/blob - Plugins/KernelEventMonitor/ev_ipv4.c
configd-395.7.tar.gz
[apple/configd.git] / Plugins / KernelEventMonitor / ev_ipv4.c
1 /*
2 * Copyright (c) 2002-2005, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * August 5, 2002 Allan Nathanson <ajn@apple.com>
28 * - split code out from eventmon.c
29 */
30
31 #include "eventmon.h"
32 #include "cache.h"
33 #include "ev_ipv4.h"
34
35 #ifndef kSCEntNetIPv4ARPCollision
36 #define kSCEntNetIPv4ARPCollision CFSTR("IPv4ARPCollision")
37 #endif /* kSCEntNetIPv4ARPCollision */
38
39 #if !TARGET_OS_IPHONE
40 #ifndef kSCEntNetIPv4PortInUse
41 #define kSCEntNetIPv4PortInUse CFSTR("PortInUse")
42 #endif /* kSCEntNetIPv4PortInUse */
43 #endif /* !TARGET_OS_IPHONE */
44
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)
48
49
50 static void
51 appendAddress(CFMutableDictionaryRef dict, CFStringRef key, struct in_addr *address)
52 {
53 CFStringRef addr;
54 CFArrayRef addrs;
55 CFMutableArrayRef newAddrs;
56
57 addrs = CFDictionaryGetValue(dict, key);
58 if (addrs) {
59 newAddrs = CFArrayCreateMutableCopy(NULL, 0, addrs);
60 } else {
61 newAddrs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
62 }
63
64 addr = CFStringCreateWithFormat(NULL, NULL, CFSTR(IP_FORMAT), IP_LIST(address));
65 CFArrayAppendValue(newAddrs, addr);
66 CFRelease(addr);
67
68 CFDictionarySetValue(dict, key, newAddrs);
69 CFRelease(newAddrs);
70 return;
71 }
72
73
74 static CFMutableDictionaryRef
75 getIF(CFStringRef key, CFMutableDictionaryRef oldIFs, CFMutableDictionaryRef newIFs)
76 {
77 CFDictionaryRef dict = NULL;
78 CFMutableDictionaryRef newDict = NULL;
79
80 if (CFDictionaryGetValueIfPresent(newIFs, key, (const void **)&dict)) {
81 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
82 } else {
83 dict = cache_SCDynamicStoreCopyValue(store, key);
84 if (dict) {
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);
92 }
93 CFRelease(dict);
94 }
95 }
96
97 if (!newDict) {
98 newDict = CFDictionaryCreateMutable(NULL,
99 0,
100 &kCFTypeDictionaryKeyCallBacks,
101 &kCFTypeDictionaryValueCallBacks);
102 }
103
104 return newDict;
105 }
106
107
108 static void
109 updateStore(const void *key, const void *value, void *context)
110 {
111 CFDictionaryRef dict;
112 CFDictionaryRef newDict = (CFDictionaryRef)value;
113 CFDictionaryRef oldIFs = (CFDictionaryRef)context;
114
115 dict = CFDictionaryGetValue(oldIFs, key);
116
117 if (!dict || !CFEqual(dict, newDict)) {
118 if (CFDictionaryGetCount(newDict) > 0) {
119 cache_SCDynamicStoreSetValue(store, key, newDict);
120 } else if (dict) {
121 cache_SCDynamicStoreRemoveValue(store, key);
122 }
123 network_changed = TRUE;
124 }
125
126 return;
127 }
128
129
130 __private_extern__
131 void
132 interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
133 {
134 struct ifaddrs *ifa;
135 struct ifaddrs *ifap_temp = NULL;
136 CFStringRef interface;
137 boolean_t interfaceFound = FALSE;
138 CFStringRef key = NULL;
139 CFMutableDictionaryRef oldIFs;
140 CFMutableDictionaryRef newDict = NULL;
141 CFMutableDictionaryRef newIFs;
142
143 oldIFs = CFDictionaryCreateMutable(NULL,
144 0,
145 &kCFTypeDictionaryKeyCallBacks,
146 &kCFTypeDictionaryValueCallBacks);
147 newIFs = CFDictionaryCreateMutable(NULL,
148 0,
149 &kCFTypeDictionaryKeyCallBacks,
150 &kCFTypeDictionaryValueCallBacks);
151
152 if (!ifap) {
153 if (getifaddrs(&ifap_temp) == -1) {
154 SCLog(TRUE, LOG_ERR, CFSTR("getifaddrs() failed: %s"), strerror(errno));
155 goto error;
156 }
157 ifap = ifap_temp;
158 }
159
160 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
161 struct sockaddr_in *sin;
162
163 if (ifa->ifa_addr->sa_family != AF_INET) {
164 continue; /* sorry, not interested */
165 }
166
167 /* check if this is the requested interface */
168 if (if_name) {
169 if (strncmp(if_name, ifa->ifa_name, IFNAMSIZ) == 0) {
170 interfaceFound = TRUE; /* yes, this is the one I want */
171 } else {
172 continue; /* sorry, not interested */
173 }
174 }
175
176 interface = CFStringCreateWithCString(NULL, ifa->ifa_name, kCFStringEncodingMacRoman);
177 key = SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
178 kSCDynamicStoreDomainState,
179 interface,
180 kSCEntNetIPv4);
181 CFRelease(interface);
182
183 newDict = getIF(key, oldIFs, newIFs);
184
185 sin = (struct sockaddr_in *)ifa->ifa_addr;
186 appendAddress(newDict, kSCPropNetIPv4Addresses, &sin->sin_addr);
187
188 if (ifa->ifa_flags & IFF_POINTOPOINT) {
189 struct sockaddr_in *dst;
190
191 dst = (struct sockaddr_in *)ifa->ifa_dstaddr;
192 appendAddress(newDict, kSCPropNetIPv4DestAddresses, &dst->sin_addr);
193 } else {
194 struct sockaddr_in *brd;
195 struct sockaddr_in *msk;
196
197 brd = (struct sockaddr_in *)ifa->ifa_broadaddr;
198 appendAddress(newDict, kSCPropNetIPv4BroadcastAddresses, &brd->sin_addr);
199 msk = (struct sockaddr_in *)ifa->ifa_netmask;
200 appendAddress(newDict, kSCPropNetIPv4SubnetMasks, &msk->sin_addr);
201 }
202
203 CFDictionarySetValue(newIFs, key, newDict);
204 CFRelease(newDict);
205 CFRelease(key);
206 }
207
208 /* if the last address[es] were removed from the target interface */
209 if (if_name && !interfaceFound) {
210 interface = CFStringCreateWithCString(NULL, if_name, kCFStringEncodingMacRoman);
211 key = SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
212 kSCDynamicStoreDomainState,
213 interface,
214 kSCEntNetIPv4);
215 CFRelease(interface);
216
217 newDict = getIF(key, oldIFs, newIFs);
218
219 CFDictionarySetValue(newIFs, key, newDict);
220 CFRelease(newDict);
221 CFRelease(key);
222 }
223
224 CFDictionaryApplyFunction(newIFs, updateStore, oldIFs);
225
226 error :
227
228 if (ifap_temp) freeifaddrs(ifap_temp);
229 CFRelease(oldIFs);
230 CFRelease(newIFs);
231
232 return;
233 }
234
235 __private_extern__
236 void
237 interface_collision_ipv4(const char *if_name, struct in_addr ip_addr, int hw_len, const void * hw_addr)
238 {
239 uint8_t * hw_addr_bytes = (uint8_t *)hw_addr;
240 int i;
241 CFStringRef if_name_cf;
242 CFMutableStringRef key;
243 CFStringRef prefix;
244
245 if_name_cf = CFStringCreateWithCString(NULL, if_name,
246 kCFStringEncodingASCII);
247 prefix = SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
248 kSCDynamicStoreDomainState,
249 if_name_cf,
250 kSCEntNetIPv4ARPCollision);
251 key = CFStringCreateMutableCopy(NULL, 0, prefix);
252 CFStringAppendFormat(key, NULL, CFSTR("/" IP_FORMAT),
253 IP_LIST(&ip_addr));
254 for (i = 0; i < hw_len; i++) {
255 CFStringAppendFormat(key, NULL, CFSTR("%s%02x"),
256 (i == 0) ? "/" : ":", hw_addr_bytes[i]);
257 }
258 cache_SCDynamicStoreNotifyValue(store, key);
259 CFRelease(key);
260 CFRelease(prefix);
261 CFRelease(if_name_cf);
262 return;
263 }
264
265 #if !TARGET_OS_IPHONE
266 __private_extern__
267 void
268 port_in_use_ipv4(uint16_t port, pid_t req_pid)
269 {
270 CFStringRef key;
271
272 key = SCDynamicStoreKeyCreate(NULL,
273 CFSTR("%@/%@/Protocol/%@/%@/%d/%d"),
274 kSCDynamicStoreDomainState,
275 kSCCompNetwork,
276 kSCEntNetIPv4,
277 kSCEntNetIPv4PortInUse,
278 port, req_pid);
279 cache_SCDynamicStoreNotifyValue(store, key);
280 CFRelease(key);
281 return;
282 }
283 #endif /* !TARGET_OS_IPHONE */