2 * Copyright (c) 2000-2008, 2010, 2011 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",
100 #ifdef KEV_DL_IFCAP_CHANGED
101 "KEV_DL_IFCAP_CHANGED",
103 #ifdef KEV_DL_LINK_QUALITY_METRIC_CHANGED
104 "KEV_DL_LINK_QUALITY_METRIC_CHANGED",
108 static const char *inet6EventName
[] = {
110 "KEV_INET6_NEW_USER_ADDR",
111 "KEV_INET6_CHANGED_ADDR",
112 "KEV_INET6_ADDR_DELETED",
113 "KEV_INET6_NEW_LL_ADDR",
114 "KEV_INET6_NEW_RTADV_ADDR",
115 "KEV_INET6_DEFROUTER"
118 #ifdef KEV_ND6_SUBCLASS
119 static const char *nd6EventNameString
[] = {
123 #endif // KEV_ND6_SUBCLASS
125 __private_extern__ Boolean network_changed
= FALSE
;
126 __private_extern__ SCDynamicStoreRef store
= NULL
;
127 __private_extern__ Boolean _verbose
= FALSE
;
131 dgram_socket(int domain
)
133 return (socket(domain
, SOCK_DGRAM
, 0));
137 ifflags_set(int s
, char * name
, short flags
)
142 bzero(&ifr
, sizeof(ifr
));
143 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
144 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
148 ifr
.ifr_flags
|= flags
;
149 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
153 ifflags_clear(int s
, char * name
, short flags
)
158 bzero(&ifr
, sizeof(ifr
));
159 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
160 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
164 ifr
.ifr_flags
&= ~flags
;
165 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
169 mark_if_up(char * name
)
171 int s
= dgram_socket(AF_INET
);
174 ifflags_set(s
, name
, IFF_UP
);
179 mark_if_down(char * name
)
181 int s
= dgram_socket(AF_INET
);
184 ifflags_clear(s
, name
, IFF_UP
);
189 post_network_changed(void)
191 if (network_changed
) {
194 status
= notify_post("com.apple.system.config.network_change");
195 if (status
!= NOTIFY_STATUS_OK
) {
196 SCLog(TRUE
, LOG_ERR
, CFSTR("notify_post() failed: error=%ld"), status
);
199 network_changed
= FALSE
;
206 logEvent(CFStringRef evStr
, struct kern_event_msg
*ev_msg
)
215 SCLog(TRUE
, LOG_DEBUG
, CFSTR("%@ event:"), evStr
);
216 SCLog(TRUE
, LOG_DEBUG
,
217 CFSTR(" Event size=%d, id=%d, vendor=%d, class=%d, subclass=%d, code=%d"),
222 ev_msg
->kev_subclass
,
224 for (i
= 0, j
= KEV_MSG_HEADER_SIZE
; j
< ev_msg
->total_size
; i
++, j
+=4) {
225 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" Event data[%2d] = %08lx"), i
, ev_msg
->event_data
[i
]);
230 inetEventNameString(uint32_t event_code
)
232 if (event_code
< sizeof(inetEventName
) / sizeof(inetEventName
[0])) {
233 return (inetEventName
[event_code
]);
235 return ("New Apple network INET subcode");
239 inet6EventNameString(uint32_t event_code
)
241 if (event_code
< sizeof(inet6EventName
) / sizeof(inet6EventName
[0])) {
242 return (inet6EventName
[event_code
]);
244 return ("New Apple network INET6 subcode");
248 dlEventNameString(uint32_t event_code
)
250 if (event_code
< sizeof(dlEventName
) / sizeof(dlEventName
[0])) {
251 return (dlEventName
[event_code
]);
253 return ("New Apple network DL subcode");
257 copy_if_name(struct net_event_data
* ev
, char * ifr_name
, int ifr_len
)
259 snprintf(ifr_name
, ifr_len
, "%s%d", ev
->if_name
, ev
->if_unit
);
264 processEvent_Apple_Network(struct kern_event_msg
*ev_msg
)
266 const char * eventName
= NULL
;
267 int dataLen
= (ev_msg
->total_size
- KEV_MSG_HEADER_SIZE
);
268 void * event_data
= &ev_msg
->event_data
[0];
269 Boolean handled
= TRUE
;
270 char ifr_name
[IFNAMSIZ
];
272 switch (ev_msg
->kev_subclass
) {
273 case KEV_INET_SUBCLASS
: {
274 eventName
= inetEventNameString(ev_msg
->event_code
);
275 switch (ev_msg
->event_code
) {
276 case KEV_INET_NEW_ADDR
:
277 case KEV_INET_CHANGED_ADDR
:
278 case KEV_INET_ADDR_DELETED
:
279 case KEV_INET_SIFDSTADDR
:
280 case KEV_INET_SIFBRDADDR
:
281 case KEV_INET_SIFNETMASK
: {
282 struct kev_in_data
* ev
;
284 ev
= (struct kev_in_data
*)event_data
;
285 if (dataLen
< sizeof(*ev
)) {
289 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
290 interface_update_ipv4(NULL
, ifr_name
);
293 case KEV_INET_ARPCOLLISION
: {
294 struct kev_in_collision
* ev
;
296 ev
= (struct kev_in_collision
*)event_data
;
297 if ((dataLen
< sizeof(*ev
))
298 || (dataLen
< (sizeof(*ev
) + ev
->hw_len
))) {
302 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
303 interface_collision_ipv4(ifr_name
,
309 #if !TARGET_OS_IPHONE
310 case KEV_INET_PORTINUSE
: {
311 struct kev_in_portinuse
* ev
;
312 ev
= (struct kev_in_portinuse
*)event_data
;
313 if (dataLen
< sizeof(*ev
)) {
317 port_in_use_ipv4(ev
->port
, ev
->req_pid
);
320 #endif /* !TARGET_OS_IPHONE */
327 case KEV_INET6_SUBCLASS
: {
328 struct kev_in6_data
* ev
;
330 eventName
= inet6EventNameString(ev_msg
->event_code
);
331 ev
= (struct kev_in6_data
*)event_data
;
332 switch (ev_msg
->event_code
) {
333 case KEV_INET6_NEW_USER_ADDR
:
334 case KEV_INET6_CHANGED_ADDR
:
335 case KEV_INET6_ADDR_DELETED
:
336 case KEV_INET6_NEW_LL_ADDR
:
337 case KEV_INET6_NEW_RTADV_ADDR
:
338 case KEV_INET6_DEFROUTER
:
339 if (dataLen
< sizeof(*ev
)) {
343 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
344 interface_update_ipv6(NULL
, ifr_name
);
353 case KEV_DL_SUBCLASS
: {
354 struct net_event_data
* ev
;
356 eventName
= dlEventNameString(ev_msg
->event_code
);
357 ev
= (struct net_event_data
*)event_data
;
358 switch (ev_msg
->event_code
) {
359 case KEV_DL_IF_ATTACHED
:
361 * new interface added
363 if (dataLen
< sizeof(*ev
)) {
367 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
371 case KEV_DL_IF_DETACHED
:
375 if (dataLen
< sizeof(*ev
)) {
379 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
380 link_remove(ifr_name
);
383 case KEV_DL_IF_DETACHING
:
385 * interface detaching
387 if (dataLen
< sizeof(*ev
)) {
391 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
392 interface_detaching(ifr_name
);
395 case KEV_DL_PROTO_ATTACHED
:
396 case KEV_DL_PROTO_DETACHED
: {
397 struct kev_dl_proto_data
* protoEvent
;
399 protoEvent
= (struct kev_dl_proto_data
*)event_data
;
400 if (dataLen
< sizeof(*protoEvent
)) {
404 copy_if_name(&protoEvent
->link_data
,
405 ifr_name
, sizeof(ifr_name
));
406 if (protoEvent
->proto_remaining_count
== 0) {
407 mark_if_down(ifr_name
);
409 mark_if_up(ifr_name
);
414 #ifdef KEV_DL_IF_IDLE_ROUTE_REFCNT
415 case KEV_DL_IF_IDLE_ROUTE_REFCNT
: {
417 * interface route refcnt idle
419 if (dataLen
< sizeof(*ev
)) {
423 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
424 interface_update_idle_state(ifr_name
);
427 #endif // KEV_DL_IF_IDLE_ROUTE_REFCNT
429 case KEV_DL_LINK_OFF
:
430 case KEV_DL_LINK_ON
:
432 * update the link status in the store
434 if (dataLen
< sizeof(*ev
)) {
438 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
439 link_update_status(ifr_name
, FALSE
);
442 #ifdef KEV_DL_LINK_QUALITY_METRIC_CHANGED
443 case KEV_DL_LINK_QUALITY_METRIC_CHANGED
: {
444 struct kev_dl_link_quality_metric_data
* lqm_data
;
445 lqm_data
= (struct kev_dl_link_quality_metric_data
*) event_data
;
447 if (dataLen
< sizeof(*ev
)) {
451 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
452 interface_update_quality_metric(ifr_name
,
453 lqm_data
->link_quality_metric
);
456 #endif // KEV_DL_LINK_QUALITY_METRIC_CHANGED
458 case KEV_DL_SIFFLAGS
:
459 case KEV_DL_SIFMETRICS
:
461 case KEV_DL_SIFPHYS
:
462 case KEV_DL_SIFMEDIA
:
463 case KEV_DL_SIFGENERIC
:
464 case KEV_DL_ADDMULTI
:
465 case KEV_DL_DELMULTI
:
466 case KEV_DL_LINK_ADDRESS_CHANGED
:
467 case KEV_DL_WAKEFLAGS_CHANGED
:
468 #ifdef KEV_DL_IFCAP_CHANGED
469 case KEV_DL_IFCAP_CHANGED
:
470 #endif // KEV_DL_IFCAP_CHANGED
479 #ifdef KEV_ND6_SUBCLASS
480 case KEV_ND6_SUBCLASS
: {
481 eventName
= nd6EventNameString(ev_msg
->event_code
);
482 switch (ev_msg
->event_code
) {
483 case KEV_KEV_ND6_RA
:
492 #endif // KEV_ND6_SUBCLASS
493 case KEV_LOG_SUBCLASS
: {
501 if (handled
== FALSE
) {
504 evStr
= CFStringCreateWithCString(NULL
,
505 (eventName
!= NULL
) ? eventName
: "New Apple network subclass",
506 kCFStringEncodingASCII
);
507 logEvent(evStr
, ev_msg
);
514 eventCallback(CFSocketRef s
, CFSocketCallBackType type
, CFDataRef address
, const void *data
, void *info
)
516 int so
= CFSocketGetNative(s
);
520 struct kern_event_msg ev_msg1
; // first kernel event
522 struct kern_event_msg
*ev_msg
= &buf
.ev_msg1
;
525 status
= recv(so
, &buf
, sizeof(buf
), 0);
527 SCLog(TRUE
, LOG_ERR
, CFSTR("recv() failed: %s"), strerror(errno
));
533 while (offset
< status
) {
534 if ((offset
+ ev_msg
->total_size
) > status
) {
535 SCLog(TRUE
, LOG_NOTICE
, CFSTR("missed SYSPROTO_EVENT event, buffer not big enough"));
539 switch (ev_msg
->vendor_code
) {
540 case KEV_VENDOR_APPLE
:
541 switch (ev_msg
->kev_class
) {
542 case KEV_NETWORK_CLASS
:
543 processEvent_Apple_Network(ev_msg
);
545 case KEV_IOKIT_CLASS
:
546 case KEV_SYSTEM_CLASS
:
547 case KEV_APPLESHARE_CLASS
:
548 case KEV_FIREWALL_CLASS
:
549 case KEV_IEEE80211_CLASS
:
552 /* unrecognized (Apple) event class */
553 logEvent(CFSTR("New (Apple) class"), ev_msg
);
558 /* unrecognized vendor code */
559 logEvent(CFSTR("New vendor"), ev_msg
);
562 offset
+= ev_msg
->total_size
;
563 ev_msg
= (struct kern_event_msg
*)(void *)&buf
.bytes
[offset
];
568 post_network_changed();
574 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
575 CFSocketInvalidate(s
);
582 prime_KernelEventMonitor()
584 struct ifaddrs
*ifap
= NULL
;
585 struct ifaddrs
*scan
;
588 SCLog(_verbose
, LOG_DEBUG
, CFSTR("prime() called"));
592 sock
= dgram_socket(AF_INET
);
594 SCLog(TRUE
, LOG_ERR
, CFSTR("could not get interface list, socket() failed: %s"), strerror(errno
));
598 if (getifaddrs(&ifap
) == -1) {
601 CFSTR("could not get interface info, getifaddrs() failed: %s"),
606 /* update list of interfaces & link status */
607 for (scan
= ifap
; scan
!= NULL
; scan
= scan
->ifa_next
) {
608 if (scan
->ifa_addr
== NULL
609 || scan
->ifa_addr
->sa_family
!= AF_LINK
) {
612 /* get the per-interface link/media information */
613 link_add(scan
->ifa_name
);
617 * update IPv4 network addresses already assigned to
620 interface_update_ipv4(ifap
, NULL
);
623 * update IPv6 network addresses already assigned to
626 interface_update_ipv6(ifap
, NULL
);
637 network_changed
= TRUE
;
638 post_network_changed();
644 kevSocketCopyDescription(const void *info
)
646 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<kernel event socket>"));
651 load_KernelEventMonitor(CFBundleRef bundle
, Boolean bundleVerbose
)
653 CFSocketContext context
= { 0
657 , kevSocketCopyDescription
660 struct kev_request kev_req
;
661 CFRunLoopSourceRef rls
;
669 SCLog(_verbose
, LOG_DEBUG
, CFSTR("load() called"));
670 SCLog(_verbose
, LOG_DEBUG
, CFSTR(" bundle ID = %@"), CFBundleGetIdentifier(bundle
));
672 /* open a "configd" session to allow cache updates */
673 store
= SCDynamicStoreCreate(NULL
,
674 CFSTR("Kernel Event Monitor plug-in"),
678 SCLog(TRUE
, LOG_ERR
, CFSTR("SCDnamicStoreCreate() failed: %s"), SCErrorString(SCError()));
679 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
683 /* Open an event socket */
684 so
= socket(PF_SYSTEM
, SOCK_RAW
, SYSPROTO_EVENT
);
686 /* establish filter to return events of interest */
687 kev_req
.vendor_code
= KEV_VENDOR_APPLE
;
688 kev_req
.kev_class
= KEV_NETWORK_CLASS
;
689 kev_req
.kev_subclass
= KEV_ANY_SUBCLASS
;
690 status
= ioctl(so
, SIOCSKEVFILT
, &kev_req
);
692 SCLog(TRUE
, LOG_ERR
, CFSTR("could not establish event filter, ioctl() failed: %s"), strerror(errno
));
697 SCLog(TRUE
, LOG_ERR
, CFSTR("could not open event socket, socket() failed: %s"), strerror(errno
));
703 status
= ioctl(so
, FIONBIO
, &yes
);
705 SCLog(TRUE
, LOG_ERR
, CFSTR("could not set non-blocking io, ioctl() failed: %s"), strerror(errno
));
712 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
717 /* Create a CFSocketRef for the PF_SYSTEM kernel event socket */
718 es
= CFSocketCreateWithNative(NULL
,
720 kCFSocketReadCallBack
,
724 /* Create and add a run loop source for the event socket */
725 rls
= CFSocketCreateRunLoopSource(NULL
, es
, 0);
726 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
737 #define appendAddress appendAddress_v4
738 #define getIF getIF_v4
739 #define updateStore updateStore_v4
745 #define appendAddress appendAddress_v6
746 #define getIF getIF_v6
747 #define updateStore updateStore_v6
754 main(int argc
, char **argv
)
757 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
759 load_KernelEventMonitor(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
760 prime_KernelEventMonitor();