Add mtu6 option to override IPv6 MTU
[project/netifd.git] / system-linux.c
index 9ff1532..b44eb76 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/rtnetlink.h>
 #include <linux/sockios.h>
 #include <linux/ip.h>
+#include <linux/if_addr.h>
 #include <linux/if_link.h>
 #include <linux/if_vlan.h>
 #include <linux/if_bridge.h>
 #define RT_TABLE_PRELOCAL 128
 #endif
 
+#ifndef IFA_F_NOPREFIXROUTE
+#define IFA_F_NOPREFIXROUTE 0x200
+#endif
+
+#ifndef IFA_FLAGS
+#define IFA_FLAGS (IFA_MULTICAST + 1)
+#endif
+
+
 #include <string.h>
 #include <fcntl.h>
 #include <glob.h>
@@ -374,7 +384,6 @@ static int system_get_neigh6reachabletime(struct device *dev, char *buf, const s
 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
 {
        struct nlmsghdr *nh = nlmsg_hdr(msg);
-       struct ifinfomsg *ifi = NLMSG_DATA(nh);
        struct nlattr *nla[__IFLA_MAX];
        int link_state = 0;
        char buf[10];
@@ -382,7 +391,7 @@ static int cb_rtnl_event(struct nl_msg *msg, void *arg)
        if (nh->nlmsg_type != RTM_NEWLINK)
                goto out;
 
-       nlmsg_parse(nh, sizeof(*ifi), nla, __IFLA_MAX - 1, NULL);
+       nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
        if (!nla[IFLA_IFNAME])
                goto out;
 
@@ -782,6 +791,7 @@ sec_to_jiffies(int val)
 
 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
 {
+       char buf[64];
        unsigned long args[4] = {};
 
        if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
@@ -799,7 +809,11 @@ int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
                bridge->ifname, cfg->igmp_snoop ? "1" : "0");
 
        system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier",
-               bridge->ifname, cfg->igmp_snoop ? "1" : "0");
+               bridge->ifname, cfg->multicast_querier ? "1" : "0");
+
+       snprintf(buf, sizeof(buf), "%i", cfg->hash_max);
+       system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/hash_max",
+               bridge->ifname, buf);
 
        args[0] = BRCTL_SET_BRIDGE_PRIORITY;
        args[1] = cfg->priority;
@@ -1005,6 +1019,10 @@ system_if_get_settings(struct device *dev, struct device_settings *s)
                s->flags |= DEV_OPT_MTU;
        }
 
+       s->mtu6 = system_update_ipv6_mtu(dev, 0);
+       if (s->mtu6 > 0)
+               s->flags |= DEV_OPT_MTU6;
+
        if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
                s->txqueuelen = ifr.ifr_qlen;
                s->flags |= DEV_OPT_TXQUEUELEN;
@@ -1093,9 +1111,6 @@ system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned
 {
        struct ifreq ifr;
 
-       if (!apply_mask)
-               return;
-
        memset(&ifr, 0, sizeof(ifr));
        strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
        if (s->flags & DEV_OPT_MTU & apply_mask) {
@@ -1103,6 +1118,9 @@ system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned
                if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
                        s->flags &= ~DEV_OPT_MTU;
        }
+       if (s->flags & DEV_OPT_MTU6 & apply_mask) {
+               system_update_ipv6_mtu(dev, s->mtu6);
+       }
        if (s->flags & DEV_OPT_TXQUEUELEN & apply_mask) {
                ifr.ifr_qlen = s->txqueuelen;
                if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
@@ -1473,6 +1491,9 @@ static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
                }
 
                nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
+
+               if (cmd == RTM_NEWADDR && (addr->flags & DEVADDR_OFFLINK))
+                       nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
        }
 
        return system_rtnl_call(msg);
@@ -2149,19 +2170,18 @@ int system_update_ipv6_mtu(struct device *dev, int mtu)
                        dev->ifname);
 
        int fd = open(buf, O_RDWR);
-       ssize_t len = read(fd, buf, sizeof(buf) - 1);
-       if (len < 0)
-               goto out;
 
-       buf[len] = 0;
-       ret = atoi(buf);
-
-       if (!mtu || ret <= mtu)
-               goto out;
+       if (!mtu) {
+               ssize_t len = read(fd, buf, sizeof(buf) - 1);
+               if (len < 0)
+                       goto out;
 
-       lseek(fd, 0, SEEK_SET);
-       if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) <= 0)
-               ret = -1;
+               buf[len] = 0;
+               ret = atoi(buf);
+       } else {
+               if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
+                       ret = mtu;
+       }
 
 out:
        close(fd);