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