]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-netlink-manual.c
util-lib: introdude _cleanup_ macros for kmod objects
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
CommitLineData
9a6704a8
SS
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Susant Sahani
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
9a6704a8 20#include <arpa/inet.h>
07630cea 21#include <libkmod.h>
9a6704a8 22#include <linux/ip.h>
07630cea 23#include <net/if.h>
9a6704a8 24#include <linux/if_tunnel.h>
9a6704a8 25
1c4baffc 26#include "sd-netlink.h"
9a6704a8 27
07630cea 28#include "macro.h"
232ac0d6 29#include "module-util.h"
07630cea
LP
30#include "util.h"
31
9a6704a8 32static int load_module(const char *mod_name) {
232ac0d6
ZJS
33 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
34 _cleanup_(kmod_module_unref_listp) struct kmod_list *list = NULL;
35 struct kmod_list *l;
9a6704a8
SS
36 int r;
37
38 ctx = kmod_new(NULL, NULL);
232ac0d6
ZJS
39 if (!ctx)
40 return log_oom();
9a6704a8
SS
41
42 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
43 if (r < 0)
44 return -1;
45
46 kmod_list_foreach(l, list) {
232ac0d6
ZJS
47 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
48
49 mod = kmod_module_get_module(l);
9a6704a8
SS
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;
9a6704a8
SS
56 }
57
9a6704a8
SS
58 return r;
59}
60
1c4baffc 61static int test_tunnel_configure(sd_netlink *rtnl) {
9a6704a8 62 int r;
1c4baffc 63 sd_netlink_message *m, *n;
9a6704a8
SS
64 struct in_addr local, remote;
65
66 /* skip test if module cannot be loaded */
67 r = load_module("ipip");
9ed794a3 68 if (r < 0)
9a6704a8
SS
69 return EXIT_TEST_SKIP;
70
9ed794a3 71 if (getuid() != 0)
9a6704a8
SS
72 return EXIT_TEST_SKIP;
73
74 /* IPIP tunnel */
75 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
76 assert_se(m);
77
1c4baffc
TG
78 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
79 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
9a6704a8 80
1c4baffc 81 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
9a6704a8 82
1c4baffc 83 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
9a6704a8
SS
84
85 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
1c4baffc 86 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
9a6704a8
SS
87
88 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
1c4baffc 89 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
9a6704a8 90
1c4baffc
TG
91 assert_se(sd_netlink_message_close_container(m) >= 0);
92 assert_se(sd_netlink_message_close_container(m) >= 0);
9a6704a8 93
1c4baffc 94 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
9a6704a8 95
1c4baffc 96 assert_se((m = sd_netlink_message_unref(m)) == NULL);
9a6704a8
SS
97
98 r = load_module("sit");
9ed794a3 99 if (r < 0)
9a6704a8
SS
100 return EXIT_TEST_SKIP;
101
102 /* sit */
103 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
104 assert_se(n);
105
1c4baffc
TG
106 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
107 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
9a6704a8 108
1c4baffc 109 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
9a6704a8 110
1c4baffc 111 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
9a6704a8 112
1c4baffc 113 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
9a6704a8
SS
114
115 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
1c4baffc 116 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
9a6704a8
SS
117
118 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
1c4baffc 119 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
9a6704a8 120
1c4baffc
TG
121 assert_se(sd_netlink_message_close_container(n) >= 0);
122 assert_se(sd_netlink_message_close_container(n) >= 0);
9a6704a8 123
1c4baffc 124 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
9a6704a8 125
19fcba36 126 assert_se((n = sd_netlink_message_unref(n)) == NULL);
9a6704a8
SS
127
128 return EXIT_SUCCESS;
129}
130
131int main(int argc, char *argv[]) {
1c4baffc 132 sd_netlink *rtnl;
9a6704a8
SS
133 int r;
134
1c4baffc 135 assert_se(sd_netlink_open(&rtnl) >= 0);
9a6704a8
SS
136 assert_se(rtnl);
137
138 r = test_tunnel_configure(rtnl);
139
1c4baffc 140 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
9a6704a8
SS
141
142 return r;
143}