Coverage Report

Created: 2026-11-16 07:12

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-2026 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
#include "internal/threads_common.h"
16
17
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
18
#include <openssl/rand.h>
19
#include <stdio.h>
20
21
#include "internal/time.h"
22
#include "internal/bio_addr.h"
23
#include "internal/thread.h"
24
25
#endif
26
27
#if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
28
29
0
#define is_dgram_pair(b) (b->pair != NULL)
30
31
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
32
33
/* ===========================================================================
34
 * Byte-wise ring buffer which supports pushing and popping blocks of multiple
35
 * bytes at a time.
36
 */
37
struct ring_buf {
38
    unsigned char *start; /* start of buffer */
39
    size_t len; /* size of buffer allocation in bytes */
40
    size_t count; /* number of bytes currently pushed */
41
    /*
42
     * These index into start. Where idx[0] == idx[1], the buffer is full
43
     * (if count is nonzero) and empty otherwise.
44
     */
45
    size_t idx[2]; /* 0: head, 1: tail */
46
};
47
48
static int ring_buf_init(struct ring_buf *r, size_t nbytes)
49
0
{
50
0
    r->start = OPENSSL_malloc(nbytes);
51
0
    if (r->start == NULL)
52
0
        return 0;
53
54
0
    r->len = nbytes;
55
0
    r->idx[0] = r->idx[1] = r->count = 0;
56
0
    return 1;
57
0
}
58
59
static void ring_buf_destroy(struct ring_buf *r)
60
0
{
61
0
    OPENSSL_free(r->start);
62
0
    r->start = NULL;
63
0
    r->len = 0;
64
0
    r->count = 0;
65
0
}
66
67
/*
68
 * Get a pointer to the next place to write data to be pushed to the ring buffer
69
 * (idx=0), or the next data to be popped from the ring buffer (idx=1). The
70
 * pointer is written to *buf and the maximum number of bytes which can be
71
 * read/written are written to *len. After writing data to the buffer, call
72
 * ring_buf_push/pop() with the number of bytes actually read/written, which
73
 * must not exceed the returned length.
74
 */
75
static void ring_buf_head_tail(struct ring_buf *r, int idx, uint8_t **buf, size_t *len)
76
0
{
77
0
    size_t max_len = r->len - r->idx[idx];
78
79
0
    if (idx == 0 && max_len > r->len - r->count)
80
0
        max_len = r->len - r->count;
81
0
    if (idx == 1 && max_len > r->count)
82
0
        max_len = r->count;
83
84
0
    *buf = (uint8_t *)r->start + r->idx[idx];
85
0
    *len = max_len;
86
0
}
87
88
0
#define ring_buf_head(r, buf, len) ring_buf_head_tail((r), 0, (buf), (len))
89
0
#define ring_buf_tail(r, buf, len) ring_buf_head_tail((r), 1, (buf), (len))
90
91
/*
92
 * Commit bytes to the ring buffer previously filled after a call to
93
 * ring_buf_head().
94
 */
95
static void ring_buf_push_pop(struct ring_buf *r, int idx, size_t num_bytes)
96
0
{
97
0
    size_t new_idx;
98
99
    /* A single push/pop op cannot wrap around, though it can reach the end.
100
     * If the caller adheres to the convention of using the length returned
101
     * by ring_buf_head/tail(), this cannot happen.
102
     */
103
0
    if (!ossl_assert(num_bytes <= r->len - r->idx[idx]))
104
0
        return;
105
106
    /*
107
     * Must not overfill the buffer, or pop more than is in the buffer either.
108
     */
109
0
    if (!ossl_assert(idx != 0 ? num_bytes <= r->count
110
0
                              : num_bytes + r->count <= r->len))
111
0
        return;
112
113
    /* Update the index. */
114
0
    new_idx = r->idx[idx] + num_bytes;
115
0
    if (new_idx == r->len)
116
0
        new_idx = 0;
117
118
0
    r->idx[idx] = new_idx;
119
0
    if (idx != 0)
120
0
        r->count -= num_bytes;
121
0
    else
122
0
        r->count += num_bytes;
123
0
}
124
125
0
#define ring_buf_push(r, num_bytes) ring_buf_push_pop((r), 0, (num_bytes))
126
0
#define ring_buf_pop(r, num_bytes) ring_buf_push_pop((r), 1, (num_bytes))
127
128
static void ring_buf_clear(struct ring_buf *r)
129
0
{
130
0
    r->idx[0] = r->idx[1] = r->count = 0;
131
0
}
132
133
static int ring_buf_resize(struct ring_buf *r, size_t nbytes)
134
0
{
135
0
    unsigned char *new_start;
136
137
0
    if (r->start == NULL)
138
0
        return ring_buf_init(r, nbytes);
139
140
0
    if (nbytes == r->len)
141
0
        return 1;
142
143
0
    if (r->count > 0 && nbytes < r->len)
144
        /* fail shrinking the ring buffer when there is any data in it */
145
0
        return 0;
146
147
0
    new_start = OPENSSL_realloc(r->start, nbytes);
148
0
    if (new_start == NULL)
149
0
        return 0;
150
151
    /* Moving tail if it is after (or equal to) head */
152
0
    if (r->count > 0) {
153
0
        if (r->idx[0] <= r->idx[1]) {
154
0
            size_t offset = nbytes - r->len;
155
156
0
            memmove(new_start + r->idx[1] + offset, new_start + r->idx[1],
157
0
                r->len - r->idx[1]);
158
0
            r->idx[1] += offset;
159
0
        }
160
0
    } else {
161
        /* just reset the head/tail because it might be pointing outside */
162
0
        r->idx[0] = r->idx[1] = 0;
163
0
    }
164
165
0
    r->start = new_start;
166
0
    r->len = nbytes;
167
168
0
    return 1;
169
0
}
170
171
/* ===========================================================================
172
 * BIO_s_dgram_pair is documented in BIO_s_dgram_pair(3).
173
 *
174
 * INTERNAL DATA STRUCTURE
175
 *
176
 * This is managed internally by using a bytewise ring buffer which supports
177
 * pushing and popping spans of multiple bytes at once. The ring buffer stores
178
 * internal packets which look like this:
179
 *
180
 *   struct dgram_hdr hdr;
181
 *   uint8_t data[];
182
 *
183
 * The header contains the length of the data and metadata such as
184
 * source/destination addresses.
185
 *
186
 * The datagram pair BIO is designed to support both traditional
187
 * BIO_read/BIO_write (likely to be used by applications) as well as
188
 * BIO_recvmmsg/BIO_sendmmsg.
189
 */
190
struct bio_dgram_pair_st;
191
static int dgram_pair_write(BIO *bio, const char *buf, int sz_);
192
static int dgram_pair_read(BIO *bio, char *buf, int sz_);
193
static int dgram_mem_read(BIO *bio, char *buf, int sz_);
194
static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr);
195
static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr);
196
static int dgram_pair_init(BIO *bio);
197
static int dgram_mem_init(BIO *bio);
198
static int dgram_pair_free(BIO *bio);
199
static int dgram_pair_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
200
    size_t num_msg, uint64_t flags,
201
    size_t *num_processed);
202
static int dgram_pair_recvmmsg(BIO *b, BIO_MSG *msg, size_t stride,
203
    size_t num_msg, uint64_t flags,
204
    size_t *num_processed);
205
206
static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1);
207
static size_t dgram_pair_read_inner(struct ring_buf *rbufptr, uint8_t *buf,
208
    size_t sz);
209
210
0
#define BIO_MSG_N(array, n) (*(BIO_MSG *)((char *)(array) + (n) * stride))
211
212
static const BIO_METHOD dgram_pair_method = {
213
    BIO_TYPE_DGRAM_PAIR,
214
    "BIO dgram pair",
215
    bwrite_conv,
216
    dgram_pair_write,
217
    bread_conv,
218
    dgram_pair_read,
219
    NULL, /* dgram_pair_puts */
220
    NULL, /* dgram_pair_gets */
221
    dgram_pair_ctrl,
222
    dgram_pair_init,
223
    dgram_pair_free,
224
    NULL, /* dgram_pair_callback_ctrl */
225
    dgram_pair_sendmmsg,
226
    dgram_pair_recvmmsg,
227
};
228
229
static const BIO_METHOD dgram_mem_method = {
230
    BIO_TYPE_DGRAM_MEM,
231
    "BIO dgram mem",
232
    bwrite_conv,
233
    dgram_pair_write,
234
    bread_conv,
235
    dgram_mem_read,
236
    NULL, /* dgram_pair_puts */
237
    NULL, /* dgram_pair_gets */
238
    dgram_mem_ctrl,
239
    dgram_mem_init,
240
    dgram_pair_free,
241
    NULL, /* dgram_pair_callback_ctrl */
242
    dgram_pair_sendmmsg,
243
    dgram_pair_recvmmsg,
244
};
245
246
const BIO_METHOD *BIO_s_dgram_pair(void)
247
0
{
248
0
    return &dgram_pair_method;
249
0
}
250
251
const BIO_METHOD *BIO_s_dgram_mem(void)
252
0
{
253
0
    return &dgram_mem_method;
254
0
}
255
256
struct dgram_hdr {
257
    size_t len; /* payload length in bytes, not including this struct */
258
    BIO_ADDR src_addr, dst_addr; /* family == 0: not present */
259
};
260
261
struct rbuf_map_st {
262
    struct bio_dgram_pair_st *self;
263
    struct ring_buf rbuf;
264
    uint32_t cap;
265
    CRYPTO_RWLOCK *lock;
266
};
267
268
/**
269
 * \defgroup peer_state Peer pairing states
270
 *
271
 * State values describing whether a peer is still associated with its
272
 * counterpart. Stored in the peer's state field and compared for
273
 * equality; the values are not a bitmask and must not be OR'd
274
 * together.
275
 *
276
 * @{
277
 */
278
279
/** Peer is associated with a live counterpart. */
280
0
#define PEER_STATE_PAIRED 0
281
282
/** Peer's counterpart has gone away; the peer is unassociated. */
283
0
#define PEER_STATE_ORPHANED 1
284
/**
285
 * \brief Shared state for a datagram BIO pair.
286
 *
287
 * Refcounted so that both ends of a pair (and any BIO holding a
288
 * reference to the peer) can keep the structure alive independently.
289
 * The object is freed when the last reference is dropped, and the peer_state
290
 * is set to PEER_STATE_ORPHANED when either side leaves the pair, either
291
 * via BIO_free() or BIO_destroy_dgram_pair()
292
 *
293
 * This structure is pointed to by each half of a BIO_dgram pair via the pair pointer
294
 * It is only allocated and assigned when a pair is formed.
295
 */
296
struct bio_dgram_peer_st {
297
    /**
298
     * Reference count. Initialised with CRYPTO_NEW_REF() and
299
     * released with CRYPTO_FREE_REF() once it reaches zero.
300
     */
301
    CRYPTO_REF_COUNT ref_cnt;
302
303
    /**
304
     * Current pairing state; one of \c PEER_STATE_PAIRED or
305
     * \c PEER_STATE_ORPHANED (see \ref peer_state).
306
     */
307
    int peer_state;
308
309
    /*
310
     * Lock for peer_state atomic ops where needed
311
     */
312
    CRYPTO_RWLOCK *peerlock;
313
314
    /**
315
     * Ring buffer mappings for the two datagram directions,
316
     * indexed one per direction.
317
     */
318
    struct rbuf_map_st map[2];
319
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
320
#ifdef OPENSSL_THREADS
321
    CRYPTO_MUTEX *pcap_mutex;
322
#endif
323
    FILE *tcpdump_f;
324
    uint16_t ip_id;
325
#endif
326
};
327
328
struct bio_dgram_pair_st {
329
    /* Track out pairing state */
330
    struct bio_dgram_peer_st *pair;
331
    /* Writes are directed to our own ringbuf and reads to our peer. */
332
    struct ring_buf rbuf;
333
    /* Requested size of rbuf buffer in bytes once we initialize. */
334
    size_t req_buf_len;
335
    /* Largest possible datagram size */
336
    size_t mtu;
337
    /* Capability flags. */
338
    uint32_t cap;
339
    /* The local address to use (if set) */
340
    BIO_ADDR *local_addr;
341
    /*
342
     * This lock protects updates to our rbuf. Since writes are directed to our
343
     * own rbuf, this means we use this lock for writes and our peer's lock for
344
     * reads.
345
     */
346
    CRYPTO_RWLOCK *lock;
347
    unsigned int no_trunc : 1; /* Reads fail if they would truncate */
348
    unsigned int local_addr_enable : 1; /* Can use BIO_MSG->local? */
349
    unsigned int role : 1; /* Determines lock order */
350
    unsigned int grows_on_write : 1; /* Set for BIO_s_dgram_mem only */
351
};
352
353
/*
354
 * When operating as a pair, we use the shared structure to hold our ring buffers
355
 * and locks to ensure that they remain allocated until the last half of a pair
356
 * dissolves the pair.
357
 */
358
static struct rbuf_map_st *dgram_rbuf_map_get_self(struct bio_dgram_pair_st *self)
359
0
{
360
0
    if (self->pair->map[0].self == self)
361
0
        return &self->pair->map[0];
362
0
    return &self->pair->map[1];
363
0
}
364
365
static struct rbuf_map_st *dgram_rbuf_map_get_peer(struct bio_dgram_pair_st *self)
366
0
{
367
0
    if (self->pair->map[0].self == self)
368
0
        return &self->pair->map[1];
369
0
    return &self->pair->map[0];
370
0
}
371
372
static void dgram_bio_get_self_data(struct bio_dgram_pair_st *self, struct ring_buf **rbufptr,
373
    CRYPTO_RWLOCK **lock)
374
0
{
375
0
    struct rbuf_map_st *map;
376
0
    CRYPTO_RWLOCK *mylock;
377
0
    struct ring_buf *myrbuf;
378
379
0
    if (is_dgram_pair(self)) {
380
0
        map = dgram_rbuf_map_get_self(self);
381
0
        mylock = map->lock;
382
0
        myrbuf = &map->rbuf;
383
0
    } else {
384
0
        mylock = self->lock;
385
0
        myrbuf = &self->rbuf;
386
0
    }
387
0
    if (lock != NULL)
388
0
        *lock = mylock;
389
0
    if (rbufptr != NULL)
390
0
        *rbufptr = myrbuf;
391
0
}
392
393
static void dgram_bio_get_peer_data(struct bio_dgram_pair_st *self, struct ring_buf **rbufptr,
394
    CRYPTO_RWLOCK **lock, uint32_t *caps, struct bio_dgram_pair_st **peer)
395
0
{
396
0
    struct rbuf_map_st *map;
397
0
    CRYPTO_RWLOCK *mylock;
398
0
    struct ring_buf *myrbuf;
399
0
    struct bio_dgram_pair_st *mypeer;
400
0
    uint32_t mycaps;
401
402
0
    if (is_dgram_pair(self)) {
403
0
        map = dgram_rbuf_map_get_peer(self);
404
0
        mylock = map->lock;
405
0
        myrbuf = &map->rbuf;
406
0
        mypeer = map->self;
407
0
        mycaps = map->cap;
408
0
    } else {
409
0
        mylock = self->lock;
410
0
        myrbuf = &self->rbuf;
411
0
        mypeer = self;
412
0
        mycaps = self->cap;
413
0
    }
414
0
    if (lock != NULL)
415
0
        *lock = mylock;
416
0
    if (rbufptr != NULL)
417
0
        *rbufptr = myrbuf;
418
0
    if (peer != NULL)
419
0
        *peer = mypeer;
420
0
    if (caps != NULL)
421
0
        *caps = mycaps;
422
0
}
423
424
0
#define MIN_BUF_LEN (1024)
425
426
static int dgram_pair_init(BIO *bio)
427
0
{
428
0
    struct bio_dgram_pair_st *b = OPENSSL_zalloc(sizeof(*b));
429
430
0
    if (b == NULL)
431
0
        return 0;
432
433
0
    b->mtu = 1472; /* conservative default MTU */
434
    /* default buffer size */
435
0
    b->req_buf_len = 9 * (sizeof(struct dgram_hdr) + b->mtu);
436
437
0
    b->lock = CRYPTO_THREAD_lock_new();
438
0
    if (b->lock == NULL) {
439
0
        OPENSSL_free(b);
440
0
        return 0;
441
0
    }
442
443
0
    bio->ptr = b;
444
445
0
    return 1;
446
0
}
447
448
static int dgram_mem_init(BIO *bio)
449
0
{
450
0
    struct bio_dgram_pair_st *b;
451
452
0
    if (!dgram_pair_init(bio))
453
0
        return 0;
454
455
0
    b = bio->ptr;
456
457
0
    if (ring_buf_init(&b->rbuf, b->req_buf_len) == 0) {
458
0
        dgram_pair_free(bio);
459
0
        ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
460
0
        return 0;
461
0
    }
462
463
0
    b->grows_on_write = 1;
464
465
0
    bio->init = 1;
466
0
    return 1;
467
0
}
468
469
static int dgram_pair_free(BIO *bio)
470
0
{
471
0
    struct bio_dgram_pair_st *b;
472
473
0
    if (bio == NULL)
474
0
        return 0;
475
476
0
    b = bio->ptr;
477
0
    if (!ossl_assert(b != NULL))
478
0
        return 0;
479
480
    /* We are being freed. Disconnect any peer and destroy buffers. */
481
0
    dgram_pair_ctrl_destroy_bio_pair(bio);
482
483
0
    ring_buf_destroy(&b->rbuf);
484
0
    BIO_ADDR_free(b->local_addr);
485
0
    CRYPTO_THREAD_lock_free(b->lock);
486
0
    OPENSSL_free(b);
487
0
    return 1;
488
0
}
489
490
/* BIO_make_bio_pair (BIO_C_MAKE_BIO_PAIR) */
491
static int dgram_pair_ctrl_make_bio_pair(BIO *bio1, BIO *bio2)
492
0
{
493
0
    struct bio_dgram_pair_st *b1, *b2;
494
0
    struct bio_dgram_peer_st *pair;
495
496
    /* peer must be non-NULL. */
497
0
    if (bio1 == NULL || bio2 == NULL) {
498
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
499
0
        return 0;
500
0
    }
501
502
    /* Ensure the BIO we have been passed is actually a dgram pair BIO. */
503
0
    if (bio1->method != &dgram_pair_method || bio2->method != &dgram_pair_method) {
504
0
        ERR_raise_data(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT,
505
0
            "both BIOs must be BIO_dgram_pair");
506
0
        return 0;
507
0
    }
508
509
0
    b1 = bio1->ptr;
510
0
    b2 = bio2->ptr;
511
512
0
    if (!ossl_assert(b1 != NULL && b2 != NULL)) {
513
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
514
0
        return 0;
515
0
    }
516
517
    /*
518
     * This ctrl cannot be used to associate a BIO pair half which is already
519
     * associated.
520
     */
521
0
    if (b1->pair != NULL || b2->pair != NULL) {
522
0
        ERR_raise_data(ERR_LIB_BIO, BIO_R_IN_USE,
523
0
            "cannot associate a BIO_dgram_pair which is already in use");
524
0
        return 0;
525
0
    }
526
527
0
    if (!ossl_assert(b1->req_buf_len >= MIN_BUF_LEN
528
0
            && b2->req_buf_len >= MIN_BUF_LEN)) {
529
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
530
0
        return 0;
531
0
    }
532
533
    /*
534
     * Create a new pair structure, init it with appropriate
535
     * ring buffers and lock, and assign it to each half of the
536
     * pair.
537
     * Once this is done, each half of the pair uses the shared
538
     * ring buffers/lock available here instead of their own private copy
539
     */
540
0
    pair = OPENSSL_zalloc(sizeof(*pair));
541
0
    if (pair == NULL) {
542
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
543
0
        return 0;
544
0
    }
545
0
    if (!CRYPTO_NEW_REF(&pair->ref_cnt, 2)) {
546
0
        OPENSSL_free(pair);
547
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
548
0
        return 0;
549
0
    }
550
0
    pair->peerlock = CRYPTO_THREAD_lock_new();
551
0
    if (pair->peerlock == NULL) {
552
0
        CRYPTO_FREE_REF(&pair->ref_cnt);
553
0
        OPENSSL_free(pair->peerlock);
554
0
        OPENSSL_free(pair);
555
0
        return 0;
556
0
    }
557
558
0
    if (ring_buf_init(&pair->map[0].rbuf, b1->req_buf_len) == 0) {
559
0
        CRYPTO_FREE_REF(&pair->ref_cnt);
560
0
        CRYPTO_THREAD_lock_free(pair->peerlock);
561
0
        OPENSSL_free(pair);
562
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
563
0
        return 0;
564
0
    }
565
566
0
    if (ring_buf_init(&pair->map[1].rbuf, b2->req_buf_len) == 0) {
567
0
        CRYPTO_FREE_REF(&pair->ref_cnt);
568
0
        CRYPTO_THREAD_lock_free(pair->peerlock);
569
0
        OPENSSL_free(pair->map[0].rbuf.start);
570
0
        OPENSSL_free(pair);
571
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
572
0
        return 0;
573
0
    }
574
0
    ring_buf_destroy(&b1->rbuf);
575
0
    ring_buf_destroy(&b2->rbuf);
576
0
    pair->map[0].lock = CRYPTO_THREAD_lock_new();
577
0
    pair->map[1].lock = CRYPTO_THREAD_lock_new();
578
0
    if (pair->map[0].lock == NULL || pair->map[1].lock == NULL) {
579
0
        CRYPTO_THREAD_lock_free(pair->map[0].lock);
580
0
        CRYPTO_THREAD_lock_free(pair->map[1].lock);
581
0
        CRYPTO_THREAD_lock_free(pair->peerlock);
582
0
        CRYPTO_FREE_REF(&pair->ref_cnt);
583
0
        OPENSSL_free(pair->map[0].rbuf.start);
584
0
        OPENSSL_free(pair->map[1].rbuf.start);
585
0
        OPENSSL_free(pair);
586
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
587
0
        return 0;
588
0
    }
589
0
    pair->map[0].self = b1;
590
0
    pair->map[1].self = b2;
591
0
    pair->map[0].cap = b1->cap;
592
0
    pair->map[1].cap = b2->cap;
593
0
    TSAN_BENIGN(pair, "publishing pair");
594
0
    b1->pair = pair;
595
0
    b2->pair = pair;
596
0
    b1->role = 0;
597
0
    b2->role = 1;
598
0
    bio1->init = 1;
599
0
    bio2->init = 1;
600
601
0
    return 1;
602
0
}
603
604
/* ARGSUSED */
605
static int dgram_pair_ctrl_set_pcap_file(BIO *bio, void *ptr)
606
0
{
607
0
    int ret = 0;
608
609
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
610
#define LINKTYPE_IPV4 0x00e4 /* IANA type */
611
    struct bio_dgram_pair_st *bp;
612
    char *filename = (char *)ptr;
613
    static struct {
614
        uint32_t pcap_magic;
615
        uint16_t pcap_maj;
616
        uint16_t pcap_min;
617
        uint32_t pcap_r1;
618
        uint32_t pcap_r2;
619
        uint32_t pcap_snap;
620
        uint32_t pcap_ltype;
621
    } pcap_f_header = {
622
        .pcap_magic = 0xA1B2C3D4,
623
        .pcap_maj = 2, /* https://www.ietf.org/archive/id/draft-gharris-opsawg-pcap-01.html */
624
        .pcap_min = 4, /* ---"--- */
625
        .pcap_r1 = 0,
626
        .pcap_r2 = 0,
627
        .pcap_snap = 65536,
628
        .pcap_ltype = LINKTYPE_IPV4,
629
    };
630
631
    bp = (struct bio_dgram_pair_st *)bio->ptr;
632
    if (bp == NULL)
633
        return 0;
634
635
    if (bp->pair == NULL || bp->pair->peer_state == PEER_STATE_ORPHANED)
636
        return 0;
637
638
    if (filename != NULL) {
639
        if (bp->pair->tcpdump_f == NULL) {
640
#ifdef OPENSSL_THREADS
641
            bp->pair->pcap_mutex = ossl_crypto_mutex_new();
642
            if (bp->pair->pcap_mutex != NULL)
643
#endif
644
            {
645
                bp->pair->tcpdump_f = fopen(filename, "wb");
646
                if (bp->pair->tcpdump_f != NULL) {
647
                    ret = fwrite(&pcap_f_header, sizeof(pcap_f_header), 1,
648
                        bp->pair->tcpdump_f);
649
                }
650
#ifdef OPENSSL_THREADS
651
                else
652
                    ossl_crypto_mutex_free(&bp->pair->pcap_mutex);
653
#endif
654
            }
655
        }
656
    }
657
#endif
658
659
0
    return ret;
660
0
}
661
662
/* BIO_destroy_bio_pair (BIO_C_DESTROY_BIO_PAIR) */
663
static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1)
664
0
{
665
0
    struct bio_dgram_pair_st *b1 = bio1->ptr;
666
0
    int ref;
667
0
    int newval = PEER_STATE_ORPHANED;
668
669
    /* Early return if we don't have a peer. */
670
0
    if (b1->pair == NULL)
671
0
        return 1;
672
673
0
    ring_buf_destroy(&b1->rbuf);
674
0
    bio1->init = 0;
675
676
0
    if (ring_buf_init(&b1->rbuf, b1->req_buf_len) == 0)
677
0
        return 0;
678
679
    /*
680
     * Since one half of the pair is going away, we are now
681
     * orphaned
682
     */
683
0
    if (!CRYPTO_atomic_store_int(&b1->pair->peer_state, newval, b1->pair->peerlock))
684
0
        return 0;
685
686
0
    if (!CRYPTO_DOWN_REF(&b1->pair->ref_cnt, &ref))
687
0
        return 0;
688
0
    if (ref == 0) {
689
        /*
690
         * The last half of the pair is leaving, clean up the
691
         * shared data
692
         */
693
0
        CRYPTO_FREE_REF(&b1->pair->ref_cnt);
694
0
        CRYPTO_THREAD_lock_free(b1->pair->map[0].lock);
695
0
        CRYPTO_THREAD_lock_free(b1->pair->map[1].lock);
696
0
        CRYPTO_THREAD_lock_free(b1->pair->peerlock);
697
0
        ring_buf_destroy(&b1->pair->map[0].rbuf);
698
0
        ring_buf_destroy(&b1->pair->map[1].rbuf);
699
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
700
        if (b1->pair->tcpdump_f != NULL) {
701
            fclose(b1->pair->tcpdump_f);
702
#ifdef OPENSSL_THREADS
703
            ossl_crypto_mutex_free(&b1->pair->pcap_mutex);
704
#endif
705
            b1->pair->tcpdump_f = NULL;
706
        }
707
#endif
708
0
        OPENSSL_free(b1->pair);
709
0
    }
710
    /*
711
     * Make sure the leaving pair no longer references the shared peer data,
712
     * since it is no longer part of the pair.
713
     */
714
0
    TSAN_BENIGN(b1->pair, "b1 no longer accesses b1->pair");
715
0
    b1->pair = NULL;
716
0
    return 1;
717
0
}
718
719
/* BIO_eof (BIO_CTRL_EOF) */
720
static int dgram_pair_ctrl_eof(BIO *bio)
721
0
{
722
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb = NULL;
723
0
    int peer_state;
724
725
0
    if (!ossl_assert(b != NULL))
726
0
        return -1;
727
728
    /* If we aren't initialized, we can never read anything */
729
0
    if (!bio->init)
730
0
        return 1;
731
0
    if (!is_dgram_pair(b))
732
0
        return 0;
733
734
    /*
735
     * orphaned pairs always return EOF
736
     */
737
0
    if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
738
0
        return -1;
739
740
0
    if (peer_state == PEER_STATE_ORPHANED)
741
0
        return 1;
742
743
0
    dgram_bio_get_peer_data(b, NULL, NULL, NULL, &peerb);
744
0
    if (!ossl_assert(peerb != NULL))
745
0
        return -1;
746
747
    /*
748
     * Since we are emulating datagram semantics, never indicate EOF so long as
749
     * we have a peer.
750
     */
751
0
    return 0;
752
0
}
753
754
/* BIO_set_write_buf_size (BIO_C_SET_WRITE_BUF_SIZE) */
755
static int dgram_pair_ctrl_set_write_buf_size(BIO *bio, size_t len)
756
0
{
757
0
    struct bio_dgram_pair_st *b = bio->ptr;
758
759
    /* Changing buffer sizes is not permitted while a peer is connected. */
760
0
    if (b->pair != NULL) {
761
0
        ERR_raise(ERR_LIB_BIO, BIO_R_IN_USE);
762
0
        return 0;
763
0
    }
764
765
    /* Enforce minimum size. */
766
0
    if (len < MIN_BUF_LEN)
767
0
        len = MIN_BUF_LEN;
768
769
0
    if (b->rbuf.start != NULL) {
770
0
        if (!ring_buf_resize(&b->rbuf, len))
771
0
            return 0;
772
0
    }
773
774
0
    b->req_buf_len = len;
775
0
    b->grows_on_write = 0;
776
0
    return 1;
777
0
}
778
779
/* BIO_reset (BIO_CTRL_RESET) */
780
static int dgram_pair_ctrl_reset(BIO *bio)
781
0
{
782
0
    struct bio_dgram_pair_st *b = bio->ptr;
783
784
0
    ring_buf_clear(&b->rbuf);
785
0
    return 1;
786
0
}
787
788
/* BIO_pending (BIO_CTRL_PENDING) (Threadsafe) */
789
static size_t dgram_pair_ctrl_pending(BIO *bio)
790
0
{
791
0
    size_t saved_idx, saved_count;
792
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
793
0
    struct dgram_hdr hdr;
794
0
    size_t l;
795
0
    struct ring_buf *rbufptr;
796
0
    CRYPTO_RWLOCK *lock;
797
0
    int peer_state;
798
799
    /* Safe to check; init may not change during this call */
800
0
    if (!bio->init)
801
0
        return 0;
802
0
    if (is_dgram_pair(b)) {
803
0
        if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
804
0
            return 0;
805
806
0
        if (peer_state == PEER_STATE_ORPHANED)
807
0
            return 0;
808
0
    }
809
810
0
    dgram_bio_get_peer_data(b, &rbufptr, &lock, NULL, &readb);
811
812
0
    if (CRYPTO_THREAD_write_lock(lock) == 0)
813
0
        return 0;
814
815
0
    saved_idx = rbufptr->idx[1];
816
0
    saved_count = rbufptr->count;
817
818
0
    l = dgram_pair_read_inner(rbufptr, (uint8_t *)&hdr, sizeof(hdr));
819
820
0
    rbufptr->idx[1] = saved_idx;
821
0
    rbufptr->count = saved_count;
822
823
0
    CRYPTO_THREAD_unlock(lock);
824
825
0
    if (!ossl_assert(l == 0 || l == sizeof(hdr)))
826
0
        return 0;
827
828
0
    return l > 0 ? hdr.len : 0;
829
0
}
830
831
/* BIO_get_write_guarantee (BIO_C_GET_WRITE_GUARANTEE) (Threadsafe) */
832
static size_t dgram_pair_ctrl_get_write_guarantee(BIO *bio)
833
0
{
834
0
    size_t l;
835
0
    struct bio_dgram_pair_st *b = bio->ptr;
836
0
    struct ring_buf *rbufptr;
837
0
    CRYPTO_RWLOCK *lock;
838
839
0
    dgram_bio_get_peer_data(b, &rbufptr, &lock, NULL, NULL);
840
841
0
    if (CRYPTO_THREAD_read_lock(lock) == 0)
842
0
        return 0;
843
844
0
    l = rbufptr->len - rbufptr->count;
845
0
    if (l >= sizeof(struct dgram_hdr))
846
0
        l -= sizeof(struct dgram_hdr);
847
848
    /*
849
     * If the amount of buffer space would not be enough to accommodate the
850
     * worst-case size of a datagram, report no space available.
851
     */
852
0
    if (l < b->mtu)
853
0
        l = 0;
854
855
0
    CRYPTO_THREAD_unlock(lock);
856
0
    return l;
857
0
}
858
859
/* BIO_dgram_get_local_addr_cap (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP) */
860
static int dgram_pair_ctrl_get_local_addr_cap(BIO *bio)
861
0
{
862
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
863
0
    int peer_state;
864
0
    uint32_t caps;
865
866
0
    if (!bio->init)
867
0
        return 0;
868
869
0
    if (is_dgram_pair(b)) {
870
0
        if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
871
0
            return 0;
872
873
0
        if (peer_state == PEER_STATE_ORPHANED)
874
0
            return 0;
875
0
    }
876
877
0
    dgram_bio_get_peer_data(b, NULL, NULL, &caps, &readb);
878
879
0
    return (~caps & (BIO_DGRAM_CAP_HANDLES_SRC_ADDR | BIO_DGRAM_CAP_PROVIDES_DST_ADDR)) == 0;
880
0
}
881
882
/* BIO_dgram_get_effective_caps (BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS) */
883
static int dgram_pair_ctrl_get_effective_caps(BIO *bio)
884
0
{
885
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
886
0
    int peer_state;
887
0
    uint32_t caps;
888
889
0
    if (b->pair == NULL)
890
0
        return 0;
891
892
0
    if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
893
0
        return 0;
894
895
0
    if (peer_state == PEER_STATE_ORPHANED)
896
0
        return 0;
897
898
0
    dgram_bio_get_peer_data(b, NULL, NULL, &caps, &peerb);
899
900
0
    return caps;
901
0
}
902
903
/* BIO_dgram_get_caps (BIO_CTRL_DGRAM_GET_CAPS) */
904
static uint32_t dgram_pair_ctrl_get_caps(BIO *bio)
905
0
{
906
0
    struct bio_dgram_pair_st *b = bio->ptr;
907
908
0
    return b->cap;
909
0
}
910
911
/* BIO_dgram_set_caps (BIO_CTRL_DGRAM_SET_CAPS) */
912
static int dgram_pair_ctrl_set_caps(BIO *bio, uint32_t caps)
913
0
{
914
0
    struct bio_dgram_pair_st *b = bio->ptr;
915
0
    struct rbuf_map_st *map;
916
917
0
    b->cap = caps;
918
919
0
    if (is_dgram_pair(b)) {
920
0
        map = dgram_rbuf_map_get_self(b);
921
0
        map->cap = caps;
922
0
    }
923
0
    return 1;
924
0
}
925
926
/* BIO_dgram_get_local_addr_enable (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE) */
927
static int dgram_pair_ctrl_get_local_addr_enable(BIO *bio)
928
0
{
929
0
    struct bio_dgram_pair_st *b = bio->ptr;
930
931
0
    return b->local_addr_enable;
932
0
}
933
934
/* BIO_dgram_set_local_addr_enable (BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE) */
935
static int dgram_pair_ctrl_set_local_addr_enable(BIO *bio, int enable)
936
0
{
937
0
    struct bio_dgram_pair_st *b = bio->ptr;
938
939
0
    if (dgram_pair_ctrl_get_local_addr_cap(bio) == 0)
940
0
        return 0;
941
942
0
    b->local_addr_enable = (enable != 0 ? 1 : 0);
943
0
    return 1;
944
0
}
945
946
/* BIO_dgram_get_mtu (BIO_CTRL_DGRAM_GET_MTU) */
947
static int dgram_pair_ctrl_get_mtu(BIO *bio)
948
0
{
949
0
    struct bio_dgram_pair_st *b = bio->ptr;
950
951
0
    return (int)b->mtu;
952
0
}
953
954
/* BIO_dgram_set_mtu (BIO_CTRL_DGRAM_SET_MTU) */
955
static int dgram_pair_ctrl_set_mtu(BIO *bio, size_t mtu)
956
0
{
957
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
958
0
    int peer_state;
959
960
0
    b->mtu = mtu;
961
962
0
    if (is_dgram_pair(b)) {
963
0
        if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
964
0
            return 0;
965
966
0
        if (peer_state == PEER_STATE_PAIRED) {
967
0
            dgram_bio_get_peer_data(b, NULL, NULL, NULL, &peerb);
968
0
            peerb->mtu = mtu;
969
0
        }
970
0
    }
971
972
0
    return 1;
973
0
}
974
975
/* BIO_dgram_set0_local_addr (BIO_CTRL_DGRAM_SET0_LOCAL_ADDR) */
976
static int dgram_pair_ctrl_set0_local_addr(BIO *bio, BIO_ADDR *addr)
977
0
{
978
0
    struct bio_dgram_pair_st *b = bio->ptr;
979
980
0
    BIO_ADDR_free(b->local_addr);
981
0
    b->local_addr = addr;
982
0
    return 1;
983
0
}
984
985
/* Partially threadsafe (some commands) */
986
static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
987
0
{
988
0
    long ret = 1;
989
0
    struct bio_dgram_pair_st *b = bio->ptr;
990
991
0
    if (!ossl_assert(b != NULL))
992
0
        return 0;
993
994
0
    switch (cmd) {
995
    /*
996
     * BIO_set_write_buf_size: Set the size of the ring buffer used for storing
997
     * datagrams. No more writes can be performed once the buffer is filled up,
998
     * until reads are performed. This cannot be used after a peer is connected.
999
     */
1000
0
    case BIO_C_SET_WRITE_BUF_SIZE: /* Non-threadsafe */
1001
0
        ret = (long)dgram_pair_ctrl_set_write_buf_size(bio, (size_t)num);
1002
0
        break;
1003
1004
    /*
1005
     * BIO_get_write_buf_size: Get ring buffer size.
1006
     */
1007
0
    case BIO_C_GET_WRITE_BUF_SIZE: /* Non-threadsafe */
1008
0
        ret = (long)b->req_buf_len;
1009
0
        break;
1010
1011
    /*
1012
     * BIO_reset: Clear all data which was written to this side of the pair.
1013
     */
1014
0
    case BIO_CTRL_RESET: /* Non-threadsafe */
1015
0
        dgram_pair_ctrl_reset(bio);
1016
0
        break;
1017
1018
    /*
1019
     * BIO_get_write_guarantee: Any BIO_write providing a buffer less than or
1020
     * equal to this value is guaranteed to succeed.
1021
     */
1022
0
    case BIO_C_GET_WRITE_GUARANTEE: /* Threadsafe */
1023
0
        ret = (long)dgram_pair_ctrl_get_write_guarantee(bio);
1024
0
        break;
1025
1026
    /* BIO_pending: Bytes available to read. */
1027
0
    case BIO_CTRL_PENDING: /* Threadsafe */
1028
0
        ret = (long)dgram_pair_ctrl_pending(bio);
1029
0
        break;
1030
1031
    /* BIO_flush: No-op. */
1032
0
    case BIO_CTRL_FLUSH: /* Threadsafe */
1033
0
        break;
1034
1035
    /* BIO_dgram_get_no_trunc */
1036
0
    case BIO_CTRL_DGRAM_GET_NO_TRUNC: /* Non-threadsafe */
1037
0
        ret = (long)b->no_trunc;
1038
0
        break;
1039
1040
    /* BIO_dgram_set_no_trunc */
1041
0
    case BIO_CTRL_DGRAM_SET_NO_TRUNC: /* Non-threadsafe */
1042
0
        b->no_trunc = (num > 0);
1043
0
        break;
1044
1045
    /* BIO_dgram_get_local_addr_enable */
1046
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
1047
0
        *(int *)ptr = (int)dgram_pair_ctrl_get_local_addr_enable(bio);
1048
0
        break;
1049
1050
    /* BIO_dgram_set_local_addr_enable */
1051
0
    case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
1052
0
        ret = (long)dgram_pair_ctrl_set_local_addr_enable(bio, num);
1053
0
        break;
1054
1055
    /* BIO_dgram_get_local_addr_cap: Can local addresses be supported? */
1056
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP: /* Non-threadsafe */
1057
0
        ret = (long)dgram_pair_ctrl_get_local_addr_cap(bio);
1058
0
        break;
1059
1060
    /* BIO_dgram_get_effective_caps */
1061
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
1062
    /* BIO_dgram_get_caps */
1063
0
    case BIO_CTRL_DGRAM_GET_CAPS: /* Non-threadsafe */
1064
0
        ret = (long)dgram_pair_ctrl_get_caps(bio);
1065
0
        break;
1066
1067
    /* BIO_dgram_set_caps */
1068
0
    case BIO_CTRL_DGRAM_SET_CAPS: /* Non-threadsafe */
1069
0
        ret = (long)dgram_pair_ctrl_set_caps(bio, (uint32_t)num);
1070
0
        break;
1071
1072
    /* BIO_dgram_get_mtu */
1073
0
    case BIO_CTRL_DGRAM_GET_MTU: /* Non-threadsafe */
1074
0
        ret = (long)dgram_pair_ctrl_get_mtu(bio);
1075
0
        break;
1076
1077
    /* BIO_dgram_set_mtu */
1078
0
    case BIO_CTRL_DGRAM_SET_MTU: /* Non-threadsafe */
1079
0
        ret = (long)dgram_pair_ctrl_set_mtu(bio, (uint32_t)num);
1080
0
        break;
1081
1082
0
    case BIO_CTRL_DGRAM_SET0_LOCAL_ADDR:
1083
0
        ret = (long)dgram_pair_ctrl_set0_local_addr(bio, (BIO_ADDR *)ptr);
1084
0
        break;
1085
1086
    /*
1087
     * BIO_eof: Returns whether this half of the BIO pair is empty of data to
1088
     * read.
1089
     */
1090
0
    case BIO_CTRL_EOF: /* Non-threadsafe */
1091
0
        ret = (long)dgram_pair_ctrl_eof(bio);
1092
0
        break;
1093
1094
0
    default:
1095
0
        ret = 0;
1096
0
        break;
1097
0
    }
1098
1099
0
    return ret;
1100
0
}
1101
1102
static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr)
1103
0
{
1104
0
    long ret = 1;
1105
1106
0
    switch (cmd) {
1107
    /*
1108
     * BIO_make_bio_pair: this is usually used by BIO_new_dgram_pair, though it
1109
     * may be used manually after manually creating each half of a BIO pair
1110
     * using BIO_new. This only needs to be called on one of the BIOs.
1111
     */
1112
0
    case BIO_C_MAKE_BIO_PAIR: /* Non-threadsafe */
1113
0
        ret = (long)dgram_pair_ctrl_make_bio_pair(bio, (BIO *)ptr);
1114
0
        break;
1115
1116
    /*
1117
     * BIO_destroy_bio_pair: Manually disconnect two halves of a BIO pair so
1118
     * that they are no longer peers.
1119
     */
1120
0
    case BIO_C_DESTROY_BIO_PAIR: /* Non-threadsafe */
1121
0
        dgram_pair_ctrl_destroy_bio_pair(bio);
1122
0
        break;
1123
1124
    /* BIO_dgram_get_effective_caps */
1125
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
1126
0
        ret = (long)dgram_pair_ctrl_get_effective_caps(bio);
1127
0
        break;
1128
1129
0
    case BIO_CTRL_DGRAM_PAIR_SET_PCAP_FILE:
1130
0
        ret = dgram_pair_ctrl_set_pcap_file(bio, ptr);
1131
0
        break;
1132
1133
0
    default:
1134
0
        ret = dgram_mem_ctrl(bio, cmd, num, ptr);
1135
0
        break;
1136
0
    }
1137
1138
0
    return ret;
1139
0
}
1140
1141
int BIO_new_bio_dgram_pair(BIO **pbio1, size_t writebuf1,
1142
    BIO **pbio2, size_t writebuf2)
1143
0
{
1144
0
    int ret = 0;
1145
0
    long r;
1146
0
    BIO *bio1 = NULL, *bio2 = NULL;
1147
1148
0
    if (writebuf1 > LONG_MAX || writebuf2 > LONG_MAX)
1149
0
        goto err;
1150
1151
0
    bio1 = BIO_new(BIO_s_dgram_pair());
1152
0
    if (bio1 == NULL)
1153
0
        goto err;
1154
1155
0
    bio2 = BIO_new(BIO_s_dgram_pair());
1156
0
    if (bio2 == NULL)
1157
0
        goto err;
1158
1159
0
    if (writebuf1 > 0) {
1160
0
        r = BIO_set_write_buf_size(bio1, (long)writebuf1);
1161
0
        if (r == 0)
1162
0
            goto err;
1163
0
    }
1164
1165
0
    if (writebuf2 > 0) {
1166
0
        r = BIO_set_write_buf_size(bio2, (long)writebuf2);
1167
0
        if (r == 0)
1168
0
            goto err;
1169
0
    }
1170
1171
0
    r = BIO_make_bio_pair(bio1, bio2);
1172
0
    if (r == 0)
1173
0
        goto err;
1174
1175
0
    ret = 1;
1176
0
err:
1177
0
    if (ret == 0) {
1178
0
        BIO_free(bio1);
1179
0
        bio1 = NULL;
1180
0
        BIO_free(bio2);
1181
0
        bio2 = NULL;
1182
0
    }
1183
1184
0
    *pbio1 = bio1;
1185
0
    *pbio2 = bio2;
1186
0
    return ret;
1187
0
}
1188
1189
/* Must hold peer write lock */
1190
static size_t dgram_pair_read_inner(struct ring_buf *rbufptr, uint8_t *buf, size_t sz)
1191
0
{
1192
0
    size_t total_read = 0;
1193
1194
    /*
1195
     * We repeat pops from the ring buffer for as long as we have more
1196
     * application *buffer to fill until we fail. We may not be able to pop
1197
     * enough data to fill the buffer in one operation if the ring buffer wraps
1198
     * around, but there may still be more data available.
1199
     */
1200
0
    while (sz > 0) {
1201
0
        uint8_t *src_buf = NULL;
1202
0
        size_t src_len = 0;
1203
1204
        /*
1205
         * There are two BIO instances, each with a ringbuf. We read from the
1206
         * peer ringbuf and write to our own ringbuf.
1207
         */
1208
0
        ring_buf_tail(rbufptr, &src_buf, &src_len);
1209
0
        if (src_len == 0)
1210
0
            break;
1211
1212
0
        if (src_len > sz)
1213
0
            src_len = sz;
1214
1215
0
        if (buf != NULL)
1216
0
            memcpy(buf, src_buf, src_len);
1217
1218
0
        ring_buf_pop(rbufptr, src_len);
1219
1220
0
        if (buf != NULL)
1221
0
            buf += src_len;
1222
0
        total_read += src_len;
1223
0
        sz -= src_len;
1224
0
    }
1225
1226
0
    return total_read;
1227
0
}
1228
1229
/*
1230
 * Must hold peer write lock. Returns number of bytes processed or negated BIO
1231
 * response code.
1232
 */
1233
static ossl_ssize_t dgram_pair_read_actual(BIO *bio, char *buf, size_t sz,
1234
    BIO_ADDR *local, BIO_ADDR *peer,
1235
    int is_multi)
1236
0
{
1237
0
    size_t l, trunc = 0, saved_idx, saved_count;
1238
0
    struct bio_dgram_pair_st *b = bio->ptr;
1239
0
    struct dgram_hdr hdr;
1240
0
    struct ring_buf *rbufptr;
1241
1242
0
    if (!is_multi)
1243
0
        BIO_clear_retry_flags(bio);
1244
1245
0
    if (!bio->init)
1246
0
        return -BIO_R_UNINITIALIZED;
1247
1248
0
    if (!ossl_assert(b != NULL))
1249
0
        return -BIO_R_TRANSFER_ERROR;
1250
1251
0
    dgram_bio_get_peer_data(b, &rbufptr, NULL, NULL, NULL);
1252
1253
0
    if (!ossl_assert(rbufptr->start != NULL))
1254
0
        return -BIO_R_TRANSFER_ERROR;
1255
1256
0
    if (sz > 0 && buf == NULL)
1257
0
        return -BIO_R_INVALID_ARGUMENT;
1258
1259
    /* If the caller wants to know the local address, it must be enabled */
1260
0
    if (local != NULL && b->local_addr_enable == 0)
1261
0
        return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
1262
1263
    /* Read the header. */
1264
0
    saved_idx = rbufptr->idx[1];
1265
0
    saved_count = rbufptr->count;
1266
0
    l = dgram_pair_read_inner(rbufptr, (uint8_t *)&hdr, sizeof(hdr));
1267
0
    if (l == 0) {
1268
        /* Buffer was empty. */
1269
0
        if (!is_multi)
1270
0
            BIO_set_retry_read(bio);
1271
0
        return -BIO_R_NON_FATAL;
1272
0
    }
1273
1274
0
    if (!ossl_assert(l == sizeof(hdr)))
1275
        /*
1276
         * This should not be possible as headers (and their following payloads)
1277
         * should always be written atomically.
1278
         */
1279
0
        return -BIO_R_BROKEN_PIPE;
1280
1281
0
    if (sz > hdr.len) {
1282
0
        sz = hdr.len;
1283
0
    } else if (sz < hdr.len) {
1284
        /* Truncation is occurring. */
1285
0
        trunc = hdr.len - sz;
1286
0
        if (b->no_trunc) {
1287
            /* Restore original state. */
1288
0
            rbufptr->idx[1] = saved_idx;
1289
0
            rbufptr->count = saved_count;
1290
0
            return -BIO_R_NON_FATAL;
1291
0
        }
1292
0
    }
1293
1294
0
    l = dgram_pair_read_inner(rbufptr, (uint8_t *)buf, sz);
1295
0
    if (!ossl_assert(l == sz))
1296
        /* We were somehow not able to read the entire datagram. */
1297
0
        return -BIO_R_TRANSFER_ERROR;
1298
1299
    /*
1300
     * If the datagram was truncated due to an inadequate buffer, discard the
1301
     * remainder.
1302
     */
1303
0
    if (trunc > 0 && !ossl_assert(dgram_pair_read_inner(rbufptr, NULL, trunc) == trunc))
1304
        /* We were somehow not able to read/skip the entire datagram. */
1305
0
        return -BIO_R_TRANSFER_ERROR;
1306
1307
0
    if (local != NULL)
1308
0
        *local = hdr.dst_addr;
1309
0
    if (peer != NULL)
1310
0
        *peer = hdr.src_addr;
1311
1312
0
    return (ossl_ssize_t)l;
1313
0
}
1314
1315
/* Threadsafe */
1316
static int dgram_pair_lock_both_write(struct bio_dgram_pair_st *a,
1317
    struct bio_dgram_pair_st *b)
1318
0
{
1319
0
    struct bio_dgram_pair_st *x, *y;
1320
1321
0
    if (is_dgram_pair(b)) {
1322
0
        if (CRYPTO_THREAD_write_lock(b->pair->map[0].lock) == 0)
1323
0
            return 0;
1324
0
        if (CRYPTO_THREAD_write_lock(b->pair->map[1].lock) == 0) {
1325
0
            CRYPTO_THREAD_unlock(b->pair->map[0].lock);
1326
0
            return 0;
1327
0
        }
1328
0
    } else {
1329
0
        x = (a->role == 1) ? a : b;
1330
0
        y = (a->role == 1) ? b : a;
1331
1332
0
        if (!ossl_assert(a->role != b->role))
1333
0
            return 0;
1334
1335
0
        if (!ossl_assert(a != b && x != y))
1336
0
            return 0;
1337
1338
0
        if (CRYPTO_THREAD_write_lock(x->lock) == 0)
1339
0
            return 0;
1340
1341
0
        if (CRYPTO_THREAD_write_lock(y->lock) == 0) {
1342
0
            CRYPTO_THREAD_unlock(x->lock);
1343
0
            return 0;
1344
0
        }
1345
0
    }
1346
1347
0
    return 1;
1348
0
}
1349
1350
static void dgram_pair_unlock_both(struct bio_dgram_pair_st *a,
1351
    struct bio_dgram_pair_st *b)
1352
0
{
1353
0
    if (is_dgram_pair(b)) {
1354
0
        CRYPTO_THREAD_unlock(b->pair->map[0].lock);
1355
0
        CRYPTO_THREAD_unlock(b->pair->map[1].lock);
1356
0
    } else {
1357
0
        CRYPTO_THREAD_unlock(a->lock);
1358
0
        CRYPTO_THREAD_unlock(b->lock);
1359
0
    }
1360
0
}
1361
1362
/* Threadsafe */
1363
static int dgram_pair_read(BIO *bio, char *buf, int sz_)
1364
0
{
1365
0
    int ret;
1366
0
    ossl_ssize_t l;
1367
0
    struct bio_dgram_pair_st *b = bio->ptr, *peerb;
1368
0
    int peer_state;
1369
1370
0
    if (sz_ < 0) {
1371
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1372
0
        return -1;
1373
0
    }
1374
1375
0
    if (b->pair == NULL) {
1376
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
1377
0
        return -1;
1378
0
    }
1379
1380
0
    if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
1381
0
        return -1;
1382
1383
0
    if (peer_state == PEER_STATE_ORPHANED) {
1384
0
        ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
1385
0
        return -1;
1386
0
    }
1387
1388
0
    dgram_bio_get_peer_data(b, NULL, NULL, NULL, &peerb);
1389
1390
    /*
1391
     * For BIO_read we have to acquire both locks because we touch the retry
1392
     * flags on the local bio. (This is avoided in the recvmmsg case as it does
1393
     * not touch the retry flags.)
1394
     */
1395
0
    if (dgram_pair_lock_both_write(peerb, b) == 0) {
1396
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1397
0
        return -1;
1398
0
    }
1399
1400
0
    l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1401
0
    if (l < 0) {
1402
0
        if (l != -BIO_R_NON_FATAL)
1403
0
            ERR_raise(ERR_LIB_BIO, (int)-l);
1404
0
        ret = -1;
1405
0
    } else {
1406
0
        ret = (int)l;
1407
0
    }
1408
1409
0
    dgram_pair_unlock_both(peerb, b);
1410
0
    return ret;
1411
0
}
1412
1413
/* Threadsafe */
1414
static int dgram_pair_recvmmsg(BIO *bio, BIO_MSG *msg,
1415
    size_t stride, size_t num_msg,
1416
    uint64_t flags,
1417
    size_t *num_processed)
1418
0
{
1419
0
    int ret;
1420
0
    ossl_ssize_t l;
1421
0
    BIO_MSG *m;
1422
0
    size_t i;
1423
0
    struct bio_dgram_pair_st *b = bio->ptr, *readb;
1424
0
    CRYPTO_RWLOCK *lock;
1425
0
    int peer_state;
1426
1427
0
    if (num_msg == 0) {
1428
0
        *num_processed = 0;
1429
0
        return 1;
1430
0
    }
1431
1432
0
    if (!bio->init) {
1433
0
        ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
1434
0
        *num_processed = 0;
1435
0
        return 0;
1436
0
    }
1437
1438
0
    if (is_dgram_pair(b)) {
1439
0
        if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock)) {
1440
0
            *num_processed = 0;
1441
0
            return 0;
1442
0
        }
1443
1444
0
        if (peer_state == PEER_STATE_ORPHANED) {
1445
0
            *num_processed = 0;
1446
0
            ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
1447
0
            return 0;
1448
0
        }
1449
0
    }
1450
1451
0
    dgram_bio_get_peer_data(b, NULL, &lock, NULL, &readb);
1452
0
    if (CRYPTO_THREAD_write_lock(lock) == 0) {
1453
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1454
0
        *num_processed = 0;
1455
0
        return 0;
1456
0
    }
1457
1458
0
    for (i = 0; i < num_msg; ++i) {
1459
0
        m = &BIO_MSG_N(msg, i);
1460
0
        l = dgram_pair_read_actual(bio, m->data, m->data_len,
1461
0
            m->local, m->peer, 1);
1462
0
        if (l < 0) {
1463
0
            *num_processed = i;
1464
0
            if (i > 0) {
1465
0
                ret = 1;
1466
0
            } else {
1467
0
                ERR_raise(ERR_LIB_BIO, (int)-l);
1468
0
                ret = 0;
1469
0
            }
1470
0
            goto out;
1471
0
        }
1472
1473
0
        m->data_len = l;
1474
0
        m->flags = 0;
1475
0
    }
1476
1477
0
    *num_processed = i;
1478
0
    ret = 1;
1479
0
out:
1480
0
    CRYPTO_THREAD_unlock(lock);
1481
0
    return ret;
1482
0
}
1483
1484
/* Threadsafe */
1485
static int dgram_mem_read(BIO *bio, char *buf, int sz_)
1486
0
{
1487
0
    int ret;
1488
0
    ossl_ssize_t l;
1489
0
    struct bio_dgram_pair_st *b = bio->ptr;
1490
1491
0
    if (sz_ < 0) {
1492
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1493
0
        return -1;
1494
0
    }
1495
1496
0
    if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
1497
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1498
0
        return -1;
1499
0
    }
1500
1501
0
    l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1502
0
    if (l < 0) {
1503
0
        if (l != -BIO_R_NON_FATAL)
1504
0
            ERR_raise(ERR_LIB_BIO, (int)-l);
1505
0
        ret = -1;
1506
0
    } else {
1507
0
        ret = (int)l;
1508
0
    }
1509
1510
0
    CRYPTO_THREAD_unlock(b->lock);
1511
0
    return ret;
1512
0
}
1513
1514
/*
1515
 * Calculate the array growth based on the target size.
1516
 *
1517
 * The growth factor is a rational number and is defined by a numerator
1518
 * and a denominator.  According to Andrew Koenig in his paper "Why Are
1519
 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
1520
 * than the golden ratio (1.618...).
1521
 *
1522
 * We use an expansion factor of 8 / 5 = 1.6
1523
 */
1524
static const size_t max_rbuf_size = SIZE_MAX / 2; /* unlimited in practice */
1525
static ossl_inline size_t compute_rbuf_growth(size_t target, size_t current)
1526
0
{
1527
0
    int err = 0;
1528
1529
0
    while (current < target) {
1530
0
        if (current >= max_rbuf_size)
1531
0
            return 0;
1532
1533
0
        current = safe_muldiv_size_t(current, 8, 5, &err);
1534
0
        if (err)
1535
0
            return 0;
1536
0
        if (current >= max_rbuf_size)
1537
0
            current = max_rbuf_size;
1538
0
    }
1539
0
    return current;
1540
0
}
1541
1542
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
1543
static void dgram_pcap(struct bio_dgram_pair_st *b, const char *buf, size_t sz)
1544
{
1545
    struct {
1546
        uint32_t pcap_secs;
1547
        uint32_t pcap_usecs;
1548
        uint32_t pcap_plen;
1549
        uint32_t pcap_olen;
1550
    } pcap_hdr;
1551
    struct {
1552
        uint8_t ip_hv;
1553
        uint8_t ip_tos;
1554
        uint16_t ip_len;
1555
        uint16_t ip_id;
1556
        uint8_t ip_frag;
1557
        uint8_t ip_foff;
1558
        uint8_t ip_ttl;
1559
        uint8_t ip_proto;
1560
        uint16_t ip_csum;
1561
        uint32_t ip_src;
1562
        uint32_t ip_dst;
1563
    } ip_hdr;
1564
    struct {
1565
        uint16_t uh_sport;
1566
        uint16_t uh_dport;
1567
        uint16_t uh_len;
1568
        uint16_t uh_csum;
1569
    } udp_hdr;
1570
    uint16_t len;
1571
    OSSL_TIME now;
1572
    struct bio_dgram_pair_st *peer;
1573
1574
    if (b->pair == NULL || b->pair->tcpdump_f == NULL || sz == 0)
1575
        return;
1576
1577
    if ((sz + sizeof(ip_hdr) + sizeof(udp_hdr)) > 65535)
1578
        return;
1579
    len = (uint16_t)(sz + sizeof(ip_hdr) + sizeof(udp_hdr));
1580
1581
    now = ossl_time_now();
1582
1583
    pcap_hdr.pcap_secs = ossl_time2seconds(now);
1584
    pcap_hdr.pcap_usecs = (uint32_t)(ossl_time2us(now) % 1000000);
1585
    pcap_hdr.pcap_plen = (uint32_t)(sz + sizeof(ip_hdr) + sizeof(udp_hdr));
1586
    pcap_hdr.pcap_olen = pcap_hdr.pcap_plen;
1587
1588
    ip_hdr.ip_hv = 0x45;
1589
    ip_hdr.ip_tos = 0;
1590
    ip_hdr.ip_len = htons(len);
1591
    ip_hdr.ip_id = b->pair->ip_id++;
1592
    ip_hdr.ip_frag = 0x40; /* don't fragment */
1593
    ip_hdr.ip_foff = 0;
1594
    ip_hdr.ip_ttl = 64;
1595
    ip_hdr.ip_proto = 17;
1596
    ip_hdr.ip_csum = 0; /* wireshark will complain with ?chksum offload? */
1597
    ip_hdr.ip_src = (b->local_addr == NULL)
1598
        ? htonl(0x7f000001)
1599
        : b->local_addr->s_in.sin_addr.s_addr;
1600
    if (b->pair->map[0].self == b) {
1601
        peer = b->pair->map[1].self;
1602
    } else {
1603
        peer = b->pair->map[0].self;
1604
    }
1605
    ip_hdr.ip_dst = (peer == NULL || peer->local_addr == NULL)
1606
        ? htonl(0x7f000001)
1607
        : peer->local_addr->s_in.sin_addr.s_addr;
1608
1609
    /*
1610
     * use some fake port numbers
1611
     */
1612
    udp_hdr.uh_sport = (b->local_addr == NULL) ? htons(8080) : b->local_addr->s_in.sin_port;
1613
    udp_hdr.uh_dport = (peer == NULL || peer->local_addr == NULL) ? htons(4040) : peer->local_addr->s_in.sin_port;
1614
    udp_hdr.uh_csum = 0;
1615
    udp_hdr.uh_len = htons((uint16_t)(sz + sizeof(udp_hdr)));
1616
#ifdef OPENSSL_THREADS
1617
    ossl_crypto_mutex_lock(b->pair->pcap_mutex);
1618
#endif
1619
    fwrite(&pcap_hdr, sizeof(pcap_hdr), 1, b->pair->tcpdump_f);
1620
    fwrite(&ip_hdr, sizeof(ip_hdr), 1, b->pair->tcpdump_f);
1621
    fwrite(&udp_hdr, sizeof(udp_hdr), 1, b->pair->tcpdump_f);
1622
    fwrite(buf, sz, 1, b->pair->tcpdump_f);
1623
#ifdef OPENSSL_THREADS
1624
    ossl_crypto_mutex_unlock(b->pair->pcap_mutex);
1625
#endif
1626
}
1627
#endif
1628
1629
/* Must hold local write lock */
1630
static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b,
1631
    const uint8_t *buf, size_t sz)
1632
0
{
1633
0
    size_t total_written = 0;
1634
0
    struct ring_buf *rbufptr;
1635
1636
0
    dgram_bio_get_self_data(b, &rbufptr, NULL);
1637
1638
    /*
1639
     * We repeat pushes to the ring buffer for as long as we have data until we
1640
     * fail. We may not be able to push in one operation if the ring buffer
1641
     * wraps around, but there may still be more room for data.
1642
     */
1643
0
    while (sz > 0) {
1644
0
        size_t dst_len;
1645
0
        uint8_t *dst_buf;
1646
1647
        /*
1648
         * There are two BIO instances, each with a ringbuf. We write to our own
1649
         * ringbuf and read from the peer ringbuf.
1650
         */
1651
0
        ring_buf_head(rbufptr, &dst_buf, &dst_len);
1652
0
        if (dst_len == 0) {
1653
0
            size_t new_len;
1654
1655
0
            if (!b->grows_on_write) /* resize only if size not set explicitly */
1656
0
                break;
1657
            /* increase the size */
1658
0
            new_len = compute_rbuf_growth(b->req_buf_len + sz, b->req_buf_len);
1659
0
            if (new_len == 0 || !ring_buf_resize(rbufptr, new_len))
1660
0
                break;
1661
0
            b->req_buf_len = new_len;
1662
0
        }
1663
1664
0
        if (dst_len > sz)
1665
0
            dst_len = sz;
1666
1667
0
        memcpy(dst_buf, buf, dst_len);
1668
0
        ring_buf_push(rbufptr, dst_len);
1669
1670
0
        buf += dst_len;
1671
0
        sz -= dst_len;
1672
0
        total_written += dst_len;
1673
0
    }
1674
1675
0
    return total_written;
1676
0
}
1677
1678
/*
1679
 * Must hold local write lock. Returns number of bytes processed or negated BIO
1680
 * response code.
1681
 */
1682
static ossl_ssize_t dgram_pair_write_actual(BIO *bio, const char *buf, size_t sz,
1683
    const BIO_ADDR *local, const BIO_ADDR *peer,
1684
    int is_multi)
1685
0
{
1686
0
    static const BIO_ADDR zero_addr;
1687
0
    size_t saved_idx, saved_count;
1688
0
    struct bio_dgram_pair_st *b = bio->ptr;
1689
0
    struct dgram_hdr hdr = { 0 };
1690
0
    struct ring_buf *rbufptr;
1691
0
    int peer_state;
1692
0
    uint32_t caps;
1693
1694
0
    if (!is_multi)
1695
0
        BIO_clear_retry_flags(bio);
1696
1697
0
    if (!bio->init)
1698
0
        return -BIO_R_UNINITIALIZED;
1699
1700
0
    if (is_dgram_pair(b)) {
1701
0
        if (!CRYPTO_atomic_load_int(&b->pair->peer_state, &peer_state, b->pair->peerlock))
1702
0
            return -BIO_R_UNINITIALIZED;
1703
0
        if (peer_state == PEER_STATE_ORPHANED)
1704
0
            return -BIO_R_BROKEN_PIPE;
1705
0
    }
1706
1707
0
    dgram_bio_get_self_data(b, &rbufptr, NULL);
1708
1709
0
    if (!ossl_assert(b != NULL && rbufptr->start != NULL))
1710
0
        return -BIO_R_TRANSFER_ERROR;
1711
1712
0
    if (sz > 0 && buf == NULL)
1713
0
        return -BIO_R_INVALID_ARGUMENT;
1714
1715
0
    if (local != NULL && b->local_addr_enable == 0)
1716
0
        return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
1717
1718
0
    dgram_bio_get_peer_data(b, NULL, NULL, &caps, NULL);
1719
1720
0
    if (peer != NULL && (caps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) == 0)
1721
0
        return -BIO_R_PEER_ADDR_NOT_AVAILABLE;
1722
1723
0
    hdr.len = sz;
1724
0
    hdr.dst_addr = (peer != NULL ? *peer : zero_addr);
1725
0
    if (local == NULL)
1726
0
        local = b->local_addr;
1727
0
    hdr.src_addr = (local != NULL ? *local : zero_addr);
1728
1729
0
    saved_idx = rbufptr->idx[0];
1730
0
    saved_count = rbufptr->count;
1731
0
    if (dgram_pair_write_inner(b, (const uint8_t *)&hdr, sizeof(hdr)) != sizeof(hdr)
1732
0
        || dgram_pair_write_inner(b, (const uint8_t *)buf, sz) != sz) {
1733
        /*
1734
         * We were not able to push the header and the entirety of the payload
1735
         * onto the ring buffer, so abort and roll back the ring buffer state.
1736
         */
1737
0
        rbufptr->idx[0] = saved_idx;
1738
0
        rbufptr->count = saved_count;
1739
0
        if (!is_multi)
1740
0
            BIO_set_retry_write(bio);
1741
0
        return -BIO_R_NON_FATAL;
1742
0
    }
1743
1744
#ifndef OPENSSL_NO_DGRAM_PAIR_PCAP
1745
    dgram_pcap(b, buf, sz);
1746
#endif
1747
1748
0
    return sz;
1749
0
}
1750
1751
/* Threadsafe */
1752
static int dgram_pair_write(BIO *bio, const char *buf, int sz_)
1753
0
{
1754
0
    int ret;
1755
0
    ossl_ssize_t l;
1756
0
    struct bio_dgram_pair_st *b = bio->ptr;
1757
0
    CRYPTO_RWLOCK *lock;
1758
1759
0
    if (sz_ < 0) {
1760
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
1761
0
        return -1;
1762
0
    }
1763
1764
0
    dgram_bio_get_self_data(b, NULL, &lock);
1765
1766
0
    if (CRYPTO_THREAD_write_lock(lock) == 0) {
1767
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1768
0
        return -1;
1769
0
    }
1770
1771
0
    l = dgram_pair_write_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
1772
0
    if (l < 0) {
1773
0
        ERR_raise(ERR_LIB_BIO, (int)-l);
1774
0
        ret = -1;
1775
0
    } else {
1776
0
        ret = (int)l;
1777
0
    }
1778
1779
0
    CRYPTO_THREAD_unlock(lock);
1780
0
    return ret;
1781
0
}
1782
1783
/* Threadsafe */
1784
static int dgram_pair_sendmmsg(BIO *bio, BIO_MSG *msg,
1785
    size_t stride, size_t num_msg,
1786
    uint64_t flags, size_t *num_processed)
1787
0
{
1788
0
    ossl_ssize_t l;
1789
0
    BIO_MSG *m;
1790
0
    size_t i;
1791
0
    struct bio_dgram_pair_st *b = bio->ptr;
1792
0
    int ret = 0;
1793
0
    CRYPTO_RWLOCK *lock;
1794
1795
0
    if (num_msg == 0) {
1796
0
        *num_processed = 0;
1797
0
        return 1;
1798
0
    }
1799
1800
0
    dgram_bio_get_self_data(b, NULL, &lock);
1801
1802
0
    if (CRYPTO_THREAD_write_lock(lock) == 0) {
1803
0
        ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
1804
0
        *num_processed = 0;
1805
0
        return 0;
1806
0
    }
1807
1808
0
    for (i = 0; i < num_msg; ++i) {
1809
0
        m = &BIO_MSG_N(msg, i);
1810
0
        l = dgram_pair_write_actual(bio, m->data, m->data_len,
1811
0
            m->local, m->peer, 1);
1812
0
        if (l < 0) {
1813
0
            *num_processed = i;
1814
0
            if (i > 0) {
1815
0
                ret = 1;
1816
0
            } else {
1817
0
                ERR_raise(ERR_LIB_BIO, (int)-l);
1818
0
            }
1819
0
            goto out;
1820
0
        }
1821
1822
0
        m->flags = 0;
1823
0
    }
1824
1825
0
    *num_processed = i;
1826
0
    ret = 1;
1827
0
out:
1828
0
    CRYPTO_THREAD_unlock(lock);
1829
0
    return ret;
1830
0
}
1831
1832
#endif