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