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