2 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
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
17 #include "bio_local.h"
18 #ifndef OPENSSL_NO_DGRAM
20 # ifndef OPENSSL_NO_SCTP
21 # include <netinet/sctp.h>
23 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
24 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
27 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
28 # define IP_MTU 14 /* linux is lame */
31 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
32 # define IPPROTO_IPV6 41 /* windows is lame */
35 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
36 /* Standard definition causes type-punning problems. */
37 # undef IN6_IS_ADDR_V4MAPPED
38 # define s6_addr32 __u6_addr.__u6_addr32
39 # define IN6_IS_ADDR_V4MAPPED(a) \
40 (((a)->s6_addr32[0] == 0) && \
41 ((a)->s6_addr32[1] == 0) && \
42 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
45 /* Determine what method to use for BIO_sendmmsg and BIO_recvmmsg. */
46 # define M_METHOD_NONE 0
47 # define M_METHOD_RECVMMSG 1
48 # define M_METHOD_RECVMSG 2
49 # define M_METHOD_RECVFROM 3
50 # define M_METHOD_WSARECVMSG 4
52 # if !defined(M_METHOD)
53 # if defined(OPENSSL_SYS_WINDOWS) && defined(BIO_HAVE_WSAMSG) && !defined(NO_WSARECVMSG)
54 # define M_METHOD M_METHOD_WSARECVMSG
55 # elif !defined(OPENSSL_SYS_WINDOWS) && defined(MSG_WAITFORONE) && !defined(NO_RECVMMSG)
56 # define M_METHOD M_METHOD_RECVMMSG
57 # elif !defined(OPENSSL_SYS_WINDOWS) && defined(CMSG_LEN) && !defined(NO_RECVMSG)
58 # define M_METHOD M_METHOD_RECVMSG
59 # elif !defined(NO_RECVFROM)
60 # define M_METHOD M_METHOD_RECVFROM
62 # define M_METHOD M_METHOD_NONE
66 # if defined(OPENSSL_SYS_WINDOWS)
67 # define BIO_CMSG_SPACE(x) WSA_CMSG_SPACE(x)
68 # define BIO_CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
69 # define BIO_CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
70 # define BIO_CMSG_DATA(x) WSA_CMSG_DATA(x)
71 # define BIO_CMSG_LEN(x) WSA_CMSG_LEN(x)
72 # define MSGHDR_TYPE WSAMSG
73 # define CMSGHDR_TYPE WSACMSGHDR
75 # define MSGHDR_TYPE struct msghdr
76 # define CMSGHDR_TYPE struct cmsghdr
77 # define BIO_CMSG_SPACE(x) CMSG_SPACE(x)
78 # define BIO_CMSG_FIRSTHDR(x) CMSG_FIRSTHDR(x)
79 # define BIO_CMSG_NXTHDR(x, y) CMSG_NXTHDR(x, y)
80 # define BIO_CMSG_DATA(x) CMSG_DATA(x)
81 # define BIO_CMSG_LEN(x) CMSG_LEN(x)
84 # if M_METHOD == M_METHOD_RECVMMSG \
85 || M_METHOD == M_METHOD_RECVMSG \
86 || M_METHOD == M_METHOD_WSARECVMSG
87 # if defined(__APPLE__)
89 * CMSG_SPACE is not a constant expresson on OSX even though POSIX
90 * says it's supposed to be. This should be adequate.
92 # define BIO_CMSG_ALLOC_LEN 64
94 # if defined(IPV6_PKTINFO)
95 # define BIO_CMSG_ALLOC_LEN_1 BIO_CMSG_SPACE(sizeof(struct in6_pktinfo))
97 # define BIO_CMSG_ALLOC_LEN_1 0
99 # if defined(IP_PKTINFO)
100 # define BIO_CMSG_ALLOC_LEN_2 BIO_CMSG_SPACE(sizeof(struct in_pktinfo))
102 # define BIO_CMSG_ALLOC_LEN_2 0
104 # if defined(IP_RECVDSTADDR)
105 # define BIO_CMSG_ALLOC_LEN_3 BIO_CMSG_SPACE(sizeof(struct in_addr))
107 # define BIO_CMSG_ALLOC_LEN_3 0
109 # define BIO_MAX(X,Y) ((X) > (Y) ? (X) : (Y))
110 # define BIO_CMSG_ALLOC_LEN \
111 BIO_MAX(BIO_CMSG_ALLOC_LEN_1, \
112 BIO_MAX(BIO_CMSG_ALLOC_LEN_2, BIO_CMSG_ALLOC_LEN_3))
114 # if (defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)) && defined(IPV6_RECVPKTINFO)
115 # define SUPPORT_LOCAL_ADDR
119 # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
121 static int dgram_write(BIO
*h
, const char *buf
, int num
);
122 static int dgram_read(BIO
*h
, char *buf
, int size
);
123 static int dgram_puts(BIO
*h
, const char *str
);
124 static long dgram_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
125 static int dgram_new(BIO
*h
);
126 static int dgram_free(BIO
*data
);
127 static int dgram_clear(BIO
*bio
);
128 static int dgram_sendmmsg(BIO
*b
, BIO_MSG
*msg
,
129 size_t stride
, size_t num_msg
,
130 uint64_t flags
, size_t *num_processed
);
131 static int dgram_recvmmsg(BIO
*b
, BIO_MSG
*msg
,
132 size_t stride
, size_t num_msg
,
133 uint64_t flags
, size_t *num_processed
);
135 # ifndef OPENSSL_NO_SCTP
136 static int dgram_sctp_write(BIO
*h
, const char *buf
, int num
);
137 static int dgram_sctp_read(BIO
*h
, char *buf
, int size
);
138 static int dgram_sctp_puts(BIO
*h
, const char *str
);
139 static long dgram_sctp_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
140 static int dgram_sctp_new(BIO
*h
);
141 static int dgram_sctp_free(BIO
*data
);
142 static int dgram_sctp_wait_for_dry(BIO
*b
);
143 static int dgram_sctp_msg_waiting(BIO
*b
);
144 # ifdef SCTP_AUTHENTICATION_EVENT
145 static void dgram_sctp_handle_auth_free_key_event(BIO
*b
, union sctp_notification
150 static int BIO_dgram_should_retry(int s
);
152 static void get_current_time(struct timeval
*t
);
154 static const BIO_METHOD methods_dgramp
= {
162 NULL
, /* dgram_gets, */
166 NULL
, /* dgram_callback_ctrl */
171 # ifndef OPENSSL_NO_SCTP
172 static const BIO_METHOD methods_dgramp_sctp
= {
174 "datagram sctp socket",
180 NULL
, /* dgram_gets, */
184 NULL
, /* dgram_callback_ctrl */
190 typedef struct bio_dgram_data_st
{
193 unsigned int connected
;
196 struct timeval next_timeout
;
197 struct timeval socket_timeout
;
198 unsigned int peekmode
;
199 char local_addr_enabled
;
202 # ifndef OPENSSL_NO_SCTP
203 typedef struct bio_dgram_sctp_save_message_st
{
207 } bio_dgram_sctp_save_message
;
209 typedef struct bio_dgram_sctp_data_st
{
211 unsigned int connected
;
214 struct bio_dgram_sctp_sndinfo sndinfo
;
215 struct bio_dgram_sctp_rcvinfo rcvinfo
;
216 struct bio_dgram_sctp_prinfo prinfo
;
217 BIO_dgram_sctp_notification_handler_fn handle_notifications
;
218 void *notification_context
;
223 int peer_auth_tested
;
224 } bio_dgram_sctp_data
;
227 const BIO_METHOD
*BIO_s_datagram(void)
229 return &methods_dgramp
;
232 BIO
*BIO_new_dgram(int fd
, int close_flag
)
236 ret
= BIO_new(BIO_s_datagram());
239 BIO_set_fd(ret
, fd
, close_flag
);
243 static int dgram_new(BIO
*bi
)
245 bio_dgram_data
*data
= OPENSSL_zalloc(sizeof(*data
));
253 static int dgram_free(BIO
*a
)
255 bio_dgram_data
*data
;
262 data
= (bio_dgram_data
*)a
->ptr
;
268 static int dgram_clear(BIO
*a
)
274 BIO_closesocket(a
->num
);
282 static void dgram_adjust_rcv_timeout(BIO
*b
)
284 # if defined(SO_RCVTIMEO)
285 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
287 /* Is a timer active? */
288 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
289 struct timeval timenow
, timeleft
;
291 /* Read current socket timeout */
292 # ifdef OPENSSL_SYS_WINDOWS
295 int sz
= sizeof(timeout
);
296 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
297 (void *)&timeout
, &sz
) < 0) {
298 perror("getsockopt");
300 data
->socket_timeout
.tv_sec
= timeout
/ 1000;
301 data
->socket_timeout
.tv_usec
= (timeout
% 1000) * 1000;
304 socklen_t sz
= sizeof(data
->socket_timeout
);
305 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
306 &(data
->socket_timeout
), &sz
) < 0) {
307 perror("getsockopt");
309 OPENSSL_assert(sz
<= sizeof(data
->socket_timeout
));
312 /* Get current time */
313 get_current_time(&timenow
);
315 /* Calculate time left until timer expires */
316 memcpy(&timeleft
, &(data
->next_timeout
), sizeof(struct timeval
));
317 if (timeleft
.tv_usec
< timenow
.tv_usec
) {
318 timeleft
.tv_usec
= 1000000 - timenow
.tv_usec
+ timeleft
.tv_usec
;
321 timeleft
.tv_usec
-= timenow
.tv_usec
;
323 if (timeleft
.tv_sec
< timenow
.tv_sec
) {
325 timeleft
.tv_usec
= 1;
327 timeleft
.tv_sec
-= timenow
.tv_sec
;
331 * Adjust socket timeout if next handshake message timer will expire
334 if ((data
->socket_timeout
.tv_sec
== 0
335 && data
->socket_timeout
.tv_usec
== 0)
336 || (data
->socket_timeout
.tv_sec
> timeleft
.tv_sec
)
337 || (data
->socket_timeout
.tv_sec
== timeleft
.tv_sec
338 && data
->socket_timeout
.tv_usec
>= timeleft
.tv_usec
)) {
339 # ifdef OPENSSL_SYS_WINDOWS
340 timeout
= timeleft
.tv_sec
* 1000 + timeleft
.tv_usec
/ 1000;
341 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
342 (void *)&timeout
, sizeof(timeout
)) < 0) {
343 perror("setsockopt");
346 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &timeleft
,
347 sizeof(struct timeval
)) < 0) {
348 perror("setsockopt");
356 static void dgram_update_local_addr(BIO
*b
)
358 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
359 socklen_t addr_len
= sizeof(data
->local_addr
);
361 if (getsockname(b
->num
, &data
->local_addr
.sa
, &addr_len
) < 0)
363 * This should not be possible, but zero-initialize and return
366 BIO_ADDR_clear(&data
->local_addr
);
369 # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
370 static int dgram_get_sock_family(BIO
*b
)
372 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
373 return data
->local_addr
.sa
.sa_family
;
377 static void dgram_reset_rcv_timeout(BIO
*b
)
379 # if defined(SO_RCVTIMEO)
380 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
382 /* Is a timer active? */
383 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
384 # ifdef OPENSSL_SYS_WINDOWS
385 int timeout
= data
->socket_timeout
.tv_sec
* 1000 +
386 data
->socket_timeout
.tv_usec
/ 1000;
387 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
388 (void *)&timeout
, sizeof(timeout
)) < 0) {
389 perror("setsockopt");
393 (b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &(data
->socket_timeout
),
394 sizeof(struct timeval
)) < 0) {
395 perror("setsockopt");
402 static int dgram_read(BIO
*b
, char *out
, int outl
)
405 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
409 socklen_t len
= sizeof(peer
);
412 clear_socket_error();
413 BIO_ADDR_clear(&peer
);
414 dgram_adjust_rcv_timeout(b
);
417 ret
= recvfrom(b
->num
, out
, outl
, flags
,
418 BIO_ADDR_sockaddr_noconst(&peer
), &len
);
420 if (!data
->connected
&& ret
>= 0)
421 BIO_ctrl(b
, BIO_CTRL_DGRAM_SET_PEER
, 0, &peer
);
423 BIO_clear_retry_flags(b
);
425 if (BIO_dgram_should_retry(ret
)) {
426 BIO_set_retry_read(b
);
427 data
->_errno
= get_last_socket_error();
431 dgram_reset_rcv_timeout(b
);
436 static int dgram_write(BIO
*b
, const char *in
, int inl
)
439 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
440 clear_socket_error();
443 ret
= writesocket(b
->num
, in
, inl
);
445 int peerlen
= BIO_ADDR_sockaddr_size(&data
->peer
);
447 ret
= sendto(b
->num
, in
, inl
, 0,
448 BIO_ADDR_sockaddr(&data
->peer
), peerlen
);
451 BIO_clear_retry_flags(b
);
453 if (BIO_dgram_should_retry(ret
)) {
454 BIO_set_retry_write(b
);
455 data
->_errno
= get_last_socket_error();
461 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
465 switch (BIO_ADDR_family(&data
->peer
)) {
468 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
472 # if OPENSSL_USE_IPV6
475 # ifdef IN6_IS_ADDR_V4MAPPED
476 struct in6_addr tmp_addr
;
477 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
478 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
480 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
486 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
493 /* We don't know. Go with the historical default */
500 /* Enables appropriate destination address reception option on the socket. */
501 # if defined(SUPPORT_LOCAL_ADDR)
502 static int enable_local_addr(BIO
*b
, int enable
) {
503 int af
= dgram_get_sock_family(b
);
506 # if defined(IP_PKTINFO)
507 /* IP_PKTINFO is preferred */
508 if (setsockopt(b
->num
, IPPROTO_IP
, IP_PKTINFO
,
509 (void *)&enable
, sizeof(enable
)) < 0)
514 # elif defined(IP_RECVDSTADDR)
515 /* Fall back to IP_RECVDSTADDR */
517 if (setsockopt(b
->num
, IPPROTO_IP
, IP_RECVDSTADDR
,
518 &enable
, sizeof(enable
)) < 0)
525 # if OPENSSL_USE_IPV6
526 if (af
== AF_INET6
) {
527 # if defined(IPV6_RECVPKTINFO)
528 if (setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
529 &enable
, sizeof(enable
)) < 0)
541 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
545 bio_dgram_data
*data
= NULL
;
548 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
549 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
550 * modern enough to define socklen_t */
555 data
= (bio_dgram_data
*)b
->ptr
;
567 b
->num
= *((int *)ptr
);
568 b
->shutdown
= (int)num
;
570 dgram_update_local_addr(b
);
571 # if defined(SUPPORT_LOCAL_ADDR)
572 if (data
->local_addr_enabled
) {
573 if (enable_local_addr(b
, 1) < 1)
574 data
->local_addr_enabled
= 0;
587 case BIO_CTRL_GET_CLOSE
:
590 case BIO_CTRL_SET_CLOSE
:
591 b
->shutdown
= (int)num
;
593 case BIO_CTRL_PENDING
:
594 case BIO_CTRL_WPENDING
:
601 case BIO_CTRL_DGRAM_CONNECT
:
602 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
604 /* (Linux)kernel sets DF bit on outgoing IP packets */
605 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
606 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
607 addr_len
= (socklen_t
) sizeof(addr
);
608 BIO_ADDR_clear(&addr
);
609 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
613 switch (addr
.sa
.sa_family
) {
615 sockopt_val
= IP_PMTUDISC_DO
;
616 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
617 &sockopt_val
, sizeof(sockopt_val
))) < 0)
618 perror("setsockopt");
620 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
622 sockopt_val
= IPV6_PMTUDISC_DO
;
623 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
624 &sockopt_val
, sizeof(sockopt_val
))) < 0)
625 perror("setsockopt");
636 case BIO_CTRL_DGRAM_QUERY_MTU
:
637 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
638 addr_len
= (socklen_t
) sizeof(addr
);
639 BIO_ADDR_clear(&addr
);
640 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
644 sockopt_len
= sizeof(sockopt_val
);
645 switch (addr
.sa
.sa_family
) {
648 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
649 &sockopt_len
)) < 0 || sockopt_val
< 0) {
653 * we assume that the transport protocol is UDP and no IP
656 data
->mtu
= sockopt_val
- 8 - 20;
660 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
663 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
664 (void *)&sockopt_val
, &sockopt_len
)) < 0
665 || sockopt_val
< 0) {
669 * we assume that the transport protocol is UDP and no IPV6
672 data
->mtu
= sockopt_val
- 8 - 40;
685 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
686 ret
= -dgram_get_mtu_overhead(data
);
687 switch (BIO_ADDR_family(&data
->peer
)) {
691 # if OPENSSL_USE_IPV6
694 # ifdef IN6_IS_ADDR_V4MAPPED
695 struct in6_addr tmp_addr
;
696 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
697 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
710 case BIO_CTRL_DGRAM_GET_MTU
:
712 case BIO_CTRL_DGRAM_SET_MTU
:
716 case BIO_CTRL_DGRAM_SET_CONNECTED
:
719 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
722 BIO_ADDR_clear(&data
->peer
);
725 case BIO_CTRL_DGRAM_GET_PEER
:
726 ret
= BIO_ADDR_sockaddr_size(&data
->peer
);
727 /* FIXME: if num < ret, we will only return part of an address.
728 That should bee an error, no? */
729 if (num
== 0 || num
> ret
)
731 memcpy(ptr
, &data
->peer
, (ret
= num
));
733 case BIO_CTRL_DGRAM_SET_PEER
:
734 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
736 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
737 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
739 # if defined(SO_RCVTIMEO)
740 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
741 # ifdef OPENSSL_SYS_WINDOWS
743 struct timeval
*tv
= (struct timeval
*)ptr
;
744 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
745 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
746 (void *)&timeout
, sizeof(timeout
)) < 0) {
747 perror("setsockopt");
752 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
753 sizeof(struct timeval
)) < 0) {
754 perror("setsockopt");
759 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
761 # ifdef OPENSSL_SYS_WINDOWS
764 struct timeval
*tv
= (struct timeval
*)ptr
;
766 sz
= sizeof(timeout
);
767 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
768 (void *)&timeout
, &sz
) < 0) {
769 perror("getsockopt");
772 tv
->tv_sec
= timeout
/ 1000;
773 tv
->tv_usec
= (timeout
% 1000) * 1000;
777 socklen_t sz
= sizeof(struct timeval
);
778 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
780 perror("getsockopt");
783 OPENSSL_assert(sz
<= sizeof(struct timeval
));
790 # if defined(SO_SNDTIMEO)
791 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
792 # ifdef OPENSSL_SYS_WINDOWS
794 struct timeval
*tv
= (struct timeval
*)ptr
;
795 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
796 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
797 (void *)&timeout
, sizeof(timeout
)) < 0) {
798 perror("setsockopt");
803 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
804 sizeof(struct timeval
)) < 0) {
805 perror("setsockopt");
810 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
812 # ifdef OPENSSL_SYS_WINDOWS
815 struct timeval
*tv
= (struct timeval
*)ptr
;
817 sz
= sizeof(timeout
);
818 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
819 (void *)&timeout
, &sz
) < 0) {
820 perror("getsockopt");
823 tv
->tv_sec
= timeout
/ 1000;
824 tv
->tv_usec
= (timeout
% 1000) * 1000;
828 socklen_t sz
= sizeof(struct timeval
);
829 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
831 perror("getsockopt");
834 OPENSSL_assert(sz
<= sizeof(struct timeval
));
841 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
843 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
844 # ifdef OPENSSL_SYS_WINDOWS
845 d_errno
= (data
->_errno
== WSAETIMEDOUT
);
847 d_errno
= (data
->_errno
== EAGAIN
);
856 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
857 if (data
->_errno
== EMSGSIZE
) {
864 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
865 sockopt_val
= num
? 1 : 0;
867 switch (data
->peer
.sa
.sa_family
) {
869 # if defined(IP_DONTFRAG)
870 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
871 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
872 perror("setsockopt");
875 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
876 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
877 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
878 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
879 perror("setsockopt");
882 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
883 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
884 (const char *)&sockopt_val
,
885 sizeof(sockopt_val
))) < 0) {
886 perror("setsockopt");
893 # if OPENSSL_USE_IPV6
895 # if defined(IPV6_DONTFRAG)
896 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
897 (const void *)&sockopt_val
,
898 sizeof(sockopt_val
))) < 0) {
899 perror("setsockopt");
902 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
903 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
904 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
905 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
906 perror("setsockopt");
919 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
920 ret
= dgram_get_mtu_overhead(data
);
924 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
925 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
926 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
927 * value has been updated to a non-clashing value. However to preserve
928 * binary compatibility we now respond to both the old value and the new one
930 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
931 case BIO_CTRL_DGRAM_SET_PEEK_MODE
:
932 data
->peekmode
= (unsigned int)num
;
935 case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP
:
936 # if defined(SUPPORT_LOCAL_ADDR)
943 case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE
:
944 # if defined(SUPPORT_LOCAL_ADDR)
946 if (num
!= data
->local_addr_enabled
) {
947 if (enable_local_addr(b
, num
) < 1) {
952 data
->local_addr_enabled
= (char)num
;
959 case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE
:
960 *(int *)ptr
= data
->local_addr_enabled
;
970 static int dgram_puts(BIO
*bp
, const char *str
)
975 ret
= dgram_write(bp
, str
, n
);
979 # if M_METHOD == M_METHOD_WSARECVMSG
980 static void translate_msg_win(BIO
*b
, WSAMSG
*mh
, WSABUF
*iov
,
981 unsigned char *control
, BIO_MSG
*msg
)
983 iov
->len
= msg
->data_len
;
984 iov
->buf
= msg
->data
;
986 /* Windows requires namelen to be set exactly */
987 mh
->name
= msg
->peer
!= NULL
? &msg
->peer
->sa
: NULL
;
988 if (msg
->peer
!= NULL
&& dgram_get_sock_family(b
) == AF_INET
)
989 mh
->namelen
= sizeof(struct sockaddr_in
);
990 # if OPENSSL_USE_IPV6
991 else if (msg
->peer
!= NULL
&& dgram_get_sock_family(b
) == AF_INET6
)
992 mh
->namelen
= sizeof(struct sockaddr_in6
);
998 * When local address reception (IP_PKTINFO, etc.) is enabled, on Windows
999 * this causes WSARecvMsg to fail if the control buffer is too small to hold
1000 * the structure, or if no control buffer is passed. So we need to give it
1001 * the control buffer even if we aren't actually going to examine the
1004 mh
->lpBuffers
= iov
;
1005 mh
->dwBufferCount
= 1;
1006 mh
->Control
.len
= BIO_CMSG_ALLOC_LEN
;
1007 mh
->Control
.buf
= control
;
1012 # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG
1013 /* Translates a BIO_MSG to a msghdr and iovec. */
1014 static void translate_msg(BIO
*b
, struct msghdr
*mh
, struct iovec
*iov
,
1015 unsigned char *control
, BIO_MSG
*msg
)
1017 iov
->iov_base
= msg
->data
;
1018 iov
->iov_len
= msg
->data_len
;
1020 /* macOS requires msg_namelen be 0 if msg_name is NULL */
1021 mh
->msg_name
= msg
->peer
!= NULL
? &msg
->peer
->sa
: NULL
;
1022 if (msg
->peer
!= NULL
&& dgram_get_sock_family(b
) == AF_INET
)
1023 mh
->msg_namelen
= sizeof(struct sockaddr_in
);
1024 # if OPENSSL_USE_IPV6
1025 else if (msg
->peer
!= NULL
&& dgram_get_sock_family(b
) == AF_INET6
)
1026 mh
->msg_namelen
= sizeof(struct sockaddr_in6
);
1029 mh
->msg_namelen
= 0;
1033 mh
->msg_control
= msg
->local
!= NULL
? control
: NULL
;
1034 mh
->msg_controllen
= msg
->local
!= NULL
? BIO_CMSG_ALLOC_LEN
: 0;
1039 # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
1040 /* Extracts destination address from the control buffer. */
1041 static int extract_local(BIO
*b
, MSGHDR_TYPE
*mh
, BIO_ADDR
*local
) {
1042 # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1044 int af
= dgram_get_sock_family(b
);
1046 for (cmsg
= BIO_CMSG_FIRSTHDR(mh
); cmsg
!= NULL
;
1047 cmsg
= BIO_CMSG_NXTHDR(mh
, cmsg
)) {
1048 if (af
== AF_INET
) {
1049 if (cmsg
->cmsg_level
!= IPPROTO_IP
)
1052 # if defined(IP_PKTINFO)
1053 if (cmsg
->cmsg_type
!= IP_PKTINFO
)
1056 local
->s_in
.sin_addr
=
1057 ((struct in_pktinfo
*)BIO_CMSG_DATA(cmsg
))->ipi_addr
;
1059 # elif defined(IP_RECVDSTADDR)
1060 if (cmsg
->cmsg_type
!= IP_RECVDSTADDR
)
1063 local
->s_in
.sin_addr
= *(struct in_addr
*)BIO_CMSG_DATA(cmsg
);
1066 # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
1068 bio_dgram_data
*data
= b
->ptr
;
1070 local
->s_in
.sin_family
= AF_INET
;
1071 local
->s_in
.sin_port
= data
->local_addr
.s_in
.sin_port
;
1076 # if OPENSSL_USE_IPV6
1077 else if (af
== AF_INET6
) {
1078 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
)
1081 # if defined(IPV6_RECVPKTINFO)
1082 if (cmsg
->cmsg_type
!= IPV6_PKTINFO
)
1086 bio_dgram_data
*data
= b
->ptr
;
1088 local
->s_in6
.sin6_addr
=
1089 ((struct in6_pktinfo
*)BIO_CMSG_DATA(cmsg
))->ipi6_addr
;
1090 local
->s_in6
.sin6_family
= AF_INET6
;
1091 local
->s_in6
.sin6_port
= data
->local_addr
.s_in6
.sin6_port
;
1092 local
->s_in6
.sin6_scope_id
=
1093 data
->local_addr
.s_in6
.sin6_scope_id
;
1094 local
->s_in6
.sin6_flowinfo
= 0;
1106 static int pack_local(BIO
*b
, MSGHDR_TYPE
*mh
, const BIO_ADDR
*local
) {
1107 int af
= dgram_get_sock_family(b
);
1108 # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1110 bio_dgram_data
*data
= b
->ptr
;
1113 if (af
== AF_INET
) {
1114 # if defined(IP_PKTINFO)
1115 struct in_pktinfo
*info
;
1117 # if defined(OPENSSL_SYS_WINDOWS)
1118 cmsg
= (CMSGHDR_TYPE
*)mh
->Control
.buf
;
1120 cmsg
= (CMSGHDR_TYPE
*)mh
->msg_control
;
1123 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in_pktinfo
));
1124 cmsg
->cmsg_level
= IPPROTO_IP
;
1125 cmsg
->cmsg_type
= IP_PKTINFO
;
1127 info
= (struct in_pktinfo
*)BIO_CMSG_DATA(cmsg
);
1128 # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_CYGWIN)
1129 info
->ipi_spec_dst
= local
->s_in
.sin_addr
;
1131 info
->ipi_addr
.s_addr
= 0;
1132 info
->ipi_ifindex
= 0;
1135 * We cannot override source port using this API, therefore
1136 * ensure the application specified a source port of 0
1137 * or the one we are bound to. (Better to error than silently
1140 if (local
->s_in
.sin_port
!= 0
1141 && data
->local_addr
.s_in
.sin_port
!= local
->s_in
.sin_port
) {
1142 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1146 # if defined(OPENSSL_SYS_WINDOWS)
1147 mh
->Control
.len
= BIO_CMSG_SPACE(sizeof(struct in_pktinfo
));
1149 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in_pktinfo
));
1153 # elif defined(IP_SENDSRCADDR)
1154 struct in_addr
*info
;
1156 cmsg
= (struct cmsghdr
*)mh
->msg_control
;
1157 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in_addr
));
1158 cmsg
->cmsg_level
= IPPROTO_IP
;
1159 cmsg
->cmsg_type
= IP_SENDSRCADDR
;
1161 info
= (struct in_addr
*)BIO_CMSG_DATA(cmsg
);
1162 *info
= local
->s_in
.sin_addr
;
1164 /* See comment above. */
1165 if (local
->s_in
.sin_port
!= 0
1166 && data
->local_addr
.s_in
.sin_port
!= local
->s_in
.sin_port
) {
1167 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1171 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in_addr
));
1175 # if OPENSSL_USE_IPV6
1176 else if (af
== AF_INET6
) {
1177 # if defined(IPV6_PKTINFO)
1178 struct in6_pktinfo
*info
;
1180 # if defined(OPENSSL_SYS_WINDOWS)
1181 cmsg
= (CMSGHDR_TYPE
*)mh
->Control
.buf
;
1183 cmsg
= (CMSGHDR_TYPE
*)mh
->msg_control
;
1185 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in6_pktinfo
));
1186 cmsg
->cmsg_level
= IPPROTO_IPV6
;
1187 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1189 info
= (struct in6_pktinfo
*)BIO_CMSG_DATA(cmsg
);
1190 info
->ipi6_addr
= local
->s_in6
.sin6_addr
;
1191 info
->ipi6_ifindex
= 0;
1194 * See comment above, but also applies to the other fields
1197 if (local
->s_in6
.sin6_port
!= 0
1198 && data
->local_addr
.s_in6
.sin6_port
!= local
->s_in6
.sin6_port
) {
1199 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1203 if (local
->s_in6
.sin6_scope_id
!= 0
1204 && data
->local_addr
.s_in6
.sin6_scope_id
!= local
->s_in6
.sin6_scope_id
) {
1205 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1209 # if defined(OPENSSL_SYS_WINDOWS)
1210 mh
->Control
.len
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1212 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1224 * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
1225 * should mask out any system flags returned by this function you cannot support
1226 * in a particular circumstance. Currently no flags are defined.
1228 # if M_METHOD != M_METHOD_NONE
1229 static int translate_flags(uint64_t flags
) {
1234 static int dgram_sendmmsg(BIO
*b
, BIO_MSG
*msg
, size_t stride
,
1235 size_t num_msg
, uint64_t flags
, size_t *num_processed
)
1237 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1240 # if M_METHOD == M_METHOD_RECVMMSG
1241 # define BIO_MAX_MSGS_PER_CALL 64
1243 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1245 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1246 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1247 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1248 int have_local_enabled
= data
->local_addr_enabled
;
1249 # elif M_METHOD == M_METHOD_RECVMSG
1251 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1255 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1256 int have_local_enabled
= data
->local_addr_enabled
;
1257 # elif M_METHOD == M_METHOD_WSARECVMSG
1258 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1259 int have_local_enabled
= data
->local_addr_enabled
;
1262 DWORD num_bytes_sent
= 0;
1263 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1265 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1274 if (num_msg
> OSSL_SSIZE_MAX
)
1275 num_msg
= OSSL_SSIZE_MAX
;
1277 # if M_METHOD != M_METHOD_NONE
1278 sysflags
= translate_flags(flags
);
1281 # if M_METHOD == M_METHOD_RECVMMSG
1283 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1284 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1285 * we place a fixed limit on the number of messages per call, in the
1286 * expectation that we will be called again if there were more messages to
1289 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1290 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1292 for (i
= 0; i
< num_msg
; ++i
) {
1293 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1294 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1296 /* If local address was requested, it must have been enabled */
1297 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
) {
1298 if (!have_local_enabled
) {
1299 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1304 if (pack_local(b
, &mh
[i
].msg_hdr
,
1305 BIO_MSG_N(msg
, stride
, i
).local
) < 1) {
1306 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1314 ret
= sendmmsg(b
->num
, mh
, num_msg
, sysflags
);
1316 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1321 for (i
= 0; i
< (size_t)ret
; ++i
) {
1322 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1323 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1326 *num_processed
= (size_t)ret
;
1329 # elif M_METHOD == M_METHOD_RECVMSG
1331 * If sendmsg is available, use it.
1333 translate_msg(b
, &mh
, &iov
, control
, msg
);
1335 if (msg
->local
!= NULL
) {
1336 if (!have_local_enabled
) {
1337 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1342 if (pack_local(b
, &mh
, msg
->local
) < 1) {
1343 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1349 l
= sendmsg(b
->num
, &mh
, sysflags
);
1351 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1356 msg
->data_len
= (size_t)l
;
1361 # elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
1362 # if M_METHOD == M_METHOD_WSARECVMSG
1363 if (bio_WSASendMsg
!= NULL
) {
1364 /* WSASendMsg-based implementation for Windows. */
1365 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1367 if (msg
[0].local
!= NULL
) {
1368 if (!have_local_enabled
) {
1369 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1374 if (pack_local(b
, &wmsg
, msg
[0].local
) < 1) {
1375 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1381 ret
= WSASendMsg((SOCKET
)b
->num
, &wmsg
, 0, &num_bytes_sent
, NULL
, NULL
);
1383 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1388 msg
[0].data_len
= num_bytes_sent
;
1396 * Fallback to sendto and send a single message.
1398 if (msg
[0].local
!= NULL
) {
1400 * We cannot set the local address if using sendto
1401 * so fail in this case
1403 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1408 ret
= sendto(b
->num
, msg
[0].data
,
1409 # if defined(OPENSSL_SYS_WINDOWS)
1410 (int)msg
[0].data_len
,
1415 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1416 msg
[0].peer
!= NULL
? sizeof(*msg
[0].peer
) : 0);
1418 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1423 msg
[0].data_len
= ret
;
1429 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1435 static int dgram_recvmmsg(BIO
*b
, BIO_MSG
*msg
,
1436 size_t stride
, size_t num_msg
,
1437 uint64_t flags
, size_t *num_processed
)
1439 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1442 # if M_METHOD == M_METHOD_RECVMMSG
1444 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1446 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1447 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1448 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1449 int have_local_enabled
= data
->local_addr_enabled
;
1450 # elif M_METHOD == M_METHOD_RECVMSG
1452 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1456 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1457 int have_local_enabled
= data
->local_addr_enabled
;
1458 # elif M_METHOD == M_METHOD_WSARECVMSG
1459 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1460 int have_local_enabled
= data
->local_addr_enabled
;
1463 DWORD num_bytes_received
= 0;
1464 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1466 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1476 if (num_msg
> OSSL_SSIZE_MAX
)
1477 num_msg
= OSSL_SSIZE_MAX
;
1479 # if M_METHOD != M_METHOD_NONE
1480 sysflags
= translate_flags(flags
);
1483 # if M_METHOD == M_METHOD_RECVMMSG
1485 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1486 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1487 * we place a fixed limit on the number of messages per call, in the
1488 * expectation that we will be called again if there were more messages to
1491 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1492 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1494 for (i
= 0; i
< num_msg
; ++i
) {
1495 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1496 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1498 /* If local address was requested, it must have been enabled */
1499 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
&& !have_local_enabled
) {
1500 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1507 ret
= recvmmsg(b
->num
, mh
, num_msg
, sysflags
, NULL
);
1509 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1514 for (i
= 0; i
< (size_t)ret
; ++i
) {
1515 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1516 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1518 * *(msg->peer) will have been filled in by recvmmsg;
1519 * for msg->local we parse the control data returned
1521 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
)
1522 if (extract_local(b
, &mh
[i
].msg_hdr
,
1523 BIO_MSG_N(msg
, stride
, i
).local
) < 1) {
1529 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1535 *num_processed
= (size_t)ret
;
1538 # elif M_METHOD == M_METHOD_RECVMSG
1540 * If recvmsg is available, use it.
1542 translate_msg(b
, &mh
, &iov
, control
, msg
);
1544 /* If local address was requested, it must have been enabled */
1545 if (msg
->local
!= NULL
&& !have_local_enabled
) {
1547 * If we have done at least one message, we must return the
1548 * count; if we haven't done any, we can give an error code
1550 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1555 l
= recvmsg(b
->num
, &mh
, sysflags
);
1557 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1562 msg
->data_len
= (size_t)l
;
1565 if (msg
->local
!= NULL
)
1566 if (extract_local(b
, &mh
, msg
->local
) < 1) {
1568 * OS X exhibits odd behaviour where it appears that if a packet is
1569 * sent before the receiving interface enables IP_PKTINFO, it will
1570 * sometimes not have any control data returned even if the
1571 * receiving interface enables IP_PKTINFO before calling recvmsg().
1572 * This appears to occur non-deterministically. Presumably, OS X
1573 * handles IP_PKTINFO at the time the packet is enqueued into a
1574 * socket's receive queue, rather than at the time recvmsg() is
1575 * called, unlike most other operating systems. Thus (if this
1576 * hypothesis is correct) there is a race between where IP_PKTINFO
1577 * is enabled by the process and when the kernel's network stack
1578 * queues the incoming message.
1580 * We cannot return the local address if we do not have it, but this
1581 * is not a caller error either, so just return a zero address
1584 * We enable this workaround for Apple only as it should not
1585 * be necessary otherwise.
1587 # if defined(__APPLE__)
1588 BIO_ADDR_clear(msg
->local
);
1590 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1599 # elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1600 # if M_METHOD == M_METHOD_WSARECVMSG
1601 if (bio_WSARecvMsg
!= NULL
) {
1602 /* WSARecvMsg-based implementation for Windows. */
1603 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1605 /* If local address was requested, it must have been enabled */
1606 if (msg
[0].local
!= NULL
&& !have_local_enabled
) {
1607 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1612 ret
= WSARecvMsg((SOCKET
)b
->num
, &wmsg
, &num_bytes_received
, NULL
, NULL
);
1614 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1619 msg
[0].data_len
= num_bytes_received
;
1621 if (msg
[0].local
!= NULL
)
1622 if (extract_local(b
, &wmsg
, msg
[0].local
) < 1)
1624 * On Windows, loopback is not a "proper" interface and it works
1625 * differently; packets are essentially short-circuited and
1626 * don't go through all of the normal processing. A consequence
1627 * of this is that packets sent from the local machine to the
1628 * local machine _will not have IP_PKTINFO_ even if the
1629 * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
1630 * Control.len to 0 on returning.
1632 * This applies regardless of whether the loopback address,
1633 * 127.0.0.1 is used, or a local interface address (e.g.
1634 * 192.168.1.1); in both cases IP_PKTINFO will not be present.
1636 * We report this condition by setting the local BIO_ADDR's
1639 BIO_ADDR_clear(msg
[0].local
);
1647 * Fallback to recvfrom and receive a single message.
1649 if (msg
[0].local
!= NULL
) {
1651 * We cannot determine the local address if using recvfrom
1652 * so fail in this case
1654 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1659 slen
= sizeof(*msg
[0].peer
);
1660 ret
= recvfrom(b
->num
, msg
[0].data
,
1661 # if defined(OPENSSL_SYS_WINDOWS)
1662 (int)msg
[0].data_len
,
1667 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1668 msg
[0].peer
!= NULL
? &slen
: NULL
);
1670 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1674 msg
[0].data_len
= ret
;
1680 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1686 # ifndef OPENSSL_NO_SCTP
1687 const BIO_METHOD
*BIO_s_datagram_sctp(void)
1689 return &methods_dgramp_sctp
;
1692 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
1695 int ret
, optval
= 20000;
1696 int auth_data
= 0, auth_forward
= 0;
1698 struct sctp_authchunk auth
;
1699 struct sctp_authchunks
*authchunks
;
1700 socklen_t sockopt_len
;
1701 # ifdef SCTP_AUTHENTICATION_EVENT
1703 struct sctp_event event
;
1705 struct sctp_event_subscribe event
;
1709 bio
= BIO_new(BIO_s_datagram_sctp());
1712 BIO_set_fd(bio
, fd
, close_flag
);
1714 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1715 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
1717 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1718 sizeof(struct sctp_authchunk
));
1721 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1722 "Ensure SCTP AUTH chunks are enabled in kernel");
1725 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
1727 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1728 sizeof(struct sctp_authchunk
));
1731 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1732 "Ensure SCTP AUTH chunks are enabled in kernel");
1737 * Test if activation was successful. When using accept(), SCTP-AUTH has
1738 * to be activated for the listening socket already, otherwise the
1739 * connected socket won't use it. Similarly with connect(): the socket
1740 * prior to connection must be activated for SCTP-AUTH
1742 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1743 authchunks
= OPENSSL_zalloc(sockopt_len
);
1744 if (authchunks
== NULL
) {
1748 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1751 OPENSSL_free(authchunks
);
1756 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1757 p
< (unsigned char *)authchunks
+ sockopt_len
;
1758 p
+= sizeof(uint8_t)) {
1759 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1761 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1765 OPENSSL_free(authchunks
);
1767 if (!auth_data
|| !auth_forward
) {
1769 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1770 "Ensure SCTP AUTH chunks are enabled on the "
1771 "underlying socket");
1775 # ifdef SCTP_AUTHENTICATION_EVENT
1777 memset(&event
, 0, sizeof(event
));
1778 event
.se_assoc_id
= 0;
1779 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1782 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1783 sizeof(struct sctp_event
));
1789 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1790 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1796 event
.sctp_authentication_event
= 1;
1799 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1800 sizeof(struct sctp_event_subscribe
));
1809 * Disable partial delivery by setting the min size larger than the max
1810 * record size of 2^14 + 2048 + 13
1813 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1823 int BIO_dgram_is_sctp(BIO
*bio
)
1825 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1828 static int dgram_sctp_new(BIO
*bi
)
1830 bio_dgram_sctp_data
*data
= NULL
;
1834 if ((data
= OPENSSL_zalloc(sizeof(*data
))) == NULL
) {
1835 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
1838 # ifdef SCTP_PR_SCTP_NONE
1839 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1847 static int dgram_sctp_free(BIO
*a
)
1849 bio_dgram_sctp_data
*data
;
1853 if (!dgram_clear(a
))
1856 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1863 # ifdef SCTP_AUTHENTICATION_EVENT
1864 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1865 union sctp_notification
*snp
)
1868 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1870 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1871 struct sctp_authkeyid authkeyid
;
1874 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1875 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1876 &authkeyid
, sizeof(struct sctp_authkeyid
));
1881 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1883 int ret
= 0, n
= 0, i
, optval
;
1885 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1888 struct cmsghdr
*cmsg
;
1892 clear_socket_error();
1895 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1898 msg
.msg_name
= NULL
;
1899 msg
.msg_namelen
= 0;
1902 msg
.msg_control
= cmsgbuf
;
1903 msg
.msg_controllen
= 512;
1905 n
= recvmsg(b
->num
, &msg
, 0);
1913 if (msg
.msg_controllen
> 0) {
1914 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1915 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1916 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1918 # ifdef SCTP_RCVINFO
1919 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1920 struct sctp_rcvinfo
*rcvinfo
;
1922 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1923 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1924 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1925 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1926 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1927 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1928 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1929 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1933 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1934 struct sctp_sndrcvinfo
*sndrcvinfo
;
1937 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1938 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1939 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1940 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1941 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1942 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1943 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1944 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1950 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1951 union sctp_notification snp
;
1953 memcpy(&snp
, out
, sizeof(snp
));
1954 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1956 struct sctp_event event
;
1958 struct sctp_event_subscribe event
;
1959 socklen_t eventsize
;
1962 /* disable sender dry event */
1964 memset(&event
, 0, sizeof(event
));
1965 event
.se_assoc_id
= 0;
1966 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1968 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1969 sizeof(struct sctp_event
));
1975 eventsize
= sizeof(struct sctp_event_subscribe
);
1976 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1983 event
.sctp_sender_dry_event
= 0;
1985 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1986 sizeof(struct sctp_event_subscribe
));
1993 # ifdef SCTP_AUTHENTICATION_EVENT
1994 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1995 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1998 if (data
->handle_notifications
!= NULL
)
1999 data
->handle_notifications(b
, data
->notification_context
,
2002 memset(&snp
, 0, sizeof(snp
));
2003 memset(out
, 0, outl
);
2008 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
2011 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
2012 /* Partial message read, this should never happen! */
2015 * The buffer was too small, this means the peer sent a message
2016 * that was larger than allowed.
2022 * Test if socket buffer can handle max record size (2^14 + 2048
2025 optlen
= (socklen_t
) sizeof(int);
2026 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
2028 OPENSSL_assert(optval
>= 18445);
2031 * Test if SCTP doesn't partially deliver below max record size
2032 * (2^14 + 2048 + 13)
2034 optlen
= (socklen_t
) sizeof(int);
2036 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
2039 OPENSSL_assert(optval
>= 18445);
2042 * Partially delivered notification??? Probably a bug....
2044 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
2047 * Everything seems ok till now, so it's most likely a message
2048 * dropped by PR-SCTP.
2050 memset(out
, 0, outl
);
2051 BIO_set_retry_read(b
);
2055 BIO_clear_retry_flags(b
);
2057 if (BIO_dgram_should_retry(ret
)) {
2058 BIO_set_retry_read(b
);
2059 data
->_errno
= get_last_socket_error();
2063 /* Test if peer uses SCTP-AUTH before continuing */
2064 if (!data
->peer_auth_tested
) {
2065 int ii
, auth_data
= 0, auth_forward
= 0;
2067 struct sctp_authchunks
*authchunks
;
2070 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
2071 authchunks
= OPENSSL_malloc(optlen
);
2072 if (authchunks
== NULL
) {
2073 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
2076 memset(authchunks
, 0, optlen
);
2077 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
2078 authchunks
, &optlen
);
2081 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
2082 p
< (unsigned char *)authchunks
+ optlen
;
2083 p
+= sizeof(uint8_t)) {
2084 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
2086 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
2090 OPENSSL_free(authchunks
);
2092 if (!auth_data
|| !auth_forward
) {
2093 ERR_raise(ERR_LIB_BIO
, BIO_R_CONNECT_ERROR
);
2097 data
->peer_auth_tested
= 1;
2104 * dgram_sctp_write - send message on SCTP socket
2105 * @b: BIO to write to
2107 * @inl: amount of bytes in @in to send
2109 * Returns -1 on error or the sent amount of bytes on success
2111 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
2114 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2115 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
2116 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
2117 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
2118 struct iovec iov
[1];
2120 struct cmsghdr
*cmsg
;
2121 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2122 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
2123 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
2124 struct sctp_sndinfo
*sndinfo
;
2125 struct sctp_prinfo
*prinfo
;
2127 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
2128 struct sctp_sndrcvinfo
*sndrcvinfo
;
2131 clear_socket_error();
2134 * If we're send anything else than application data, disable all user
2135 * parameters and flags.
2138 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
2139 # ifdef SCTP_SACK_IMMEDIATELY
2140 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
2142 sinfo
= &handshake_sinfo
;
2145 /* We can only send a shutdown alert if the socket is dry */
2146 if (data
->save_shutdown
) {
2147 ret
= BIO_dgram_sctp_wait_for_dry(b
);
2151 BIO_clear_retry_flags(b
);
2152 BIO_set_retry_write(b
);
2157 iov
[0].iov_base
= (char *)in
;
2158 iov
[0].iov_len
= inl
;
2159 msg
.msg_name
= NULL
;
2160 msg
.msg_namelen
= 0;
2163 msg
.msg_control
= (caddr_t
) cmsgbuf
;
2164 msg
.msg_controllen
= 0;
2166 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2167 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2168 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2169 cmsg
->cmsg_type
= SCTP_SNDINFO
;
2170 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
2171 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
2172 memset(sndinfo
, 0, sizeof(*sndinfo
));
2173 sndinfo
->snd_sid
= sinfo
->snd_sid
;
2174 sndinfo
->snd_flags
= sinfo
->snd_flags
;
2175 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
2176 sndinfo
->snd_context
= sinfo
->snd_context
;
2177 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
2180 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
2181 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2182 cmsg
->cmsg_type
= SCTP_PRINFO
;
2183 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
2184 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
2185 memset(prinfo
, 0, sizeof(*prinfo
));
2186 prinfo
->pr_policy
= pinfo
->pr_policy
;
2187 prinfo
->pr_value
= pinfo
->pr_value
;
2188 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
2190 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2191 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2192 cmsg
->cmsg_type
= SCTP_SNDRCV
;
2193 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
2194 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
2195 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
2196 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
2197 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
2199 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
2201 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
2202 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
2203 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
2204 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
2207 ret
= sendmsg(b
->num
, &msg
, 0);
2209 BIO_clear_retry_flags(b
);
2211 if (BIO_dgram_should_retry(ret
)) {
2212 BIO_set_retry_write(b
);
2213 data
->_errno
= get_last_socket_error();
2219 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
2222 bio_dgram_sctp_data
*data
= NULL
;
2223 socklen_t sockopt_len
= 0;
2224 struct sctp_authkeyid authkeyid
;
2225 struct sctp_authkey
*authkey
= NULL
;
2227 data
= (bio_dgram_sctp_data
*) b
->ptr
;
2230 case BIO_CTRL_DGRAM_QUERY_MTU
:
2232 * Set to maximum (2^14) and ignore user input to enable transport
2233 * protocol fragmentation. Returns always 2^14.
2238 case BIO_CTRL_DGRAM_SET_MTU
:
2240 * Set to maximum (2^14) and ignore input to enable transport
2241 * protocol fragmentation. Returns always 2^14.
2246 case BIO_CTRL_DGRAM_SET_CONNECTED
:
2247 case BIO_CTRL_DGRAM_CONNECT
:
2248 /* Returns always -1. */
2251 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
2253 * SCTP doesn't need the DTLS timer Returns always 1.
2256 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
2258 * We allow transport protocol fragmentation so this is irrelevant
2262 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
2264 data
->in_handshake
= 1;
2266 data
->in_handshake
= 0;
2269 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
2270 &data
->in_handshake
, sizeof(int));
2272 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
2274 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2277 /* Get active key */
2278 sockopt_len
= sizeof(struct sctp_authkeyid
);
2280 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2286 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
2287 authkey
= OPENSSL_malloc(sockopt_len
);
2288 if (authkey
== NULL
) {
2292 memset(authkey
, 0, sockopt_len
);
2293 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
2294 # ifndef __FreeBSD__
2296 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2297 * and higher work without it.
2299 authkey
->sca_keylength
= 64;
2301 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
2304 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
2306 OPENSSL_free(authkey
);
2311 /* Reset active key */
2312 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2313 &authkeyid
, sizeof(struct sctp_authkeyid
));
2318 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
2319 /* Returns 0 on success, -1 otherwise. */
2321 /* Get active key */
2322 sockopt_len
= sizeof(struct sctp_authkeyid
);
2324 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2329 /* Set active key */
2330 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
2331 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2332 &authkeyid
, sizeof(struct sctp_authkeyid
));
2337 * CCS has been sent, so remember that and fall through to check if
2338 * we need to deactivate an old key
2343 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
2344 /* Returns 0 on success, -1 otherwise. */
2347 * Has this command really been called or is this just a
2350 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
2354 * CSS has been both, received and sent, so deactivate an old key
2356 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
2357 /* Get active key */
2358 sockopt_len
= sizeof(struct sctp_authkeyid
);
2360 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2361 &authkeyid
, &sockopt_len
);
2366 * Deactivate key or delete second last key if
2367 * SCTP_AUTHENTICATION_EVENT is not available.
2369 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2370 # ifdef SCTP_AUTH_DEACTIVATE_KEY
2371 sockopt_len
= sizeof(struct sctp_authkeyid
);
2372 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
2373 &authkeyid
, sockopt_len
);
2377 # ifndef SCTP_AUTHENTICATION_EVENT
2378 if (authkeyid
.scact_keynumber
> 0) {
2379 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2380 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
2381 &authkeyid
, sizeof(struct sctp_authkeyid
));
2391 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
2392 /* Returns the size of the copied struct. */
2393 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2394 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2396 memcpy(ptr
, &(data
->sndinfo
), num
);
2399 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
2400 /* Returns the size of the copied struct. */
2401 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2402 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2404 memcpy(&(data
->sndinfo
), ptr
, num
);
2406 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
2407 /* Returns the size of the copied struct. */
2408 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2409 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2411 memcpy(ptr
, &data
->rcvinfo
, num
);
2415 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
2416 /* Returns the size of the copied struct. */
2417 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2418 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2420 memcpy(&(data
->rcvinfo
), ptr
, num
);
2422 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
2423 /* Returns the size of the copied struct. */
2424 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2425 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2427 memcpy(ptr
, &(data
->prinfo
), num
);
2430 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
2431 /* Returns the size of the copied struct. */
2432 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2433 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2435 memcpy(&(data
->prinfo
), ptr
, num
);
2437 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
2438 /* Returns always 1. */
2440 data
->save_shutdown
= 1;
2442 data
->save_shutdown
= 0;
2444 case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
:
2445 return dgram_sctp_wait_for_dry(b
);
2446 case BIO_CTRL_DGRAM_SCTP_MSG_WAITING
:
2447 return dgram_sctp_msg_waiting(b
);
2451 * Pass to default ctrl function to process SCTP unspecific commands
2453 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
2459 int BIO_dgram_sctp_notification_cb(BIO
*b
,
2460 BIO_dgram_sctp_notification_handler_fn handle_notifications
,
2463 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2465 if (handle_notifications
!= NULL
) {
2466 data
->handle_notifications
= handle_notifications
;
2467 data
->notification_context
= context
;
2475 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2476 * @b: The BIO to check for the dry event
2478 * Wait until the peer confirms all packets have been received, and so that
2479 * our kernel doesn't have anything to send anymore. This is only received by
2480 * the peer's kernel, not the application.
2484 * 0 when not dry yet
2487 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
2489 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
, 0, NULL
);
2492 static int dgram_sctp_wait_for_dry(BIO
*b
)
2497 union sctp_notification snp
;
2501 struct sctp_event event
;
2503 struct sctp_event_subscribe event
;
2504 socklen_t eventsize
;
2506 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2508 /* set sender dry event */
2510 memset(&event
, 0, sizeof(event
));
2511 event
.se_assoc_id
= 0;
2512 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2515 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2516 sizeof(struct sctp_event
));
2518 eventsize
= sizeof(struct sctp_event_subscribe
);
2519 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
2523 event
.sctp_sender_dry_event
= 1;
2526 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2527 sizeof(struct sctp_event_subscribe
));
2532 /* peek for notification */
2533 memset(&snp
, 0, sizeof(snp
));
2534 iov
.iov_base
= (char *)&snp
;
2535 iov
.iov_len
= sizeof(union sctp_notification
);
2536 msg
.msg_name
= NULL
;
2537 msg
.msg_namelen
= 0;
2540 msg
.msg_control
= NULL
;
2541 msg
.msg_controllen
= 0;
2544 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2546 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2547 && (get_last_socket_error() != EWOULDBLOCK
))
2553 /* if we find a notification, process it and try again if necessary */
2554 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
2555 memset(&snp
, 0, sizeof(snp
));
2556 iov
.iov_base
= (char *)&snp
;
2557 iov
.iov_len
= sizeof(union sctp_notification
);
2558 msg
.msg_name
= NULL
;
2559 msg
.msg_namelen
= 0;
2562 msg
.msg_control
= NULL
;
2563 msg
.msg_controllen
= 0;
2566 n
= recvmsg(b
->num
, &msg
, 0);
2568 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2569 && (get_last_socket_error() != EWOULDBLOCK
))
2575 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
2578 /* disable sender dry event */
2580 memset(&event
, 0, sizeof(event
));
2581 event
.se_assoc_id
= 0;
2582 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2585 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2586 sizeof(struct sctp_event
));
2588 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
2590 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2595 event
.sctp_sender_dry_event
= 0;
2598 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2599 sizeof(struct sctp_event_subscribe
));
2604 # ifdef SCTP_AUTHENTICATION_EVENT
2605 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2606 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2609 if (data
->handle_notifications
!= NULL
)
2610 data
->handle_notifications(b
, data
->notification_context
,
2613 /* found notification, peek again */
2614 memset(&snp
, 0, sizeof(snp
));
2615 iov
.iov_base
= (char *)&snp
;
2616 iov
.iov_len
= sizeof(union sctp_notification
);
2617 msg
.msg_name
= NULL
;
2618 msg
.msg_namelen
= 0;
2621 msg
.msg_control
= NULL
;
2622 msg
.msg_controllen
= 0;
2625 /* if we have seen the dry already, don't wait */
2627 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2628 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2631 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2634 fcntl(b
->num
, F_SETFL
, sockflags
);
2638 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2639 && (get_last_socket_error() != EWOULDBLOCK
))
2646 /* read anything else */
2650 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
2652 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_MSG_WAITING
, 0, NULL
);
2655 static int dgram_sctp_msg_waiting(BIO
*b
)
2658 union sctp_notification snp
;
2661 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2663 /* Check if there are any messages waiting to be read */
2665 memset(&snp
, 0, sizeof(snp
));
2666 iov
.iov_base
= (char *)&snp
;
2667 iov
.iov_len
= sizeof(union sctp_notification
);
2668 msg
.msg_name
= NULL
;
2669 msg
.msg_namelen
= 0;
2672 msg
.msg_control
= NULL
;
2673 msg
.msg_controllen
= 0;
2676 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2677 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2678 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2679 fcntl(b
->num
, F_SETFL
, sockflags
);
2681 /* if notification, process and try again */
2682 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
2683 # ifdef SCTP_AUTHENTICATION_EVENT
2684 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2685 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2688 memset(&snp
, 0, sizeof(snp
));
2689 iov
.iov_base
= (char *)&snp
;
2690 iov
.iov_len
= sizeof(union sctp_notification
);
2691 msg
.msg_name
= NULL
;
2692 msg
.msg_namelen
= 0;
2695 msg
.msg_control
= NULL
;
2696 msg
.msg_controllen
= 0;
2698 n
= recvmsg(b
->num
, &msg
, 0);
2700 if (data
->handle_notifications
!= NULL
)
2701 data
->handle_notifications(b
, data
->notification_context
,
2705 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
2707 /* Return 1 if there is a message to be read, return 0 otherwise. */
2714 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
2719 ret
= dgram_sctp_write(bp
, str
, n
);
2724 static int BIO_dgram_should_retry(int i
)
2728 if ((i
== 0) || (i
== -1)) {
2729 err
= get_last_socket_error();
2731 # if defined(OPENSSL_SYS_WINDOWS)
2733 * If the socket return value (i) is -1 and err is unexpectedly 0 at
2734 * this point, the error code was overwritten by another system call
2735 * before this error handling is called.
2739 return BIO_dgram_non_fatal_error(err
);
2744 int BIO_dgram_non_fatal_error(int err
)
2747 # if defined(OPENSSL_SYS_WINDOWS)
2748 # if defined(WSAEWOULDBLOCK)
2749 case WSAEWOULDBLOCK
:
2754 # ifdef WSAEWOULDBLOCK
2755 # if WSAEWOULDBLOCK != EWOULDBLOCK
2768 # if EWOULDBLOCK != EAGAIN
2792 static void get_current_time(struct timeval
*t
)
2794 # if defined(_WIN32)
2796 unsigned __int64 now_ul
;
2800 SystemTimeToFileTime(&st
, &now_ft
);
2801 now_ul
= ((unsigned __int64
)now_ft
.dwHighDateTime
<< 32) | now_ft
.dwLowDateTime
;
2803 now_ul
-= 116444736000000000ULL;
2805 now_ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
2807 t
->tv_sec
= (long)(now_ul
/ 10000000);
2808 t
->tv_usec
= ((int)(now_ul
% 10000000)) / 10;
2810 if (gettimeofday(t
, NULL
) < 0)
2811 perror("gettimeofday");