Coverage Report

Created: 2026-05-24 07:14

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