xref: /linux/tools/testing/selftests/net/tls.c (revision 69fe8ec4f673b5b35a34718dc39a5131bfc55e36)
17f657d5bSDave Watson // SPDX-License-Identifier: GPL-2.0
27f657d5bSDave Watson 
37f657d5bSDave Watson #define _GNU_SOURCE
47f657d5bSDave Watson 
57f657d5bSDave Watson #include <arpa/inet.h>
67f657d5bSDave Watson #include <errno.h>
77f657d5bSDave Watson #include <error.h>
87f657d5bSDave Watson #include <fcntl.h>
97f657d5bSDave Watson #include <poll.h>
107f657d5bSDave Watson #include <stdio.h>
117f657d5bSDave Watson #include <stdlib.h>
127f657d5bSDave Watson #include <unistd.h>
137f657d5bSDave Watson 
147f657d5bSDave Watson #include <linux/tls.h>
157f657d5bSDave Watson #include <linux/tcp.h>
167f657d5bSDave Watson #include <linux/socket.h>
177f657d5bSDave Watson 
1823fcb62bSJakub Kicinski #include <sys/epoll.h>
197f657d5bSDave Watson #include <sys/types.h>
207f657d5bSDave Watson #include <sys/sendfile.h>
217f657d5bSDave Watson #include <sys/socket.h>
227f657d5bSDave Watson #include <sys/stat.h>
237f657d5bSDave Watson 
247f657d5bSDave Watson #include "../kselftest_harness.h"
257f657d5bSDave Watson 
267f657d5bSDave Watson #define TLS_PAYLOAD_MAX_LEN 16384
277f657d5bSDave Watson #define SOL_TLS 282
287f657d5bSDave Watson 
29d113c395SMagali Lemes static int fips_enabled;
30d113c395SMagali Lemes 
31291c53e4SJakub Kicinski struct tls_crypto_info_keys {
32291c53e4SJakub Kicinski 	union {
33f27ad62fSSabrina Dubroca 		struct tls_crypto_info crypto_info;
34291c53e4SJakub Kicinski 		struct tls12_crypto_info_aes_gcm_128 aes128;
35291c53e4SJakub Kicinski 		struct tls12_crypto_info_chacha20_poly1305 chacha20;
36e506342aSTianjia Zhang 		struct tls12_crypto_info_sm4_gcm sm4gcm;
37e506342aSTianjia Zhang 		struct tls12_crypto_info_sm4_ccm sm4ccm;
38d76c51f9SVadim Fedorenko 		struct tls12_crypto_info_aes_ccm_128 aesccm128;
3913bf99abSVadim Fedorenko 		struct tls12_crypto_info_aes_gcm_256 aesgcm256;
4084e306b0SSabrina Dubroca 		struct tls12_crypto_info_aria_gcm_128 ariagcm128;
4184e306b0SSabrina Dubroca 		struct tls12_crypto_info_aria_gcm_256 ariagcm256;
42291c53e4SJakub Kicinski 	};
43291c53e4SJakub Kicinski 	size_t len;
44291c53e4SJakub Kicinski };
45291c53e4SJakub Kicinski 
46291c53e4SJakub Kicinski static void tls_crypto_info_init(uint16_t tls_version, uint16_t cipher_type,
47291c53e4SJakub Kicinski 				 struct tls_crypto_info_keys *tls12)
48291c53e4SJakub Kicinski {
49291c53e4SJakub Kicinski 	memset(tls12, 0, sizeof(*tls12));
50291c53e4SJakub Kicinski 
51291c53e4SJakub Kicinski 	switch (cipher_type) {
52291c53e4SJakub Kicinski 	case TLS_CIPHER_CHACHA20_POLY1305:
53291c53e4SJakub Kicinski 		tls12->len = sizeof(struct tls12_crypto_info_chacha20_poly1305);
54291c53e4SJakub Kicinski 		tls12->chacha20.info.version = tls_version;
55291c53e4SJakub Kicinski 		tls12->chacha20.info.cipher_type = cipher_type;
56291c53e4SJakub Kicinski 		break;
57291c53e4SJakub Kicinski 	case TLS_CIPHER_AES_GCM_128:
58291c53e4SJakub Kicinski 		tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_128);
59291c53e4SJakub Kicinski 		tls12->aes128.info.version = tls_version;
60291c53e4SJakub Kicinski 		tls12->aes128.info.cipher_type = cipher_type;
61291c53e4SJakub Kicinski 		break;
62e506342aSTianjia Zhang 	case TLS_CIPHER_SM4_GCM:
63e506342aSTianjia Zhang 		tls12->len = sizeof(struct tls12_crypto_info_sm4_gcm);
64e506342aSTianjia Zhang 		tls12->sm4gcm.info.version = tls_version;
65e506342aSTianjia Zhang 		tls12->sm4gcm.info.cipher_type = cipher_type;
66e506342aSTianjia Zhang 		break;
67e506342aSTianjia Zhang 	case TLS_CIPHER_SM4_CCM:
68e506342aSTianjia Zhang 		tls12->len = sizeof(struct tls12_crypto_info_sm4_ccm);
69e506342aSTianjia Zhang 		tls12->sm4ccm.info.version = tls_version;
70e506342aSTianjia Zhang 		tls12->sm4ccm.info.cipher_type = cipher_type;
71e506342aSTianjia Zhang 		break;
72d76c51f9SVadim Fedorenko 	case TLS_CIPHER_AES_CCM_128:
73d76c51f9SVadim Fedorenko 		tls12->len = sizeof(struct tls12_crypto_info_aes_ccm_128);
74d76c51f9SVadim Fedorenko 		tls12->aesccm128.info.version = tls_version;
75d76c51f9SVadim Fedorenko 		tls12->aesccm128.info.cipher_type = cipher_type;
76d76c51f9SVadim Fedorenko 		break;
7713bf99abSVadim Fedorenko 	case TLS_CIPHER_AES_GCM_256:
7813bf99abSVadim Fedorenko 		tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_256);
7913bf99abSVadim Fedorenko 		tls12->aesgcm256.info.version = tls_version;
8013bf99abSVadim Fedorenko 		tls12->aesgcm256.info.cipher_type = cipher_type;
8113bf99abSVadim Fedorenko 		break;
8284e306b0SSabrina Dubroca 	case TLS_CIPHER_ARIA_GCM_128:
8384e306b0SSabrina Dubroca 		tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_128);
8484e306b0SSabrina Dubroca 		tls12->ariagcm128.info.version = tls_version;
8584e306b0SSabrina Dubroca 		tls12->ariagcm128.info.cipher_type = cipher_type;
8684e306b0SSabrina Dubroca 		break;
8784e306b0SSabrina Dubroca 	case TLS_CIPHER_ARIA_GCM_256:
8884e306b0SSabrina Dubroca 		tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_256);
8984e306b0SSabrina Dubroca 		tls12->ariagcm256.info.version = tls_version;
9084e306b0SSabrina Dubroca 		tls12->ariagcm256.info.cipher_type = cipher_type;
9184e306b0SSabrina Dubroca 		break;
92291c53e4SJakub Kicinski 	default:
93291c53e4SJakub Kicinski 		break;
94291c53e4SJakub Kicinski 	}
95291c53e4SJakub Kicinski }
96291c53e4SJakub Kicinski 
97baa00119SJakub Kicinski static void memrnd(void *s, size_t n)
98baa00119SJakub Kicinski {
99baa00119SJakub Kicinski 	int *dword = s;
100baa00119SJakub Kicinski 	char *byte;
101baa00119SJakub Kicinski 
102baa00119SJakub Kicinski 	for (; n >= 4; n -= 4)
103baa00119SJakub Kicinski 		*dword++ = rand();
104baa00119SJakub Kicinski 	byte = (void *)dword;
105baa00119SJakub Kicinski 	while (n--)
106baa00119SJakub Kicinski 		*byte++ = rand();
107baa00119SJakub Kicinski }
108baa00119SJakub Kicinski 
109a125f91fSJakub Kicinski static void ulp_sock_pair(struct __test_metadata *_metadata,
110a125f91fSJakub Kicinski 			  int *fd, int *cfd, bool *notls)
111cf32526cSJakub Kicinski {
112cf32526cSJakub Kicinski 	struct sockaddr_in addr;
113cf32526cSJakub Kicinski 	socklen_t len;
114cf32526cSJakub Kicinski 	int sfd, ret;
115cf32526cSJakub Kicinski 
116a125f91fSJakub Kicinski 	*notls = false;
117cf32526cSJakub Kicinski 	len = sizeof(addr);
118cf32526cSJakub Kicinski 
119cf32526cSJakub Kicinski 	addr.sin_family = AF_INET;
120cf32526cSJakub Kicinski 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
121cf32526cSJakub Kicinski 	addr.sin_port = 0;
122cf32526cSJakub Kicinski 
123a125f91fSJakub Kicinski 	*fd = socket(AF_INET, SOCK_STREAM, 0);
124cf32526cSJakub Kicinski 	sfd = socket(AF_INET, SOCK_STREAM, 0);
125cf32526cSJakub Kicinski 
126cf32526cSJakub Kicinski 	ret = bind(sfd, &addr, sizeof(addr));
127cf32526cSJakub Kicinski 	ASSERT_EQ(ret, 0);
128cf32526cSJakub Kicinski 	ret = listen(sfd, 10);
129cf32526cSJakub Kicinski 	ASSERT_EQ(ret, 0);
130cf32526cSJakub Kicinski 
131cf32526cSJakub Kicinski 	ret = getsockname(sfd, &addr, &len);
132cf32526cSJakub Kicinski 	ASSERT_EQ(ret, 0);
133cf32526cSJakub Kicinski 
134a125f91fSJakub Kicinski 	ret = connect(*fd, &addr, sizeof(addr));
135cf32526cSJakub Kicinski 	ASSERT_EQ(ret, 0);
136cf32526cSJakub Kicinski 
137a125f91fSJakub Kicinski 	*cfd = accept(sfd, &addr, &len);
138a125f91fSJakub Kicinski 	ASSERT_GE(*cfd, 0);
139cf32526cSJakub Kicinski 
140cf32526cSJakub Kicinski 	close(sfd);
141cf32526cSJakub Kicinski 
142a125f91fSJakub Kicinski 	ret = setsockopt(*fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
143cf32526cSJakub Kicinski 	if (ret != 0) {
144e29903c4SJakub Kicinski 		ASSERT_EQ(errno, ENOENT);
145a125f91fSJakub Kicinski 		*notls = true;
146cf32526cSJakub Kicinski 		printf("Failure setting TCP_ULP, testing without tls\n");
147cf32526cSJakub Kicinski 		return;
148cf32526cSJakub Kicinski 	}
149cf32526cSJakub Kicinski 
150a125f91fSJakub Kicinski 	ret = setsockopt(*cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
151cf32526cSJakub Kicinski 	ASSERT_EQ(ret, 0);
152cf32526cSJakub Kicinski }
153cf32526cSJakub Kicinski 
15431180adbSJakub Kicinski /* Produce a basic cmsg */
15531180adbSJakub Kicinski static int tls_send_cmsg(int fd, unsigned char record_type,
15631180adbSJakub Kicinski 			 void *data, size_t len, int flags)
15731180adbSJakub Kicinski {
15831180adbSJakub Kicinski 	char cbuf[CMSG_SPACE(sizeof(char))];
15931180adbSJakub Kicinski 	int cmsg_len = sizeof(char);
16031180adbSJakub Kicinski 	struct cmsghdr *cmsg;
16131180adbSJakub Kicinski 	struct msghdr msg;
16231180adbSJakub Kicinski 	struct iovec vec;
16331180adbSJakub Kicinski 
16431180adbSJakub Kicinski 	vec.iov_base = data;
16531180adbSJakub Kicinski 	vec.iov_len = len;
16631180adbSJakub Kicinski 	memset(&msg, 0, sizeof(struct msghdr));
16731180adbSJakub Kicinski 	msg.msg_iov = &vec;
16831180adbSJakub Kicinski 	msg.msg_iovlen = 1;
16931180adbSJakub Kicinski 	msg.msg_control = cbuf;
17031180adbSJakub Kicinski 	msg.msg_controllen = sizeof(cbuf);
17131180adbSJakub Kicinski 	cmsg = CMSG_FIRSTHDR(&msg);
17231180adbSJakub Kicinski 	cmsg->cmsg_level = SOL_TLS;
17331180adbSJakub Kicinski 	/* test sending non-record types. */
17431180adbSJakub Kicinski 	cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
17531180adbSJakub Kicinski 	cmsg->cmsg_len = CMSG_LEN(cmsg_len);
17631180adbSJakub Kicinski 	*CMSG_DATA(cmsg) = record_type;
17731180adbSJakub Kicinski 	msg.msg_controllen = cmsg->cmsg_len;
17831180adbSJakub Kicinski 
17931180adbSJakub Kicinski 	return sendmsg(fd, &msg, flags);
18031180adbSJakub Kicinski }
18131180adbSJakub Kicinski 
18231180adbSJakub Kicinski static int tls_recv_cmsg(struct __test_metadata *_metadata,
18331180adbSJakub Kicinski 			 int fd, unsigned char record_type,
18431180adbSJakub Kicinski 			 void *data, size_t len, int flags)
18531180adbSJakub Kicinski {
18631180adbSJakub Kicinski 	char cbuf[CMSG_SPACE(sizeof(char))];
18731180adbSJakub Kicinski 	struct cmsghdr *cmsg;
18831180adbSJakub Kicinski 	unsigned char ctype;
18931180adbSJakub Kicinski 	struct msghdr msg;
19031180adbSJakub Kicinski 	struct iovec vec;
19131180adbSJakub Kicinski 	int n;
19231180adbSJakub Kicinski 
19331180adbSJakub Kicinski 	vec.iov_base = data;
19431180adbSJakub Kicinski 	vec.iov_len = len;
19531180adbSJakub Kicinski 	memset(&msg, 0, sizeof(struct msghdr));
19631180adbSJakub Kicinski 	msg.msg_iov = &vec;
19731180adbSJakub Kicinski 	msg.msg_iovlen = 1;
19831180adbSJakub Kicinski 	msg.msg_control = cbuf;
19931180adbSJakub Kicinski 	msg.msg_controllen = sizeof(cbuf);
20031180adbSJakub Kicinski 
20131180adbSJakub Kicinski 	n = recvmsg(fd, &msg, flags);
20231180adbSJakub Kicinski 
20331180adbSJakub Kicinski 	cmsg = CMSG_FIRSTHDR(&msg);
20431180adbSJakub Kicinski 	EXPECT_NE(cmsg, NULL);
20531180adbSJakub Kicinski 	EXPECT_EQ(cmsg->cmsg_level, SOL_TLS);
20631180adbSJakub Kicinski 	EXPECT_EQ(cmsg->cmsg_type, TLS_GET_RECORD_TYPE);
20731180adbSJakub Kicinski 	ctype = *((unsigned char *)CMSG_DATA(cmsg));
20831180adbSJakub Kicinski 	EXPECT_EQ(ctype, record_type);
20931180adbSJakub Kicinski 
21031180adbSJakub Kicinski 	return n;
21131180adbSJakub Kicinski }
21231180adbSJakub Kicinski 
213a125f91fSJakub Kicinski FIXTURE(tls_basic)
214a125f91fSJakub Kicinski {
215a125f91fSJakub Kicinski 	int fd, cfd;
216a125f91fSJakub Kicinski 	bool notls;
217a125f91fSJakub Kicinski };
218a125f91fSJakub Kicinski 
219a125f91fSJakub Kicinski FIXTURE_SETUP(tls_basic)
220a125f91fSJakub Kicinski {
221a125f91fSJakub Kicinski 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
222a125f91fSJakub Kicinski }
223a125f91fSJakub Kicinski 
224cf32526cSJakub Kicinski FIXTURE_TEARDOWN(tls_basic)
225cf32526cSJakub Kicinski {
226cf32526cSJakub Kicinski 	close(self->fd);
227cf32526cSJakub Kicinski 	close(self->cfd);
228cf32526cSJakub Kicinski }
229cf32526cSJakub Kicinski 
230cf32526cSJakub Kicinski /* Send some data through with ULP but no keys */
231cf32526cSJakub Kicinski TEST_F(tls_basic, base_base)
232cf32526cSJakub Kicinski {
233cf32526cSJakub Kicinski 	char const *test_str = "test_read";
234cf32526cSJakub Kicinski 	int send_len = 10;
235cf32526cSJakub Kicinski 	char buf[10];
236cf32526cSJakub Kicinski 
237cf32526cSJakub Kicinski 	ASSERT_EQ(strlen(test_str) + 1, send_len);
238cf32526cSJakub Kicinski 
239cf32526cSJakub Kicinski 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
240cf32526cSJakub Kicinski 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
241cf32526cSJakub Kicinski 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
242cf32526cSJakub Kicinski };
243cf32526cSJakub Kicinski 
2444bfb6224SSabrina Dubroca TEST_F(tls_basic, bad_cipher)
2454bfb6224SSabrina Dubroca {
2464bfb6224SSabrina Dubroca 	struct tls_crypto_info_keys tls12;
2474bfb6224SSabrina Dubroca 
2484bfb6224SSabrina Dubroca 	tls12.crypto_info.version = 200;
2494bfb6224SSabrina Dubroca 	tls12.crypto_info.cipher_type = TLS_CIPHER_AES_GCM_128;
2504bfb6224SSabrina Dubroca 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
2514bfb6224SSabrina Dubroca 
2524bfb6224SSabrina Dubroca 	tls12.crypto_info.version = TLS_1_2_VERSION;
2534bfb6224SSabrina Dubroca 	tls12.crypto_info.cipher_type = 50;
2544bfb6224SSabrina Dubroca 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
2554bfb6224SSabrina Dubroca 
2564bfb6224SSabrina Dubroca 	tls12.crypto_info.version = TLS_1_2_VERSION;
2574bfb6224SSabrina Dubroca 	tls12.crypto_info.cipher_type = 59;
2584bfb6224SSabrina Dubroca 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
2594bfb6224SSabrina Dubroca 
2604bfb6224SSabrina Dubroca 	tls12.crypto_info.version = TLS_1_2_VERSION;
2614bfb6224SSabrina Dubroca 	tls12.crypto_info.cipher_type = 10;
2624bfb6224SSabrina Dubroca 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
2634bfb6224SSabrina Dubroca 
2644bfb6224SSabrina Dubroca 	tls12.crypto_info.version = TLS_1_2_VERSION;
2654bfb6224SSabrina Dubroca 	tls12.crypto_info.cipher_type = 70;
2664bfb6224SSabrina Dubroca 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
2674bfb6224SSabrina Dubroca }
2684bfb6224SSabrina Dubroca 
2697f657d5bSDave Watson FIXTURE(tls)
2707f657d5bSDave Watson {
2717f657d5bSDave Watson 	int fd, cfd;
2727f657d5bSDave Watson 	bool notls;
2737f657d5bSDave Watson };
2747f657d5bSDave Watson 
2750feba221SJakub Kicinski FIXTURE_VARIANT(tls)
2760feba221SJakub Kicinski {
2773502bd9bSVadim Fedorenko 	uint16_t tls_version;
2783502bd9bSVadim Fedorenko 	uint16_t cipher_type;
279d113c395SMagali Lemes 	bool nopad, fips_non_compliant;
2800feba221SJakub Kicinski };
2810feba221SJakub Kicinski 
282e506342aSTianjia Zhang FIXTURE_VARIANT_ADD(tls, 12_aes_gcm)
2830feba221SJakub Kicinski {
2840feba221SJakub Kicinski 	.tls_version = TLS_1_2_VERSION,
2854f336e88SVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_GCM_128,
2860feba221SJakub Kicinski };
2870feba221SJakub Kicinski 
288e506342aSTianjia Zhang FIXTURE_VARIANT_ADD(tls, 13_aes_gcm)
2890feba221SJakub Kicinski {
2900feba221SJakub Kicinski 	.tls_version = TLS_1_3_VERSION,
2914f336e88SVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_GCM_128,
2924f336e88SVadim Fedorenko };
2934f336e88SVadim Fedorenko 
2944f336e88SVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 12_chacha)
2954f336e88SVadim Fedorenko {
2964f336e88SVadim Fedorenko 	.tls_version = TLS_1_2_VERSION,
2974f336e88SVadim Fedorenko 	.cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
298d113c395SMagali Lemes 	.fips_non_compliant = true,
2994f336e88SVadim Fedorenko };
3004f336e88SVadim Fedorenko 
3014f336e88SVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 13_chacha)
3024f336e88SVadim Fedorenko {
3034f336e88SVadim Fedorenko 	.tls_version = TLS_1_3_VERSION,
3044f336e88SVadim Fedorenko 	.cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
305d113c395SMagali Lemes 	.fips_non_compliant = true,
3060feba221SJakub Kicinski };
3070feba221SJakub Kicinski 
308e506342aSTianjia Zhang FIXTURE_VARIANT_ADD(tls, 13_sm4_gcm)
309e506342aSTianjia Zhang {
310e506342aSTianjia Zhang 	.tls_version = TLS_1_3_VERSION,
311e506342aSTianjia Zhang 	.cipher_type = TLS_CIPHER_SM4_GCM,
312d113c395SMagali Lemes 	.fips_non_compliant = true,
313e506342aSTianjia Zhang };
314e506342aSTianjia Zhang 
315e506342aSTianjia Zhang FIXTURE_VARIANT_ADD(tls, 13_sm4_ccm)
316e506342aSTianjia Zhang {
317e506342aSTianjia Zhang 	.tls_version = TLS_1_3_VERSION,
318e506342aSTianjia Zhang 	.cipher_type = TLS_CIPHER_SM4_CCM,
319d113c395SMagali Lemes 	.fips_non_compliant = true,
320e506342aSTianjia Zhang };
321e506342aSTianjia Zhang 
322d76c51f9SVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 12_aes_ccm)
323d76c51f9SVadim Fedorenko {
324d76c51f9SVadim Fedorenko 	.tls_version = TLS_1_2_VERSION,
325d76c51f9SVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_CCM_128,
326d76c51f9SVadim Fedorenko };
327d76c51f9SVadim Fedorenko 
328d76c51f9SVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 13_aes_ccm)
329d76c51f9SVadim Fedorenko {
330d76c51f9SVadim Fedorenko 	.tls_version = TLS_1_3_VERSION,
331d76c51f9SVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_CCM_128,
332d76c51f9SVadim Fedorenko };
333d76c51f9SVadim Fedorenko 
33413bf99abSVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 12_aes_gcm_256)
33513bf99abSVadim Fedorenko {
33613bf99abSVadim Fedorenko 	.tls_version = TLS_1_2_VERSION,
33713bf99abSVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_GCM_256,
33813bf99abSVadim Fedorenko };
33913bf99abSVadim Fedorenko 
34013bf99abSVadim Fedorenko FIXTURE_VARIANT_ADD(tls, 13_aes_gcm_256)
34113bf99abSVadim Fedorenko {
34213bf99abSVadim Fedorenko 	.tls_version = TLS_1_3_VERSION,
34313bf99abSVadim Fedorenko 	.cipher_type = TLS_CIPHER_AES_GCM_256,
34413bf99abSVadim Fedorenko };
34513bf99abSVadim Fedorenko 
346f36068a2SJakub Kicinski FIXTURE_VARIANT_ADD(tls, 13_nopad)
347f36068a2SJakub Kicinski {
348f36068a2SJakub Kicinski 	.tls_version = TLS_1_3_VERSION,
349f36068a2SJakub Kicinski 	.cipher_type = TLS_CIPHER_AES_GCM_128,
350f36068a2SJakub Kicinski 	.nopad = true,
351f36068a2SJakub Kicinski };
352f36068a2SJakub Kicinski 
35384e306b0SSabrina Dubroca FIXTURE_VARIANT_ADD(tls, 12_aria_gcm)
35484e306b0SSabrina Dubroca {
35584e306b0SSabrina Dubroca 	.tls_version = TLS_1_2_VERSION,
35684e306b0SSabrina Dubroca 	.cipher_type = TLS_CIPHER_ARIA_GCM_128,
35784e306b0SSabrina Dubroca };
35884e306b0SSabrina Dubroca 
35984e306b0SSabrina Dubroca FIXTURE_VARIANT_ADD(tls, 12_aria_gcm_256)
36084e306b0SSabrina Dubroca {
36184e306b0SSabrina Dubroca 	.tls_version = TLS_1_2_VERSION,
36284e306b0SSabrina Dubroca 	.cipher_type = TLS_CIPHER_ARIA_GCM_256,
36384e306b0SSabrina Dubroca };
36484e306b0SSabrina Dubroca 
3657f657d5bSDave Watson FIXTURE_SETUP(tls)
3667f657d5bSDave Watson {
367291c53e4SJakub Kicinski 	struct tls_crypto_info_keys tls12;
368f36068a2SJakub Kicinski 	int one = 1;
369a125f91fSJakub Kicinski 	int ret;
3707f657d5bSDave Watson 
371d113c395SMagali Lemes 	if (fips_enabled && variant->fips_non_compliant)
372d113c395SMagali Lemes 		SKIP(return, "Unsupported cipher in FIPS mode");
373d113c395SMagali Lemes 
374291c53e4SJakub Kicinski 	tls_crypto_info_init(variant->tls_version, variant->cipher_type,
375291c53e4SJakub Kicinski 			     &tls12);
3767f657d5bSDave Watson 
377a125f91fSJakub Kicinski 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
3787f657d5bSDave Watson 
379a125f91fSJakub Kicinski 	if (self->notls)
380a125f91fSJakub Kicinski 		return;
3817f657d5bSDave Watson 
382a125f91fSJakub Kicinski 	ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
3837f657d5bSDave Watson 	ASSERT_EQ(ret, 0);
3847f657d5bSDave Watson 
385a125f91fSJakub Kicinski 	ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12, tls12.len);
3867f657d5bSDave Watson 	ASSERT_EQ(ret, 0);
387f36068a2SJakub Kicinski 
388f36068a2SJakub Kicinski 	if (variant->nopad) {
389f36068a2SJakub Kicinski 		ret = setsockopt(self->cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
390f36068a2SJakub Kicinski 				 (void *)&one, sizeof(one));
391f36068a2SJakub Kicinski 		ASSERT_EQ(ret, 0);
392f36068a2SJakub Kicinski 	}
3937f657d5bSDave Watson }
3947f657d5bSDave Watson 
3957f657d5bSDave Watson FIXTURE_TEARDOWN(tls)
3967f657d5bSDave Watson {
3977f657d5bSDave Watson 	close(self->fd);
3987f657d5bSDave Watson 	close(self->cfd);
3997f657d5bSDave Watson }
4007f657d5bSDave Watson 
4017f657d5bSDave Watson TEST_F(tls, sendfile)
4027f657d5bSDave Watson {
4037f657d5bSDave Watson 	int filefd = open("/proc/self/exe", O_RDONLY);
4047f657d5bSDave Watson 	struct stat st;
4057f657d5bSDave Watson 
4067f657d5bSDave Watson 	EXPECT_GE(filefd, 0);
4077f657d5bSDave Watson 	fstat(filefd, &st);
4087f657d5bSDave Watson 	EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
4097f657d5bSDave Watson }
4107f657d5bSDave Watson 
4117f657d5bSDave Watson TEST_F(tls, send_then_sendfile)
4127f657d5bSDave Watson {
4137f657d5bSDave Watson 	int filefd = open("/proc/self/exe", O_RDONLY);
4147f657d5bSDave Watson 	char const *test_str = "test_send";
4157f657d5bSDave Watson 	int to_send = strlen(test_str) + 1;
4167f657d5bSDave Watson 	char recv_buf[10];
4177f657d5bSDave Watson 	struct stat st;
4187f657d5bSDave Watson 	char *buf;
4197f657d5bSDave Watson 
4207f657d5bSDave Watson 	EXPECT_GE(filefd, 0);
4217f657d5bSDave Watson 	fstat(filefd, &st);
4227f657d5bSDave Watson 	buf = (char *)malloc(st.st_size);
4237f657d5bSDave Watson 
4247f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, to_send, 0), to_send);
4250185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, recv_buf, to_send, MSG_WAITALL), to_send);
4267f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, recv_buf, to_send), 0);
4277f657d5bSDave Watson 
4287f657d5bSDave Watson 	EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
4290185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, st.st_size, MSG_WAITALL), st.st_size);
4307f657d5bSDave Watson }
4317f657d5bSDave Watson 
4320e6fbe39SPooja Trivedi static void chunked_sendfile(struct __test_metadata *_metadata,
4330e6fbe39SPooja Trivedi 			     struct _test_data_tls *self,
4340e6fbe39SPooja Trivedi 			     uint16_t chunk_size,
4350e6fbe39SPooja Trivedi 			     uint16_t extra_payload_size)
4360e6fbe39SPooja Trivedi {
4370e6fbe39SPooja Trivedi 	char buf[TLS_PAYLOAD_MAX_LEN];
4380e6fbe39SPooja Trivedi 	uint16_t test_payload_size;
4390e6fbe39SPooja Trivedi 	int size = 0;
4400e6fbe39SPooja Trivedi 	int ret;
4410e6fbe39SPooja Trivedi 	char filename[] = "/tmp/mytemp.XXXXXX";
4420e6fbe39SPooja Trivedi 	int fd = mkstemp(filename);
4430e6fbe39SPooja Trivedi 	off_t offset = 0;
4440e6fbe39SPooja Trivedi 
4450e6fbe39SPooja Trivedi 	unlink(filename);
4460e6fbe39SPooja Trivedi 	ASSERT_GE(fd, 0);
4470e6fbe39SPooja Trivedi 	EXPECT_GE(chunk_size, 1);
4480e6fbe39SPooja Trivedi 	test_payload_size = chunk_size + extra_payload_size;
4490e6fbe39SPooja Trivedi 	ASSERT_GE(TLS_PAYLOAD_MAX_LEN, test_payload_size);
4500e6fbe39SPooja Trivedi 	memset(buf, 1, test_payload_size);
4510e6fbe39SPooja Trivedi 	size = write(fd, buf, test_payload_size);
4520e6fbe39SPooja Trivedi 	EXPECT_EQ(size, test_payload_size);
4530e6fbe39SPooja Trivedi 	fsync(fd);
4540e6fbe39SPooja Trivedi 
4550e6fbe39SPooja Trivedi 	while (size > 0) {
4560e6fbe39SPooja Trivedi 		ret = sendfile(self->fd, fd, &offset, chunk_size);
4570e6fbe39SPooja Trivedi 		EXPECT_GE(ret, 0);
4580e6fbe39SPooja Trivedi 		size -= ret;
4590e6fbe39SPooja Trivedi 	}
4600e6fbe39SPooja Trivedi 
4610e6fbe39SPooja Trivedi 	EXPECT_EQ(recv(self->cfd, buf, test_payload_size, MSG_WAITALL),
4620e6fbe39SPooja Trivedi 		  test_payload_size);
4630e6fbe39SPooja Trivedi 
4640e6fbe39SPooja Trivedi 	close(fd);
4650e6fbe39SPooja Trivedi }
4660e6fbe39SPooja Trivedi 
4670e6fbe39SPooja Trivedi TEST_F(tls, multi_chunk_sendfile)
4680e6fbe39SPooja Trivedi {
4690e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 4096);
4700e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 0);
4710e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 1);
4720e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 2048);
4730e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 8192, 2048);
4740e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 8192);
4750e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 8192, 4096);
4760e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 12288, 1024);
4770e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 12288, 2000);
4780e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 15360, 100);
4790e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 15360, 300);
4800e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 1, 4096);
4810e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 2048, 4096);
4820e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 2048, 8192);
4830e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 4096, 8192);
4840e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 1024, 12288);
4850e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 2000, 12288);
4860e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 100, 15360);
4870e6fbe39SPooja Trivedi 	chunked_sendfile(_metadata, self, 300, 15360);
4880e6fbe39SPooja Trivedi }
4890e6fbe39SPooja Trivedi 
4907f657d5bSDave Watson TEST_F(tls, recv_max)
4917f657d5bSDave Watson {
4927f657d5bSDave Watson 	unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
4937f657d5bSDave Watson 	char recv_mem[TLS_PAYLOAD_MAX_LEN];
4947f657d5bSDave Watson 	char buf[TLS_PAYLOAD_MAX_LEN];
4957f657d5bSDave Watson 
496baa00119SJakub Kicinski 	memrnd(buf, sizeof(buf));
497baa00119SJakub Kicinski 
4987f657d5bSDave Watson 	EXPECT_GE(send(self->fd, buf, send_len, 0), 0);
4997f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
5007f657d5bSDave Watson 	EXPECT_EQ(memcmp(buf, recv_mem, send_len), 0);
5017f657d5bSDave Watson }
5027f657d5bSDave Watson 
5037f657d5bSDave Watson TEST_F(tls, recv_small)
5047f657d5bSDave Watson {
5057f657d5bSDave Watson 	char const *test_str = "test_read";
5067f657d5bSDave Watson 	int send_len = 10;
5077f657d5bSDave Watson 	char buf[10];
5087f657d5bSDave Watson 
5097f657d5bSDave Watson 	send_len = strlen(test_str) + 1;
5107f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
5117f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
5127f657d5bSDave Watson 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
5137f657d5bSDave Watson }
5147f657d5bSDave Watson 
5157f657d5bSDave Watson TEST_F(tls, msg_more)
5167f657d5bSDave Watson {
5177f657d5bSDave Watson 	char const *test_str = "test_read";
5187f657d5bSDave Watson 	int send_len = 10;
5197f657d5bSDave Watson 	char buf[10 * 2];
5207f657d5bSDave Watson 
5217f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
5227f657d5bSDave Watson 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
5237f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
5240185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, send_len * 2, MSG_WAITALL),
5257f657d5bSDave Watson 		  send_len * 2);
5267f657d5bSDave Watson 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
5277f657d5bSDave Watson }
5287f657d5bSDave Watson 
5298051bb7fSJakub Kicinski TEST_F(tls, msg_more_unsent)
5308051bb7fSJakub Kicinski {
5318051bb7fSJakub Kicinski 	char const *test_str = "test_read";
5328051bb7fSJakub Kicinski 	int send_len = 10;
5338051bb7fSJakub Kicinski 	char buf[10];
5348051bb7fSJakub Kicinski 
5358051bb7fSJakub Kicinski 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
5368051bb7fSJakub Kicinski 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
5378051bb7fSJakub Kicinski }
5388051bb7fSJakub Kicinski 
5398790c6a4SHannes Reinecke TEST_F(tls, msg_eor)
5408790c6a4SHannes Reinecke {
5418790c6a4SHannes Reinecke 	char const *test_str = "test_read";
5428790c6a4SHannes Reinecke 	int send_len = 10;
5438790c6a4SHannes Reinecke 	char buf[10];
5448790c6a4SHannes Reinecke 
5458790c6a4SHannes Reinecke 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_EOR), send_len);
5468790c6a4SHannes Reinecke 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
5478790c6a4SHannes Reinecke 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
5488790c6a4SHannes Reinecke }
5498790c6a4SHannes Reinecke 
5507f657d5bSDave Watson TEST_F(tls, sendmsg_single)
5517f657d5bSDave Watson {
5527f657d5bSDave Watson 	struct msghdr msg;
5537f657d5bSDave Watson 
5547f657d5bSDave Watson 	char const *test_str = "test_sendmsg";
5557f657d5bSDave Watson 	size_t send_len = 13;
5567f657d5bSDave Watson 	struct iovec vec;
5577f657d5bSDave Watson 	char buf[13];
5587f657d5bSDave Watson 
5597f657d5bSDave Watson 	vec.iov_base = (char *)test_str;
5607f657d5bSDave Watson 	vec.iov_len = send_len;
5617f657d5bSDave Watson 	memset(&msg, 0, sizeof(struct msghdr));
5627f657d5bSDave Watson 	msg.msg_iov = &vec;
5637f657d5bSDave Watson 	msg.msg_iovlen = 1;
5647f657d5bSDave Watson 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
5650185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
5667f657d5bSDave Watson 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
5677f657d5bSDave Watson }
5687f657d5bSDave Watson 
56965190f77SJakub Kicinski #define MAX_FRAGS	64
57065190f77SJakub Kicinski #define SEND_LEN	13
57165190f77SJakub Kicinski TEST_F(tls, sendmsg_fragmented)
57265190f77SJakub Kicinski {
57365190f77SJakub Kicinski 	char const *test_str = "test_sendmsg";
57465190f77SJakub Kicinski 	char buf[SEND_LEN * MAX_FRAGS];
57565190f77SJakub Kicinski 	struct iovec vec[MAX_FRAGS];
57665190f77SJakub Kicinski 	struct msghdr msg;
57765190f77SJakub Kicinski 	int i, frags;
57865190f77SJakub Kicinski 
57965190f77SJakub Kicinski 	for (frags = 1; frags <= MAX_FRAGS; frags++) {
58065190f77SJakub Kicinski 		for (i = 0; i < frags; i++) {
58165190f77SJakub Kicinski 			vec[i].iov_base = (char *)test_str;
58265190f77SJakub Kicinski 			vec[i].iov_len = SEND_LEN;
58365190f77SJakub Kicinski 		}
58465190f77SJakub Kicinski 
58565190f77SJakub Kicinski 		memset(&msg, 0, sizeof(struct msghdr));
58665190f77SJakub Kicinski 		msg.msg_iov = vec;
58765190f77SJakub Kicinski 		msg.msg_iovlen = frags;
58865190f77SJakub Kicinski 
58965190f77SJakub Kicinski 		EXPECT_EQ(sendmsg(self->fd, &msg, 0), SEND_LEN * frags);
59065190f77SJakub Kicinski 		EXPECT_EQ(recv(self->cfd, buf, SEND_LEN * frags, MSG_WAITALL),
59165190f77SJakub Kicinski 			  SEND_LEN * frags);
59265190f77SJakub Kicinski 
59365190f77SJakub Kicinski 		for (i = 0; i < frags; i++)
59465190f77SJakub Kicinski 			EXPECT_EQ(memcmp(buf + SEND_LEN * i,
59565190f77SJakub Kicinski 					 test_str, SEND_LEN), 0);
59665190f77SJakub Kicinski 	}
59765190f77SJakub Kicinski }
59865190f77SJakub Kicinski #undef MAX_FRAGS
59965190f77SJakub Kicinski #undef SEND_LEN
60065190f77SJakub Kicinski 
6017f657d5bSDave Watson TEST_F(tls, sendmsg_large)
6027f657d5bSDave Watson {
6037f657d5bSDave Watson 	void *mem = malloc(16384);
6047f657d5bSDave Watson 	size_t send_len = 16384;
6057f657d5bSDave Watson 	size_t sends = 128;
6067f657d5bSDave Watson 	struct msghdr msg;
6077f657d5bSDave Watson 	size_t recvs = 0;
6087f657d5bSDave Watson 	size_t sent = 0;
6097f657d5bSDave Watson 
6107f657d5bSDave Watson 	memset(&msg, 0, sizeof(struct msghdr));
6117f657d5bSDave Watson 	while (sent++ < sends) {
6127f657d5bSDave Watson 		struct iovec vec = { (void *)mem, send_len };
6137f657d5bSDave Watson 
6147f657d5bSDave Watson 		msg.msg_iov = &vec;
6157f657d5bSDave Watson 		msg.msg_iovlen = 1;
616c326ca98SSabrina Dubroca 		EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
6177f657d5bSDave Watson 	}
6187f657d5bSDave Watson 
619f50688b4SKees Cook 	while (recvs++ < sends) {
620c326ca98SSabrina Dubroca 		EXPECT_NE(recv(self->cfd, mem, send_len, 0), -1);
621f50688b4SKees Cook 	}
6227f657d5bSDave Watson 
6237f657d5bSDave Watson 	free(mem);
6247f657d5bSDave Watson }
6257f657d5bSDave Watson 
6267f657d5bSDave Watson TEST_F(tls, sendmsg_multiple)
6277f657d5bSDave Watson {
6287f657d5bSDave Watson 	char const *test_str = "test_sendmsg_multiple";
6297f657d5bSDave Watson 	struct iovec vec[5];
6307f657d5bSDave Watson 	char *test_strs[5];
6317f657d5bSDave Watson 	struct msghdr msg;
6327f657d5bSDave Watson 	int total_len = 0;
6337f657d5bSDave Watson 	int len_cmp = 0;
6347f657d5bSDave Watson 	int iov_len = 5;
6357f657d5bSDave Watson 	char *buf;
6367f657d5bSDave Watson 	int i;
6377f657d5bSDave Watson 
6387f657d5bSDave Watson 	memset(&msg, 0, sizeof(struct msghdr));
6397f657d5bSDave Watson 	for (i = 0; i < iov_len; i++) {
6407f657d5bSDave Watson 		test_strs[i] = (char *)malloc(strlen(test_str) + 1);
6417f657d5bSDave Watson 		snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
6427f657d5bSDave Watson 		vec[i].iov_base = (void *)test_strs[i];
6437f657d5bSDave Watson 		vec[i].iov_len = strlen(test_strs[i]) + 1;
6447f657d5bSDave Watson 		total_len += vec[i].iov_len;
6457f657d5bSDave Watson 	}
6467f657d5bSDave Watson 	msg.msg_iov = vec;
6477f657d5bSDave Watson 	msg.msg_iovlen = iov_len;
6487f657d5bSDave Watson 
649c326ca98SSabrina Dubroca 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
6507f657d5bSDave Watson 	buf = malloc(total_len);
651c326ca98SSabrina Dubroca 	EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
6527f657d5bSDave Watson 	for (i = 0; i < iov_len; i++) {
6537f657d5bSDave Watson 		EXPECT_EQ(memcmp(test_strs[i], buf + len_cmp,
6547f657d5bSDave Watson 				 strlen(test_strs[i])),
6557f657d5bSDave Watson 			  0);
6567f657d5bSDave Watson 		len_cmp += strlen(buf + len_cmp) + 1;
6577f657d5bSDave Watson 	}
6587f657d5bSDave Watson 	for (i = 0; i < iov_len; i++)
6597f657d5bSDave Watson 		free(test_strs[i]);
6607f657d5bSDave Watson 	free(buf);
6617f657d5bSDave Watson }
6627f657d5bSDave Watson 
6637f657d5bSDave Watson TEST_F(tls, sendmsg_multiple_stress)
6647f657d5bSDave Watson {
6657f657d5bSDave Watson 	char const *test_str = "abcdefghijklmno";
6667f657d5bSDave Watson 	struct iovec vec[1024];
6677f657d5bSDave Watson 	char *test_strs[1024];
6687f657d5bSDave Watson 	int iov_len = 1024;
6697f657d5bSDave Watson 	int total_len = 0;
6707f657d5bSDave Watson 	char buf[1 << 14];
6717f657d5bSDave Watson 	struct msghdr msg;
6727f657d5bSDave Watson 	int len_cmp = 0;
6737f657d5bSDave Watson 	int i;
6747f657d5bSDave Watson 
6757f657d5bSDave Watson 	memset(&msg, 0, sizeof(struct msghdr));
6767f657d5bSDave Watson 	for (i = 0; i < iov_len; i++) {
6777f657d5bSDave Watson 		test_strs[i] = (char *)malloc(strlen(test_str) + 1);
6787f657d5bSDave Watson 		snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
6797f657d5bSDave Watson 		vec[i].iov_base = (void *)test_strs[i];
6807f657d5bSDave Watson 		vec[i].iov_len = strlen(test_strs[i]) + 1;
6817f657d5bSDave Watson 		total_len += vec[i].iov_len;
6827f657d5bSDave Watson 	}
6837f657d5bSDave Watson 	msg.msg_iov = vec;
6847f657d5bSDave Watson 	msg.msg_iovlen = iov_len;
6857f657d5bSDave Watson 
6867f657d5bSDave Watson 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
6877f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
6887f657d5bSDave Watson 
6897f657d5bSDave Watson 	for (i = 0; i < iov_len; i++)
6907f657d5bSDave Watson 		len_cmp += strlen(buf + len_cmp) + 1;
6917f657d5bSDave Watson 
6927f657d5bSDave Watson 	for (i = 0; i < iov_len; i++)
6937f657d5bSDave Watson 		free(test_strs[i]);
6947f657d5bSDave Watson }
6957f657d5bSDave Watson 
6967f657d5bSDave Watson TEST_F(tls, splice_from_pipe)
6977f657d5bSDave Watson {
6987f657d5bSDave Watson 	int send_len = TLS_PAYLOAD_MAX_LEN;
6997f657d5bSDave Watson 	char mem_send[TLS_PAYLOAD_MAX_LEN];
7007f657d5bSDave Watson 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
7017f657d5bSDave Watson 	int p[2];
7027f657d5bSDave Watson 
7037f657d5bSDave Watson 	ASSERT_GE(pipe(p), 0);
7047f657d5bSDave Watson 	EXPECT_GE(write(p[1], mem_send, send_len), 0);
7057f657d5bSDave Watson 	EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), 0);
7060ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
7077f657d5bSDave Watson 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
7087f657d5bSDave Watson }
7097f657d5bSDave Watson 
710034ea130SJohn Fastabend TEST_F(tls, splice_more)
711034ea130SJohn Fastabend {
712034ea130SJohn Fastabend 	unsigned int f = SPLICE_F_NONBLOCK | SPLICE_F_MORE | SPLICE_F_GIFT;
713034ea130SJohn Fastabend 	int send_len = TLS_PAYLOAD_MAX_LEN;
714034ea130SJohn Fastabend 	char mem_send[TLS_PAYLOAD_MAX_LEN];
715034ea130SJohn Fastabend 	int i, send_pipe = 1;
716034ea130SJohn Fastabend 	int p[2];
717034ea130SJohn Fastabend 
718034ea130SJohn Fastabend 	ASSERT_GE(pipe(p), 0);
719034ea130SJohn Fastabend 	EXPECT_GE(write(p[1], mem_send, send_len), 0);
720034ea130SJohn Fastabend 	for (i = 0; i < 32; i++)
721034ea130SJohn Fastabend 		EXPECT_EQ(splice(p[0], NULL, self->fd, NULL, send_pipe, f), 1);
722034ea130SJohn Fastabend }
723034ea130SJohn Fastabend 
7247f657d5bSDave Watson TEST_F(tls, splice_from_pipe2)
7257f657d5bSDave Watson {
7267f657d5bSDave Watson 	int send_len = 16000;
7277f657d5bSDave Watson 	char mem_send[16000];
7287f657d5bSDave Watson 	char mem_recv[16000];
7297f657d5bSDave Watson 	int p2[2];
7307f657d5bSDave Watson 	int p[2];
7317f657d5bSDave Watson 
73286c591fbSJakub Kicinski 	memrnd(mem_send, sizeof(mem_send));
73386c591fbSJakub Kicinski 
7347f657d5bSDave Watson 	ASSERT_GE(pipe(p), 0);
7357f657d5bSDave Watson 	ASSERT_GE(pipe(p2), 0);
73686c591fbSJakub Kicinski 	EXPECT_EQ(write(p[1], mem_send, 8000), 8000);
73786c591fbSJakub Kicinski 	EXPECT_EQ(splice(p[0], NULL, self->fd, NULL, 8000, 0), 8000);
73886c591fbSJakub Kicinski 	EXPECT_EQ(write(p2[1], mem_send + 8000, 8000), 8000);
73986c591fbSJakub Kicinski 	EXPECT_EQ(splice(p2[0], NULL, self->fd, NULL, 8000, 0), 8000);
7400185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
7417f657d5bSDave Watson 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
7427f657d5bSDave Watson }
7437f657d5bSDave Watson 
7447f657d5bSDave Watson TEST_F(tls, send_and_splice)
7457f657d5bSDave Watson {
7467f657d5bSDave Watson 	int send_len = TLS_PAYLOAD_MAX_LEN;
7477f657d5bSDave Watson 	char mem_send[TLS_PAYLOAD_MAX_LEN];
7487f657d5bSDave Watson 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
7497f657d5bSDave Watson 	char const *test_str = "test_read";
7507f657d5bSDave Watson 	int send_len2 = 10;
7517f657d5bSDave Watson 	char buf[10];
7527f657d5bSDave Watson 	int p[2];
7537f657d5bSDave Watson 
7547f657d5bSDave Watson 	ASSERT_GE(pipe(p), 0);
7557f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len2, 0), send_len2);
7560ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, send_len2, MSG_WAITALL), send_len2);
7577f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, buf, send_len2), 0);
7587f657d5bSDave Watson 
7597f657d5bSDave Watson 	EXPECT_GE(write(p[1], mem_send, send_len), send_len);
7607f657d5bSDave Watson 	EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), send_len);
7617f657d5bSDave Watson 
7620ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
7637f657d5bSDave Watson 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
7647f657d5bSDave Watson }
7657f657d5bSDave Watson 
7667f657d5bSDave Watson TEST_F(tls, splice_to_pipe)
7677f657d5bSDave Watson {
7687f657d5bSDave Watson 	int send_len = TLS_PAYLOAD_MAX_LEN;
7697f657d5bSDave Watson 	char mem_send[TLS_PAYLOAD_MAX_LEN];
7707f657d5bSDave Watson 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
7717f657d5bSDave Watson 	int p[2];
7727f657d5bSDave Watson 
77386c591fbSJakub Kicinski 	memrnd(mem_send, sizeof(mem_send));
77486c591fbSJakub Kicinski 
7757f657d5bSDave Watson 	ASSERT_GE(pipe(p), 0);
77686c591fbSJakub Kicinski 	EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
77786c591fbSJakub Kicinski 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), send_len);
77886c591fbSJakub Kicinski 	EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
7797f657d5bSDave Watson 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
7807f657d5bSDave Watson }
7817f657d5bSDave Watson 
782d87d67fdSJakub Kicinski TEST_F(tls, splice_cmsg_to_pipe)
783d87d67fdSJakub Kicinski {
784d87d67fdSJakub Kicinski 	char *test_str = "test_read";
785d87d67fdSJakub Kicinski 	char record_type = 100;
786d87d67fdSJakub Kicinski 	int send_len = 10;
787d87d67fdSJakub Kicinski 	char buf[10];
788d87d67fdSJakub Kicinski 	int p[2];
789d87d67fdSJakub Kicinski 
7905c7e49beSJakub Kicinski 	if (self->notls)
7915c7e49beSJakub Kicinski 		SKIP(return, "no TLS support");
7925c7e49beSJakub Kicinski 
793d87d67fdSJakub Kicinski 	ASSERT_GE(pipe(p), 0);
794d87d67fdSJakub Kicinski 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
795d87d67fdSJakub Kicinski 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
796d87d67fdSJakub Kicinski 	EXPECT_EQ(errno, EINVAL);
797d87d67fdSJakub Kicinski 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
798d87d67fdSJakub Kicinski 	EXPECT_EQ(errno, EIO);
799d87d67fdSJakub Kicinski 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
800d87d67fdSJakub Kicinski 				buf, sizeof(buf), MSG_WAITALL),
801d87d67fdSJakub Kicinski 		  send_len);
802d87d67fdSJakub Kicinski 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
803d87d67fdSJakub Kicinski }
804d87d67fdSJakub Kicinski 
805d87d67fdSJakub Kicinski TEST_F(tls, splice_dec_cmsg_to_pipe)
806d87d67fdSJakub Kicinski {
807d87d67fdSJakub Kicinski 	char *test_str = "test_read";
808d87d67fdSJakub Kicinski 	char record_type = 100;
809d87d67fdSJakub Kicinski 	int send_len = 10;
810d87d67fdSJakub Kicinski 	char buf[10];
811d87d67fdSJakub Kicinski 	int p[2];
812d87d67fdSJakub Kicinski 
8135c7e49beSJakub Kicinski 	if (self->notls)
8145c7e49beSJakub Kicinski 		SKIP(return, "no TLS support");
8155c7e49beSJakub Kicinski 
816d87d67fdSJakub Kicinski 	ASSERT_GE(pipe(p), 0);
817d87d67fdSJakub Kicinski 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
818d87d67fdSJakub Kicinski 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
819d87d67fdSJakub Kicinski 	EXPECT_EQ(errno, EIO);
820d87d67fdSJakub Kicinski 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
821d87d67fdSJakub Kicinski 	EXPECT_EQ(errno, EINVAL);
822d87d67fdSJakub Kicinski 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
823d87d67fdSJakub Kicinski 				buf, sizeof(buf), MSG_WAITALL),
824d87d67fdSJakub Kicinski 		  send_len);
825d87d67fdSJakub Kicinski 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
826d87d67fdSJakub Kicinski }
827d87d67fdSJakub Kicinski 
828274af0f9SJakub Kicinski TEST_F(tls, recv_and_splice)
829274af0f9SJakub Kicinski {
830274af0f9SJakub Kicinski 	int send_len = TLS_PAYLOAD_MAX_LEN;
831274af0f9SJakub Kicinski 	char mem_send[TLS_PAYLOAD_MAX_LEN];
832274af0f9SJakub Kicinski 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
833274af0f9SJakub Kicinski 	int half = send_len / 2;
834274af0f9SJakub Kicinski 	int p[2];
835274af0f9SJakub Kicinski 
836274af0f9SJakub Kicinski 	ASSERT_GE(pipe(p), 0);
837274af0f9SJakub Kicinski 	EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
838274af0f9SJakub Kicinski 	/* Recv hald of the record, splice the other half */
839274af0f9SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, mem_recv, half, MSG_WAITALL), half);
840274af0f9SJakub Kicinski 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, half, SPLICE_F_NONBLOCK),
841274af0f9SJakub Kicinski 		  half);
842274af0f9SJakub Kicinski 	EXPECT_EQ(read(p[0], &mem_recv[half], half), half);
843274af0f9SJakub Kicinski 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
844274af0f9SJakub Kicinski }
845274af0f9SJakub Kicinski 
846274af0f9SJakub Kicinski TEST_F(tls, peek_and_splice)
847274af0f9SJakub Kicinski {
848274af0f9SJakub Kicinski 	int send_len = TLS_PAYLOAD_MAX_LEN;
849274af0f9SJakub Kicinski 	char mem_send[TLS_PAYLOAD_MAX_LEN];
850274af0f9SJakub Kicinski 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
851274af0f9SJakub Kicinski 	int chunk = TLS_PAYLOAD_MAX_LEN / 4;
852274af0f9SJakub Kicinski 	int n, i, p[2];
853274af0f9SJakub Kicinski 
854274af0f9SJakub Kicinski 	memrnd(mem_send, sizeof(mem_send));
855274af0f9SJakub Kicinski 
856274af0f9SJakub Kicinski 	ASSERT_GE(pipe(p), 0);
857274af0f9SJakub Kicinski 	for (i = 0; i < 4; i++)
858274af0f9SJakub Kicinski 		EXPECT_EQ(send(self->fd, &mem_send[chunk * i], chunk, 0),
859274af0f9SJakub Kicinski 			  chunk);
860274af0f9SJakub Kicinski 
861274af0f9SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, mem_recv, chunk * 5 / 2,
862274af0f9SJakub Kicinski 		       MSG_WAITALL | MSG_PEEK),
863274af0f9SJakub Kicinski 		  chunk * 5 / 2);
864274af0f9SJakub Kicinski 	EXPECT_EQ(memcmp(mem_send, mem_recv, chunk * 5 / 2), 0);
865274af0f9SJakub Kicinski 
866274af0f9SJakub Kicinski 	n = 0;
867274af0f9SJakub Kicinski 	while (n < send_len) {
868274af0f9SJakub Kicinski 		i = splice(self->cfd, NULL, p[1], NULL, send_len - n, 0);
869274af0f9SJakub Kicinski 		EXPECT_GT(i, 0);
870274af0f9SJakub Kicinski 		n += i;
871274af0f9SJakub Kicinski 	}
872274af0f9SJakub Kicinski 	EXPECT_EQ(n, send_len);
873274af0f9SJakub Kicinski 	EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
874274af0f9SJakub Kicinski 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
875274af0f9SJakub Kicinski }
876274af0f9SJakub Kicinski 
8777f657d5bSDave Watson TEST_F(tls, recvmsg_single)
8787f657d5bSDave Watson {
8797f657d5bSDave Watson 	char const *test_str = "test_recvmsg_single";
8807f657d5bSDave Watson 	int send_len = strlen(test_str) + 1;
8817f657d5bSDave Watson 	char buf[20];
8827f657d5bSDave Watson 	struct msghdr hdr;
8837f657d5bSDave Watson 	struct iovec vec;
8847f657d5bSDave Watson 
8857f657d5bSDave Watson 	memset(&hdr, 0, sizeof(hdr));
8867f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
8877f657d5bSDave Watson 	vec.iov_base = (char *)buf;
8887f657d5bSDave Watson 	vec.iov_len = send_len;
8897f657d5bSDave Watson 	hdr.msg_iovlen = 1;
8907f657d5bSDave Watson 	hdr.msg_iov = &vec;
8917f657d5bSDave Watson 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
8927f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
8937f657d5bSDave Watson }
8947f657d5bSDave Watson 
8957f657d5bSDave Watson TEST_F(tls, recvmsg_single_max)
8967f657d5bSDave Watson {
8977f657d5bSDave Watson 	int send_len = TLS_PAYLOAD_MAX_LEN;
8987f657d5bSDave Watson 	char send_mem[TLS_PAYLOAD_MAX_LEN];
8997f657d5bSDave Watson 	char recv_mem[TLS_PAYLOAD_MAX_LEN];
9007f657d5bSDave Watson 	struct iovec vec;
9017f657d5bSDave Watson 	struct msghdr hdr;
9027f657d5bSDave Watson 
903baa00119SJakub Kicinski 	memrnd(send_mem, sizeof(send_mem));
904baa00119SJakub Kicinski 
9057f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, send_mem, send_len, 0), send_len);
9067f657d5bSDave Watson 	vec.iov_base = (char *)recv_mem;
9077f657d5bSDave Watson 	vec.iov_len = TLS_PAYLOAD_MAX_LEN;
9087f657d5bSDave Watson 
9097f657d5bSDave Watson 	hdr.msg_iovlen = 1;
9107f657d5bSDave Watson 	hdr.msg_iov = &vec;
9117f657d5bSDave Watson 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
9127f657d5bSDave Watson 	EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
9137f657d5bSDave Watson }
9147f657d5bSDave Watson 
9157f657d5bSDave Watson TEST_F(tls, recvmsg_multiple)
9167f657d5bSDave Watson {
9177f657d5bSDave Watson 	unsigned int msg_iovlen = 1024;
9187f657d5bSDave Watson 	struct iovec vec[1024];
9197f657d5bSDave Watson 	char *iov_base[1024];
9207f657d5bSDave Watson 	unsigned int iov_len = 16;
9217f657d5bSDave Watson 	int send_len = 1 << 14;
9227f657d5bSDave Watson 	char buf[1 << 14];
9237f657d5bSDave Watson 	struct msghdr hdr;
9247f657d5bSDave Watson 	int i;
9257f657d5bSDave Watson 
926baa00119SJakub Kicinski 	memrnd(buf, sizeof(buf));
927baa00119SJakub Kicinski 
9287f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, buf, send_len, 0), send_len);
9297f657d5bSDave Watson 	for (i = 0; i < msg_iovlen; i++) {
9307f657d5bSDave Watson 		iov_base[i] = (char *)malloc(iov_len);
9317f657d5bSDave Watson 		vec[i].iov_base = iov_base[i];
9327f657d5bSDave Watson 		vec[i].iov_len = iov_len;
9337f657d5bSDave Watson 	}
9347f657d5bSDave Watson 
9357f657d5bSDave Watson 	hdr.msg_iovlen = msg_iovlen;
9367f657d5bSDave Watson 	hdr.msg_iov = vec;
9377f657d5bSDave Watson 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
9387f657d5bSDave Watson 
9397f657d5bSDave Watson 	for (i = 0; i < msg_iovlen; i++)
9407f657d5bSDave Watson 		free(iov_base[i]);
9417f657d5bSDave Watson }
9427f657d5bSDave Watson 
9437f657d5bSDave Watson TEST_F(tls, single_send_multiple_recv)
9447f657d5bSDave Watson {
9457f657d5bSDave Watson 	unsigned int total_len = TLS_PAYLOAD_MAX_LEN * 2;
9467f657d5bSDave Watson 	unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
9477f657d5bSDave Watson 	char send_mem[TLS_PAYLOAD_MAX_LEN * 2];
9487f657d5bSDave Watson 	char recv_mem[TLS_PAYLOAD_MAX_LEN * 2];
9497f657d5bSDave Watson 
950baa00119SJakub Kicinski 	memrnd(send_mem, sizeof(send_mem));
951baa00119SJakub Kicinski 
9527f657d5bSDave Watson 	EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
9537f657d5bSDave Watson 	memset(recv_mem, 0, total_len);
9547f657d5bSDave Watson 
9557f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
9567f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, recv_mem + send_len, send_len, 0), -1);
9577f657d5bSDave Watson 	EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
9587f657d5bSDave Watson }
9597f657d5bSDave Watson 
9607f657d5bSDave Watson TEST_F(tls, multiple_send_single_recv)
9617f657d5bSDave Watson {
9627f657d5bSDave Watson 	unsigned int total_len = 2 * 10;
9637f657d5bSDave Watson 	unsigned int send_len = 10;
9647f657d5bSDave Watson 	char recv_mem[2 * 10];
9657f657d5bSDave Watson 	char send_mem[10];
9667f657d5bSDave Watson 
96786c591fbSJakub Kicinski 	memrnd(send_mem, sizeof(send_mem));
96886c591fbSJakub Kicinski 
9697f657d5bSDave Watson 	EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
9707f657d5bSDave Watson 	EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
9717f657d5bSDave Watson 	memset(recv_mem, 0, total_len);
9720185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, recv_mem, total_len, MSG_WAITALL), total_len);
9737f657d5bSDave Watson 
9747f657d5bSDave Watson 	EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
9757f657d5bSDave Watson 	EXPECT_EQ(memcmp(send_mem, recv_mem + send_len, send_len), 0);
9767f657d5bSDave Watson }
9777f657d5bSDave Watson 
978043556d0SJakub Kicinski TEST_F(tls, single_send_multiple_recv_non_align)
979043556d0SJakub Kicinski {
980043556d0SJakub Kicinski 	const unsigned int total_len = 15;
981043556d0SJakub Kicinski 	const unsigned int recv_len = 10;
982043556d0SJakub Kicinski 	char recv_mem[recv_len * 2];
983043556d0SJakub Kicinski 	char send_mem[total_len];
984043556d0SJakub Kicinski 
98586c591fbSJakub Kicinski 	memrnd(send_mem, sizeof(send_mem));
98686c591fbSJakub Kicinski 
987043556d0SJakub Kicinski 	EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
988043556d0SJakub Kicinski 	memset(recv_mem, 0, total_len);
989043556d0SJakub Kicinski 
990043556d0SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, recv_len, 0), recv_len);
991043556d0SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem + recv_len, recv_len, 0), 5);
992043556d0SJakub Kicinski 	EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
993043556d0SJakub Kicinski }
994043556d0SJakub Kicinski 
9957f657d5bSDave Watson TEST_F(tls, recv_partial)
9967f657d5bSDave Watson {
9977f657d5bSDave Watson 	char const *test_str = "test_read_partial";
9987f657d5bSDave Watson 	char const *test_str_first = "test_read";
9997f657d5bSDave Watson 	char const *test_str_second = "_partial";
10007f657d5bSDave Watson 	int send_len = strlen(test_str) + 1;
10017f657d5bSDave Watson 	char recv_mem[18];
10027f657d5bSDave Watson 
10037f657d5bSDave Watson 	memset(recv_mem, 0, sizeof(recv_mem));
10047f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
100549d82106SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, strlen(test_str_first),
100649d82106SJakub Kicinski 		       MSG_WAITALL), strlen(test_str_first));
10077f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str_first, recv_mem, strlen(test_str_first)), 0);
10087f657d5bSDave Watson 	memset(recv_mem, 0, sizeof(recv_mem));
100949d82106SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, strlen(test_str_second),
101049d82106SJakub Kicinski 		       MSG_WAITALL), strlen(test_str_second));
10117f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str_second, recv_mem, strlen(test_str_second)),
10127f657d5bSDave Watson 		  0);
10137f657d5bSDave Watson }
10147f657d5bSDave Watson 
10157f657d5bSDave Watson TEST_F(tls, recv_nonblock)
10167f657d5bSDave Watson {
10177f657d5bSDave Watson 	char buf[4096];
10187f657d5bSDave Watson 	bool err;
10197f657d5bSDave Watson 
10207f657d5bSDave Watson 	EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_DONTWAIT), -1);
10217f657d5bSDave Watson 	err = (errno == EAGAIN || errno == EWOULDBLOCK);
10227f657d5bSDave Watson 	EXPECT_EQ(err, true);
10237f657d5bSDave Watson }
10247f657d5bSDave Watson 
10257f657d5bSDave Watson TEST_F(tls, recv_peek)
10267f657d5bSDave Watson {
10277f657d5bSDave Watson 	char const *test_str = "test_read_peek";
10287f657d5bSDave Watson 	int send_len = strlen(test_str) + 1;
10297f657d5bSDave Watson 	char buf[15];
10307f657d5bSDave Watson 
10317f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
103286c591fbSJakub Kicinski 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_PEEK), send_len);
10337f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
10347f657d5bSDave Watson 	memset(buf, 0, sizeof(buf));
103586c591fbSJakub Kicinski 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), send_len);
10367f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
10377f657d5bSDave Watson }
10387f657d5bSDave Watson 
10397f657d5bSDave Watson TEST_F(tls, recv_peek_multiple)
10407f657d5bSDave Watson {
10417f657d5bSDave Watson 	char const *test_str = "test_read_peek";
10427f657d5bSDave Watson 	int send_len = strlen(test_str) + 1;
10437f657d5bSDave Watson 	unsigned int num_peeks = 100;
10447f657d5bSDave Watson 	char buf[15];
10457f657d5bSDave Watson 	int i;
10467f657d5bSDave Watson 
10477f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
10487f657d5bSDave Watson 	for (i = 0; i < num_peeks; i++) {
10497f657d5bSDave Watson 		EXPECT_NE(recv(self->cfd, buf, send_len, MSG_PEEK), -1);
10507f657d5bSDave Watson 		EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
10517f657d5bSDave Watson 		memset(buf, 0, sizeof(buf));
10527f657d5bSDave Watson 	}
10537f657d5bSDave Watson 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
10547f657d5bSDave Watson 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
10557f657d5bSDave Watson }
10567f657d5bSDave Watson 
105750c6b58aSDaniel Borkmann TEST_F(tls, recv_peek_multiple_records)
105850c6b58aSDaniel Borkmann {
105950c6b58aSDaniel Borkmann 	char const *test_str = "test_read_peek_mult_recs";
106050c6b58aSDaniel Borkmann 	char const *test_str_first = "test_read_peek";
106150c6b58aSDaniel Borkmann 	char const *test_str_second = "_mult_recs";
106250c6b58aSDaniel Borkmann 	int len;
106350c6b58aSDaniel Borkmann 	char buf[64];
106450c6b58aSDaniel Borkmann 
106550c6b58aSDaniel Borkmann 	len = strlen(test_str_first);
106650c6b58aSDaniel Borkmann 	EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
106750c6b58aSDaniel Borkmann 
106850c6b58aSDaniel Borkmann 	len = strlen(test_str_second) + 1;
106950c6b58aSDaniel Borkmann 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
107050c6b58aSDaniel Borkmann 
10710ed3015cSVakul Garg 	len = strlen(test_str_first);
107250c6b58aSDaniel Borkmann 	memset(buf, 0, len);
10730ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
107450c6b58aSDaniel Borkmann 
107550c6b58aSDaniel Borkmann 	/* MSG_PEEK can only peek into the current record. */
10760ed3015cSVakul Garg 	len = strlen(test_str_first);
107750c6b58aSDaniel Borkmann 	EXPECT_EQ(memcmp(test_str_first, buf, len), 0);
107850c6b58aSDaniel Borkmann 
10790ed3015cSVakul Garg 	len = strlen(test_str) + 1;
108050c6b58aSDaniel Borkmann 	memset(buf, 0, len);
10810ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
108250c6b58aSDaniel Borkmann 
108350c6b58aSDaniel Borkmann 	/* Non-MSG_PEEK will advance strparser (and therefore record)
108450c6b58aSDaniel Borkmann 	 * however.
108550c6b58aSDaniel Borkmann 	 */
108650c6b58aSDaniel Borkmann 	len = strlen(test_str) + 1;
108750c6b58aSDaniel Borkmann 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
108850c6b58aSDaniel Borkmann 
108950c6b58aSDaniel Borkmann 	/* MSG_MORE will hold current record open, so later MSG_PEEK
109050c6b58aSDaniel Borkmann 	 * will see everything.
109150c6b58aSDaniel Borkmann 	 */
109250c6b58aSDaniel Borkmann 	len = strlen(test_str_first);
109350c6b58aSDaniel Borkmann 	EXPECT_EQ(send(self->fd, test_str_first, len, MSG_MORE), len);
109450c6b58aSDaniel Borkmann 
109550c6b58aSDaniel Borkmann 	len = strlen(test_str_second) + 1;
109650c6b58aSDaniel Borkmann 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
109750c6b58aSDaniel Borkmann 
10980ed3015cSVakul Garg 	len = strlen(test_str) + 1;
109950c6b58aSDaniel Borkmann 	memset(buf, 0, len);
11000ed3015cSVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
110150c6b58aSDaniel Borkmann 
110250c6b58aSDaniel Borkmann 	len = strlen(test_str) + 1;
110350c6b58aSDaniel Borkmann 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
110450c6b58aSDaniel Borkmann }
110550c6b58aSDaniel Borkmann 
1106c2ad647cSVakul Garg TEST_F(tls, recv_peek_large_buf_mult_recs)
1107c2ad647cSVakul Garg {
1108c2ad647cSVakul Garg 	char const *test_str = "test_read_peek_mult_recs";
1109c2ad647cSVakul Garg 	char const *test_str_first = "test_read_peek";
1110c2ad647cSVakul Garg 	char const *test_str_second = "_mult_recs";
1111c2ad647cSVakul Garg 	int len;
1112c2ad647cSVakul Garg 	char buf[64];
1113c2ad647cSVakul Garg 
1114c2ad647cSVakul Garg 	len = strlen(test_str_first);
1115c2ad647cSVakul Garg 	EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
1116c2ad647cSVakul Garg 
1117c2ad647cSVakul Garg 	len = strlen(test_str_second) + 1;
1118c2ad647cSVakul Garg 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1119c2ad647cSVakul Garg 
1120cea3bfb3SVakul Garg 	len = strlen(test_str) + 1;
1121c2ad647cSVakul Garg 	memset(buf, 0, len);
1122cea3bfb3SVakul Garg 	EXPECT_NE((len = recv(self->cfd, buf, len,
1123cea3bfb3SVakul Garg 			      MSG_PEEK | MSG_WAITALL)), -1);
1124c2ad647cSVakul Garg 	len = strlen(test_str) + 1;
1125c2ad647cSVakul Garg 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
1126c2ad647cSVakul Garg }
1127c2ad647cSVakul Garg 
11287718a855SJakub Kicinski TEST_F(tls, recv_lowat)
11297718a855SJakub Kicinski {
11307718a855SJakub Kicinski 	char send_mem[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
11317718a855SJakub Kicinski 	char recv_mem[20];
11327718a855SJakub Kicinski 	int lowat = 8;
11337718a855SJakub Kicinski 
11347718a855SJakub Kicinski 	EXPECT_EQ(send(self->fd, send_mem, 10, 0), 10);
11357718a855SJakub Kicinski 	EXPECT_EQ(send(self->fd, send_mem, 5, 0), 5);
11367718a855SJakub Kicinski 
11377718a855SJakub Kicinski 	memset(recv_mem, 0, 20);
11387718a855SJakub Kicinski 	EXPECT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_RCVLOWAT,
11397718a855SJakub Kicinski 			     &lowat, sizeof(lowat)), 0);
11407718a855SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, 1, MSG_WAITALL), 1);
11417718a855SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem + 1, 6, MSG_WAITALL), 6);
11427718a855SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem + 7, 10, 0), 8);
11437718a855SJakub Kicinski 
11447718a855SJakub Kicinski 	EXPECT_EQ(memcmp(send_mem, recv_mem, 10), 0);
11457718a855SJakub Kicinski 	EXPECT_EQ(memcmp(send_mem, recv_mem + 10, 5), 0);
11467718a855SJakub Kicinski }
1147e366fa43SDavid S. Miller 
114865d41fb3SJakub Kicinski TEST_F(tls, bidir)
114965d41fb3SJakub Kicinski {
115065d41fb3SJakub Kicinski 	char const *test_str = "test_read";
115165d41fb3SJakub Kicinski 	int send_len = 10;
115265d41fb3SJakub Kicinski 	char buf[10];
115365d41fb3SJakub Kicinski 	int ret;
115465d41fb3SJakub Kicinski 
1155e29903c4SJakub Kicinski 	if (!self->notls) {
1156291c53e4SJakub Kicinski 		struct tls_crypto_info_keys tls12;
1157e29903c4SJakub Kicinski 
1158291c53e4SJakub Kicinski 		tls_crypto_info_init(variant->tls_version, variant->cipher_type,
1159291c53e4SJakub Kicinski 				     &tls12);
116065d41fb3SJakub Kicinski 
1161e29903c4SJakub Kicinski 		ret = setsockopt(self->fd, SOL_TLS, TLS_RX, &tls12,
1162291c53e4SJakub Kicinski 				 tls12.len);
116365d41fb3SJakub Kicinski 		ASSERT_EQ(ret, 0);
116465d41fb3SJakub Kicinski 
1165e29903c4SJakub Kicinski 		ret = setsockopt(self->cfd, SOL_TLS, TLS_TX, &tls12,
1166291c53e4SJakub Kicinski 				 tls12.len);
116765d41fb3SJakub Kicinski 		ASSERT_EQ(ret, 0);
1168e29903c4SJakub Kicinski 	}
116965d41fb3SJakub Kicinski 
117065d41fb3SJakub Kicinski 	ASSERT_EQ(strlen(test_str) + 1, send_len);
117165d41fb3SJakub Kicinski 
117265d41fb3SJakub Kicinski 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
117365d41fb3SJakub Kicinski 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
117465d41fb3SJakub Kicinski 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
117565d41fb3SJakub Kicinski 
117665d41fb3SJakub Kicinski 	memset(buf, 0, sizeof(buf));
117765d41fb3SJakub Kicinski 
117865d41fb3SJakub Kicinski 	EXPECT_EQ(send(self->cfd, test_str, send_len, 0), send_len);
117965d41fb3SJakub Kicinski 	EXPECT_NE(recv(self->fd, buf, send_len, 0), -1);
118065d41fb3SJakub Kicinski 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
118165d41fb3SJakub Kicinski };
118265d41fb3SJakub Kicinski 
11837f657d5bSDave Watson TEST_F(tls, pollin)
11847f657d5bSDave Watson {
11857f657d5bSDave Watson 	char const *test_str = "test_poll";
11867f657d5bSDave Watson 	struct pollfd fd = { 0, 0, 0 };
11877f657d5bSDave Watson 	char buf[10];
11887f657d5bSDave Watson 	int send_len = 10;
11897f657d5bSDave Watson 
11907f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
11917f657d5bSDave Watson 	fd.fd = self->cfd;
11927f657d5bSDave Watson 	fd.events = POLLIN;
11937f657d5bSDave Watson 
11947f657d5bSDave Watson 	EXPECT_EQ(poll(&fd, 1, 20), 1);
11957f657d5bSDave Watson 	EXPECT_EQ(fd.revents & POLLIN, 1);
11960185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
11977f657d5bSDave Watson 	/* Test timing out */
11987f657d5bSDave Watson 	EXPECT_EQ(poll(&fd, 1, 20), 0);
11997f657d5bSDave Watson }
12007f657d5bSDave Watson 
12017f657d5bSDave Watson TEST_F(tls, poll_wait)
12027f657d5bSDave Watson {
12037f657d5bSDave Watson 	char const *test_str = "test_poll_wait";
12047f657d5bSDave Watson 	int send_len = strlen(test_str) + 1;
12057f657d5bSDave Watson 	struct pollfd fd = { 0, 0, 0 };
12067f657d5bSDave Watson 	char recv_mem[15];
12077f657d5bSDave Watson 
12087f657d5bSDave Watson 	fd.fd = self->cfd;
12097f657d5bSDave Watson 	fd.events = POLLIN;
12107f657d5bSDave Watson 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
12117f657d5bSDave Watson 	/* Set timeout to inf. secs */
12127f657d5bSDave Watson 	EXPECT_EQ(poll(&fd, 1, -1), 1);
12137f657d5bSDave Watson 	EXPECT_EQ(fd.revents & POLLIN, 1);
12140185e2e6SVakul Garg 	EXPECT_EQ(recv(self->cfd, recv_mem, send_len, MSG_WAITALL), send_len);
12157f657d5bSDave Watson }
12167f657d5bSDave Watson 
121781a89ef6SJakub Kicinski TEST_F(tls, poll_wait_split)
121881a89ef6SJakub Kicinski {
121981a89ef6SJakub Kicinski 	struct pollfd fd = { 0, 0, 0 };
122081a89ef6SJakub Kicinski 	char send_mem[20] = {};
122181a89ef6SJakub Kicinski 	char recv_mem[15];
122281a89ef6SJakub Kicinski 
122381a89ef6SJakub Kicinski 	fd.fd = self->cfd;
122481a89ef6SJakub Kicinski 	fd.events = POLLIN;
122581a89ef6SJakub Kicinski 	/* Send 20 bytes */
122681a89ef6SJakub Kicinski 	EXPECT_EQ(send(self->fd, send_mem, sizeof(send_mem), 0),
122781a89ef6SJakub Kicinski 		  sizeof(send_mem));
122881a89ef6SJakub Kicinski 	/* Poll with inf. timeout */
122981a89ef6SJakub Kicinski 	EXPECT_EQ(poll(&fd, 1, -1), 1);
123081a89ef6SJakub Kicinski 	EXPECT_EQ(fd.revents & POLLIN, 1);
123181a89ef6SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), MSG_WAITALL),
123281a89ef6SJakub Kicinski 		  sizeof(recv_mem));
123381a89ef6SJakub Kicinski 
123481a89ef6SJakub Kicinski 	/* Now the remaining 5 bytes of record data are in TLS ULP */
123581a89ef6SJakub Kicinski 	fd.fd = self->cfd;
123681a89ef6SJakub Kicinski 	fd.events = POLLIN;
123781a89ef6SJakub Kicinski 	EXPECT_EQ(poll(&fd, 1, -1), 1);
123881a89ef6SJakub Kicinski 	EXPECT_EQ(fd.revents & POLLIN, 1);
123981a89ef6SJakub Kicinski 	EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), 0),
124081a89ef6SJakub Kicinski 		  sizeof(send_mem) - sizeof(recv_mem));
124181a89ef6SJakub Kicinski }
124281a89ef6SJakub Kicinski 
12437f657d5bSDave Watson TEST_F(tls, blocking)
12447f657d5bSDave Watson {
12457f657d5bSDave Watson 	size_t data = 100000;
12467f657d5bSDave Watson 	int res = fork();
12477f657d5bSDave Watson 
12487f657d5bSDave Watson 	EXPECT_NE(res, -1);
12497f657d5bSDave Watson 
12507f657d5bSDave Watson 	if (res) {
12517f657d5bSDave Watson 		/* parent */
12527f657d5bSDave Watson 		size_t left = data;
12537f657d5bSDave Watson 		char buf[16384];
12547f657d5bSDave Watson 		int status;
12557f657d5bSDave Watson 		int pid2;
12567f657d5bSDave Watson 
12577f657d5bSDave Watson 		while (left) {
12587f657d5bSDave Watson 			int res = send(self->fd, buf,
12597f657d5bSDave Watson 				       left > 16384 ? 16384 : left, 0);
12607f657d5bSDave Watson 
12617f657d5bSDave Watson 			EXPECT_GE(res, 0);
12627f657d5bSDave Watson 			left -= res;
12637f657d5bSDave Watson 		}
12647f657d5bSDave Watson 
12657f657d5bSDave Watson 		pid2 = wait(&status);
12667f657d5bSDave Watson 		EXPECT_EQ(status, 0);
12677f657d5bSDave Watson 		EXPECT_EQ(res, pid2);
12687f657d5bSDave Watson 	} else {
12697f657d5bSDave Watson 		/* child */
12707f657d5bSDave Watson 		size_t left = data;
12717f657d5bSDave Watson 		char buf[16384];
12727f657d5bSDave Watson 
12737f657d5bSDave Watson 		while (left) {
12747f657d5bSDave Watson 			int res = recv(self->cfd, buf,
12757f657d5bSDave Watson 				       left > 16384 ? 16384 : left, 0);
12767f657d5bSDave Watson 
12777f657d5bSDave Watson 			EXPECT_GE(res, 0);
12787f657d5bSDave Watson 			left -= res;
12797f657d5bSDave Watson 		}
12807f657d5bSDave Watson 	}
12817f657d5bSDave Watson }
12827f657d5bSDave Watson 
12837f657d5bSDave Watson TEST_F(tls, nonblocking)
12847f657d5bSDave Watson {
12857f657d5bSDave Watson 	size_t data = 100000;
12867f657d5bSDave Watson 	int sendbuf = 100;
12877f657d5bSDave Watson 	int flags;
12887f657d5bSDave Watson 	int res;
12897f657d5bSDave Watson 
12907f657d5bSDave Watson 	flags = fcntl(self->fd, F_GETFL, 0);
12917f657d5bSDave Watson 	fcntl(self->fd, F_SETFL, flags | O_NONBLOCK);
12927f657d5bSDave Watson 	fcntl(self->cfd, F_SETFL, flags | O_NONBLOCK);
12937f657d5bSDave Watson 
12947f657d5bSDave Watson 	/* Ensure nonblocking behavior by imposing a small send
12957f657d5bSDave Watson 	 * buffer.
12967f657d5bSDave Watson 	 */
12977f657d5bSDave Watson 	EXPECT_EQ(setsockopt(self->fd, SOL_SOCKET, SO_SNDBUF,
12987f657d5bSDave Watson 			     &sendbuf, sizeof(sendbuf)), 0);
12997f657d5bSDave Watson 
13007f657d5bSDave Watson 	res = fork();
13017f657d5bSDave Watson 	EXPECT_NE(res, -1);
13027f657d5bSDave Watson 
13037f657d5bSDave Watson 	if (res) {
13047f657d5bSDave Watson 		/* parent */
13057f657d5bSDave Watson 		bool eagain = false;
13067f657d5bSDave Watson 		size_t left = data;
13077f657d5bSDave Watson 		char buf[16384];
13087f657d5bSDave Watson 		int status;
13097f657d5bSDave Watson 		int pid2;
13107f657d5bSDave Watson 
13117f657d5bSDave Watson 		while (left) {
13127f657d5bSDave Watson 			int res = send(self->fd, buf,
13137f657d5bSDave Watson 				       left > 16384 ? 16384 : left, 0);
13147f657d5bSDave Watson 
13157f657d5bSDave Watson 			if (res == -1 && errno == EAGAIN) {
13167f657d5bSDave Watson 				eagain = true;
13177f657d5bSDave Watson 				usleep(10000);
13187f657d5bSDave Watson 				continue;
13197f657d5bSDave Watson 			}
13207f657d5bSDave Watson 			EXPECT_GE(res, 0);
13217f657d5bSDave Watson 			left -= res;
13227f657d5bSDave Watson 		}
13237f657d5bSDave Watson 
13247f657d5bSDave Watson 		EXPECT_TRUE(eagain);
13257f657d5bSDave Watson 		pid2 = wait(&status);
13267f657d5bSDave Watson 
13277f657d5bSDave Watson 		EXPECT_EQ(status, 0);
13287f657d5bSDave Watson 		EXPECT_EQ(res, pid2);
13297f657d5bSDave Watson 	} else {
13307f657d5bSDave Watson 		/* child */
13317f657d5bSDave Watson 		bool eagain = false;
13327f657d5bSDave Watson 		size_t left = data;
13337f657d5bSDave Watson 		char buf[16384];
13347f657d5bSDave Watson 
13357f657d5bSDave Watson 		while (left) {
13367f657d5bSDave Watson 			int res = recv(self->cfd, buf,
13377f657d5bSDave Watson 				       left > 16384 ? 16384 : left, 0);
13387f657d5bSDave Watson 
13397f657d5bSDave Watson 			if (res == -1 && errno == EAGAIN) {
13407f657d5bSDave Watson 				eagain = true;
13417f657d5bSDave Watson 				usleep(10000);
13427f657d5bSDave Watson 				continue;
13437f657d5bSDave Watson 			}
13447f657d5bSDave Watson 			EXPECT_GE(res, 0);
13457f657d5bSDave Watson 			left -= res;
13467f657d5bSDave Watson 		}
13477f657d5bSDave Watson 		EXPECT_TRUE(eagain);
13487f657d5bSDave Watson 	}
13497f657d5bSDave Watson }
13507f657d5bSDave Watson 
135141098af5SJakub Kicinski static void
135241098af5SJakub Kicinski test_mutliproc(struct __test_metadata *_metadata, struct _test_data_tls *self,
135341098af5SJakub Kicinski 	       bool sendpg, unsigned int n_readers, unsigned int n_writers)
135441098af5SJakub Kicinski {
135541098af5SJakub Kicinski 	const unsigned int n_children = n_readers + n_writers;
135641098af5SJakub Kicinski 	const size_t data = 6 * 1000 * 1000;
135741098af5SJakub Kicinski 	const size_t file_sz = data / 100;
135841098af5SJakub Kicinski 	size_t read_bias, write_bias;
135941098af5SJakub Kicinski 	int i, fd, child_id;
136041098af5SJakub Kicinski 	char buf[file_sz];
136141098af5SJakub Kicinski 	pid_t pid;
136241098af5SJakub Kicinski 
136341098af5SJakub Kicinski 	/* Only allow multiples for simplicity */
136441098af5SJakub Kicinski 	ASSERT_EQ(!(n_readers % n_writers) || !(n_writers % n_readers), true);
136541098af5SJakub Kicinski 	read_bias = n_writers / n_readers ?: 1;
136641098af5SJakub Kicinski 	write_bias = n_readers / n_writers ?: 1;
136741098af5SJakub Kicinski 
136841098af5SJakub Kicinski 	/* prep a file to send */
136941098af5SJakub Kicinski 	fd = open("/tmp/", O_TMPFILE | O_RDWR, 0600);
137041098af5SJakub Kicinski 	ASSERT_GE(fd, 0);
137141098af5SJakub Kicinski 
137241098af5SJakub Kicinski 	memset(buf, 0xac, file_sz);
137341098af5SJakub Kicinski 	ASSERT_EQ(write(fd, buf, file_sz), file_sz);
137441098af5SJakub Kicinski 
137541098af5SJakub Kicinski 	/* spawn children */
137641098af5SJakub Kicinski 	for (child_id = 0; child_id < n_children; child_id++) {
137741098af5SJakub Kicinski 		pid = fork();
137841098af5SJakub Kicinski 		ASSERT_NE(pid, -1);
137941098af5SJakub Kicinski 		if (!pid)
138041098af5SJakub Kicinski 			break;
138141098af5SJakub Kicinski 	}
138241098af5SJakub Kicinski 
138341098af5SJakub Kicinski 	/* parent waits for all children */
138441098af5SJakub Kicinski 	if (pid) {
138541098af5SJakub Kicinski 		for (i = 0; i < n_children; i++) {
138641098af5SJakub Kicinski 			int status;
138741098af5SJakub Kicinski 
138841098af5SJakub Kicinski 			wait(&status);
138941098af5SJakub Kicinski 			EXPECT_EQ(status, 0);
139041098af5SJakub Kicinski 		}
139141098af5SJakub Kicinski 
139241098af5SJakub Kicinski 		return;
139341098af5SJakub Kicinski 	}
139441098af5SJakub Kicinski 
139541098af5SJakub Kicinski 	/* Split threads for reading and writing */
139641098af5SJakub Kicinski 	if (child_id < n_readers) {
139741098af5SJakub Kicinski 		size_t left = data * read_bias;
139841098af5SJakub Kicinski 		char rb[8001];
139941098af5SJakub Kicinski 
140041098af5SJakub Kicinski 		while (left) {
140141098af5SJakub Kicinski 			int res;
140241098af5SJakub Kicinski 
140341098af5SJakub Kicinski 			res = recv(self->cfd, rb,
140441098af5SJakub Kicinski 				   left > sizeof(rb) ? sizeof(rb) : left, 0);
140541098af5SJakub Kicinski 
140641098af5SJakub Kicinski 			EXPECT_GE(res, 0);
140741098af5SJakub Kicinski 			left -= res;
140841098af5SJakub Kicinski 		}
140941098af5SJakub Kicinski 	} else {
141041098af5SJakub Kicinski 		size_t left = data * write_bias;
141141098af5SJakub Kicinski 
141241098af5SJakub Kicinski 		while (left) {
141341098af5SJakub Kicinski 			int res;
141441098af5SJakub Kicinski 
141541098af5SJakub Kicinski 			ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0);
141641098af5SJakub Kicinski 			if (sendpg)
141741098af5SJakub Kicinski 				res = sendfile(self->fd, fd, NULL,
141841098af5SJakub Kicinski 					       left > file_sz ? file_sz : left);
141941098af5SJakub Kicinski 			else
142041098af5SJakub Kicinski 				res = send(self->fd, buf,
142141098af5SJakub Kicinski 					   left > file_sz ? file_sz : left, 0);
142241098af5SJakub Kicinski 
142341098af5SJakub Kicinski 			EXPECT_GE(res, 0);
142441098af5SJakub Kicinski 			left -= res;
142541098af5SJakub Kicinski 		}
142641098af5SJakub Kicinski 	}
142741098af5SJakub Kicinski }
142841098af5SJakub Kicinski 
142941098af5SJakub Kicinski TEST_F(tls, mutliproc_even)
143041098af5SJakub Kicinski {
143141098af5SJakub Kicinski 	test_mutliproc(_metadata, self, false, 6, 6);
143241098af5SJakub Kicinski }
143341098af5SJakub Kicinski 
143441098af5SJakub Kicinski TEST_F(tls, mutliproc_readers)
143541098af5SJakub Kicinski {
143641098af5SJakub Kicinski 	test_mutliproc(_metadata, self, false, 4, 12);
143741098af5SJakub Kicinski }
143841098af5SJakub Kicinski 
143941098af5SJakub Kicinski TEST_F(tls, mutliproc_writers)
144041098af5SJakub Kicinski {
144141098af5SJakub Kicinski 	test_mutliproc(_metadata, self, false, 10, 2);
144241098af5SJakub Kicinski }
144341098af5SJakub Kicinski 
144441098af5SJakub Kicinski TEST_F(tls, mutliproc_sendpage_even)
144541098af5SJakub Kicinski {
144641098af5SJakub Kicinski 	test_mutliproc(_metadata, self, true, 6, 6);
144741098af5SJakub Kicinski }
144841098af5SJakub Kicinski 
144941098af5SJakub Kicinski TEST_F(tls, mutliproc_sendpage_readers)
145041098af5SJakub Kicinski {
145141098af5SJakub Kicinski 	test_mutliproc(_metadata, self, true, 4, 12);
145241098af5SJakub Kicinski }
145341098af5SJakub Kicinski 
145441098af5SJakub Kicinski TEST_F(tls, mutliproc_sendpage_writers)
145541098af5SJakub Kicinski {
145641098af5SJakub Kicinski 	test_mutliproc(_metadata, self, true, 10, 2);
145741098af5SJakub Kicinski }
145841098af5SJakub Kicinski 
14597f657d5bSDave Watson TEST_F(tls, control_msg)
14607f657d5bSDave Watson {
146131180adbSJakub Kicinski 	char *test_str = "test_read";
14627f657d5bSDave Watson 	char record_type = 100;
14637f657d5bSDave Watson 	int send_len = 10;
14647f657d5bSDave Watson 	char buf[10];
14657f657d5bSDave Watson 
146631180adbSJakub Kicinski 	if (self->notls)
146731180adbSJakub Kicinski 		SKIP(return, "no TLS support");
14687f657d5bSDave Watson 
146931180adbSJakub Kicinski 	EXPECT_EQ(tls_send_cmsg(self->fd, record_type, test_str, send_len, 0),
147031180adbSJakub Kicinski 		  send_len);
14717f657d5bSDave Watson 	/* Should fail because we didn't provide a control message */
14727f657d5bSDave Watson 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
14737f657d5bSDave Watson 
147431180adbSJakub Kicinski 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
147531180adbSJakub Kicinski 				buf, sizeof(buf), MSG_WAITALL | MSG_PEEK),
147631180adbSJakub Kicinski 		  send_len);
1477203ef5f1SVakul Garg 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1478203ef5f1SVakul Garg 
1479203ef5f1SVakul Garg 	/* Recv the message again without MSG_PEEK */
1480203ef5f1SVakul Garg 	memset(buf, 0, sizeof(buf));
1481203ef5f1SVakul Garg 
148231180adbSJakub Kicinski 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
148331180adbSJakub Kicinski 				buf, sizeof(buf), MSG_WAITALL),
148431180adbSJakub Kicinski 		  send_len);
14857f657d5bSDave Watson 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
14867f657d5bSDave Watson }
14877f657d5bSDave Watson 
14887b2a4c2aSSabrina Dubroca TEST_F(tls, control_msg_nomerge)
14897b2a4c2aSSabrina Dubroca {
14907b2a4c2aSSabrina Dubroca 	char *rec1 = "1111";
14917b2a4c2aSSabrina Dubroca 	char *rec2 = "2222";
14927b2a4c2aSSabrina Dubroca 	int send_len = 5;
14937b2a4c2aSSabrina Dubroca 	char buf[15];
14947b2a4c2aSSabrina Dubroca 
14957b2a4c2aSSabrina Dubroca 	if (self->notls)
14967b2a4c2aSSabrina Dubroca 		SKIP(return, "no TLS support");
14977b2a4c2aSSabrina Dubroca 
14987b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, rec1, send_len, 0), send_len);
14997b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, rec2, send_len, 0), send_len);
15007b2a4c2aSSabrina Dubroca 
15017b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, 100, buf, sizeof(buf), MSG_PEEK), send_len);
15027b2a4c2aSSabrina Dubroca 	EXPECT_EQ(memcmp(buf, rec1, send_len), 0);
15037b2a4c2aSSabrina Dubroca 
15047b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, 100, buf, sizeof(buf), MSG_PEEK), send_len);
15057b2a4c2aSSabrina Dubroca 	EXPECT_EQ(memcmp(buf, rec1, send_len), 0);
15067b2a4c2aSSabrina Dubroca 
15077b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, 100, buf, sizeof(buf), 0), send_len);
15087b2a4c2aSSabrina Dubroca 	EXPECT_EQ(memcmp(buf, rec1, send_len), 0);
15097b2a4c2aSSabrina Dubroca 
15107b2a4c2aSSabrina Dubroca 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, 100, buf, sizeof(buf), 0), send_len);
15117b2a4c2aSSabrina Dubroca 	EXPECT_EQ(memcmp(buf, rec2, send_len), 0);
15127b2a4c2aSSabrina Dubroca }
15137b2a4c2aSSabrina Dubroca 
15142bf61726SSabrina Dubroca TEST_F(tls, data_control_data)
15152bf61726SSabrina Dubroca {
15162bf61726SSabrina Dubroca 	char *rec1 = "1111";
15172bf61726SSabrina Dubroca 	char *rec2 = "2222";
15182bf61726SSabrina Dubroca 	char *rec3 = "3333";
15192bf61726SSabrina Dubroca 	int send_len = 5;
15202bf61726SSabrina Dubroca 	char buf[15];
15212bf61726SSabrina Dubroca 
15222bf61726SSabrina Dubroca 	if (self->notls)
15232bf61726SSabrina Dubroca 		SKIP(return, "no TLS support");
15242bf61726SSabrina Dubroca 
15252bf61726SSabrina Dubroca 	EXPECT_EQ(send(self->fd, rec1, send_len, 0), send_len);
15262bf61726SSabrina Dubroca 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, rec2, send_len, 0), send_len);
15272bf61726SSabrina Dubroca 	EXPECT_EQ(send(self->fd, rec3, send_len, 0), send_len);
15282bf61726SSabrina Dubroca 
15292bf61726SSabrina Dubroca 	EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_PEEK), send_len);
15302bf61726SSabrina Dubroca 	EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_PEEK), send_len);
15312bf61726SSabrina Dubroca }
15322bf61726SSabrina Dubroca 
1533d4d34185SJakub Kicinski TEST_F(tls, shutdown)
1534d4d34185SJakub Kicinski {
1535d4d34185SJakub Kicinski 	char const *test_str = "test_read";
1536d4d34185SJakub Kicinski 	int send_len = 10;
1537d4d34185SJakub Kicinski 	char buf[10];
1538d4d34185SJakub Kicinski 
1539d4d34185SJakub Kicinski 	ASSERT_EQ(strlen(test_str) + 1, send_len);
1540d4d34185SJakub Kicinski 
1541d4d34185SJakub Kicinski 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1542d4d34185SJakub Kicinski 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1543d4d34185SJakub Kicinski 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1544d4d34185SJakub Kicinski 
1545d4d34185SJakub Kicinski 	shutdown(self->fd, SHUT_RDWR);
1546d4d34185SJakub Kicinski 	shutdown(self->cfd, SHUT_RDWR);
1547d4d34185SJakub Kicinski }
1548d4d34185SJakub Kicinski 
1549d4d34185SJakub Kicinski TEST_F(tls, shutdown_unsent)
1550d4d34185SJakub Kicinski {
1551d4d34185SJakub Kicinski 	char const *test_str = "test_read";
1552d4d34185SJakub Kicinski 	int send_len = 10;
1553d4d34185SJakub Kicinski 
1554d4d34185SJakub Kicinski 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
1555d4d34185SJakub Kicinski 
1556d4d34185SJakub Kicinski 	shutdown(self->fd, SHUT_RDWR);
1557d4d34185SJakub Kicinski 	shutdown(self->cfd, SHUT_RDWR);
1558d4d34185SJakub Kicinski }
1559d4d34185SJakub Kicinski 
1560cd114d2eSJakub Kicinski TEST_F(tls, shutdown_reuse)
1561cd114d2eSJakub Kicinski {
1562cd114d2eSJakub Kicinski 	struct sockaddr_in addr;
1563cd114d2eSJakub Kicinski 	int ret;
1564cd114d2eSJakub Kicinski 
1565cd114d2eSJakub Kicinski 	shutdown(self->fd, SHUT_RDWR);
1566cd114d2eSJakub Kicinski 	shutdown(self->cfd, SHUT_RDWR);
1567cd114d2eSJakub Kicinski 	close(self->cfd);
1568cd114d2eSJakub Kicinski 
1569cd114d2eSJakub Kicinski 	addr.sin_family = AF_INET;
1570cd114d2eSJakub Kicinski 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
1571cd114d2eSJakub Kicinski 	addr.sin_port = 0;
1572cd114d2eSJakub Kicinski 
1573cd114d2eSJakub Kicinski 	ret = bind(self->fd, &addr, sizeof(addr));
1574cd114d2eSJakub Kicinski 	EXPECT_EQ(ret, 0);
1575cd114d2eSJakub Kicinski 	ret = listen(self->fd, 10);
1576cd114d2eSJakub Kicinski 	EXPECT_EQ(ret, -1);
1577cd114d2eSJakub Kicinski 	EXPECT_EQ(errno, EINVAL);
1578cd114d2eSJakub Kicinski 
1579cd114d2eSJakub Kicinski 	ret = connect(self->fd, &addr, sizeof(addr));
1580cd114d2eSJakub Kicinski 	EXPECT_EQ(ret, -1);
1581cd114d2eSJakub Kicinski 	EXPECT_EQ(errno, EISCONN);
1582cd114d2eSJakub Kicinski }
1583cd114d2eSJakub Kicinski 
1584f27ad62fSSabrina Dubroca TEST_F(tls, getsockopt)
1585f27ad62fSSabrina Dubroca {
1586f27ad62fSSabrina Dubroca 	struct tls_crypto_info_keys expect, get;
1587f27ad62fSSabrina Dubroca 	socklen_t len;
1588f27ad62fSSabrina Dubroca 
1589f27ad62fSSabrina Dubroca 	/* get only the version/cipher */
1590f27ad62fSSabrina Dubroca 	len = sizeof(struct tls_crypto_info);
1591f27ad62fSSabrina Dubroca 	memrnd(&get, sizeof(get));
1592f27ad62fSSabrina Dubroca 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1593f27ad62fSSabrina Dubroca 	EXPECT_EQ(len, sizeof(struct tls_crypto_info));
1594f27ad62fSSabrina Dubroca 	EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1595f27ad62fSSabrina Dubroca 	EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1596f27ad62fSSabrina Dubroca 
1597f27ad62fSSabrina Dubroca 	/* get the full crypto_info */
1598f27ad62fSSabrina Dubroca 	tls_crypto_info_init(variant->tls_version, variant->cipher_type, &expect);
1599f27ad62fSSabrina Dubroca 	len = expect.len;
1600f27ad62fSSabrina Dubroca 	memrnd(&get, sizeof(get));
1601f27ad62fSSabrina Dubroca 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1602f27ad62fSSabrina Dubroca 	EXPECT_EQ(len, expect.len);
1603f27ad62fSSabrina Dubroca 	EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1604f27ad62fSSabrina Dubroca 	EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1605f27ad62fSSabrina Dubroca 	EXPECT_EQ(memcmp(&get, &expect, expect.len), 0);
1606f27ad62fSSabrina Dubroca 
1607f27ad62fSSabrina Dubroca 	/* short get should fail */
1608f27ad62fSSabrina Dubroca 	len = sizeof(struct tls_crypto_info) - 1;
1609f27ad62fSSabrina Dubroca 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1610f27ad62fSSabrina Dubroca 	EXPECT_EQ(errno, EINVAL);
1611f27ad62fSSabrina Dubroca 
1612f27ad62fSSabrina Dubroca 	/* partial get of the cipher data should fail */
1613f27ad62fSSabrina Dubroca 	len = expect.len - 1;
1614f27ad62fSSabrina Dubroca 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1615f27ad62fSSabrina Dubroca 	EXPECT_EQ(errno, EINVAL);
1616f27ad62fSSabrina Dubroca }
1617f27ad62fSSabrina Dubroca 
1618ef0fc0b3SJakub Kicinski FIXTURE(tls_err)
1619ef0fc0b3SJakub Kicinski {
1620ef0fc0b3SJakub Kicinski 	int fd, cfd;
1621ef0fc0b3SJakub Kicinski 	int fd2, cfd2;
1622ef0fc0b3SJakub Kicinski 	bool notls;
1623ef0fc0b3SJakub Kicinski };
1624ef0fc0b3SJakub Kicinski 
1625ef0fc0b3SJakub Kicinski FIXTURE_VARIANT(tls_err)
1626ef0fc0b3SJakub Kicinski {
1627ef0fc0b3SJakub Kicinski 	uint16_t tls_version;
1628ef0fc0b3SJakub Kicinski };
1629ef0fc0b3SJakub Kicinski 
1630ef0fc0b3SJakub Kicinski FIXTURE_VARIANT_ADD(tls_err, 12_aes_gcm)
1631ef0fc0b3SJakub Kicinski {
1632ef0fc0b3SJakub Kicinski 	.tls_version = TLS_1_2_VERSION,
1633ef0fc0b3SJakub Kicinski };
1634ef0fc0b3SJakub Kicinski 
1635ef0fc0b3SJakub Kicinski FIXTURE_VARIANT_ADD(tls_err, 13_aes_gcm)
1636ef0fc0b3SJakub Kicinski {
1637ef0fc0b3SJakub Kicinski 	.tls_version = TLS_1_3_VERSION,
1638ef0fc0b3SJakub Kicinski };
1639ef0fc0b3SJakub Kicinski 
1640ef0fc0b3SJakub Kicinski FIXTURE_SETUP(tls_err)
1641ef0fc0b3SJakub Kicinski {
1642ef0fc0b3SJakub Kicinski 	struct tls_crypto_info_keys tls12;
1643ef0fc0b3SJakub Kicinski 	int ret;
1644ef0fc0b3SJakub Kicinski 
1645ef0fc0b3SJakub Kicinski 	tls_crypto_info_init(variant->tls_version, TLS_CIPHER_AES_GCM_128,
1646ef0fc0b3SJakub Kicinski 			     &tls12);
1647ef0fc0b3SJakub Kicinski 
1648ef0fc0b3SJakub Kicinski 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
1649ef0fc0b3SJakub Kicinski 	ulp_sock_pair(_metadata, &self->fd2, &self->cfd2, &self->notls);
1650ef0fc0b3SJakub Kicinski 	if (self->notls)
1651ef0fc0b3SJakub Kicinski 		return;
1652ef0fc0b3SJakub Kicinski 
1653ef0fc0b3SJakub Kicinski 	ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
1654ef0fc0b3SJakub Kicinski 	ASSERT_EQ(ret, 0);
1655ef0fc0b3SJakub Kicinski 
1656ef0fc0b3SJakub Kicinski 	ret = setsockopt(self->cfd2, SOL_TLS, TLS_RX, &tls12, tls12.len);
1657ef0fc0b3SJakub Kicinski 	ASSERT_EQ(ret, 0);
1658ef0fc0b3SJakub Kicinski }
1659ef0fc0b3SJakub Kicinski 
1660ef0fc0b3SJakub Kicinski FIXTURE_TEARDOWN(tls_err)
1661ef0fc0b3SJakub Kicinski {
1662ef0fc0b3SJakub Kicinski 	close(self->fd);
1663ef0fc0b3SJakub Kicinski 	close(self->cfd);
1664ef0fc0b3SJakub Kicinski 	close(self->fd2);
1665ef0fc0b3SJakub Kicinski 	close(self->cfd2);
1666ef0fc0b3SJakub Kicinski }
1667ef0fc0b3SJakub Kicinski 
1668ef0fc0b3SJakub Kicinski TEST_F(tls_err, bad_rec)
1669ef0fc0b3SJakub Kicinski {
1670ef0fc0b3SJakub Kicinski 	char buf[64];
1671ef0fc0b3SJakub Kicinski 
1672ef0fc0b3SJakub Kicinski 	if (self->notls)
1673ef0fc0b3SJakub Kicinski 		SKIP(return, "no TLS support");
1674ef0fc0b3SJakub Kicinski 
1675ef0fc0b3SJakub Kicinski 	memset(buf, 0x55, sizeof(buf));
1676ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd2, buf, sizeof(buf), 0), sizeof(buf));
1677ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1678ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EMSGSIZE);
1679ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), MSG_DONTWAIT), -1);
1680ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EAGAIN);
1681ef0fc0b3SJakub Kicinski }
1682ef0fc0b3SJakub Kicinski 
1683ef0fc0b3SJakub Kicinski TEST_F(tls_err, bad_auth)
1684ef0fc0b3SJakub Kicinski {
1685ef0fc0b3SJakub Kicinski 	char buf[128];
1686ef0fc0b3SJakub Kicinski 	int n;
1687ef0fc0b3SJakub Kicinski 
1688ef0fc0b3SJakub Kicinski 	if (self->notls)
1689ef0fc0b3SJakub Kicinski 		SKIP(return, "no TLS support");
1690ef0fc0b3SJakub Kicinski 
1691ef0fc0b3SJakub Kicinski 	memrnd(buf, sizeof(buf) / 2);
1692ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd, buf, sizeof(buf) / 2, 0), sizeof(buf) / 2);
1693ef0fc0b3SJakub Kicinski 	n = recv(self->cfd, buf, sizeof(buf), 0);
1694ef0fc0b3SJakub Kicinski 	EXPECT_GT(n, sizeof(buf) / 2);
1695ef0fc0b3SJakub Kicinski 
1696ef0fc0b3SJakub Kicinski 	buf[n - 1]++;
1697ef0fc0b3SJakub Kicinski 
1698ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd2, buf, n, 0), n);
1699ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1700ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1701ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1702ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1703ef0fc0b3SJakub Kicinski }
1704ef0fc0b3SJakub Kicinski 
1705ef0fc0b3SJakub Kicinski TEST_F(tls_err, bad_in_large_read)
1706ef0fc0b3SJakub Kicinski {
1707ef0fc0b3SJakub Kicinski 	char txt[3][64];
1708ef0fc0b3SJakub Kicinski 	char cip[3][128];
1709ef0fc0b3SJakub Kicinski 	char buf[3 * 128];
1710ef0fc0b3SJakub Kicinski 	int i, n;
1711ef0fc0b3SJakub Kicinski 
1712ef0fc0b3SJakub Kicinski 	if (self->notls)
1713ef0fc0b3SJakub Kicinski 		SKIP(return, "no TLS support");
1714ef0fc0b3SJakub Kicinski 
1715ef0fc0b3SJakub Kicinski 	/* Put 3 records in the sockets */
1716ef0fc0b3SJakub Kicinski 	for (i = 0; i < 3; i++) {
1717ef0fc0b3SJakub Kicinski 		memrnd(txt[i], sizeof(txt[i]));
1718ef0fc0b3SJakub Kicinski 		EXPECT_EQ(send(self->fd, txt[i], sizeof(txt[i]), 0),
1719ef0fc0b3SJakub Kicinski 			  sizeof(txt[i]));
1720ef0fc0b3SJakub Kicinski 		n = recv(self->cfd, cip[i], sizeof(cip[i]), 0);
1721ef0fc0b3SJakub Kicinski 		EXPECT_GT(n, sizeof(txt[i]));
1722ef0fc0b3SJakub Kicinski 		/* Break the third message */
1723ef0fc0b3SJakub Kicinski 		if (i == 2)
1724ef0fc0b3SJakub Kicinski 			cip[2][n - 1]++;
1725ef0fc0b3SJakub Kicinski 		EXPECT_EQ(send(self->fd2, cip[i], n, 0), n);
1726ef0fc0b3SJakub Kicinski 	}
1727ef0fc0b3SJakub Kicinski 
1728ef0fc0b3SJakub Kicinski 	/* We should be able to receive the first two messages */
1729ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt[0]) * 2);
1730ef0fc0b3SJakub Kicinski 	EXPECT_EQ(memcmp(buf, txt[0], sizeof(txt[0])), 0);
1731ef0fc0b3SJakub Kicinski 	EXPECT_EQ(memcmp(buf + sizeof(txt[0]), txt[1], sizeof(txt[1])), 0);
1732ef0fc0b3SJakub Kicinski 	/* Third mesasge is bad */
1733ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1734ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1735ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1736ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1737ef0fc0b3SJakub Kicinski }
1738ef0fc0b3SJakub Kicinski 
1739ef0fc0b3SJakub Kicinski TEST_F(tls_err, bad_cmsg)
1740ef0fc0b3SJakub Kicinski {
1741ef0fc0b3SJakub Kicinski 	char *test_str = "test_read";
1742ef0fc0b3SJakub Kicinski 	int send_len = 10;
1743ef0fc0b3SJakub Kicinski 	char cip[128];
1744ef0fc0b3SJakub Kicinski 	char buf[128];
1745ef0fc0b3SJakub Kicinski 	char txt[64];
1746ef0fc0b3SJakub Kicinski 	int n;
1747ef0fc0b3SJakub Kicinski 
1748ef0fc0b3SJakub Kicinski 	if (self->notls)
1749ef0fc0b3SJakub Kicinski 		SKIP(return, "no TLS support");
1750ef0fc0b3SJakub Kicinski 
1751ef0fc0b3SJakub Kicinski 	/* Queue up one data record */
1752ef0fc0b3SJakub Kicinski 	memrnd(txt, sizeof(txt));
1753ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd, txt, sizeof(txt), 0), sizeof(txt));
1754ef0fc0b3SJakub Kicinski 	n = recv(self->cfd, cip, sizeof(cip), 0);
1755ef0fc0b3SJakub Kicinski 	EXPECT_GT(n, sizeof(txt));
1756ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1757ef0fc0b3SJakub Kicinski 
1758ef0fc0b3SJakub Kicinski 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
1759ef0fc0b3SJakub Kicinski 	n = recv(self->cfd, cip, sizeof(cip), 0);
1760ef0fc0b3SJakub Kicinski 	cip[n - 1]++; /* Break it */
1761ef0fc0b3SJakub Kicinski 	EXPECT_GT(n, send_len);
1762ef0fc0b3SJakub Kicinski 	EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1763ef0fc0b3SJakub Kicinski 
1764ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt));
1765ef0fc0b3SJakub Kicinski 	EXPECT_EQ(memcmp(buf, txt, sizeof(txt)), 0);
1766ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1767ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1768ef0fc0b3SJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1769ef0fc0b3SJakub Kicinski 	EXPECT_EQ(errno, EBADMSG);
1770ef0fc0b3SJakub Kicinski }
1771ef0fc0b3SJakub Kicinski 
1772842463f2SJakub Kicinski TEST_F(tls_err, timeo)
1773842463f2SJakub Kicinski {
1774842463f2SJakub Kicinski 	struct timeval tv = { .tv_usec = 10000, };
1775842463f2SJakub Kicinski 	char buf[128];
1776842463f2SJakub Kicinski 	int ret;
1777842463f2SJakub Kicinski 
1778842463f2SJakub Kicinski 	if (self->notls)
1779842463f2SJakub Kicinski 		SKIP(return, "no TLS support");
1780842463f2SJakub Kicinski 
1781842463f2SJakub Kicinski 	ret = setsockopt(self->cfd2, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1782842463f2SJakub Kicinski 	ASSERT_EQ(ret, 0);
1783842463f2SJakub Kicinski 
1784842463f2SJakub Kicinski 	ret = fork();
1785842463f2SJakub Kicinski 	ASSERT_GE(ret, 0);
1786842463f2SJakub Kicinski 
1787842463f2SJakub Kicinski 	if (ret) {
1788842463f2SJakub Kicinski 		usleep(1000); /* Give child a head start */
1789842463f2SJakub Kicinski 
1790842463f2SJakub Kicinski 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1791842463f2SJakub Kicinski 		EXPECT_EQ(errno, EAGAIN);
1792842463f2SJakub Kicinski 
1793842463f2SJakub Kicinski 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1794842463f2SJakub Kicinski 		EXPECT_EQ(errno, EAGAIN);
1795842463f2SJakub Kicinski 
1796842463f2SJakub Kicinski 		wait(&ret);
1797842463f2SJakub Kicinski 	} else {
1798842463f2SJakub Kicinski 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1799842463f2SJakub Kicinski 		EXPECT_EQ(errno, EAGAIN);
1800842463f2SJakub Kicinski 		exit(0);
1801842463f2SJakub Kicinski 	}
1802842463f2SJakub Kicinski }
1803842463f2SJakub Kicinski 
180423fcb62bSJakub Kicinski TEST_F(tls_err, poll_partial_rec)
180523fcb62bSJakub Kicinski {
180623fcb62bSJakub Kicinski 	struct pollfd pfd = { };
180723fcb62bSJakub Kicinski 	ssize_t rec_len;
180823fcb62bSJakub Kicinski 	char rec[256];
180923fcb62bSJakub Kicinski 	char buf[128];
181023fcb62bSJakub Kicinski 
181123fcb62bSJakub Kicinski 	if (self->notls)
181223fcb62bSJakub Kicinski 		SKIP(return, "no TLS support");
181323fcb62bSJakub Kicinski 
181423fcb62bSJakub Kicinski 	pfd.fd = self->cfd2;
181523fcb62bSJakub Kicinski 	pfd.events = POLLIN;
181623fcb62bSJakub Kicinski 	EXPECT_EQ(poll(&pfd, 1, 1), 0);
181723fcb62bSJakub Kicinski 
181823fcb62bSJakub Kicinski 	memrnd(buf, sizeof(buf));
181923fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
182023fcb62bSJakub Kicinski 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
182123fcb62bSJakub Kicinski 	EXPECT_GT(rec_len, sizeof(buf));
182223fcb62bSJakub Kicinski 
182323fcb62bSJakub Kicinski 	/* Write 100B, not the full record ... */
182423fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
182523fcb62bSJakub Kicinski 	/* ... no full record should mean no POLLIN */
182623fcb62bSJakub Kicinski 	pfd.fd = self->cfd2;
182723fcb62bSJakub Kicinski 	pfd.events = POLLIN;
182823fcb62bSJakub Kicinski 	EXPECT_EQ(poll(&pfd, 1, 1), 0);
182923fcb62bSJakub Kicinski 	/* Now write the rest, and it should all pop out of the other end. */
183023fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
183123fcb62bSJakub Kicinski 	pfd.fd = self->cfd2;
183223fcb62bSJakub Kicinski 	pfd.events = POLLIN;
183323fcb62bSJakub Kicinski 	EXPECT_EQ(poll(&pfd, 1, 1), 1);
183423fcb62bSJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
183523fcb62bSJakub Kicinski 	EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
183623fcb62bSJakub Kicinski }
183723fcb62bSJakub Kicinski 
183823fcb62bSJakub Kicinski TEST_F(tls_err, epoll_partial_rec)
183923fcb62bSJakub Kicinski {
184023fcb62bSJakub Kicinski 	struct epoll_event ev, events[10];
184123fcb62bSJakub Kicinski 	ssize_t rec_len;
184223fcb62bSJakub Kicinski 	char rec[256];
184323fcb62bSJakub Kicinski 	char buf[128];
184423fcb62bSJakub Kicinski 	int epollfd;
184523fcb62bSJakub Kicinski 
184623fcb62bSJakub Kicinski 	if (self->notls)
184723fcb62bSJakub Kicinski 		SKIP(return, "no TLS support");
184823fcb62bSJakub Kicinski 
184923fcb62bSJakub Kicinski 	epollfd = epoll_create1(0);
185023fcb62bSJakub Kicinski 	ASSERT_GE(epollfd, 0);
185123fcb62bSJakub Kicinski 
185223fcb62bSJakub Kicinski 	memset(&ev, 0, sizeof(ev));
185323fcb62bSJakub Kicinski 	ev.events = EPOLLIN;
185423fcb62bSJakub Kicinski 	ev.data.fd = self->cfd2;
185523fcb62bSJakub Kicinski 	ASSERT_GE(epoll_ctl(epollfd, EPOLL_CTL_ADD, self->cfd2, &ev), 0);
185623fcb62bSJakub Kicinski 
185723fcb62bSJakub Kicinski 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
185823fcb62bSJakub Kicinski 
185923fcb62bSJakub Kicinski 	memrnd(buf, sizeof(buf));
186023fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
186123fcb62bSJakub Kicinski 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
186223fcb62bSJakub Kicinski 	EXPECT_GT(rec_len, sizeof(buf));
186323fcb62bSJakub Kicinski 
186423fcb62bSJakub Kicinski 	/* Write 100B, not the full record ... */
186523fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
186623fcb62bSJakub Kicinski 	/* ... no full record should mean no POLLIN */
186723fcb62bSJakub Kicinski 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
186823fcb62bSJakub Kicinski 	/* Now write the rest, and it should all pop out of the other end. */
186923fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
187023fcb62bSJakub Kicinski 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 1);
187123fcb62bSJakub Kicinski 	EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
187223fcb62bSJakub Kicinski 	EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
187323fcb62bSJakub Kicinski 
187423fcb62bSJakub Kicinski 	close(epollfd);
187523fcb62bSJakub Kicinski }
187623fcb62bSJakub Kicinski 
187723fcb62bSJakub Kicinski TEST_F(tls_err, poll_partial_rec_async)
187823fcb62bSJakub Kicinski {
187923fcb62bSJakub Kicinski 	struct pollfd pfd = { };
188023fcb62bSJakub Kicinski 	ssize_t rec_len;
188123fcb62bSJakub Kicinski 	char rec[256];
188223fcb62bSJakub Kicinski 	char buf[128];
188323fcb62bSJakub Kicinski 	char token;
188423fcb62bSJakub Kicinski 	int p[2];
188523fcb62bSJakub Kicinski 	int ret;
188623fcb62bSJakub Kicinski 
188723fcb62bSJakub Kicinski 	if (self->notls)
188823fcb62bSJakub Kicinski 		SKIP(return, "no TLS support");
188923fcb62bSJakub Kicinski 
189023fcb62bSJakub Kicinski 	ASSERT_GE(pipe(p), 0);
189123fcb62bSJakub Kicinski 
189223fcb62bSJakub Kicinski 	memrnd(buf, sizeof(buf));
189323fcb62bSJakub Kicinski 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
189423fcb62bSJakub Kicinski 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
189523fcb62bSJakub Kicinski 	EXPECT_GT(rec_len, sizeof(buf));
189623fcb62bSJakub Kicinski 
189723fcb62bSJakub Kicinski 	ret = fork();
189823fcb62bSJakub Kicinski 	ASSERT_GE(ret, 0);
189923fcb62bSJakub Kicinski 
190023fcb62bSJakub Kicinski 	if (ret) {
190123fcb62bSJakub Kicinski 		int status, pid2;
190223fcb62bSJakub Kicinski 
190323fcb62bSJakub Kicinski 		close(p[1]);
190423fcb62bSJakub Kicinski 		usleep(1000); /* Give child a head start */
190523fcb62bSJakub Kicinski 
190623fcb62bSJakub Kicinski 		EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
190723fcb62bSJakub Kicinski 
190823fcb62bSJakub Kicinski 		EXPECT_EQ(read(p[0], &token, 1), 1); /* Barrier #1 */
190923fcb62bSJakub Kicinski 
191023fcb62bSJakub Kicinski 		EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0),
191123fcb62bSJakub Kicinski 			  rec_len - 100);
191223fcb62bSJakub Kicinski 
191323fcb62bSJakub Kicinski 		pid2 = wait(&status);
191423fcb62bSJakub Kicinski 		EXPECT_EQ(pid2, ret);
191523fcb62bSJakub Kicinski 		EXPECT_EQ(status, 0);
191623fcb62bSJakub Kicinski 	} else {
191723fcb62bSJakub Kicinski 		close(p[0]);
191823fcb62bSJakub Kicinski 
191923fcb62bSJakub Kicinski 		/* Child should sleep in poll(), never get a wake */
192023fcb62bSJakub Kicinski 		pfd.fd = self->cfd2;
192123fcb62bSJakub Kicinski 		pfd.events = POLLIN;
19222ec197fdSJakub Kicinski 		EXPECT_EQ(poll(&pfd, 1, 20), 0);
192323fcb62bSJakub Kicinski 
192423fcb62bSJakub Kicinski 		EXPECT_EQ(write(p[1], &token, 1), 1); /* Barrier #1 */
192523fcb62bSJakub Kicinski 
192623fcb62bSJakub Kicinski 		pfd.fd = self->cfd2;
192723fcb62bSJakub Kicinski 		pfd.events = POLLIN;
19282ec197fdSJakub Kicinski 		EXPECT_EQ(poll(&pfd, 1, 20), 1);
192923fcb62bSJakub Kicinski 
1930*69fe8ec4SJakub Kicinski 		exit(!__test_passed(_metadata));
193123fcb62bSJakub Kicinski 	}
193223fcb62bSJakub Kicinski }
193323fcb62bSJakub Kicinski 
193478b5dc3dSJakub Kicinski TEST(non_established) {
193578b5dc3dSJakub Kicinski 	struct tls12_crypto_info_aes_gcm_256 tls12;
193678b5dc3dSJakub Kicinski 	struct sockaddr_in addr;
193778b5dc3dSJakub Kicinski 	int sfd, ret, fd;
193878b5dc3dSJakub Kicinski 	socklen_t len;
193978b5dc3dSJakub Kicinski 
194078b5dc3dSJakub Kicinski 	len = sizeof(addr);
194178b5dc3dSJakub Kicinski 
194278b5dc3dSJakub Kicinski 	memset(&tls12, 0, sizeof(tls12));
194378b5dc3dSJakub Kicinski 	tls12.info.version = TLS_1_2_VERSION;
194478b5dc3dSJakub Kicinski 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
194578b5dc3dSJakub Kicinski 
194678b5dc3dSJakub Kicinski 	addr.sin_family = AF_INET;
194778b5dc3dSJakub Kicinski 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
194878b5dc3dSJakub Kicinski 	addr.sin_port = 0;
194978b5dc3dSJakub Kicinski 
195078b5dc3dSJakub Kicinski 	fd = socket(AF_INET, SOCK_STREAM, 0);
195178b5dc3dSJakub Kicinski 	sfd = socket(AF_INET, SOCK_STREAM, 0);
195278b5dc3dSJakub Kicinski 
195378b5dc3dSJakub Kicinski 	ret = bind(sfd, &addr, sizeof(addr));
195478b5dc3dSJakub Kicinski 	ASSERT_EQ(ret, 0);
195578b5dc3dSJakub Kicinski 	ret = listen(sfd, 10);
195678b5dc3dSJakub Kicinski 	ASSERT_EQ(ret, 0);
195778b5dc3dSJakub Kicinski 
195878b5dc3dSJakub Kicinski 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
195978b5dc3dSJakub Kicinski 	EXPECT_EQ(ret, -1);
196078b5dc3dSJakub Kicinski 	/* TLS ULP not supported */
196178b5dc3dSJakub Kicinski 	if (errno == ENOENT)
196278b5dc3dSJakub Kicinski 		return;
19634a5cdc60SValentin Vidic 	EXPECT_EQ(errno, ENOTCONN);
196478b5dc3dSJakub Kicinski 
196578b5dc3dSJakub Kicinski 	ret = setsockopt(sfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
196678b5dc3dSJakub Kicinski 	EXPECT_EQ(ret, -1);
19674a5cdc60SValentin Vidic 	EXPECT_EQ(errno, ENOTCONN);
196878b5dc3dSJakub Kicinski 
196978b5dc3dSJakub Kicinski 	ret = getsockname(sfd, &addr, &len);
197078b5dc3dSJakub Kicinski 	ASSERT_EQ(ret, 0);
197178b5dc3dSJakub Kicinski 
197278b5dc3dSJakub Kicinski 	ret = connect(fd, &addr, sizeof(addr));
197378b5dc3dSJakub Kicinski 	ASSERT_EQ(ret, 0);
197478b5dc3dSJakub Kicinski 
197578b5dc3dSJakub Kicinski 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
197678b5dc3dSJakub Kicinski 	ASSERT_EQ(ret, 0);
197778b5dc3dSJakub Kicinski 
197878b5dc3dSJakub Kicinski 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
197978b5dc3dSJakub Kicinski 	EXPECT_EQ(ret, -1);
198078b5dc3dSJakub Kicinski 	EXPECT_EQ(errno, EEXIST);
198178b5dc3dSJakub Kicinski 
198278b5dc3dSJakub Kicinski 	close(fd);
198378b5dc3dSJakub Kicinski 	close(sfd);
198478b5dc3dSJakub Kicinski }
198578b5dc3dSJakub Kicinski 
1986fb99bce7SDave Watson TEST(keysizes) {
1987fb99bce7SDave Watson 	struct tls12_crypto_info_aes_gcm_256 tls12;
1988a125f91fSJakub Kicinski 	int ret, fd, cfd;
1989fb99bce7SDave Watson 	bool notls;
1990fb99bce7SDave Watson 
1991fb99bce7SDave Watson 	memset(&tls12, 0, sizeof(tls12));
1992fb99bce7SDave Watson 	tls12.info.version = TLS_1_2_VERSION;
1993fb99bce7SDave Watson 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1994fb99bce7SDave Watson 
1995a125f91fSJakub Kicinski 	ulp_sock_pair(_metadata, &fd, &cfd, &notls);
1996fb99bce7SDave Watson 
1997fb99bce7SDave Watson 	if (!notls) {
1998fb99bce7SDave Watson 		ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12,
1999fb99bce7SDave Watson 				 sizeof(tls12));
2000fb99bce7SDave Watson 		EXPECT_EQ(ret, 0);
2001fb99bce7SDave Watson 
2002fb99bce7SDave Watson 		ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12,
2003fb99bce7SDave Watson 				 sizeof(tls12));
2004fb99bce7SDave Watson 		EXPECT_EQ(ret, 0);
2005fb99bce7SDave Watson 	}
2006fb99bce7SDave Watson 
2007fb99bce7SDave Watson 	close(fd);
2008fb99bce7SDave Watson 	close(cfd);
2009fb99bce7SDave Watson }
2010fb99bce7SDave Watson 
20111d55f203SJakub Kicinski TEST(no_pad) {
20121d55f203SJakub Kicinski 	struct tls12_crypto_info_aes_gcm_256 tls12;
20131d55f203SJakub Kicinski 	int ret, fd, cfd, val;
20141d55f203SJakub Kicinski 	socklen_t len;
20151d55f203SJakub Kicinski 	bool notls;
20161d55f203SJakub Kicinski 
20171d55f203SJakub Kicinski 	memset(&tls12, 0, sizeof(tls12));
20181d55f203SJakub Kicinski 	tls12.info.version = TLS_1_3_VERSION;
20191d55f203SJakub Kicinski 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
20201d55f203SJakub Kicinski 
20211d55f203SJakub Kicinski 	ulp_sock_pair(_metadata, &fd, &cfd, &notls);
20221d55f203SJakub Kicinski 
20231d55f203SJakub Kicinski 	if (notls)
20241d55f203SJakub Kicinski 		exit(KSFT_SKIP);
20251d55f203SJakub Kicinski 
20261d55f203SJakub Kicinski 	ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, sizeof(tls12));
20271d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20281d55f203SJakub Kicinski 
20291d55f203SJakub Kicinski 	ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, sizeof(tls12));
20301d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20311d55f203SJakub Kicinski 
20321d55f203SJakub Kicinski 	val = 1;
20331d55f203SJakub Kicinski 	ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
20341d55f203SJakub Kicinski 			 (void *)&val, sizeof(val));
20351d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20361d55f203SJakub Kicinski 
20371d55f203SJakub Kicinski 	len = sizeof(val);
20381d55f203SJakub Kicinski 	val = 2;
20391d55f203SJakub Kicinski 	ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
20401d55f203SJakub Kicinski 			 (void *)&val, &len);
20411d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20421d55f203SJakub Kicinski 	EXPECT_EQ(val, 1);
20431d55f203SJakub Kicinski 	EXPECT_EQ(len, 4);
20441d55f203SJakub Kicinski 
20451d55f203SJakub Kicinski 	val = 0;
20461d55f203SJakub Kicinski 	ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
20471d55f203SJakub Kicinski 			 (void *)&val, sizeof(val));
20481d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20491d55f203SJakub Kicinski 
20501d55f203SJakub Kicinski 	len = sizeof(val);
20511d55f203SJakub Kicinski 	val = 2;
20521d55f203SJakub Kicinski 	ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
20531d55f203SJakub Kicinski 			 (void *)&val, &len);
20541d55f203SJakub Kicinski 	EXPECT_EQ(ret, 0);
20551d55f203SJakub Kicinski 	EXPECT_EQ(val, 0);
20561d55f203SJakub Kicinski 	EXPECT_EQ(len, 4);
20571d55f203SJakub Kicinski 
20581d55f203SJakub Kicinski 	close(fd);
20591d55f203SJakub Kicinski 	close(cfd);
20601d55f203SJakub Kicinski }
20611d55f203SJakub Kicinski 
2062f884a342SJakub Kicinski TEST(tls_v6ops) {
2063f884a342SJakub Kicinski 	struct tls_crypto_info_keys tls12;
2064f884a342SJakub Kicinski 	struct sockaddr_in6 addr, addr2;
2065f884a342SJakub Kicinski 	int sfd, ret, fd;
2066f884a342SJakub Kicinski 	socklen_t len, len2;
2067f884a342SJakub Kicinski 
2068f884a342SJakub Kicinski 	tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_128, &tls12);
2069f884a342SJakub Kicinski 
2070f884a342SJakub Kicinski 	addr.sin6_family = AF_INET6;
2071f884a342SJakub Kicinski 	addr.sin6_addr = in6addr_any;
2072f884a342SJakub Kicinski 	addr.sin6_port = 0;
2073f884a342SJakub Kicinski 
2074f884a342SJakub Kicinski 	fd = socket(AF_INET6, SOCK_STREAM, 0);
2075f884a342SJakub Kicinski 	sfd = socket(AF_INET6, SOCK_STREAM, 0);
2076f884a342SJakub Kicinski 
2077f884a342SJakub Kicinski 	ret = bind(sfd, &addr, sizeof(addr));
2078f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2079f884a342SJakub Kicinski 	ret = listen(sfd, 10);
2080f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2081f884a342SJakub Kicinski 
2082f884a342SJakub Kicinski 	len = sizeof(addr);
2083f884a342SJakub Kicinski 	ret = getsockname(sfd, &addr, &len);
2084f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2085f884a342SJakub Kicinski 
2086f884a342SJakub Kicinski 	ret = connect(fd, &addr, sizeof(addr));
2087f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2088f884a342SJakub Kicinski 
2089f884a342SJakub Kicinski 	len = sizeof(addr);
2090f884a342SJakub Kicinski 	ret = getsockname(fd, &addr, &len);
2091f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2092f884a342SJakub Kicinski 
2093f884a342SJakub Kicinski 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2094f884a342SJakub Kicinski 	if (ret) {
2095f884a342SJakub Kicinski 		ASSERT_EQ(errno, ENOENT);
2096f884a342SJakub Kicinski 		SKIP(return, "no TLS support");
2097f884a342SJakub Kicinski 	}
2098f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2099f884a342SJakub Kicinski 
2100f884a342SJakub Kicinski 	ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
2101f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2102f884a342SJakub Kicinski 
2103f884a342SJakub Kicinski 	ret = setsockopt(fd, SOL_TLS, TLS_RX, &tls12, tls12.len);
2104f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2105f884a342SJakub Kicinski 
2106f884a342SJakub Kicinski 	len2 = sizeof(addr2);
2107f884a342SJakub Kicinski 	ret = getsockname(fd, &addr2, &len2);
2108f884a342SJakub Kicinski 	ASSERT_EQ(ret, 0);
2109f884a342SJakub Kicinski 
2110f884a342SJakub Kicinski 	EXPECT_EQ(len2, len);
2111f884a342SJakub Kicinski 	EXPECT_EQ(memcmp(&addr, &addr2, len), 0);
2112f884a342SJakub Kicinski 
2113f884a342SJakub Kicinski 	close(fd);
2114f884a342SJakub Kicinski 	close(sfd);
2115f884a342SJakub Kicinski }
2116f884a342SJakub Kicinski 
2117a504d246SJakub Kicinski TEST(prequeue) {
2118a504d246SJakub Kicinski 	struct tls_crypto_info_keys tls12;
2119a504d246SJakub Kicinski 	char buf[20000], buf2[20000];
2120a504d246SJakub Kicinski 	struct sockaddr_in addr;
2121a504d246SJakub Kicinski 	int sfd, cfd, ret, fd;
2122a504d246SJakub Kicinski 	socklen_t len;
2123a504d246SJakub Kicinski 
2124a504d246SJakub Kicinski 	len = sizeof(addr);
2125a504d246SJakub Kicinski 	memrnd(buf, sizeof(buf));
2126a504d246SJakub Kicinski 
2127a504d246SJakub Kicinski 	tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_256, &tls12);
2128a504d246SJakub Kicinski 
2129a504d246SJakub Kicinski 	addr.sin_family = AF_INET;
2130a504d246SJakub Kicinski 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
2131a504d246SJakub Kicinski 	addr.sin_port = 0;
2132a504d246SJakub Kicinski 
2133a504d246SJakub Kicinski 	fd = socket(AF_INET, SOCK_STREAM, 0);
2134a504d246SJakub Kicinski 	sfd = socket(AF_INET, SOCK_STREAM, 0);
2135a504d246SJakub Kicinski 
2136a504d246SJakub Kicinski 	ASSERT_EQ(bind(sfd, &addr, sizeof(addr)), 0);
2137a504d246SJakub Kicinski 	ASSERT_EQ(listen(sfd, 10), 0);
2138a504d246SJakub Kicinski 	ASSERT_EQ(getsockname(sfd, &addr, &len), 0);
2139a504d246SJakub Kicinski 	ASSERT_EQ(connect(fd, &addr, sizeof(addr)), 0);
2140a504d246SJakub Kicinski 	ASSERT_GE(cfd = accept(sfd, &addr, &len), 0);
2141a504d246SJakub Kicinski 	close(sfd);
2142a504d246SJakub Kicinski 
2143a504d246SJakub Kicinski 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2144a504d246SJakub Kicinski 	if (ret) {
2145a504d246SJakub Kicinski 		ASSERT_EQ(errno, ENOENT);
2146a504d246SJakub Kicinski 		SKIP(return, "no TLS support");
2147a504d246SJakub Kicinski 	}
2148a504d246SJakub Kicinski 
2149a504d246SJakub Kicinski 	ASSERT_EQ(setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
2150a504d246SJakub Kicinski 	EXPECT_EQ(send(fd, buf, sizeof(buf), MSG_DONTWAIT), sizeof(buf));
2151a504d246SJakub Kicinski 
2152a504d246SJakub Kicinski 	ASSERT_EQ(setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")), 0);
2153a504d246SJakub Kicinski 	ASSERT_EQ(setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, tls12.len), 0);
2154a504d246SJakub Kicinski 	EXPECT_EQ(recv(cfd, buf2, sizeof(buf2), MSG_WAITALL), sizeof(buf2));
2155a504d246SJakub Kicinski 
2156a504d246SJakub Kicinski 	EXPECT_EQ(memcmp(buf, buf2, sizeof(buf)), 0);
2157a504d246SJakub Kicinski 
2158a504d246SJakub Kicinski 	close(fd);
2159a504d246SJakub Kicinski 	close(cfd);
2160a504d246SJakub Kicinski }
2161a504d246SJakub Kicinski 
2162d113c395SMagali Lemes static void __attribute__((constructor)) fips_check(void) {
2163d113c395SMagali Lemes 	int res;
2164d113c395SMagali Lemes 	FILE *f;
2165d113c395SMagali Lemes 
2166d113c395SMagali Lemes 	f = fopen("/proc/sys/crypto/fips_enabled", "r");
2167d113c395SMagali Lemes 	if (f) {
2168d113c395SMagali Lemes 		res = fscanf(f, "%d", &fips_enabled);
2169d113c395SMagali Lemes 		if (res != 1)
2170d113c395SMagali Lemes 			ksft_print_msg("ERROR: Couldn't read /proc/sys/crypto/fips_enabled\n");
2171d113c395SMagali Lemes 		fclose(f);
2172d113c395SMagali Lemes 	}
2173d113c395SMagali Lemes }
2174d113c395SMagali Lemes 
21757f657d5bSDave Watson TEST_HARNESS_MAIN
2176