| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Tests for include/netlink/route/nh.h getters and setters. |
| 5 |
* |
| 6 |
* We cover two aspects: |
| 7 |
* 1. Pure userspace logic – manipulating an allocated rtnl_nh object and |
| 8 |
* validating all getters, including negative/error conditions. |
| 9 |
* 2. Kernel round-trip – create a real nexthop in a private network |
| 10 |
* namespace, query it back via the cache and verify attributes round-trip. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "nl-default.h" |
| 14 |
|
| 15 |
#include <linux/if.h> |
| 16 |
#include <linux/ila.h> |
| 17 |
#include <linux/lwtunnel.h> |
| 18 |
#include <linux/nexthop.h> |
| 19 |
#include <linux-private/linux/if_tunnel.h> |
| 20 |
|
| 21 |
#include <netlink/route/nh.h> |
| 22 |
#include <netlink/route/link.h> |
| 23 |
#include <netlink/route/addr.h> |
| 24 |
#include <netlink/route/nexthop.h> |
| 25 |
|
| 26 |
#include "cksuite-all.h" |
| 27 |
|
| 28 |
/*****************************************************************************/ |
| 29 |
/* Kernel round-trip tests exercising rtnl_nh_add() and message parsing */ |
| 30 |
|
| 31 |
START_TEST(test_kernel_roundtrip_basic_v4) |
| 32 |
{ |
| 33 |
const char *IFNAME_DUMMY = "nh-dummy0"; |
| 34 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 35 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 36 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 37 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 38 |
struct rtnl_nh *nh_kernel; |
| 39 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 40 |
int ifindex_dummy; |
| 41 |
int r; |
| 42 |
|
| 43 |
if (_nltst_skip_no_netns()) |
| 44 |
return; |
| 45 |
|
| 46 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 47 |
|
| 48 |
/* Create dummy underlay, bring up and add an IPv4 address */ |
| 49 |
auto_del_dummy = IFNAME_DUMMY; |
| 50 |
_nltst_add_dummy_v4_with_addr(sk, IFNAME_DUMMY, &ifindex_dummy, |
| 51 |
"192.0.2.2", 24); |
| 52 |
|
| 53 |
/* Build nexthop: v4 gateway over dummy OIF with explicit id */ |
| 54 |
nh = rtnl_nh_alloc(); |
| 55 |
ck_assert_ptr_nonnull(nh); |
| 56 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET), 0); |
| 57 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 1001), 0); |
| 58 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 59 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 60 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 61 |
|
| 62 |
r = rtnl_nh_add(sk, nh, NLM_F_CREATE); |
| 63 |
ck_assert_int_eq(r, 0); |
| 64 |
|
| 65 |
/* Query and verify attributes */ |
| 66 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 67 |
nh_kernel = rtnl_nh_get(cache, 1001); |
| 68 |
ck_assert_ptr_nonnull(nh_kernel); |
| 69 |
|
| 70 |
ck_assert_int_eq(rtnl_nh_get_id(nh_kernel), 1001); |
| 71 |
ck_assert_int_eq(rtnl_nh_get_family(nh_kernel), AF_INET); |
| 72 |
ck_assert_int_eq(rtnl_nh_get_oif(nh_kernel), ifindex_dummy); |
| 73 |
ck_assert_ptr_nonnull(rtnl_nh_get_gateway(nh_kernel)); |
| 74 |
ck_assert_int_eq(nl_addr_get_family(rtnl_nh_get_gateway(nh_kernel)), |
| 75 |
AF_INET); |
| 76 |
} |
| 77 |
END_TEST |
| 78 |
|
| 79 |
/* Kernel round-trip tests for MPLS encap on rtnl_nh */ |
| 80 |
|
| 81 |
START_TEST(test_kernel_roundtrip_encap_mpls) |
| 82 |
{ |
| 83 |
const char *IFNAME_DUMMY = "nh-dummy-encap0"; |
| 84 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 85 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 86 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 87 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 88 |
struct rtnl_nh *knh; |
| 89 |
struct rtnl_nh_encap *kencap; |
| 90 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 91 |
_nl_auto_nl_addr struct nl_addr *labels = NULL; |
| 92 |
struct rtnl_nh_encap *encap = NULL; |
| 93 |
int ifindex_dummy; |
| 94 |
|
| 95 |
if (_nltst_skip_no_netns()) |
| 96 |
return; |
| 97 |
|
| 98 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 99 |
|
| 100 |
/* Create underlay with IPv4 address */ |
| 101 |
auto_del_dummy = IFNAME_DUMMY; |
| 102 |
_nltst_add_dummy_v4_with_addr(sk, IFNAME_DUMMY, &ifindex_dummy, |
| 103 |
"192.0.2.2", 24); |
| 104 |
|
| 105 |
/* Build nexthop: v4 gw over dummy with MPLS encap */ |
| 106 |
nh = rtnl_nh_alloc(); |
| 107 |
ck_assert_ptr_nonnull(nh); |
| 108 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3101), 0); |
| 109 |
|
| 110 |
/* Surprisingly the kernel accepts a nexthop with encap and a gw */ |
| 111 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 112 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 113 |
|
| 114 |
encap = rtnl_nh_encap_alloc(); |
| 115 |
ck_assert_ptr_nonnull(encap); |
| 116 |
ck_assert_ptr_null(rtnl_nh_get_encap_mpls_dst(encap)); |
| 117 |
ck_assert_int_eq(rtnl_nh_get_encap_mpls_ttl(encap), -NLE_INVAL); |
| 118 |
ck_assert_int_eq(nl_addr_parse("100", AF_MPLS, &labels), 0); |
| 119 |
ck_assert_int_eq(rtnl_nh_encap_mpls(encap, labels, 64), 0); |
| 120 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, encap), 0); |
| 121 |
|
| 122 |
/* Fails - we need a family & oif*/ |
| 123 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), -NLE_INVAL); |
| 124 |
|
| 125 |
/* Fails, we need a family */ |
| 126 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 127 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), -NLE_INVAL); |
| 128 |
|
| 129 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET), 0); |
| 130 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), 0); |
| 131 |
|
| 132 |
/* Query and verify */ |
| 133 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 134 |
knh = rtnl_nh_get(cache, 3101); |
| 135 |
ck_assert_ptr_nonnull(knh); |
| 136 |
ck_assert_int_eq(rtnl_nh_get_id(knh), 3101); |
| 137 |
ck_assert_int_eq(rtnl_nh_get_oif(knh), ifindex_dummy); |
| 138 |
|
| 139 |
kencap = rtnl_nh_get_encap(knh); |
| 140 |
ck_assert_ptr_nonnull(kencap); |
| 141 |
ck_assert_int_eq(rtnl_nh_encap_get_type(kencap), LWTUNNEL_ENCAP_MPLS); |
| 142 |
ck_assert_ptr_nonnull(rtnl_nh_get_encap_mpls_dst(kencap)); |
| 143 |
ck_assert_uint_eq(rtnl_nh_get_encap_mpls_ttl(kencap), 64); |
| 144 |
} |
| 145 |
END_TEST |
| 146 |
|
| 147 |
START_TEST(test_kernel_negative_mismatched_gw_family) |
| 148 |
{ |
| 149 |
const char *IFNAME_DUMMY = "nh-dummy-neg0"; |
| 150 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 151 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 152 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 153 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 154 |
int ifindex_dummy; |
| 155 |
|
| 156 |
if (_nltst_skip_no_netns()) |
| 157 |
return; |
| 158 |
|
| 159 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 160 |
|
| 161 |
auto_del_dummy = IFNAME_DUMMY; |
| 162 |
_nltst_add_dummy_and_up(sk, IFNAME_DUMMY, &ifindex_dummy); |
| 163 |
|
| 164 |
/* Build nexthop with AF_INET6 but an IPv4 gateway -> invalid */ |
| 165 |
nh = rtnl_nh_alloc(); |
| 166 |
ck_assert_ptr_nonnull(nh); |
| 167 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET6), 0); |
| 168 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3001), 0); |
| 169 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 170 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 171 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 172 |
|
| 173 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), -NLE_INVAL); |
| 174 |
} |
| 175 |
END_TEST |
| 176 |
|
| 177 |
START_TEST(test_kernel_negative_group_without_entries) |
| 178 |
{ |
| 179 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 180 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 181 |
|
| 182 |
if (_nltst_skip_no_netns()) |
| 183 |
return; |
| 184 |
|
| 185 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 186 |
|
| 187 |
/* Build a group nexthop with a type set but without any entries */ |
| 188 |
nh = rtnl_nh_alloc(); |
| 189 |
ck_assert_ptr_nonnull(nh); |
| 190 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET), 0); |
| 191 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3002), 0); |
| 192 |
ck_assert_int_eq(rtnl_nh_set_group_type(nh, NEXTHOP_GRP_TYPE_MPATH), 0); |
| 193 |
|
| 194 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), -NLE_INVAL); |
| 195 |
} |
| 196 |
END_TEST |
| 197 |
|
| 198 |
START_TEST(test_kernel_negative_gateway_without_oif) |
| 199 |
{ |
| 200 |
const char *IFNAME_DUMMY = "nh-dummy-neg1"; |
| 201 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 202 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 203 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 204 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 205 |
int ifindex_dummy; |
| 206 |
|
| 207 |
if (_nltst_skip_no_netns()) |
| 208 |
return; |
| 209 |
|
| 210 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 211 |
|
| 212 |
/* Create a dummy device to avoid dependency on system state */ |
| 213 |
auto_del_dummy = IFNAME_DUMMY; |
| 214 |
_nltst_add_dummy_and_up(sk, IFNAME_DUMMY, &ifindex_dummy); |
| 215 |
|
| 216 |
/* Build nexthop with IPv4 gateway but no OIF -> invalid */ |
| 217 |
nh = rtnl_nh_alloc(); |
| 218 |
ck_assert_ptr_nonnull(nh); |
| 219 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET), 0); |
| 220 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3003), 0); |
| 221 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 222 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 223 |
|
| 224 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), -NLE_INVAL); |
| 225 |
} |
| 226 |
END_TEST |
| 227 |
|
| 228 |
START_TEST(test_kernel_roundtrip_oif_only) |
| 229 |
{ |
| 230 |
const char *IFNAME_DUMMY = "nh-dummy1"; |
| 231 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 232 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 233 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 234 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 235 |
struct rtnl_nh *nh_kernel; |
| 236 |
int ifindex_dummy; |
| 237 |
int r; |
| 238 |
|
| 239 |
if (_nltst_skip_no_netns()) |
| 240 |
return; |
| 241 |
|
| 242 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 243 |
|
| 244 |
auto_del_dummy = IFNAME_DUMMY; |
| 245 |
_nltst_add_dummy_and_up(sk, IFNAME_DUMMY, &ifindex_dummy); |
| 246 |
|
| 247 |
/* Build nexthop: OIF only, unspecified family */ |
| 248 |
nh = rtnl_nh_alloc(); |
| 249 |
ck_assert_ptr_nonnull(nh); |
| 250 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_UNSPEC), 0); |
| 251 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 1002), 0); |
| 252 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 253 |
|
| 254 |
r = rtnl_nh_add(sk, nh, NLM_F_CREATE); |
| 255 |
ck_assert_int_eq(r, -NLE_INVAL); /* Because family was AF_UNSPEC */ |
| 256 |
|
| 257 |
/* Fix the family and now rtnl_nh_add should succeed */ |
| 258 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET6), 0); |
| 259 |
r = rtnl_nh_add(sk, nh, NLM_F_CREATE); |
| 260 |
ck_assert_int_eq(r, 0); |
| 261 |
|
| 262 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 263 |
nh_kernel = rtnl_nh_get(cache, 1002); |
| 264 |
ck_assert_ptr_nonnull(nh_kernel); |
| 265 |
|
| 266 |
ck_assert_int_eq(rtnl_nh_get_id(nh_kernel), 1002); |
| 267 |
ck_assert_int_eq(rtnl_nh_get_oif(nh_kernel), ifindex_dummy); |
| 268 |
ck_assert_ptr_null(rtnl_nh_get_gateway(nh_kernel)); |
| 269 |
} |
| 270 |
END_TEST |
| 271 |
|
| 272 |
START_TEST(test_kernel_roundtrip_group_mpath) |
| 273 |
{ |
| 274 |
const char *IFNAME_DUMMY = "nh-dummy2"; |
| 275 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 276 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 277 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 278 |
_nl_auto_rtnl_nh struct rtnl_nh *nh1 = NULL; |
| 279 |
_nl_auto_rtnl_nh struct rtnl_nh *nh2 = NULL; |
| 280 |
_nl_auto_rtnl_nh struct rtnl_nh *grp = NULL; |
| 281 |
struct rtnl_nh *grp_kernel; |
| 282 |
nl_nh_group_info_t entries[2]; |
| 283 |
int ifindex_dummy; |
| 284 |
int r; |
| 285 |
|
| 286 |
if (_nltst_skip_no_netns()) |
| 287 |
return; |
| 288 |
|
| 289 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 290 |
|
| 291 |
auto_del_dummy = IFNAME_DUMMY; |
| 292 |
_nltst_add_dummy_and_up(sk, IFNAME_DUMMY, &ifindex_dummy); |
| 293 |
|
| 294 |
/* Two basic nexthops to reference in the group */ |
| 295 |
nh1 = rtnl_nh_alloc(); |
| 296 |
ck_assert_ptr_nonnull(nh1); |
| 297 |
ck_assert_int_eq(rtnl_nh_set_id(nh1, 1003), 0); |
| 298 |
ck_assert_int_eq(rtnl_nh_set_family(nh1, AF_INET6), 0); |
| 299 |
ck_assert_int_eq(rtnl_nh_set_oif(nh1, (uint32_t)ifindex_dummy), 0); |
| 300 |
r = rtnl_nh_add(sk, nh1, NLM_F_CREATE); |
| 301 |
ck_assert_int_eq(r, 0); |
| 302 |
|
| 303 |
nh2 = rtnl_nh_alloc(); |
| 304 |
ck_assert_ptr_nonnull(nh2); |
| 305 |
ck_assert_int_eq(rtnl_nh_set_id(nh2, 1004), 0); |
| 306 |
ck_assert_int_eq(rtnl_nh_set_family(nh2, AF_INET6), 0); |
| 307 |
ck_assert_int_eq(rtnl_nh_set_oif(nh2, (uint32_t)ifindex_dummy), 0); |
| 308 |
r = rtnl_nh_add(sk, nh2, NLM_F_CREATE); |
| 309 |
ck_assert_int_eq(r, 0); |
| 310 |
|
| 311 |
/* Group nexthop referencing the above two, with weights */ |
| 312 |
grp = rtnl_nh_alloc(); |
| 313 |
ck_assert_ptr_nonnull(grp); |
| 314 |
ck_assert_int_eq(rtnl_nh_set_id(grp, 2000), 0); |
| 315 |
entries[0].nh_id = 666; /* Does not exist */ |
| 316 |
entries[0].weight = 1; |
| 317 |
entries[1].nh_id = 1004; |
| 318 |
entries[1].weight = 2; |
| 319 |
ck_assert_int_eq(rtnl_nh_set_group(grp, entries, 2), 0); |
| 320 |
ck_assert_int_eq(rtnl_nh_set_group_type(grp, NEXTHOP_GRP_TYPE_MPATH), |
| 321 |
0); |
| 322 |
|
| 323 |
r = rtnl_nh_add(sk, grp, NLM_F_CREATE); |
| 324 |
ck_assert_int_eq(r, -NLE_INVAL); /* One of the nh_ids did not exist */ |
| 325 |
|
| 326 |
/* Fix entries[0].nh_id - now rtnl_nh_add will pass */ |
| 327 |
entries[0].nh_id = 1003; |
| 328 |
ck_assert_int_eq(rtnl_nh_set_group(grp, entries, 2), 0); |
| 329 |
|
| 330 |
r = rtnl_nh_add(sk, grp, NLM_F_CREATE); |
| 331 |
ck_assert_int_eq(r, 0); |
| 332 |
|
| 333 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 334 |
grp_kernel = rtnl_nh_get(cache, 2000); |
| 335 |
ck_assert_ptr_nonnull(grp_kernel); |
| 336 |
|
| 337 |
ck_assert_int_eq(rtnl_nh_get_group_type(grp_kernel), |
| 338 |
NEXTHOP_GRP_TYPE_MPATH); |
| 339 |
ck_assert_int_eq(rtnl_nh_get_group_size(grp_kernel), 2); |
| 340 |
ck_assert_int_eq(rtnl_nh_get_group_entry(grp_kernel, 0), 1003); |
| 341 |
ck_assert_int_eq(rtnl_nh_get_group_entry(grp_kernel, 1), 1004); |
| 342 |
} |
| 343 |
END_TEST |
| 344 |
|
| 345 |
START_TEST(test_kernel_roundtrip_group_resilient) |
| 346 |
{ |
| 347 |
const char *IFNAME_DUMMY = "nh-dummy3"; |
| 348 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 349 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 350 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 351 |
_nl_auto_rtnl_nh struct rtnl_nh *nh1 = NULL; |
| 352 |
_nl_auto_rtnl_nh struct rtnl_nh *nh2 = NULL; |
| 353 |
_nl_auto_rtnl_nh struct rtnl_nh *grp = NULL; |
| 354 |
struct rtnl_nh *grp_kernel; |
| 355 |
nl_nh_group_info_t entries[2]; |
| 356 |
int ifindex_dummy; |
| 357 |
uint32_t tmp32; |
| 358 |
|
| 359 |
if (_nltst_skip_no_netns()) |
| 360 |
return; |
| 361 |
|
| 362 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 363 |
|
| 364 |
auto_del_dummy = IFNAME_DUMMY; |
| 365 |
_nltst_add_dummy_and_up(sk, IFNAME_DUMMY, &ifindex_dummy); |
| 366 |
|
| 367 |
/* Two basic nexthops to reference in the group */ |
| 368 |
nh1 = rtnl_nh_alloc(); |
| 369 |
ck_assert_ptr_nonnull(nh1); |
| 370 |
ck_assert_int_eq(rtnl_nh_set_id(nh1, 1005), 0); |
| 371 |
ck_assert_int_eq(rtnl_nh_set_family(nh1, AF_INET6), 0); |
| 372 |
ck_assert_int_eq(rtnl_nh_set_oif(nh1, (uint32_t)ifindex_dummy), 0); |
| 373 |
ck_assert_int_eq(rtnl_nh_add(sk, nh1, NLM_F_CREATE), 0); |
| 374 |
|
| 375 |
nh2 = rtnl_nh_alloc(); |
| 376 |
ck_assert_ptr_nonnull(nh2); |
| 377 |
ck_assert_int_eq(rtnl_nh_set_id(nh2, 1006), 0); |
| 378 |
ck_assert_int_eq(rtnl_nh_set_family(nh2, AF_INET6), 0); |
| 379 |
ck_assert_int_eq(rtnl_nh_set_oif(nh2, (uint32_t)ifindex_dummy), 0); |
| 380 |
ck_assert_int_eq(rtnl_nh_add(sk, nh2, NLM_F_CREATE), 0); |
| 381 |
|
| 382 |
/* Resilient group with parameters */ |
| 383 |
grp = rtnl_nh_alloc(); |
| 384 |
ck_assert_ptr_nonnull(grp); |
| 385 |
ck_assert_int_eq(rtnl_nh_set_id(grp, 2001), 0); |
| 386 |
entries[0].nh_id = 1005; |
| 387 |
entries[0].weight = 1; |
| 388 |
entries[1].nh_id = 1006; |
| 389 |
entries[1].weight = 1; |
| 390 |
ck_assert_int_eq(rtnl_nh_set_group(grp, entries, 2), 0); |
| 391 |
ck_assert_int_eq(rtnl_nh_set_group_type(grp, NEXTHOP_GRP_TYPE_RES), 0); |
| 392 |
ck_assert_int_eq(rtnl_nh_set_res_group_bucket_size(grp, 128), 0); |
| 393 |
ck_assert_int_eq(rtnl_nh_set_res_group_idle_timer(grp, 15), 0); |
| 394 |
ck_assert_int_eq(rtnl_nh_set_res_group_unbalanced_timer(grp, 25), 0); |
| 395 |
|
| 396 |
ck_assert_int_eq(rtnl_nh_add(sk, grp, NLM_F_CREATE), 0); |
| 397 |
|
| 398 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 399 |
grp_kernel = rtnl_nh_get(cache, 2001); |
| 400 |
ck_assert_ptr_nonnull(grp_kernel); |
| 401 |
|
| 402 |
ck_assert_int_eq(rtnl_nh_get_group_type(grp_kernel), |
| 403 |
NEXTHOP_GRP_TYPE_RES); |
| 404 |
ck_assert_int_eq(rtnl_nh_get_group_size(grp_kernel), 2); |
| 405 |
ck_assert_int_eq(rtnl_nh_get_group_entry(grp_kernel, 0), 1005); |
| 406 |
ck_assert_int_eq(rtnl_nh_get_group_entry(grp_kernel, 1), 1006); |
| 407 |
|
| 408 |
ck_assert_int_eq(rtnl_nh_get_res_group_bucket_size(grp_kernel), 128); |
| 409 |
tmp32 = 0; |
| 410 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(grp_kernel, &tmp32), |
| 411 |
0); |
| 412 |
ck_assert_uint_ge(tmp32, 12U); |
| 413 |
ck_assert_uint_le(tmp32, 15U); |
| 414 |
tmp32 = 0; |
| 415 |
ck_assert_int_eq( |
| 416 |
rtnl_nh_get_res_group_unbalanced_timer(grp_kernel, &tmp32), 0); |
| 417 |
ck_assert_uint_ge(tmp32, 24U); |
| 418 |
ck_assert_uint_le(tmp32, 25U); |
| 419 |
} |
| 420 |
END_TEST |
| 421 |
|
| 422 |
/* Userspace comprehensive test covering all API calls */ |
| 423 |
|
| 424 |
START_TEST(test_api_set_get_all) |
| 425 |
{ |
| 426 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 427 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 428 |
_nl_auto_nl_addr struct nl_addr *gw6 = NULL; |
| 429 |
nl_nh_group_info_t entries[2]; |
| 430 |
uint32_t tmp32; |
| 431 |
|
| 432 |
nh = rtnl_nh_alloc(); |
| 433 |
ck_assert_ptr_nonnull(nh); |
| 434 |
|
| 435 |
/* Family set/get + negative on NULL */ |
| 436 |
ck_assert_int_eq(rtnl_nh_get_family(nh), AF_UNSPEC); |
| 437 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET6), 0); |
| 438 |
ck_assert_int_eq(rtnl_nh_get_family(nh), AF_INET6); |
| 439 |
ck_assert_int_eq(rtnl_nh_set_family(NULL, AF_INET), -NLE_INVAL); |
| 440 |
ck_assert_int_eq(rtnl_nh_get_family(NULL), -NLE_INVAL); |
| 441 |
|
| 442 |
/* ID get before set -> -NLE_INVAL; set/get/clear */ |
| 443 |
ck_assert_int_eq(rtnl_nh_get_id(nh), -NLE_INVAL); |
| 444 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 1001), 0); |
| 445 |
ck_assert_int_eq(rtnl_nh_get_id(nh), 1001); |
| 446 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 0), 0); |
| 447 |
ck_assert_int_eq(rtnl_nh_get_id(nh), -NLE_INVAL); |
| 448 |
ck_assert_int_eq(rtnl_nh_set_id(NULL, 1), -NLE_INVAL); |
| 449 |
|
| 450 |
/* OIF get before set is 0; set/get; clear; negative on NULL */ |
| 451 |
ck_assert_int_eq(rtnl_nh_get_oif(nh), 0); |
| 452 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, 1234), 0); |
| 453 |
ck_assert_int_eq(rtnl_nh_get_oif(nh), 1234); |
| 454 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, 0), 0); |
| 455 |
ck_assert_int_eq(rtnl_nh_get_oif(nh), 0); |
| 456 |
ck_assert_int_eq(rtnl_nh_set_oif(NULL, 1), -NLE_INVAL); |
| 457 |
|
| 458 |
/* Gateway get/set; start NULL, set v4 then replace with v6 */ |
| 459 |
ck_assert_ptr_eq(rtnl_nh_get_gateway(nh), NULL); |
| 460 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 461 |
ck_assert_int_eq(rtnl_nh_set_gateway(NULL, gw4), -NLE_INVAL); |
| 462 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, NULL), 0); |
| 463 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 464 |
|
| 465 |
ck_assert_ptr_nonnull(rtnl_nh_get_gateway(nh)); |
| 466 |
ck_assert_int_eq(nl_addr_parse("2001:db8::1", AF_INET6, &gw6), 0); |
| 467 |
|
| 468 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw6), 0); |
| 469 |
|
| 470 |
/* Previous set_gateway replaced the old one. It should no more be shared now */ |
| 471 |
ck_assert_int_eq(nl_addr_shared(gw4), 0); |
| 472 |
|
| 473 |
ck_assert_int_eq(nl_addr_get_family(rtnl_nh_get_gateway(nh)), AF_INET6); |
| 474 |
|
| 475 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, NULL), 0); |
| 476 |
ck_assert_ptr_eq(rtnl_nh_get_gateway(nh), NULL); |
| 477 |
|
| 478 |
/* FDB flag set/clear */ |
| 479 |
ck_assert_int_eq(rtnl_nh_get_fdb(nh), 0); |
| 480 |
ck_assert_int_eq(rtnl_nh_set_fdb(nh, 1), 0); |
| 481 |
ck_assert_int_ne(rtnl_nh_get_fdb(nh), 1); |
| 482 |
ck_assert_int_eq(rtnl_nh_set_fdb(nh, 0), 0); |
| 483 |
ck_assert_int_eq(rtnl_nh_get_fdb(nh), 0); |
| 484 |
|
| 485 |
/* Group getters before set -> missing */ |
| 486 |
ck_assert_int_eq(rtnl_nh_get_group_size(nh), -NLE_MISSING_ATTR); |
| 487 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 0), -NLE_MISSING_ATTR); |
| 488 |
|
| 489 |
/* Group setter - negative tests */ |
| 490 |
ck_assert_int_eq(rtnl_nh_set_group(NULL, entries, 1), -NLE_INVAL); |
| 491 |
ck_assert_int_eq(rtnl_nh_set_group(nh, NULL, 1), -NLE_INVAL); |
| 492 |
|
| 493 |
/* Group set/get and indexing */ |
| 494 |
entries[0].nh_id = 10; |
| 495 |
entries[0].weight = 1; |
| 496 |
entries[1].nh_id = 20; |
| 497 |
entries[1].weight = 2; |
| 498 |
ck_assert_int_eq(rtnl_nh_set_group(nh, entries, 2), 0); |
| 499 |
ck_assert_int_eq(rtnl_nh_get_group_size(nh), 2); |
| 500 |
/* Unset the group with size == 0 */ |
| 501 |
ck_assert_int_eq(rtnl_nh_set_group(nh, entries, 0), 0); |
| 502 |
ck_assert_int_eq(rtnl_nh_get_group_size(nh), -NLE_MISSING_ATTR); |
| 503 |
ck_assert_int_eq(rtnl_nh_set_group(nh, entries, 2), 0); |
| 504 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, -1), -NLE_INVAL); |
| 505 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 2), -NLE_INVAL); |
| 506 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 0), 10); |
| 507 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 1), 20); |
| 508 |
|
| 509 |
/* Replace group with a different size */ |
| 510 |
ck_assert_int_eq(rtnl_nh_set_group(nh, entries, 1), 0); |
| 511 |
ck_assert_int_eq(rtnl_nh_get_group_size(nh), 1); |
| 512 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 0), 10); |
| 513 |
ck_assert_int_eq(rtnl_nh_get_group_entry(nh, 1), -NLE_INVAL); |
| 514 |
|
| 515 |
/* Remove group */ |
| 516 |
ck_assert_int_eq(rtnl_nh_set_group(nh, NULL, 0), 0); |
| 517 |
ck_assert_int_eq(rtnl_nh_get_group_size(nh), -NLE_MISSING_ATTR); |
| 518 |
|
| 519 |
/* Group type get negative before set, set/get after */ |
| 520 |
ck_assert_int_eq(rtnl_nh_get_group_type(NULL), -NLE_INVAL); |
| 521 |
ck_assert_int_eq(rtnl_nh_get_group_type(nh), -NLE_INVAL); |
| 522 |
ck_assert_int_eq(rtnl_nh_set_group_type(NULL, NEXTHOP_GRP_TYPE_MPATH), |
| 523 |
-NLE_INVAL); |
| 524 |
ck_assert_int_eq(rtnl_nh_set_group_type(nh, NEXTHOP_GRP_TYPE_MPATH), 0); |
| 525 |
ck_assert_int_eq(rtnl_nh_get_group_type(nh), NEXTHOP_GRP_TYPE_MPATH); |
| 526 |
ck_assert_int_eq(rtnl_nh_set_group_type(NULL, NEXTHOP_GRP_TYPE_RES), |
| 527 |
-NLE_INVAL); |
| 528 |
|
| 529 |
/* Resilient group setters/getters negative when type != RES */ |
| 530 |
ck_assert_int_eq(rtnl_nh_set_res_group_bucket_size(nh, 64), -NLE_INVAL); |
| 531 |
ck_assert_int_eq(rtnl_nh_get_res_group_bucket_size(nh), -NLE_INVAL); |
| 532 |
ck_assert_int_eq(rtnl_nh_set_res_group_idle_timer(nh, 10), -NLE_INVAL); |
| 533 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(nh, &tmp32), |
| 534 |
-NLE_INVAL); |
| 535 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(nh, NULL), |
| 536 |
-NLE_INVAL); |
| 537 |
ck_assert_int_eq(rtnl_nh_set_res_group_unbalanced_timer(nh, 20), |
| 538 |
-NLE_INVAL); |
| 539 |
ck_assert_int_eq(rtnl_nh_get_res_group_unbalanced_timer(nh, &tmp32), |
| 540 |
-NLE_INVAL); |
| 541 |
ck_assert_int_eq(rtnl_nh_get_res_group_unbalanced_timer(nh, NULL), |
| 542 |
-NLE_INVAL); |
| 543 |
|
| 544 |
/* Switch to resilient type and test getters/setters */ |
| 545 |
ck_assert_int_eq(rtnl_nh_set_group_type(nh, NEXTHOP_GRP_TYPE_RES), 0); |
| 546 |
|
| 547 |
/* Bucket size: missing before set, then set/get, then clear */ |
| 548 |
ck_assert_int_eq(rtnl_nh_get_res_group_bucket_size(nh), |
| 549 |
-NLE_MISSING_ATTR); |
| 550 |
ck_assert_int_eq(rtnl_nh_set_res_group_bucket_size(nh, 128), 0); |
| 551 |
ck_assert_int_eq(rtnl_nh_get_res_group_bucket_size(nh), 128); |
| 552 |
ck_assert_int_eq(rtnl_nh_set_res_group_bucket_size(nh, 0), 0); |
| 553 |
ck_assert_int_eq(rtnl_nh_get_res_group_bucket_size(nh), |
| 554 |
-NLE_MISSING_ATTR); |
| 555 |
|
| 556 |
/* Idle timer: missing before set, then set/get; out NULL negative */ |
| 557 |
tmp32 = 0; |
| 558 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(nh, &tmp32), |
| 559 |
-NLE_MISSING_ATTR); |
| 560 |
ck_assert_int_eq(rtnl_nh_set_res_group_idle_timer(nh, 15), 0); |
| 561 |
tmp32 = 0; |
| 562 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(nh, &tmp32), 0); |
| 563 |
ck_assert_int_eq(tmp32, 15U); |
| 564 |
ck_assert_int_eq(rtnl_nh_get_res_group_idle_timer(nh, NULL), |
| 565 |
-NLE_INVAL); |
| 566 |
|
| 567 |
/* Unbalanced timer: missing before set, then set/get; out NULL negative */ |
| 568 |
tmp32 = 0; |
| 569 |
ck_assert_int_eq(rtnl_nh_get_res_group_unbalanced_timer(nh, &tmp32), |
| 570 |
-NLE_MISSING_ATTR); |
| 571 |
ck_assert_int_eq(rtnl_nh_set_res_group_unbalanced_timer(nh, 25), 0); |
| 572 |
tmp32 = 0; |
| 573 |
ck_assert_int_eq(rtnl_nh_get_res_group_unbalanced_timer(nh, &tmp32), 0); |
| 574 |
ck_assert_int_eq(tmp32, 25U); |
| 575 |
ck_assert_int_eq(rtnl_nh_get_res_group_unbalanced_timer(nh, NULL), |
| 576 |
-NLE_INVAL); |
| 577 |
} |
| 578 |
END_TEST |
| 579 |
|
| 580 |
/* Userspace tests for MPLS encap on rtnl_nh */ |
| 581 |
|
| 582 |
START_TEST(test_api_encap_mpls_set_get) |
| 583 |
{ |
| 584 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 585 |
struct rtnl_nh_encap *encap = NULL; |
| 586 |
struct rtnl_nh_encap *got = NULL; |
| 587 |
_nl_auto_nl_addr struct nl_addr *labels = NULL; |
| 588 |
|
| 589 |
/* Allocate nh and an encap container */ |
| 590 |
nh = rtnl_nh_alloc(); |
| 591 |
ck_assert_ptr_nonnull(nh); |
| 592 |
|
| 593 |
/* Negative: NULL nh */ |
| 594 |
encap = rtnl_nh_encap_alloc(); |
| 595 |
ck_assert_ptr_nonnull(encap); |
| 596 |
/* This will free encap */ |
| 597 |
ck_assert_int_eq(rtnl_nh_set_encap(NULL, encap), -NLE_INVAL); |
| 598 |
|
| 599 |
encap = rtnl_nh_encap_alloc(); |
| 600 |
ck_assert_ptr_nonnull(encap); |
| 601 |
|
| 602 |
/* "empty" encap (no type set) cannot be assigned. */ |
| 603 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, encap), -NLE_INVAL); |
| 604 |
ck_assert_ptr_eq(rtnl_nh_get_encap_mpls_dst(rtnl_nh_get_encap(nh)), |
| 605 |
NULL); |
| 606 |
ck_assert_uint_eq(rtnl_nh_get_encap_mpls_ttl(rtnl_nh_get_encap(nh)), |
| 607 |
-NLE_INVAL); |
| 608 |
/* Type getter negatives */ |
| 609 |
ck_assert_int_eq(rtnl_nh_encap_get_type(NULL), -NLE_INVAL); |
| 610 |
ck_assert_int_eq(rtnl_nh_encap_get_type(rtnl_nh_get_encap(nh)), |
| 611 |
-NLE_INVAL); |
| 612 |
|
| 613 |
encap = rtnl_nh_encap_alloc(); |
| 614 |
ck_assert_ptr_nonnull(encap); |
| 615 |
/* Now build a valid MPLS encap: push label 100 with TTL 64 */ |
| 616 |
ck_assert_ptr_null(rtnl_nh_get_encap_mpls_dst(encap)); |
| 617 |
ck_assert_int_eq(rtnl_nh_get_encap_mpls_ttl(encap), -NLE_INVAL); |
| 618 |
ck_assert_int_eq(nl_addr_parse("100", AF_MPLS, &labels), 0); |
| 619 |
ck_assert_int_eq(rtnl_nh_encap_mpls(encap, labels, 64), 0); |
| 620 |
|
| 621 |
/* Attach and retrieve */ |
| 622 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, encap), 0); |
| 623 |
got = rtnl_nh_get_encap(nh); |
| 624 |
ck_assert_ptr_nonnull(got); |
| 625 |
ck_assert_int_eq(rtnl_nh_encap_get_type(got), LWTUNNEL_ENCAP_MPLS); |
| 626 |
|
| 627 |
/* Access MPLS-specific getters */ |
| 628 |
ck_assert_ptr_nonnull(rtnl_nh_get_encap_mpls_dst(got)); |
| 629 |
ck_assert_uint_eq(rtnl_nh_get_encap_mpls_ttl(got), 64); |
| 630 |
|
| 631 |
/* Clear encap */ |
| 632 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, NULL), 0); |
| 633 |
ck_assert_ptr_eq(rtnl_nh_get_encap(nh), NULL); |
| 634 |
} |
| 635 |
END_TEST |
| 636 |
|
| 637 |
/* Userspace tests for IPv6 encap on rtnl_nh (set/get + wrong-type negatives) */ |
| 638 |
|
| 639 |
START_TEST(test_api_encap_ip6) |
| 640 |
{ |
| 641 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 642 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 643 |
struct rtnl_nh_encap *got = NULL; |
| 644 |
struct nl_addr *got_dst = NULL; |
| 645 |
struct nl_addr *got_src = NULL; |
| 646 |
_nl_auto_nl_addr struct nl_addr *dst6 = NULL; |
| 647 |
_nl_auto_nl_addr struct nl_addr *src6 = NULL; |
| 648 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap_mpls = NULL; |
| 649 |
_nl_auto_nl_addr struct nl_addr *label = NULL; |
| 650 |
uint64_t id_val; |
| 651 |
uint16_t flags; |
| 652 |
|
| 653 |
/* Allocate nh and an encap container */ |
| 654 |
nh = rtnl_nh_alloc(); |
| 655 |
ck_assert_ptr_nonnull(nh); |
| 656 |
|
| 657 |
/* Negative: NULL nh */ |
| 658 |
encap = rtnl_nh_encap_alloc(); |
| 659 |
ck_assert_ptr_nonnull(encap); |
| 660 |
|
| 661 |
/* This will free encap */ |
| 662 |
ck_assert_int_eq(rtnl_nh_set_encap(NULL, _nl_steal_pointer(&encap)), |
| 663 |
-NLE_INVAL); |
| 664 |
|
| 665 |
/* Allocate a fresh encap after negative test freed the previous one */ |
| 666 |
encap = rtnl_nh_encap_alloc(); |
| 667 |
ck_assert_ptr_nonnull(encap); |
| 668 |
|
| 669 |
/* Now build a valid IPv6 encap */ |
| 670 |
ck_assert_int_eq(nl_addr_parse("2001:db8:1::1", AF_INET6, &dst6), 0); |
| 671 |
ck_assert_int_eq(rtnl_nh_encap_ip6(encap, dst6), 0); |
| 672 |
ck_assert_int_eq(nl_addr_parse("2001:db8:1::2", AF_INET6, &src6), 0); |
| 673 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_src(encap, src6), 0); |
| 674 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_hoplimit(encap, 32), 0); |
| 675 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_tc(encap, 0x2e), 0); |
| 676 |
flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ; |
| 677 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_flags(encap, flags), 0); |
| 678 |
|
| 679 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(encap), 0); |
| 680 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_id(encap, 0x1122334455667788ULL), |
| 681 |
0); |
| 682 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(encap), |
| 683 |
0x1122334455667788ULL); |
| 684 |
|
| 685 |
/* Attach and retrieve */ |
| 686 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 687 |
got = rtnl_nh_get_encap(nh); |
| 688 |
ck_assert_ptr_nonnull(got); |
| 689 |
ck_assert_int_eq(rtnl_nh_encap_get_type(got), LWTUNNEL_ENCAP_IP6); |
| 690 |
|
| 691 |
/* Access IPv6-specific getters */ |
| 692 |
got_dst = rtnl_nh_get_encap_ip6_dst(got); |
| 693 |
ck_assert_ptr_nonnull(got_dst); |
| 694 |
ck_assert_int_eq(nl_addr_cmp(got_dst, dst6), 0); |
| 695 |
got_src = rtnl_nh_get_encap_ip6_src(got); |
| 696 |
ck_assert_ptr_nonnull(got_src); |
| 697 |
ck_assert_int_eq(nl_addr_cmp(got_src, src6), 0); |
| 698 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_hoplimit(got), 32); |
| 699 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_tc(got), 0x2e); |
| 700 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_flags(got), flags); |
| 701 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(NULL), 0); |
| 702 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(got), 0x1122334455667788ULL); |
| 703 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_id(got, 0x8877665544332211ULL), |
| 704 |
0); |
| 705 |
id_val = rtnl_nh_get_encap_ip6_id(got); |
| 706 |
ck_assert(id_val == 0x8877665544332211ULL); |
| 707 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_id(got, 0), 0); |
| 708 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(got), 0); |
| 709 |
|
| 710 |
/* Clear/zero optional fields and verify getters */ |
| 711 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_hoplimit(got, 0), 0); |
| 712 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_tc(got, 0), 0); |
| 713 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_hoplimit(got), 0); |
| 714 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_tc(got), 0); |
| 715 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_flags(got, 0), 0); |
| 716 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_flags(got), 0); |
| 717 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_src(got, NULL), 0); |
| 718 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip6_src(got), NULL); |
| 719 |
|
| 720 |
/* Clear encap */ |
| 721 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, NULL), 0); |
| 722 |
ck_assert_ptr_eq(rtnl_nh_get_encap(nh), NULL); |
| 723 |
/* Type getter negative on NULL */ |
| 724 |
ck_assert_int_eq(rtnl_nh_encap_get_type(NULL), -NLE_INVAL); |
| 725 |
|
| 726 |
/* Negative tests for IPv6 setters on a non-IPv6 encap (wrong type) */ |
| 727 |
encap_mpls = rtnl_nh_encap_alloc(); |
| 728 |
ck_assert_ptr_nonnull(encap_mpls); |
| 729 |
ck_assert_int_eq(nl_addr_parse("100", AF_MPLS, &label), 0); |
| 730 |
ck_assert_int_eq(rtnl_nh_encap_mpls(encap_mpls, label, 64), 0); |
| 731 |
|
| 732 |
/* Now try IPv6-specific setters/getters on MPLS encap */ |
| 733 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_hoplimit(encap_mpls, 16), |
| 734 |
-NLE_INVAL); |
| 735 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_tc(encap_mpls, 1), -NLE_INVAL); |
| 736 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_src(encap_mpls, NULL), |
| 737 |
-NLE_INVAL); |
| 738 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_flags(encap_mpls, 1), |
| 739 |
-NLE_INVAL); |
| 740 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_id(encap_mpls, 1), -NLE_INVAL); |
| 741 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(encap_mpls), 0); |
| 742 |
/* And verify its type is MPLS */ |
| 743 |
ck_assert_int_eq(rtnl_nh_encap_get_type(encap_mpls), |
| 744 |
LWTUNNEL_ENCAP_MPLS); |
| 745 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip6_dst(encap_mpls), NULL); |
| 746 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip6_src(encap_mpls), NULL); |
| 747 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_hoplimit(encap_mpls), |
| 748 |
-NLE_INVAL); |
| 749 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_tc(encap_mpls), -NLE_INVAL); |
| 750 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_flags(encap_mpls), -NLE_INVAL); |
| 751 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(encap_mpls), 0); |
| 752 |
} |
| 753 |
END_TEST |
| 754 |
|
| 755 |
START_TEST(test_api_encap_ila) |
| 756 |
{ |
| 757 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 758 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 759 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap_mpls = NULL; |
| 760 |
struct rtnl_nh_encap *got = NULL; |
| 761 |
_nl_auto_nl_addr struct nl_addr *label = NULL; |
| 762 |
uint64_t locator = 0; |
| 763 |
|
| 764 |
nh = rtnl_nh_alloc(); |
| 765 |
ck_assert_ptr_nonnull(nh); |
| 766 |
|
| 767 |
encap = rtnl_nh_encap_alloc(); |
| 768 |
ck_assert_ptr_nonnull(encap); |
| 769 |
ck_assert_int_eq(rtnl_nh_encap_ila(encap, 0x1122334455667788ULL), 0); |
| 770 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_csum_mode(encap, |
| 771 |
ILA_CSUM_NEUTRAL_MAP), |
| 772 |
0); |
| 773 |
ck_assert_int_eq( |
| 774 |
rtnl_nh_set_encap_ila_ident_type(encap, ILA_ATYPE_LUID), 0); |
| 775 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_hook_type(encap, |
| 776 |
ILA_HOOK_ROUTE_OUTPUT), |
| 777 |
0); |
| 778 |
|
| 779 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 780 |
got = rtnl_nh_get_encap(nh); |
| 781 |
ck_assert_ptr_nonnull(got); |
| 782 |
ck_assert_int_eq(rtnl_nh_encap_get_type(got), LWTUNNEL_ENCAP_ILA); |
| 783 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_locator(got, &locator), 0); |
| 784 |
ck_assert_uint_eq(locator, 0x1122334455667788ULL); |
| 785 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_locator(got, NULL), -NLE_INVAL); |
| 786 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_csum_mode(got), |
| 787 |
ILA_CSUM_NEUTRAL_MAP); |
| 788 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_ident_type(got), ILA_ATYPE_LUID); |
| 789 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_hook_type(got), |
| 790 |
ILA_HOOK_ROUTE_OUTPUT); |
| 791 |
|
| 792 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_csum_mode(got, 0), 0); |
| 793 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_csum_mode(got), 0); |
| 794 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_ident_type(got, 0), 0); |
| 795 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_ident_type(got), 0); |
| 796 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_hook_type(got, 0), 0); |
| 797 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_hook_type(got), 0); |
| 798 |
|
| 799 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, NULL), 0); |
| 800 |
ck_assert_ptr_eq(rtnl_nh_get_encap(nh), NULL); |
| 801 |
|
| 802 |
encap_mpls = rtnl_nh_encap_alloc(); |
| 803 |
ck_assert_ptr_nonnull(encap_mpls); |
| 804 |
ck_assert_int_eq(nl_addr_parse("100", AF_MPLS, &label), 0); |
| 805 |
ck_assert_int_eq(rtnl_nh_encap_mpls(encap_mpls, label, 64), 0); |
| 806 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_csum_mode(encap_mpls, 1), |
| 807 |
-NLE_INVAL); |
| 808 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_locator(encap_mpls, &locator), |
| 809 |
-NLE_INVAL); |
| 810 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_csum_mode(encap_mpls), |
| 811 |
-NLE_INVAL); |
| 812 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_ident_type(encap_mpls), |
| 813 |
-NLE_INVAL); |
| 814 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_hook_type(encap_mpls), |
| 815 |
-NLE_INVAL); |
| 816 |
} |
| 817 |
END_TEST |
| 818 |
|
| 819 |
/* Userspace tests for IPv4 encap on rtnl_nh (set/get + wrong-type negatives) */ |
| 820 |
|
| 821 |
START_TEST(test_api_encap_ip) |
| 822 |
{ |
| 823 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 824 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 825 |
struct rtnl_nh_encap *got = NULL; |
| 826 |
struct nl_addr *got_dst = NULL; |
| 827 |
struct nl_addr *got_src = NULL; |
| 828 |
_nl_auto_nl_addr struct nl_addr *dst6 = NULL; |
| 829 |
_nl_auto_nl_addr struct nl_addr *src6 = NULL; |
| 830 |
_nl_auto_nl_addr struct nl_addr *dst4 = NULL; |
| 831 |
_nl_auto_nl_addr struct nl_addr *src4 = NULL; |
| 832 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap_mpls = NULL; |
| 833 |
_nl_auto_nl_addr struct nl_addr *label = NULL; |
| 834 |
uint64_t id = 0; |
| 835 |
|
| 836 |
nh = rtnl_nh_alloc(); |
| 837 |
ck_assert_ptr_nonnull(nh); |
| 838 |
|
| 839 |
encap = rtnl_nh_encap_alloc(); |
| 840 |
ck_assert_ptr_nonnull(encap); |
| 841 |
/* This will free encap */ |
| 842 |
ck_assert_int_eq(rtnl_nh_set_encap(NULL, _nl_steal_pointer(&encap)), |
| 843 |
-NLE_INVAL); |
| 844 |
|
| 845 |
encap = rtnl_nh_encap_alloc(); |
| 846 |
ck_assert_ptr_nonnull(encap); |
| 847 |
|
| 848 |
/* Family mismatch should be rejected */ |
| 849 |
ck_assert_int_eq(nl_addr_parse("2001:db8::1", AF_INET6, &dst6), 0); |
| 850 |
ck_assert_int_eq(rtnl_nh_encap_ip(encap, dst6), -NLE_INVAL); |
| 851 |
|
| 852 |
/* Build a valid IPv4 encap */ |
| 853 |
ck_assert_int_eq(nl_addr_parse("198.51.100.1", AF_INET, &dst4), 0); |
| 854 |
ck_assert_int_eq(rtnl_nh_encap_ip(encap, dst4), 0); |
| 855 |
ck_assert_int_eq(nl_addr_parse("2001:db8::2", AF_INET6, &src6), 0); |
| 856 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_src(encap, src6), -NLE_INVAL); |
| 857 |
ck_assert_int_eq(nl_addr_parse("198.51.100.2", AF_INET, &src4), 0); |
| 858 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_src(encap, src4), 0); |
| 859 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_id(encap, 0x123456789ULL), 0); |
| 860 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(encap), 0x123456789ULL); |
| 861 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_ttl(encap, 32), 0); |
| 862 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_tos(encap, 0x2e), 0); |
| 863 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_flags(encap, 0x3), 0); |
| 864 |
|
| 865 |
/* Attach and retrieve */ |
| 866 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 867 |
got = rtnl_nh_get_encap(nh); |
| 868 |
ck_assert_ptr_nonnull(got); |
| 869 |
ck_assert_int_eq(rtnl_nh_encap_get_type(got), LWTUNNEL_ENCAP_IP); |
| 870 |
|
| 871 |
/* Access IPv4-specific getters */ |
| 872 |
got_dst = rtnl_nh_get_encap_ip_dst(got); |
| 873 |
ck_assert_ptr_nonnull(got_dst); |
| 874 |
ck_assert_int_eq(nl_addr_cmp(got_dst, dst4), 0); |
| 875 |
got_src = rtnl_nh_get_encap_ip_src(got); |
| 876 |
ck_assert_ptr_nonnull(got_src); |
| 877 |
ck_assert_int_eq(nl_addr_cmp(got_src, src4), 0); |
| 878 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_ttl(got), 32); |
| 879 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_tos(got), 0x2e); |
| 880 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(NULL), 0); |
| 881 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(got), 0x123456789ULL); |
| 882 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_id(got, 0x987654321ULL), 0); |
| 883 |
id = rtnl_nh_get_encap_ip_id(got); |
| 884 |
ck_assert(id == 0x987654321ULL); |
| 885 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_flags(got), 0x3); |
| 886 |
|
| 887 |
/* Clear/zero optional fields and verify getters */ |
| 888 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_ttl(got, 0), 0); |
| 889 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_tos(got, 0), 0); |
| 890 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_id(got, 0), 0); |
| 891 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_flags(got, 0), 0); |
| 892 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_ttl(got), 0); |
| 893 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_tos(got), 0); |
| 894 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(got), 0); |
| 895 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_flags(got), 0); |
| 896 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_src(got, NULL), 0); |
| 897 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip_src(got), NULL); |
| 898 |
|
| 899 |
/* Clear encap */ |
| 900 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, NULL), 0); |
| 901 |
ck_assert_ptr_eq(rtnl_nh_get_encap(nh), NULL); |
| 902 |
/* Type getter negative on NULL */ |
| 903 |
ck_assert_int_eq(rtnl_nh_encap_get_type(NULL), -NLE_INVAL); |
| 904 |
|
| 905 |
/* Negative tests for IPv4 setters on a non-IPv4 encap (wrong type) */ |
| 906 |
encap_mpls = rtnl_nh_encap_alloc(); |
| 907 |
ck_assert_ptr_nonnull(encap_mpls); |
| 908 |
ck_assert_int_eq(nl_addr_parse("100", AF_MPLS, &label), 0); |
| 909 |
ck_assert_int_eq(rtnl_nh_encap_mpls(encap_mpls, label, 64), 0); |
| 910 |
|
| 911 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_ttl(encap_mpls, 16), -NLE_INVAL); |
| 912 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_tos(encap_mpls, 1), -NLE_INVAL); |
| 913 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_id(encap_mpls, 1), -NLE_INVAL); |
| 914 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_flags(encap_mpls, 1), -NLE_INVAL); |
| 915 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_src(encap_mpls, NULL), |
| 916 |
-NLE_INVAL); |
| 917 |
ck_assert_int_eq(rtnl_nh_encap_get_type(encap_mpls), |
| 918 |
LWTUNNEL_ENCAP_MPLS); |
| 919 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip_dst(encap_mpls), NULL); |
| 920 |
ck_assert_ptr_eq(rtnl_nh_get_encap_ip_src(encap_mpls), NULL); |
| 921 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_ttl(encap_mpls), -NLE_INVAL); |
| 922 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_tos(encap_mpls), -NLE_INVAL); |
| 923 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(encap_mpls), 0); |
| 924 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_flags(encap_mpls), -NLE_INVAL); |
| 925 |
} |
| 926 |
END_TEST |
| 927 |
|
| 928 |
/* Kernel round-trip tests for IPv6 encap on rtnl_nh */ |
| 929 |
|
| 930 |
START_TEST(test_kernel_roundtrip_encap_ip6) |
| 931 |
{ |
| 932 |
const char *IFNAME_DUMMY = "nh-dummy-encap6"; |
| 933 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 934 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 935 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 936 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 937 |
struct rtnl_nh *knh; |
| 938 |
struct rtnl_nh_encap *kencap; |
| 939 |
struct nl_addr *kdst = NULL; |
| 940 |
struct nl_addr *ksrc = NULL; |
| 941 |
_nl_auto_nl_addr struct nl_addr *gw6 = NULL; |
| 942 |
_nl_auto_nl_addr struct nl_addr *dst6 = NULL; |
| 943 |
_nl_auto_nl_addr struct nl_addr *src6 = NULL; |
| 944 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 945 |
uint16_t flags; |
| 946 |
int ifindex_dummy; |
| 947 |
|
| 948 |
if (_nltst_skip_no_netns()) |
| 949 |
return; |
| 950 |
|
| 951 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 952 |
|
| 953 |
/* Create underlay, bring it up and assign an IPv6 address */ |
| 954 |
auto_del_dummy = IFNAME_DUMMY; |
| 955 |
_nltst_add_dummy_v6_with_addr(sk, IFNAME_DUMMY, &ifindex_dummy, |
| 956 |
"2001:db8::2", 64); |
| 957 |
|
| 958 |
/* Build nexthop: IPv6 encap with dst/src/hoplimit/tc/flags and IPv6 gw */ |
| 959 |
nh = rtnl_nh_alloc(); |
| 960 |
ck_assert_ptr_nonnull(nh); |
| 961 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3201), 0); |
| 962 |
|
| 963 |
ck_assert_int_eq(nl_addr_parse("2001:db8::1", AF_INET6, &gw6), 0); |
| 964 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw6), 0); |
| 965 |
|
| 966 |
encap = rtnl_nh_encap_alloc(); |
| 967 |
ck_assert_ptr_nonnull(encap); |
| 968 |
ck_assert_int_eq(nl_addr_parse("2001:db8:1::1", AF_INET6, &dst6), 0); |
| 969 |
ck_assert_int_eq(rtnl_nh_encap_ip6(encap, dst6), 0); |
| 970 |
ck_assert_int_eq(nl_addr_parse("2001:db8:1::2", AF_INET6, &src6), 0); |
| 971 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_src(encap, src6), 0); |
| 972 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_hoplimit(encap, 32), 0); |
| 973 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_tc(encap, 0x2e), 0); |
| 974 |
flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ; |
| 975 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_flags(encap, flags), 0); |
| 976 |
ck_assert_int_eq(rtnl_nh_set_encap_ip6_id(encap, 0x1122334455667788ULL), |
| 977 |
0); |
| 978 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 979 |
|
| 980 |
/* Set required attributes and add */ |
| 981 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 982 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET6), 0); |
| 983 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), 0); |
| 984 |
|
| 985 |
/* Query and verify */ |
| 986 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 987 |
knh = rtnl_nh_get(cache, 3201); |
| 988 |
ck_assert_ptr_nonnull(knh); |
| 989 |
ck_assert_int_eq(rtnl_nh_get_id(knh), 3201); |
| 990 |
ck_assert_int_eq(rtnl_nh_get_oif(knh), ifindex_dummy); |
| 991 |
|
| 992 |
kencap = rtnl_nh_get_encap(knh); |
| 993 |
ck_assert_ptr_nonnull(kencap); |
| 994 |
ck_assert_int_eq(rtnl_nh_encap_get_type(kencap), LWTUNNEL_ENCAP_IP6); |
| 995 |
kdst = rtnl_nh_get_encap_ip6_dst(kencap); |
| 996 |
ck_assert_ptr_nonnull(kdst); |
| 997 |
ck_assert_int_eq(nl_addr_cmp(kdst, dst6), 0); |
| 998 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_hoplimit(kencap), 32); |
| 999 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_tc(kencap), 0x2e); |
| 1000 |
ksrc = rtnl_nh_get_encap_ip6_src(kencap); |
| 1001 |
ck_assert_ptr_nonnull(ksrc); |
| 1002 |
ck_assert_int_eq(nl_addr_cmp(ksrc, src6), 0); |
| 1003 |
ck_assert_int_eq(rtnl_nh_get_encap_ip6_flags(kencap), flags); |
| 1004 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip6_id(kencap), |
| 1005 |
0x1122334455667788ULL); |
| 1006 |
} |
| 1007 |
END_TEST |
| 1008 |
|
| 1009 |
START_TEST(test_kernel_roundtrip_encap_ila) |
| 1010 |
{ |
| 1011 |
const char *IFNAME_DUMMY = "encap-ila"; |
| 1012 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 1013 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 1014 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 1015 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 1016 |
_nl_auto_nl_addr struct nl_addr *gw6 = NULL; |
| 1017 |
struct rtnl_nh *knh; |
| 1018 |
struct rtnl_nh_encap *kencap; |
| 1019 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 1020 |
uint64_t locator = 0; |
| 1021 |
int ifindex_dummy; |
| 1022 |
int ret; |
| 1023 |
|
| 1024 |
if (_nltst_skip_no_netns()) |
| 1025 |
return; |
| 1026 |
|
| 1027 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 1028 |
|
| 1029 |
/* Create underlay, bring it up and assign an IPv6 address */ |
| 1030 |
auto_del_dummy = IFNAME_DUMMY; |
| 1031 |
_nltst_add_dummy_v6_with_addr(sk, IFNAME_DUMMY, &ifindex_dummy, |
| 1032 |
"2001:db8:2::2", 64); |
| 1033 |
|
| 1034 |
nh = rtnl_nh_alloc(); |
| 1035 |
ck_assert_ptr_nonnull(nh); |
| 1036 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3401), 0); |
| 1037 |
|
| 1038 |
ck_assert_int_eq(nl_addr_parse("2001:db8:2::1", AF_INET6, &gw6), 0); |
| 1039 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw6), 0); |
| 1040 |
|
| 1041 |
encap = rtnl_nh_encap_alloc(); |
| 1042 |
ck_assert_ptr_nonnull(encap); |
| 1043 |
ck_assert_int_eq(rtnl_nh_encap_ila(encap, 0x1122334455667788ULL), 0); |
| 1044 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_csum_mode(encap, |
| 1045 |
ILA_CSUM_NEUTRAL_MAP), |
| 1046 |
0); |
| 1047 |
ck_assert_int_eq( |
| 1048 |
rtnl_nh_set_encap_ila_ident_type(encap, ILA_ATYPE_LUID), 0); |
| 1049 |
ck_assert_int_eq(rtnl_nh_set_encap_ila_hook_type(encap, |
| 1050 |
ILA_HOOK_ROUTE_OUTPUT), |
| 1051 |
0); |
| 1052 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 1053 |
|
| 1054 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 1055 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET6), 0); |
| 1056 |
ret = rtnl_nh_add(sk, nh, NLM_F_CREATE); |
| 1057 |
if (ret == -NLE_OPNOTSUPP) { |
| 1058 |
/* ila module is not loaded - skipping */ |
| 1059 |
return; |
| 1060 |
} |
| 1061 |
ck_assert_int_eq(ret, 0); |
| 1062 |
|
| 1063 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 1064 |
knh = rtnl_nh_get(cache, 3401); |
| 1065 |
ck_assert_ptr_nonnull(knh); |
| 1066 |
ck_assert_int_eq(rtnl_nh_get_id(knh), 3401); |
| 1067 |
ck_assert_int_eq(rtnl_nh_get_oif(knh), ifindex_dummy); |
| 1068 |
|
| 1069 |
kencap = rtnl_nh_get_encap(knh); |
| 1070 |
ck_assert_ptr_nonnull(kencap); |
| 1071 |
ck_assert_int_eq(rtnl_nh_encap_get_type(kencap), LWTUNNEL_ENCAP_ILA); |
| 1072 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_locator(kencap, &locator), 0); |
| 1073 |
ck_assert_uint_eq(locator, 0x1122334455667788ULL); |
| 1074 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_csum_mode(kencap), |
| 1075 |
ILA_CSUM_NEUTRAL_MAP); |
| 1076 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_ident_type(kencap), |
| 1077 |
ILA_ATYPE_LUID); |
| 1078 |
ck_assert_int_eq(rtnl_nh_get_encap_ila_hook_type(kencap), |
| 1079 |
ILA_HOOK_ROUTE_OUTPUT); |
| 1080 |
} |
| 1081 |
END_TEST |
| 1082 |
|
| 1083 |
/* Kernel round-trip tests for IPv4 encap on rtnl_nh */ |
| 1084 |
|
| 1085 |
START_TEST(test_kernel_roundtrip_encap_ip) |
| 1086 |
{ |
| 1087 |
const char *IFNAME_DUMMY = "nh-dummy-encap4"; |
| 1088 |
_nltst_auto_delete_link const char *auto_del_dummy = NULL; |
| 1089 |
_nl_auto_nl_socket struct nl_sock *sk = NULL; |
| 1090 |
_nl_auto_nl_cache struct nl_cache *cache = NULL; |
| 1091 |
_nl_auto_rtnl_nh struct rtnl_nh *nh = NULL; |
| 1092 |
struct rtnl_nh *knh; |
| 1093 |
struct rtnl_nh_encap *kencap; |
| 1094 |
struct nl_addr *kdst = NULL; |
| 1095 |
struct nl_addr *ksrc = NULL; |
| 1096 |
_nl_auto_nl_addr struct nl_addr *gw4 = NULL; |
| 1097 |
_nl_auto_nl_addr struct nl_addr *dst4 = NULL; |
| 1098 |
_nl_auto_nl_addr struct nl_addr *src4 = NULL; |
| 1099 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 1100 |
int ifindex_dummy; |
| 1101 |
uint64_t id = 0; |
| 1102 |
|
| 1103 |
if (_nltst_skip_no_netns()) |
| 1104 |
return; |
| 1105 |
|
| 1106 |
sk = _nltst_socket(NETLINK_ROUTE); |
| 1107 |
|
| 1108 |
/* Create underlay and assign an IPv4 address */ |
| 1109 |
auto_del_dummy = IFNAME_DUMMY; |
| 1110 |
_nltst_add_dummy_v4_with_addr(sk, IFNAME_DUMMY, &ifindex_dummy, |
| 1111 |
"192.0.2.2", 24); |
| 1112 |
|
| 1113 |
/* Build nexthop: IPv4 encap with dst/src/ttl/tos and IPv4 gw */ |
| 1114 |
nh = rtnl_nh_alloc(); |
| 1115 |
ck_assert_ptr_nonnull(nh); |
| 1116 |
ck_assert_int_eq(rtnl_nh_set_id(nh, 3301), 0); |
| 1117 |
|
| 1118 |
ck_assert_int_eq(nl_addr_parse("192.0.2.1", AF_INET, &gw4), 0); |
| 1119 |
ck_assert_int_eq(rtnl_nh_set_gateway(nh, gw4), 0); |
| 1120 |
|
| 1121 |
encap = rtnl_nh_encap_alloc(); |
| 1122 |
ck_assert_ptr_nonnull(encap); |
| 1123 |
ck_assert_int_eq(nl_addr_parse("198.51.100.1", AF_INET, &dst4), 0); |
| 1124 |
ck_assert_int_eq(rtnl_nh_encap_ip(encap, dst4), 0); |
| 1125 |
ck_assert_int_eq(nl_addr_parse("198.51.100.2", AF_INET, &src4), 0); |
| 1126 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_src(encap, src4), 0); |
| 1127 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_id(encap, 0xABCDEFULL), 0); |
| 1128 |
ck_assert_uint_eq(rtnl_nh_get_encap_ip_id(encap), 0xABCDEFULL); |
| 1129 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_ttl(encap, 32), 0); |
| 1130 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_tos(encap, 0x2e), 0); |
| 1131 |
ck_assert_int_eq(rtnl_nh_set_encap_ip_flags(encap, 0x5), 0); |
| 1132 |
ck_assert_int_eq(rtnl_nh_set_encap(nh, _nl_steal_pointer(&encap)), 0); |
| 1133 |
|
| 1134 |
/* Set required attributes and add */ |
| 1135 |
ck_assert_int_eq(rtnl_nh_set_oif(nh, (uint32_t)ifindex_dummy), 0); |
| 1136 |
ck_assert_int_eq(rtnl_nh_set_family(nh, AF_INET), 0); |
| 1137 |
ck_assert_int_eq(rtnl_nh_add(sk, nh, NLM_F_CREATE), 0); |
| 1138 |
|
| 1139 |
/* Query and verify */ |
| 1140 |
ck_assert_int_eq(rtnl_nh_alloc_cache(sk, AF_UNSPEC, &cache), 0); |
| 1141 |
knh = rtnl_nh_get(cache, 3301); |
| 1142 |
ck_assert_ptr_nonnull(knh); |
| 1143 |
ck_assert_int_eq(rtnl_nh_get_id(knh), 3301); |
| 1144 |
ck_assert_int_eq(rtnl_nh_get_oif(knh), ifindex_dummy); |
| 1145 |
|
| 1146 |
kencap = rtnl_nh_get_encap(knh); |
| 1147 |
ck_assert_ptr_nonnull(kencap); |
| 1148 |
ck_assert_int_eq(rtnl_nh_encap_get_type(kencap), LWTUNNEL_ENCAP_IP); |
| 1149 |
kdst = rtnl_nh_get_encap_ip_dst(kencap); |
| 1150 |
ck_assert_ptr_nonnull(kdst); |
| 1151 |
ck_assert_int_eq(nl_addr_cmp(kdst, dst4), 0); |
| 1152 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_ttl(kencap), 32); |
| 1153 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_tos(kencap), 0x2e); |
| 1154 |
id = rtnl_nh_get_encap_ip_id(kencap); |
| 1155 |
ck_assert(id == 0xABCDEFULL); |
| 1156 |
ck_assert_int_eq(rtnl_nh_get_encap_ip_flags(kencap), 0x5); |
| 1157 |
ksrc = rtnl_nh_get_encap_ip_src(kencap); |
| 1158 |
ck_assert_ptr_nonnull(ksrc); |
| 1159 |
ck_assert_int_eq(nl_addr_cmp(ksrc, src4), 0); |
| 1160 |
} |
| 1161 |
END_TEST |
| 1162 |
|
| 1163 |
Suite *make_nl_route_nh_suite(void) |
| 1164 |
{ |
| 1165 |
Suite *suite = suite_create("route-nh"); |
| 1166 |
TCase *tc_api = tcase_create("Userspace-API"); |
| 1167 |
TCase *tc_kernel = tcase_create("Kernel"); |
| 1168 |
|
| 1169 |
/* Comprehensive API setter/getter test (userspace only) */ |
| 1170 |
tcase_add_test(tc_api, test_api_set_get_all); |
| 1171 |
/* Userspace encap tests */ |
| 1172 |
tcase_add_test(tc_api, test_api_encap_mpls_set_get); |
| 1173 |
/* Userspace IPv6 encap tests */ |
| 1174 |
tcase_add_test(tc_api, test_api_encap_ip6); |
| 1175 |
/* Userspace ILA encap tests */ |
| 1176 |
tcase_add_test(tc_api, test_api_encap_ila); |
| 1177 |
/* Userspace IPv4 encap tests */ |
| 1178 |
tcase_add_test(tc_api, test_api_encap_ip); |
| 1179 |
suite_add_tcase(suite, tc_api); |
| 1180 |
|
| 1181 |
/* Kernel round-trip – needs private netns */ |
| 1182 |
tcase_add_checked_fixture(tc_kernel, nltst_netns_fixture_setup, |
| 1183 |
nltst_netns_fixture_teardown); |
| 1184 |
/* Exercise add+parse for all possible APIs: basic v4 gw, oif-only, |
| 1185 |
* multipath groups, resilient groups with parameters. |
| 1186 |
*/ |
| 1187 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_basic_v4); |
| 1188 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_oif_only); |
| 1189 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_group_mpath); |
| 1190 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_group_resilient); |
| 1191 |
/* Encap (MPLS) on rtnl_nh */ |
| 1192 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_encap_mpls); |
| 1193 |
/* Encap (IPv6) on rtnl_nh */ |
| 1194 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_encap_ip6); |
| 1195 |
/* Encap (ILA) on rtnl_nh */ |
| 1196 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_encap_ila); |
| 1197 |
/* Encap (IPv4) on rtnl_nh */ |
| 1198 |
tcase_add_test(tc_kernel, test_kernel_roundtrip_encap_ip); |
| 1199 |
/* Negative tests: kernel should reject invalid nexthops */ |
| 1200 |
tcase_add_test(tc_kernel, test_kernel_negative_mismatched_gw_family); |
| 1201 |
tcase_add_test(tc_kernel, test_kernel_negative_group_without_entries); |
| 1202 |
tcase_add_test(tc_kernel, test_kernel_negative_gateway_without_oif); |
| 1203 |
suite_add_tcase(suite, tc_kernel); |
| 1204 |
|
| 1205 |
return suite; |
| 1206 |
} |