]> git.saurik.com Git - apple/configd.git/blobdiff - Plugins/KernelEventMonitor/ev_ipv4.c
configd-1061.101.1.tar.gz
[apple/configd.git] / Plugins / KernelEventMonitor / ev_ipv4.c
index c8071e7827707b0c6418a06d4c055baef3af8985..55c4cf294944f0019418e267838f9d3267012141 100644 (file)
@@ -1,15 +1,15 @@
 /*
 /*
- * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2002-2018 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
  * @APPLE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * compliance with the License. Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this
  * file.
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * compliance with the License. Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this
  * file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -17,7 +17,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_LICENSE_HEADER_END@
  */
 
  * @APPLE_LICENSE_HEADER_END@
  */
 
  */
 
 #include "eventmon.h"
  */
 
 #include "eventmon.h"
-#include "cache.h"
 #include "ev_ipv4.h"
 
 #include "ev_ipv4.h"
 
-#ifndef kSCEntNetIPv4ARPCollision
-#define kSCEntNetIPv4ARPCollision      CFSTR("IPv4ARPCollision")
-#endif kSCEntNetIPv4ARPCollision
-
 #define IP_FORMAT      "%d.%d.%d.%d"
 #define IP_CH(ip, i)   (((u_char *)(ip))[i])
 #define IP_LIST(ip)    IP_CH(ip,0),IP_CH(ip,1),IP_CH(ip,2),IP_CH(ip,3)
 #define IP_FORMAT      "%d.%d.%d.%d"
 #define IP_CH(ip, i)   (((u_char *)(ip))[i])
 #define IP_LIST(ip)    IP_CH(ip,0),IP_CH(ip,1),IP_CH(ip,2),IP_CH(ip,3)
@@ -66,7 +61,7 @@ appendAddress(CFMutableDictionaryRef dict, CFStringRef key, struct in_addr *addr
 
 
 static CFMutableDictionaryRef
 
 
 static CFMutableDictionaryRef
-getIF(CFStringRef key, CFMutableDictionaryRef oldIFs, CFMutableDictionaryRef newIFs)
+copyIF(CFStringRef key, CFMutableDictionaryRef oldIFs, CFMutableDictionaryRef newIFs)
 {
        CFDictionaryRef         dict            = NULL;
        CFMutableDictionaryRef  newDict         = NULL;
 {
        CFDictionaryRef         dict            = NULL;
        CFMutableDictionaryRef  newDict         = NULL;
@@ -74,7 +69,7 @@ getIF(CFStringRef key, CFMutableDictionaryRef oldIFs, CFMutableDictionaryRef new
        if (CFDictionaryGetValueIfPresent(newIFs, key, (const void **)&dict)) {
                newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
        } else {
        if (CFDictionaryGetValueIfPresent(newIFs, key, (const void **)&dict)) {
                newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
        } else {
-               dict = cache_SCDynamicStoreCopyValue(store, key);
+               dict = SCDynamicStoreCopyValue(store, key);
                if (dict) {
                        CFDictionarySetValue(oldIFs, key, dict);
                        if (isA_CFDictionary(dict)) {
                if (dict) {
                        CFDictionarySetValue(oldIFs, key, dict);
                        if (isA_CFDictionary(dict)) {
@@ -110,10 +105,19 @@ updateStore(const void *key, const void *value, void *context)
 
        if (!dict || !CFEqual(dict, newDict)) {
                if (CFDictionaryGetCount(newDict) > 0) {
 
        if (!dict || !CFEqual(dict, newDict)) {
                if (CFDictionaryGetCount(newDict) > 0) {
-                       cache_SCDynamicStoreSetValue(store, key, newDict);
+                       SC_log(LOG_DEBUG, "Update interface configuration: %@: %@", key, newDict);
+                       SCDynamicStoreSetValue(store, key, newDict);
                } else if (dict) {
                } else if (dict) {
-                       cache_SCDynamicStoreRemoveValue(store, key);
+                       CFDictionaryRef         oldDict;
+
+                       oldDict = SCDynamicStoreCopyValue(store, key);
+                       if (oldDict != NULL) {
+                               SC_log(LOG_DEBUG, "Update interface configuration: %@: <removed>", key);
+                               CFRelease(oldDict);
+                       }
+                       SCDynamicStoreRemoveValue(store, key);
                }
                }
+               network_changed = TRUE;
        }
 
        return;
        }
 
        return;
@@ -122,7 +126,7 @@ updateStore(const void *key, const void *value, void *context)
 
 __private_extern__
 void
 
 __private_extern__
 void
-interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
+ipv4_interface_update(struct ifaddrs *ifap, const char *if_name)
 {
        struct ifaddrs          *ifa;
        struct ifaddrs          *ifap_temp      = NULL;
 {
        struct ifaddrs          *ifa;
        struct ifaddrs          *ifap_temp      = NULL;
@@ -143,8 +147,8 @@ interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
                                           &kCFTypeDictionaryValueCallBacks);
 
        if (!ifap) {
                                           &kCFTypeDictionaryValueCallBacks);
 
        if (!ifap) {
-               if (getifaddrs(&ifap_temp) < 0) {
-                       SCLog(TRUE, LOG_ERR, CFSTR("getifaddrs() failed: %s"), strerror(errno));
+               if (getifaddrs(&ifap_temp) == -1) {
+                       SC_log(LOG_NOTICE, "getifaddrs() failed: %s", strerror(errno));
                        goto error;
                }
                ifap = ifap_temp;
                        goto error;
                }
                ifap = ifap_temp;
@@ -173,23 +177,28 @@ interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
                                                                          kSCEntNetIPv4);
                CFRelease(interface);
 
                                                                          kSCEntNetIPv4);
                CFRelease(interface);
 
-               newDict = getIF(key, oldIFs, newIFs);
+               newDict = copyIF(key, oldIFs, newIFs);
 
 
-               sin = (struct sockaddr_in *)ifa->ifa_addr;
+               /* ALIGN: cast ok, this should be aligned (getifaddrs). */
+               sin = (struct sockaddr_in *)(void *)ifa->ifa_addr;
                appendAddress(newDict, kSCPropNetIPv4Addresses, &sin->sin_addr);
 
                if (ifa->ifa_flags & IFF_POINTOPOINT) {
                        struct sockaddr_in      *dst;
 
                appendAddress(newDict, kSCPropNetIPv4Addresses, &sin->sin_addr);
 
                if (ifa->ifa_flags & IFF_POINTOPOINT) {
                        struct sockaddr_in      *dst;
 
-                       dst = (struct sockaddr_in *)ifa->ifa_dstaddr;
+                       /* ALIGN: cast ok, this should be aligned (getifaddrs). */
+                       dst = (struct sockaddr_in *)(void *)ifa->ifa_dstaddr;
                        appendAddress(newDict, kSCPropNetIPv4DestAddresses, &dst->sin_addr);
                } else {
                        struct sockaddr_in      *brd;
                        struct sockaddr_in      *msk;
 
                        appendAddress(newDict, kSCPropNetIPv4DestAddresses, &dst->sin_addr);
                } else {
                        struct sockaddr_in      *brd;
                        struct sockaddr_in      *msk;
 
-                       brd = (struct sockaddr_in *)ifa->ifa_broadaddr;
-                       appendAddress(newDict, kSCPropNetIPv4BroadcastAddresses, &brd->sin_addr);
-                       msk = (struct sockaddr_in *)ifa->ifa_netmask;
+                       /* ALIGN: cast ok, this should be aligned (getifaddrs). */
+                       brd = (struct sockaddr_in *)(void *)ifa->ifa_broadaddr;
+                       appendAddress(newDict, kSCPropNetIPv4BroadcastAddresses,&brd->sin_addr);
+
+                       /* ALIGN: cast ok, this should be aligned (getifaddrs). */
+                       msk = (struct sockaddr_in *)(void *)ifa->ifa_netmask;
                        appendAddress(newDict, kSCPropNetIPv4SubnetMasks, &msk->sin_addr);
                }
 
                        appendAddress(newDict, kSCPropNetIPv4SubnetMasks, &msk->sin_addr);
                }
 
@@ -207,7 +216,7 @@ interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
                                                                          kSCEntNetIPv4);
                CFRelease(interface);
 
                                                                          kSCEntNetIPv4);
                CFRelease(interface);
 
-               newDict = getIF(key, oldIFs, newIFs);
+               newDict = copyIF(key, oldIFs, newIFs);
 
                CFDictionarySetValue(newIFs, key, newDict);
                CFRelease(newDict);
 
                CFDictionarySetValue(newIFs, key, newDict);
                CFRelease(newDict);
@@ -227,7 +236,7 @@ interface_update_ipv4(struct ifaddrs *ifap, const char *if_name)
 
 __private_extern__
 void
 
 __private_extern__
 void
-interface_collision_ipv4(const char *if_name, struct in_addr ip_addr, int hw_len, const void * hw_addr)
+ipv4_arp_collision(const char *if_name, struct in_addr ip_addr, int hw_len, const void * hw_addr)
 {
        uint8_t *               hw_addr_bytes = (uint8_t *)hw_addr;
        int                     i;
 {
        uint8_t *               hw_addr_bytes = (uint8_t *)hw_addr;
        int                     i;
@@ -248,9 +257,64 @@ interface_collision_ipv4(const char *if_name, struct in_addr ip_addr, int hw_len
            CFStringAppendFormat(key, NULL, CFSTR("%s%02x"),
                                 (i == 0) ? "/" : ":", hw_addr_bytes[i]);
        }
            CFStringAppendFormat(key, NULL, CFSTR("%s%02x"),
                                 (i == 0) ? "/" : ":", hw_addr_bytes[i]);
        }
-       cache_SCDynamicStoreNotifyValue(store, key);
+       SC_log(LOG_DEBUG, "Post ARP collision: %@", key);
+       SCDynamicStoreNotifyValue(store, key);
        CFRelease(key);
        CFRelease(prefix);
        CFRelease(if_name_cf);
        return;
 }
        CFRelease(key);
        CFRelease(prefix);
        CFRelease(if_name_cf);
        return;
 }
+
+#if    !TARGET_OS_IPHONE
+__private_extern__
+void
+ipv4_port_in_use(uint16_t port, pid_t req_pid)
+{
+       CFStringRef             key;
+
+       key = SCDynamicStoreKeyCreate(NULL,
+                                     CFSTR("%@/%@/Protocol/%@/%@/%d/%d"),
+                                     kSCDynamicStoreDomainState,
+                                     kSCCompNetwork,
+                                     kSCEntNetIPv4,
+                                     kSCEntNetIPv4PortInUse,
+                                     port, req_pid);
+       SC_log(LOG_DEBUG, "Post port-in-use: %@", key);
+       SCDynamicStoreNotifyValue(store, key);
+       CFRelease(key);
+       return;
+}
+#endif /* !TARGET_OS_IPHONE */
+
+static void
+interface_notify_entity(const char * if_name, const char * type, CFStringRef entity)
+{
+       CFStringRef             if_name_cf;
+       CFStringRef             key;
+
+       if_name_cf = CFStringCreateWithCString(NULL, if_name,
+                                              kCFStringEncodingASCII);
+       key = SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
+                                                           kSCDynamicStoreDomainState,
+                                                           if_name_cf,
+                                                           entity);
+       CFRelease(if_name_cf);
+       SC_log(LOG_DEBUG, "Post %s: %@", type, key);
+       SCDynamicStoreNotifyValue(store, key);
+       CFRelease(key);
+       return;
+}
+
+__private_extern__ void
+ipv4_router_arp_failure(const char * if_name)
+{
+       interface_notify_entity(if_name, "Router ARP failure", kSCEntNetIPv4RouterARPFailure);
+       return;
+}
+
+__private_extern__ void
+ipv4_router_arp_alive(const char * if_name)
+{
+       interface_notify_entity(if_name, "Router ARP alive", kSCEntNetIPv4RouterARPAlive);
+       return;
+}