]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-netlink-manual.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <linux/if_tunnel.h>
11 #include "sd-netlink.h"
14 #include "module-util.h"
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
;
24 ctx
= kmod_new(NULL
, NULL
);
28 r
= kmod_module_new_from_lookup(ctx
, mod_name
, &list
);
32 kmod_list_foreach(l
, list
) {
33 _cleanup_(kmod_module_unrefp
) struct kmod_module
*mod
= NULL
;
35 mod
= kmod_module_get_module(l
);
37 r
= kmod_module_probe_insert_module(mod
, 0, NULL
, NULL
, NULL
, NULL
);
45 static int test_tunnel_configure(sd_netlink
*rtnl
) {
47 sd_netlink_message
*m
, *n
;
48 struct in_addr local
, remote
;
50 /* skip test if module cannot be loaded */
51 r
= load_module("ipip");
53 return log_tests_skipped_errno(r
, "failed to load module 'ipip'");
55 r
= load_module("sit");
57 return log_tests_skipped_errno(r
, "failed to load module 'sit'");
60 return log_tests_skipped("not root");
63 assert_se(sd_rtnl_message_new_link(rtnl
, &m
, RTM_NEWLINK
, 0) >= 0);
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);
69 assert_se(sd_netlink_message_open_container(m
, IFLA_LINKINFO
) >= 0);
71 assert_se(sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, "ipip") >= 0);
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);
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);
79 assert_se(sd_netlink_message_close_container(m
) >= 0);
80 assert_se(sd_netlink_message_close_container(m
) >= 0);
82 assert_se(sd_netlink_call(rtnl
, m
, -1, 0) == 1);
84 assert_se((m
= sd_netlink_message_unref(m
)) == NULL
);
87 assert_se(sd_rtnl_message_new_link(rtnl
, &n
, RTM_NEWLINK
, 0) >= 0);
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);
93 assert_se(sd_netlink_message_open_container(n
, IFLA_LINKINFO
) >= 0);
95 assert_se(sd_netlink_message_open_container_union(n
, IFLA_INFO_DATA
, "sit") >= 0);
97 assert_se(sd_netlink_message_append_u8(n
, IFLA_IPTUN_PROTO
, IPPROTO_IPIP
) >= 0);
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);
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);
105 assert_se(sd_netlink_message_close_container(n
) >= 0);
106 assert_se(sd_netlink_message_close_container(n
) >= 0);
108 assert_se(sd_netlink_call(rtnl
, n
, -1, 0) == 1);
110 assert_se((n
= sd_netlink_message_unref(n
)) == NULL
);
115 int main(int argc
, char *argv
[]) {
119 test_setup_logging(LOG_INFO
);
121 assert_se(sd_netlink_open(&rtnl
) >= 0);
124 r
= test_tunnel_configure(rtnl
);
126 assert_se((rtnl
= sd_netlink_unref(rtnl
)) == NULL
);