net: Add helper function to parse netlink msg of ip_tunnel_encap
authorLiu Jian <liujian56@huawei.com>
Thu, 29 Sep 2022 13:52:02 +0000 (21:52 +0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 3 Oct 2022 06:59:06 +0000 (07:59 +0100)
Add ip_tunnel_netlink_encap_parms to parse netlink msg of ip_tunnel_encap.
Reduces duplicate code, no actual functional changes.

Signed-off-by: Liu Jian <liujian56@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/ip_tunnels.h
net/ipv4/ip_tunnel_core.c
net/ipv4/ipip.c
net/ipv6/ip6_tunnel.c
net/ipv6/sit.c

index ced80e2f8b58f1e363a4c46ff602451ce9a066bc..51da2957cf48faa50511518a713b2b73e895665e 100644 (file)
@@ -302,6 +302,9 @@ int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
                      struct ip_tunnel_parm *p, __u32 fwmark);
 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
 
+bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
+                                  struct ip_tunnel_encap *encap);
+
 extern const struct header_ops ip_tunnel_header_ops;
 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb);
 
index cc1caab4a654921a267bac1cca56bb91244b9ea9..6d08f7e3919151823504230b786d5dc177a4b3db 100644 (file)
@@ -1079,3 +1079,38 @@ EXPORT_SYMBOL(ip_tunnel_parse_protocol);
 
 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
 EXPORT_SYMBOL(ip_tunnel_header_ops);
+
+/* This function returns true when ENCAP attributes are present in the nl msg */
+bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
+                                  struct ip_tunnel_encap *encap)
+{
+       bool ret = false;
+
+       memset(encap, 0, sizeof(*encap));
+
+       if (!data)
+               return ret;
+
+       if (data[IFLA_IPTUN_ENCAP_TYPE]) {
+               ret = true;
+               encap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
+       }
+
+       if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
+               ret = true;
+               encap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
+       }
+
+       if (data[IFLA_IPTUN_ENCAP_SPORT]) {
+               ret = true;
+               encap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
+       }
+
+       if (data[IFLA_IPTUN_ENCAP_DPORT]) {
+               ret = true;
+               encap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
+       }
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(ip_tunnel_netlink_encap_parms);
index 123ea63a04cbf04786bd7a170ea942301cc9e0bb..7c64ca06adf38caf5929fccc3719c36784bd3f28 100644 (file)
@@ -448,40 +448,6 @@ static void ipip_netlink_parms(struct nlattr *data[],
                *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
 }
 
-/* This function returns true when ENCAP attributes are present in the nl msg */
-static bool ipip_netlink_encap_parms(struct nlattr *data[],
-                                    struct ip_tunnel_encap *ipencap)
-{
-       bool ret = false;
-
-       memset(ipencap, 0, sizeof(*ipencap));
-
-       if (!data)
-               return ret;
-
-       if (data[IFLA_IPTUN_ENCAP_TYPE]) {
-               ret = true;
-               ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
-               ret = true;
-               ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_SPORT]) {
-               ret = true;
-               ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_DPORT]) {
-               ret = true;
-               ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
-       }
-
-       return ret;
-}
-
 static int ipip_newlink(struct net *src_net, struct net_device *dev,
                        struct nlattr *tb[], struct nlattr *data[],
                        struct netlink_ext_ack *extack)
@@ -491,7 +457,7 @@ static int ipip_newlink(struct net *src_net, struct net_device *dev,
        struct ip_tunnel_encap ipencap;
        __u32 fwmark = 0;
 
-       if (ipip_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                int err = ip_tunnel_encap_setup(t, &ipencap);
 
                if (err < 0)
@@ -512,7 +478,7 @@ static int ipip_changelink(struct net_device *dev, struct nlattr *tb[],
        bool collect_md;
        __u32 fwmark = t->fwmark;
 
-       if (ipip_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                int err = ip_tunnel_encap_setup(t, &ipencap);
 
                if (err < 0)
index 9e97f3b4c7e815cdd8d0695f5fd3e8cd76ce29fa..cc5d5e75b658f3972122d573eac88c7d59d637cb 100644 (file)
@@ -1988,39 +1988,6 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[],
                parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
 }
 
-static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
-                                       struct ip_tunnel_encap *ipencap)
-{
-       bool ret = false;
-
-       memset(ipencap, 0, sizeof(*ipencap));
-
-       if (!data)
-               return ret;
-
-       if (data[IFLA_IPTUN_ENCAP_TYPE]) {
-               ret = true;
-               ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
-               ret = true;
-               ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_SPORT]) {
-               ret = true;
-               ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_DPORT]) {
-               ret = true;
-               ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
-       }
-
-       return ret;
-}
-
 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
                           struct nlattr *tb[], struct nlattr *data[],
                           struct netlink_ext_ack *extack)
@@ -2033,7 +2000,7 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
 
        nt = netdev_priv(dev);
 
-       if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                err = ip6_tnl_encap_setup(nt, &ipencap);
                if (err < 0)
                        return err;
@@ -2070,7 +2037,7 @@ static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
        if (dev == ip6n->fb_tnl_dev)
                return -EINVAL;
 
-       if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                int err = ip6_tnl_encap_setup(t, &ipencap);
 
                if (err < 0)
index 98f1cf40746f6a582d1c995de67bb62cc1519bd8..a8a258f672fa03890ab55fa84ed2d52fe5c135ed 100644 (file)
@@ -1534,40 +1534,6 @@ static void ipip6_netlink_parms(struct nlattr *data[],
                *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
 }
 
-/* This function returns true when ENCAP attributes are present in the nl msg */
-static bool ipip6_netlink_encap_parms(struct nlattr *data[],
-                                     struct ip_tunnel_encap *ipencap)
-{
-       bool ret = false;
-
-       memset(ipencap, 0, sizeof(*ipencap));
-
-       if (!data)
-               return ret;
-
-       if (data[IFLA_IPTUN_ENCAP_TYPE]) {
-               ret = true;
-               ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
-               ret = true;
-               ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_SPORT]) {
-               ret = true;
-               ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
-       }
-
-       if (data[IFLA_IPTUN_ENCAP_DPORT]) {
-               ret = true;
-               ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
-       }
-
-       return ret;
-}
-
 #ifdef CONFIG_IPV6_SIT_6RD
 /* This function returns true when 6RD attributes are present in the nl msg */
 static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
@@ -1619,7 +1585,7 @@ static int ipip6_newlink(struct net *src_net, struct net_device *dev,
 
        nt = netdev_priv(dev);
 
-       if (ipip6_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                err = ip_tunnel_encap_setup(nt, &ipencap);
                if (err < 0)
                        return err;
@@ -1671,7 +1637,7 @@ static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
        if (dev == sitn->fb_tunnel_dev)
                return -EINVAL;
 
-       if (ipip6_netlink_encap_parms(data, &ipencap)) {
+       if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
                err = ip_tunnel_encap_setup(t, &ipencap);
                if (err < 0)
                        return err;