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