Configure the interface before bringing it up

Currently, clatd brings the clat4 interface up before assigning
its IPv4 address. This can cause a race condition, because as
soon as the interface comes up, the framework notices and reads
the interface configuration into the LinkProperties. If this
happens before the IPv4 address is configured, then the framework
ends up thinking clat4's IPv4 address is 0.0.0.0/0.

Fix this by configuring the address before the interface is
brought up.

Currently the framework does not use this address for anything,
so this is purely cosmetic, but it could make debugging more
confusing.

Bug: 8276725
Change-Id: I2bfee586a0d70050c53b10cc3f7eb9a98173e11d
diff --git a/clatd.c b/clatd.c
index ea8363c..edd8a2d 100644
--- a/clatd.c
+++ b/clatd.c
@@ -192,25 +192,26 @@
  * tunnel - tun device data
  */
 void configure_tun_ip(const struct tun_data *tunnel) {
-  struct in_addr default_4;
   int status;
 
-  default_4.s_addr = INADDR_ANY;
+  // Configure the interface before bringing it up. As soon as we bring the interface up, the
+  // framework will be notified and will assume the interface's configuration has been finalized.
+  status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
+      32, &Global_Clatd_Config.ipv4_local_subnet);
+  if(status < 0) {
+    logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
+    exit(1);
+  }
 
   if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
     logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
     exit(1);
   }
+
   if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
     logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
     exit(1);
   }
-  status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
-      32, &Global_Clatd_Config.ipv4_local_subnet);
-  if(status < 0) {
-    logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
-    exit(1);
-  }
 
   configure_tun_ipv6(tunnel);
 }