12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328 |
- /*
- * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
- *
- * Licensed under the Apache License 2.0 (the "License"). You may not use
- * this file except in compliance with the License. You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
- #include <stdio.h>
- #include <errno.h>
- #include "bio_local.h"
- #include "internal/cryptlib.h"
- #include "internal/safe_math.h"
- #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
- OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
- /* ===========================================================================
- * Byte-wise ring buffer which supports pushing and popping blocks of multiple
- * bytes at a time.
- */
- struct ring_buf {
- unsigned char *start; /* start of buffer */
- size_t len; /* size of buffer allocation in bytes */
- size_t count; /* number of bytes currently pushed */
- /*
- * These index into start. Where idx[0] == idx[1], the buffer is full
- * (if count is nonzero) and empty otherwise.
- */
- size_t idx[2]; /* 0: head, 1: tail */
- };
- static int ring_buf_init(struct ring_buf *r, size_t nbytes)
- {
- r->start = OPENSSL_malloc(nbytes);
- if (r->start == NULL)
- return 0;
- r->len = nbytes;
- r->idx[0] = r->idx[1] = r->count = 0;
- return 1;
- }
- static void ring_buf_destroy(struct ring_buf *r)
- {
- OPENSSL_free(r->start);
- r->start = NULL;
- r->len = 0;
- r->count = 0;
- }
- /*
- * Get a pointer to the next place to write data to be pushed to the ring buffer
- * (idx=0), or the next data to be popped from the ring buffer (idx=1). The
- * pointer is written to *buf and the maximum number of bytes which can be
- * read/written are written to *len. After writing data to the buffer, call
- * ring_buf_push/pop() with the number of bytes actually read/written, which
- * must not exceed the returned length.
- */
- static void ring_buf_head_tail(struct ring_buf *r, int idx, uint8_t **buf, size_t *len)
- {
- size_t max_len = r->len - r->idx[idx];
- if (idx == 0 && max_len > r->len - r->count)
- max_len = r->len - r->count;
- if (idx == 1 && max_len > r->count)
- max_len = r->count;
- *buf = (uint8_t *)r->start + r->idx[idx];
- *len = max_len;
- }
- #define ring_buf_head(r, buf, len) ring_buf_head_tail((r), 0, (buf), (len))
- #define ring_buf_tail(r, buf, len) ring_buf_head_tail((r), 1, (buf), (len))
- /*
- * Commit bytes to the ring buffer previously filled after a call to
- * ring_buf_head().
- */
- static void ring_buf_push_pop(struct ring_buf *r, int idx, size_t num_bytes)
- {
- size_t new_idx;
- /* A single push/pop op cannot wrap around, though it can reach the end.
- * If the caller adheres to the convention of using the length returned
- * by ring_buf_head/tail(), this cannot happen.
- */
- if (!ossl_assert(num_bytes <= r->len - r->idx[idx]))
- return;
- /*
- * Must not overfill the buffer, or pop more than is in the buffer either.
- */
- if (!ossl_assert(idx != 0 ? num_bytes <= r->count
- : num_bytes + r->count <= r->len))
- return;
- /* Update the index. */
- new_idx = r->idx[idx] + num_bytes;
- if (new_idx == r->len)
- new_idx = 0;
- r->idx[idx] = new_idx;
- if (idx != 0)
- r->count -= num_bytes;
- else
- r->count += num_bytes;
- }
- #define ring_buf_push(r, num_bytes) ring_buf_push_pop((r), 0, (num_bytes))
- #define ring_buf_pop(r, num_bytes) ring_buf_push_pop((r), 1, (num_bytes))
- static void ring_buf_clear(struct ring_buf *r)
- {
- r->idx[0] = r->idx[1] = r->count = 0;
- }
- static int ring_buf_resize(struct ring_buf *r, size_t nbytes)
- {
- unsigned char *new_start;
- if (r->start == NULL)
- return ring_buf_init(r, nbytes);
- if (nbytes == r->len)
- return 1;
- if (r->count > 0 && nbytes < r->len)
- /* fail shrinking the ring buffer when there is any data in it */
- return 0;
- new_start = OPENSSL_realloc(r->start, nbytes);
- if (new_start == NULL)
- return 0;
- /* Moving tail if it is after (or equal to) head */
- if (r->count > 0) {
- if (r->idx[0] <= r->idx[1]) {
- size_t offset = nbytes - r->len;
- memmove(new_start + r->idx[1] + offset, new_start + r->idx[1],
- r->len - r->idx[1]);
- r->idx[1] += offset;
- }
- } else {
- /* just reset the head/tail because it might be pointing outside */
- r->idx[0] = r->idx[1] = 0;
- }
- r->start = new_start;
- r->len = nbytes;
- return 1;
- }
- /* ===========================================================================
- * BIO_s_dgram_pair is documented in BIO_s_dgram_pair(3).
- *
- * INTERNAL DATA STRUCTURE
- *
- * This is managed internally by using a bytewise ring buffer which supports
- * pushing and popping spans of multiple bytes at once. The ring buffer stores
- * internal packets which look like this:
- *
- * struct dgram_hdr hdr;
- * uint8_t data[];
- *
- * The header contains the length of the data and metadata such as
- * source/destination addresses.
- *
- * The datagram pair BIO is designed to support both traditional
- * BIO_read/BIO_write (likely to be used by applications) as well as
- * BIO_recvmmsg/BIO_sendmmsg.
- */
- struct bio_dgram_pair_st;
- static int dgram_pair_write(BIO *bio, const char *buf, int sz_);
- static int dgram_pair_read(BIO *bio, char *buf, int sz_);
- static int dgram_mem_read(BIO *bio, char *buf, int sz_);
- static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr);
- static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr);
- static int dgram_pair_init(BIO *bio);
- static int dgram_mem_init(BIO *bio);
- static int dgram_pair_free(BIO *bio);
- static int dgram_pair_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
- size_t num_msg, uint64_t flags,
- size_t *num_processed);
- static int dgram_pair_recvmmsg(BIO *b, BIO_MSG *msg, size_t stride,
- size_t num_msg, uint64_t flags,
- size_t *num_processed);
- static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1);
- static size_t dgram_pair_read_inner(struct bio_dgram_pair_st *b, uint8_t *buf,
- size_t sz);
- #define BIO_MSG_N(array, n) (*(BIO_MSG *)((char *)(array) + (n)*stride))
- static const BIO_METHOD dgram_pair_method = {
- BIO_TYPE_DGRAM_PAIR,
- "BIO dgram pair",
- bwrite_conv,
- dgram_pair_write,
- bread_conv,
- dgram_pair_read,
- NULL, /* dgram_pair_puts */
- NULL, /* dgram_pair_gets */
- dgram_pair_ctrl,
- dgram_pair_init,
- dgram_pair_free,
- NULL, /* dgram_pair_callback_ctrl */
- dgram_pair_sendmmsg,
- dgram_pair_recvmmsg,
- };
- static const BIO_METHOD dgram_mem_method = {
- BIO_TYPE_DGRAM_MEM,
- "BIO dgram mem",
- bwrite_conv,
- dgram_pair_write,
- bread_conv,
- dgram_mem_read,
- NULL, /* dgram_pair_puts */
- NULL, /* dgram_pair_gets */
- dgram_mem_ctrl,
- dgram_mem_init,
- dgram_pair_free,
- NULL, /* dgram_pair_callback_ctrl */
- dgram_pair_sendmmsg,
- dgram_pair_recvmmsg,
- };
- const BIO_METHOD *BIO_s_dgram_pair(void)
- {
- return &dgram_pair_method;
- }
- const BIO_METHOD *BIO_s_dgram_mem(void)
- {
- return &dgram_mem_method;
- }
- struct dgram_hdr {
- size_t len; /* payload length in bytes, not including this struct */
- BIO_ADDR src_addr, dst_addr; /* family == 0: not present */
- };
- struct bio_dgram_pair_st {
- /* The other half of the BIO pair. NULL for dgram_mem. */
- BIO *peer;
- /* Writes are directed to our own ringbuf and reads to our peer. */
- struct ring_buf rbuf;
- /* Requested size of rbuf buffer in bytes once we initialize. */
- size_t req_buf_len;
- /* Largest possible datagram size */
- size_t mtu;
- /* Capability flags. */
- uint32_t cap;
- /*
- * This lock protects updates to our rbuf. Since writes are directed to our
- * own rbuf, this means we use this lock for writes and our peer's lock for
- * reads.
- */
- CRYPTO_RWLOCK *lock;
- unsigned int no_trunc : 1; /* Reads fail if they would truncate */
- unsigned int local_addr_enable : 1; /* Can use BIO_MSG->local? */
- unsigned int role : 1; /* Determines lock order */
- unsigned int grows_on_write : 1; /* Set for BIO_s_dgram_mem only */
- };
- #define MIN_BUF_LEN (1024)
- #define is_dgram_pair(b) (b->peer != NULL)
- static int dgram_pair_init(BIO *bio)
- {
- struct bio_dgram_pair_st *b = OPENSSL_zalloc(sizeof(*b));
- if (b == NULL)
- return 0;
- b->mtu = 1472; /* conservative default MTU */
- /* default buffer size */
- b->req_buf_len = 9 * (sizeof(struct dgram_hdr) + b->mtu);
- b->lock = CRYPTO_THREAD_lock_new();
- if (b->lock == NULL) {
- OPENSSL_free(b);
- return 0;
- }
- bio->ptr = b;
- return 1;
- }
- static int dgram_mem_init(BIO *bio)
- {
- struct bio_dgram_pair_st *b;
- if (!dgram_pair_init(bio))
- return 0;
- b = bio->ptr;
- if (ring_buf_init(&b->rbuf, b->req_buf_len) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
- return 0;
- }
- b->grows_on_write = 1;
- bio->init = 1;
- return 1;
- }
- static int dgram_pair_free(BIO *bio)
- {
- struct bio_dgram_pair_st *b;
- if (bio == NULL)
- return 0;
- b = bio->ptr;
- if (!ossl_assert(b != NULL))
- return 0;
- /* We are being freed. Disconnect any peer and destroy buffers. */
- dgram_pair_ctrl_destroy_bio_pair(bio);
- CRYPTO_THREAD_lock_free(b->lock);
- OPENSSL_free(b);
- return 1;
- }
- /* BIO_make_bio_pair (BIO_C_MAKE_BIO_PAIR) */
- static int dgram_pair_ctrl_make_bio_pair(BIO *bio1, BIO *bio2)
- {
- struct bio_dgram_pair_st *b1, *b2;
- /* peer must be non-NULL. */
- if (bio1 == NULL || bio2 == NULL) {
- ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
- return 0;
- }
- /* Ensure the BIO we have been passed is actually a dgram pair BIO. */
- if (bio1->method != &dgram_pair_method || bio2->method != &dgram_pair_method) {
- ERR_raise_data(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT,
- "both BIOs must be BIO_dgram_pair");
- return 0;
- }
- b1 = bio1->ptr;
- b2 = bio2->ptr;
- if (!ossl_assert(b1 != NULL && b2 != NULL)) {
- ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
- return 0;
- }
- /*
- * This ctrl cannot be used to associate a BIO pair half which is already
- * associated.
- */
- if (b1->peer != NULL || b2->peer != NULL) {
- ERR_raise_data(ERR_LIB_BIO, BIO_R_IN_USE,
- "cannot associate a BIO_dgram_pair which is already in use");
- return 0;
- }
- if (!ossl_assert(b1->req_buf_len >= MIN_BUF_LEN
- && b2->req_buf_len >= MIN_BUF_LEN)) {
- ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
- return 0;
- }
- if (b1->rbuf.len != b1->req_buf_len)
- if (ring_buf_init(&b1->rbuf, b1->req_buf_len) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
- return 0;
- }
- if (b2->rbuf.len != b2->req_buf_len)
- if (ring_buf_init(&b2->rbuf, b2->req_buf_len) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
- ring_buf_destroy(&b1->rbuf);
- return 0;
- }
- b1->peer = bio2;
- b2->peer = bio1;
- b1->role = 0;
- b2->role = 1;
- bio1->init = 1;
- bio2->init = 1;
- return 1;
- }
- /* BIO_destroy_bio_pair (BIO_C_DESTROY_BIO_PAIR) */
- static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1)
- {
- BIO *bio2;
- struct bio_dgram_pair_st *b1 = bio1->ptr, *b2;
- ring_buf_destroy(&b1->rbuf);
- bio1->init = 0;
- /* Early return if we don't have a peer. */
- if (b1->peer == NULL)
- return 1;
- bio2 = b1->peer;
- b2 = bio2->ptr;
- /* Invariant. */
- if (!ossl_assert(b2->peer == bio1))
- return 0;
- /* Free buffers. */
- ring_buf_destroy(&b2->rbuf);
- bio2->init = 0;
- b1->peer = NULL;
- b2->peer = NULL;
- return 1;
- }
- /* BIO_eof (BIO_CTRL_EOF) */
- static int dgram_pair_ctrl_eof(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr, *peerb;
- if (!ossl_assert(b != NULL))
- return -1;
- /* If we aren't initialized, we can never read anything */
- if (!bio->init)
- return 1;
- if (!is_dgram_pair(b))
- return 0;
- peerb = b->peer->ptr;
- if (!ossl_assert(peerb != NULL))
- return -1;
- /*
- * Since we are emulating datagram semantics, never indicate EOF so long as
- * we have a peer.
- */
- return 0;
- }
- /* BIO_set_write_buf_size (BIO_C_SET_WRITE_BUF_SIZE) */
- static int dgram_pair_ctrl_set_write_buf_size(BIO *bio, size_t len)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- /* Changing buffer sizes is not permitted while a peer is connected. */
- if (b->peer != NULL) {
- ERR_raise(ERR_LIB_BIO, BIO_R_IN_USE);
- return 0;
- }
- /* Enforce minimum size. */
- if (len < MIN_BUF_LEN)
- len = MIN_BUF_LEN;
- if (b->rbuf.start != NULL) {
- if (!ring_buf_resize(&b->rbuf, len))
- return 0;
- }
- b->req_buf_len = len;
- b->grows_on_write = 0;
- return 1;
- }
- /* BIO_reset (BIO_CTRL_RESET) */
- static int dgram_pair_ctrl_reset(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- ring_buf_clear(&b->rbuf);
- return 1;
- }
- /* BIO_pending (BIO_CTRL_PENDING) (Threadsafe) */
- static size_t dgram_pair_ctrl_pending(BIO *bio)
- {
- size_t saved_idx, saved_count;
- struct bio_dgram_pair_st *b = bio->ptr, *readb;
- struct dgram_hdr hdr;
- size_t l;
- /* Safe to check; init may not change during this call */
- if (!bio->init)
- return 0;
- if (is_dgram_pair(b))
- readb = b->peer->ptr;
- else
- readb = b;
- if (CRYPTO_THREAD_write_lock(readb->lock) == 0)
- return 0;
- saved_idx = readb->rbuf.idx[1];
- saved_count = readb->rbuf.count;
- l = dgram_pair_read_inner(readb, (uint8_t *)&hdr, sizeof(hdr));
- readb->rbuf.idx[1] = saved_idx;
- readb->rbuf.count = saved_count;
- CRYPTO_THREAD_unlock(readb->lock);
- if (!ossl_assert(l == 0 || l == sizeof(hdr)))
- return 0;
- return l > 0 ? hdr.len : 0;
- }
- /* BIO_get_write_guarantee (BIO_C_GET_WRITE_GUARANTEE) (Threadsafe) */
- static size_t dgram_pair_ctrl_get_write_guarantee(BIO *bio)
- {
- size_t l;
- struct bio_dgram_pair_st *b = bio->ptr;
- if (CRYPTO_THREAD_read_lock(b->lock) == 0)
- return 0;
- l = b->rbuf.len - b->rbuf.count;
- if (l >= sizeof(struct dgram_hdr))
- l -= sizeof(struct dgram_hdr);
- /*
- * If the amount of buffer space would not be enough to accommodate the
- * worst-case size of a datagram, report no space available.
- */
- if (l < b->mtu)
- l = 0;
- CRYPTO_THREAD_unlock(b->lock);
- return l;
- }
- /* BIO_dgram_get_local_addr_cap (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP) */
- static int dgram_pair_ctrl_get_local_addr_cap(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr, *readb;
- if (!bio->init)
- return 0;
- if (is_dgram_pair(b))
- readb = b->peer->ptr;
- else
- readb = b;
- return (~readb->cap & (BIO_DGRAM_CAP_HANDLES_SRC_ADDR
- | BIO_DGRAM_CAP_PROVIDES_DST_ADDR)) == 0;
- }
- /* BIO_dgram_get_effective_caps (BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS) */
- static int dgram_pair_ctrl_get_effective_caps(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr, *peerb;
- if (b->peer == NULL)
- return 0;
- peerb = b->peer->ptr;
- return peerb->cap;
- }
- /* BIO_dgram_get_caps (BIO_CTRL_DGRAM_GET_CAPS) */
- static uint32_t dgram_pair_ctrl_get_caps(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- return b->cap;
- }
- /* BIO_dgram_set_caps (BIO_CTRL_DGRAM_SET_CAPS) */
- static int dgram_pair_ctrl_set_caps(BIO *bio, uint32_t caps)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- b->cap = caps;
- return 1;
- }
- /* BIO_dgram_get_local_addr_enable (BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE) */
- static int dgram_pair_ctrl_get_local_addr_enable(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- return b->local_addr_enable;
- }
- /* BIO_dgram_set_local_addr_enable (BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE) */
- static int dgram_pair_ctrl_set_local_addr_enable(BIO *bio, int enable)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- if (dgram_pair_ctrl_get_local_addr_cap(bio) == 0)
- return 0;
- b->local_addr_enable = (enable != 0 ? 1 : 0);
- return 1;
- }
- /* BIO_dgram_get_mtu (BIO_CTRL_DGRAM_GET_MTU) */
- static int dgram_pair_ctrl_get_mtu(BIO *bio)
- {
- struct bio_dgram_pair_st *b = bio->ptr;
- return b->mtu;
- }
- /* BIO_dgram_set_mtu (BIO_CTRL_DGRAM_SET_MTU) */
- static int dgram_pair_ctrl_set_mtu(BIO *bio, size_t mtu)
- {
- struct bio_dgram_pair_st *b = bio->ptr, *peerb;
- b->mtu = mtu;
- if (b->peer != NULL) {
- peerb = b->peer->ptr;
- peerb->mtu = mtu;
- }
- return 1;
- }
- /* Partially threadsafe (some commands) */
- static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
- {
- long ret = 1;
- struct bio_dgram_pair_st *b = bio->ptr;
- if (!ossl_assert(b != NULL))
- return 0;
- switch (cmd) {
- /*
- * BIO_set_write_buf_size: Set the size of the ring buffer used for storing
- * datagrams. No more writes can be performed once the buffer is filled up,
- * until reads are performed. This cannot be used after a peer is connected.
- */
- case BIO_C_SET_WRITE_BUF_SIZE: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_set_write_buf_size(bio, (size_t)num);
- break;
- /*
- * BIO_get_write_buf_size: Get ring buffer size.
- */
- case BIO_C_GET_WRITE_BUF_SIZE: /* Non-threadsafe */
- ret = (long)b->req_buf_len;
- break;
- /*
- * BIO_reset: Clear all data which was written to this side of the pair.
- */
- case BIO_CTRL_RESET: /* Non-threadsafe */
- dgram_pair_ctrl_reset(bio);
- break;
- /*
- * BIO_get_write_guarantee: Any BIO_write providing a buffer less than or
- * equal to this value is guaranteed to succeed.
- */
- case BIO_C_GET_WRITE_GUARANTEE: /* Threadsafe */
- ret = (long)dgram_pair_ctrl_get_write_guarantee(bio);
- break;
- /* BIO_pending: Bytes available to read. */
- case BIO_CTRL_PENDING: /* Threadsafe */
- ret = (long)dgram_pair_ctrl_pending(bio);
- break;
- /* BIO_flush: No-op. */
- case BIO_CTRL_FLUSH: /* Threadsafe */
- break;
- /* BIO_dgram_get_no_trunc */
- case BIO_CTRL_DGRAM_GET_NO_TRUNC: /* Non-threadsafe */
- ret = (long)b->no_trunc;
- break;
- /* BIO_dgram_set_no_trunc */
- case BIO_CTRL_DGRAM_SET_NO_TRUNC: /* Non-threadsafe */
- b->no_trunc = (num > 0);
- break;
- /* BIO_dgram_get_local_addr_enable */
- case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
- *(int *)ptr = (int)dgram_pair_ctrl_get_local_addr_enable(bio);
- break;
- /* BIO_dgram_set_local_addr_enable */
- case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_set_local_addr_enable(bio, num);
- break;
- /* BIO_dgram_get_local_addr_cap: Can local addresses be supported? */
- case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_get_local_addr_cap(bio);
- break;
- /* BIO_dgram_get_effective_caps */
- case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
- /* BIO_dgram_get_caps */
- case BIO_CTRL_DGRAM_GET_CAPS: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_get_caps(bio);
- break;
- /* BIO_dgram_set_caps */
- case BIO_CTRL_DGRAM_SET_CAPS: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_set_caps(bio, (uint32_t)num);
- break;
- /* BIO_dgram_get_mtu */
- case BIO_CTRL_DGRAM_GET_MTU: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_get_mtu(bio);
- break;
- /* BIO_dgram_set_mtu */
- case BIO_CTRL_DGRAM_SET_MTU: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_set_mtu(bio, (uint32_t)num);
- break;
- /*
- * BIO_eof: Returns whether this half of the BIO pair is empty of data to
- * read.
- */
- case BIO_CTRL_EOF: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_eof(bio);
- break;
- default:
- ret = 0;
- break;
- }
- return ret;
- }
- static long dgram_pair_ctrl(BIO *bio, int cmd, long num, void *ptr)
- {
- long ret = 1;
- switch (cmd) {
- /*
- * BIO_make_bio_pair: this is usually used by BIO_new_dgram_pair, though it
- * may be used manually after manually creating each half of a BIO pair
- * using BIO_new. This only needs to be called on one of the BIOs.
- */
- case BIO_C_MAKE_BIO_PAIR: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_make_bio_pair(bio, (BIO *)ptr);
- break;
- /*
- * BIO_destroy_bio_pair: Manually disconnect two halves of a BIO pair so
- * that they are no longer peers.
- */
- case BIO_C_DESTROY_BIO_PAIR: /* Non-threadsafe */
- dgram_pair_ctrl_destroy_bio_pair(bio);
- break;
- /* BIO_dgram_get_effective_caps */
- case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS: /* Non-threadsafe */
- ret = (long)dgram_pair_ctrl_get_effective_caps(bio);
- break;
- default:
- ret = dgram_mem_ctrl(bio, cmd, num, ptr);
- break;
- }
- return ret;
- }
- int BIO_new_bio_dgram_pair(BIO **pbio1, size_t writebuf1,
- BIO **pbio2, size_t writebuf2)
- {
- int ret = 0;
- long r;
- BIO *bio1 = NULL, *bio2 = NULL;
- bio1 = BIO_new(BIO_s_dgram_pair());
- if (bio1 == NULL)
- goto err;
- bio2 = BIO_new(BIO_s_dgram_pair());
- if (bio2 == NULL)
- goto err;
- if (writebuf1 > 0) {
- r = BIO_set_write_buf_size(bio1, writebuf1);
- if (r == 0)
- goto err;
- }
- if (writebuf2 > 0) {
- r = BIO_set_write_buf_size(bio2, writebuf2);
- if (r == 0)
- goto err;
- }
- r = BIO_make_bio_pair(bio1, bio2);
- if (r == 0)
- goto err;
- ret = 1;
- err:
- if (ret == 0) {
- BIO_free(bio1);
- bio1 = NULL;
- BIO_free(bio2);
- bio2 = NULL;
- }
- *pbio1 = bio1;
- *pbio2 = bio2;
- return ret;
- }
- /* Must hold peer write lock */
- static size_t dgram_pair_read_inner(struct bio_dgram_pair_st *b, uint8_t *buf, size_t sz)
- {
- size_t total_read = 0;
- /*
- * We repeat pops from the ring buffer for as long as we have more
- * application *buffer to fill until we fail. We may not be able to pop
- * enough data to fill the buffer in one operation if the ring buffer wraps
- * around, but there may still be more data available.
- */
- while (sz > 0) {
- uint8_t *src_buf = NULL;
- size_t src_len = 0;
- /*
- * There are two BIO instances, each with a ringbuf. We read from the
- * peer ringbuf and write to our own ringbuf.
- */
- ring_buf_tail(&b->rbuf, &src_buf, &src_len);
- if (src_len == 0)
- break;
- if (src_len > sz)
- src_len = sz;
- if (buf != NULL)
- memcpy(buf, src_buf, src_len);
- ring_buf_pop(&b->rbuf, src_len);
- if (buf != NULL)
- buf += src_len;
- total_read += src_len;
- sz -= src_len;
- }
- return total_read;
- }
- /*
- * Must hold peer write lock. Returns number of bytes processed or negated BIO
- * response code.
- */
- static ossl_ssize_t dgram_pair_read_actual(BIO *bio, char *buf, size_t sz,
- BIO_ADDR *local, BIO_ADDR *peer,
- int is_multi)
- {
- size_t l, trunc = 0, saved_idx, saved_count;
- struct bio_dgram_pair_st *b = bio->ptr, *readb;
- struct dgram_hdr hdr;
- if (!is_multi)
- BIO_clear_retry_flags(bio);
- if (!bio->init)
- return -BIO_R_UNINITIALIZED;
- if (!ossl_assert(b != NULL))
- return -BIO_R_TRANSFER_ERROR;
- if (is_dgram_pair(b))
- readb = b->peer->ptr;
- else
- readb = b;
- if (!ossl_assert(readb != NULL && readb->rbuf.start != NULL))
- return -BIO_R_TRANSFER_ERROR;
- if (sz > 0 && buf == NULL)
- return -BIO_R_INVALID_ARGUMENT;
- /* If the caller wants to know the local address, it must be enabled */
- if (local != NULL && b->local_addr_enable == 0)
- return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
- /* Read the header. */
- saved_idx = readb->rbuf.idx[1];
- saved_count = readb->rbuf.count;
- l = dgram_pair_read_inner(readb, (uint8_t *)&hdr, sizeof(hdr));
- if (l == 0) {
- /* Buffer was empty. */
- if (!is_multi)
- BIO_set_retry_read(bio);
- return -BIO_R_NON_FATAL;
- }
- if (!ossl_assert(l == sizeof(hdr)))
- /*
- * This should not be possible as headers (and their following payloads)
- * should always be written atomically.
- */
- return -BIO_R_BROKEN_PIPE;
- if (sz > hdr.len) {
- sz = hdr.len;
- } else if (sz < hdr.len) {
- /* Truncation is occurring. */
- trunc = hdr.len - sz;
- if (b->no_trunc) {
- /* Restore original state. */
- readb->rbuf.idx[1] = saved_idx;
- readb->rbuf.count = saved_count;
- return -BIO_R_NON_FATAL;
- }
- }
- l = dgram_pair_read_inner(readb, (uint8_t *)buf, sz);
- if (!ossl_assert(l == sz))
- /* We were somehow not able to read the entire datagram. */
- return -BIO_R_TRANSFER_ERROR;
- /*
- * If the datagram was truncated due to an inadequate buffer, discard the
- * remainder.
- */
- if (trunc > 0 && !ossl_assert(dgram_pair_read_inner(readb, NULL, trunc) == trunc))
- /* We were somehow not able to read/skip the entire datagram. */
- return -BIO_R_TRANSFER_ERROR;
- if (local != NULL)
- *local = hdr.dst_addr;
- if (peer != NULL)
- *peer = hdr.src_addr;
- return (ossl_ssize_t)l;
- }
- /* Threadsafe */
- static int dgram_pair_lock_both_write(struct bio_dgram_pair_st *a,
- struct bio_dgram_pair_st *b)
- {
- struct bio_dgram_pair_st *x, *y;
- x = (a->role == 1) ? a : b;
- y = (a->role == 1) ? b : a;
- if (!ossl_assert(a->role != b->role))
- return 0;
- if (!ossl_assert(a != b && x != y))
- return 0;
- if (CRYPTO_THREAD_write_lock(x->lock) == 0)
- return 0;
- if (CRYPTO_THREAD_write_lock(y->lock) == 0) {
- CRYPTO_THREAD_unlock(x->lock);
- return 0;
- }
- return 1;
- }
- static void dgram_pair_unlock_both(struct bio_dgram_pair_st *a,
- struct bio_dgram_pair_st *b)
- {
- CRYPTO_THREAD_unlock(a->lock);
- CRYPTO_THREAD_unlock(b->lock);
- }
- /* Threadsafe */
- static int dgram_pair_read(BIO *bio, char *buf, int sz_)
- {
- int ret;
- ossl_ssize_t l;
- struct bio_dgram_pair_st *b = bio->ptr, *peerb;
- if (sz_ < 0) {
- ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
- return -1;
- }
- if (b->peer == NULL) {
- ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
- return -1;
- }
- peerb = b->peer->ptr;
- /*
- * For BIO_read we have to acquire both locks because we touch the retry
- * flags on the local bio. (This is avoided in the recvmmsg case as it does
- * not touch the retry flags.)
- */
- if (dgram_pair_lock_both_write(peerb, b) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
- return -1;
- }
- l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
- if (l < 0) {
- if (l != -BIO_R_NON_FATAL)
- ERR_raise(ERR_LIB_BIO, -l);
- ret = -1;
- } else {
- ret = (int)l;
- }
- dgram_pair_unlock_both(peerb, b);
- return ret;
- }
- /* Threadsafe */
- static int dgram_pair_recvmmsg(BIO *bio, BIO_MSG *msg,
- size_t stride, size_t num_msg,
- uint64_t flags,
- size_t *num_processed)
- {
- int ret;
- ossl_ssize_t l;
- BIO_MSG *m;
- size_t i;
- struct bio_dgram_pair_st *b = bio->ptr, *readb;
- if (num_msg == 0) {
- *num_processed = 0;
- return 1;
- }
- if (!bio->init) {
- ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
- *num_processed = 0;
- return 0;
- }
- if (is_dgram_pair(b))
- readb = b->peer->ptr;
- else
- readb = b;
- if (CRYPTO_THREAD_write_lock(readb->lock) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
- *num_processed = 0;
- return 0;
- }
- for (i = 0; i < num_msg; ++i) {
- m = &BIO_MSG_N(msg, i);
- l = dgram_pair_read_actual(bio, m->data, m->data_len,
- m->local, m->peer, 1);
- if (l < 0) {
- *num_processed = i;
- if (i > 0) {
- ret = 1;
- } else {
- ERR_raise(ERR_LIB_BIO, -l);
- ret = 0;
- }
- goto out;
- }
- m->data_len = l;
- m->flags = 0;
- }
- *num_processed = i;
- ret = 1;
- out:
- CRYPTO_THREAD_unlock(readb->lock);
- return ret;
- }
- /* Threadsafe */
- static int dgram_mem_read(BIO *bio, char *buf, int sz_)
- {
- int ret;
- ossl_ssize_t l;
- struct bio_dgram_pair_st *b = bio->ptr;
- if (sz_ < 0) {
- ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
- return -1;
- }
- if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
- return -1;
- }
- l = dgram_pair_read_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
- if (l < 0) {
- if (l != -BIO_R_NON_FATAL)
- ERR_raise(ERR_LIB_BIO, -l);
- ret = -1;
- } else {
- ret = (int)l;
- }
- CRYPTO_THREAD_unlock(b->lock);
- return ret;
- }
- /*
- * Calculate the array growth based on the target size.
- *
- * The growth factor is a rational number and is defined by a numerator
- * and a denominator. According to Andrew Koenig in his paper "Why Are
- * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
- * than the golden ratio (1.618...).
- *
- * We use an expansion factor of 8 / 5 = 1.6
- */
- static const size_t max_rbuf_size = SIZE_MAX / 2; /* unlimited in practice */
- static ossl_inline size_t compute_rbuf_growth(size_t target, size_t current)
- {
- int err = 0;
- while (current < target) {
- if (current >= max_rbuf_size)
- return 0;
- current = safe_muldiv_size_t(current, 8, 5, &err);
- if (err)
- return 0;
- if (current >= max_rbuf_size)
- current = max_rbuf_size;
- }
- return current;
- }
- /* Must hold local write lock */
- static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b,
- const uint8_t *buf, size_t sz)
- {
- size_t total_written = 0;
- /*
- * We repeat pushes to the ring buffer for as long as we have data until we
- * fail. We may not be able to push in one operation if the ring buffer
- * wraps around, but there may still be more room for data.
- */
- while (sz > 0) {
- size_t dst_len;
- uint8_t *dst_buf;
- /*
- * There are two BIO instances, each with a ringbuf. We write to our own
- * ringbuf and read from the peer ringbuf.
- */
- ring_buf_head(&b->rbuf, &dst_buf, &dst_len);
- if (dst_len == 0) {
- size_t new_len;
- if (!b->grows_on_write) /* resize only if size not set explicitly */
- break;
- /* increase the size */
- new_len = compute_rbuf_growth(b->req_buf_len + sz, b->req_buf_len);
- if (new_len == 0 || !ring_buf_resize(&b->rbuf, new_len))
- break;
- b->req_buf_len = new_len;
- }
- if (dst_len > sz)
- dst_len = sz;
- memcpy(dst_buf, buf, dst_len);
- ring_buf_push(&b->rbuf, dst_len);
- buf += dst_len;
- sz -= dst_len;
- total_written += dst_len;
- }
- return total_written;
- }
- /*
- * Must hold local write lock. Returns number of bytes processed or negated BIO
- * response code.
- */
- static ossl_ssize_t dgram_pair_write_actual(BIO *bio, const char *buf, size_t sz,
- const BIO_ADDR *local, const BIO_ADDR *peer,
- int is_multi)
- {
- static const BIO_ADDR zero_addr;
- size_t saved_idx, saved_count;
- struct bio_dgram_pair_st *b = bio->ptr, *readb;
- struct dgram_hdr hdr = {0};
- if (!is_multi)
- BIO_clear_retry_flags(bio);
- if (!bio->init)
- return -BIO_R_UNINITIALIZED;
- if (!ossl_assert(b != NULL && b->rbuf.start != NULL))
- return -BIO_R_TRANSFER_ERROR;
- if (sz > 0 && buf == NULL)
- return -BIO_R_INVALID_ARGUMENT;
- if (local != NULL && b->local_addr_enable == 0)
- return -BIO_R_LOCAL_ADDR_NOT_AVAILABLE;
- if (is_dgram_pair(b))
- readb = b->peer->ptr;
- else
- readb = b;
- if (peer != NULL && (readb->cap & BIO_DGRAM_CAP_HANDLES_DST_ADDR) == 0)
- return -BIO_R_PEER_ADDR_NOT_AVAILABLE;
- hdr.len = sz;
- hdr.dst_addr = (peer != NULL ? *peer : zero_addr);
- hdr.src_addr = (local != NULL ? *local : zero_addr);
- saved_idx = b->rbuf.idx[0];
- saved_count = b->rbuf.count;
- if (dgram_pair_write_inner(b, (const uint8_t *)&hdr, sizeof(hdr)) != sizeof(hdr)
- || dgram_pair_write_inner(b, (const uint8_t *)buf, sz) != sz) {
- /*
- * We were not able to push the header and the entirety of the payload
- * onto the ring buffer, so abort and roll back the ring buffer state.
- */
- b->rbuf.idx[0] = saved_idx;
- b->rbuf.count = saved_count;
- if (!is_multi)
- BIO_set_retry_write(bio);
- return -BIO_R_NON_FATAL;
- }
- return sz;
- }
- /* Threadsafe */
- static int dgram_pair_write(BIO *bio, const char *buf, int sz_)
- {
- int ret;
- ossl_ssize_t l;
- struct bio_dgram_pair_st *b = bio->ptr;
- if (sz_ < 0) {
- ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
- return -1;
- }
- if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
- return -1;
- }
- l = dgram_pair_write_actual(bio, buf, (size_t)sz_, NULL, NULL, 0);
- if (l < 0) {
- ERR_raise(ERR_LIB_BIO, -l);
- ret = -1;
- } else {
- ret = (int)l;
- }
- CRYPTO_THREAD_unlock(b->lock);
- return ret;
- }
- /* Threadsafe */
- static int dgram_pair_sendmmsg(BIO *bio, BIO_MSG *msg,
- size_t stride, size_t num_msg,
- uint64_t flags, size_t *num_processed)
- {
- ossl_ssize_t ret, l;
- BIO_MSG *m;
- size_t i;
- struct bio_dgram_pair_st *b = bio->ptr;
- if (num_msg == 0) {
- *num_processed = 0;
- return 1;
- }
- if (CRYPTO_THREAD_write_lock(b->lock) == 0) {
- ERR_raise(ERR_LIB_BIO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
- *num_processed = 0;
- return 0;
- }
- for (i = 0; i < num_msg; ++i) {
- m = &BIO_MSG_N(msg, i);
- l = dgram_pair_write_actual(bio, m->data, m->data_len,
- m->local, m->peer, 1);
- if (l < 0) {
- *num_processed = i;
- if (i > 0) {
- ret = 1;
- } else {
- ERR_raise(ERR_LIB_BIO, -l);
- ret = 0;
- }
- goto out;
- }
- m->flags = 0;
- }
- *num_processed = i;
- ret = 1;
- out:
- CRYPTO_THREAD_unlock(b->lock);
- return ret;
- }
- #endif
|