[PATCH] libnl: introduce gre tunnel api

Susant Sahani susant at redhat.com
Wed Apr 23 09:45:46 PDT 2014


This patch introduces ip gre tunnel support
---
 include/Makefile.am                |   1 +
 include/netlink/route/link/ipgre.h |  59 ++++
 lib/Makefile.am                    |   2 +-
 lib/route/link/ipgre.c             | 591 +++++++++++++++++++++++++++++++++++++
 tests/Makefile.am                  |   1 +
 tests/test-create-gre.c            |  58 ++++
 6 files changed, 711 insertions(+), 1 deletion(-)
 create mode 100644 include/netlink/route/link/ipgre.h
 create mode 100644 lib/route/link/ipgre.c
 create mode 100644 tests/test-create-gre.c

diff --git a/include/Makefile.am b/include/Makefile.am
index 1b13f5a..523b34e 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -53,6 +53,7 @@ nobase_libnlinclude_HEADERS = \
 	netlink/route/link/vlan.h \
 	netlink/route/link/vxlan.h \
 	netlink/route/link/veth.h \
+	netlink/route/link/ipgre.h \
 	netlink/route/qdisc/cbq.h \
 	netlink/route/qdisc/dsmark.h \
 	netlink/route/qdisc/fifo.h \
diff --git a/include/netlink/route/link/ipgre.h b/include/netlink/route/link/ipgre.h
new file mode 100644
index 0000000..230e8ac
--- /dev/null
+++ b/include/netlink/route/link/ipgre.h
@@ -0,0 +1,59 @@
+/*
+ * netlink/route/link/ip_gre.h		IPIP interface
+ *
+ *	This library is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU Lesser General Public
+ *	License as published by the Free Software Foundation version 2.1
+ *	of the License.
+ *
+ * Copyright (c) 2014 Susant Sahani <susant at redhat.com>
+ */
+
+#ifndef NETLINK_LINK_IPGRE_H_
+#define NETLINK_LINK_IPGRE_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+        extern struct rtnl_link *rtnl_link_ipgre_alloc(void);
+        extern int rtnl_link_ipgre_add(struct nl_sock *sk, const char *name);
+
+        extern int rtnl_link_ipgre_set_link(struct rtnl_link *link,  uint32_t index);
+        extern uint32_t rtnl_link_ipgre_get_link(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_iflags(struct rtnl_link *link, uint16_t iflags);
+        extern uint16_t rtnl_link_get_iflags(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_oflags(struct rtnl_link *link, uint16_t oflags);
+        extern uint16_t rtnl_link_get_oflags(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_ikey(struct rtnl_link *link, uint32_t ikey);
+        extern uint32_t rtnl_link_get_ikey(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_okey(struct rtnl_link *link, uint32_t okey);
+        extern uint32_t rtnl_link_get_okey(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_local(struct rtnl_link *link, uint32_t addr);
+        extern uint32_t rtnl_link_get_local(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_remote(struct rtnl_link *link, uint32_t addr);
+        extern uint32_t rtnl_link_get_remote(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_ttl(struct rtnl_link *link, uint8_t ttl);
+        extern uint8_t rtnl_link_get_ttl(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_tos(struct rtnl_link *link, uint8_t tos);
+        extern uint8_t rtnl_link_get_tos(struct rtnl_link *link);
+
+        extern int rtnl_link_ipgre_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc);
+        extern uint8_t rtnl_link_get_pmtudisc(struct rtnl_link *link);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4a1e8d9..db3b638 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -78,7 +78,7 @@ libnl_route_3_la_SOURCES = \
 	route/link/api.c route/link/vlan.c route/link/dummy.c \
 	route/link/bridge.c route/link/inet6.c route/link/inet.c \
 	route/link/bonding.c route/link/can.c route/link/macvlan.c \
-	route/link/vxlan.c route/link/veth.c \
+	route/link/vxlan.c route/link/veth.c route/link/ipgre.c \
 	\
 	route/qdisc/blackhole.c route/qdisc/cbq.c route/qdisc/dsmark.c \
 	route/qdisc/fifo.c route/qdisc/htb.c route/qdisc/netem.c \
diff --git a/lib/route/link/ipgre.c b/lib/route/link/ipgre.c
new file mode 100644
index 0000000..6f26abf
--- /dev/null
+++ b/lib/route/link/ipgre.c
@@ -0,0 +1,591 @@
+/*
+ * lib/route/link/ip_gre.c        IPGRE Link Info
+ *
+ *      This library is free software; you can redistribute it and/or
+ *      modify it under the terms of the GNU Lesser General Public
+ *      License as published by the Free Software Foundation version 2.1
+ *      of the License.
+ *
+ * Copyright (c) 2014 Susant Sahani <susant at redhat.com>
+ */
+
+/**
+ * @ingroup link
+ * @defgroup ipgre IPGRE
+ * ipgre link module
+ *
+ * @details
+ * \b Link Type Name: "ipgre"
+ *
+ * @route_doc{link_ipgre, IPGRE Documentation}
+ *
+ * @{
+ */
+
+#include <netlink-private/netlink.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+#include <netlink/route/rtnl.h>
+#include <netlink-private/route/link/api.h>
+#include <linux/if_tunnel.h>
+
+#define IPGRE_ATTR_LINK          (1 << 0)
+#define IPGRE_ATTR_IFLAGS        (1 << 1)
+#define IPGRE_ATTR_OFLAGS        (1 << 2)
+#define IPGRE_ATTR_IKEY          (1 << 3)
+#define IPGRE_ATTR_OKEY          (1 << 4)
+#define IPGRE_ATTR_LOCAL         (1 << 5)
+#define IPGRE_ATTR_REMOTE        (1 << 6)
+#define IPGRE_ATTR_TTL           (1 << 7)
+#define IPGRE_ATTR_TOS           (1 << 8)
+#define IPGRE_ATTR_PMTUDISC      (1 << 9)
+
+struct ipgre_info
+{
+        uint8_t    ttl;
+        uint8_t    tos;
+        uint8_t    pmtudisc;
+        uint16_t   iflags;
+        uint16_t   oflags;
+        uint32_t   ikey;
+        uint32_t   okey;
+        uint32_t   link;
+        uint32_t   local;
+        uint32_t   remote;
+        uint32_t   ipgre_mask;
+};
+
+static  struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
+        [IFLA_GRE_LINK]     = { .type = NLA_U32 },
+        [IFLA_GRE_IFLAGS]   = { .type = NLA_U16 },
+        [IFLA_GRE_OFLAGS]   = { .type = NLA_U16 },
+        [IFLA_GRE_IKEY]     = { .type = NLA_U32 },
+        [IFLA_GRE_OKEY]     = { .type = NLA_U32 },
+        [IFLA_GRE_LOCAL]    = { .type = NLA_U32 },
+        [IFLA_GRE_REMOTE]   = { .type = NLA_U32 },
+        [IFLA_GRE_TTL]      = { .type = NLA_U8 },
+        [IFLA_GRE_TOS]      = { .type = NLA_U8 },
+        [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
+};
+
+static int ipgre_alloc(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre;
+
+        ipgre = calloc(1, sizeof(*ipgre));
+        if (!ipgre)
+                return -NLE_NOMEM;
+
+        link->l_info = ipgre;
+
+        return 0;
+}
+
+static int ipgre_parse(struct rtnl_link *link, struct nlattr *data,
+                       struct nlattr *xstats)
+{
+        struct nlattr *tb[IFLA_IPTUN_MAX + 1];
+        struct ipgre_info *ipgre;
+        int err;
+
+        NL_DBG(3, "Parsing IPGRE link info");
+
+        err = nla_parse_nested(tb, IFLA_GRE_MAX, data, ipgre_policy);
+        if (err < 0)
+                goto errout;
+
+        err = ipgre_alloc(link);
+        if (err < 0)
+                goto errout;
+
+        ipgre = link->l_info;
+        
+        if (tb[IFLA_GRE_LINK]) {
+                ipgre->link = nla_get_u32(tb[IFLA_GRE_LINK]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_LINK;
+        }
+
+        if (tb[IFLA_GRE_IFLAGS]) {
+                ipgre->iflags = nla_get_u16(tb[IFLA_GRE_IFLAGS]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_IFLAGS;
+        }
+
+        if (tb[IFLA_GRE_OFLAGS]) {
+                ipgre->oflags = nla_get_u16(tb[IFLA_GRE_OFLAGS]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_OFLAGS;
+        }
+
+        if (tb[IFLA_GRE_IKEY]) {
+                ipgre->ikey = nla_get_u32(tb[IFLA_GRE_IKEY]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_IKEY;
+        }
+
+        if (tb[IFLA_GRE_OKEY]) {
+                ipgre->okey = nla_get_u32(tb[IFLA_GRE_OKEY]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_OKEY;
+        }
+
+        if (tb[IFLA_GRE_LOCAL]) {
+                ipgre->local = nla_get_u32(tb[IFLA_GRE_LOCAL]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_LOCAL;
+        }
+
+        if (tb[IFLA_GRE_LOCAL]) {
+                ipgre->remote = nla_get_u32(tb[IFLA_GRE_LOCAL]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_REMOTE;
+        }
+
+        if (tb[IFLA_GRE_TTL]) {
+                ipgre->ttl = nla_get_u8(tb[IFLA_GRE_TTL]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_TTL;
+        }
+
+        if (tb[IFLA_GRE_TOS]) {
+                ipgre->tos = nla_get_u8(tb[IFLA_GRE_TOS]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_TOS;
+        }
+
+        if (tb[IFLA_GRE_PMTUDISC]) {
+                ipgre->pmtudisc = nla_get_u8(tb[IFLA_GRE_PMTUDISC]);
+                ipgre->ipgre_mask |= IPGRE_ATTR_PMTUDISC;
+        }
+
+        err = 0;
+
+ errout:
+        return err;
+}
+
+static int ipgre_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+        struct nlattr *data;
+
+        data = nla_nest_start(msg, IFLA_INFO_DATA);
+        if (!data)
+                return -NLE_MSGSIZE;
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_LINK)
+                NLA_PUT_U32(msg, IFLA_GRE_LINK, ipgre->link);
+
+        if (ipgre->ipgre_mask & IFLA_GRE_IFLAGS)
+                NLA_PUT_U16(msg, IFLA_GRE_IFLAGS, ipgre->iflags);
+
+        if (ipgre->ipgre_mask & IFLA_GRE_OFLAGS)
+                NLA_PUT_U16(msg, IFLA_GRE_OFLAGS, ipgre->oflags);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_IKEY)
+                NLA_PUT_U32(msg, IFLA_GRE_IKEY, ipgre->ikey);
+
+        if (ipgre->ipgre_mask & IFLA_GRE_OKEY)
+                NLA_PUT_U32(msg, IFLA_GRE_OKEY, ipgre->okey);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_LOCAL)
+                NLA_PUT_U32(msg, IFLA_GRE_LOCAL, ipgre->local);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_REMOTE)
+                NLA_PUT_U32(msg, IFLA_GRE_REMOTE, ipgre->remote);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_TTL)
+                NLA_PUT_U8(msg, IFLA_GRE_TTL, ipgre->ttl);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_TOS)
+                NLA_PUT_U8(msg, IFLA_GRE_TOS, ipgre->tos);
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_PMTUDISC)
+                NLA_PUT_U8(msg, IFLA_GRE_PMTUDISC, ipgre->pmtudisc);
+
+        nla_nest_end(msg, data);
+
+ nla_put_failure:
+
+        return 0;
+}
+
+static void ipgre_free(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        free(ipgre);
+        link->l_info = NULL;
+}
+
+static void ipgre_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
+{
+        nl_dump(p, "ipgre : %s", link->l_name);
+}
+
+static void ipgre_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
+{
+        struct ipgre_info *ipgre = link->l_info;
+        char *name, addr[INET_ADDRSTRLEN];
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_LINK) {
+                nl_dump(p, "      link ");
+                name = rtnl_link_get_name(link);
+                if (name)
+                        nl_dump_line(p, "%s\n", name);
+                else
+                        nl_dump_line(p, "%u\n", ipgre->link);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_IFLAGS) {
+                nl_dump(p, "      iflags ");
+                nl_dump_line(p, "%x\n", ipgre->iflags);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_OFLAGS) {
+                nl_dump(p, "      oflags ");
+                nl_dump_line(p, "%x\n", ipgre->oflags);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_IKEY) {
+                nl_dump(p, "    ikey   ");
+                nl_dump_line(p, "%x\n",ipgre->ikey);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_OKEY) {
+                nl_dump(p, "      okey ");
+                nl_dump_line(p, "%x\n", ipgre->okey);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_LOCAL) {
+                nl_dump(p, "      local ");
+                if(inet_ntop(AF_INET, &ipgre->local, addr, sizeof(addr)))
+                        nl_dump_line(p, "%s\n", addr);
+                else
+                        nl_dump_line(p, "%#x\n", ntohs(ipgre->local));
+        } 
+        
+        if (ipgre->ipgre_mask & IPGRE_ATTR_REMOTE) {
+                nl_dump(p, "      remote ");
+                if(inet_ntop(AF_INET, &ipgre->remote, addr, sizeof(addr)))
+                        nl_dump_line(p, "%s\n", addr);
+                else
+                        nl_dump_line(p, "%#x\n", ntohs(ipgre->remote));
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_TTL) {
+                nl_dump(p, "      ttl ");
+                nl_dump_line(p, "%u\n", ipgre->ttl);
+        }
+        
+        if (ipgre->ipgre_mask & IPGRE_ATTR_TOS) {
+                nl_dump(p, "      tos ");
+                nl_dump_line(p, "%u\n", ipgre->tos);
+        }
+
+        if (ipgre->ipgre_mask & IPGRE_ATTR_PMTUDISC) {
+                nl_dump(p, "      pmtudisc ");
+                nl_dump_line(p, "enabled (%#x)\n", ipgre->pmtudisc);
+        }
+}
+
+static int ipgre_clone(struct rtnl_link *dst, struct rtnl_link *src)
+{
+        struct ipgre_info *ipgre_dst, *ipgre_src = src->l_info;
+        int err;
+
+        dst->l_info = NULL;
+
+        err = rtnl_link_set_type(dst, "gre");
+        if (err < 0)
+                return err;
+
+        ipgre_dst = dst->l_info;
+
+        if (!ipgre_dst || !ipgre_src)
+                return -NLE_NOMEM;
+
+        memcpy(ipgre_dst, ipgre_src, sizeof(struct ipgre_info));
+
+        return 0;
+}
+
+static struct rtnl_link_info_ops ipgre_info_ops = {
+        .io_name                = "gre",
+        .io_alloc               = ipgre_alloc,
+        .io_parse               = ipgre_parse,
+        .io_dump = {
+                [NL_DUMP_LINE]  = ipgre_dump_line,
+                [NL_DUMP_DETAILS] = ipgre_dump_details,
+        },
+        .io_clone               = ipgre_clone,
+        .io_put_attrs           = ipgre_put_attrs,
+        .io_free                = ipgre_free,
+};
+
+#define IS_IPGRE_LINK_ASSERT(link)                                            \
+        if ((link)->l_info_ops != &ipgre_info_ops) {                          \
+                APPBUG("Link is not a ipgre link. set type \"gre\" first.");\
+                return -NLE_OPNOTSUPP;                                        \
+        }
+
+struct rtnl_link *rtnl_link_ipgre_alloc(void)
+{
+        struct rtnl_link *link;
+        int err;
+
+        link = rtnl_link_alloc();
+        if (!link)
+                return NULL;
+
+        err = rtnl_link_set_type(link, "gre");
+        if (err < 0) {
+                rtnl_link_put(link);
+                return NULL;
+        }
+
+        return link;
+}
+
+/**
+ * Check if link is a IPGRE link
+ * @arg link            Link object
+ *
+ * @return True if link is a IPGRE link, otherwise false is returned.
+ */
+int rtnl_link_is_ipgre(struct rtnl_link *link)
+{
+        return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "gre");
+}
+
+int rtnl_link_ipgre_add(struct nl_sock *sk, const char *name)
+{
+        struct rtnl_link *link;
+        int err;
+
+        link = rtnl_link_ipgre_alloc();
+        if (!link)
+                return -NLE_NOMEM;
+
+        if(name)
+                rtnl_link_set_name(link, name);
+
+        err = rtnl_link_add(sk, link, NLM_F_CREATE);
+        rtnl_link_put(link);
+
+        return err;
+}
+
+int rtnl_link_ipgre_set_link(struct rtnl_link *link,  uint32_t index)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->link = index;
+        ipgre->ipgre_mask |= IPGRE_ATTR_LINK;
+
+        return 0;
+}
+
+uint32_t rtnl_link_ipgre_get_link(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->link;
+}
+
+int rtnl_link_ipgre_set_iflags(struct rtnl_link *link, uint16_t iflags)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->iflags = iflags;
+        ipgre->ipgre_mask |= IPGRE_ATTR_IFLAGS;
+
+        return 0;
+}
+
+uint16_t rtnl_link_get_iflags(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->iflags;
+}
+
+int rtnl_link_ipgre_set_oflags(struct rtnl_link *link, uint16_t oflags)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->oflags = oflags;
+        ipgre->ipgre_mask |= IPGRE_ATTR_OFLAGS;
+
+        return 0;
+}
+
+uint16_t rtnl_link_get_oflags(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->oflags;
+}
+
+int rtnl_link_ipgre_set_ikey(struct rtnl_link *link, uint32_t ikey)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->ikey = ikey;
+        ipgre->ipgre_mask |= IPGRE_ATTR_IKEY;
+
+        return 0;
+}
+
+uint32_t rtnl_link_get_ikey(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->ikey;
+}
+
+int rtnl_link_ipgre_set_okey(struct rtnl_link *link, uint32_t okey)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->okey = okey;
+        ipgre->ipgre_mask |= IPGRE_ATTR_OKEY;
+
+        return 0;
+}
+
+uint32_t rtnl_link_get_okey(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->okey;
+}
+
+int rtnl_link_ipgre_set_local(struct rtnl_link *link, uint32_t addr)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->local = addr;
+        ipgre->ipgre_mask |= IPGRE_ATTR_LOCAL;
+
+        return 0;
+}
+
+uint32_t rtnl_link_get_local(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->local;
+}
+
+int rtnl_link_ipgre_set_remote(struct rtnl_link *link, uint32_t addr)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->remote = addr;
+        ipgre->ipgre_mask |= IPGRE_ATTR_REMOTE;
+
+        return 0;
+}
+
+uint32_t rtnl_link_get_remote(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->remote;
+}
+
+int rtnl_link_ipgre_set_ttl(struct rtnl_link *link, uint8_t ttl)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->ttl = ttl;
+        ipgre->ipgre_mask |= IPGRE_ATTR_TTL;
+
+        return 0;
+}
+
+uint8_t rtnl_link_get_ttl(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->ttl;
+}
+
+int rtnl_link_ipgre_set_tos(struct rtnl_link *link, uint8_t tos)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->tos = tos;
+        ipgre->ipgre_mask |= IPGRE_ATTR_TOS;
+
+        return 0;
+}
+
+uint8_t rtnl_link_get_tos(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->tos;
+}
+
+int rtnl_link_ipgre_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        ipgre->pmtudisc = pmtudisc;
+        ipgre->ipgre_mask |= IPGRE_ATTR_PMTUDISC;
+
+        return 0;
+}
+
+uint8_t rtnl_link_get_pmtudisc(struct rtnl_link *link)
+{
+        struct ipgre_info *ipgre = link->l_info;
+
+        IS_IPGRE_LINK_ASSERT(link);
+
+        return ipgre->pmtudisc;
+}
+
+static void __init ipgre_init(void)
+{
+        rtnl_link_register_info(&ipgre_info_ops);
+}
+
+static void __exit ipgre_exit(void)
+{
+        rtnl_link_unregister_info(&ipgre_info_ops);
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f5f2c26..8cf0a06 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -24,6 +24,7 @@ check_PROGRAMS = \
 	test-create-vxlan \
 	test-create-veth \
 	test-create-bridge \
+	test-create-ip_gre \
 	test-delete-link \
 	test-socket-creation \
 	test-complex-HTB-with-hash-filters \
diff --git a/tests/test-create-gre.c b/tests/test-create-gre.c
new file mode 100644
index 0000000..64f0ae5
--- /dev/null
+++ b/tests/test-create-gre.c
@@ -0,0 +1,58 @@
+#include <libnl3/netlink/netlink.h>
+#include <libnl3/netlink/route/link.h>
+#include <libnl3/netlink/route/link/ipgre.h>
+#include <linux/netlink.h>
+
+int main(int argc, char *argv[])
+{
+        struct in_addr local, remote;
+        struct rtnl_link *link;
+        struct nl_cache *link_cache;
+        struct nl_sock *sk;
+        int err, if_index;
+
+        sk = nl_socket_alloc();
+        if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+                nl_perror(err, "Unable to connect socket");
+                return err;
+        }
+
+        err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+        if ( err < 0) {
+                nl_perror(err, "Unable to allocate cache");
+                return err;
+        }
+
+        if_index = rtnl_link_name2i(link_cache, "eno16777736");
+        if (!if_index) {
+                fprintf(stderr, "Unable to lookup eno16777736");
+                return -1;
+        }
+
+        link = rtnl_link_ipgre_alloc();
+        if(!link) {
+                nl_perror(err, "Unable to allocate link");
+                return -1;
+
+        }
+        rtnl_link_set_name(link, "ipgre-tun");
+        rtnl_link_ipgre_set_link(link, if_index);
+
+        inet_pton(AF_INET, "192.168.254.12", &local.s_addr);
+        rtnl_link_ipgre_set_local(link, local.s_addr);
+
+        inet_pton(AF_INET, "192.168.254.13", &remote.s_addr);
+        rtnl_link_ipgre_set_remote(link, remote.s_addr);
+        
+        rtnl_link_ipgre_set_ttl(link, 64);
+        err = rtnl_link_add(sk, link, NLM_F_CREATE);
+        if (err < 0) {
+                nl_perror(err, "Unable to add link");
+                return err;
+        }
+
+        rtnl_link_put(link);
+        nl_close(sk);
+
+        return 0;
+}
-- 
1.9.0




More information about the libnl mailing list