2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/bio.h>
12 #include <openssl/rand.h>
14 #include "internal/sockets.h"
16 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
18 static int compare_addr(const BIO_ADDR *a, const BIO_ADDR *b)
20 struct in_addr xa, xb;
22 struct in6_addr xa6, xb6;
27 if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
30 if (BIO_ADDR_family(a) == AF_INET) {
36 else if (BIO_ADDR_family(a) == AF_INET6) {
47 if (!TEST_int_eq(BIO_ADDR_rawaddress(a, pa, &tmplen), 1))
51 if (!TEST_int_eq(BIO_ADDR_rawaddress(b, pb, &tmplen), 1))
54 if (!TEST_mem_eq(pa, slen, pb, slen))
57 if (!TEST_int_eq(BIO_ADDR_rawport(a), BIO_ADDR_rawport(b)))
63 static int do_sendmmsg(BIO *b, BIO_MSG *msg,
64 size_t num_msg, uint64_t flags,
65 size_t *num_processed)
69 for (done = 0; done < num_msg; ) {
70 if (!BIO_sendmmsg(b, msg + done, sizeof(BIO_MSG),
71 num_msg - done, flags, num_processed))
74 done += *num_processed;
77 *num_processed = done;
81 static int do_recvmmsg(BIO *b, BIO_MSG *msg,
82 size_t num_msg, uint64_t flags,
83 size_t *num_processed)
87 for (done = 0; done < num_msg; ) {
88 if (!BIO_recvmmsg(b, msg + done, sizeof(BIO_MSG),
89 num_msg - done, flags, num_processed))
92 done += *num_processed;
95 *num_processed = done;
99 static int test_bio_dgram_impl(int af, int use_local)
102 BIO *b1 = NULL, *b2 = NULL;
103 int fd1 = -1, fd2 = -1;
104 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL,
105 *addr5 = NULL, *addr6 = NULL;
108 struct in6_addr ina6;
112 union BIO_sock_info_u info1 = {0}, info2 = {0};
113 char rx_buf[128], rx_buf2[128];
114 BIO_MSG tx_msg[128], rx_msg[128];
116 size_t num_processed = 0;
119 TEST_info("# Testing with AF_INET, local=%d\n", use_local);
124 else if (af == AF_INET6) {
125 TEST_info("# Testing with AF_INET6, local=%d\n", use_local);
134 memset(pina, 0, inal);
135 ina.s_addr = htonl(0x7f000001UL);
137 ina6.s6_addr[15] = 1;
140 addr1 = BIO_ADDR_new();
141 if (!TEST_ptr(addr1))
144 addr2 = BIO_ADDR_new();
145 if (!TEST_ptr(addr2))
148 addr3 = BIO_ADDR_new();
149 if (!TEST_ptr(addr3))
152 addr4 = BIO_ADDR_new();
153 if (!TEST_ptr(addr4))
156 addr5 = BIO_ADDR_new();
157 if (!TEST_ptr(addr5))
160 addr6 = BIO_ADDR_new();
161 if (!TEST_ptr(addr6))
164 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, af, pina, inal, 0), 1))
167 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, af, pina, inal, 0), 1))
170 fd1 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
171 if (!TEST_int_ge(fd1, 0))
174 fd2 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
175 if (!TEST_int_ge(fd2, 0))
178 if (!TEST_int_gt(BIO_bind(fd1, addr1, 0), 0))
181 if (!TEST_int_gt(BIO_bind(fd2, addr2, 0), 0))
185 if (!TEST_int_gt(BIO_sock_info(fd1, BIO_SOCK_INFO_ADDRESS, &info1), 0))
189 if (!TEST_int_gt(BIO_sock_info(fd2, BIO_SOCK_INFO_ADDRESS, &info2), 0))
192 if (!TEST_int_gt(BIO_ADDR_rawport(addr1), 0))
195 if (!TEST_int_gt(BIO_ADDR_rawport(addr2), 0))
198 b1 = BIO_new_dgram(fd1, 0);
202 b2 = BIO_new_dgram(fd2, 0);
206 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr2), 0))
209 if (!TEST_int_gt(BIO_write(b1, "hello", 5), 0))
212 /* Receiving automatically sets peer as source addr */
213 if (!TEST_int_eq(BIO_read(b2, rx_buf, sizeof(rx_buf)), 5))
216 if (!TEST_mem_eq(rx_buf, 5, "hello", 5))
219 if (!TEST_int_gt(BIO_dgram_get_peer(b2, addr3), 0))
222 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
226 if (!TEST_int_gt(BIO_ADDR_rawmake(addr3, af, pina, inal, 0), 0))
229 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr3), 0))
232 if (!TEST_int_gt(BIO_dgram_set_peer(b2, addr3), 0))
235 /* Now test using sendmmsg/recvmmsg with no peer set */
236 tx_msg[0].data = "apple";
237 tx_msg[0].data_len = 5;
238 tx_msg[0].peer = NULL;
239 tx_msg[0].local = NULL;
242 tx_msg[1].data = "orange";
243 tx_msg[1].data_len = 6;
244 tx_msg[1].peer = NULL;
245 tx_msg[1].local = NULL;
248 /* First effort should fail due to missing destination address */
249 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
250 || !TEST_size_t_eq(num_processed, 0))
254 * Second effort should fail due to local being requested
257 tx_msg[0].peer = addr2;
258 tx_msg[0].local = addr1;
259 tx_msg[1].peer = addr2;
260 tx_msg[1].local = addr1;
261 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed)
262 || !TEST_size_t_eq(num_processed, 0)))
265 /* Enable local if we are using it */
266 if (BIO_dgram_get_local_addr_cap(b1) > 0 && use_local) {
267 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b1, 1), 1))
270 tx_msg[0].local = NULL;
271 tx_msg[1].local = NULL;
275 /* Third effort should succeed */
276 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
277 || !TEST_size_t_eq(num_processed, 2))
280 /* Now try receiving */
281 rx_msg[0].data = rx_buf;
282 rx_msg[0].data_len = sizeof(rx_buf);
283 rx_msg[0].peer = addr3;
284 rx_msg[0].local = addr4;
285 rx_msg[0].flags = (1UL<<31); /* undefined flag, should be erased */
286 memset(rx_buf, 0, sizeof(rx_buf));
288 rx_msg[1].data = rx_buf2;
289 rx_msg[1].data_len = sizeof(rx_buf2);
290 rx_msg[1].peer = addr5;
291 rx_msg[1].local = addr6;
292 rx_msg[1].flags = (1UL<<31); /* undefined flag, should be erased */
293 memset(rx_buf2, 0, sizeof(rx_buf2));
296 * Should fail at first due to local being requested when not
299 if (!TEST_false(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
300 || !TEST_size_t_eq(num_processed, 0))
303 /* Fields have not been modified */
304 if (!TEST_int_eq((int)rx_msg[0].data_len, sizeof(rx_buf)))
307 if (!TEST_int_eq((int)rx_msg[1].data_len, sizeof(rx_buf2)))
310 if (!TEST_ulong_eq((unsigned long)rx_msg[0].flags, 1UL<<31))
313 if (!TEST_ulong_eq((unsigned long)rx_msg[1].flags, 1UL<<31))
316 /* Enable local if we are using it */
317 if (BIO_dgram_get_local_addr_cap(b2) > 0 && use_local) {
318 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b2, 1), 1))
321 rx_msg[0].local = NULL;
322 rx_msg[1].local = NULL;
326 /* Do the receive. */
327 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
328 || !TEST_size_t_eq(num_processed, 2))
331 /* data_len should have been updated correctly */
332 if (!TEST_int_eq((int)rx_msg[0].data_len, 5))
335 if (!TEST_int_eq((int)rx_msg[1].data_len, 6))
338 /* flags should have been zeroed */
339 if (!TEST_int_eq((int)rx_msg[0].flags, 0))
342 if (!TEST_int_eq((int)rx_msg[1].flags, 0))
345 /* peer address should match expected */
346 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
349 if (!TEST_int_eq(compare_addr(addr5, addr1), 1))
353 * Do not test local address yet as some platforms do not reliably return
354 * local addresses for messages queued for RX before local address support
355 * was enabled. Instead, send some new messages and test they're received
356 * with the correct local addresses.
358 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
359 || !TEST_size_t_eq(num_processed, 2))
362 /* Receive the messages. */
363 rx_msg[0].data_len = sizeof(rx_buf);
364 rx_msg[1].data_len = sizeof(rx_buf2);
366 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
367 || !TEST_size_t_eq(num_processed, 2))
370 if (rx_msg[0].local != NULL) {
371 /* If we are using local, it should match expected */
372 if (!TEST_int_eq(compare_addr(addr4, addr2), 1))
375 if (!TEST_int_eq(compare_addr(addr6, addr2), 1))
380 * Try sending more than can be handled in one sendmmsg call (when using the
381 * sendmmsg implementation)
383 for (i = 0; i < OSSL_NELEM(tx_msg); ++i) {
385 tx_msg[i].data = tx_buf + i;
386 tx_msg[i].data_len = 1;
387 tx_msg[i].peer = addr2;
388 tx_msg[i].local = use_local ? addr1 : NULL;
391 if (!TEST_true(do_sendmmsg(b1, tx_msg, OSSL_NELEM(tx_msg), 0, &num_processed))
392 || !TEST_size_t_eq(num_processed, OSSL_NELEM(tx_msg)))
396 * Try receiving more than can be handled in one recvmmsg call (when using
397 * the recvmmsg implementation)
399 for (i = 0; i < OSSL_NELEM(rx_msg); ++i) {
401 rx_msg[i].data = rx_buf + i;
402 rx_msg[i].data_len = 1;
403 rx_msg[i].peer = NULL;
404 rx_msg[i].local = NULL;
407 if (!TEST_true(do_recvmmsg(b2, rx_msg, OSSL_NELEM(rx_msg), 0, &num_processed))
408 || !TEST_size_t_eq(num_processed, OSSL_NELEM(rx_msg)))
411 if (!TEST_mem_eq(tx_buf, OSSL_NELEM(tx_msg), rx_buf, OSSL_NELEM(tx_msg)))
419 BIO_closesocket(fd1);
421 BIO_closesocket(fd2);
422 BIO_ADDR_free(addr1);
423 BIO_ADDR_free(addr2);
424 BIO_ADDR_free(addr3);
425 BIO_ADDR_free(addr4);
426 BIO_ADDR_free(addr5);
427 BIO_ADDR_free(addr6);
431 struct bio_dgram_case {
435 static const struct bio_dgram_case bio_dgram_cases[] = {
436 /* Test without local */
441 /* Test with local */
448 static int test_bio_dgram(int idx)
450 return test_bio_dgram_impl(bio_dgram_cases[idx].af,
451 bio_dgram_cases[idx].local);
454 static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
457 EVP_CIPHER_CTX *ctx = NULL;
458 EVP_CIPHER *cipher = NULL;
459 static const uint8_t zeroes[2048];
460 uint32_t counter[4] = {0};
462 counter[0] = (uint32_t)offset;
464 ctx = EVP_CIPHER_CTX_new();
468 cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
472 if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
475 while (data_len > 0) {
476 outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
477 if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
486 EVP_CIPHER_CTX_free(ctx);
487 EVP_CIPHER_free(cipher);
491 static int test_bio_dgram_pair(void)
493 int testresult = 0, blen, mtu1, mtu2, r;
494 BIO *bio1 = NULL, *bio2 = NULL;
495 uint8_t scratch[2048 + 4], scratch2[2048];
497 size_t i, num_dgram, num_processed = 0;
498 BIO_MSG msgs[2] = {0}, rmsgs[2] = {0};
499 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
500 struct in_addr in_local;
502 const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
503 | BIO_DGRAM_CAP_HANDLES_DST_ADDR
504 | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
505 | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
507 in_local.s_addr = ntohl(0x7f000001);
509 for (i = 0; i < OSSL_NELEM(key); ++i)
510 key[i] = test_random();
512 if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
515 mtu1 = BIO_dgram_get_mtu(bio1);
516 if (!TEST_int_ge(mtu1, 1280))
519 mtu2 = BIO_dgram_get_mtu(bio2);
520 if (!TEST_int_ge(mtu2, 1280))
523 if (!TEST_int_eq(mtu1, mtu2))
526 if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
530 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
533 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
534 r = BIO_write(bio1, scratch + 4, blen);
538 if (!TEST_int_eq(r, blen))
542 if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
547 * Should be able to fit at least 9 datagrams in default write buffer size
550 if (!TEST_int_ge(i, 9))
553 /* Check we read back the same data */
555 for (i = 0; i < num_dgram; ++i) {
556 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
559 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
560 r = BIO_read(bio2, scratch2, sizeof(scratch2));
561 if (!TEST_int_eq(r, blen))
564 if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
568 /* Should now be out of data */
569 if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
572 /* sendmmsg/recvmmsg */
573 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
576 msgs[0].data = scratch;
577 msgs[0].data_len = 19;
578 msgs[1].data = scratch + 19;
579 msgs[1].data_len = 46;
581 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
583 || !TEST_size_t_eq(num_processed, 2))
586 rmsgs[0].data = scratch2;
587 rmsgs[0].data_len = 64;
588 rmsgs[1].data = scratch2 + 64;
589 rmsgs[1].data_len = 64;
590 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
592 || !TEST_size_t_eq(num_processed, 2))
595 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
598 if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
601 /* sendmmsg/recvmmsg with peer */
602 addr1 = BIO_ADDR_new();
603 if (!TEST_ptr(addr1))
606 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
607 sizeof(in_local), 1234), 1))
610 addr2 = BIO_ADDR_new();
611 if (!TEST_ptr(addr2))
614 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
615 sizeof(in_local), 2345), 1))
618 addr3 = BIO_ADDR_new();
619 if (!TEST_ptr(addr3))
622 addr4 = BIO_ADDR_new();
623 if (!TEST_ptr(addr4))
626 msgs[0].peer = addr1;
628 /* fails due to lack of caps on peer */
629 if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs),
631 || !TEST_size_t_eq(num_processed, 0))
634 if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
637 if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
640 if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
643 if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
646 if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
649 /* succeeds with cap now available */
650 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
651 || !TEST_size_t_eq(num_processed, 1))
654 /* enable local addr support */
655 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
658 rmsgs[0].data = scratch2;
659 rmsgs[0].data_len = 64;
660 rmsgs[0].peer = addr3;
661 rmsgs[0].local = addr4;
662 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
664 || !TEST_size_t_eq(num_processed, 1))
667 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
670 /* We didn't set the source address so this should be zero */
671 if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
674 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
677 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
680 /* test source address */
681 msgs[0].local = addr2;
683 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
686 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
687 || !TEST_size_t_eq(num_processed, 1))
690 rmsgs[0].data = scratch2;
691 rmsgs[0].data_len = 64;
692 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
693 || !TEST_size_t_eq(num_processed, 1))
696 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
697 msgs[0].data, msgs[0].data_len))
700 if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
703 if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
706 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
709 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
712 /* test truncation, pending */
713 r = BIO_write(bio1, scratch, 64);
714 if (!TEST_int_eq(r, 64))
717 memset(scratch2, 0, 64);
718 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
721 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
724 if (!TEST_int_eq(BIO_pending(bio2), 64))
727 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
730 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
733 if (!TEST_mem_eq(scratch, 32, scratch2, 32))
740 BIO_ADDR_free(addr1);
741 BIO_ADDR_free(addr2);
742 BIO_ADDR_free(addr3);
743 BIO_ADDR_free(addr4);
747 #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
749 int setup_tests(void)
751 if (!test_skip_common_options()) {
752 TEST_error("Error parsing test options\n");
756 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
757 ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
758 ADD_TEST(test_bio_dgram_pair);