Add netlink and setup for encapsulation

Signed-off-by: Tom Herbert <t...@herbertland.com>
---
 net/ipv6/ip6_gre.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c
index 70a1f72..ed5ddcc 100644
--- a/net/ipv6/ip6_gre.c
+++ b/net/ipv6/ip6_gre.c
@@ -1027,6 +1027,8 @@ static int ip6gre_tunnel_init_common(struct net_device 
*dev)
 
        tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
 
+       tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
+
        t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
 
        dev->needed_headroom    = LL_MAX_HEADER + t_hlen + 4;
@@ -1293,15 +1295,57 @@ static void ip6gre_tap_setup(struct net_device *dev)
        dev->priv_flags &= ~IFF_TX_SKB_SHARING;
 }
 
+static bool ip6gre_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_GRE_ENCAP_TYPE]) {
+               ret = true;
+               ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
+       }
+
+       if (data[IFLA_GRE_ENCAP_FLAGS]) {
+               ret = true;
+               ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
+       }
+
+       if (data[IFLA_GRE_ENCAP_SPORT]) {
+               ret = true;
+               ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
+       }
+
+       if (data[IFLA_GRE_ENCAP_DPORT]) {
+               ret = true;
+               ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
+       }
+
+       return ret;
+}
+
 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
        struct nlattr *tb[], struct nlattr *data[])
 {
        struct ip6_tnl *nt;
        struct net *net = dev_net(dev);
        struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
+       struct ip_tunnel_encap ipencap;
        int err;
 
        nt = netdev_priv(dev);
+
+       if (ip6gre_netlink_encap_parms(data, &ipencap)) {
+               int err = ip6_tnl_encap_setup(nt, &ipencap);
+
+               if (err < 0)
+                       return err;
+       }
+
        ip6gre_netlink_parms(data, &nt->parms);
 
        if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
@@ -1348,10 +1392,18 @@ static int ip6gre_changelink(struct net_device *dev, 
struct nlattr *tb[],
        struct net *net = nt->net;
        struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
        struct __ip6_tnl_parm p;
+       struct ip_tunnel_encap ipencap;
 
        if (dev == ign->fb_tunnel_dev)
                return -EINVAL;
 
+       if (ip6gre_netlink_encap_parms(data, &ipencap)) {
+               int err = ip6_tnl_encap_setup(nt, &ipencap);
+
+               if (err < 0)
+                       return err;
+       }
+
        ip6gre_netlink_parms(data, &p);
 
        t = ip6gre_tunnel_locate(net, &p, 0);
@@ -1405,6 +1457,14 @@ static size_t ip6gre_get_size(const struct net_device 
*dev)
                nla_total_size(4) +
                /* IFLA_GRE_FLAGS */
                nla_total_size(4) +
+               /* IFLA_GRE_ENCAP_TYPE */
+               nla_total_size(2) +
+               /* IFLA_GRE_ENCAP_FLAGS */
+               nla_total_size(2) +
+               /* IFLA_GRE_ENCAP_SPORT */
+               nla_total_size(2) +
+               /* IFLA_GRE_ENCAP_DPORT */
+               nla_total_size(2) +
                0;
 }
 
@@ -1428,6 +1488,17 @@ static int ip6gre_fill_info(struct sk_buff *skb, const 
struct net_device *dev)
            nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
            nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
                goto nla_put_failure;
+
+       if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
+                       t->encap.type) ||
+           nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
+                        t->encap.sport) ||
+           nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
+                        t->encap.dport) ||
+           nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
+                       t->encap.flags))
+               goto nla_put_failure;
+
        return 0;
 
 nla_put_failure:
@@ -1446,6 +1517,10 @@ static const struct nla_policy 
ip6gre_policy[IFLA_GRE_MAX + 1] = {
        [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
        [IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
        [IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
+       [IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
+       [IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
+       [IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
+       [IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
 };
 
 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
-- 
2.8.0.rc2

Reply via email to