]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-netlink-manual.c
tree-wide: drop several missing_*.h and import relevant headers from kernel-5.0
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <arpa/inet.h>
4 #include <libkmod.h>
5 #include <linux/if_tunnel.h>
6 #include <linux/ip.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include "sd-netlink.h"
11
12 #include "macro.h"
13 #include "module-util.h"
14 #include "tests.h"
15 #include "util.h"
16
17 static int load_module(const char *mod_name) {
18 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
19 _cleanup_(kmod_module_unref_listp) struct kmod_list *list = NULL;
20 struct kmod_list *l;
21 int r;
22
23 ctx = kmod_new(NULL, NULL);
24 if (!ctx)
25 return log_oom();
26
27 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
28 if (r < 0)
29 return r;
30
31 kmod_list_foreach(l, list) {
32 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
33
34 mod = kmod_module_get_module(l);
35
36 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
37 if (r > 0)
38 r = -EINVAL;
39 }
40
41 return r;
42 }
43
44 static int test_tunnel_configure(sd_netlink *rtnl) {
45 int r;
46 sd_netlink_message *m, *n;
47 struct in_addr local, remote;
48
49 /* skip test if module cannot be loaded */
50 r = load_module("ipip");
51 if (r < 0)
52 return log_tests_skipped_errno(r, "failed to load module 'ipip'");
53
54 r = load_module("sit");
55 if (r < 0)
56 return log_tests_skipped_errno(r, "failed to load module 'sit'");
57
58 if (getuid() != 0)
59 return log_tests_skipped("not root");
60
61 /* IPIP tunnel */
62 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
63 assert_se(m);
64
65 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
66 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
67
68 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
69
70 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
71
72 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
73 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
74
75 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
76 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
77
78 assert_se(sd_netlink_message_close_container(m) >= 0);
79 assert_se(sd_netlink_message_close_container(m) >= 0);
80
81 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
82
83 assert_se((m = sd_netlink_message_unref(m)) == NULL);
84
85 /* sit */
86 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
87 assert_se(n);
88
89 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
90 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
91
92 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
93
94 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
95
96 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
97
98 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
99 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
100
101 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
102 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
103
104 assert_se(sd_netlink_message_close_container(n) >= 0);
105 assert_se(sd_netlink_message_close_container(n) >= 0);
106
107 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
108
109 assert_se((n = sd_netlink_message_unref(n)) == NULL);
110
111 return EXIT_SUCCESS;
112 }
113
114 int main(int argc, char *argv[]) {
115 sd_netlink *rtnl;
116 int r;
117
118 test_setup_logging(LOG_INFO);
119
120 assert_se(sd_netlink_open(&rtnl) >= 0);
121 assert_se(rtnl);
122
123 r = test_tunnel_configure(rtnl);
124
125 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
126
127 return r;
128 }