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