From 8f6301426a00d992d00238c7ddd666e96aa63e28 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Tue, 6 May 2014 11:11:30 +0530 Subject: [PATCH] ipvti: introduce vti tunnel support Signed-off-by: Susant Sahani [thaller@redhat.com: some whitespace fixes] Signed-off-by: Thomas Haller --- doc/route.txt | 60 ++++ include/Makefile.am | 1 + include/netlink/route/link/ipvti.h | 43 +++ lib/Makefile.am | 2 +- lib/route/link/ipvti.c | 477 +++++++++++++++++++++++++++++ tests/Makefile.am | 1 + tests/test-create-ipvti.c | 55 ++++ 7 files changed, 638 insertions(+), 1 deletion(-) create mode 100644 include/netlink/route/link/ipvti.h create mode 100644 lib/route/link/ipvti.c create mode 100644 tests/test-create-ipvti.c diff --git a/doc/route.txt b/doc/route.txt index f70ad87..d87e79f 100644 --- a/doc/route.txt +++ b/doc/route.txt @@ -1081,6 +1081,66 @@ rtnl_link_put(link); ----- +[[link_ipvti]] +==== IPVTI + +[source,c] +----- +extern struct rtnl_link *rtnl_link_ipvti_alloc(void); +extern int rtnl_link_ipvti_add(struct nl_sock *sk, const char *name); + +extern int rtnl_link_ipvti_set_link(struct rtnl_link *link, uint32_t index); +extern uint32_t rtnl_link_ipvti_get_link(struct rtnl_link *link); + +extern int rtnl_link_ipvti_set_ikey(struct rtnl_link *link, uint32_t ikey); +extern uint32_t rtnl_link_get_ikey(struct rtnl_link *link); + +extern int rtnl_link_ipvti_set_okey(struct rtnl_link *link, uint32_t okey); +extern uint32_t rtnl_link_get_okey(struct rtnl_link *link) + +extern int rtnl_link_ipvti_set_local(struct rtnl_link *link, uint32_t addr); +extern uint32_t rtnl_link_ipvti_get_local(struct rtnl_link *link); + +extern int rtnl_link_ipvti_set_remote(struct rtnl_link *link, uint32_t addr); +extern uint32_t rtnl_link_ipvti_get_remote(struct rtnl_link *link); + +----- + +.Example: Add a ipvti tunnel device +[source,c] +----- +struct rtnl_link *link +struct in_addr addr + +/* allocate new link object of type vxlan */ +if(!(link = rtnl_link_ipvti_alloc())) + /* error */ + +/* set ipvti tunnel name */ +if ((err = rtnl_link_set_name(link, "ipvti-tun")) < 0) + /* error */ + +/* set link index */ +if ((err = rtnl_link_ipvti_set_link(link, if_index)) < 0) + /* error */ + +/* set local address */ +inet_pton(AF_INET, "192.168.254.12", &addr.s_addr); +if ((err = rtnl_link_ipvti_set_local(link, addr.s_addr)) < 0) + /* error */ + +/* set remote address */ +inet_pton(AF_INET, "192.168.254.13", &addr.s_addr +if ((err = rtnl_link_ipvti_set_remote(link, addr.s_addr)) < 0) + /* error */ + +if((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) + /* error */ + +rtnl_link_put(link); +----- + + == Neighbouring == Routing diff --git a/include/Makefile.am b/include/Makefile.am index f957d1d..a27857c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -55,6 +55,7 @@ nobase_libnlinclude_HEADERS = \ netlink/route/link/veth.h \ netlink/route/link/ipgre.h \ netlink/route/link/ipip.h \ + netlink/route/link/ipvti.h \ netlink/route/link/sit.h \ netlink/route/qdisc/cbq.h \ netlink/route/qdisc/dsmark.h \ diff --git a/include/netlink/route/link/ipvti.h b/include/netlink/route/link/ipvti.h new file mode 100644 index 0000000..a3e7bba --- /dev/null +++ b/include/netlink/route/link/ipvti.h @@ -0,0 +1,43 @@ +/* + * netlink/route/link/ipvti.h IPVTI 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 + */ + +#ifndef NETLINK_LINK_IPVTI_H_ +#define NETLINK_LINK_IPVTI_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + extern struct rtnl_link *rtnl_link_ipvti_alloc(void); + extern int rtnl_link_ipvti_add(struct nl_sock *sk, const char *name); + + extern int rtnl_link_ipvti_set_link(struct rtnl_link *link, uint32_t index); + extern uint32_t rtnl_link_ipvti_get_link(struct rtnl_link *link); + + extern int rtnl_link_ipvti_set_ikey(struct rtnl_link *link, uint32_t ikey); + extern uint32_t rtnl_link_get_ikey(struct rtnl_link *link); + + extern int rtnl_link_ipvti_set_okey(struct rtnl_link *link, uint32_t okey); + extern uint32_t rtnl_link_get_okey(struct rtnl_link *link); + + extern int rtnl_link_ipvti_set_local(struct rtnl_link *link, uint32_t addr); + extern uint32_t rtnl_link_get_local(struct rtnl_link *link); + + extern int rtnl_link_ipvti_set_remote(struct rtnl_link *link, uint32_t addr); + extern uint32_t rtnl_link_get_remote(struct rtnl_link *link); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/Makefile.am b/lib/Makefile.am index ae35a96..b2d142c 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -79,7 +79,7 @@ libnl_route_3_la_SOURCES = \ 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/ipip.c \ - route/link/ipgre.c route/link/sit.c\ + route/link/ipgre.c route/link/sit.c route/link/ipvti.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/ipvti.c b/lib/route/link/ipvti.c new file mode 100644 index 0000000..71f61c3 --- /dev/null +++ b/lib/route/link/ipvti.c @@ -0,0 +1,477 @@ + /* + * lib/route/link/ipvti.c IPVTI 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 + */ + +/** + * @ingroup link + * @defgroup ipvti IPVTI + * ipvti link module + * + * @details + * \b Link Type Name: "ipvti" + * + * @route_doc{link_ipvti, IPVTI Documentation} + * + * @{ + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define IPVTI_ATTR_LINK (1 << 0) +#define IPVTI_ATTR_IKEY (1 << 1) +#define IPVTI_ATTR_OKEY (1 << 2) +#define IPVTI_ATTR_LOCAL (1 << 3) +#define IPVTI_ATTR_REMOTE (1 << 4) + +struct ipvti_info +{ + uint32_t link; + uint32_t ikey; + uint32_t okey; + uint32_t local; + uint32_t remote; + uint32_t ipvti_mask; +}; + +static struct nla_policy ipvti_policy[IFLA_GRE_MAX + 1] = { + [IFLA_VTI_LINK] = { .type = NLA_U32 }, + [IFLA_VTI_IKEY] = { .type = NLA_U32 }, + [IFLA_VTI_OKEY] = { .type = NLA_U32 }, + [IFLA_VTI_LOCAL] = { .type = NLA_U32 }, + [IFLA_VTI_REMOTE] = { .type = NLA_U32 }, +}; + +static int ipvti_alloc(struct rtnl_link *link) +{ + struct ipvti_info *ipvti; + + ipvti = calloc(1, sizeof(*ipvti)); + if (!ipvti) + return -NLE_NOMEM; + + link->l_info = ipvti; + + return 0; +} + +static int ipvti_parse(struct rtnl_link *link, struct nlattr *data, + struct nlattr *xstats) +{ + struct nlattr *tb[IFLA_IPTUN_MAX + 1]; + struct ipvti_info *ipvti; + int err; + + NL_DBG(3, "Parsing IPVTI link info"); + + err = nla_parse_nested(tb, IFLA_GRE_MAX, data, ipvti_policy); + if (err < 0) + goto errout; + + err = ipvti_alloc(link); + if (err < 0) + goto errout; + + ipvti = link->l_info; + + if (tb[IFLA_VTI_LINK]) { + ipvti->link = nla_get_u32(tb[IFLA_VTI_LINK]); + ipvti->ipvti_mask |= IPVTI_ATTR_LINK; + } + + if (tb[IFLA_VTI_IKEY]) { + ipvti->ikey = nla_get_u32(tb[IFLA_VTI_IKEY]); + ipvti->ipvti_mask |= IPVTI_ATTR_IKEY; + } + + if (tb[IFLA_VTI_OKEY]) { + ipvti->okey = nla_get_u32(tb[IFLA_VTI_OKEY]); + ipvti->ipvti_mask |= IPVTI_ATTR_OKEY; + } + + if (tb[IFLA_VTI_LOCAL]) { + ipvti->local = nla_get_u32(tb[IFLA_VTI_LOCAL]); + ipvti->ipvti_mask |= IPVTI_ATTR_LOCAL; + } + + if (tb[IFLA_VTI_REMOTE]) { + ipvti->remote = nla_get_u32(tb[IFLA_VTI_REMOTE]); + ipvti->ipvti_mask |= IPVTI_ATTR_REMOTE; + } + + err = 0; + + errout: + return err; +} + +static int ipvti_put_attrs(struct nl_msg *msg, struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + struct nlattr *data; + + data = nla_nest_start(msg, IFLA_INFO_DATA); + if (!data) + return -NLE_MSGSIZE; + + if (ipvti->ipvti_mask & IPVTI_ATTR_LINK) + NLA_PUT_U32(msg, IFLA_VTI_LINK, ipvti->link); + + if (ipvti->ipvti_mask & IPVTI_ATTR_IKEY) + NLA_PUT_U32(msg, IFLA_VTI_IKEY, ipvti->ikey); + + if (ipvti->ipvti_mask & IFLA_VTI_IKEY) + NLA_PUT_U32(msg, IFLA_VTI_OKEY, ipvti->okey); + + if (ipvti->ipvti_mask & IPVTI_ATTR_LOCAL) + NLA_PUT_U32(msg, IFLA_VTI_LOCAL, ipvti->local); + + if (ipvti->ipvti_mask & IPVTI_ATTR_REMOTE) + NLA_PUT_U32(msg, IFLA_VTI_REMOTE, ipvti->remote); + + nla_nest_end(msg, data); + +nla_put_failure: + + return 0; +} + +static void ipvti_free(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + free(ipvti); + link->l_info = NULL; +} + +static void ipvti_dump_line(struct rtnl_link *link, struct nl_dump_params *p) +{ + nl_dump(p, "ipvti : %s", link->l_name); +} + +static void ipvti_dump_details(struct rtnl_link *link, struct nl_dump_params *p) +{ + struct ipvti_info *ipvti = link->l_info; + char *name, addr[INET_ADDRSTRLEN]; + + if (ipvti->ipvti_mask & IPVTI_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", ipvti->link); + } + + if (ipvti->ipvti_mask & IPVTI_ATTR_IKEY) { + nl_dump(p, " ikey "); + nl_dump_line(p, "%x\n",ipvti->ikey); + } + + if (ipvti->ipvti_mask & IPVTI_ATTR_OKEY) { + nl_dump(p, " okey "); + nl_dump_line(p, "%x\n", ipvti->okey); + } + + if (ipvti->ipvti_mask & IPVTI_ATTR_LOCAL) { + nl_dump(p, " local "); + if(inet_ntop(AF_INET, &ipvti->local, addr, sizeof(addr))) + nl_dump_line(p, "%s\n", addr); + else + nl_dump_line(p, "%#x\n", ntohs(ipvti->local)); + } + + if (ipvti->ipvti_mask & IPVTI_ATTR_REMOTE) { + nl_dump(p, " remote "); + if(inet_ntop(AF_INET, &ipvti->remote, addr, sizeof(addr))) + nl_dump_line(p, "%s\n", addr); + else + nl_dump_line(p, "%#x\n", ntohs(ipvti->remote)); + } +} + +static int ipvti_clone(struct rtnl_link *dst, struct rtnl_link *src) +{ + struct ipvti_info *ipvti_dst, *ipvti_src = src->l_info; + int err; + + dst->l_info = NULL; + + err = rtnl_link_set_type(dst, "vti"); + if (err < 0) + return err; + + ipvti_dst = dst->l_info; + + if (!ipvti_dst || !ipvti_src) + BUG(); + + memcpy(ipvti_dst, ipvti_src, sizeof(struct ipvti_info)); + + return 0; +} + +static struct rtnl_link_info_ops ipvti_info_ops = { + .io_name = "vti", + .io_alloc = ipvti_alloc, + .io_parse = ipvti_parse, + .io_dump = { + [NL_DUMP_LINE] = ipvti_dump_line, + [NL_DUMP_DETAILS] = ipvti_dump_details, + }, + .io_clone = ipvti_clone, + .io_put_attrs = ipvti_put_attrs, + .io_free = ipvti_free, +}; + +#define IS_IPVTI_LINK_ASSERT(link) \ + if ((link)->l_info_ops != &ipvti_info_ops) { \ + APPBUG("Link is not a ipvti link. set type \vti\" first."); \ + return -NLE_OPNOTSUPP; \ + } + +struct rtnl_link *rtnl_link_ipvti_alloc(void) +{ + struct rtnl_link *link; + int err; + + link = rtnl_link_alloc(); + if (!link) + return NULL; + + err = rtnl_link_set_type(link, "vti"); + if (err < 0) { + rtnl_link_put(link); + return NULL; + } + + return link; +} + +/** + * Check if link is a IPVTI link + * @arg link Link object + * + * @return True if link is a IPVTI link, otherwise 0 is returned. + */ +int rtnl_link_is_ipvti(struct rtnl_link *link) +{ + return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vti"); +} +/** + * Create a new ipvti tunnel device + * @arg sock netlink socket + * @arg name name of the tunnel deviceL + * + * Creates a new ipvti tunnel device in the kernel + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_add(struct nl_sock *sk, const char *name) +{ + struct rtnl_link *link; + int err; + + link = rtnl_link_ipvti_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; +} +/** + * Set IPVTI tunnel interface index + * @arg link Link object + * @arg index interface index + * + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_set_link(struct rtnl_link *link, uint32_t index) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + ipvti->link = index; + ipvti->ipvti_mask |= IPVTI_ATTR_LINK; + + return 0; +} + +/** + * Get IPVTI tunnel interface index + * @arg link Link object + * + * @return interface index + */ +uint32_t rtnl_link_ipvti_get_link(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + return ipvti->link; +} + +/** + * Set IPVTI tunnel set ikey + * @arg link Link object + * @arg ikey gre ikey + * + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_set_ikey(struct rtnl_link *link, uint32_t ikey) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + ipvti->ikey = ikey; + ipvti->ipvti_mask |= IPVTI_ATTR_IKEY; + + return 0; +} + +/** + * Get IPVTI tunnel ikey + * @arg link Link object + * + * @return ikey + */ +uint32_t rtnl_link_ipvti_get_ikey(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + return ipvti->ikey; +} + +/** + * Set IPVTI tunnel set okey + * @arg link Link object + * @arg okey gre okey + * + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_set_okey(struct rtnl_link *link, uint32_t okey) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + ipvti->okey = okey; + ipvti->ipvti_mask |= IPVTI_ATTR_OKEY; + + return 0; +} + +/** + * Get IPVTI tunnel okey + * @arg link Link object + * + * @return okey value + */ +uint32_t rtnl_link_ipvti_get_okey(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + return ipvti->okey; +} + +/** + * Set IPVTI tunnel local address + * @arg link Link object + * @arg addr local address + * + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_set_local(struct rtnl_link *link, uint32_t addr) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + ipvti->local = addr; + ipvti->ipvti_mask |= IPVTI_ATTR_LOCAL; + + return 0; +} + +/** + * Get IPVTI tunnel local address + * @arg link Link object + * + * @return local address + */ +uint32_t rtnl_link_ipvti_get_local(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + return ipvti->local; +} + +/** + * Set IPVTI tunnel remote address + * @arg link Link object + * @arg remote remote address + * + * @return 0 on success or a negative error code + */ +int rtnl_link_ipvti_set_remote(struct rtnl_link *link, uint32_t remote) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + ipvti->remote = remote; + ipvti->ipvti_mask |= IPVTI_ATTR_REMOTE; + + return 0; +} + +/** + * Get IPVTI tunnel remote address + * @arg link Link object + * + * @return remote address on success or a negative error code + */ +uint32_t rtnl_link_ipvti_get_remote(struct rtnl_link *link) +{ + struct ipvti_info *ipvti = link->l_info; + + IS_IPVTI_LINK_ASSERT(link); + + return ipvti->remote; +} + +static void __init ipvti_init(void) +{ + rtnl_link_register_info(&ipvti_info_ops); +} + +static void __exit ipvti_exit(void) +{ + rtnl_link_unregister_info(&ipvti_info_ops); +} diff --git a/tests/Makefile.am b/tests/Makefile.am index 584ab60..acc4f93 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -26,6 +26,7 @@ check_PROGRAMS = \ test-create-bridge \ test-create-ipgre \ test-create-ipip \ + test-create-ipvti \ test-create-sit \ test-delete-link \ test-socket-creation \ diff --git a/tests/test-create-ipvti.c b/tests/test-create-ipvti.c new file mode 100644 index 0000000..6cb92d7 --- /dev/null +++ b/tests/test-create-ipvti.c @@ -0,0 +1,55 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + struct nl_cache *link_cache; + struct rtnl_link *link; + struct in_addr addr; + 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, "ens33"); + if (!if_index) { + fprintf(stderr, "Unable to lookup ens33"); + return -1; + } + + link = rtnl_link_ipvti_alloc(); + if(!link) { + nl_perror(err, "Unable to allocate link"); + return -1; + + } + rtnl_link_set_name(link, "ipvti-tun"); + rtnl_link_ipvti_set_link(link, if_index); + + inet_pton(AF_INET, "192.168.254.12", &addr.s_addr); + rtnl_link_ipvti_set_local(link, addr.s_addr); + + inet_pton(AF_INET, "192.168.254.13", &addr.s_addr); + rtnl_link_ipvti_set_remote(link, addr.s_addr); + + 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; +} -- 2.40.0