2 * Copyright (c) 2000-2007 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>
61 #include "ev_appletalk.h"
65 static const char *inetEventName
[] = {
68 "INET address changed",
69 "INET address deleted",
70 "INET destination address changed",
71 "INET broadcast address changed",
72 "INET netmask changed",
76 static const char *dlEventName
[] = {
87 "KEV_DL_IF_DETACHING",
91 "KEV_DL_PROTO_ATTACHED",
92 "KEV_DL_PROTO_DETACHED",
95 static const char *atalkEventName
[] = {
99 "KEV_ATALK_ZONEUPDATED",
100 "KEV_ATALK_ROUTERUP",
101 "KEV_ATALK_ROUTERUP_INVALID",
102 "KEV_ATALK_ROUTERDOWN",
103 "KEV_ATALK_ZONELISTCHANGED"
106 static const char *inet6EventName
[] = {
108 "KEV_INET6_NEW_USER_ADDR",
109 "KEV_INET6_CHANGED_ADDR",
110 "KEV_INET6_ADDR_DELETED",
111 "KEV_INET6_NEW_LL_ADDR",
112 "KEV_INET6_NEW_RTADV_ADDR",
113 "KEV_INET6_DEFROUTER"
117 __private_extern__ Boolean network_changed
= FALSE
;
118 __private_extern__ SCDynamicStoreRef store
= NULL
;
119 __private_extern__ Boolean _verbose
= FALSE
;
124 dgram_socket(int domain
)
126 return (socket(domain
, SOCK_DGRAM
, 0));
130 ifflags_set(int s
, char * name
, short flags
)
135 bzero(&ifr
, sizeof(ifr
));
136 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
137 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
141 ifr
.ifr_flags
|= flags
;
142 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
146 ifflags_clear(int s
, char * name
, short flags
)
151 bzero(&ifr
, sizeof(ifr
));
152 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
153 ret
= ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
157 ifr
.ifr_flags
&= ~flags
;
158 return (ioctl(s
, SIOCSIFFLAGS
, &ifr
));
162 mark_if_up(char * name
)
164 int s
= dgram_socket(AF_INET
);
167 ifflags_set(s
, name
, IFF_UP
);
172 mark_if_down(char * name
)
174 int s
= dgram_socket(AF_INET
);
177 ifflags_clear(s
, name
, IFF_UP
);
182 post_network_changed(void)
184 if (network_changed
) {
187 status
= notify_post("com.apple.system.config.network_change");
188 if (status
!= NOTIFY_STATUS_OK
) {
189 SCLog(TRUE
, LOG_ERR
, CFSTR("notify_post() failed: error=%ld"), status
);
192 network_changed
= FALSE
;
199 logEvent(CFStringRef evStr
, struct kern_event_msg
*ev_msg
)
204 SCLog(_verbose
, LOG_DEBUG
, CFSTR("%@ event:"), evStr
);
205 SCLog(_verbose
, LOG_DEBUG
,
206 CFSTR(" Event size=%d, id=%d, vendor=%d, class=%d, subclass=%d, code=%d"),
211 ev_msg
->kev_subclass
,
213 for (i
= 0, j
= KEV_MSG_HEADER_SIZE
; j
< ev_msg
->total_size
; i
++, j
+=4) {
214 SCLog(_verbose
, LOG_DEBUG
, CFSTR(" Event data[%2d] = %08lx"), i
, ev_msg
->event_data
[i
]);
219 inetEventNameString(uint32_t event_code
)
221 if (event_code
<= KEV_INET_ARPCOLLISION
) {
222 return (inetEventName
[event_code
]);
224 return ("New Apple network INET subcode");
228 inet6EventNameString(uint32_t event_code
)
230 if (event_code
<= KEV_INET6_DEFROUTER
) {
231 return (inet6EventName
[event_code
]);
233 return ("New Apple network INET6 subcode");
237 dlEventNameString(uint32_t event_code
)
239 if (event_code
<= KEV_DL_PROTO_DETACHED
) {
240 return (dlEventName
[event_code
]);
242 return ("New Apple network DL subcode");
246 atalkEventNameString(uint32_t event_code
)
248 if (event_code
<= KEV_ATALK_ZONELISTCHANGED
) {
249 return (atalkEventName
[event_code
]);
251 return ("New Apple network AppleTalk subcode");
256 copy_if_name(struct net_event_data
* ev
, char * ifr_name
, int ifr_len
)
258 snprintf(ifr_name
, ifr_len
, "%s%d", ev
->if_name
, ev
->if_unit
);
263 processEvent_Apple_Network(struct kern_event_msg
*ev_msg
)
265 const char * eventName
= NULL
;
266 int dataLen
= (ev_msg
->total_size
- KEV_MSG_HEADER_SIZE
);
267 void * event_data
= &ev_msg
->event_data
[0];
268 Boolean handled
= TRUE
;
269 char ifr_name
[IFNAMSIZ
+ 1];
271 switch (ev_msg
->kev_subclass
) {
272 case KEV_INET_SUBCLASS
: {
273 eventName
= inetEventNameString(ev_msg
->event_code
);
274 switch (ev_msg
->event_code
) {
275 case KEV_INET_NEW_ADDR
:
276 case KEV_INET_CHANGED_ADDR
:
277 case KEV_INET_ADDR_DELETED
:
278 case KEV_INET_SIFDSTADDR
:
279 case KEV_INET_SIFBRDADDR
:
280 case KEV_INET_SIFNETMASK
: {
281 struct kev_in_data
* ev
;
283 ev
= (struct kev_in_data
*)event_data
;
284 if (dataLen
< sizeof(*ev
)) {
288 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
289 interface_update_ipv4(NULL
, ifr_name
);
292 case KEV_INET_ARPCOLLISION
: {
293 struct kev_in_collision
* ev
;
295 ev
= (struct kev_in_collision
*)event_data
;
296 if ((dataLen
< sizeof(*ev
))
297 || (dataLen
< (sizeof(*ev
) + ev
->hw_len
))) {
301 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
302 interface_collision_ipv4(ifr_name
,
308 case KEV_INET_PORTINUSE
: {
309 struct kev_in_portinuse
* ev
;
310 ev
= (struct kev_in_portinuse
*)event_data
;
311 if (dataLen
< sizeof(*ev
)) {
315 port_in_use_ipv4(ev
->port
, ev
->req_pid
);
324 case KEV_INET6_SUBCLASS
: {
325 struct kev_in6_data
* ev
;
327 eventName
= inet6EventNameString(ev_msg
->event_code
);
328 ev
= (struct kev_in6_data
*)event_data
;
329 switch (ev_msg
->event_code
) {
330 case KEV_INET6_NEW_USER_ADDR
:
331 case KEV_INET6_CHANGED_ADDR
:
332 case KEV_INET6_ADDR_DELETED
:
333 case KEV_INET6_NEW_LL_ADDR
:
334 case KEV_INET6_NEW_RTADV_ADDR
:
335 case KEV_INET6_DEFROUTER
:
336 if (dataLen
< sizeof(*ev
)) {
340 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
341 interface_update_ipv6(NULL
, ifr_name
);
350 case KEV_DL_SUBCLASS
: {
351 struct net_event_data
* ev
;
353 eventName
= dlEventNameString(ev_msg
->event_code
);
354 ev
= (struct net_event_data
*)event_data
;
355 switch (ev_msg
->event_code
) {
356 case KEV_DL_IF_ATTACHED
:
358 * new interface added
360 if (dataLen
< sizeof(*ev
)) {
364 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
368 case KEV_DL_IF_DETACHED
:
372 if (dataLen
< sizeof(*ev
)) {
376 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
377 link_remove(ifr_name
);
380 case KEV_DL_IF_DETACHING
:
382 * interface detaching
384 if (dataLen
< sizeof(*ev
)) {
388 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
389 interface_detaching(ifr_name
);
392 case KEV_DL_SIFFLAGS
:
393 case KEV_DL_SIFMETRICS
:
395 case KEV_DL_SIFPHYS
:
396 case KEV_DL_SIFMEDIA
:
397 case KEV_DL_SIFGENERIC
:
398 case KEV_DL_ADDMULTI
:
399 case KEV_DL_DELMULTI
:
403 case KEV_DL_PROTO_ATTACHED
:
404 case KEV_DL_PROTO_DETACHED
: {
405 struct kev_dl_proto_data
* protoEvent
;
407 protoEvent
= (struct kev_dl_proto_data
*)event_data
;
408 if (dataLen
< sizeof(*protoEvent
)) {
412 copy_if_name(&protoEvent
->link_data
,
413 ifr_name
, sizeof(ifr_name
));
414 if (protoEvent
->proto_remaining_count
== 0) {
415 mark_if_down(ifr_name
);
417 mark_if_up(ifr_name
);
422 case KEV_DL_LINK_OFF
:
423 case KEV_DL_LINK_ON
:
425 * update the link status in the store
427 if (dataLen
< sizeof(*ev
)) {
431 copy_if_name(ev
, ifr_name
, sizeof(ifr_name
));
432 link_update_status(ifr_name
, FALSE
);
441 case KEV_ATALK_SUBCLASS
: {
442 struct kev_atalk_data
* ev
;
444 eventName
= atalkEventNameString(ev_msg
->event_code
);
445 ev
= (struct kev_atalk_data
*)event_data
;
446 if (dataLen
< sizeof(*ev
)) {
450 copy_if_name(&ev
->link_data
, ifr_name
, sizeof(ifr_name
));
451 switch (ev_msg
->event_code
) {
452 case KEV_ATALK_ENABLED
:
453 interface_update_atalk_address(ev
, ifr_name
);
456 case KEV_ATALK_DISABLED
:
457 interface_update_shutdown_atalk();
460 case KEV_ATALK_ZONEUPDATED
:
461 interface_update_atalk_zone(ev
, ifr_name
);
464 case KEV_ATALK_ROUTERUP
:
465 case KEV_ATALK_ROUTERUP_INVALID
:
466 case KEV_ATALK_ROUTERDOWN
:
467 interface_update_appletalk(NULL
, ifr_name
);
470 case KEV_ATALK_ZONELISTCHANGED
:
484 if (handled
== FALSE
) {
487 evStr
= CFStringCreateWithCString(NULL
,
488 (eventName
!= NULL
) ? eventName
: "New Apple network subclass",
489 kCFStringEncodingASCII
);
490 logEvent(evStr
, ev_msg
);
498 processEvent_Apple_IOKit(struct kern_event_msg
*ev_msg
)
500 switch (ev_msg
->kev_subclass
) {
502 logEvent(CFSTR("New Apple IOKit subclass"), ev_msg
);
511 eventCallback(CFSocketRef s
, CFSocketCallBackType type
, CFDataRef address
, const void *data
, void *info
)
513 int so
= CFSocketGetNative(s
);
516 struct kern_event_msg
*ev_msg
= (struct kern_event_msg
*)&buf
[0];
519 status
= recv(so
, &buf
, sizeof(buf
), 0);
521 SCLog(TRUE
, LOG_ERR
, CFSTR("recv() failed: %s"), strerror(errno
));
527 while (offset
< status
) {
528 if ((offset
+ ev_msg
->total_size
) > status
) {
529 SCLog(TRUE
, LOG_NOTICE
, CFSTR("missed SYSPROTO_EVENT event, buffer not big enough"));
533 switch (ev_msg
->vendor_code
) {
534 case KEV_VENDOR_APPLE
:
535 switch (ev_msg
->kev_class
) {
536 case KEV_NETWORK_CLASS
:
537 processEvent_Apple_Network(ev_msg
);
539 case KEV_IOKIT_CLASS
:
540 processEvent_Apple_IOKit(ev_msg
);
543 /* unrecognized (Apple) event class */
544 logEvent(CFSTR("New (Apple) class"), ev_msg
);
549 /* unrecognized vendor code */
550 logEvent(CFSTR("New vendor"), ev_msg
);
553 offset
+= ev_msg
->total_size
;
554 ev_msg
= (struct kern_event_msg
*)&buf
[offset
];
559 post_network_changed();
565 SCLog(TRUE
, LOG_ERR
, CFSTR("kernel event monitor disabled."));
566 CFSocketInvalidate(s
);
574 prime_KernelEventMonitor()
576 struct ifaddrs
*ifap
= NULL
;
577 struct ifaddrs
*scan
;
580 SCLog(_verbose
, LOG_DEBUG
, CFSTR("prime() called"));
584 sock
= dgram_socket(AF_INET
);
586 SCLog(TRUE
, LOG_ERR
, CFSTR("could not get interface list, socket() failed: %s"), strerror(errno
));
590 if (getifaddrs(&ifap
) == -1) {
593 CFSTR("could not get interface info, getifaddrs() failed: %s"),
598 /* update list of interfaces & link status */
599 for (scan
= ifap
; scan
!= NULL
; scan
= scan
->ifa_next
) {
600 if (scan
->ifa_addr
== NULL
601 || scan
->ifa_addr
->sa_family
!= AF_LINK
) {
604 /* get the per-interface link/media information */
605 link_add(scan
->ifa_name
);
609 * update IPv4 network addresses already assigned to
612 interface_update_ipv4(ifap
, NULL
);
615 * update IPv6 network addresses already assigned to
618 interface_update_ipv6(ifap
, NULL
);
621 * update AppleTalk network addresses already assigned
624 interface_update_appletalk(ifap
, NULL
);
635 network_changed
= TRUE
;
636 post_network_changed();
643 kevSocketCopyDescription(const void *info
)
645 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<kernel event socket>"));
651 load_KernelEventMonitor(CFBundleRef bundle
, Boolean bundleVerbose
)
655 struct kev_request kev_req
;
657 CFSocketContext context
= { 0
661 , kevSocketCopyDescription
663 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 all events */
687 kev_req
.vendor_code
= 0;
688 kev_req
.kev_class
= 0; /* Not used if vendor_code is 0 */
689 kev_req
.kev_subclass
= 0; /* Not used if either kev_class OR vendor_code are 0 */
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
753 #define getIF getIF_at
754 #define updateStore updateStore_at
755 #include "ev_appletalk.c"
760 main(int argc
, char **argv
)
763 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
765 load_KernelEventMonitor(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
766 prime_KernelEventMonitor();