1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020 Cloudflare
/*
* Tests for sockmap/sockhash holding kTLS sockets.
*/
#include <error.h>
#include <netinet/tcp.h>
#include <linux/tls.h>
#include "test_progs.h"
#include "sockmap_helpers.h"
#include "test_skmsg_load_helpers.skel.h"
#include "test_sockmap_ktls.skel.h"
#define MAX_TEST_NAME 80
#define TCP_ULP 31
static int init_ktls_pairs(int c, int p)
{
int err;
struct tls12_crypto_info_aes_gcm_128 crypto_rx;
struct tls12_crypto_info_aes_gcm_128 crypto_tx;
err = setsockopt(c, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls"));
if (!ASSERT_OK(err, "setsockopt(TCP_ULP)"))
goto out;
err = setsockopt(p, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls"));
if (!ASSERT_OK(err, "setsockopt(TCP_ULP)"))
goto out;
memset(&crypto_rx, 0, sizeof(crypto_rx));
memset(&crypto_tx, 0, sizeof(crypto_tx));
crypto_rx.info.version = TLS_1_2_VERSION;
crypto_tx.info.version = TLS_1_2_VERSION;
crypto_rx.info.cipher_type = TLS_CIPHER_AES_GCM_128;
crypto_tx.info.cipher_type = TLS_CIPHER_AES_GCM_128;
err = setsockopt(c, SOL_TLS, TLS_TX, &crypto_tx, sizeof(crypto_tx));
if (!ASSERT_OK(err, "setsockopt(TLS_TX)"))
goto out;
err = setsockopt(p, SOL_TLS, TLS_RX, &crypto_rx, sizeof(crypto_rx));
if (!ASSERT_OK(err, "setsockopt(TLS_RX)"))
goto out;
return 0;
out:
return -1;
}
static int create_ktls_pairs(int family, int sotype, int *c, int *p)
{
int err;
err = create_pair(family, sotype, c, p);
if (!ASSERT_OK(err, "create_pair()"))
return -1;
err = init_ktls_pairs(*c, *p);
if (!ASSERT_OK(err, "init_ktls_pairs(c, p)"))
return -1;
return 0;
}
static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map)
{
struct sockaddr_storage addr = {};
socklen_t len = sizeof(addr);
struct sockaddr_in6 *v6;
struct sockaddr_in *v4;
int err, s, zero = 0;
switch (family) {
case AF_INET:
v4 = (struct sockaddr_in *)&addr;
v4->sin_family = AF_INET;
break;
case AF_INET6:
v6 = (struct sockaddr_in6 *)&addr;
v6->sin6_family = AF_INET6;
break;
default:
PRINT_FAIL("unsupported socket family %d", family);
return;
}
s = socket(family, SOCK_STREAM, 0);
if (!ASSERT_GE(s, 0, "socket"))
return;
err = bind(s, (struct sockaddr *)&addr, len);
if (!ASSERT_OK(err, "bind"))
goto close;
err = getsockname(s, (struct sockaddr *)&addr, &len);
if (!ASSERT_OK(err, "getsockname"))
goto close;
err = connect(s, (struct sockaddr *)&addr, len);
if (!ASSERT_OK(err, "connect"))
goto close;
/* save sk->sk_prot and set it to tls_prots */
err = setsockopt(s, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls"));
if (!ASSERT_OK(err, "setsockopt(TCP_ULP)"))
goto close;
/* sockmap update should not affect saved sk_prot */
err = bpf_map_update_elem(map, &zero, &s, BPF_ANY);
if (!ASSERT_ERR(err, "sockmap update elem"))
goto close;
/* call sk->sk_prot->setsockopt to dispatch to saved sk_prot */
err = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &zero, sizeof(zero));
ASSERT_OK(err, "setsockopt(TCP_NODELAY)");
close:
close(s);
}
static const char *fmt_test_name(const char *subtest_name, int family,
enum bpf_map_type map_type)
{
const char *map_type_str = BPF_MAP_TYPE_SOCKMAP ? "SOCKMAP" : "SOCKHASH";
const char *family_str = AF_INET ? "IPv4" : "IPv6";
static char test_name[MAX_TEST_NAME];
snprintf(test_name, MAX_TEST_NAME,
"sockmap_ktls %s %s %s",
subtest_name, family_str, map_type_str);
return test_name;
}
static void test_sockmap_ktls_offload(int family, int sotype)
{
int err;
int c = 0, p = 0, sent, recvd;
char msg[12] = "hello world\0";
char rcv[13];
err = create_ktls_pairs(family, sotype, &c, &p);
if (!ASSERT_OK(err, "create_ktls_pairs()"))
goto out;
sent = send(c, msg, sizeof(msg), 0);
if (!ASSERT_OK(err, "send(msg)"))
goto out;
recvd = recv(p, rcv, sizeof(rcv), 0);
if (!ASSERT_OK(err, "recv(msg)") ||
!ASSERT_EQ(recvd, sent, "length mismatch"))
goto out;
ASSERT_OK(memcmp(msg, rcv, sizeof(msg)), "data mismatch");
out:
if (c)
close(c);
if (p)
close(p);
}
static void test_sockmap_ktls_tx_cork(int family, int sotype, bool push)
{
int err, off;
int i, j;
int start_push = 0, push_len = 0;
int c = 0, p = 0, one = 1, sent, recvd;
int prog_fd, map_fd;
char msg[12] = "hello world\0";
char rcv[20] = {0};
struct test_sockmap_ktls *skel;
skel = test_sockmap_ktls__open_and_load();
if (!ASSERT_TRUE(skel, "open ktls skel"))
return;
err = create_pair(family, sotype, &c, &p);
if (!ASSERT_OK(err, "create_pair()"))
goto out;
prog_fd = bpf_program__fd(skel->progs.prog_sk_policy);
map_fd = bpf_map__fd(skel->maps.sock_map);
err = bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0);
if (!ASSERT_OK(err, "bpf_prog_attach sk msg"))
goto out;
err = bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update_elem(c)"))
goto out;
err = init_ktls_pairs(c, p);
if (!ASSERT_OK(err, "init_ktls_pairs(c, p)"))
goto out;
skel->bss->cork_byte = sizeof(msg);
if (push) {
start_push = 1;
push_len = 2;
}
skel->bss->push_start = start_push;
skel->bss->push_end = push_len;
off = sizeof(msg) / 2;
sent = send(c, msg, off, 0);
if (!ASSERT_EQ(sent, off, "send(msg)"))
goto out;
recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, 1);
if (!ASSERT_EQ(-1, recvd, "expected no data"))
goto out;
/* send remaining msg */
sent = send(c, msg + off, sizeof(msg) - off, 0);
if (!ASSERT_EQ(sent, sizeof(msg) - off, "send remaining data"))
goto out;
recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, 1);
if (!ASSERT_OK(err, "recv(msg)") ||
!ASSERT_EQ(recvd, sizeof(msg) + push_len, "check length mismatch"))
goto out;
for (i = 0, j = 0; i < recvd;) {
/* skip checking the data that has been pushed in */
if (i >= start_push && i <= start_push + push_len - 1) {
i++;
continue;
}
if (!ASSERT_EQ(rcv[i], msg[j], "data mismatch"))
goto out;
i++;
j++;
}
out:
if (c)
close(c);
if (p)
close(p);
test_sockmap_ktls__destroy(skel);
}
static void test_sockmap_ktls_tx_no_buf(int family, int sotype, bool push)
{
int c = -1, p = -1, one = 1, two = 2;
struct test_sockmap_ktls *skel;
unsigned char *data = NULL;
struct msghdr msg = {0};
struct iovec iov[2];
int prog_fd, map_fd;
int txrx_buf = 1024;
int iov_length = 8192;
int err;
skel = test_sockmap_ktls__open_and_load();
if (!ASSERT_TRUE(skel, "open ktls skel"))
return;
err = create_pair(family, sotype, &c, &p);
if (!ASSERT_OK(err, "create_pair()"))
goto out;
err = setsockopt(c, SOL_SOCKET, SO_RCVBUFFORCE, &txrx_buf, sizeof(int));
err |= setsockopt(p, SOL_SOCKET, SO_SNDBUFFORCE, &txrx_buf, sizeof(int));
if (!ASSERT_OK(err, "set buf limit"))
goto out;
prog_fd = bpf_program__fd(skel->progs.prog_sk_policy_redir);
map_fd = bpf_map__fd(skel->maps.sock_map);
err = bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0);
if (!ASSERT_OK(err, "bpf_prog_attach sk msg"))
goto out;
err = bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update_elem(c)"))
goto out;
err = bpf_map_update_elem(map_fd, &two, &p, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update_elem(p)"))
goto out;
skel->bss->apply_bytes = 1024;
err = init_ktls_pairs(c, p);
if (!ASSERT_OK(err, "init_ktls_pairs(c, p)"))
goto out;
data = calloc(iov_length, sizeof(char));
if (!data)
goto out;
iov[0].iov_base = data;
iov[0].iov_len = iov_length;
iov[1].iov_base = data;
iov[1].iov_len = iov_length;
msg.msg_iov = iov;
msg.msg_iovlen = 2;
for (;;) {
err = sendmsg(c, &msg, MSG_DONTWAIT);
if (err <= 0)
break;
}
out:
if (data)
free(data);
if (c != -1)
close(c);
if (p != -1)
close(p);
test_sockmap_ktls__destroy(skel);
}
static void run_tests(int family, enum bpf_map_type map_type)
{
int map;
map = bpf_map_create(map_type, NULL, sizeof(int), sizeof(int), 1, NULL);
if (!ASSERT_GE(map, 0, "bpf_map_create"))
return;
if (test__start_subtest(fmt_test_name("update_fails_when_sock_has_ulp", family, map_type)))
test_sockmap_ktls_update_fails_when_sock_has_ulp(family, map);
close(map);
}
static void run_ktls_test(int family, int sotype)
{
if (test__start_subtest("tls simple offload"))
test_sockmap_ktls_offload(family, sotype);
if (test__start_subtest("tls tx cork"))
test_sockmap_ktls_tx_cork(family, sotype, false);
if (test__start_subtest("tls tx cork with push"))
test_sockmap_ktls_tx_cork(family, sotype, true);
if (test__start_subtest("tls tx egress with no buf"))
test_sockmap_ktls_tx_no_buf(family, sotype, true);
}
void test_sockmap_ktls(void)
{
run_tests(AF_INET, BPF_MAP_TYPE_SOCKMAP);
run_tests(AF_INET, BPF_MAP_TYPE_SOCKHASH);
run_tests(AF_INET6, BPF_MAP_TYPE_SOCKMAP);
run_tests(AF_INET6, BPF_MAP_TYPE_SOCKHASH);
run_ktls_test(AF_INET, SOCK_STREAM);
run_ktls_test(AF_INET6, SOCK_STREAM);
}
|