am 774abe7b: Cherry-pick 49f1857d from master. do not merge.

* commit '774abe7b7e463436107545250aa573c99e246a86':
  Cherry-pick 49f1857d from master. do not merge.
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 28a0ad9..6d94035 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -69,6 +69,7 @@
     Action *next;
 
     char cmd[64];    
+    const char *prod;
     void *data;
     unsigned size;
 
@@ -183,6 +184,16 @@
         return status;
     }
 
+    if (a->prod) {
+        if (strcmp(a->prod, cur_product) != 0) {
+            double split = now();
+            fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
+                    cur_product, a->prod, (split - a->start));
+            a->start = split;
+            return 0;
+        }
+    }
+
     yes = match(resp, value, count);
     if (invert) yes = !yes;
 
@@ -214,10 +225,12 @@
     return cb_check(a, status, resp, 1);
 }
 
-void fb_queue_require(const char *var, int invert, unsigned nvalues, const char **value)
+void fb_queue_require(const char *prod, const char *var,
+		int invert, unsigned nvalues, const char **value)
 {
     Action *a;
     a = queue_action(OP_QUERY, "getvar:%s", var);
+    a->prod = prod;
     a->data = value;
     a->size = nvalues;
     a->msg = mkmsg("checking %s", var);
@@ -244,6 +257,25 @@
     a->func = cb_display;
 }
 
+static int cb_save(Action *a, int status, char *resp)
+{
+    if (status) {
+        fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
+        return status;
+    }
+    strncpy(a->data, resp, a->size);
+    return 0;
+}
+
+void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
+{
+    Action *a;
+    a = queue_action(OP_QUERY, "getvar:%s", var);
+    a->data = (void *)dest;
+    a->size = dest_size;
+    a->func = cb_save;
+}
+
 static int cb_do_nothing(Action *a, int status, char *resp)
 {
     fprintf(stderr,"\n");
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 2ddc4f8..4a2de20 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -42,6 +42,8 @@
 
 #include "fastboot.h"
 
+char cur_product[FB_RESPONSE_SZ + 1];
+
 void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
 
 boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
@@ -340,6 +342,7 @@
 {
     char *val[MAX_OPTIONS];
     const char **out;
+    char *prod = NULL;
     unsigned n, count;
     char *x;
     int invert = 0;
@@ -350,6 +353,14 @@
     } else if (!strncmp(name, "require ", 8)) {
         name += 8;
         invert = 0;
+    } else if (!strncmp(name, "require-for-product:", 20)) {
+        // Get the product and point name past it
+        prod = name + 20;
+        name = strchr(name, ' ');
+        if (!name) return -1;
+        *name = 0;
+        name += 1;
+        invert = 0;
     }
 
     x = strchr(name, '=');
@@ -381,7 +392,7 @@
         if (out[n] == 0) return -1;
     }
 
-    fb_queue_require(name, invert, n, out);
+    fb_queue_require(prod, name, invert, n, out);
     return 0;
 }
 
@@ -432,6 +443,8 @@
 
     queue_info_dump();
 
+    fb_queue_query_save("product", cur_product, sizeof(cur_product));
+
     zdata = load_file(fn, &zsize);
     if (zdata == 0) die("failed to load '%s'", fn);
 
@@ -498,6 +511,8 @@
 
     queue_info_dump();
 
+    fb_queue_query_save("product", cur_product, sizeof(cur_product));
+
     fname = find_item("info", product);
     if (fname == 0) die("cannot find android-info.txt");
     data = load_file(fname, &sz);
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index a4b27a0..9e043fe 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -43,8 +43,10 @@
 /* engine.c - high level command queue engine */
 void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
 void fb_queue_erase(const char *ptn);
-void fb_queue_require(const char *var, int invert, unsigned nvalues, const char **value);
+void fb_queue_require(const char *prod, const char *var, int invert,
+        unsigned nvalues, const char **value);
 void fb_queue_display(const char *var, const char *prettyname);
+void fb_queue_query_save(const char *var, char *dest, unsigned dest_size);
 void fb_queue_reboot(void);
 void fb_queue_command(const char *cmd, const char *msg);
 void fb_queue_download(const char *name, void *data, unsigned size);
@@ -54,4 +56,7 @@
 /* util stuff */
 void die(const char *fmt, ...);
 
+/* Current product */
+extern char cur_product[FB_RESPONSE_SZ + 1];
+
 #endif
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index e5c58b9..e7a7130 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -269,7 +269,7 @@
     return 0;
 }
 
-int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
+int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, int prefix_length,
       struct in_addr gw)
 {
     struct rtentry rt;
@@ -301,7 +301,7 @@
         return -errno;
     }
 
-    result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt);
+    result = ioctl(ifc_ctl_sock, action, &rt);
     if (result < 0) {
         if (errno == EEXIST) {
             result = 0;
@@ -320,17 +320,7 @@
     in_dst.s_addr = 0;
     in_gw.s_addr = gw;
 
-    return ifc_add_ipv4_route(name, in_dst, 0, in_gw);
-}
-
-int ifc_add_host_route(const char *name, in_addr_t dst)
-{
-    struct in_addr in_dst, in_gw;
-
-    in_dst.s_addr = dst;
-    in_gw.s_addr = 0;
-
-    return ifc_add_ipv4_route(name, in_dst, 32, in_gw);
+    return ifc_act_on_route(SIOCADDRT, name, in_dst, 0, in_gw);
 }
 
 int ifc_enable(const char *ifname)
@@ -366,10 +356,12 @@
 int ifc_reset_connections(const char *ifname)
 {
 #ifdef HAVE_ANDROID_OS
-    int result;
+    int result, success;
     in_addr_t myaddr;
     struct ifreq ifr;
+    struct in6_ifreq ifr6;
 
+    /* IPv4. Clear connections on the IP address. */
     ifc_init();
     ifc_get_info(ifname, &myaddr, NULL, NULL);
     ifc_init_ifr(ifname, &ifr);
@@ -377,6 +369,21 @@
     result = ioctl(ifc_ctl_sock, SIOCKILLADDR,  &ifr);
     ifc_close();
 
+    /*
+     * IPv6. On Linux, when an interface goes down it loses all its IPv6
+     * addresses, so we don't know which connections belonged to that interface
+     * So we clear all unused IPv6 connections on the device by specifying an
+     * empty IPv6 address.
+     */
+    ifc_init6();
+    // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
+    memset(&ifr6, 0, sizeof(ifr6));
+    success = ioctl(ifc_ctl_sock6, SIOCKILLADDR,  &ifr6);
+    if (result == 0) {
+        result = success;
+    }
+    ifc_close6();
+
     return result;
 #else
     return 0;
@@ -432,67 +439,6 @@
 }
 
 /*
- * Return the address of the default gateway
- *
- * TODO: factor out common code from this and remove_host_routes()
- * so that we only scan /proc/net/route in one place.
- */
-int ifc_get_default_route(const char *ifname)
-{
-    char name[64];
-    in_addr_t dest, gway, mask;
-    int flags, refcnt, use, metric, mtu, win, irtt;
-    int result;
-    FILE *fp;
-
-    fp = fopen("/proc/net/route", "r");
-    if (fp == NULL)
-        return 0;
-    /* Skip the header line */
-    if (fscanf(fp, "%*[^\n]\n") < 0) {
-        fclose(fp);
-        return 0;
-    }
-    ifc_init();
-    result = 0;
-    for (;;) {
-        int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
-                           name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
-                           &mtu, &win, &irtt);
-        if (nread != 11) {
-            break;
-        }
-        if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
-                && dest == 0
-                && strcmp(ifname, name) == 0) {
-            result = gway;
-            break;
-        }
-    }
-    fclose(fp);
-    ifc_close();
-    return result;
-}
-
-/*
- * Sets the specified gateway as the default route for the named interface.
- */
-int ifc_set_default_route(const char *ifname, in_addr_t gateway)
-{
-    struct in_addr addr;
-    int result;
-
-    ifc_init();
-    addr.s_addr = gateway;
-    if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
-        LOGD("failed to add %s as default route for %s: %s",
-             inet_ntoa(addr), ifname, strerror(errno));
-    }
-    ifc_close();
-    return result;
-}
-
-/*
  * Removes the default route for the named interface.
  */
 int ifc_remove_default_route(const char *ifname)
@@ -555,7 +501,7 @@
     return 0;
 }
 
-int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
+int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
       struct in6_addr gw)
 {
     struct in6_rtmsg rtmsg;
@@ -590,7 +536,7 @@
         return -errno;
     }
 
-    result = ioctl(ifc_ctl_sock6, SIOCADDRT, &rtmsg);
+    result = ioctl(ifc_ctl_sock6, action, &rtmsg);
     if (result < 0) {
         if (errno == EEXIST) {
             result = 0;
@@ -602,8 +548,8 @@
     return result;
 }
 
-int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
-      const char *gw)
+int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
+        const char *gw)
 {
     int ret = 0;
     struct sockaddr_in ipv4_dst, ipv4_gw;
@@ -621,7 +567,7 @@
         return -EINVAL;
     }
 
-    if (gw == NULL) {
+    if (gw == NULL || (strlen(gw) == 0)) {
         if (addr_ai->ai_family == AF_INET6) {
             gw = "::";
         } else if (addr_ai->ai_family == AF_INET) {
@@ -629,6 +575,13 @@
         }
     }
 
+    if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
+            ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
+        printerr("ifc_add_route: invalid prefix length");
+        freeaddrinfo(addr_ai);
+        return -EINVAL;
+    }
+
     ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
     if (ret != 0) {
         printerr("getaddrinfo failed: invalid gateway %s\n", gw);
@@ -646,13 +599,13 @@
     if (addr_ai->ai_family == AF_INET6) {
         memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
         memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
-        ret = ifc_add_ipv6_route(ifname, ipv6_dst.sin6_addr, prefix_length,
-              ipv6_gw.sin6_addr);
+        ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
+                prefix_length, ipv6_gw.sin6_addr);
     } else if (addr_ai->ai_family == AF_INET) {
         memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
         memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
-        ret = ifc_add_ipv4_route(ifname, ipv4_dst.sin_addr, prefix_length,
-              ipv4_gw.sin_addr);
+        ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
+                prefix_length, ipv4_gw.sin_addr);
     } else {
         printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
                   addr_ai->ai_family);
@@ -663,3 +616,13 @@
     freeaddrinfo(gw_ai);
     return ret;
 }
+
+int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
+{
+    return ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
+}
+
+int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
+{
+    return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
+}