1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Susant Sahani
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
23 #include <arpa/inet.h>
26 #include <linux/if_tunnel.h>
32 #include "socket-util.h"
33 #include "rtnl-util.h"
34 #include "event-util.h"
35 #include "rtnl-internal.h"
37 static int load_module(const char *mod_name) {
39 struct kmod_list *list = NULL, *l;
42 ctx = kmod_new(NULL, NULL);
48 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
52 kmod_list_foreach(l, list) {
53 struct kmod_module *mod = kmod_module_get_module(l);
55 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
61 kmod_module_unref(mod);
64 kmod_module_unref_list(list);
70 static int test_tunnel_configure(sd_rtnl *rtnl) {
72 sd_rtnl_message *m, *n;
73 struct in_addr local, remote;
75 /* skip test if module cannot be loaded */
76 r = load_module("ipip");
78 return EXIT_TEST_SKIP;
81 return EXIT_TEST_SKIP;
84 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
87 assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "eth0") >= 0);
88 assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, 1234)>= 0);
90 assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
91 assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "ipip") >= 0);
93 assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0);
95 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
96 assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
98 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
99 assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
101 assert_se(sd_rtnl_message_close_container(m) >= 0);
102 assert_se(sd_rtnl_message_close_container(m) >= 0);
104 assert_se(sd_rtnl_call(rtnl, m, -1, 0) == 1);
106 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
108 r = load_module("sit");
110 return EXIT_TEST_SKIP;
113 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
116 assert_se(sd_rtnl_message_append_string(n, IFLA_IFNAME, "eth1") >= 0);
117 assert_se(sd_rtnl_message_append_u32(n, IFLA_MTU, 1234)>= 0);
119 assert_se(sd_rtnl_message_open_container(n, IFLA_LINKINFO) >= 0);
120 assert_se(sd_rtnl_message_append_string(n, IFLA_INFO_KIND, "sit") >= 0);
122 assert_se(sd_rtnl_message_open_container(n, IFLA_INFO_DATA) >= 0);
124 assert_se(sd_rtnl_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
126 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
127 assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
129 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
130 assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
132 assert_se(sd_rtnl_message_close_container(n) >= 0);
133 assert_se(sd_rtnl_message_close_container(n) >= 0);
135 assert_se(sd_rtnl_call(rtnl, n, -1, 0) == 1);
137 assert_se((m = sd_rtnl_message_unref(n)) == NULL);
142 int main(int argc, char *argv[]) {
146 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
149 r = test_tunnel_configure(rtnl);
151 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);