]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-netlink-manual.c
tree-wide: minor formatting inconsistency cleanups
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Susant Sahani
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <arpa/inet.h>
21 #include <libkmod.h>
22 #include <linux/ip.h>
23 #include <net/if.h>
24 #include <linux/if_tunnel.h>
25
26 #include "sd-netlink.h"
27
28 #include "macro.h"
29 #include "util.h"
30
31 static int load_module(const char *mod_name) {
32 struct kmod_ctx *ctx;
33 struct kmod_list *list = NULL, *l;
34 int r;
35
36 ctx = kmod_new(NULL, NULL);
37 if (!ctx) {
38 kmod_unref(ctx);
39 return -ENOMEM;
40 }
41
42 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
43 if (r < 0)
44 return -1;
45
46 kmod_list_foreach(l, list) {
47 struct kmod_module *mod = kmod_module_get_module(l);
48
49 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
50 if (r >= 0)
51 r = 0;
52 else
53 r = -1;
54
55 kmod_module_unref(mod);
56 }
57
58 kmod_module_unref_list(list);
59 kmod_unref(ctx);
60
61 return r;
62 }
63
64 static int test_tunnel_configure(sd_netlink *rtnl) {
65 int r;
66 sd_netlink_message *m, *n;
67 struct in_addr local, remote;
68
69 /* skip test if module cannot be loaded */
70 r = load_module("ipip");
71 if (r < 0)
72 return EXIT_TEST_SKIP;
73
74 if (getuid() != 0)
75 return EXIT_TEST_SKIP;
76
77 /* IPIP tunnel */
78 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
79 assert_se(m);
80
81 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
82 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
83
84 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
85
86 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
87
88 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
89 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
90
91 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
92 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
93
94 assert_se(sd_netlink_message_close_container(m) >= 0);
95 assert_se(sd_netlink_message_close_container(m) >= 0);
96
97 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
98
99 assert_se((m = sd_netlink_message_unref(m)) == NULL);
100
101 r = load_module("sit");
102 if (r < 0)
103 return EXIT_TEST_SKIP;
104
105 /* sit */
106 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
107 assert_se(n);
108
109 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
110 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
111
112 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
113
114 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
115
116 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
117
118 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
119 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
120
121 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
122 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
123
124 assert_se(sd_netlink_message_close_container(n) >= 0);
125 assert_se(sd_netlink_message_close_container(n) >= 0);
126
127 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
128
129 assert_se((n = sd_netlink_message_unref(n)) == NULL);
130
131 return EXIT_SUCCESS;
132 }
133
134 int main(int argc, char *argv[]) {
135 sd_netlink *rtnl;
136 int r;
137
138 assert_se(sd_netlink_open(&rtnl) >= 0);
139 assert_se(rtnl);
140
141 r = test_tunnel_configure(rtnl);
142
143 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
144
145 return r;
146 }