2 * Copyright (c) 2000-2017 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>
62 #include <sys/sysctl.h>
63 #include <sys/kern_event.h>
64 #include <network/config.h>
65 #include <netinet6/nd6.h>
67 static dispatch_queue_t S_kev_queue
;
68 static dispatch_source_t S_kev_source
;
69 __private_extern__ Boolean network_changed
= FALSE
;
70 __private_extern__ SCDynamicStoreRef store
= NULL
;
71 __private_extern__ Boolean _verbose
= FALSE
;
74 __private_extern__ os_log_t
75 __log_KernelEventMonitor()
77 static os_log_t log
= NULL
;
80 log
= os_log_create("com.apple.SystemConfiguration", "KernelEventMonitor");
87 #define MESSAGES_MAX 100
88 static CFMutableArrayRef S_messages
;
89 static Boolean S_messages_modified
;
94 S_messages
= CFArrayCreateMutable(NULL
,
96 &kCFTypeArrayCallBacks
);
103 if (S_messages
!= NULL
) {
104 CFRelease(S_messages
);
111 messages_should_add_message(void)
113 if (S_messages
== NULL
114 || CFArrayGetCount(S_messages
) >= MESSAGES_MAX
) {
121 messages_add_message(CFStringRef message
)
123 if (messages_should_add_message()) {
124 CFArrayAppendValue(S_messages
, message
);
125 S_messages_modified
= TRUE
;
130 __private_extern__
void
131 messages_add_msg_with_arg(const char * msg
, const char * arg
)
133 if (messages_should_add_message()) {
136 str
= CFStringCreateWithFormat(NULL
, NULL
,
137 CFSTR("%12.8f: %s %s"),
138 CFAbsoluteTimeGetCurrent(),
140 messages_add_message(str
);
149 if (S_messages
!= NULL
&& S_messages_modified
) {
150 SCDynamicStoreSetValue(NULL
,
151 CFSTR("Plugin:KernelEventMonitor"),
153 S_messages_modified
= FALSE
;
159 check_interface_link_status(const char * if_name
)
161 if (S_messages
== NULL
) {
162 return; /* we're not in early boot of system */
164 link_update_status_if_missing(if_name
);
170 dgram_socket(int domain
)
174 s
= socket(domain
, SOCK_DGRAM
, 0);
176 SC_log(LOG_ERR
, "socket() failed: %s", strerror(errno
));
183 post_network_changed(void)
185 if (network_changed
) {
188 status
= notify_post(_SC_NOTIFY_NETWORK_CHANGE
);
189 if (status
!= NOTIFY_STATUS_OK
) {
190 SC_log(LOG_NOTICE
, "notify_post() failed: error=%u", status
);
193 network_changed
= FALSE
;
200 logEvent(CFStringRef evStr
, struct kern_event_msg
*ev_msg
)
209 SC_log(LOG_DEBUG
, "%@ event:", evStr
);
210 SC_log(LOG_DEBUG
, " Event size=%d, id=%d, vendor=%d, class=%d, subclass=%d, code=%d",
215 ev_msg
->kev_subclass
,
217 for (i
= 0, j
= KEV_MSG_HEADER_SIZE
; j
< ev_msg
->total_size
; i
++, j
+=4) {
218 SC_log(LOG_DEBUG
, " Event data[%2d] = %08x", i
, ev_msg
->event_data
[i
]);
223 copy_if_name(const struct net_event_data
* ev
, char * ifr_name
, int ifr_len
)
225 snprintf(ifr_name
, ifr_len
, "%s%d", ev
->if_name
, ev
->if_unit
);
229 static uint8_t info_zero
[DLIL_MODARGLEN
];
232 processEvent_Apple_Network(struct kern_event_msg
*ev_msg
)
234 size_t dataLen
= (ev_msg
->total_size
- KEV_MSG_HEADER_SIZE
);
235 void * event_data
= &ev_msg
->event_data
[0];
236 char ifr_name
[IFNAMSIZ
];
237 Boolean handled
= TRUE
;
239 switch (ev_msg
->kev_subclass
) {
240 case KEV_INET_SUBCLASS
: {
241 switch (ev_msg
->event_code
) {
242 case KEV_INET_NEW_ADDR
:
243 case KEV_INET_CHANGED_ADDR
:
244 case KEV_INET_ADDR_DELETED
:
245 case KEV_INET_SIFDSTADDR
:
246 case KEV_INET_SIFBRDADDR
:
247 case KEV_INET_SIFNETMASK
: {
248 struct kev_in_data
* ev
;
250 ev
= (struct kev_in_data
*)event_data
;
251 if (dataLen
< sizeof(*ev
)) {
255 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
256 SC_log(LOG_INFO
, "Process IPv4 address change: %s: %d", (char *)ifr_name
, ev_msg
->event_code
);
257 ipv4_interface_update(NULL
, ifr_name
);
258 if (ev_msg
->event_code
259 != KEV_INET_ADDR_DELETED
) {
260 check_interface_link_status(ifr_name
);
264 case KEV_INET_ARPCOLLISION
: {
265 struct kev_in_collision
* ev
;
267 ev
= (struct kev_in_collision
*)event_data
;
268 if ((dataLen
< sizeof(*ev
))
269 || (dataLen
< (sizeof(*ev
) + ev
->hw_len
))) {
273 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
274 SC_log(LOG_INFO
, "Process ARP collision: %s", (char *)ifr_name
);
275 ipv4_arp_collision(ifr_name
,
281 #if !TARGET_OS_IPHONE
282 case KEV_INET_PORTINUSE
: {
283 struct kev_in_portinuse
* ev
;
284 ev
= (struct kev_in_portinuse
*)event_data
;
285 if (dataLen
< sizeof(*ev
)) {
289 SC_log(LOG_INFO
, "Process port-in-use: %hu, %u", ev
->port
, ev
->req_pid
);
290 ipv4_port_in_use(ev
->port
, ev
->req_pid
);
293 #endif /* !TARGET_OS_IPHONE */
294 case KEV_INET_ARPRTRFAILURE
: {
295 const struct kev_in_arpfailure
* ev
;
297 ev
= (const struct kev_in_arpfailure
*)event_data
;
298 if (dataLen
< sizeof(*ev
)) {
302 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
303 SC_log(LOG_INFO
, "Process router ARP failure: %s", (char *)ifr_name
);
304 ipv4_router_arp_failure(ifr_name
);
307 case KEV_INET_ARPRTRALIVE
: {
308 const struct kev_in_arpalive
* ev
;
310 ev
= (const struct kev_in_arpalive
*)event_data
;
311 if (dataLen
< sizeof(*ev
)) {
315 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
316 SC_log(LOG_INFO
, "Process router ARP alive: %s", (char *)ifr_name
);
317 ipv4_router_arp_alive(ifr_name
);
326 case KEV_INET6_SUBCLASS
: {
327 struct kev_in6_data
* ev
;
329 ev
= (struct kev_in6_data
*)event_data
;
330 switch (ev_msg
->event_code
) {
331 case KEV_INET6_NEW_USER_ADDR
:
332 case KEV_INET6_CHANGED_ADDR
:
333 case KEV_INET6_ADDR_DELETED
:
334 case KEV_INET6_NEW_LL_ADDR
:
335 case KEV_INET6_NEW_RTADV_ADDR
:
336 if (dataLen
< sizeof(*ev
)) {
340 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
341 SC_log(LOG_INFO
, "Process IPv6 address change: %s: %d", (char *)ifr_name
, ev_msg
->event_code
);
342 interface_update_ipv6(NULL
, ifr_name
);
343 if (ev_msg
->event_code
== KEV_INET6_NEW_USER_ADDR
344 && (ev
->ia6_flags
& IN6_IFF_DUPLICATED
) != 0) {
345 ipv6_duplicated_address(ifr_name
,
346 &ev
->ia_addr
.sin6_addr
,
350 if (ev_msg
->event_code
351 != KEV_INET6_ADDR_DELETED
) {
352 check_interface_link_status(ifr_name
);
356 case KEV_INET6_REQUEST_NAT64_PREFIX
:
357 if (dataLen
< sizeof(*ev
)) {
361 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
362 SC_log(LOG_INFO
, "Process NAT64 prefix request: %s", (char *)ifr_name
);
363 nat64_prefix_request(ifr_name
);
372 case KEV_DL_SUBCLASS
: {
373 struct net_event_data
* ev
;
375 ev
= (struct net_event_data
*)event_data
;
376 switch (ev_msg
->event_code
) {
377 case KEV_DL_IF_ATTACHED
:
379 * new interface added
381 if (dataLen
< sizeof(*ev
)) {
385 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
386 SC_log(LOG_INFO
, "Process interface attach: %s", (char *)ifr_name
);
390 case KEV_DL_IF_DETACHED
:
394 if (dataLen
< sizeof(*ev
)) {
398 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
399 SC_log(LOG_INFO
, "Process interface detach: %s", (char *)ifr_name
);
400 link_remove(ifr_name
);
403 case KEV_DL_IF_DETACHING
:
405 * interface detaching
407 if (dataLen
< sizeof(*ev
)) {
411 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
412 SC_log(LOG_INFO
, "Process interface detaching: %s", (char *)ifr_name
);
413 interface_detaching(ifr_name
);
416 case KEV_DL_PROTO_ATTACHED
:
417 case KEV_DL_PROTO_DETACHED
: {
418 struct kev_dl_proto_data
* protoEvent
;
420 protoEvent
= (struct kev_dl_proto_data
*)event_data
;
421 if (dataLen
< sizeof(*protoEvent
)) {
425 copy_if_name(&protoEvent
->link_data
,
426 ifr_name
, sizeof(ifr_name
));
427 SC_log(LOG_INFO
, "Process protocol %s: %s (pf=%d, n=%d)",
428 (ev_msg
->event_code
== KEV_DL_PROTO_ATTACHED
) ? "attach" : "detach",
430 protoEvent
->proto_family
,
431 protoEvent
->proto_remaining_count
);
435 #ifdef KEV_DL_IF_IDLE_ROUTE_REFCNT
436 case KEV_DL_IF_IDLE_ROUTE_REFCNT
: {
438 * interface route refcnt idle
440 if (dataLen
< sizeof(*ev
)) {
444 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
445 SC_log(LOG_INFO
, "Process interface idle: %s", (char *)ifr_name
);
446 interface_update_idle_state(ifr_name
);
449 #endif // KEV_DL_IF_IDLE_ROUTE_REFCNT
451 case KEV_DL_LINK_OFF
:
452 case KEV_DL_LINK_ON
:
454 * update the link status in the store
456 if (dataLen
< sizeof(*ev
)) {
460 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
461 SC_log(LOG_INFO
, "Process interface link %s: %s",
462 (ev_msg
->event_code
== KEV_DL_LINK_ON
) ? "up" : "down",
464 link_update_status(ifr_name
, FALSE
, FALSE
);
467 #ifdef KEV_DL_LINK_QUALITY_METRIC_CHANGED
468 case KEV_DL_LINK_QUALITY_METRIC_CHANGED
: {
469 struct kev_dl_link_quality_metric_data
* lqm_data
;
470 lqm_data
= (struct kev_dl_link_quality_metric_data
*) event_data
;
472 if (dataLen
< sizeof(*ev
)) {
476 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
477 SC_log(LOG_INFO
, "Process interface quality: %s (q=%d)",
479 lqm_data
->link_quality_metric
);
480 interface_update_quality_metric(ifr_name
,
481 lqm_data
->link_quality_metric
);
484 #endif // KEV_DL_LINK_QUALITY_METRIC_CHANGED
487 case KEV_DL_ISSUES
: {
488 struct kev_dl_issues
*issues
;
490 issues
= (struct kev_dl_issues
*)event_data
;
491 if (dataLen
< sizeof(*ev
)) {
495 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
496 SC_log(LOG_INFO
, "Process interface link issues: %s",
498 interface_update_link_issues(ifr_name
,
503 (bcmp(issues
->info
, info_zero
, DLIL_MODARGLEN
) != 0)
508 #endif // KEV_DL_ISSUES
516 #ifdef KEV_NETPOLICY_SUBCLASS
517 case KEV_NETPOLICY_SUBCLASS
: {
520 #endif // KEV_NETPOLICY_SUBCLASS
522 #ifdef KEV_SOCKET_SUBCLASS
523 case KEV_SOCKET_SUBCLASS
: {
526 #endif // KEV_SOCKET_SUBCLASS
528 #ifdef KEV_ND6_SUBCLASS
529 case KEV_ND6_SUBCLASS
: {
530 struct kev_nd6_event
* ev
;
532 ev
= (struct kev_nd6_event
*)event_data
;
533 switch (ev_msg
->event_code
) {
534 case KEV_ND6_ADDR_DETACHED
:
535 case KEV_ND6_ADDR_DEPRECATED
:
536 case KEV_ND6_ADDR_EXPIRED
:
537 if (dataLen
< sizeof(*ev
)) {
541 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
542 SC_log(LOG_INFO
, "Process ND6 address change: %s: %d", (char *)ifr_name
, ev_msg
->event_code
);
543 interface_update_ipv6(NULL
, ifr_name
);
545 case KEV_ND6_RTR_EXPIRED
:
546 if (dataLen
< sizeof(*ev
)) {
550 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
551 SC_log(LOG_INFO
, "Process IPv6 router expired: %s: %d", (char *)ifr_name
, ev_msg
->event_code
);
552 ipv6_router_expired(ifr_name
);
559 #endif // KEV_ND6_SUBCLASS
561 #ifdef KEV_NECP_SUBCLASS
562 case KEV_NECP_SUBCLASS
: {
565 #endif // KEV_NECP_SUBCLASS
567 #ifdef KEV_NETAGENT_SUBCLASS
568 case KEV_NETAGENT_SUBCLASS
: {
571 #endif // KEV_NETAGENT_SUBCLASS
573 #ifdef KEV_LOG_SUBCLASS
574 case KEV_LOG_SUBCLASS
: {
577 #endif // KEV_LOG_SUBCLASS
579 #ifdef KEV_NETEVENT_SUBCLASS
580 case KEV_NETEVENT_SUBCLASS
: {
583 #endif // KEV_NETEVENT_SUBCLASS
590 logEvent(CFSTR("Error processing (Apple network subclass)"), ev_msg
);
596 eventCallback(int so
)
600 struct kern_event_msg ev_msg1
; // first kernel event
602 struct kern_event_msg
*ev_msg
= &buf
.ev_msg1
;
606 status
= recv(so
, &buf
, sizeof(buf
), 0);
608 SC_log(LOG_NOTICE
, "recv() failed: %s", strerror(errno
));
614 while (offset
< status
) {
615 if ((offset
+ (ssize_t
)ev_msg
->total_size
) > status
) {
616 SC_log(LOG_NOTICE
, "missed SYSPROTO_EVENT event, buffer not big enough");
620 switch (ev_msg
->vendor_code
) {
621 case KEV_VENDOR_APPLE
:
622 switch (ev_msg
->kev_class
) {
623 case KEV_NETWORK_CLASS
:
624 processEvent_Apple_Network(ev_msg
);
634 offset
+= ev_msg
->total_size
;
635 ev_msg
= (struct kern_event_msg
*)(void *)&buf
.bytes
[offset
];
640 post_network_changed();
647 __private_extern__
void
648 config_new_interface(const char * ifname
)
650 xpc_object_t if_list
;
652 if (ifname
== NULL
) {
653 network_config_check_interface_settings(NULL
);
656 if_list
= xpc_array_create(NULL
, 0);
657 xpc_array_set_string(if_list
, XPC_ARRAY_APPEND
, ifname
);
658 network_config_check_interface_settings(if_list
);
659 xpc_release(if_list
);
664 update_interfaces(const char * msg
, Boolean first_time
)
666 Boolean added
= FALSE
;
667 struct ifaddrs
* ifap
= NULL
;
668 CFMutableArrayRef ifList
= NULL
;
669 struct ifaddrs
* scan
;
671 if (getifaddrs(&ifap
) == -1) {
672 messages_add_msg_with_arg("getifaddrs", strerror(errno
));
673 SC_log(LOG_NOTICE
, "getifaddrs() failed: %s", strerror(errno
));
677 /* update list of interfaces & link status */
678 ifList
= interfaceListCopy();
679 for (scan
= ifap
; scan
!= NULL
; scan
= scan
->ifa_next
) {
680 if (scan
->ifa_addr
== NULL
681 || scan
->ifa_addr
->sa_family
!= AF_LINK
) {
684 /* get the per-interface link/media information */
685 if (interfaceListAddInterface(ifList
, scan
->ifa_name
)) {
686 messages_add_msg_with_arg(msg
, scan
->ifa_name
);
689 config_new_interface(scan
->ifa_name
);
694 /* update the global list if an interface was added */
696 interfaceListUpdate(ifList
);
700 /* update IPv4/IPv6 addresses that are already assigned */
702 ipv4_interface_update(ifap
, NULL
);
703 interface_update_ipv6(ifap
, NULL
);
710 /* tell networkd to get the interface list itself */
711 config_new_interface(NULL
);
716 #define TIMER_INTERVAL (6LL * NSEC_PER_SEC)
717 #define MAX_TIMER_COUNT 20
720 check_for_new_interfaces(void * context
);
725 dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW
, TIMER_INTERVAL
),
728 check_for_new_interfaces
);
733 check_for_new_interfaces(void * context
)
735 #pragma unused(context)
741 /* update KEV driven content in case a message got dropped */
742 snprintf(msg
, sizeof(msg
), "update %d (of %d)", count
, MAX_TIMER_COUNT
);
744 update_interfaces(msg
, FALSE
);
749 /* schedule the next timer, if needed */
750 if (count
< MAX_TIMER_COUNT
) {
763 SC_log(LOG_DEBUG
, "prime() called");
767 update_interfaces("prime", TRUE
);
771 network_changed
= TRUE
;
772 post_network_changed();
775 /* start handling kernel events */
776 dispatch_resume(S_kev_source
);
778 /* schedule polling timer */
787 prime_KernelEventMonitor()
789 dispatch_async(S_kev_queue
, ^{ prime(); });
794 initialize_store(void)
796 store
= SCDynamicStoreCreate(NULL
,
797 CFSTR("Kernel Event Monitor plug-in"),
801 SC_log(LOG_ERR
, "SCDynamicStoreCreate() failed: %s", SCErrorString(SCError()));
810 load_KernelEventMonitor(CFBundleRef bundle
, Boolean bundleVerbose
)
812 struct kev_request kev_req
;
820 SC_log(LOG_DEBUG
, "load() called");
821 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
823 if (!initialize_store()) {
824 SC_log(LOG_ERR
, "kernel event monitor disabled");
828 /* Open an event socket */
829 so
= socket(PF_SYSTEM
, SOCK_RAW
, SYSPROTO_EVENT
);
831 /* establish filter to return events of interest */
832 kev_req
.vendor_code
= KEV_VENDOR_APPLE
;
833 kev_req
.kev_class
= KEV_NETWORK_CLASS
;
834 kev_req
.kev_subclass
= KEV_ANY_SUBCLASS
;
835 status
= ioctl(so
, SIOCSKEVFILT
, &kev_req
);
837 SC_log(LOG_ERR
, "could not establish event filter, ioctl() failed: %s", strerror(errno
));
842 SC_log(LOG_ERR
, "could not open event socket, socket() failed: %s", strerror(errno
));
848 status
= ioctl(so
, FIONBIO
, &yes
);
850 SC_log(LOG_ERR
, "could not set non-blocking io, ioctl() failed: %s", strerror(errno
));
857 SC_log(LOG_ERR
, "kernel event monitor disabled");
862 S_kev_queue
= dispatch_queue_create("com.apple.SystemConfiguration.KernelEventMonitor", NULL
);
864 = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, so
, 0, S_kev_queue
);
865 dispatch_source_set_cancel_handler(S_kev_source
, ^{
868 dispatch_source_set_event_handler(S_kev_source
, ^{
869 os_activity_t activity
;
872 activity
= os_activity_create("processing network kernel events",
874 OS_ACTIVITY_FLAG_DEFAULT
);
875 os_activity_scope(activity
);
877 ok
= eventCallback(so
);
879 SC_log(LOG_ERR
, "kernel event monitor disabled");
880 dispatch_source_cancel(S_kev_source
);
883 os_release(activity
);
885 // NOTE: dispatch_resume() will be called in prime()
894 #define appendAddress appendAddress_v4
895 #define getIF getIF_v4
896 #define updateStore updateStore_v4
902 #define appendAddress appendAddress_v6
903 #define getIF getIF_v6
904 #define updateStore updateStore_v6
911 main(int argc
, char **argv
)
914 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
916 load_KernelEventMonitor(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
917 prime_KernelEventMonitor();