interface: fix jail ifdown and jails without jail_ifname
[project/netifd.git] / iprule.c
index 105f469b2b350ec244fa7f63aba1de19524d0820..c3a629f6103624e6cb8578959ecd3ed4c814bf7a 100644 (file)
--- a/iprule.c
+++ b/iprule.c
@@ -13,7 +13,6 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
-#include <assert.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -69,11 +68,13 @@ const struct uci_blob_param_list rule_attr_list = {
 };
 
 /* interface based rules are dynamic. */
-static bool rule_ready(struct iprule *rule) {
-       if (rule->flags & IPRULE_OUT && rule->out_dev == NULL)
+static bool
+rule_ready(struct iprule *rule)
+{
+       if (rule->flags & IPRULE_OUT && !rule->out_dev[0])
                return false;
 
-       if (rule->flags & IPRULE_IN && rule->in_dev == NULL)
+       if (rule->flags & IPRULE_IN && !rule->in_dev[0])
                return false;
 
        return true;
@@ -110,10 +111,8 @@ iprule_parse_mark(const char *mark, struct iprule *rule)
 }
 
 /* called on interface changes of the incoming interface */
-static void rule_in_cb(
-               struct interface_user *dep,
-               struct interface *iface,
-               enum interface_event ev)
+static void
+rule_in_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
 {
        struct iprule *rule = container_of(dep, struct iprule, in_iface_user);
 
@@ -121,7 +120,8 @@ static void rule_in_cb(
        case IFEV_UP:
                if (!iface->l3_dev.dev)
                        break;
-               memcpy(rule->in_dev, iface->l3_dev.dev->ifname, sizeof(rule->in_dev));
+
+               strcpy(rule->in_dev, iface->l3_dev.dev->ifname);
                if (rule_ready(rule))
                        system_add_iprule(rule);
                break;
@@ -130,6 +130,7 @@ static void rule_in_cb(
        case IFEV_FREE:
                if (rule_ready(rule))
                        system_del_iprule(rule);
+
                rule->in_dev[0] = 0;
                break;
        default:
@@ -138,10 +139,8 @@ static void rule_in_cb(
 }
 
 /* called on interface changes of the outgoing interface */
-static void rule_out_cb(
-               struct interface_user *dep,
-               struct interface *iface,
-               enum interface_event ev)
+static void
+rule_out_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
 {
        struct iprule *rule = container_of(dep, struct iprule, out_iface_user);
 
@@ -149,7 +148,8 @@ static void rule_out_cb(
        case IFEV_UP:
                if (!iface->l3_dev.dev)
                        break;
-               memcpy(rule->out_dev, iface->l3_dev.dev->ifname, sizeof(rule->out_dev));
+
+               strcpy(rule->out_dev, iface->l3_dev.dev->ifname);
                if (rule_ready(rule))
                        system_add_iprule(rule);
                break;
@@ -158,6 +158,7 @@ static void rule_out_cb(
        case IFEV_FREE:
                if (rule_ready(rule))
                        system_del_iprule(rule);
+
                rule->out_dev[0] = 0;
                break;
        default:
@@ -166,10 +167,9 @@ static void rule_out_cb(
 }
 
 /* called on all interface events */
-static void generic_interface_cb(
-               struct interface_user *dep,
-               struct interface *iface,
-               enum interface_event ev)
+static void
+generic_interface_cb(struct interface_user *dep,
+                       struct interface *iface, enum interface_event ev)
 {
        struct iprule *rule;
 
@@ -181,17 +181,11 @@ static void generic_interface_cb(
                if (rule_ready(rule))
                        continue;
 
-               if (!strcmp(rule->out_iface, iface->name)) {
-                       assert(!rule->out_dev);
-                       memcpy(rule->out_dev, iface->l3_dev.dev->ifname, sizeof(rule->out_dev));
+               if ((rule->flags & IPRULE_OUT) && !strcmp(rule->out_iface, iface->name))
                        interface_add_user(&rule->out_iface_user, iface);
-               }
 
-               if (!strcmp(rule->in_iface, iface->name)) {
-                       assert(!rule->in_dev);
-                       memcpy(rule->in_dev, iface->l3_dev.dev->ifname, sizeof(rule->in_dev));
+               if ((rule->flags & IPRULE_IN) && !strcmp(rule->in_iface, iface->name))
                        interface_add_user(&rule->in_iface_user, iface);
-               }
        }
 }
 
@@ -296,7 +290,7 @@ iprule_add(struct blob_attr *attr, bool v6)
                rule->flags |= IPRULE_GOTO;
        }
 
-       vlist_add(&iprules, &rule->node, &rule->flags);
+       vlist_add(&iprules, &rule->node, rule);
        return;
 
 error:
@@ -326,7 +320,32 @@ iprule_update_complete(void)
 static int
 rule_cmp(const void *k1, const void *k2, void *ptr)
 {
-       return memcmp(k1, k2, sizeof(struct iprule)-offsetof(struct iprule, flags));
+       const struct iprule *r1 = k1, *r2 = k2;
+       int ret;
+
+       /* First compare the interface names */
+       if (r1->flags & IPRULE_IN || r2->flags & IPRULE_IN) {
+               char *str1 = r1->flags & IPRULE_IN ? r1->in_iface : "";
+               char *str2 = r2->flags & IPRULE_IN ? r2->in_iface : "";
+
+               ret = strcmp(str1, str2);
+               if (ret)
+                       return ret;
+       }
+
+       if (r1->flags & IPRULE_OUT || r2->flags & IPRULE_OUT) {
+               char *str1 = r1->flags & IPRULE_OUT ? r1->out_iface : "";
+               char *str2 = r2->flags & IPRULE_OUT ? r2->out_iface : "";
+
+               ret = strcmp(str1, str2);
+               if (ret)
+                       return ret;
+       }
+
+       /* Next compare everything after the flags field */
+       return memcmp(k1 + offsetof(struct iprule, flags),
+                     k2 + offsetof(struct iprule, flags),
+                     sizeof(struct iprule) - offsetof(struct iprule, flags));
 }
 
 static void deregister_interfaces(struct iprule *rule)