Never use a statically-configured IID on wifi.

Bug: 12111730
Change-Id: I062d81f2b65e6e89baae0f840cbd673f8051d523
diff --git a/Android.mk b/Android.mk
index b3b9fa3..988d925 100644
--- a/Android.mk
+++ b/Android.mk
@@ -6,7 +6,7 @@
 LOCAL_CFLAGS := -Wall -Werror -Wunused-parameter
 LOCAL_C_INCLUDES := external/libnl/include bionic/libc/dns/include
 LOCAL_STATIC_LIBRARIES := libnl
-LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_SHARED_LIBRARIES := libcutils liblog libnetutils
 
 # The clat daemon.
 LOCAL_MODULE := clatd
diff --git a/clatd_test.cpp b/clatd_test.cpp
index 085a9b7..fe52c21 100644
--- a/clatd_test.cpp
+++ b/clatd_test.cpp
@@ -565,6 +565,7 @@
     inet_pton(AF_INET6, kIPv6PlatSubnet, &Global_Clatd_Config.plat_subnet);
     inet_pton(AF_INET6, kIPv6LocalAddr, &Global_Clatd_Config.ipv6_local_subnet);
     Global_Clatd_Config.ipv6_host_id = in6addr_any;
+    Global_Clatd_Config.use_dynamic_iid = 1;
   }
 };
 
@@ -621,11 +622,16 @@
 
 TEST_F(ClatdTest, TestGenIIDConfigured) {
   struct in6_addr myaddr, expected;
+  Global_Clatd_Config.use_dynamic_iid = 0;
   ASSERT_TRUE(inet_pton(AF_INET6, "::bad:ace:d00d", &Global_Clatd_Config.ipv6_host_id));
   ASSERT_TRUE(inet_pton(AF_INET6, "2001:db8:1:2:0:bad:ace:d00d", &expected));
   ASSERT_TRUE(inet_pton(AF_INET6, "2001:db8:1:2:f076:ae99:124e:aa54", &myaddr));
   config_generate_local_ipv6_subnet(&myaddr);
   expect_ipv6_addr_equal(&expected, &myaddr);
+
+  Global_Clatd_Config.use_dynamic_iid = 1;
+  config_generate_local_ipv6_subnet(&myaddr);
+  EXPECT_FALSE(IN6_ARE_ADDR_EQUAL(&expected, &myaddr));
 }
 
 TEST_F(ClatdTest, TestGenIIDRandom) {
diff --git a/config.c b/config.c
index de8a26f..b147868 100644
--- a/config.c
+++ b/config.c
@@ -25,6 +25,7 @@
 #include <unistd.h>
 
 #include <cutils/config_utils.h>
+#include <netutils/ifc.h>
 
 #include "config.h"
 #include "dns64.h"
@@ -276,7 +277,7 @@
 void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) {
   int i;
 
-  if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) {
+  if (Global_Clatd_Config.use_dynamic_iid) {
     /* Generate a random interface ID. */
     gen_random_iid(interface_ip,
                    &Global_Clatd_Config.ipv4_local_subnet,
@@ -300,6 +301,7 @@
         unsigned net_id) {
   cnode *root = config_node("", "");
   void *tmp_ptr = NULL;
+  unsigned flags;
 
   if(!root) {
     logmsg(ANDROID_LOG_FATAL,"out of memory");
@@ -358,6 +360,17 @@
   if (!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id))
     goto failed;
 
+  /* In order to prevent multiple devices attempting to use the same clat address, never use a
+     statically-configured interface ID on a broadcast interface such as wifi. */
+  if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) {
+    ifc_init();
+    ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags);
+    ifc_close();
+    Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0;
+  } else {
+    Global_Clatd_Config.use_dynamic_iid = 1;
+  }
+
   return 1;
 
 failed:
diff --git a/config.h b/config.h
index 05b3a5a..b6d9ae1 100644
--- a/config.h
+++ b/config.h
@@ -33,6 +33,7 @@
   struct in6_addr plat_subnet;
   char *default_pdp_interface;
   char *plat_from_dns64_hostname;
+  int use_dynamic_iid;
 };
 
 extern struct clat_config Global_Clatd_Config;