| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Tests for rtnl_link_ip6_tnl_set/get_collect_metadata(). |
| 5 |
* |
| 6 |
* We cover two aspects: |
| 7 |
* 1. Pure userspace logic – flag manipulation on an allocated link object. |
| 8 |
* 2. Kernel round-trip – create a real ip6tnl interface inside a private |
| 9 |
* network namespace with the flag enabled, query it back from the kernel |
| 10 |
* and verify that the flag round-trips correctly. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "nl-default.h" |
| 14 |
|
| 15 |
#include <netlink/route/link/ip6tnl.h> |
| 16 |
|
| 17 |
#include "cksuite-all.h" |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
#define TTL 42 |
| 22 |
#define ENCAPLIMIT 3 |
| 23 |
#define TOS 0x1a |
| 24 |
/* 0x7f means all IP6_TNL_F_ are set */ |
| 25 |
#define FLAGS 0x7f |
| 26 |
/* The kernel will OR the flags with 0x30000 (IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV) */ |
| 27 |
#define FLAGS_KERNEL (0x30000 | FLAGS) |
| 28 |
#define FLOWINFO 0xbeefdead |
| 29 |
#define PROTO 41 /* IPPROTO_IPV6 in decimal */ |
| 30 |
#define FWMARK 0xdead |
| 31 |
|
| 32 |
/* Kernel round-trip all attributes */ |
| 33 |
|
| 34 |
START_TEST(test_kernel_roundtrip_all) |
| 35 |
{ |
| 36 |
const char *IFNAME = "xit6tnl1"; |
| 37 |
const char *IFNAME_DUMMY = "xit6tnl-under"; |
| 38 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 39 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 40 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 41 |
_nl_auto_rtnl_link struct rtnl_link *link = NULL; |
| 42 |
struct in6_addr addr_local, addr_remote, addr_tmp; |
| 43 |
struct rtnl_link *link_kernel; |
| 44 |
int ifindex_dummy; |
| 45 |
uint32_t u32tmp; |
| 46 |
int enabled; |
| 47 |
int r; |
| 48 |
|
| 49 |
if (_nltst_skip_no_netns()) |
| 50 |
return; |
| 51 |
|
| 52 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 53 |
|
| 54 |
link = rtnl_link_ip6_tnl_alloc(); |
| 55 |
ck_assert_ptr_nonnull(link); |
| 56 |
|
| 57 |
auto_del_dummy = IFNAME_DUMMY; |
| 58 |
|
| 59 |
rtnl_link_set_name(link, IFNAME); |
| 60 |
|
| 61 |
_nltst_add_link(sk, IFNAME_DUMMY, "dummy", &ifindex_dummy); |
| 62 |
r = rtnl_link_ip6_tnl_set_link(link, ifindex_dummy); |
| 63 |
ck_assert_int_eq(r, 0); |
| 64 |
|
| 65 |
addr_local = _nltst_inet6("2001:db8::10"); |
| 66 |
addr_remote = _nltst_inet6("2001:db8::20"); |
| 67 |
|
| 68 |
r = rtnl_link_ip6_tnl_set_local(link, &addr_local); |
| 69 |
ck_assert_int_eq(r, 0); |
| 70 |
r = rtnl_link_ip6_tnl_set_remote(link, &addr_remote); |
| 71 |
ck_assert_int_eq(r, 0); |
| 72 |
|
| 73 |
r = rtnl_link_ip6_tnl_set_ttl(link, TTL); |
| 74 |
ck_assert_int_eq(r, 0); |
| 75 |
r = rtnl_link_ip6_tnl_set_encaplimit(link, ENCAPLIMIT); |
| 76 |
ck_assert_int_eq(r, 0); |
| 77 |
|
| 78 |
r = rtnl_link_ip6_tnl_set_tos(link, TOS); |
| 79 |
ck_assert_int_eq(r, 0); |
| 80 |
|
| 81 |
r = rtnl_link_ip6_tnl_set_flags(link, FLAGS); |
| 82 |
ck_assert_int_eq(r, 0); |
| 83 |
|
| 84 |
r = rtnl_link_ip6_tnl_set_flowinfo(link, FLOWINFO); |
| 85 |
ck_assert_int_eq(r, 0); |
| 86 |
|
| 87 |
r = rtnl_link_ip6_tnl_set_proto(link, PROTO); |
| 88 |
ck_assert_int_eq(r, 0); |
| 89 |
|
| 90 |
r = rtnl_link_ip6_tnl_set_fwmark(link, FWMARK); |
| 91 |
ck_assert_int_eq(r, 0); |
| 92 |
|
| 93 |
r = rtnl_link_ip6_tnl_set_collect_metadata(link, 1); |
| 94 |
ck_assert_int_eq(r, 0); |
| 95 |
|
| 96 |
/* Add the link to the kernel. |
| 97 |
* This tests the netlink-message construction. |
| 98 |
*/ |
| 99 |
r = rtnl_link_add(sk, link, NLM_F_CREATE); |
| 100 |
ck_assert_int_eq(r, 0); |
| 101 |
|
| 102 |
/* Now, query it and check whether all the attributes passed. |
| 103 |
* This checks the netlink-message parsing. |
| 104 |
*/ |
| 105 |
cache = _nltst_rtnl_link_alloc_cache(sk, AF_UNSPEC, 0); |
| 106 |
link_kernel = _nltst_cache_get_link(cache, IFNAME); |
| 107 |
ck_assert_ptr_nonnull(link_kernel); |
| 108 |
|
| 109 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_link(link_kernel), |
| 110 |
(uint32_t)ifindex_dummy); |
| 111 |
|
| 112 |
ck_assert_int_eq(rtnl_link_is_ip6_tnl(link_kernel), 1); |
| 113 |
|
| 114 |
r = rtnl_link_ip6_tnl_get_local(link_kernel, &addr_tmp); |
| 115 |
ck_assert_int_eq(r, 0); |
| 116 |
ck_assert_int_eq(memcmp(&addr_tmp, &addr_local, sizeof(addr_local)), 0); |
| 117 |
r = rtnl_link_ip6_tnl_get_remote(link_kernel, &addr_tmp); |
| 118 |
ck_assert_int_eq(r, 0); |
| 119 |
ck_assert_int_eq(memcmp(&addr_tmp, &addr_remote, sizeof(addr_remote)), |
| 120 |
0); |
| 121 |
|
| 122 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_ttl(link_kernel), TTL); |
| 123 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_encaplimit(link_kernel), |
| 124 |
ENCAPLIMIT); |
| 125 |
|
| 126 |
/* Note: IP_TOS is actually not set'able for ip6-tnl. One can set the |
| 127 |
* traffic-class through the flowinfo. |
| 128 |
* |
| 129 |
* But, the API exists in libnl, so let's test for it. |
| 130 |
*/ |
| 131 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_tos(link_kernel), 0); |
| 132 |
|
| 133 |
u32tmp = 0; |
| 134 |
r = rtnl_link_ip6_tnl_get_fwmark(link_kernel, &u32tmp); |
| 135 |
ck_assert_int_eq(r, 0); |
| 136 |
ck_assert_uint_eq(u32tmp, FWMARK); |
| 137 |
|
| 138 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_flags(link_kernel), |
| 139 |
FLAGS_KERNEL); |
| 140 |
|
| 141 |
/* Flowinfo and proto should be preserved by kernel */ |
| 142 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_flowinfo(link_kernel), |
| 143 |
FLOWINFO); |
| 144 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_proto(link_kernel), PROTO); |
| 145 |
|
| 146 |
enabled = 0; |
| 147 |
r = rtnl_link_ip6_tnl_get_collect_metadata(link_kernel, &enabled); |
| 148 |
ck_assert_int_eq(r, 0); |
| 149 |
ck_assert_int_eq(enabled, 1); |
| 150 |
|
| 151 |
rtnl_link_delete(sk, link); |
| 152 |
} |
| 153 |
END_TEST |
| 154 |
|
| 155 |
/*****************************************************************************/ |
| 156 |
/* Userspace comprehensive test covering all API calls */ |
| 157 |
|
| 158 |
START_TEST(test_api_set_get_all) |
| 159 |
{ |
| 160 |
struct rtnl_link *link; |
| 161 |
struct in6_addr addr_local, addr_remote, tmp_addr; |
| 162 |
int r; |
| 163 |
uint32_t u32tmp; |
| 164 |
int enabled; |
| 165 |
|
| 166 |
link = rtnl_link_ip6_tnl_alloc(); |
| 167 |
ck_assert_ptr_nonnull(link); |
| 168 |
ck_assert_int_eq(rtnl_link_is_ip6_tnl(link), 1); |
| 169 |
|
| 170 |
r = rtnl_link_ip6_tnl_set_link(link, 123); |
| 171 |
ck_assert_int_eq(r, 0); |
| 172 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_link(link), 123); |
| 173 |
|
| 174 |
/* Local / Remote addresses */ |
| 175 |
addr_local = _nltst_inet6("2001:db8::1"); |
| 176 |
addr_remote = _nltst_inet6("2001:db8::2"); |
| 177 |
r = rtnl_link_ip6_tnl_set_local(link, &addr_local); |
| 178 |
ck_assert_int_eq(r, 0); |
| 179 |
r = rtnl_link_ip6_tnl_set_remote(link, &addr_remote); |
| 180 |
ck_assert_int_eq(r, 0); |
| 181 |
|
| 182 |
r = rtnl_link_ip6_tnl_get_local(link, &tmp_addr); |
| 183 |
ck_assert_int_eq(r, 0); |
| 184 |
ck_assert_int_eq(memcmp(&tmp_addr, &addr_local, sizeof(addr_local)), 0); |
| 185 |
|
| 186 |
r = rtnl_link_ip6_tnl_get_remote(link, &tmp_addr); |
| 187 |
ck_assert_int_eq(r, 0); |
| 188 |
ck_assert_int_eq(memcmp(&tmp_addr, &addr_remote, sizeof(addr_remote)), |
| 189 |
0); |
| 190 |
|
| 191 |
r = rtnl_link_ip6_tnl_set_ttl(link, TTL); |
| 192 |
ck_assert_int_eq(r, 0); |
| 193 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_ttl(link), TTL); |
| 194 |
|
| 195 |
r = rtnl_link_ip6_tnl_set_tos(link, TOS); |
| 196 |
ck_assert_int_eq(r, 0); |
| 197 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_tos(link), TOS); |
| 198 |
|
| 199 |
r = rtnl_link_ip6_tnl_set_encaplimit(link, ENCAPLIMIT); |
| 200 |
ck_assert_int_eq(r, 0); |
| 201 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_encaplimit(link), ENCAPLIMIT); |
| 202 |
|
| 203 |
r = rtnl_link_ip6_tnl_set_flags(link, FLAGS); |
| 204 |
ck_assert_int_eq(r, 0); |
| 205 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_flags(link), FLAGS); |
| 206 |
|
| 207 |
r = rtnl_link_ip6_tnl_set_flowinfo(link, FLOWINFO); |
| 208 |
ck_assert_int_eq(r, 0); |
| 209 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_flowinfo(link), FLOWINFO); |
| 210 |
|
| 211 |
r = rtnl_link_ip6_tnl_set_proto(link, PROTO); |
| 212 |
ck_assert_int_eq(r, 0); |
| 213 |
ck_assert_uint_eq(rtnl_link_ip6_tnl_get_proto(link), PROTO); |
| 214 |
|
| 215 |
r = rtnl_link_ip6_tnl_set_fwmark(link, FWMARK); |
| 216 |
ck_assert_int_eq(r, 0); |
| 217 |
u32tmp = 0; |
| 218 |
r = rtnl_link_ip6_tnl_get_fwmark(link, &u32tmp); |
| 219 |
ck_assert_int_eq(r, 0); |
| 220 |
ck_assert_uint_eq(u32tmp, FWMARK); |
| 221 |
|
| 222 |
r = rtnl_link_ip6_tnl_set_collect_metadata(link, 1); |
| 223 |
ck_assert_int_eq(r, 0); |
| 224 |
enabled = 0; |
| 225 |
r = rtnl_link_ip6_tnl_get_collect_metadata(link, &enabled); |
| 226 |
ck_assert_int_eq(r, 0); |
| 227 |
ck_assert_int_eq(enabled, 1); |
| 228 |
|
| 229 |
r = rtnl_link_ip6_tnl_set_collect_metadata(link, 0); |
| 230 |
ck_assert_int_eq(r, 0); |
| 231 |
enabled = 1; |
| 232 |
r = rtnl_link_ip6_tnl_get_collect_metadata(link, &enabled); |
| 233 |
ck_assert_int_eq(r, 0); |
| 234 |
ck_assert_int_eq(enabled, 0); |
| 235 |
|
| 236 |
rtnl_link_put(link); |
| 237 |
} |
| 238 |
END_TEST |
| 239 |
|
| 240 |
Suite *make_nl_ip6_tnl_suite(void) |
| 241 |
{ |
| 242 |
Suite *suite = suite_create("ip6tnl"); |
| 243 |
TCase *tc_api = tcase_create("Userspace-API"); |
| 244 |
TCase *tc_kernel = tcase_create("Kernel"); |
| 245 |
|
| 246 |
/* Comprehensive API setter/getter test (userspace only) */ |
| 247 |
tcase_add_test(tc_api, test_api_set_get_all); |
| 248 |
suite_add_tcase(suite, tc_api); |
| 249 |
|
| 250 |
/* Kernel round-trip – needs private netns */ |
| 251 |
tcase_add_checked_fixture(tc_kernel, nltst_netns_fixture_setup, |
| 252 |
nltst_netns_fixture_teardown); |
| 253 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_all); |
| 254 |
suite_add_tcase(suite, tc_kernel); |
| 255 |
|
| 256 |
return suite; |
| 257 |
} |