diff --git a/include/re_tls.h b/include/re_tls.h index 6bfc0f2..af77c78 100644 --- a/include/re_tls.h +++ b/include/re_tls.h @@ -63,6 +63,7 @@ int dtls_listen(struct dtls_sock **sockp, const struct sa *laddr, struct udp_sock *us, uint32_t htsize, int layer, dtls_conn_h *connh, void *arg); struct udp_sock *dtls_udp_sock(struct dtls_sock *sock); +void dtls_set_mtu(struct dtls_sock *sock, size_t mtu); int dtls_connect(struct tls_conn **ptc, struct tls *tls, struct dtls_sock *sock, const struct sa *peer, dtls_estab_h *estabh, dtls_recv_h *recvh, diff --git a/src/tls/openssl/tls_udp.c b/src/tls/openssl/tls_udp.c index 91947a6..c437d43 100644 --- a/src/tls/openssl/tls_udp.c +++ b/src/tls/openssl/tls_udp.c @@ -25,6 +25,12 @@ #include +enum { + MTU_DEFAULT = 1400, + MTU_FALLBACK = 548, +}; + + struct dtls_sock { struct sa peer; struct udp_helper *uh; @@ -33,6 +39,7 @@ struct dtls_sock { struct mbuf *mb; dtls_conn_h *connh; void *arg; + size_t mtu; }; @@ -103,13 +110,21 @@ static int bio_write(BIO *b, const char *buf, int len) static long bio_ctrl(BIO *b, int cmd, long num, void *ptr) { - (void)b; + struct tls_conn *tc = b->ptr; (void)num; (void)ptr; - if (cmd == BIO_CTRL_FLUSH) { + switch (cmd) { + + case BIO_CTRL_FLUSH: /* The OpenSSL library needs this */ return 1; + + case BIO_CTRL_DGRAM_QUERY_MTU: + return tc ? tc->sock->mtu : MTU_DEFAULT; + + case BIO_CTRL_DGRAM_GET_FALLBACK_MTU: + return MTU_FALLBACK; } return 0; @@ -706,6 +721,7 @@ int dtls_listen(struct dtls_sock **sockp, const struct sa *laddr, if (err) goto out; + sock->mtu = MTU_DEFAULT; sock->connh = connh; sock->arg = arg; @@ -730,3 +746,18 @@ struct udp_sock *dtls_udp_sock(struct dtls_sock *sock) { return sock ? sock->us : NULL; } + + +/** + * Set MTU on a DTLS Socket + * + * @param sock DTLS Socket + * @param mtu MTU value + */ +void dtls_set_mtu(struct dtls_sock *sock, size_t mtu) +{ + if (!sock) + return; + + sock->mtu = mtu; +}