Coverage Report

Created: 2026-05-20 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bss_dgram_pair.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
11
#include <errno.h>
12
#include "bio_local.h"
13
#include "internal/cryptlib.h"
14
#include "internal/safe_math.h"
15
16
#if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
17
18
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
19
20
/* ===========================================================================
21
 * Byte-wise ring buffer which supports pushing and popping blocks of multiple
22
 * bytes at a time.
23
 */
24
struct ring_buf {
25
    unsigned char *start; /* start of buffer */
26
    size_t len; /* size of buffer allocation in bytes */
27
    size_t count; /* number of bytes currently pushed */
28
    /*
29
     * These index into start. Where idx[0] == idx[1], the buffer is full
30
     * (if count is nonzero) and empty otherwise.
31
     */
32
    size_t idx[2]; /* 0: head, 1: tail */
33
};
34
35
static int ring_buf_init(struct ring_buf *r, size_t nbytes)
36
0
{
37
0
    r->start = OPENSSL_malloc(nbytes);
38
0
    if (r->start == NULL)
39
0
        return 0;
40
41
0
    r->len = nbytes;
42
0
    r->idx[0] = r->idx[1] = r->count = 0;
43
0
    return 1;
44
0
}
45
46
static void ring_buf_destroy(struct ring_buf *r)
47
0
{
48
0
    OPENSSL_free(r->start);
49
0
    r->start = NULL;
50
0
    r->len = 0;
51
0
    r->count = 0;
52
0
}
53
54
/*
55
 * Get a pointer to the next place to write data to be pushed to the ring buffer
56
 * (idx=0), or the next data to be popped from the ring buffer (idx=1). The
57
 * pointer is written to *buf and the maximum number of bytes which can be
58
 * read/written are written to *len. After writing data to the buffer, call
59
 * ring_buf_push/pop() with the number of bytes actually read/written, which
60
 * must not exceed the returned length.
61
 */
62
static void ring_buf_head_tail(struct ring_buf *r, int idx, uint8_t **buf, size_t *len)
63
0
{
64
0
    size_t max_len = r->len - r->idx[idx];
65
66
0
    if (idx == 0 && max_len > r->len - r->count)
67
0
        max_len = r->len - r->count;
68
0
    if (idx == 1 && max_len > r->count)
69
0
        max_len = r->count;
70
71
0
    *buf = (uint8_t *)r->start + r->idx[idx];
72
0
    *len = max_len;
73
0
}
74
75
0
#define ring_buf_head(r, buf, len) ring_buf_head_tail((r), 0, (buf), (len))
76
0
#define ring_buf_tail(r, buf, len) ring_buf_head_tail((r), 1, (buf), (len))
77
78
/*
79
 * Commit bytes to the ring buffer previously filled after a call to
80
 * ring_buf_head().
81
 */
82
static void ring_buf_push_pop(struct ring_buf *r, int idx, size_t num_bytes)
83
0
{
84
0
    size_t new_idx;
85
86
    /* A single push/pop op cannot wrap around, though it can reach the end.
87
     * If the caller adheres to the convention of using the length returned
88
     * by ring_buf_head/tail(), this cannot happen.
89
     */
90
0
    if (!ossl_assert(num_bytes <= r->len - r->idx[idx]))
91
0
        return;
92
93
    /*
94
     * Must not overfill the buffer, or pop more than is in the buffer either.
95
     */
96
0
    if (!ossl_assert(idx != 0 ? num_bytes <= r->count
97
0
                              : num_bytes + r->count <= r->len))
98
0
        return;
99
100
    /* Update the index. */
101
0
    new_idx = r->idx[idx] + num_bytes;
102
0
    if (new_idx == r->len)
103
0
        new_idx = 0;
104
105
0
    r->idx[idx] = new_idx;
106
0
    if (idx != 0)
107
0
        r->count -= num_bytes;
108
0
    else
109
0
        r->count += num_bytes;
110
0
}
111
112
0
#define ring_buf_push(r, num_bytes) ring_buf_push_pop((r), 0, (num_bytes))
113
0
#define ring_buf_pop(r, num_bytes) ring_buf_push_pop((r), 1, (num_bytes))
114
115
static void ring_buf_clear(struct ring_buf *r)
116
0
{
117
0
    r->idx[0] = r->idx[1] = r->count = 0;
118
0
}
119
120
static int ring_buf_resize(struct ring_buf *r, size_t nbytes)
121
0
{
122
0
    unsigned char *new_start;
123
124
0
    if (r->start == NULL)
125
0
        return ring_buf_init(r, nbytes);
126
127
0
    if (nbytes == r->len)
128
0
        return 1;
129
130
0
    if (r->count > 0 && nbytes < r->len)
131
        /* fail shrinking the ring buffer when there is any data in it */
132
0
        return 0;
133
134
0
    new_start = OPENSSL_realloc(r->start, nbytes);
135
0
    if (new_start == NULL)
136
0
        return 0;
137
138
    /* Moving tail if it is after (or equal to) head */
139
0
    if (r->count > 0) {
140
0
        if (r->idx[0] <= r->idx[1]) {
141
0
            size_t offset = nbytes - r->len;
142
143
0
            memmove(new_start + r->idx[1] + offset, new_start + r->idx[1],
144
0
                r->len - r->idx[1]);
145
0
            r->idx[1] += offset;
146
0
        }
147
0
    } else {
148
        /* just reset the head/tail because it might be pointing outside */
149
0
        r->idx[0] = r->idx[1] = 0;
150
0
    }
151
152
0
    r->start = new_start;
153
0
    r->len = nbytes;
154
155
0
    return 1;
156
0
}
157
158
/* ===========================================================================
159
 * BIO_s_dgram_pair is documented in BIO_s_dgram_pair(3).
160
 *
161
 * INTERNAL DATA STRUCTURE
162
 *
163
 * This is managed internally by using a bytewise ring buffer which supports
164
 * pushing and popping spans of multiple bytes at once. The ring buffer stores
165
 * internal packets which look like this:
166
 *
167
 *   struct dgram_hdr hdr;
168
 *   uint8_t data[];
169
 *
170
 * The header contains the length of the data and metadata such as
171
 * source/destination addresses.
172
 *
173
 * The datagram pair BIO is designed to support both traditional
174
 * BIO_read/BIO_write (likely to be used by applications) as well as
175
 * BIO_recvmmsg/BIO_sendmmsg.
176
 */
177
struct bio_dgram_pair_st;
178
static int dgram_pair_write(BIO *bio, const char *buf, int sz_);
179
static int dgram_pair_read(BIO *bio, char *buf, int sz_);
180
static int dgram_mem_read(BIO *bio, char *buf, int sz_);
181
static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr);
182
static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr);
183
static int dgram_pair_init(BIO *bio);
184
static int dgram_mem_init(BIO *bio);
185
static int dgram_pair_free(BIO *bio);
186
static int dgram_pair_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
187
    size_t num_msg, uint64_t flags,
188
    size_t *num_processed);
189
static int dgram_pair_recvmmsg(BIO *b, BIO_MSG *msg, size_t stride,
190
    size_t num_msg, uint64_t flags,
191
    size_t *num_processed);
192
193
static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1);
194
static size_t dgram_pair_read_inner(struct bio_dgram_pair_st *b, uint8_t *buf,
195
    size_t sz);
196
197
0
#define BIO_MSG_N(array, n) (*(BIO_MSG *)((char *)(array) + (n) * stride))
198
199
static const BIO_METHOD dgram_pair_method = {
200
    BIO_TYPE_DGRAM_PAIR,
201
    "BIO dgram pair",
202
    bwrite_conv,
203
    dgram_pair_write,
204
    bread_conv,
205
    dgram_pair_read,
206
    NULL, /* dgram_pair_puts */
207
    NULL, /* dgram_pair_gets */
208
    dgram_pair_ctrl,
209
    dgram_pair_init,
210
    dgram_pair_free,
211
    NULL, /* dgram_pair_callback_ctrl */
212
    dgram_pair_sendmmsg,
213
    dgram_pair_recvmmsg,
214
};
215
216
static const BIO_METHOD dgram_mem_method = {
217
    BIO_TYPE_DGRAM_MEM,
218
    "BIO dgram mem",
219
    bwrite_conv,
220
    dgram_pair_write,
221
    bread_conv,
222
    dgram_mem_read,
223
    NULL, /* dgram_pair_puts */
224
    NULL, /* dgram_pair_gets */
225
    dgram_mem_ctrl,
226
    dgram_mem_init,
227
    dgram_pair_free,
228
    NULL, /* dgram_pair_callback_ctrl */
229
    dgram_pair_sendmmsg,
230
    dgram_pair_recvmmsg,
231
};
232
233
const BIO_METHOD *BIO_s_dgram_pair(void)
234
0
{
235
0
    return &dgram_pair_method;
236
0
}
237
238
const BIO_METHOD *BIO_s_dgram_mem(void)
239
0
{
240
0
    return &dgram_mem_method;
241
0
}
242
243
struct dgram_hdr {
244
    size_t len; /* payload length in bytes, not including this struct */
245
    BIO_ADDR src_addr, dst_addr; /* family == 0: not present */
246
};
247
248
struct bio_dgram_pair_st {
249
    /* The other half of the BIO pair. NULL for dgram_mem. */
250
    BIO *peer;
251
    /* Writes are directed to our own ringbuf and reads to our peer. */
252
    struct ring_buf rbuf;
253
    /* Requested size of rbuf buffer in bytes once we initialize. */
254
    size_t req_buf_len;
255
    /* Largest possible datagram size */
256
    size_t mtu;
257
    /* Capability flags. */
258
    uint32_t cap;
259
    /* The local address to use (if set) */
260
    BIO_ADDR *local_addr;
261
    /*
262
     * This lock protects updates to our rbuf. Since writes are directed to our
263
     * own rbuf, this means we use this lock for writes and our peer's lock for
264
     * reads.
265
     */
266
    CRYPTO_RWLOCK *lock;
267
    unsigned int no_trunc : 1; /* Reads fail if they would truncate */
268
    unsigned int local_addr_enable : 1; /* Can use BIO_MSG->local? */
269
    unsigned int role : 1; /* Determines lock order */
270
    unsigned int grows_on_write : 1; /* Set for BIO_s_dgram_mem only */
271
};
272
273
0
#define MIN_BUF_LEN (1024)
274
275
0
#define is_dgram_pair(b) (b->peer != NULL)
276
277
static int dgram_pair_init(BIO *bio)
278
0
{
279
0
    struct bio_dgram_pair_st *b = OPENSSL_zalloc(sizeof(*b));
280
281
0
    if (b == NULL)
282
0
        return 0;
283
284
0
    b->mtu = 1472; /* conservative default MTU */
285
    /* default buffer size */
286
0
    b->req_buf_len = 9 * (sizeof(struct dgram_hdr) + b->mtu);
287
288
0
    b->lock = CRYPTO_THREAD_lock_new();
289
0
    if (b->lock == NULL) {
290
0
        OPENSSL_free(b);
291
0
        return 0;
292
0
    }
293
294
0
    bio->ptr = b;
295
0
    return 1;
296
0
}
297
298
static int dgram_mem_init(BIO *bio)
299
0
{
300
0
    struct bio_dgram_pair_st *b;
301
302
0
    if (!dgram_pair_init(bio))
303
0
        return 0;
304
305
0
    b = bio->ptr;
306
307
0
    if (ring_buf_init(&b->rbuf, b->req_buf_len) == 0) {
308
0
        dgram_pair_free(bio);
309
0
        ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
310
0
        return 0;
311
0
    }
312
313
0
    b->grows_on_write = 1;
314
315
0
    bio->init = 1;
316
0
    return 1;
317
0
}
318
319
static int dgram_pair_free(BIO *bio)
320
0
{
321
0
    struct bio_dgram_pair_st *b;
322
323
0
    if (bio == NULL)
324
0
        return 0;
325
326
0
    b = bio->ptr;
327
0
    if (!ossl_assert(b != NULL))
328
0
        return 0;
329
330
    /* We are being freed. Disconnect any peer and destroy buffers. */
331
0
    dgram_pair_ctrl_destroy_bio_pair(bio);
332
333
0
    CRYPTO_THREAD_lock_free(b->lock);
334
0
    OPENSSL_free(b);
335
0
    return 1;
336
0
}
337
338
/* BIO_make_bio_pair (BIO_C_MAKE_BIO_PAIR) */
339
static int dgram_pair_ctrl_make_bio_pair(BIO *bio1, BIO *bio2)
340
0
{
341
0
    struct bio_dgram_pair_st *b1, *b2;
342
343
    /* peer must be non-NULL. */
344
0
    if (bio1 == NULL || bio2 == NULL) {
345
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
346
0
        return 0;
347
0
    }
348
349
    /* Ensure the BIO we have been passed is actually a dgram pair BIO. */
350
0
    if (bio1->method != &dgram_pair_method || bio2->method != &dgram_pair_method) {
351
0
        ERR_raise_data(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT,
352
0
            "both BIOs must be BIO_dgram_pair");
353
0
        return 0;
354
0
    }
355
356
0
    b1 = bio1->ptr;
357
0
    b2 = bio2->ptr;
358
359
0
    if (!ossl_assert(b1 != NULL && b2 != NULL)) {
360
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
361
0
        return 0;
362
0
    }
363
364
    /*
365
     * This ctrl cannot be used to associate a BIO pair half which is already
366
     * associated.
367
     */
368
0
    if (b1->peer != NULL || b2->peer != NULL) {
369
0
        ERR_raise_data(ERR_LIB_BIO, BIO_R_IN_USE,
370
0
            "cannot associate a BIO_dgram_pair which is already in use");
371
0
        return 0;
372
0
    }
373
374
0
    if (!ossl_assert(b1->req_buf_len >= MIN_BUF_LEN
375
0
            && b2->req_buf_len >= MIN_BUF_LEN)) {
376
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
377
0
        return 0;
378
0
    }
379
380
0
    if (b1->rbuf.len != b1->req_buf_len)
381
0
        if (ring_buf_init(&b1->rbuf, b1->req_buf_len) == 0) {
382
0
            ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
383
0
            return 0;
384
0
        }
385
386
0
    if (b2->rbuf.len != b2->req_buf_len)
387
0
        if (ring_buf_init(&b2->rbuf, b2->req_buf_len) == 0) {
388
0
            ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
389
0
            ring_buf_destroy(&b1->rbuf);
390
0
            return 0;
391
0
        }
392
393
0
    b1->peer = bio2;
394
0
    b2->peer = bio1;
395
0
    b1->role = 0;
396
0
    b2->role = 1;
397
0
    bio1->init = 1;
398
0
    bio2->init = 1;
399
0
    return 1;
400
0
}
401
402
/* BIO_destroy_bio_pair (BIO_C_DESTROY_BIO_PAIR) */
403
static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1)
404
0
{
405
0
    BIO *bio2;
406
0
    struct bio_dgram_pair_st *b1 = bio1->ptr, *b2;
407
408
0
    ring_buf_destroy(&b1->rbuf);
409
0
    bio1->init = 0;
410
411
0
    BIO_ADDR_free(b1->local_addr);
412
413
    /* Early return if we don't have a peer. */
414
0
    if (b1->peer == NULL)
415
0
        return 1;
416
417
0
    bio2 = b1->peer;
418
0
    b2 = bio2->ptr;
419
420
    /* Invariant. */
421
0
    if (!ossl_assert(b2->peer == bio1))
422
0
        return 0;
423
424
    /* Free buffers. */
425
0
    ring_buf_destroy(&b2->rbuf);
426
427
0
    bio2->init = 0;
428
0
    b1->peer = NULL;
429
0
    b2->peer = NULL;
430
0
    return 1;
431
0
}
432
433
/* BIO_eof (BIO_CTRL_EOF) */
434
static int dgram_pair_ctrl_eof(BIO *bio)
435
0
{
436
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
437
438
0
    if (!ossl_assert(b != NULL))
439
0
        return -1;
440
441
    /* If we aren't initialized, we can never read anything */
442
0
    if (!bio->init)
443
0
        return 1;
444
0
    if (!is_dgram_pair(b))
445
0
        return 0;
446
447
0
    peerb = b->peer->ptr;
448
0
    if (!ossl_assert(peerb != NULL))
449
0
        return -1;
450
451
    /*
452
     * Since we are emulating datagram semantics, never indicate EOF so long as
453
     * we have a peer.
454
     */
455
0
    return 0;
456
0
}
457
458
/* BIO_set_write_buf_size (BIO_C_SET_WRITE_BUF_SIZE) */
459
static int dgram_pair_ctrl_set_write_buf_size(BIO *bio, size_t len)
460
0
{
461
0
    struct bio_dgram_pair_st *b = bio->ptr;
462
463
    /* Changing buffer sizes is not permitted while a peer is connected. */
464
0
    if (b->peer != NULL) {
465
0
        ERR_raise(ERR_LIB_BIO, BIO_R_IN_USE);
466
0
        return 0;
467
0
    }
468
469
    /* Enforce minimum size. */
470
0
    if (len < MIN_BUF_LEN)
471
0
        len = MIN_BUF_LEN;
472
473
0
    if (b->rbuf.start != NULL) {
474
0
        if (!ring_buf_resize(&b->rbuf, len))
475
0
            return 0;
476
0
    }
477
478
0
    b->req_buf_len = len;
479
0
    b->grows_on_write = 0;
480
0
    return 1;
481
0
}
482
483
/* BIO_reset (BIO_CTRL_RESET) */
484
static int dgram_pair_ctrl_reset(BIO *bio)
485
0
{
486
0
    struct bio_dgram_pair_st *b = bio->ptr;
487
488
0
    ring_buf_clear(&b->rbuf);
489
0
    return 1;
490
0
}
491
492
/* BIO_pending (BIO_CTRL_PENDING) (Threadsafe) */
493
static size_t dgram_pair_ctrl_pending(BIO *bio)
494
0
{
495
0
    size_t saved_idx, saved_count;
496
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
497
0
    struct dgram_hdr hdr;
498
0
    size_t l;
499
500
    /* Safe to check; init may not change during this call */
501
0
    if (!bio->init)
502
0
        return 0;
503
0
    if (is_dgram_pair(b))
504
0
        readb = b->peer->ptr;
505
0
    else
506
0
        readb = b;
507
508
0
    if (CRYPTO_THREAD_write_lock(readb->lock) == 0)
509
0
        return 0;
510
511
0
    saved_idx = readb->rbuf.idx[1];
512
0
    saved_count = readb->rbuf.count;
513
514
0
    l = dgram_pair_read_inner(readb, (uint8_t *)&hdr, sizeof(hdr));
515
516
0
    readb->rbuf.idx[1] = saved_idx;
517
0
    readb->rbuf.count = saved_count;
518
519
0
    CRYPTO_THREAD_unlock(readb->lock);
520
521
0
    if (!ossl_assert(l == 0 || l == sizeof(hdr)))
522
0
        return 0;
523
524
0
    return l > 0 ? hdr.len : 0;
525
0
}
526
527
/* BIO_get_write_guarantee (BIO_C_GET_WRITE_GUARANTEE) (Threadsafe) */
528
static size_t dgram_pair_ctrl_get_write_guarantee(BIO *bio)
529
0
{
530
0
    size_t l;
531
0
    struct bio_dgram_pair_st *b = bio->ptr;
532
533
0
    if (CRYPTO_THREAD_read_lock(b->lock) == 0)
534
0
        return 0;
535
536
0
    l = b->rbuf.len - b->rbuf.count;
537
0
    if (l >= sizeof(struct dgram_hdr))
538
0
        l -= sizeof(struct dgram_hdr);
539
540
    /*
541
     * If the amount of buffer space would not be enough to accommodate the
542
     * worst-case size of a datagram, report no space available.
543
     */
544
0
    if (l < b->mtu)
545
0
        l = 0;
546
547
0
    CRYPTO_THREAD_unlock(b->lock);
548
0
    return l;
549
0
}
550
551
/* BIO_dgram_get_local_addr_cap (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP) */
552
static int dgram_pair_ctrl_get_local_addr_cap(BIO *bio)
553
0
{
554
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
555
556
0
    if (!bio->init)
557
0
        return 0;
558
559
0
    if (is_dgram_pair(b))
560
0
        readb = b->peer->ptr;
561
0
    else
562
0
        readb = b;
563
564
0
    return (~readb->cap & (BIO_DGRAM_CAP_HANDLES_SRC_ADDR | BIO_DGRAM_CAP_PROVIDES_DST_ADDR)) == 0;
565
0
}
566
567
/* BIO_dgram_get_effective_caps (BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS) */
568
static int dgram_pair_ctrl_get_effective_caps(BIO *bio)
569
0
{
570
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
571
572
0
    if (b->peer == NULL)
573
0
        return 0;
574
575
0
    peerb = b->peer->ptr;
576
577
0
    return peerb->cap;
578
0
}
579
580
/* BIO_dgram_get_caps (BIO_CTRL_DGRAM_GET_CAPS) */
581
static uint32_t dgram_pair_ctrl_get_caps(BIO *bio)
582
0
{
583
0
    struct bio_dgram_pair_st *b = bio->ptr;
584
585
0
    return b->cap;
586
0
}
587
588
/* BIO_dgram_set_caps (BIO_CTRL_DGRAM_SET_CAPS) */
589
static int dgram_pair_ctrl_set_caps(BIO *bio, uint32_t caps)
590
0
{
591
0
    struct bio_dgram_pair_st *b = bio->ptr;
592
593
0
    b->cap = caps;
594
0
    return 1;
595
0
}
596
597
/* BIO_dgram_get_local_addr_enable (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE) */
598
static int dgram_pair_ctrl_get_local_addr_enable(BIO *bio)
599
0
{
600
0
    struct bio_dgram_pair_st *b = bio->ptr;
601
602
0
    return b->local_addr_enable;
603
0
}
604
605
/* BIO_dgram_set_local_addr_enable (BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE) */
606
static int dgram_pair_ctrl_set_local_addr_enable(BIO *bio, int enable)
607
0
{
608
0
    struct bio_dgram_pair_st *b = bio->ptr;
609
610
0
    if (dgram_pair_ctrl_get_local_addr_cap(bio) == 0)
611
0
        return 0;
612
613
0
    b->local_addr_enable = (enable != 0 ? 1 : 0);
614
0
    return 1;
615
0
}
616
617
/* BIO_dgram_get_mtu (BIO_CTRL_DGRAM_GET_MTU) */
618
static int dgram_pair_ctrl_get_mtu(BIO *bio)
619
0
{
620
0
    struct bio_dgram_pair_st *b = bio->ptr;
621
622
0
    return (int)b->mtu;
623
0
}
624
625
/* BIO_dgram_set_mtu (BIO_CTRL_DGRAM_SET_MTU) */
626
static int dgram_pair_ctrl_set_mtu(BIO *bio, size_t mtu)
627
0
{
628
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
629
630
0
    b->mtu = mtu;
631
632
0
    if (b->peer != NULL) {
633
0
        peerb = b->peer->ptr;
634
0
        peerb->mtu = mtu;
635
0
    }
636
637
0
    return 1;
638
0
}
639
640
/* BIO_dgram_set0_local_addr (BIO_CTRL_DGRAM_SET0_LOCAL_ADDR) */
641
static int dgram_pair_ctrl_set0_local_addr(BIO *bio, BIO_ADDR *addr)
642
0
{
643
0
    struct bio_dgram_pair_st *b = bio->ptr;
644
645
0
    BIO_ADDR_free(b->local_addr);
646
0
    b->local_addr = addr;
647
0
    return 1;
648
0
}
649
650
/* Partially threadsafe (some commands) */
651
static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
652
0
{
653
0
    long ret = 1;
654
0
    struct bio_dgram_pair_st *b = bio->ptr;
655
656
0
    if (!ossl_assert(b != NULL))
657
0
        return 0;
658
659
0
    switch (cmd) {
660
    /*
661
     * BIO_set_write_buf_size: Set the size of the ring buffer used for storing
662
     * datagrams. No more writes can be performed once the buffer is filled up,
663
     * until reads are performed. This cannot be used after a peer is connected.
664
     */
665
0
    case BIO_C_SET_WRITE_BUF_SIZE: /* Non-threadsafe */
666
0
        ret = (long)dgram_pair_ctrl_set_write_buf_size(bio, (size_t)num);
667
0
        break;
668
669
    /*
670
     * BIO_get_write_buf_size: Get ring buffer size.
671
     */
672
0
    case BIO_C_GET_WRITE_BUF_SIZE: /* Non-threadsafe */
673
0
        ret = (long)b->req_buf_len;
674
0
        break;
675
676
    /*
677
     * BIO_reset: Clear all data which was written to this side of the pair.
678
     */
679
0
    case BIO_CTRL_RESET: /* Non-threadsafe */
680
0
        dgram_pair_ctrl_reset(bio);
681
0
        break;
682
683
    /*
684
     * BIO_get_write_guarantee: Any BIO_write providing a buffer less than or
685
     * equal to this value is guaranteed to succeed.
686
     */
687
0
    case BIO_C_GET_WRITE_GUARANTEE: /* Threadsafe */
688
0
        ret = (long)dgram_pair_ctrl_get_write_guarantee(bio);
689
0
        break;
690
691
    /* BIO_pending: Bytes available to read. */
692
0
    case BIO_CTRL_PENDING: /* Threadsafe */
693
0
        ret = (long)dgram_pair_ctrl_pending(bio);
694
0
        break;
695
696
    /* BIO_flush: No-op. */
697
0
    case BIO_CTRL_FLUSH: /* Threadsafe */
698
0
        break;
699
700
    /* BIO_dgram_get_no_trunc */
701
0
    case BIO_CTRL_DGRAM_GET_NO_TRUNC: /* Non-threadsafe */
702
0
        ret = (long)b->no_trunc;
703
0
        break;
704
705
    /* BIO_dgram_set_no_trunc */
706
0
    case BIO_CTRL_DGRAM_SET_NO_TRUNC: /* Non-threadsafe */
707
0
        b->no_trunc = (num > 0);
708
0
        break;
709
710
    /* BIO_dgram_get_local_addr_enable */
711
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
712
0
        *(int *)ptr = (int)dgram_pair_ctrl_get_local_addr_enable(bio);
713
0
        break;
714
715
    /* BIO_dgram_set_local_addr_enable */
716
0
    case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
717
0
        ret = (long)dgram_pair_ctrl_set_local_addr_enable(bio, num);
718
0
        break;
719
720
    /* BIO_dgram_get_local_addr_cap: Can local addresses be supported? */
721
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP: /* Non-threadsafe */
722
0
        ret = (long)dgram_pair_ctrl_get_local_addr_cap(bio);
723
0
        break;
724
725
    /* BIO_dgram_get_effective_caps */
726
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
727
    /* BIO_dgram_get_caps */
728
0
    case BIO_CTRL_DGRAM_GET_CAPS: /* Non-threadsafe */
729
0
        ret = (long)dgram_pair_ctrl_get_caps(bio);
730
0
        break;
731
732
    /* BIO_dgram_set_caps */
733
0
    case BIO_CTRL_DGRAM_SET_CAPS: /* Non-threadsafe */
734
0
        ret = (long)dgram_pair_ctrl_set_caps(bio, (uint32_t)num);
735
0
        break;
736
737
    /* BIO_dgram_get_mtu */
738
0
    case BIO_CTRL_DGRAM_GET_MTU: /* Non-threadsafe */
739
0
        ret = (long)dgram_pair_ctrl_get_mtu(bio);
740
0
        break;
741
742
    /* BIO_dgram_set_mtu */
743
0
    case BIO_CTRL_DGRAM_SET_MTU: /* Non-threadsafe */
744
0
        ret = (long)dgram_pair_ctrl_set_mtu(bio, (uint32_t)num);
745
0
        break;
746
747
0
    case BIO_CTRL_DGRAM_SET0_LOCAL_ADDR:
748
0
        ret = (long)dgram_pair_ctrl_set0_local_addr(bio, (BIO_ADDR *)ptr);
749
0
        break;
750
751
    /*
752
     * BIO_eof: Returns whether this half of the BIO pair is empty of data to
753
     * read.
754
     */
755
0
    case BIO_CTRL_EOF: /* Non-threadsafe */
756
0
        ret = (long)dgram_pair_ctrl_eof(bio);
757
0
        break;
758
759
0
    default:
760
0
        ret = 0;
761
0
        break;
762
0
    }
763
764
0
    return ret;
765
0
}
766
767
static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr)
768
0
{
769
0
    long ret = 1;
770
771
0
    switch (cmd) {
772
    /*
773
     * BIO_make_bio_pair: this is usually used by BIO_new_dgram_pair, though it
774
     * may be used manually after manually creating each half of a BIO pair
775
     * using BIO_new. This only needs to be called on one of the BIOs.
776
     */
777
0
    case BIO_C_MAKE_BIO_PAIR: /* Non-threadsafe */
778
0
        ret = (long)dgram_pair_ctrl_make_bio_pair(bio, (BIO *)ptr);
779
0
        break;
780
781
    /*
782
     * BIO_destroy_bio_pair: Manually disconnect two halves of a BIO pair so
783
     * that they are no longer peers.
784
     */
785
0
    case BIO_C_DESTROY_BIO_PAIR: /* Non-threadsafe */
786
0
        dgram_pair_ctrl_destroy_bio_pair(bio);
787
0
        break;
788
789
    /* BIO_dgram_get_effective_caps */
790
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
791
0
        ret = (long)dgram_pair_ctrl_get_effective_caps(bio);
792
0
        break;
793
794
0
    default:
795
0
        ret = dgram_mem_ctrl(bio, cmd, num, ptr);
796
0
        break;
797
0
    }
798
799
0
    return ret;
800
0
}
801
802
int BIO_new_bio_dgram_pair(BIO **pbio1, size_t writebuf1,
803
    BIO **pbio2, size_t writebuf2)
804
0
{
805
0
    int ret = 0;
806
0
    long r;
807
0
    BIO *bio1 = NULL, *bio2 = NULL;
808
809
0
    if (writebuf1 > LONG_MAX || writebuf2 > LONG_MAX)
810
0
        goto err;
811
812
0
    bio1 = BIO_new(BIO_s_dgram_pair());
813
0
    if (bio1 == NULL)
814
0
        goto err;
815
816
0
    bio2 = BIO_new(BIO_s_dgram_pair());
817
0
    if (bio2 == NULL)
818
0
        goto err;
819
820
0
    if (writebuf1 > 0) {
821
0
        r = BIO_set_write_buf_size(bio1, (long)writebuf1);
822
0
        if (r == 0)
823
0
            goto err;
824
0
    }
825
826
0
    if (writebuf2 > 0) {
827
0
        r = BIO_set_write_buf_size(bio2, (long)writebuf2);
828
0
        if (r == 0)
829
0
            goto err;
830
0
    }
831
832
0
    r = BIO_make_bio_pair(bio1, bio2);
833
0
    if (r == 0)
834
0
        goto err;
835
836
0
    ret = 1;
837
0
err:
838
0
    if (ret == 0) {
839
0
        BIO_free(bio1);
840
0
        bio1 = NULL;
841
0
        BIO_free(bio2);
842
0
        bio2 = NULL;
843
0
    }
844
845
0
    *pbio1 = bio1;
846
0
    *pbio2 = bio2;
847
0
    return ret;
848
0
}
849
850
/* Must hold peer write lock */
851
static size_t dgram_pair_read_inner(struct bio_dgram_pair_st *b, uint8_t *buf, size_t sz)
852
0
{
853
0
    size_t total_read = 0;
854
855
    /*
856
     * We repeat pops from the ring buffer for as long as we have more
857
     * application *buffer to fill until we fail. We may not be able to pop
858
     * enough data to fill the buffer in one operation if the ring buffer wraps
859
     * around, but there may still be more data available.
860
     */
861
0
    while (sz > 0) {
862
0
        uint8_t *src_buf = NULL;
863
0
        size_t src_len = 0;
864
865
        /*
866
         * There are two BIO instances, each with a ringbuf. We read from the
867
         * peer ringbuf and write to our own ringbuf.
868
         */
869
0
        ring_buf_tail(&b->rbuf, &src_buf, &src_len);
870
0
        if (src_len == 0)
871
0
            break;
872
873
0
        if (src_len > sz)
874
0
            src_len = sz;
875
876
0
        if (buf != NULL)
877
0
            memcpy(buf, src_buf, src_len);
878
879
0
        ring_buf_pop(&b->rbuf, src_len);
880
881
0
        if (buf != NULL)
882
0
            buf += src_len;
883
0
        total_read += src_len;
884
0
        sz -= src_len;
885
0
    }
886
887
0
    return total_read;
888
0
}
889
890
/*
891
 * Must hold peer write lock. Returns number of bytes processed or negated BIO
892
 * response code.
893
 */
894
static ossl_ssize_t dgram_pair_read_actual(BIO *bio, char *buf, size_t sz,
895
    BIO_ADDR *local, BIO_ADDR *peer,
896
    int is_multi)
897
0
{
898
0
    size_t l, trunc = 0, saved_idx, saved_count;
899
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
900
0
    struct dgram_hdr hdr;
901
902
0
    if (!is_multi)
903
0
        BIO_clear_retry_flags(bio);
904
905
0
    if (!bio->init)
906
0
        return -BIO_R_UNINITIALIZED;
907
908
0
    if (!ossl_assert(b != NULL))
909
0
        return -BIO_R_TRANSFER_ERROR;
910
911
0
    if (is_dgram_pair(b))
912
0
        readb = b->peer->ptr;
913
0
    else
914
0
        readb = b;
915
0
    if (!ossl_assert(readb != NULL && readb->rbuf.start != NULL))
916
0
        return -BIO_R_TRANSFER_ERROR;
917
918
0
    if (sz > 0 && buf == NULL)
919
0
        return -BIO_R_INVALID_ARGUMENT;
920
921
    /* If the caller wants to know the local address, it must be enabled */
922
0
    if (local != NULL && b->local_addr_enable == 0)
923
0
        return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
924
925
    /* Read the header. */
926
0
    saved_idx = readb->rbuf.idx[1];
927
0
    saved_count = readb->rbuf.count;
928
0
    l = dgram_pair_read_inner(readb, (uint8_t *)&hdr, sizeof(hdr));
929
0
    if (l == 0) {
930
        /* Buffer was empty. */
931
0
        if (!is_multi)
932
0
            BIO_set_retry_read(bio);
933
0
        return -BIO_R_NON_FATAL;
934
0
    }
935
936
0
    if (!ossl_assert(l == sizeof(hdr)))
937
        /*
938
         * This should not be possible as headers (and their following payloads)
939
         * should always be written atomically.
940
         */
941
0
        return -BIO_R_BROKEN_PIPE;
942
943
0
    if (sz > hdr.len) {
944
0
        sz = hdr.len;
945
0
    } else if (sz < hdr.len) {
946
        /* Truncation is occurring. */
947
0
        trunc = hdr.len - sz;
948
0
        if (b->no_trunc) {
949
            /* Restore original state. */
950
0
            readb->rbuf.idx[1] = saved_idx;
951
0
            readb->rbuf.count = saved_count;
952
0
            return -BIO_R_NON_FATAL;
953
0
        }
954
0
    }
955
956
0
    l = dgram_pair_read_inner(readb, (uint8_t *)buf, sz);
957
0
    if (!ossl_assert(l == sz))
958
        /* We were somehow not able to read the entire datagram. */
959
0
        return -BIO_R_TRANSFER_ERROR;
960
961
    /*
962
     * If the datagram was truncated due to an inadequate buffer, discard the
963
     * remainder.
964
     */
965
0
    if (trunc > 0 && !ossl_assert(dgram_pair_read_inner(readb, NULL, trunc) == trunc))
966
        /* We were somehow not able to read/skip the entire datagram. */
967
0
        return -BIO_R_TRANSFER_ERROR;
968
969
0
    if (local != NULL)
970
0
        *local = hdr.dst_addr;
971
0
    if (peer != NULL)
972
0
        *peer = hdr.src_addr;
973
974
0
    return (ossl_ssize_t)l;
975
0
}
976
977
/* Threadsafe */
978
static int dgram_pair_lock_both_write(struct bio_dgram_pair_st *a,
979
    struct bio_dgram_pair_st *b)
980
0
{
981
0
    struct bio_dgram_pair_st *x, *y;
982
983
0
    x = (a->role == 1) ? a : b;
984
0
    y = (a->role == 1) ? b : a;
985
986
0
    if (!ossl_assert(a->role != b->role))
987
0
        return 0;
988
989
0
    if (!ossl_assert(a != b && x != y))
990
0
        return 0;
991
992
0
    if (CRYPTO_THREAD_write_lock(x->lock) == 0)
993
0
        return 0;
994
995
0
    if (CRYPTO_THREAD_write_lock(y->lock) == 0) {
996
0
        CRYPTO_THREAD_unlock(x->lock);
997
0
        return 0;
998
0
    }
999
1000
0
    return 1;
1001
0
}
1002
1003
static void dgram_pair_unlock_both(struct bio_dgram_pair_st *a,
1004
    struct bio_dgram_pair_st *b)
1005
0
{
1006
0
    CRYPTO_THREAD_unlock(a->lock);
1007
0
    CRYPTO_THREAD_unlock(b->lock);
1008
0
}
1009
1010
/* Threadsafe */
1011
static int dgram_pair_read(BIO *bio, char *buf, int sz_)
1012
0
{
1013
0
    int ret;
1014
0
    ossl_ssize_t l;
1015
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
1016
1017
0
    if (sz_ < 0) {
1018
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1019
0
        return -1;
1020
0
    }
1021
1022
0
    if (b->peer == NULL) {
1023
0
        ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
1024
0
        return -1;
1025
0
    }
1026
1027
0
    peerb = b->peer->ptr;
1028
1029
    /*
1030
     * For BIO_read we have to acquire both locks because we touch the retry
1031
     * flags on the local bio. (This is avoided in the recvmmsg case as it does
1032
     * not touch the retry flags.)
1033
     */
1034
0
    if (dgram_pair_lock_both_write(peerb, b) == 0) {
1035
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1036
0
        return -1;
1037
0
    }
1038
1039
0
    l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1040
0
    if (l < 0) {
1041
0
        if (l != -BIO_R_NON_FATAL)
1042
0
            ERR_raise(ERR_LIB_BIO, (int)-l);
1043
0
        ret = -1;
1044
0
    } else {
1045
0
        ret = (int)l;
1046
0
    }
1047
1048
0
    dgram_pair_unlock_both(peerb, b);
1049
0
    return ret;
1050
0
}
1051
1052
/* Threadsafe */
1053
static int dgram_pair_recvmmsg(BIO *bio, BIO_MSG *msg,
1054
    size_t stride, size_t num_msg,
1055
    uint64_t flags,
1056
    size_t *num_processed)
1057
0
{
1058
0
    int ret;
1059
0
    ossl_ssize_t l;
1060
0
    BIO_MSG *m;
1061
0
    size_t i;
1062
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
1063
1064
0
    if (num_msg == 0) {
1065
0
        *num_processed = 0;
1066
0
        return 1;
1067
0
    }
1068
1069
0
    if (!bio->init) {
1070
0
        ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
1071
0
        *num_processed = 0;
1072
0
        return 0;
1073
0
    }
1074
1075
0
    if (is_dgram_pair(b))
1076
0
        readb = b->peer->ptr;
1077
0
    else
1078
0
        readb = b;
1079
1080
0
    if (CRYPTO_THREAD_write_lock(readb->lock) == 0) {
1081
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1082
0
        *num_processed = 0;
1083
0
        return 0;
1084
0
    }
1085
1086
0
    for (i = 0; i < num_msg; ++i) {
1087
0
        m = &BIO_MSG_N(msg, i);
1088
0
        l = dgram_pair_read_actual(bio, m->data, m->data_len,
1089
0
            m->local, m->peer, 1);
1090
0
        if (l < 0) {
1091
0
            *num_processed = i;
1092
0
            if (i > 0) {
1093
0
                ret = 1;
1094
0
            } else {
1095
0
                ERR_raise(ERR_LIB_BIO, (int)-l);
1096
0
                ret = 0;
1097
0
            }
1098
0
            goto out;
1099
0
        }
1100
1101
0
        m->data_len = l;
1102
0
        m->flags = 0;
1103
0
    }
1104
1105
0
    *num_processed = i;
1106
0
    ret = 1;
1107
0
out:
1108
0
    CRYPTO_THREAD_unlock(readb->lock);
1109
0
    return ret;
1110
0
}
1111
1112
/* Threadsafe */
1113
static int dgram_mem_read(BIO *bio, char *buf, int sz_)
1114
0
{
1115
0
    int ret;
1116
0
    ossl_ssize_t l;
1117
0
    struct bio_dgram_pair_st *b = bio->ptr;
1118
1119
0
    if (sz_ < 0) {
1120
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1121
0
        return -1;
1122
0
    }
1123
1124
0
    if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
1125
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1126
0
        return -1;
1127
0
    }
1128
1129
0
    l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1130
0
    if (l < 0) {
1131
0
        if (l != -BIO_R_NON_FATAL)
1132
0
            ERR_raise(ERR_LIB_BIO, (int)-l);
1133
0
        ret = -1;
1134
0
    } else {
1135
0
        ret = (int)l;
1136
0
    }
1137
1138
0
    CRYPTO_THREAD_unlock(b->lock);
1139
0
    return ret;
1140
0
}
1141
1142
/*
1143
 * Calculate the array growth based on the target size.
1144
 *
1145
 * The growth factor is a rational number and is defined by a numerator
1146
 * and a denominator.  According to Andrew Koenig in his paper "Why Are
1147
 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
1148
 * than the golden ratio (1.618...).
1149
 *
1150
 * We use an expansion factor of 8 / 5 = 1.6
1151
 */
1152
static const size_t max_rbuf_size = SIZE_MAX / 2; /* unlimited in practice */
1153
static ossl_inline size_t compute_rbuf_growth(size_t target, size_t current)
1154
0
{
1155
0
    int err = 0;
1156
1157
0
    while (current < target) {
1158
0
        if (current >= max_rbuf_size)
1159
0
            return 0;
1160
1161
0
        current = safe_muldiv_size_t(current, 8, 5, &err);
1162
0
        if (err)
1163
0
            return 0;
1164
0
        if (current >= max_rbuf_size)
1165
0
            current = max_rbuf_size;
1166
0
    }
1167
0
    return current;
1168
0
}
1169
1170
/* Must hold local write lock */
1171
static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b,
1172
    const uint8_t *buf, size_t sz)
1173
0
{
1174
0
    size_t total_written = 0;
1175
1176
    /*
1177
     * We repeat pushes to the ring buffer for as long as we have data until we
1178
     * fail. We may not be able to push in one operation if the ring buffer
1179
     * wraps around, but there may still be more room for data.
1180
     */
1181
0
    while (sz > 0) {
1182
0
        size_t dst_len;
1183
0
        uint8_t *dst_buf;
1184
1185
        /*
1186
         * There are two BIO instances, each with a ringbuf. We write to our own
1187
         * ringbuf and read from the peer ringbuf.
1188
         */
1189
0
        ring_buf_head(&b->rbuf, &dst_buf, &dst_len);
1190
0
        if (dst_len == 0) {
1191
0
            size_t new_len;
1192
1193
0
            if (!b->grows_on_write) /* resize only if size not set explicitly */
1194
0
                break;
1195
            /* increase the size */
1196
0
            new_len = compute_rbuf_growth(b->req_buf_len + sz, b->req_buf_len);
1197
0
            if (new_len == 0 || !ring_buf_resize(&b->rbuf, new_len))
1198
0
                break;
1199
0
            b->req_buf_len = new_len;
1200
0
        }
1201
1202
0
        if (dst_len > sz)
1203
0
            dst_len = sz;
1204
1205
0
        memcpy(dst_buf, buf, dst_len);
1206
0
        ring_buf_push(&b->rbuf, dst_len);
1207
1208
0
        buf += dst_len;
1209
0
        sz -= dst_len;
1210
0
        total_written += dst_len;
1211
0
    }
1212
1213
0
    return total_written;
1214
0
}
1215
1216
/*
1217
 * Must hold local write lock. Returns number of bytes processed or negated BIO
1218
 * response code.
1219
 */
1220
static ossl_ssize_t dgram_pair_write_actual(BIO *bio, const char *buf, size_t sz,
1221
    const BIO_ADDR *local, const BIO_ADDR *peer,
1222
    int is_multi)
1223
0
{
1224
0
    static const BIO_ADDR zero_addr;
1225
0
    size_t saved_idx, saved_count;
1226
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
1227
0
    struct dgram_hdr hdr = { 0 };
1228
1229
0
    if (!is_multi)
1230
0
        BIO_clear_retry_flags(bio);
1231
1232
0
    if (!bio->init)
1233
0
        return -BIO_R_UNINITIALIZED;
1234
1235
0
    if (!ossl_assert(b != NULL && b->rbuf.start != NULL))
1236
0
        return -BIO_R_TRANSFER_ERROR;
1237
1238
0
    if (sz > 0 && buf == NULL)
1239
0
        return -BIO_R_INVALID_ARGUMENT;
1240
1241
0
    if (local != NULL && b->local_addr_enable == 0)
1242
0
        return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
1243
1244
0
    if (is_dgram_pair(b))
1245
0
        readb = b->peer->ptr;
1246
0
    else
1247
0
        readb = b;
1248
0
    if (peer != NULL && (readb->cap & BIO_DGRAM_CAP_HANDLES_DST_ADDR) == 0)
1249
0
        return -BIO_R_PEER_ADDR_NOT_AVAILABLE;
1250
1251
0
    hdr.len = sz;
1252
0
    hdr.dst_addr = (peer != NULL ? *peer : zero_addr);
1253
0
    if (local == NULL)
1254
0
        local = b->local_addr;
1255
0
    hdr.src_addr = (local != NULL ? *local : zero_addr);
1256
1257
0
    saved_idx = b->rbuf.idx[0];
1258
0
    saved_count = b->rbuf.count;
1259
0
    if (dgram_pair_write_inner(b, (const uint8_t *)&hdr, sizeof(hdr)) != sizeof(hdr)
1260
0
        || dgram_pair_write_inner(b, (const uint8_t *)buf, sz) != sz) {
1261
        /*
1262
         * We were not able to push the header and the entirety of the payload
1263
         * onto the ring buffer, so abort and roll back the ring buffer state.
1264
         */
1265
0
        b->rbuf.idx[0] = saved_idx;
1266
0
        b->rbuf.count = saved_count;
1267
0
        if (!is_multi)
1268
0
            BIO_set_retry_write(bio);
1269
0
        return -BIO_R_NON_FATAL;
1270
0
    }
1271
1272
0
    return sz;
1273
0
}
1274
1275
/* Threadsafe */
1276
static int dgram_pair_write(BIO *bio, const char *buf, int sz_)
1277
0
{
1278
0
    int ret;
1279
0
    ossl_ssize_t l;
1280
0
    struct bio_dgram_pair_st *b = bio->ptr;
1281
1282
0
    if (sz_ < 0) {
1283
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1284
0
        return -1;
1285
0
    }
1286
1287
0
    if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
1288
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1289
0
        return -1;
1290
0
    }
1291
1292
0
    l = dgram_pair_write_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1293
0
    if (l < 0) {
1294
0
        ERR_raise(ERR_LIB_BIO, (int)-l);
1295
0
        ret = -1;
1296
0
    } else {
1297
0
        ret = (int)l;
1298
0
    }
1299
1300
0
    CRYPTO_THREAD_unlock(b->lock);
1301
0
    return ret;
1302
0
}
1303
1304
/* Threadsafe */
1305
static int dgram_pair_sendmmsg(BIO *bio, BIO_MSG *msg,
1306
    size_t stride, size_t num_msg,
1307
    uint64_t flags, size_t *num_processed)
1308
0
{
1309
0
    ossl_ssize_t l;
1310
0
    BIO_MSG *m;
1311
0
    size_t i;
1312
0
    struct bio_dgram_pair_st *b = bio->ptr;
1313
0
    int ret = 0;
1314
1315
0
    if (num_msg == 0) {
1316
0
        *num_processed = 0;
1317
0
        return 1;
1318
0
    }
1319
1320
0
    if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
1321
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1322
0
        *num_processed = 0;
1323
0
        return 0;
1324
0
    }
1325
1326
0
    for (i = 0; i < num_msg; ++i) {
1327
0
        m = &BIO_MSG_N(msg, i);
1328
0
        l = dgram_pair_write_actual(bio, m->data, m->data_len,
1329
0
            m->local, m->peer, 1);
1330
0
        if (l < 0) {
1331
0
            *num_processed = i;
1332
0
            if (i > 0) {
1333
0
                ret = 1;
1334
0
            } else {
1335
0
                ERR_raise(ERR_LIB_BIO, (int)-l);
1336
0
            }
1337
0
            goto out;
1338
0
        }
1339
1340
0
        m->flags = 0;
1341
0
    }
1342
1343
0
    *num_processed = i;
1344
0
    ret = 1;
1345
0
out:
1346
0
    CRYPTO_THREAD_unlock(b->lock);
1347
0
    return ret;
1348
0
}
1349
1350
#endif