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
;
1157 * At least FreeBSD is very pedantic about using IP_SENDSRCADDR when we
1158 * are not bound to 0.0.0.0 or ::, even if the address matches what we
1159 * bound to. Support this by not packing the structure if the address
1160 * matches our understanding of our local address. IP_SENDSRCADDR is a
1161 * BSD thing, so we don't need an explicit test for BSD here.
1163 if (local
->s_in
.sin_addr
.s_addr
== data
->local_addr
.s_in
.sin_addr
.s_addr
) {
1164 mh
->msg_control
= NULL
;
1165 mh
->msg_controllen
= 0;
1169 cmsg
= (struct cmsghdr
*)mh
->msg_control
;
1170 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in_addr
));
1171 cmsg
->cmsg_level
= IPPROTO_IP
;
1172 cmsg
->cmsg_type
= IP_SENDSRCADDR
;
1174 info
= (struct in_addr
*)BIO_CMSG_DATA(cmsg
);
1175 *info
= local
->s_in
.sin_addr
;
1177 /* See comment above. */
1178 if (local
->s_in
.sin_port
!= 0
1179 && data
->local_addr
.s_in
.sin_port
!= local
->s_in
.sin_port
) {
1180 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1184 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in_addr
));
1188 # if OPENSSL_USE_IPV6
1189 else if (af
== AF_INET6
) {
1190 # if defined(IPV6_PKTINFO)
1191 struct in6_pktinfo
*info
;
1193 # if defined(OPENSSL_SYS_WINDOWS)
1194 cmsg
= (CMSGHDR_TYPE
*)mh
->Control
.buf
;
1196 cmsg
= (CMSGHDR_TYPE
*)mh
->msg_control
;
1198 cmsg
->cmsg_len
= BIO_CMSG_LEN(sizeof(struct in6_pktinfo
));
1199 cmsg
->cmsg_level
= IPPROTO_IPV6
;
1200 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1202 info
= (struct in6_pktinfo
*)BIO_CMSG_DATA(cmsg
);
1203 info
->ipi6_addr
= local
->s_in6
.sin6_addr
;
1204 info
->ipi6_ifindex
= 0;
1207 * See comment above, but also applies to the other fields
1210 if (local
->s_in6
.sin6_port
!= 0
1211 && data
->local_addr
.s_in6
.sin6_port
!= local
->s_in6
.sin6_port
) {
1212 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1216 if (local
->s_in6
.sin6_scope_id
!= 0
1217 && data
->local_addr
.s_in6
.sin6_scope_id
!= local
->s_in6
.sin6_scope_id
) {
1218 ERR_raise(ERR_LIB_BIO
, BIO_R_PORT_MISMATCH
);
1222 # if defined(OPENSSL_SYS_WINDOWS)
1223 mh
->Control
.len
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1225 mh
->msg_controllen
= BIO_CMSG_SPACE(sizeof(struct in6_pktinfo
));
1237 * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
1238 * should mask out any system flags returned by this function you cannot support
1239 * in a particular circumstance. Currently no flags are defined.
1241 # if M_METHOD != M_METHOD_NONE
1242 static int translate_flags(uint64_t flags
) {
1247 static int dgram_sendmmsg(BIO
*b
, BIO_MSG
*msg
, size_t stride
,
1248 size_t num_msg
, uint64_t flags
, size_t *num_processed
)
1250 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1253 # if M_METHOD == M_METHOD_RECVMMSG
1254 # define BIO_MAX_MSGS_PER_CALL 64
1256 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1258 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1259 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1260 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1261 int have_local_enabled
= data
->local_addr_enabled
;
1262 # elif M_METHOD == M_METHOD_RECVMSG
1264 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1268 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1269 int have_local_enabled
= data
->local_addr_enabled
;
1270 # elif M_METHOD == M_METHOD_WSARECVMSG
1271 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1272 int have_local_enabled
= data
->local_addr_enabled
;
1275 DWORD num_bytes_sent
= 0;
1276 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1278 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1287 if (num_msg
> OSSL_SSIZE_MAX
)
1288 num_msg
= OSSL_SSIZE_MAX
;
1290 # if M_METHOD != M_METHOD_NONE
1291 sysflags
= translate_flags(flags
);
1294 # if M_METHOD == M_METHOD_RECVMMSG
1296 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1297 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1298 * we place a fixed limit on the number of messages per call, in the
1299 * expectation that we will be called again if there were more messages to
1302 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1303 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1305 for (i
= 0; i
< num_msg
; ++i
) {
1306 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1307 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1309 /* If local address was requested, it must have been enabled */
1310 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
) {
1311 if (!have_local_enabled
) {
1312 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1317 if (pack_local(b
, &mh
[i
].msg_hdr
,
1318 BIO_MSG_N(msg
, stride
, i
).local
) < 1) {
1319 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1327 ret
= sendmmsg(b
->num
, mh
, num_msg
, sysflags
);
1329 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1334 for (i
= 0; i
< (size_t)ret
; ++i
) {
1335 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1336 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1339 *num_processed
= (size_t)ret
;
1342 # elif M_METHOD == M_METHOD_RECVMSG
1344 * If sendmsg is available, use it.
1346 translate_msg(b
, &mh
, &iov
, control
, msg
);
1348 if (msg
->local
!= NULL
) {
1349 if (!have_local_enabled
) {
1350 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1355 if (pack_local(b
, &mh
, msg
->local
) < 1) {
1356 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1362 l
= sendmsg(b
->num
, &mh
, sysflags
);
1364 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1369 msg
->data_len
= (size_t)l
;
1374 # elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
1375 # if M_METHOD == M_METHOD_WSARECVMSG
1376 if (bio_WSASendMsg
!= NULL
) {
1377 /* WSASendMsg-based implementation for Windows. */
1378 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1380 if (msg
[0].local
!= NULL
) {
1381 if (!have_local_enabled
) {
1382 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1387 if (pack_local(b
, &wmsg
, msg
[0].local
) < 1) {
1388 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1394 ret
= WSASendMsg((SOCKET
)b
->num
, &wmsg
, 0, &num_bytes_sent
, NULL
, NULL
);
1396 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1401 msg
[0].data_len
= num_bytes_sent
;
1409 * Fallback to sendto and send a single message.
1411 if (msg
[0].local
!= NULL
) {
1413 * We cannot set the local address if using sendto
1414 * so fail in this case
1416 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1421 ret
= sendto(b
->num
, msg
[0].data
,
1422 # if defined(OPENSSL_SYS_WINDOWS)
1423 (int)msg
[0].data_len
,
1428 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1429 msg
[0].peer
!= NULL
? sizeof(*msg
[0].peer
) : 0);
1431 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1436 msg
[0].data_len
= ret
;
1442 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1448 static int dgram_recvmmsg(BIO
*b
, BIO_MSG
*msg
,
1449 size_t stride
, size_t num_msg
,
1450 uint64_t flags
, size_t *num_processed
)
1452 # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1455 # if M_METHOD == M_METHOD_RECVMMSG
1457 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1459 struct mmsghdr mh
[BIO_MAX_MSGS_PER_CALL
];
1460 struct iovec iov
[BIO_MAX_MSGS_PER_CALL
];
1461 unsigned char control
[BIO_MAX_MSGS_PER_CALL
][BIO_CMSG_ALLOC_LEN
];
1462 int have_local_enabled
= data
->local_addr_enabled
;
1463 # elif M_METHOD == M_METHOD_RECVMSG
1465 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1469 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1470 int have_local_enabled
= data
->local_addr_enabled
;
1471 # elif M_METHOD == M_METHOD_WSARECVMSG
1472 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
1473 int have_local_enabled
= data
->local_addr_enabled
;
1476 DWORD num_bytes_received
= 0;
1477 unsigned char control
[BIO_CMSG_ALLOC_LEN
];
1479 # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1489 if (num_msg
> OSSL_SSIZE_MAX
)
1490 num_msg
= OSSL_SSIZE_MAX
;
1492 # if M_METHOD != M_METHOD_NONE
1493 sysflags
= translate_flags(flags
);
1496 # if M_METHOD == M_METHOD_RECVMMSG
1498 * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1499 * msghdr and struct iovec on the stack to support multithreaded use. Thus
1500 * we place a fixed limit on the number of messages per call, in the
1501 * expectation that we will be called again if there were more messages to
1504 if (num_msg
> BIO_MAX_MSGS_PER_CALL
)
1505 num_msg
= BIO_MAX_MSGS_PER_CALL
;
1507 for (i
= 0; i
< num_msg
; ++i
) {
1508 translate_msg(b
, &mh
[i
].msg_hdr
, &iov
[i
],
1509 control
[i
], &BIO_MSG_N(msg
, stride
, i
));
1511 /* If local address was requested, it must have been enabled */
1512 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
&& !have_local_enabled
) {
1513 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1520 ret
= recvmmsg(b
->num
, mh
, num_msg
, sysflags
, NULL
);
1522 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1527 for (i
= 0; i
< (size_t)ret
; ++i
) {
1528 BIO_MSG_N(msg
, stride
, i
).data_len
= mh
[i
].msg_len
;
1529 BIO_MSG_N(msg
, stride
, i
).flags
= 0;
1531 * *(msg->peer) will have been filled in by recvmmsg;
1532 * for msg->local we parse the control data returned
1534 if (BIO_MSG_N(msg
, stride
, i
).local
!= NULL
)
1535 if (extract_local(b
, &mh
[i
].msg_hdr
,
1536 BIO_MSG_N(msg
, stride
, i
).local
) < 1)
1538 * It appears BSDs do not support local addresses for
1539 * loopback sockets. In this case, just clear the local
1540 * address, as for OS X and Windows in some circumstances
1543 BIO_ADDR_clear(msg
->local
);
1546 *num_processed
= (size_t)ret
;
1549 # elif M_METHOD == M_METHOD_RECVMSG
1551 * If recvmsg is available, use it.
1553 translate_msg(b
, &mh
, &iov
, control
, msg
);
1555 /* If local address was requested, it must have been enabled */
1556 if (msg
->local
!= NULL
&& !have_local_enabled
) {
1558 * If we have done at least one message, we must return the
1559 * count; if we haven't done any, we can give an error code
1561 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1566 l
= recvmsg(b
->num
, &mh
, sysflags
);
1568 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1573 msg
->data_len
= (size_t)l
;
1576 if (msg
->local
!= NULL
)
1577 if (extract_local(b
, &mh
, msg
->local
) < 1)
1579 * OS X exhibits odd behaviour where it appears that if a packet is
1580 * sent before the receiving interface enables IP_PKTINFO, it will
1581 * sometimes not have any control data returned even if the
1582 * receiving interface enables IP_PKTINFO before calling recvmsg().
1583 * This appears to occur non-deterministically. Presumably, OS X
1584 * handles IP_PKTINFO at the time the packet is enqueued into a
1585 * socket's receive queue, rather than at the time recvmsg() is
1586 * called, unlike most other operating systems. Thus (if this
1587 * hypothesis is correct) there is a race between where IP_PKTINFO
1588 * is enabled by the process and when the kernel's network stack
1589 * queues the incoming message.
1591 * We cannot return the local address if we do not have it, but this
1592 * is not a caller error either, so just return a zero address
1593 * structure. This is similar to how we handle Windows loopback
1594 * interfaces (see below). We enable this workaround for all
1595 * platforms, not just Apple, as this kind of quirk in OS networking
1596 * stacks seems to be common enough that failing hard if a local
1597 * address is not provided appears to be too brittle.
1599 BIO_ADDR_clear(msg
->local
);
1604 # elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1605 # if M_METHOD == M_METHOD_WSARECVMSG
1606 if (bio_WSARecvMsg
!= NULL
) {
1607 /* WSARecvMsg-based implementation for Windows. */
1608 translate_msg_win(b
, &wmsg
, &wbuf
, control
, msg
);
1610 /* If local address was requested, it must have been enabled */
1611 if (msg
[0].local
!= NULL
&& !have_local_enabled
) {
1612 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1617 ret
= WSARecvMsg((SOCKET
)b
->num
, &wmsg
, &num_bytes_received
, NULL
, NULL
);
1619 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1624 msg
[0].data_len
= num_bytes_received
;
1626 if (msg
[0].local
!= NULL
)
1627 if (extract_local(b
, &wmsg
, msg
[0].local
) < 1)
1629 * On Windows, loopback is not a "proper" interface and it works
1630 * differently; packets are essentially short-circuited and
1631 * don't go through all of the normal processing. A consequence
1632 * of this is that packets sent from the local machine to the
1633 * local machine _will not have IP_PKTINFO_ even if the
1634 * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
1635 * Control.len to 0 on returning.
1637 * This applies regardless of whether the loopback address,
1638 * 127.0.0.1 is used, or a local interface address (e.g.
1639 * 192.168.1.1); in both cases IP_PKTINFO will not be present.
1641 * We report this condition by setting the local BIO_ADDR's
1644 BIO_ADDR_clear(msg
[0].local
);
1652 * Fallback to recvfrom and receive a single message.
1654 if (msg
[0].local
!= NULL
) {
1656 * We cannot determine the local address if using recvfrom
1657 * so fail in this case
1659 ERR_raise(ERR_LIB_BIO
, BIO_R_LOCAL_ADDR_NOT_AVAILABLE
);
1664 slen
= sizeof(*msg
[0].peer
);
1665 ret
= recvfrom(b
->num
, msg
[0].data
,
1666 # if defined(OPENSSL_SYS_WINDOWS)
1667 (int)msg
[0].data_len
,
1672 msg
[0].peer
!= NULL
? &msg
[0].peer
->sa
: NULL
,
1673 msg
[0].peer
!= NULL
? &slen
: NULL
);
1675 ERR_raise(ERR_LIB_SYS
, get_last_socket_error());
1679 msg
[0].data_len
= ret
;
1685 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
1691 # ifndef OPENSSL_NO_SCTP
1692 const BIO_METHOD
*BIO_s_datagram_sctp(void)
1694 return &methods_dgramp_sctp
;
1697 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
1700 int ret
, optval
= 20000;
1701 int auth_data
= 0, auth_forward
= 0;
1703 struct sctp_authchunk auth
;
1704 struct sctp_authchunks
*authchunks
;
1705 socklen_t sockopt_len
;
1706 # ifdef SCTP_AUTHENTICATION_EVENT
1708 struct sctp_event event
;
1710 struct sctp_event_subscribe event
;
1714 bio
= BIO_new(BIO_s_datagram_sctp());
1717 BIO_set_fd(bio
, fd
, close_flag
);
1719 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1720 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
1722 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1723 sizeof(struct sctp_authchunk
));
1726 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1727 "Ensure SCTP AUTH chunks are enabled in kernel");
1730 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
1732 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1733 sizeof(struct sctp_authchunk
));
1736 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1737 "Ensure SCTP AUTH chunks are enabled in kernel");
1742 * Test if activation was successful. When using accept(), SCTP-AUTH has
1743 * to be activated for the listening socket already, otherwise the
1744 * connected socket won't use it. Similarly with connect(): the socket
1745 * prior to connection must be activated for SCTP-AUTH
1747 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1748 authchunks
= OPENSSL_zalloc(sockopt_len
);
1749 if (authchunks
== NULL
) {
1753 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1756 OPENSSL_free(authchunks
);
1761 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1762 p
< (unsigned char *)authchunks
+ sockopt_len
;
1763 p
+= sizeof(uint8_t)) {
1764 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1766 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1770 OPENSSL_free(authchunks
);
1772 if (!auth_data
|| !auth_forward
) {
1774 ERR_raise_data(ERR_LIB_BIO
, ERR_R_SYS_LIB
,
1775 "Ensure SCTP AUTH chunks are enabled on the "
1776 "underlying socket");
1780 # ifdef SCTP_AUTHENTICATION_EVENT
1782 memset(&event
, 0, sizeof(event
));
1783 event
.se_assoc_id
= 0;
1784 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1787 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1788 sizeof(struct sctp_event
));
1794 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1795 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1801 event
.sctp_authentication_event
= 1;
1804 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1805 sizeof(struct sctp_event_subscribe
));
1814 * Disable partial delivery by setting the min size larger than the max
1815 * record size of 2^14 + 2048 + 13
1818 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1828 int BIO_dgram_is_sctp(BIO
*bio
)
1830 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1833 static int dgram_sctp_new(BIO
*bi
)
1835 bio_dgram_sctp_data
*data
= NULL
;
1839 if ((data
= OPENSSL_zalloc(sizeof(*data
))) == NULL
) {
1840 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
1843 # ifdef SCTP_PR_SCTP_NONE
1844 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1852 static int dgram_sctp_free(BIO
*a
)
1854 bio_dgram_sctp_data
*data
;
1858 if (!dgram_clear(a
))
1861 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1868 # ifdef SCTP_AUTHENTICATION_EVENT
1869 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1870 union sctp_notification
*snp
)
1873 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1875 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1876 struct sctp_authkeyid authkeyid
;
1879 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1880 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1881 &authkeyid
, sizeof(struct sctp_authkeyid
));
1886 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1888 int ret
= 0, n
= 0, i
, optval
;
1890 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1893 struct cmsghdr
*cmsg
;
1897 clear_socket_error();
1900 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1903 msg
.msg_name
= NULL
;
1904 msg
.msg_namelen
= 0;
1907 msg
.msg_control
= cmsgbuf
;
1908 msg
.msg_controllen
= 512;
1910 n
= recvmsg(b
->num
, &msg
, 0);
1918 if (msg
.msg_controllen
> 0) {
1919 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1920 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1921 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1923 # ifdef SCTP_RCVINFO
1924 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1925 struct sctp_rcvinfo
*rcvinfo
;
1927 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1928 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1929 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1930 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1931 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1932 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1933 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1934 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1938 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1939 struct sctp_sndrcvinfo
*sndrcvinfo
;
1942 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1943 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1944 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1945 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1946 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1947 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1948 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1949 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1955 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1956 union sctp_notification snp
;
1958 memcpy(&snp
, out
, sizeof(snp
));
1959 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1961 struct sctp_event event
;
1963 struct sctp_event_subscribe event
;
1964 socklen_t eventsize
;
1967 /* disable sender dry event */
1969 memset(&event
, 0, sizeof(event
));
1970 event
.se_assoc_id
= 0;
1971 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1973 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1974 sizeof(struct sctp_event
));
1980 eventsize
= sizeof(struct sctp_event_subscribe
);
1981 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1988 event
.sctp_sender_dry_event
= 0;
1990 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1991 sizeof(struct sctp_event_subscribe
));
1998 # ifdef SCTP_AUTHENTICATION_EVENT
1999 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2000 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2003 if (data
->handle_notifications
!= NULL
)
2004 data
->handle_notifications(b
, data
->notification_context
,
2007 memset(&snp
, 0, sizeof(snp
));
2008 memset(out
, 0, outl
);
2013 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
2016 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
2017 /* Partial message read, this should never happen! */
2020 * The buffer was too small, this means the peer sent a message
2021 * that was larger than allowed.
2027 * Test if socket buffer can handle max record size (2^14 + 2048
2030 optlen
= (socklen_t
) sizeof(int);
2031 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
2033 OPENSSL_assert(optval
>= 18445);
2036 * Test if SCTP doesn't partially deliver below max record size
2037 * (2^14 + 2048 + 13)
2039 optlen
= (socklen_t
) sizeof(int);
2041 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
2044 OPENSSL_assert(optval
>= 18445);
2047 * Partially delivered notification??? Probably a bug....
2049 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
2052 * Everything seems ok till now, so it's most likely a message
2053 * dropped by PR-SCTP.
2055 memset(out
, 0, outl
);
2056 BIO_set_retry_read(b
);
2060 BIO_clear_retry_flags(b
);
2062 if (BIO_dgram_should_retry(ret
)) {
2063 BIO_set_retry_read(b
);
2064 data
->_errno
= get_last_socket_error();
2068 /* Test if peer uses SCTP-AUTH before continuing */
2069 if (!data
->peer_auth_tested
) {
2070 int ii
, auth_data
= 0, auth_forward
= 0;
2072 struct sctp_authchunks
*authchunks
;
2075 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
2076 authchunks
= OPENSSL_malloc(optlen
);
2077 if (authchunks
== NULL
) {
2078 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
2081 memset(authchunks
, 0, optlen
);
2082 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
2083 authchunks
, &optlen
);
2086 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
2087 p
< (unsigned char *)authchunks
+ optlen
;
2088 p
+= sizeof(uint8_t)) {
2089 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
2091 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
2095 OPENSSL_free(authchunks
);
2097 if (!auth_data
|| !auth_forward
) {
2098 ERR_raise(ERR_LIB_BIO
, BIO_R_CONNECT_ERROR
);
2102 data
->peer_auth_tested
= 1;
2109 * dgram_sctp_write - send message on SCTP socket
2110 * @b: BIO to write to
2112 * @inl: amount of bytes in @in to send
2114 * Returns -1 on error or the sent amount of bytes on success
2116 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
2119 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2120 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
2121 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
2122 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
2123 struct iovec iov
[1];
2125 struct cmsghdr
*cmsg
;
2126 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2127 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
2128 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
2129 struct sctp_sndinfo
*sndinfo
;
2130 struct sctp_prinfo
*prinfo
;
2132 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
2133 struct sctp_sndrcvinfo
*sndrcvinfo
;
2136 clear_socket_error();
2139 * If we're send anything else than application data, disable all user
2140 * parameters and flags.
2143 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
2144 # ifdef SCTP_SACK_IMMEDIATELY
2145 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
2147 sinfo
= &handshake_sinfo
;
2150 /* We can only send a shutdown alert if the socket is dry */
2151 if (data
->save_shutdown
) {
2152 ret
= BIO_dgram_sctp_wait_for_dry(b
);
2156 BIO_clear_retry_flags(b
);
2157 BIO_set_retry_write(b
);
2162 iov
[0].iov_base
= (char *)in
;
2163 iov
[0].iov_len
= inl
;
2164 msg
.msg_name
= NULL
;
2165 msg
.msg_namelen
= 0;
2168 msg
.msg_control
= (caddr_t
) cmsgbuf
;
2169 msg
.msg_controllen
= 0;
2171 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2172 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2173 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2174 cmsg
->cmsg_type
= SCTP_SNDINFO
;
2175 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
2176 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
2177 memset(sndinfo
, 0, sizeof(*sndinfo
));
2178 sndinfo
->snd_sid
= sinfo
->snd_sid
;
2179 sndinfo
->snd_flags
= sinfo
->snd_flags
;
2180 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
2181 sndinfo
->snd_context
= sinfo
->snd_context
;
2182 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
2185 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
2186 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2187 cmsg
->cmsg_type
= SCTP_PRINFO
;
2188 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
2189 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
2190 memset(prinfo
, 0, sizeof(*prinfo
));
2191 prinfo
->pr_policy
= pinfo
->pr_policy
;
2192 prinfo
->pr_value
= pinfo
->pr_value
;
2193 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
2195 cmsg
= (struct cmsghdr
*)cmsgbuf
;
2196 cmsg
->cmsg_level
= IPPROTO_SCTP
;
2197 cmsg
->cmsg_type
= SCTP_SNDRCV
;
2198 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
2199 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
2200 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
2201 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
2202 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
2204 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
2206 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
2207 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
2208 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
2209 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
2212 ret
= sendmsg(b
->num
, &msg
, 0);
2214 BIO_clear_retry_flags(b
);
2216 if (BIO_dgram_should_retry(ret
)) {
2217 BIO_set_retry_write(b
);
2218 data
->_errno
= get_last_socket_error();
2224 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
2227 bio_dgram_sctp_data
*data
= NULL
;
2228 socklen_t sockopt_len
= 0;
2229 struct sctp_authkeyid authkeyid
;
2230 struct sctp_authkey
*authkey
= NULL
;
2232 data
= (bio_dgram_sctp_data
*) b
->ptr
;
2235 case BIO_CTRL_DGRAM_QUERY_MTU
:
2237 * Set to maximum (2^14) and ignore user input to enable transport
2238 * protocol fragmentation. Returns always 2^14.
2243 case BIO_CTRL_DGRAM_SET_MTU
:
2245 * Set to maximum (2^14) and ignore input to enable transport
2246 * protocol fragmentation. Returns always 2^14.
2251 case BIO_CTRL_DGRAM_SET_CONNECTED
:
2252 case BIO_CTRL_DGRAM_CONNECT
:
2253 /* Returns always -1. */
2256 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
2258 * SCTP doesn't need the DTLS timer Returns always 1.
2261 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
2263 * We allow transport protocol fragmentation so this is irrelevant
2267 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
2269 data
->in_handshake
= 1;
2271 data
->in_handshake
= 0;
2274 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
2275 &data
->in_handshake
, sizeof(int));
2277 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
2279 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2282 /* Get active key */
2283 sockopt_len
= sizeof(struct sctp_authkeyid
);
2285 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2291 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
2292 authkey
= OPENSSL_malloc(sockopt_len
);
2293 if (authkey
== NULL
) {
2297 memset(authkey
, 0, sockopt_len
);
2298 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
2299 # ifndef __FreeBSD__
2301 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2302 * and higher work without it.
2304 authkey
->sca_keylength
= 64;
2306 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
2309 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
2311 OPENSSL_free(authkey
);
2316 /* Reset active key */
2317 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2318 &authkeyid
, sizeof(struct sctp_authkeyid
));
2323 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
2324 /* Returns 0 on success, -1 otherwise. */
2326 /* Get active key */
2327 sockopt_len
= sizeof(struct sctp_authkeyid
);
2329 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
2334 /* Set active key */
2335 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
2336 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2337 &authkeyid
, sizeof(struct sctp_authkeyid
));
2342 * CCS has been sent, so remember that and fall through to check if
2343 * we need to deactivate an old key
2348 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
2349 /* Returns 0 on success, -1 otherwise. */
2352 * Has this command really been called or is this just a
2355 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
2359 * CSS has been both, received and sent, so deactivate an old key
2361 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
2362 /* Get active key */
2363 sockopt_len
= sizeof(struct sctp_authkeyid
);
2365 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
2366 &authkeyid
, &sockopt_len
);
2371 * Deactivate key or delete second last key if
2372 * SCTP_AUTHENTICATION_EVENT is not available.
2374 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2375 # ifdef SCTP_AUTH_DEACTIVATE_KEY
2376 sockopt_len
= sizeof(struct sctp_authkeyid
);
2377 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
2378 &authkeyid
, sockopt_len
);
2382 # ifndef SCTP_AUTHENTICATION_EVENT
2383 if (authkeyid
.scact_keynumber
> 0) {
2384 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
2385 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
2386 &authkeyid
, sizeof(struct sctp_authkeyid
));
2396 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
2397 /* Returns the size of the copied struct. */
2398 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2399 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2401 memcpy(ptr
, &(data
->sndinfo
), num
);
2404 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
2405 /* Returns the size of the copied struct. */
2406 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
2407 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
2409 memcpy(&(data
->sndinfo
), ptr
, num
);
2411 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
2412 /* Returns the size of the copied struct. */
2413 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2414 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2416 memcpy(ptr
, &data
->rcvinfo
, num
);
2420 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
2421 /* Returns the size of the copied struct. */
2422 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
2423 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
2425 memcpy(&(data
->rcvinfo
), ptr
, num
);
2427 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
2428 /* Returns the size of the copied struct. */
2429 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2430 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2432 memcpy(ptr
, &(data
->prinfo
), num
);
2435 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
2436 /* Returns the size of the copied struct. */
2437 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
2438 num
= sizeof(struct bio_dgram_sctp_prinfo
);
2440 memcpy(&(data
->prinfo
), ptr
, num
);
2442 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
2443 /* Returns always 1. */
2445 data
->save_shutdown
= 1;
2447 data
->save_shutdown
= 0;
2449 case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
:
2450 return dgram_sctp_wait_for_dry(b
);
2451 case BIO_CTRL_DGRAM_SCTP_MSG_WAITING
:
2452 return dgram_sctp_msg_waiting(b
);
2456 * Pass to default ctrl function to process SCTP unspecific commands
2458 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
2464 int BIO_dgram_sctp_notification_cb(BIO
*b
,
2465 BIO_dgram_sctp_notification_handler_fn handle_notifications
,
2468 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2470 if (handle_notifications
!= NULL
) {
2471 data
->handle_notifications
= handle_notifications
;
2472 data
->notification_context
= context
;
2480 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2481 * @b: The BIO to check for the dry event
2483 * Wait until the peer confirms all packets have been received, and so that
2484 * our kernel doesn't have anything to send anymore. This is only received by
2485 * the peer's kernel, not the application.
2489 * 0 when not dry yet
2492 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
2494 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY
, 0, NULL
);
2497 static int dgram_sctp_wait_for_dry(BIO
*b
)
2502 union sctp_notification snp
;
2506 struct sctp_event event
;
2508 struct sctp_event_subscribe event
;
2509 socklen_t eventsize
;
2511 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2513 /* set sender dry event */
2515 memset(&event
, 0, sizeof(event
));
2516 event
.se_assoc_id
= 0;
2517 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2520 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2521 sizeof(struct sctp_event
));
2523 eventsize
= sizeof(struct sctp_event_subscribe
);
2524 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
2528 event
.sctp_sender_dry_event
= 1;
2531 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2532 sizeof(struct sctp_event_subscribe
));
2537 /* peek for notification */
2538 memset(&snp
, 0, sizeof(snp
));
2539 iov
.iov_base
= (char *)&snp
;
2540 iov
.iov_len
= sizeof(union sctp_notification
);
2541 msg
.msg_name
= NULL
;
2542 msg
.msg_namelen
= 0;
2545 msg
.msg_control
= NULL
;
2546 msg
.msg_controllen
= 0;
2549 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2551 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2552 && (get_last_socket_error() != EWOULDBLOCK
))
2558 /* if we find a notification, process it and try again if necessary */
2559 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
2560 memset(&snp
, 0, sizeof(snp
));
2561 iov
.iov_base
= (char *)&snp
;
2562 iov
.iov_len
= sizeof(union sctp_notification
);
2563 msg
.msg_name
= NULL
;
2564 msg
.msg_namelen
= 0;
2567 msg
.msg_control
= NULL
;
2568 msg
.msg_controllen
= 0;
2571 n
= recvmsg(b
->num
, &msg
, 0);
2573 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2574 && (get_last_socket_error() != EWOULDBLOCK
))
2580 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
2583 /* disable sender dry event */
2585 memset(&event
, 0, sizeof(event
));
2586 event
.se_assoc_id
= 0;
2587 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
2590 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
2591 sizeof(struct sctp_event
));
2593 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
2595 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2600 event
.sctp_sender_dry_event
= 0;
2603 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
2604 sizeof(struct sctp_event_subscribe
));
2609 # ifdef SCTP_AUTHENTICATION_EVENT
2610 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2611 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2614 if (data
->handle_notifications
!= NULL
)
2615 data
->handle_notifications(b
, data
->notification_context
,
2618 /* found notification, peek again */
2619 memset(&snp
, 0, sizeof(snp
));
2620 iov
.iov_base
= (char *)&snp
;
2621 iov
.iov_len
= sizeof(union sctp_notification
);
2622 msg
.msg_name
= NULL
;
2623 msg
.msg_namelen
= 0;
2626 msg
.msg_control
= NULL
;
2627 msg
.msg_controllen
= 0;
2630 /* if we have seen the dry already, don't wait */
2632 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2633 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2636 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2639 fcntl(b
->num
, F_SETFL
, sockflags
);
2643 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
2644 && (get_last_socket_error() != EWOULDBLOCK
))
2651 /* read anything else */
2655 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
2657 return (int)BIO_ctrl(b
, BIO_CTRL_DGRAM_SCTP_MSG_WAITING
, 0, NULL
);
2660 static int dgram_sctp_msg_waiting(BIO
*b
)
2663 union sctp_notification snp
;
2666 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
2668 /* Check if there are any messages waiting to be read */
2670 memset(&snp
, 0, sizeof(snp
));
2671 iov
.iov_base
= (char *)&snp
;
2672 iov
.iov_len
= sizeof(union sctp_notification
);
2673 msg
.msg_name
= NULL
;
2674 msg
.msg_namelen
= 0;
2677 msg
.msg_control
= NULL
;
2678 msg
.msg_controllen
= 0;
2681 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
2682 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
2683 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
2684 fcntl(b
->num
, F_SETFL
, sockflags
);
2686 /* if notification, process and try again */
2687 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
2688 # ifdef SCTP_AUTHENTICATION_EVENT
2689 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
2690 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
2693 memset(&snp
, 0, sizeof(snp
));
2694 iov
.iov_base
= (char *)&snp
;
2695 iov
.iov_len
= sizeof(union sctp_notification
);
2696 msg
.msg_name
= NULL
;
2697 msg
.msg_namelen
= 0;
2700 msg
.msg_control
= NULL
;
2701 msg
.msg_controllen
= 0;
2703 n
= recvmsg(b
->num
, &msg
, 0);
2705 if (data
->handle_notifications
!= NULL
)
2706 data
->handle_notifications(b
, data
->notification_context
,
2710 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
2712 /* Return 1 if there is a message to be read, return 0 otherwise. */
2719 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
2724 ret
= dgram_sctp_write(bp
, str
, n
);
2729 static int BIO_dgram_should_retry(int i
)
2733 if ((i
== 0) || (i
== -1)) {
2734 err
= get_last_socket_error();
2736 # if defined(OPENSSL_SYS_WINDOWS)
2738 * If the socket return value (i) is -1 and err is unexpectedly 0 at
2739 * this point, the error code was overwritten by another system call
2740 * before this error handling is called.
2744 return BIO_dgram_non_fatal_error(err
);
2749 int BIO_dgram_non_fatal_error(int err
)
2752 # if defined(OPENSSL_SYS_WINDOWS)
2753 # if defined(WSAEWOULDBLOCK)
2754 case WSAEWOULDBLOCK
:
2759 # ifdef WSAEWOULDBLOCK
2760 # if WSAEWOULDBLOCK != EWOULDBLOCK
2773 # if EWOULDBLOCK != EAGAIN
2797 static void get_current_time(struct timeval
*t
)
2799 # if defined(_WIN32)
2801 unsigned __int64 now_ul
;
2805 SystemTimeToFileTime(&st
, &now_ft
);
2806 now_ul
= ((unsigned __int64
)now_ft
.dwHighDateTime
<< 32) | now_ft
.dwLowDateTime
;
2808 now_ul
-= 116444736000000000ULL;
2810 now_ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
2812 t
->tv_sec
= (long)(now_ul
/ 10000000);
2813 t
->tv_usec
= ((int)(now_ul
% 10000000)) / 10;
2815 if (gettimeofday(t
, NULL
) < 0)
2816 perror("gettimeofday");