summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSusant Sahani <susant@redhat.com>2014-03-25 14:13:30 +0530
committerTom Gundersen <teg@jklm.no>2014-03-25 14:44:33 +0100
commit9a6704a81fb5431f7b32dea267a837b1c2b86801 (patch)
tree464e289c1c713d0764f4602c2452351578b13ded /src
parent41ca2c206bb70168f662200784ded59d1af22044 (diff)
sd-rtnl: add support for tunnel attributes
Added support for tunneling netlink attrributes (ipip, gre, sit). These works with kernel module ipip, gre and sit . The test cases are moved to a separate file and manual test as well because they require respective kernel modules as well.
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd/sd-rtnl/rtnl-message.c37
-rw-r--r--src/test/test-rtnl-manual.c154
2 files changed, 187 insertions, 4 deletions
diff --git a/src/libsystemd/sd-rtnl/rtnl-message.c b/src/libsystemd/sd-rtnl/rtnl-message.c
index cf40572b81..84a8ffa59e 100644
--- a/src/libsystemd/sd-rtnl/rtnl-message.c
+++ b/src/libsystemd/sd-rtnl/rtnl-message.c
@@ -24,6 +24,9 @@
#include <stdbool.h>
#include <unistd.h>
#include <linux/veth.h>
+#include <linux/if.h>
+#include <linux/ip.h>
+#include <linux/if_tunnel.h>
#include <linux/if_bridge.h>
#include "util.h"
@@ -456,6 +459,12 @@ int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t d
case IFLA_CARRIER:
case IFLA_OPERSTATE:
case IFLA_LINKMODE:
+ case IFLA_IPTUN_TTL:
+ case IFLA_IPTUN_TOS:
+ case IFLA_IPTUN_PROTO:
+ case IFLA_IPTUN_PMTUDISC:
+ case IFLA_IPTUN_ENCAP_LIMIT:
+ case IFLA_GRE_TTL:
break;
default:
return -ENOTSUP;
@@ -493,12 +502,22 @@ int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t
case RTM_DELLINK:
if (m->n_containers == 2 &&
GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO &&
- GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA &&
- type == IFLA_VLAN_ID)
- break;
- else
+ GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA) {
+ switch (type) {
+ case IFLA_VLAN_ID:
+ case IFLA_IPTUN_FLAGS:
+ case IFLA_GRE_IFLAGS:
+ case IFLA_GRE_OFLAGS:
+ case IFLA_IPTUN_6RD_PREFIXLEN:
+ case IFLA_IPTUN_6RD_RELAY_PREFIXLEN:
+ break;
+ default:
+ return -ENOTSUP;
+ }
+ } else
return -ENOTSUP;
+ break;
default:
return -ENOTSUP;
}
@@ -539,7 +558,12 @@ int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t
case IFLA_PROMISCUITY:
case IFLA_NUM_TX_QUEUES:
case IFLA_NUM_RX_QUEUES:
+ case IFLA_IPTUN_LOCAL:
+ case IFLA_IPTUN_REMOTE:
case IFLA_MACVLAN_MODE:
+ case IFLA_IPTUN_FLAGS:
+ case IFLA_IPTUN_FLOWINFO:
+ case IFLA_GRE_FLOWINFO:
break;
default:
return -ENOTSUP;
@@ -594,6 +618,8 @@ int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, cons
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
+ case IFLA_GRE_LOCAL:
+ case IFLA_GRE_REMOTE:
ifa = NLMSG_DATA(m->hdr);
if (ifa->ifa_family != AF_INET)
@@ -656,6 +682,9 @@ int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, con
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
+ case IFLA_GRE_LOCAL:
+ case IFLA_GRE_REMOTE:
+ case IFLA_IPTUN_6RD_PREFIX:
ifa = NLMSG_DATA(m->hdr);
if (ifa->ifa_family != AF_INET6)
diff --git a/src/test/test-rtnl-manual.c b/src/test/test-rtnl-manual.c
new file mode 100644
index 0000000000..e76fb81a66
--- /dev/null
+++ b/src/test/test-rtnl-manual.c
@@ -0,0 +1,154 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Susant Sahani
+
+ systemd 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; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <netinet/ether.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <linux/ip.h>
+#include <linux/if_tunnel.h>
+#include <libkmod.h>
+
+#include "util.h"
+#include "macro.h"
+#include "sd-rtnl.h"
+#include "socket-util.h"
+#include "rtnl-util.h"
+#include "event-util.h"
+#include "rtnl-internal.h"
+
+static int load_module(const char *mod_name) {
+ struct kmod_ctx *ctx;
+ struct kmod_list *list = NULL, *l;
+ int r;
+
+ ctx = kmod_new(NULL, NULL);
+ if (!ctx) {
+ kmod_unref(ctx);
+ return -ENOMEM;
+ }
+
+ r = kmod_module_new_from_lookup(ctx, mod_name, &list);
+ if (r < 0)
+ return -1;
+
+ kmod_list_foreach(l, list) {
+ struct kmod_module *mod = kmod_module_get_module(l);
+
+ r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
+ if (r >= 0)
+ r = 0;
+ else
+ r = -1;
+
+ kmod_module_unref(mod);
+ }
+
+ kmod_module_unref_list(list);
+ kmod_unref(ctx);
+
+ return r;
+}
+
+static int test_tunnel_configure(sd_rtnl *rtnl) {
+ int r;
+ sd_rtnl_message *m, *n;
+ struct in_addr local, remote;
+
+ /* skip test if module cannot be loaded */
+ r = load_module("ipip");
+ if(r < 0)
+ return EXIT_TEST_SKIP;
+
+ if(getuid() != 0)
+ return EXIT_TEST_SKIP;
+
+ /* IPIP tunnel */
+ assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
+ assert_se(m);
+
+ assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "eth0") >= 0);
+ assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, 1234)>= 0);
+
+ assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
+ assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "ipip") >= 0);
+
+ assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0);
+
+ inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
+ assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
+
+ inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
+ assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
+
+ assert_se(sd_rtnl_message_close_container(m) >= 0);
+ assert_se(sd_rtnl_message_close_container(m) >= 0);
+
+ assert_se(sd_rtnl_call(rtnl, m, -1, 0) == 1);
+
+ assert_se((m = sd_rtnl_message_unref(m)) == NULL);
+
+ r = load_module("sit");
+ if(r < 0)
+ return EXIT_TEST_SKIP;
+
+ /* sit */
+ assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
+ assert_se(n);
+
+ assert_se(sd_rtnl_message_append_string(n, IFLA_IFNAME, "eth1") >= 0);
+ assert_se(sd_rtnl_message_append_u32(n, IFLA_MTU, 1234)>= 0);
+
+ assert_se(sd_rtnl_message_open_container(n, IFLA_LINKINFO) >= 0);
+ assert_se(sd_rtnl_message_append_string(n, IFLA_INFO_KIND, "sit") >= 0);
+
+ assert_se(sd_rtnl_message_open_container(n, IFLA_INFO_DATA) >= 0);
+
+ assert_se(sd_rtnl_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
+
+ inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
+ assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
+
+ inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
+ assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
+
+ assert_se(sd_rtnl_message_close_container(n) >= 0);
+ assert_se(sd_rtnl_message_close_container(n) >= 0);
+
+ assert_se(sd_rtnl_call(rtnl, n, -1, 0) == 1);
+
+ assert_se((m = sd_rtnl_message_unref(n)) == NULL);
+
+ return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[]) {
+ sd_rtnl *rtnl;
+ int r;
+
+ assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
+ assert_se(rtnl);
+
+ r = test_tunnel_configure(rtnl);
+
+ assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
+
+ return r;
+}