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
);
1109 if (af
== AF_INET
) {
1110 # if defined(IP_PKTINFO)
1112 struct in_pktinfo
*info
;
1113 bio_dgram_data
*data
= b
->ptr
;
1115 # if defined(OPENSSL_SYS_WINDOWS)
1116 cmsg
= (CMSGHDR_TYPE
*)mh
->Control
.buf
;
1118 cmsg
= (CMSGHDR_TYPE
*)mh
->msg_control
;
1121 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in_pktinfo
));
1122 cmsg
->cmsg_level
= IPPROTO_IP
;
1123 cmsg
->cmsg_type
= IP_PKTINFO
;
1125 info
= (struct in_pktinfo
*)BIO_CMSG_DATA(cmsg
);
1126 info
->ipi_spec_dst
= local
->s_in
.sin_addr
;
1127 info
->ipi_addr
.s_addr
= 0;
1128 info
->ipi_ifindex
= 0;
1131 * We cannot override source port using this API, therefore
1132 * ensure the application specified a source port of 0
1133 * or the one we are bound to. (Better to error than silently
1136 if (local
->s_in
.sin_port
!= 0
1137 && data
->local_addr
.s_in
.sin_port
!= local
->s_in
.sin_port
) {
1138 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1142 # if defined(OPENSSL_SYS_WINDOWS)
1143 mh
->Control
.len
= BIO_CMSG_SPACE(sizeof(struct in_pktinfo
));
1145 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in_pktinfo
));
1149 # elif defined(IP_SENDSRCADDR)
1151 struct cmsghdr
*cmsg
;
1152 struct in_addr
*info
;
1154 cmsg
= (struct cmsghdr
*)mh
->msg_control
;
1155 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in_addr
));
1156 cmsg
->cmsg_level
= IPPROTO_IP
;
1157 cmsg
->cmsg_type
= IP_SENDSRCADDR
;
1159 info
= (struct in_addr
*)BIO_CMSG_DATA(cmsg
);
1160 *info
= local
->s_in
.sin_addr
;
1163 /* See comment above. */
1164 if (local
->s_in
.sin_port
!= 0
1165 && data
->local_addr
.s_in
.sin_port
!= local
->s_in
.sin_port
) {
1166 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1170 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in_addr
));
1174 # if OPENSSL_USE_IPV6
1175 else if (af
== AF_INET6
) {
1176 # if defined(IPV6_PKTINFO)
1178 struct in6_pktinfo
*info
;
1179 bio_dgram_data
*data
= b
->ptr
;
1181 # if defined(OPENSSL_SYS_WINDOWS)
1182 cmsg
= (CMSGHDR_TYPE
*)mh
->Control
.buf
;
1184 cmsg
= (CMSGHDR_TYPE
*)mh
->msg_control
;
1186 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in6_pktinfo
));
1187 cmsg
->cmsg_level
= IPPROTO_IPV6
;
1188 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1190 info
= (struct in6_pktinfo
*)BIO_CMSG_DATA(cmsg
);
1191 info
->ipi6_addr
= local
->s_in6
.sin6_addr
;
1192 info
->ipi6_ifindex
= 0;
1195 * See comment above, but also applies to the other fields
1198 if (local
->s_in6
.sin6_port
!= 0
1199 && data
->local_addr
.s_in6
.sin6_port
!= local
->s_in6
.sin6_port
) {
1200 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1204 if (local
->s_in6
.sin6_scope_id
!= 0
1205 && data
->local_addr
.s_in6
.sin6_scope_id
!= local
->s_in6
.sin6_scope_id
) {
1206 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1210 # if defined(OPENSSL_SYS_WINDOWS)
1211 mh
->Control
.len
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1213 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1225 * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
1226 * should mask out any system flags returned by this function you cannot support
1227 * in a particular circumstance. Currently no flags are defined.
1229 # if M_METHOD != M_METHOD_NONE
1230 static int translate_flags(uint64_t flags
) {
1235 static int dgram_sendmmsg(BIO
*b
, BIO_MSG
*msg
, size_t stride
,
1236 size_t num_msg
, uint64_t flags
, size_t *num_processed
)
1238 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1241 # if M_METHOD == M_METHOD_RECVMMSG
1242 # define BIO_MAX_MSGS_PER_CALL 64
1244 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1246 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1247 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1248 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1249 int have_local_enabled
= data
->local_addr_enabled
;
1250 # elif M_METHOD == M_METHOD_RECVMSG
1252 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1256 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1257 int have_local_enabled
= data
->local_addr_enabled
;
1258 # elif M_METHOD == M_METHOD_WSARECVMSG
1259 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1260 int have_local_enabled
= data
->local_addr_enabled
;
1263 DWORD num_bytes_sent
= 0;
1264 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1266 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1275 if (num_msg
> OSSL_SSIZE_MAX
)
1276 num_msg
= OSSL_SSIZE_MAX
;
1278 # if M_METHOD != M_METHOD_NONE
1279 sysflags
= translate_flags(flags
);
1282 # if M_METHOD == M_METHOD_RECVMMSG
1284 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1285 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1286 * we place a fixed limit on the number of messages per call, in the
1287 * expectation that we will be called again if there were more messages to
1290 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1291 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1293 for (i
= 0; i
< num_msg
; ++i
) {
1294 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1295 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1297 /* If local address was requested, it must have been enabled */
1298 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
) {
1299 if (!have_local_enabled
) {
1300 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1305 if (pack_local(b
, &mh
[i
].msg_hdr
,
1306 BIO_MSG_N(msg
, stride
, i
).local
) < 1) {
1307 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1315 ret
= sendmmsg(b
->num
, mh
, num_msg
, sysflags
);
1317 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1322 for (i
= 0; i
< (size_t)ret
; ++i
) {
1323 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1324 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1327 *num_processed
= (size_t)ret
;
1330 # elif M_METHOD == M_METHOD_RECVMSG
1332 * If sendmsg is available, use it.
1334 translate_msg(b
, &mh
, &iov
, control
, msg
);
1336 if (msg
->local
!= NULL
) {
1337 if (!have_local_enabled
) {
1338 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1343 if (pack_local(b
, &mh
, msg
->local
) < 1) {
1344 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1350 l
= sendmsg(b
->num
, &mh
, sysflags
);
1352 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1357 msg
->data_len
= (size_t)l
;
1362 # elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
1363 # if M_METHOD == M_METHOD_WSARECVMSG
1364 if (bio_WSASendMsg
!= NULL
) {
1365 /* WSASendMsg-based implementation for Windows. */
1366 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1368 if (msg
[0].local
!= NULL
) {
1369 if (!have_local_enabled
) {
1370 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1375 if (pack_local(b
, &wmsg
, msg
[0].local
) < 1) {
1376 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1382 ret
= WSASendMsg((SOCKET
)b
->num
, &wmsg
, 0, &num_bytes_sent
, NULL
, NULL
);
1384 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1389 msg
[0].data_len
= num_bytes_sent
;
1397 * Fallback to sendto and send a single message.
1399 if (msg
[0].local
!= NULL
) {
1401 * We cannot set the local address if using sendto
1402 * so fail in this case
1404 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1409 ret
= sendto(b
->num
, msg
[0].data
,
1410 # if defined(OPENSSL_SYS_WINDOWS)
1411 (int)msg
[0].data_len
,
1416 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1417 msg
[0].peer
!= NULL
? sizeof(*msg
[0].peer
) : 0);
1419 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1424 msg
[0].data_len
= ret
;
1430 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1436 static int dgram_recvmmsg(BIO
*b
, BIO_MSG
*msg
,
1437 size_t stride
, size_t num_msg
,
1438 uint64_t flags
, size_t *num_processed
)
1440 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1443 # if M_METHOD == M_METHOD_RECVMMSG
1445 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1447 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1448 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1449 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1450 int have_local_enabled
= data
->local_addr_enabled
;
1451 # elif M_METHOD == M_METHOD_RECVMSG
1453 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1457 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1458 int have_local_enabled
= data
->local_addr_enabled
;
1459 # elif M_METHOD == M_METHOD_WSARECVMSG
1460 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1461 int have_local_enabled
= data
->local_addr_enabled
;
1464 DWORD num_bytes_received
= 0;
1465 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1467 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1477 if (num_msg
> OSSL_SSIZE_MAX
)
1478 num_msg
= OSSL_SSIZE_MAX
;
1480 # if M_METHOD != M_METHOD_NONE
1481 sysflags
= translate_flags(flags
);
1484 # if M_METHOD == M_METHOD_RECVMMSG
1486 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1487 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1488 * we place a fixed limit on the number of messages per call, in the
1489 * expectation that we will be called again if there were more messages to
1492 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1493 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1495 for (i
= 0; i
< num_msg
; ++i
) {
1496 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1497 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1499 /* If local address was requested, it must have been enabled */
1500 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
&& !have_local_enabled
) {
1501 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1508 ret
= recvmmsg(b
->num
, mh
, num_msg
, sysflags
, NULL
);
1510 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1515 for (i
= 0; i
< (size_t)ret
; ++i
) {
1516 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1517 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1519 * *(msg->peer) will have been filled in by recvmmsg;
1520 * for msg->local we parse the control data returned
1522 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
)
1523 if (extract_local(b
, &mh
[i
].msg_hdr
,
1524 BIO_MSG_N(msg
, stride
, i
).local
) < 1) {
1530 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1536 *num_processed
= (size_t)ret
;
1539 # elif M_METHOD == M_METHOD_RECVMSG
1541 * If recvmsg is available, use it.
1543 translate_msg(b
, &mh
, &iov
, control
, msg
);
1545 /* If local address was requested, it must have been enabled */
1546 if (msg
->local
!= NULL
&& !have_local_enabled
) {
1548 * If we have done at least one message, we must return the
1549 * count; if we haven't done any, we can give an error code
1551 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1556 l
= recvmsg(b
->num
, &mh
, sysflags
);
1558 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1563 msg
->data_len
= (size_t)l
;
1566 if (msg
->local
!= NULL
)
1567 if (extract_local(b
, &mh
, msg
->local
) < 1) {
1569 * OS X exhibits odd behaviour where it appears that if a packet is
1570 * sent before the receiving interface enables IP_PKTINFO, it will
1571 * sometimes not have any control data returned even if the
1572 * receiving interface enables IP_PKTINFO before calling recvmsg().
1573 * This appears to occur non-deterministically. Presumably, OS X
1574 * handles IP_PKTINFO at the time the packet is enqueued into a
1575 * socket's receive queue, rather than at the time recvmsg() is
1576 * called, unlike most other operating systems. Thus (if this
1577 * hypothesis is correct) there is a race between where IP_PKTINFO
1578 * is enabled by the process and when the kernel's network stack
1579 * queues the incoming message.
1581 * We cannot return the local address if we do not have it, but this
1582 * is not a caller error either, so just return a zero address
1585 * We enable this workaround for Apple only as it should not
1586 * be necessary otherwise.
1588 # if defined(__APPLE__)
1589 BIO_ADDR_clear(msg
->local
);
1591 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1600 # elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1601 # if M_METHOD == M_METHOD_WSARECVMSG
1602 if (bio_WSARecvMsg
!= NULL
) {
1603 /* WSARecvMsg-based implementation for Windows. */
1604 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1606 /* If local address was requested, it must have been enabled */
1607 if (msg
[0].local
!= NULL
&& !have_local_enabled
) {
1608 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1613 ret
= WSARecvMsg((SOCKET
)b
->num
, &wmsg
, &num_bytes_received
, NULL
, NULL
);
1615 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1620 msg
[0].data_len
= num_bytes_received
;
1622 if (msg
[0].local
!= NULL
)
1623 if (extract_local(b
, &wmsg
, msg
[0].local
) < 1)
1625 * On Windows, loopback is not a "proper" interface and it works
1626 * differently; packets are essentially short-circuited and
1627 * don't go through all of the normal processing. A consequence
1628 * of this is that packets sent from the local machine to the
1629 * local machine _will not have IP_PKTINFO_ even if the
1630 * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
1631 * Control.len to 0 on returning.
1633 * This applies regardless of whether the loopback address,
1634 * 127.0.0.1 is used, or a local interface address (e.g.
1635 * 192.168.1.1); in both cases IP_PKTINFO will not be present.
1637 * We report this condition by setting the local BIO_ADDR's
1640 BIO_ADDR_clear(msg
[0].local
);
1648 * Fallback to recvfrom and receive a single message.
1650 if (msg
[0].local
!= NULL
) {
1652 * We cannot determine the local address if using recvfrom
1653 * so fail in this case
1655 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1660 slen
= sizeof(*msg
[0].peer
);
1661 ret
= recvfrom(b
->num
, msg
[0].data
,
1662 # if defined(OPENSSL_SYS_WINDOWS)
1663 (int)msg
[0].data_len
,
1668 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1669 msg
[0].peer
!= NULL
? &slen
: NULL
);
1671 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1675 msg
[0].data_len
= ret
;
1681 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1687 # ifndef OPENSSL_NO_SCTP
1688 const BIO_METHOD
*BIO_s_datagram_sctp(void)
1690 return &methods_dgramp_sctp
;
1693 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
1696 int ret
, optval
= 20000;
1697 int auth_data
= 0, auth_forward
= 0;
1699 struct sctp_authchunk auth
;
1700 struct sctp_authchunks
*authchunks
;
1701 socklen_t sockopt_len
;
1702 # ifdef SCTP_AUTHENTICATION_EVENT
1704 struct sctp_event event
;
1706 struct sctp_event_subscribe event
;
1710 bio
= BIO_new(BIO_s_datagram_sctp());
1713 BIO_set_fd(bio
, fd
, close_flag
);
1715 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1716 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
1718 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1719 sizeof(struct sctp_authchunk
));
1722 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1723 "Ensure SCTP AUTH chunks are enabled in kernel");
1726 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
1728 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1729 sizeof(struct sctp_authchunk
));
1732 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1733 "Ensure SCTP AUTH chunks are enabled in kernel");
1738 * Test if activation was successful. When using accept(), SCTP-AUTH has
1739 * to be activated for the listening socket already, otherwise the
1740 * connected socket won't use it. Similarly with connect(): the socket
1741 * prior to connection must be activated for SCTP-AUTH
1743 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1744 authchunks
= OPENSSL_zalloc(sockopt_len
);
1745 if (authchunks
== NULL
) {
1749 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1752 OPENSSL_free(authchunks
);
1757 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1758 p
< (unsigned char *)authchunks
+ sockopt_len
;
1759 p
+= sizeof(uint8_t)) {
1760 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1762 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1766 OPENSSL_free(authchunks
);
1768 if (!auth_data
|| !auth_forward
) {
1770 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1771 "Ensure SCTP AUTH chunks are enabled on the "
1772 "underlying socket");
1776 # ifdef SCTP_AUTHENTICATION_EVENT
1778 memset(&event
, 0, sizeof(event
));
1779 event
.se_assoc_id
= 0;
1780 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1783 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1784 sizeof(struct sctp_event
));
1790 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1791 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1797 event
.sctp_authentication_event
= 1;
1800 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1801 sizeof(struct sctp_event_subscribe
));
1810 * Disable partial delivery by setting the min size larger than the max
1811 * record size of 2^14 + 2048 + 13
1814 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1824 int BIO_dgram_is_sctp(BIO
*bio
)
1826 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1829 static int dgram_sctp_new(BIO
*bi
)
1831 bio_dgram_sctp_data
*data
= NULL
;
1835 if ((data
= OPENSSL_zalloc(sizeof(*data
))) == NULL
) {
1836 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
1839 # ifdef SCTP_PR_SCTP_NONE
1840 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1848 static int dgram_sctp_free(BIO
*a
)
1850 bio_dgram_sctp_data
*data
;
1854 if (!dgram_clear(a
))
1857 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1864 # ifdef SCTP_AUTHENTICATION_EVENT
1865 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1866 union sctp_notification
*snp
)
1869 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1871 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1872 struct sctp_authkeyid authkeyid
;
1875 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1876 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1877 &authkeyid
, sizeof(struct sctp_authkeyid
));
1882 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1884 int ret
= 0, n
= 0, i
, optval
;
1886 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1889 struct cmsghdr
*cmsg
;
1893 clear_socket_error();
1896 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1899 msg
.msg_name
= NULL
;
1900 msg
.msg_namelen
= 0;
1903 msg
.msg_control
= cmsgbuf
;
1904 msg
.msg_controllen
= 512;
1906 n
= recvmsg(b
->num
, &msg
, 0);
1914 if (msg
.msg_controllen
> 0) {
1915 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1916 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1917 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1919 # ifdef SCTP_RCVINFO
1920 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1921 struct sctp_rcvinfo
*rcvinfo
;
1923 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1924 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1925 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1926 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1927 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1928 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1929 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1930 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1934 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1935 struct sctp_sndrcvinfo
*sndrcvinfo
;
1938 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1939 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1940 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1941 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1942 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1943 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1944 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1945 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1951 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1952 union sctp_notification snp
;
1954 memcpy(&snp
, out
, sizeof(snp
));
1955 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1957 struct sctp_event event
;
1959 struct sctp_event_subscribe event
;
1960 socklen_t eventsize
;
1963 /* disable sender dry event */
1965 memset(&event
, 0, sizeof(event
));
1966 event
.se_assoc_id
= 0;
1967 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1969 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1970 sizeof(struct sctp_event
));
1976 eventsize
= sizeof(struct sctp_event_subscribe
);
1977 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1984 event
.sctp_sender_dry_event
= 0;
1986 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1987 sizeof(struct sctp_event_subscribe
));
1994 # ifdef SCTP_AUTHENTICATION_EVENT
1995 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1996 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1999 if (data
->handle_notifications
!= NULL
)
2000 data
->handle_notifications(b
, data
->notification_context
,
2003 memset(&snp
, 0, sizeof(snp
));
2004 memset(out
, 0, outl
);
2009 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
2012 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
2013 /* Partial message read, this should never happen! */
2016 * The buffer was too small, this means the peer sent a message
2017 * that was larger than allowed.
2023 * Test if socket buffer can handle max record size (2^14 + 2048
2026 optlen
= (socklen_t
) sizeof(int);
2027 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
2029 OPENSSL_assert(optval
>= 18445);
2032 * Test if SCTP doesn't partially deliver below max record size
2033 * (2^14 + 2048 + 13)
2035 optlen
= (socklen_t
) sizeof(int);
2037 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
2040 OPENSSL_assert(optval
>= 18445);
2043 * Partially delivered notification??? Probably a bug....
2045 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
2048 * Everything seems ok till now, so it's most likely a message
2049 * dropped by PR-SCTP.
2051 memset(out
, 0, outl
);
2052 BIO_set_retry_read(b
);
2056 BIO_clear_retry_flags(b
);
2058 if (BIO_dgram_should_retry(ret
)) {
2059 BIO_set_retry_read(b
);
2060 data
->_errno
= get_last_socket_error();
2064 /* Test if peer uses SCTP-AUTH before continuing */
2065 if (!data
->peer_auth_tested
) {
2066 int ii
, auth_data
= 0, auth_forward
= 0;
2068 struct sctp_authchunks
*authchunks
;
2071 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
2072 authchunks
= OPENSSL_malloc(optlen
);
2073 if (authchunks
== NULL
) {
2074 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
2077 memset(authchunks
, 0, optlen
);
2078 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
2079 authchunks
, &optlen
);
2082 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
2083 p
< (unsigned char *)authchunks
+ optlen
;
2084 p
+= sizeof(uint8_t)) {
2085 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
2087 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
2091 OPENSSL_free(authchunks
);
2093 if (!auth_data
|| !auth_forward
) {
2094 ERR_raise(ERR_LIB_BIO
, BIO_R_CONNECT_ERROR
);
2098 data
->peer_auth_tested
= 1;
2105 * dgram_sctp_write - send message on SCTP socket
2106 * @b: BIO to write to
2108 * @inl: amount of bytes in @in to send
2110 * Returns -1 on error or the sent amount of bytes on success
2112 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
2115 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2116 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
2117 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
2118 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
2119 struct iovec iov
[1];
2121 struct cmsghdr
*cmsg
;
2122 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2123 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
2124 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
2125 struct sctp_sndinfo
*sndinfo
;
2126 struct sctp_prinfo
*prinfo
;
2128 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
2129 struct sctp_sndrcvinfo
*sndrcvinfo
;
2132 clear_socket_error();
2135 * If we're send anything else than application data, disable all user
2136 * parameters and flags.
2139 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
2140 # ifdef SCTP_SACK_IMMEDIATELY
2141 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
2143 sinfo
= &handshake_sinfo
;
2146 /* We can only send a shutdown alert if the socket is dry */
2147 if (data
->save_shutdown
) {
2148 ret
= BIO_dgram_sctp_wait_for_dry(b
);
2152 BIO_clear_retry_flags(b
);
2153 BIO_set_retry_write(b
);
2158 iov
[0].iov_base
= (char *)in
;
2159 iov
[0].iov_len
= inl
;
2160 msg
.msg_name
= NULL
;
2161 msg
.msg_namelen
= 0;
2164 msg
.msg_control
= (caddr_t
) cmsgbuf
;
2165 msg
.msg_controllen
= 0;
2167 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2168 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2169 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2170 cmsg
->cmsg_type
= SCTP_SNDINFO
;
2171 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
2172 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
2173 memset(sndinfo
, 0, sizeof(*sndinfo
));
2174 sndinfo
->snd_sid
= sinfo
->snd_sid
;
2175 sndinfo
->snd_flags
= sinfo
->snd_flags
;
2176 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
2177 sndinfo
->snd_context
= sinfo
->snd_context
;
2178 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
2181 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
2182 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2183 cmsg
->cmsg_type
= SCTP_PRINFO
;
2184 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
2185 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
2186 memset(prinfo
, 0, sizeof(*prinfo
));
2187 prinfo
->pr_policy
= pinfo
->pr_policy
;
2188 prinfo
->pr_value
= pinfo
->pr_value
;
2189 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
2191 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2192 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2193 cmsg
->cmsg_type
= SCTP_SNDRCV
;
2194 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
2195 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
2196 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
2197 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
2198 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
2200 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
2202 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
2203 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
2204 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
2205 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
2208 ret
= sendmsg(b
->num
, &msg
, 0);
2210 BIO_clear_retry_flags(b
);
2212 if (BIO_dgram_should_retry(ret
)) {
2213 BIO_set_retry_write(b
);
2214 data
->_errno
= get_last_socket_error();
2220 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
2223 bio_dgram_sctp_data
*data
= NULL
;
2224 socklen_t sockopt_len
= 0;
2225 struct sctp_authkeyid authkeyid
;
2226 struct sctp_authkey
*authkey
= NULL
;
2228 data
= (bio_dgram_sctp_data
*) b
->ptr
;
2231 case BIO_CTRL_DGRAM_QUERY_MTU
:
2233 * Set to maximum (2^14) and ignore user input to enable transport
2234 * protocol fragmentation. Returns always 2^14.
2239 case BIO_CTRL_DGRAM_SET_MTU
:
2241 * Set to maximum (2^14) and ignore input to enable transport
2242 * protocol fragmentation. Returns always 2^14.
2247 case BIO_CTRL_DGRAM_SET_CONNECTED
:
2248 case BIO_CTRL_DGRAM_CONNECT
:
2249 /* Returns always -1. */
2252 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
2254 * SCTP doesn't need the DTLS timer Returns always 1.
2257 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
2259 * We allow transport protocol fragmentation so this is irrelevant
2263 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
2265 data
->in_handshake
= 1;
2267 data
->in_handshake
= 0;
2270 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
2271 &data
->in_handshake
, sizeof(int));
2273 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
2275 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2278 /* Get active key */
2279 sockopt_len
= sizeof(struct sctp_authkeyid
);
2281 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2287 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
2288 authkey
= OPENSSL_malloc(sockopt_len
);
2289 if (authkey
== NULL
) {
2293 memset(authkey
, 0, sockopt_len
);
2294 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
2295 # ifndef __FreeBSD__
2297 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2298 * and higher work without it.
2300 authkey
->sca_keylength
= 64;
2302 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
2305 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
2307 OPENSSL_free(authkey
);
2312 /* Reset active key */
2313 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2314 &authkeyid
, sizeof(struct sctp_authkeyid
));
2319 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
2320 /* Returns 0 on success, -1 otherwise. */
2322 /* Get active key */
2323 sockopt_len
= sizeof(struct sctp_authkeyid
);
2325 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2330 /* Set active key */
2331 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
2332 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2333 &authkeyid
, sizeof(struct sctp_authkeyid
));
2338 * CCS has been sent, so remember that and fall through to check if
2339 * we need to deactivate an old key
2344 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
2345 /* Returns 0 on success, -1 otherwise. */
2348 * Has this command really been called or is this just a
2351 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
2355 * CSS has been both, received and sent, so deactivate an old key
2357 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
2358 /* Get active key */
2359 sockopt_len
= sizeof(struct sctp_authkeyid
);
2361 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2362 &authkeyid
, &sockopt_len
);
2367 * Deactivate key or delete second last key if
2368 * SCTP_AUTHENTICATION_EVENT is not available.
2370 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2371 # ifdef SCTP_AUTH_DEACTIVATE_KEY
2372 sockopt_len
= sizeof(struct sctp_authkeyid
);
2373 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
2374 &authkeyid
, sockopt_len
);
2378 # ifndef SCTP_AUTHENTICATION_EVENT
2379 if (authkeyid
.scact_keynumber
> 0) {
2380 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2381 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
2382 &authkeyid
, sizeof(struct sctp_authkeyid
));
2392 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
2393 /* Returns the size of the copied struct. */
2394 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2395 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2397 memcpy(ptr
, &(data
->sndinfo
), num
);
2400 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
2401 /* Returns the size of the copied struct. */
2402 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2403 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2405 memcpy(&(data
->sndinfo
), ptr
, num
);
2407 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
2408 /* Returns the size of the copied struct. */
2409 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2410 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2412 memcpy(ptr
, &data
->rcvinfo
, num
);
2416 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
2417 /* Returns the size of the copied struct. */
2418 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2419 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2421 memcpy(&(data
->rcvinfo
), ptr
, num
);
2423 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
2424 /* Returns the size of the copied struct. */
2425 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2426 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2428 memcpy(ptr
, &(data
->prinfo
), num
);
2431 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
2432 /* Returns the size of the copied struct. */
2433 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2434 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2436 memcpy(&(data
->prinfo
), ptr
, num
);
2438 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
2439 /* Returns always 1. */
2441 data
->save_shutdown
= 1;
2443 data
->save_shutdown
= 0;
2445 case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
:
2446 return dgram_sctp_wait_for_dry(b
);
2447 case BIO_CTRL_DGRAM_SCTP_MSG_WAITING
:
2448 return dgram_sctp_msg_waiting(b
);
2452 * Pass to default ctrl function to process SCTP unspecific commands
2454 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
2460 int BIO_dgram_sctp_notification_cb(BIO
*b
,
2461 BIO_dgram_sctp_notification_handler_fn handle_notifications
,
2464 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2466 if (handle_notifications
!= NULL
) {
2467 data
->handle_notifications
= handle_notifications
;
2468 data
->notification_context
= context
;
2476 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2477 * @b: The BIO to check for the dry event
2479 * Wait until the peer confirms all packets have been received, and so that
2480 * our kernel doesn't have anything to send anymore. This is only received by
2481 * the peer's kernel, not the application.
2485 * 0 when not dry yet
2488 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
2490 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
, 0, NULL
);
2493 static int dgram_sctp_wait_for_dry(BIO
*b
)
2498 union sctp_notification snp
;
2502 struct sctp_event event
;
2504 struct sctp_event_subscribe event
;
2505 socklen_t eventsize
;
2507 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2509 /* set sender dry event */
2511 memset(&event
, 0, sizeof(event
));
2512 event
.se_assoc_id
= 0;
2513 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2516 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2517 sizeof(struct sctp_event
));
2519 eventsize
= sizeof(struct sctp_event_subscribe
);
2520 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
2524 event
.sctp_sender_dry_event
= 1;
2527 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2528 sizeof(struct sctp_event_subscribe
));
2533 /* peek for notification */
2534 memset(&snp
, 0, sizeof(snp
));
2535 iov
.iov_base
= (char *)&snp
;
2536 iov
.iov_len
= sizeof(union sctp_notification
);
2537 msg
.msg_name
= NULL
;
2538 msg
.msg_namelen
= 0;
2541 msg
.msg_control
= NULL
;
2542 msg
.msg_controllen
= 0;
2545 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2547 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2548 && (get_last_socket_error() != EWOULDBLOCK
))
2554 /* if we find a notification, process it and try again if necessary */
2555 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
2556 memset(&snp
, 0, sizeof(snp
));
2557 iov
.iov_base
= (char *)&snp
;
2558 iov
.iov_len
= sizeof(union sctp_notification
);
2559 msg
.msg_name
= NULL
;
2560 msg
.msg_namelen
= 0;
2563 msg
.msg_control
= NULL
;
2564 msg
.msg_controllen
= 0;
2567 n
= recvmsg(b
->num
, &msg
, 0);
2569 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2570 && (get_last_socket_error() != EWOULDBLOCK
))
2576 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
2579 /* disable sender dry event */
2581 memset(&event
, 0, sizeof(event
));
2582 event
.se_assoc_id
= 0;
2583 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2586 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2587 sizeof(struct sctp_event
));
2589 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
2591 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2596 event
.sctp_sender_dry_event
= 0;
2599 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2600 sizeof(struct sctp_event_subscribe
));
2605 # ifdef SCTP_AUTHENTICATION_EVENT
2606 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2607 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2610 if (data
->handle_notifications
!= NULL
)
2611 data
->handle_notifications(b
, data
->notification_context
,
2614 /* found notification, peek again */
2615 memset(&snp
, 0, sizeof(snp
));
2616 iov
.iov_base
= (char *)&snp
;
2617 iov
.iov_len
= sizeof(union sctp_notification
);
2618 msg
.msg_name
= NULL
;
2619 msg
.msg_namelen
= 0;
2622 msg
.msg_control
= NULL
;
2623 msg
.msg_controllen
= 0;
2626 /* if we have seen the dry already, don't wait */
2628 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2629 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2632 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2635 fcntl(b
->num
, F_SETFL
, sockflags
);
2639 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2640 && (get_last_socket_error() != EWOULDBLOCK
))
2647 /* read anything else */
2651 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
2653 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_MSG_WAITING
, 0, NULL
);
2656 static int dgram_sctp_msg_waiting(BIO
*b
)
2659 union sctp_notification snp
;
2662 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2664 /* Check if there are any messages waiting to be read */
2666 memset(&snp
, 0, sizeof(snp
));
2667 iov
.iov_base
= (char *)&snp
;
2668 iov
.iov_len
= sizeof(union sctp_notification
);
2669 msg
.msg_name
= NULL
;
2670 msg
.msg_namelen
= 0;
2673 msg
.msg_control
= NULL
;
2674 msg
.msg_controllen
= 0;
2677 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2678 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2679 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2680 fcntl(b
->num
, F_SETFL
, sockflags
);
2682 /* if notification, process and try again */
2683 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
2684 # ifdef SCTP_AUTHENTICATION_EVENT
2685 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2686 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2689 memset(&snp
, 0, sizeof(snp
));
2690 iov
.iov_base
= (char *)&snp
;
2691 iov
.iov_len
= sizeof(union sctp_notification
);
2692 msg
.msg_name
= NULL
;
2693 msg
.msg_namelen
= 0;
2696 msg
.msg_control
= NULL
;
2697 msg
.msg_controllen
= 0;
2699 n
= recvmsg(b
->num
, &msg
, 0);
2701 if (data
->handle_notifications
!= NULL
)
2702 data
->handle_notifications(b
, data
->notification_context
,
2706 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
2708 /* Return 1 if there is a message to be read, return 0 otherwise. */
2715 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
2720 ret
= dgram_sctp_write(bp
, str
, n
);
2725 static int BIO_dgram_should_retry(int i
)
2729 if ((i
== 0) || (i
== -1)) {
2730 err
= get_last_socket_error();
2732 # if defined(OPENSSL_SYS_WINDOWS)
2734 * If the socket return value (i) is -1 and err is unexpectedly 0 at
2735 * this point, the error code was overwritten by another system call
2736 * before this error handling is called.
2740 return BIO_dgram_non_fatal_error(err
);
2745 int BIO_dgram_non_fatal_error(int err
)
2748 # if defined(OPENSSL_SYS_WINDOWS)
2749 # if defined(WSAEWOULDBLOCK)
2750 case WSAEWOULDBLOCK
:
2755 # ifdef WSAEWOULDBLOCK
2756 # if WSAEWOULDBLOCK != EWOULDBLOCK
2769 # if EWOULDBLOCK != EAGAIN
2793 static void get_current_time(struct timeval
*t
)
2795 # if defined(_WIN32)
2797 unsigned __int64 now_ul
;
2801 SystemTimeToFileTime(&st
, &now_ft
);
2802 now_ul
= ((unsigned __int64
)now_ft
.dwHighDateTime
<< 32) | now_ft
.dwLowDateTime
;
2804 now_ul
-= 116444736000000000ULL;
2806 now_ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
2808 t
->tv_sec
= (long)(now_ul
/ 10000000);
2809 t
->tv_usec
= ((int)(now_ul
% 10000000)) / 10;
2811 if (gettimeofday(t
, NULL
) < 0)
2812 perror("gettimeofday");