]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-netlink-manual.c
f03fcffc6246101b40b0748655d6271029bafe7e
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* Make sure the net/if.h header is included before any linux/ one */
4 #include <net/if.h>
5 #include <arpa/inet.h>
6 #include <linux/if_tunnel.h>
7 #include <linux/ip.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
17 static int load_module(const char *mod_name) {
18 _cleanup_(sym_kmod_unrefp) struct kmod_ctx *ctx = NULL;
19 _cleanup_(sym_kmod_module_unref_listp) struct kmod_list *list = NULL;
20 struct kmod_list *l;
21 int r;
22
23 r = dlopen_libkmod();
24 if (r < 0)
25 return log_error_errno(r, "Failed to load libkmod: %m");
26
27 ctx = sym_kmod_new(NULL, NULL);
28 if (!ctx)
29 return log_oom();
30
31 r = sym_kmod_module_new_from_lookup(ctx, mod_name, &list);
32 if (r < 0)
33 return r;
34
35 sym_kmod_list_foreach(l, list) {
36 _cleanup_(sym_kmod_module_unrefp) struct kmod_module *mod = NULL;
37
38 mod = sym_kmod_module_get_module(l);
39
40 r = sym_kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
41 if (r > 0)
42 r = -EINVAL;
43 }
44
45 return r;
46 }
47
48 static int test_tunnel_configure(sd_netlink *rtnl) {
49 int r;
50 sd_netlink_message *m, *n;
51 struct in_addr local, remote;
52
53 /* skip test if module cannot be loaded */
54 r = load_module("ipip");
55 if (r < 0)
56 return log_tests_skipped_errno(r, "failed to load module 'ipip'");
57
58 r = load_module("sit");
59 if (r < 0)
60 return log_tests_skipped_errno(r, "failed to load module 'sit'");
61
62 if (getuid() != 0)
63 return log_tests_skipped("not root");
64
65 /* IPIP tunnel */
66 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
67 assert_se(m);
68
69 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
70 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
71
72 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
73
74 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
75
76 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
77 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
78
79 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
80 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
81
82 assert_se(sd_netlink_message_close_container(m) >= 0);
83 assert_se(sd_netlink_message_close_container(m) >= 0);
84
85 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
86
87 assert_se((m = sd_netlink_message_unref(m)) == NULL);
88
89 /* sit */
90 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
91 assert_se(n);
92
93 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
94 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
95
96 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
97
98 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
99
100 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
101
102 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
103 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
104
105 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
106 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
107
108 assert_se(sd_netlink_message_close_container(n) >= 0);
109 assert_se(sd_netlink_message_close_container(n) >= 0);
110
111 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
112
113 assert_se((n = sd_netlink_message_unref(n)) == NULL);
114
115 return EXIT_SUCCESS;
116 }
117
118 int main(int argc, char *argv[]) {
119 sd_netlink *rtnl;
120 int r;
121
122 test_setup_logging(LOG_INFO);
123
124 assert_se(sd_netlink_open(&rtnl) >= 0);
125 assert_se(rtnl);
126
127 r = test_tunnel_configure(rtnl);
128
129 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
130
131 return r;
132 }