2 * Copyright (c) 2000-2008, 2010 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 * December 3, 2002 Dieter Siegmund <dieter@apple.com>
28 * - handle the new KEV_INET_ARPCOLLISION event
29 * - format the event into a DynamicStore key
30 * State:/Network/Interface/ifname/IPv4Collision/ip_addr/hw_addr
31 * and send a notification on the key
33 * August 8, 2002 Allan Nathanson <ajn@apple.com>
34 * - added support for KEV_INET6_xxx events
36 * January 6, 2002 Jessica Vazquez <vazquez@apple.com>
37 * - added handling for KEV_ATALK_xxx events
39 * July 2, 2001 Dieter Siegmund <dieter@apple.com>
40 * - added handling for KEV_DL_PROTO_{ATTACHED, DETACHED}
41 * - mark an interface up if the number of protocols remaining is not 0,
42 * mark an interface down if the number is zero
43 * - allocate socket on demand instead of keeping it open all the time
45 * June 23, 2001 Allan Nathanson <ajn@apple.com>
46 * - update to public SystemConfiguration.framework APIs
48 * May 17, 2001 Allan Nathanson <ajn@apple.com>
49 * - add/maintain per-interface address/netmask/destaddr information
50 * in the dynamic store.
52 * June 30, 2000 Allan Nathanson <ajn@apple.com>
64 #define KEV_LOG_SUBCLASS 10
66 static const char *inetEventName
[] = {
69 "INET address changed",
70 "INET address deleted",
71 "INET destination address changed",
72 "INET broadcast address changed",
73 "INET netmask changed",
78 static const char *dlEventName
[] = {
89 "KEV_DL_IF_DETACHING",
93 "KEV_DL_PROTO_ATTACHED",
94 "KEV_DL_PROTO_DETACHED",
95 "KEV_DL_LINK_ADDRESS_CHANGED",
96 "KEV_DL_WAKEFLAGS_CHANGED",
97 #ifdef KEV_DL_IF_IDLE_ROUTE_REFCNT
98 "KEV_DL_IF_IDLE_ROUTE_REFCNT",
99 #endif // KEV_DL_IF_IDLE_ROUTE_REFCNT
102 static const char *inet6EventName
[] = {
104 "KEV_INET6_NEW_USER_ADDR",
105 "KEV_INET6_CHANGED_ADDR",
106 "KEV_INET6_ADDR_DELETED",
107 "KEV_INET6_NEW_LL_ADDR",
108 "KEV_INET6_NEW_RTADV_ADDR",
109 "KEV_INET6_DEFROUTER"
112 __private_extern__ Boolean network_changed
= FALSE
;
113 __private_extern__ SCDynamicStoreRef store
= NULL
;
114 __private_extern__ Boolean _verbose
= FALSE
;
118 dgram_socket(int domain
)
120 return (socket(domain
, SOCK_DGRAM
, 0));
124 ifflags_set(int s
, char * name
, short flags
)
129 bzero(&ifr
, sizeof(ifr
));
130 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
131 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
135 ifr
.ifr_flags
|= flags
;
136 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
140 ifflags_clear(int s
, char * name
, short flags
)
145 bzero(&ifr
, sizeof(ifr
));
146 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
147 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
151 ifr
.ifr_flags
&= ~flags
;
152 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
156 mark_if_up(char * name
)
158 int s
= dgram_socket(AF_INET
);
161 ifflags_set(s
, name
, IFF_UP
);
166 mark_if_down(char * name
)
168 int s
= dgram_socket(AF_INET
);
171 ifflags_clear(s
, name
, IFF_UP
);
176 post_network_changed(void)
178 if (network_changed
) {
181 status
= notify_post("com.apple.system.config.network_change");
182 if (status
!= NOTIFY_STATUS_OK
) {
183 SCLog(TRUE
, LOG_ERR
, CFSTR("notify_post() failed: error=%ld"), status
);
186 network_changed
= FALSE
;
193 logEvent(CFStringRef evStr
, struct kern_event_msg
*ev_msg
)
202 SCLog(TRUE
, LOG_DEBUG
, CFSTR("%@ event:"), evStr
);
203 SCLog(TRUE
, LOG_DEBUG
,
204 CFSTR(" Event size=%d, id=%d, vendor=%d, class=%d, subclass=%d, code=%d"),
209 ev_msg
->kev_subclass
,
211 for (i
= 0, j
= KEV_MSG_HEADER_SIZE
; j
< ev_msg
->total_size
; i
++, j
+=4) {
212 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" Event data[%2d] = %08lx"), i
, ev_msg
->event_data
[i
]);
217 inetEventNameString(uint32_t event_code
)
219 if (event_code
< sizeof(inetEventName
) / sizeof(inetEventName
[0])) {
220 return (inetEventName
[event_code
]);
222 return ("New Apple network INET subcode");
226 inet6EventNameString(uint32_t event_code
)
228 if (event_code
< sizeof(inet6EventName
) / sizeof(inet6EventName
[0])) {
229 return (inet6EventName
[event_code
]);
231 return ("New Apple network INET6 subcode");
235 dlEventNameString(uint32_t event_code
)
237 if (event_code
< sizeof(dlEventName
) / sizeof(dlEventName
[0])) {
238 return (dlEventName
[event_code
]);
240 return ("New Apple network DL subcode");
244 copy_if_name(struct net_event_data
* ev
, char * ifr_name
, int ifr_len
)
246 snprintf(ifr_name
, ifr_len
, "%s%d", ev
->if_name
, ev
->if_unit
);
251 processEvent_Apple_Network(struct kern_event_msg
*ev_msg
)
253 const char * eventName
= NULL
;
254 int dataLen
= (ev_msg
->total_size
- KEV_MSG_HEADER_SIZE
);
255 void * event_data
= &ev_msg
->event_data
[0];
256 Boolean handled
= TRUE
;
257 char ifr_name
[IFNAMSIZ
];
259 switch (ev_msg
->kev_subclass
) {
260 case KEV_INET_SUBCLASS
: {
261 eventName
= inetEventNameString(ev_msg
->event_code
);
262 switch (ev_msg
->event_code
) {
263 case KEV_INET_NEW_ADDR
:
264 case KEV_INET_CHANGED_ADDR
:
265 case KEV_INET_ADDR_DELETED
:
266 case KEV_INET_SIFDSTADDR
:
267 case KEV_INET_SIFBRDADDR
:
268 case KEV_INET_SIFNETMASK
: {
269 struct kev_in_data
* ev
;
271 ev
= (struct kev_in_data
*)event_data
;
272 if (dataLen
< sizeof(*ev
)) {
276 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
277 interface_update_ipv4(NULL
, ifr_name
);
280 case KEV_INET_ARPCOLLISION
: {
281 struct kev_in_collision
* ev
;
283 ev
= (struct kev_in_collision
*)event_data
;
284 if ((dataLen
< sizeof(*ev
))
285 || (dataLen
< (sizeof(*ev
) + ev
->hw_len
))) {
289 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
290 interface_collision_ipv4(ifr_name
,
296 #if !TARGET_OS_IPHONE
297 case KEV_INET_PORTINUSE
: {
298 struct kev_in_portinuse
* ev
;
299 ev
= (struct kev_in_portinuse
*)event_data
;
300 if (dataLen
< sizeof(*ev
)) {
304 port_in_use_ipv4(ev
->port
, ev
->req_pid
);
307 #endif /* !TARGET_OS_IPHONE */
314 case KEV_INET6_SUBCLASS
: {
315 struct kev_in6_data
* ev
;
317 eventName
= inet6EventNameString(ev_msg
->event_code
);
318 ev
= (struct kev_in6_data
*)event_data
;
319 switch (ev_msg
->event_code
) {
320 case KEV_INET6_NEW_USER_ADDR
:
321 case KEV_INET6_CHANGED_ADDR
:
322 case KEV_INET6_ADDR_DELETED
:
323 case KEV_INET6_NEW_LL_ADDR
:
324 case KEV_INET6_NEW_RTADV_ADDR
:
325 case KEV_INET6_DEFROUTER
:
326 if (dataLen
< sizeof(*ev
)) {
330 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
331 interface_update_ipv6(NULL
, ifr_name
);
340 case KEV_DL_SUBCLASS
: {
341 struct net_event_data
* ev
;
343 eventName
= dlEventNameString(ev_msg
->event_code
);
344 ev
= (struct net_event_data
*)event_data
;
345 switch (ev_msg
->event_code
) {
346 case KEV_DL_IF_ATTACHED
:
348 * new interface added
350 if (dataLen
< sizeof(*ev
)) {
354 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
358 case KEV_DL_IF_DETACHED
:
362 if (dataLen
< sizeof(*ev
)) {
366 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
367 link_remove(ifr_name
);
370 case KEV_DL_IF_DETACHING
:
372 * interface detaching
374 if (dataLen
< sizeof(*ev
)) {
378 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
379 interface_detaching(ifr_name
);
382 case KEV_DL_PROTO_ATTACHED
:
383 case KEV_DL_PROTO_DETACHED
: {
384 struct kev_dl_proto_data
* protoEvent
;
386 protoEvent
= (struct kev_dl_proto_data
*)event_data
;
387 if (dataLen
< sizeof(*protoEvent
)) {
391 copy_if_name(&protoEvent
->link_data
,
392 ifr_name
, sizeof(ifr_name
));
393 if (protoEvent
->proto_remaining_count
== 0) {
394 mark_if_down(ifr_name
);
396 mark_if_up(ifr_name
);
401 case KEV_DL_LINK_OFF
:
402 case KEV_DL_LINK_ON
:
404 * update the link status in the store
406 if (dataLen
< sizeof(*ev
)) {
410 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
411 link_update_status(ifr_name
, FALSE
);
414 case KEV_DL_SIFFLAGS
:
415 case KEV_DL_SIFMETRICS
:
417 case KEV_DL_SIFPHYS
:
418 case KEV_DL_SIFMEDIA
:
419 case KEV_DL_SIFGENERIC
:
420 case KEV_DL_ADDMULTI
:
421 case KEV_DL_DELMULTI
:
422 case KEV_DL_LINK_ADDRESS_CHANGED
:
423 case KEV_DL_WAKEFLAGS_CHANGED
:
424 #ifdef KEV_DL_IF_IDLE_ROUTE_REFCNT
425 case KEV_DL_IF_IDLE_ROUTE_REFCNT
:
426 #endif // KEV_DL_IF_IDLE_ROUTE_REFCNT
435 case KEV_LOG_SUBCLASS
: {
443 if (handled
== FALSE
) {
446 evStr
= CFStringCreateWithCString(NULL
,
447 (eventName
!= NULL
) ? eventName
: "New Apple network subclass",
448 kCFStringEncodingASCII
);
449 logEvent(evStr
, ev_msg
);
456 eventCallback(CFSocketRef s
, CFSocketCallBackType type
, CFDataRef address
, const void *data
, void *info
)
458 int so
= CFSocketGetNative(s
);
461 struct kern_event_msg
*ev_msg
= (struct kern_event_msg
*)&buf
[0];
464 status
= recv(so
, &buf
, sizeof(buf
), 0);
466 SCLog(TRUE
, LOG_ERR
, CFSTR("recv() failed: %s"), strerror(errno
));
472 while (offset
< status
) {
473 if ((offset
+ ev_msg
->total_size
) > status
) {
474 SCLog(TRUE
, LOG_NOTICE
, CFSTR("missed SYSPROTO_EVENT event, buffer not big enough"));
478 switch (ev_msg
->vendor_code
) {
479 case KEV_VENDOR_APPLE
:
480 switch (ev_msg
->kev_class
) {
481 case KEV_NETWORK_CLASS
:
482 processEvent_Apple_Network(ev_msg
);
484 case KEV_IOKIT_CLASS
:
485 case KEV_SYSTEM_CLASS
:
486 case KEV_APPLESHARE_CLASS
:
487 case KEV_FIREWALL_CLASS
:
488 case KEV_IEEE80211_CLASS
:
491 /* unrecognized (Apple) event class */
492 logEvent(CFSTR("New (Apple) class"), ev_msg
);
497 /* unrecognized vendor code */
498 logEvent(CFSTR("New vendor"), ev_msg
);
501 offset
+= ev_msg
->total_size
;
502 ev_msg
= (struct kern_event_msg
*)&buf
[offset
];
507 post_network_changed();
513 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
514 CFSocketInvalidate(s
);
521 prime_KernelEventMonitor()
523 struct ifaddrs
*ifap
= NULL
;
524 struct ifaddrs
*scan
;
527 SCLog(_verbose
, LOG_DEBUG
, CFSTR("prime() called"));
531 sock
= dgram_socket(AF_INET
);
533 SCLog(TRUE
, LOG_ERR
, CFSTR("could not get interface list, socket() failed: %s"), strerror(errno
));
537 if (getifaddrs(&ifap
) == -1) {
540 CFSTR("could not get interface info, getifaddrs() failed: %s"),
545 /* update list of interfaces & link status */
546 for (scan
= ifap
; scan
!= NULL
; scan
= scan
->ifa_next
) {
547 if (scan
->ifa_addr
== NULL
548 || scan
->ifa_addr
->sa_family
!= AF_LINK
) {
551 /* get the per-interface link/media information */
552 link_add(scan
->ifa_name
);
556 * update IPv4 network addresses already assigned to
559 interface_update_ipv4(ifap
, NULL
);
562 * update IPv6 network addresses already assigned to
565 interface_update_ipv6(ifap
, NULL
);
576 network_changed
= TRUE
;
577 post_network_changed();
583 kevSocketCopyDescription(const void *info
)
585 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<kernel event socket>"));
590 load_KernelEventMonitor(CFBundleRef bundle
, Boolean bundleVerbose
)
592 CFSocketContext context
= { 0
596 , kevSocketCopyDescription
599 struct kev_request kev_req
;
600 CFRunLoopSourceRef rls
;
608 SCLog(_verbose
, LOG_DEBUG
, CFSTR("load() called"));
609 SCLog(_verbose
, LOG_DEBUG
, CFSTR(" bundle ID = %@"), CFBundleGetIdentifier(bundle
));
611 /* open a "configd" session to allow cache updates */
612 store
= SCDynamicStoreCreate(NULL
,
613 CFSTR("Kernel Event Monitor plug-in"),
617 SCLog(TRUE
, LOG_ERR
, CFSTR("SCDnamicStoreCreate() failed: %s"), SCErrorString(SCError()));
618 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
622 /* Open an event socket */
623 so
= socket(PF_SYSTEM
, SOCK_RAW
, SYSPROTO_EVENT
);
625 /* establish filter to return events of interest */
626 kev_req
.vendor_code
= KEV_VENDOR_APPLE
;
627 kev_req
.kev_class
= KEV_NETWORK_CLASS
;
628 kev_req
.kev_subclass
= KEV_ANY_SUBCLASS
;
629 status
= ioctl(so
, SIOCSKEVFILT
, &kev_req
);
631 SCLog(TRUE
, LOG_ERR
, CFSTR("could not establish event filter, ioctl() failed: %s"), strerror(errno
));
636 SCLog(TRUE
, LOG_ERR
, CFSTR("could not open event socket, socket() failed: %s"), strerror(errno
));
642 status
= ioctl(so
, FIONBIO
, &yes
);
644 SCLog(TRUE
, LOG_ERR
, CFSTR("could not set non-blocking io, ioctl() failed: %s"), strerror(errno
));
651 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
656 /* Create a CFSocketRef for the PF_SYSTEM kernel event socket */
657 es
= CFSocketCreateWithNative(NULL
,
659 kCFSocketReadCallBack
,
663 /* Create and add a run loop source for the event socket */
664 rls
= CFSocketCreateRunLoopSource(NULL
, es
, 0);
665 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
676 #define appendAddress appendAddress_v4
677 #define getIF getIF_v4
678 #define updateStore updateStore_v4
684 #define appendAddress appendAddress_v6
685 #define getIF getIF_v6
686 #define updateStore updateStore_v6
693 main(int argc
, char **argv
)
696 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
698 load_KernelEventMonitor(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
699 prime_KernelEventMonitor();