netifd: vxlan: handle srcport range
[project/netifd.git] / system-linux.c
index 97b38e750c63ba697a3111fd50ee156c4bb96d5f..42f0de3bbe0ec6259d69a6454dd02f4506d1599f 100644 (file)
@@ -854,6 +854,53 @@ int system_bridge_delif(struct device *bridge, struct device *dev)
        return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
 }
 
+int system_bridge_vlan(const char *iface, uint16_t vid, bool add, unsigned int vflags)
+{
+       struct ifinfomsg ifi = { .ifi_family = PF_BRIDGE, };
+       struct bridge_vlan_info vinfo = { .vid = vid, };
+       unsigned short flags = 0;
+       struct nlattr *afspec;
+       struct nl_msg *nlm;
+       int ret = 0;
+
+       ifi.ifi_index = if_nametoindex(iface);
+       if (!ifi.ifi_index)
+               return -1;
+
+       nlm = nlmsg_alloc_simple(add ? RTM_SETLINK : RTM_DELLINK, NLM_F_REQUEST);
+       if (!nlm)
+               return -1;
+
+       nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
+
+       if (vflags & BRVLAN_F_SELF)
+               flags |= BRIDGE_FLAGS_SELF;
+
+       if (vflags & BRVLAN_F_PVID)
+               vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
+
+       if (vflags & BRVLAN_F_UNTAGGED)
+               vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
+
+       afspec = nla_nest_start(nlm, IFLA_AF_SPEC);
+       if (!afspec) {
+               ret = -ENOMEM;
+               goto failure;
+       }
+
+       if (flags)
+               nla_put_u16(nlm, IFLA_BRIDGE_FLAGS, flags);
+
+       nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
+       nla_nest_end(nlm, afspec);
+
+       return system_rtnl_call(nlm);
+
+failure:
+       nlmsg_free(nlm);
+       return ret;
+}
+
 int system_if_resolve(struct device *dev)
 {
        struct ifreq ifr;
@@ -3137,6 +3184,32 @@ static int system_add_vxlan(const char *name, const unsigned int link, struct bl
        }
        nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
 
+       if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
+               struct ifla_vxlan_port_range srcports = {0,0};
+
+               uint32_t low = blobmsg_get_u32(cur);
+               if (low < 1 || low > 65535 - 1) {
+                       ret = -EINVAL;
+                       goto failure;
+               }
+
+               srcports.low = htons((uint16_t) low);
+               srcports.high = htons((uint16_t) (low+1));
+
+               if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
+                       uint32_t high = blobmsg_get_u32(cur);
+                       if (high < 1 || high > 65535) {
+                               ret = -EINVAL;
+                               goto failure;
+                       }
+
+                       if (high > low)
+                               srcports.high = htons((uint16_t) high);
+               }
+
+               nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
+       }
+
        if ((cur = tb_data[VXLAN_DATA_ATTR_RXCSUM])) {
                bool rxcsum = blobmsg_get_bool(cur);
                nla_put_u8(msg, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, !rxcsum);