]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-netlink-manual.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
CommitLineData
9a6704a8
SS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Susant Sahani
7
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.
12
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.
17
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/>.
20***/
21
9a6704a8 22#include <arpa/inet.h>
07630cea 23#include <libkmod.h>
9a6704a8 24#include <linux/ip.h>
07630cea 25#include <net/if.h>
9a6704a8 26#include <linux/if_tunnel.h>
9a6704a8 27
1c4baffc 28#include "sd-netlink.h"
9a6704a8 29
07630cea
LP
30#include "macro.h"
31#include "util.h"
32
9a6704a8
SS
33static int load_module(const char *mod_name) {
34 struct kmod_ctx *ctx;
35 struct kmod_list *list = NULL, *l;
36 int r;
37
38 ctx = kmod_new(NULL, NULL);
39 if (!ctx) {
40 kmod_unref(ctx);
41 return -ENOMEM;
42 }
43
44 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
45 if (r < 0)
46 return -1;
47
48 kmod_list_foreach(l, list) {
49 struct kmod_module *mod = kmod_module_get_module(l);
50
51 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
52 if (r >= 0)
53 r = 0;
54 else
55 r = -1;
56
57 kmod_module_unref(mod);
58 }
59
60 kmod_module_unref_list(list);
61 kmod_unref(ctx);
62
63 return r;
64}
65
1c4baffc 66static int test_tunnel_configure(sd_netlink *rtnl) {
9a6704a8 67 int r;
1c4baffc 68 sd_netlink_message *m, *n;
9a6704a8
SS
69 struct in_addr local, remote;
70
71 /* skip test if module cannot be loaded */
72 r = load_module("ipip");
73 if(r < 0)
74 return EXIT_TEST_SKIP;
75
76 if(getuid() != 0)
77 return EXIT_TEST_SKIP;
78
79 /* IPIP tunnel */
80 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
81 assert_se(m);
82
1c4baffc
TG
83 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
84 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
9a6704a8 85
1c4baffc 86 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
9a6704a8 87
1c4baffc 88 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
9a6704a8
SS
89
90 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
1c4baffc 91 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
9a6704a8
SS
92
93 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
1c4baffc 94 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
9a6704a8 95
1c4baffc
TG
96 assert_se(sd_netlink_message_close_container(m) >= 0);
97 assert_se(sd_netlink_message_close_container(m) >= 0);
9a6704a8 98
1c4baffc 99 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
9a6704a8 100
1c4baffc 101 assert_se((m = sd_netlink_message_unref(m)) == NULL);
9a6704a8
SS
102
103 r = load_module("sit");
104 if(r < 0)
105 return EXIT_TEST_SKIP;
106
107 /* sit */
108 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
109 assert_se(n);
110
1c4baffc
TG
111 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
112 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
9a6704a8 113
1c4baffc 114 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
9a6704a8 115
1c4baffc 116 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
9a6704a8 117
1c4baffc 118 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
9a6704a8
SS
119
120 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
1c4baffc 121 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
9a6704a8
SS
122
123 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
1c4baffc 124 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
9a6704a8 125
1c4baffc
TG
126 assert_se(sd_netlink_message_close_container(n) >= 0);
127 assert_se(sd_netlink_message_close_container(n) >= 0);
9a6704a8 128
1c4baffc 129 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
9a6704a8 130
19fcba36 131 assert_se((n = sd_netlink_message_unref(n)) == NULL);
9a6704a8
SS
132
133 return EXIT_SUCCESS;
134}
135
136int main(int argc, char *argv[]) {
1c4baffc 137 sd_netlink *rtnl;
9a6704a8
SS
138 int r;
139
1c4baffc 140 assert_se(sd_netlink_open(&rtnl) >= 0);
9a6704a8
SS
141 assert_se(rtnl);
142
143 r = test_tunnel_configure(rtnl);
144
1c4baffc 145 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
9a6704a8
SS
146
147 return r;
148}