2 * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
14 #ifndef OPENSSL_NO_DGRAM
16 # ifndef OPENSSL_NO_SCTP
17 # include <netinet/sctp.h>
19 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
20 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
23 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
24 # define IP_MTU 14 /* linux is lame */
27 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
28 # define IPPROTO_IPV6 41 /* windows is lame */
31 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
32 /* Standard definition causes type-punning problems. */
33 # undef IN6_IS_ADDR_V4MAPPED
34 # define s6_addr32 __u6_addr.__u6_addr32
35 # define IN6_IS_ADDR_V4MAPPED(a) \
36 (((a)->s6_addr32[0] == 0) && \
37 ((a)->s6_addr32[1] == 0) && \
38 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
41 static int dgram_write(BIO
*h
, const char *buf
, int num
);
42 static int dgram_read(BIO
*h
, char *buf
, int size
);
43 static int dgram_puts(BIO
*h
, const char *str
);
44 static long dgram_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
45 static int dgram_new(BIO
*h
);
46 static int dgram_free(BIO
*data
);
47 static int dgram_clear(BIO
*bio
);
49 # ifndef OPENSSL_NO_SCTP
50 static int dgram_sctp_write(BIO
*h
, const char *buf
, int num
);
51 static int dgram_sctp_read(BIO
*h
, char *buf
, int size
);
52 static int dgram_sctp_puts(BIO
*h
, const char *str
);
53 static long dgram_sctp_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
54 static int dgram_sctp_new(BIO
*h
);
55 static int dgram_sctp_free(BIO
*data
);
56 # ifdef SCTP_AUTHENTICATION_EVENT
57 static void dgram_sctp_handle_auth_free_key_event(BIO
*b
, union sctp_notification
62 static int BIO_dgram_should_retry(int s
);
64 static void get_current_time(struct timeval
*t
);
66 static const BIO_METHOD methods_dgramp
= {
69 /* TODO: Convert to new style write function */
72 /* TODO: Convert to new style read function */
76 NULL
, /* dgram_gets, */
83 # ifndef OPENSSL_NO_SCTP
84 static const BIO_METHOD methods_dgramp_sctp
= {
86 "datagram sctp socket",
87 /* TODO: Convert to new style write function */
90 /* TODO: Convert to new style write function */
94 NULL
, /* dgram_gets, */
102 typedef struct bio_dgram_data_st
{
104 unsigned int connected
;
107 struct timeval next_timeout
;
108 struct timeval socket_timeout
;
109 unsigned int peekmode
;
112 # ifndef OPENSSL_NO_SCTP
113 typedef struct bio_dgram_sctp_save_message_st
{
117 } bio_dgram_sctp_save_message
;
119 typedef struct bio_dgram_sctp_data_st
{
121 unsigned int connected
;
124 struct bio_dgram_sctp_sndinfo sndinfo
;
125 struct bio_dgram_sctp_rcvinfo rcvinfo
;
126 struct bio_dgram_sctp_prinfo prinfo
;
127 void (*handle_notifications
) (BIO
*bio
, void *context
, void *buf
);
128 void *notification_context
;
133 int peer_auth_tested
;
134 } bio_dgram_sctp_data
;
137 const BIO_METHOD
*BIO_s_datagram(void)
139 return &methods_dgramp
;
142 BIO
*BIO_new_dgram(int fd
, int close_flag
)
146 ret
= BIO_new(BIO_s_datagram());
149 BIO_set_fd(ret
, fd
, close_flag
);
153 static int dgram_new(BIO
*bi
)
155 bio_dgram_data
*data
= OPENSSL_zalloc(sizeof(*data
));
163 static int dgram_free(BIO
*a
)
165 bio_dgram_data
*data
;
172 data
= (bio_dgram_data
*)a
->ptr
;
178 static int dgram_clear(BIO
*a
)
184 BIO_closesocket(a
->num
);
192 static void dgram_adjust_rcv_timeout(BIO
*b
)
194 # if defined(SO_RCVTIMEO)
195 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
203 /* Is a timer active? */
204 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
205 struct timeval timenow
, timeleft
;
207 /* Read current socket timeout */
208 # ifdef OPENSSL_SYS_WINDOWS
211 sz
.i
= sizeof(timeout
);
212 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
213 (void *)&timeout
, &sz
.i
) < 0) {
214 perror("getsockopt");
216 data
->socket_timeout
.tv_sec
= timeout
/ 1000;
217 data
->socket_timeout
.tv_usec
= (timeout
% 1000) * 1000;
220 sz
.i
= sizeof(data
->socket_timeout
);
221 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
222 &(data
->socket_timeout
), (void *)&sz
) < 0) {
223 perror("getsockopt");
224 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0)
225 OPENSSL_assert(sz
.s
<= sizeof(data
->socket_timeout
));
228 /* Get current time */
229 get_current_time(&timenow
);
231 /* Calculate time left until timer expires */
232 memcpy(&timeleft
, &(data
->next_timeout
), sizeof(struct timeval
));
233 if (timeleft
.tv_usec
< timenow
.tv_usec
) {
234 timeleft
.tv_usec
= 1000000 - timenow
.tv_usec
+ timeleft
.tv_usec
;
237 timeleft
.tv_usec
-= timenow
.tv_usec
;
239 if (timeleft
.tv_sec
< timenow
.tv_sec
) {
241 timeleft
.tv_usec
= 1;
243 timeleft
.tv_sec
-= timenow
.tv_sec
;
247 * Adjust socket timeout if next handshake message timer will expire
250 if ((data
->socket_timeout
.tv_sec
== 0
251 && data
->socket_timeout
.tv_usec
== 0)
252 || (data
->socket_timeout
.tv_sec
> timeleft
.tv_sec
)
253 || (data
->socket_timeout
.tv_sec
== timeleft
.tv_sec
254 && data
->socket_timeout
.tv_usec
>= timeleft
.tv_usec
)) {
255 # ifdef OPENSSL_SYS_WINDOWS
256 timeout
= timeleft
.tv_sec
* 1000 + timeleft
.tv_usec
/ 1000;
257 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
258 (void *)&timeout
, sizeof(timeout
)) < 0) {
259 perror("setsockopt");
262 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &timeleft
,
263 sizeof(struct timeval
)) < 0) {
264 perror("setsockopt");
272 static void dgram_reset_rcv_timeout(BIO
*b
)
274 # if defined(SO_RCVTIMEO)
275 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
277 /* Is a timer active? */
278 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
279 # ifdef OPENSSL_SYS_WINDOWS
280 int timeout
= data
->socket_timeout
.tv_sec
* 1000 +
281 data
->socket_timeout
.tv_usec
/ 1000;
282 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
283 (void *)&timeout
, sizeof(timeout
)) < 0) {
284 perror("setsockopt");
288 (b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &(data
->socket_timeout
),
289 sizeof(struct timeval
)) < 0) {
290 perror("setsockopt");
297 static int dgram_read(BIO
*b
, char *out
, int outl
)
300 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
304 socklen_t len
= sizeof(peer
);
307 clear_socket_error();
308 memset(&peer
, 0, sizeof(peer
));
309 dgram_adjust_rcv_timeout(b
);
312 ret
= recvfrom(b
->num
, out
, outl
, flags
,
313 BIO_ADDR_sockaddr_noconst(&peer
), &len
);
315 if (!data
->connected
&& ret
>= 0)
316 BIO_ctrl(b
, BIO_CTRL_DGRAM_SET_PEER
, 0, &peer
);
318 BIO_clear_retry_flags(b
);
320 if (BIO_dgram_should_retry(ret
)) {
321 BIO_set_retry_read(b
);
322 data
->_errno
= get_last_socket_error();
326 dgram_reset_rcv_timeout(b
);
331 static int dgram_write(BIO
*b
, const char *in
, int inl
)
334 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
335 clear_socket_error();
338 ret
= writesocket(b
->num
, in
, inl
);
340 int peerlen
= BIO_ADDR_sockaddr_size(&data
->peer
);
342 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
343 ret
= sendto(b
->num
, (char *)in
, inl
, 0,
344 BIO_ADDR_sockaddr(&data
->peer
), peerlen
);
346 ret
= sendto(b
->num
, in
, inl
, 0,
347 BIO_ADDR_sockaddr(&data
->peer
), peerlen
);
351 BIO_clear_retry_flags(b
);
353 if (BIO_dgram_should_retry(ret
)) {
354 BIO_set_retry_write(b
);
355 data
->_errno
= get_last_socket_error();
361 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
365 switch (BIO_ADDR_family(&data
->peer
)) {
368 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
375 # ifdef IN6_IS_ADDR_V4MAPPED
376 struct in6_addr tmp_addr
;
377 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
378 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
380 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
386 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
393 /* We don't know. Go with the historical default */
400 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
404 bio_dgram_data
*data
= NULL
;
407 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
408 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
409 * modern enough to define socklen_t */
414 data
= (bio_dgram_data
*)b
->ptr
;
426 b
->num
= *((int *)ptr
);
427 b
->shutdown
= (int)num
;
439 case BIO_CTRL_GET_CLOSE
:
442 case BIO_CTRL_SET_CLOSE
:
443 b
->shutdown
= (int)num
;
445 case BIO_CTRL_PENDING
:
446 case BIO_CTRL_WPENDING
:
453 case BIO_CTRL_DGRAM_CONNECT
:
454 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
456 /* (Linux)kernel sets DF bit on outgoing IP packets */
457 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
458 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
459 addr_len
= (socklen_t
) sizeof(addr
);
460 memset(&addr
, 0, sizeof(addr
));
461 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
465 switch (addr
.sa
.sa_family
) {
467 sockopt_val
= IP_PMTUDISC_DO
;
468 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
469 &sockopt_val
, sizeof(sockopt_val
))) < 0)
470 perror("setsockopt");
472 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
474 sockopt_val
= IPV6_PMTUDISC_DO
;
475 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
476 &sockopt_val
, sizeof(sockopt_val
))) < 0)
477 perror("setsockopt");
488 case BIO_CTRL_DGRAM_QUERY_MTU
:
489 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
490 addr_len
= (socklen_t
) sizeof(addr
);
491 memset(&addr
, 0, sizeof(addr
));
492 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
496 sockopt_len
= sizeof(sockopt_val
);
497 switch (addr
.sa
.sa_family
) {
500 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
501 &sockopt_len
)) < 0 || sockopt_val
< 0) {
505 * we assume that the transport protocol is UDP and no IP
508 data
->mtu
= sockopt_val
- 8 - 20;
512 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
515 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
516 (void *)&sockopt_val
, &sockopt_len
)) < 0
517 || sockopt_val
< 0) {
521 * we assume that the transport protocol is UDP and no IPV6
524 data
->mtu
= sockopt_val
- 8 - 40;
537 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
538 ret
= -dgram_get_mtu_overhead(data
);
539 switch (BIO_ADDR_family(&data
->peer
)) {
543 # if OPENSSL_USE_IPV6
546 # ifdef IN6_IS_ADDR_V4MAPPED
547 struct in6_addr tmp_addr
;
548 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
549 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
562 case BIO_CTRL_DGRAM_GET_MTU
:
564 case BIO_CTRL_DGRAM_SET_MTU
:
568 case BIO_CTRL_DGRAM_SET_CONNECTED
:
571 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
574 memset(&data
->peer
, 0, sizeof(data
->peer
));
577 case BIO_CTRL_DGRAM_GET_PEER
:
578 ret
= BIO_ADDR_sockaddr_size(&data
->peer
);
579 /* FIXME: if num < ret, we will only return part of an address.
580 That should bee an error, no? */
581 if (num
== 0 || num
> ret
)
583 memcpy(ptr
, &data
->peer
, (ret
= num
));
585 case BIO_CTRL_DGRAM_SET_PEER
:
586 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
588 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
589 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
591 # if defined(SO_RCVTIMEO)
592 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
593 # ifdef OPENSSL_SYS_WINDOWS
595 struct timeval
*tv
= (struct timeval
*)ptr
;
596 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
597 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
598 (void *)&timeout
, sizeof(timeout
)) < 0) {
599 perror("setsockopt");
604 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
605 sizeof(struct timeval
)) < 0) {
606 perror("setsockopt");
611 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
619 # ifdef OPENSSL_SYS_WINDOWS
621 struct timeval
*tv
= (struct timeval
*)ptr
;
623 sz
.i
= sizeof(timeout
);
624 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
625 (void *)&timeout
, &sz
.i
) < 0) {
626 perror("getsockopt");
629 tv
->tv_sec
= timeout
/ 1000;
630 tv
->tv_usec
= (timeout
% 1000) * 1000;
634 sz
.i
= sizeof(struct timeval
);
635 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
636 ptr
, (void *)&sz
) < 0) {
637 perror("getsockopt");
639 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
640 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
648 # if defined(SO_SNDTIMEO)
649 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
650 # ifdef OPENSSL_SYS_WINDOWS
652 struct timeval
*tv
= (struct timeval
*)ptr
;
653 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
654 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
655 (void *)&timeout
, sizeof(timeout
)) < 0) {
656 perror("setsockopt");
661 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
662 sizeof(struct timeval
)) < 0) {
663 perror("setsockopt");
668 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
676 # ifdef OPENSSL_SYS_WINDOWS
678 struct timeval
*tv
= (struct timeval
*)ptr
;
680 sz
.i
= sizeof(timeout
);
681 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
682 (void *)&timeout
, &sz
.i
) < 0) {
683 perror("getsockopt");
686 tv
->tv_sec
= timeout
/ 1000;
687 tv
->tv_usec
= (timeout
% 1000) * 1000;
691 sz
.i
= sizeof(struct timeval
);
692 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
693 ptr
, (void *)&sz
) < 0) {
694 perror("getsockopt");
696 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
697 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
705 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
707 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
708 # ifdef OPENSSL_SYS_WINDOWS
709 d_errno
= (data
->_errno
== WSAETIMEDOUT
);
711 d_errno
= (data
->_errno
== EAGAIN
);
720 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
721 if (data
->_errno
== EMSGSIZE
) {
728 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
729 sockopt_val
= num
? 1 : 0;
731 switch (data
->peer
.sa
.sa_family
) {
733 # if defined(IP_DONTFRAG)
734 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
735 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
736 perror("setsockopt");
739 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
740 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
741 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
742 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
743 perror("setsockopt");
746 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
747 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
748 (const char *)&sockopt_val
,
749 sizeof(sockopt_val
))) < 0) {
750 perror("setsockopt");
757 # if OPENSSL_USE_IPV6
759 # if defined(IPV6_DONTFRAG)
760 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
761 (const void *)&sockopt_val
,
762 sizeof(sockopt_val
))) < 0) {
763 perror("setsockopt");
766 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
767 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
768 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
769 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
770 perror("setsockopt");
783 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
784 ret
= dgram_get_mtu_overhead(data
);
788 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
789 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
790 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
791 * value has been updated to a non-clashing value. However to preserve
792 * binary compatiblity we now respond to both the old value and the new one
794 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
795 case BIO_CTRL_DGRAM_SET_PEEK_MODE
:
796 data
->peekmode
= (unsigned int)num
;
805 static int dgram_puts(BIO
*bp
, const char *str
)
810 ret
= dgram_write(bp
, str
, n
);
814 # ifndef OPENSSL_NO_SCTP
815 const BIO_METHOD
*BIO_s_datagram_sctp(void)
817 return &methods_dgramp_sctp
;
820 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
823 int ret
, optval
= 20000;
824 int auth_data
= 0, auth_forward
= 0;
826 struct sctp_authchunk auth
;
827 struct sctp_authchunks
*authchunks
;
828 socklen_t sockopt_len
;
829 # ifdef SCTP_AUTHENTICATION_EVENT
831 struct sctp_event event
;
833 struct sctp_event_subscribe event
;
837 bio
= BIO_new(BIO_s_datagram_sctp());
840 BIO_set_fd(bio
, fd
, close_flag
);
842 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
843 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
845 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
846 sizeof(struct sctp_authchunk
));
849 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP
, ERR_R_SYS_LIB
);
850 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
853 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
855 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
856 sizeof(struct sctp_authchunk
));
859 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP
, ERR_R_SYS_LIB
);
860 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
865 * Test if activation was successful. When using accept(), SCTP-AUTH has
866 * to be activated for the listening socket already, otherwise the
867 * connected socket won't use it. Similarly with connect(): the socket
868 * prior to connection must be activated for SCTP-AUTH
870 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
871 authchunks
= OPENSSL_zalloc(sockopt_len
);
872 if (authchunks
== NULL
) {
876 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
879 OPENSSL_free(authchunks
);
884 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
885 p
< (unsigned char *)authchunks
+ sockopt_len
;
886 p
+= sizeof(uint8_t)) {
887 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
889 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
893 OPENSSL_free(authchunks
);
895 if (!auth_data
|| !auth_forward
) {
897 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP
, ERR_R_SYS_LIB
);
898 ERR_add_error_data(1,
899 "Ensure SCTP AUTH chunks are enabled on the "
900 "underlying socket");
904 # ifdef SCTP_AUTHENTICATION_EVENT
906 memset(&event
, 0, sizeof(event
));
907 event
.se_assoc_id
= 0;
908 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
911 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
912 sizeof(struct sctp_event
));
918 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
919 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
925 event
.sctp_authentication_event
= 1;
928 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
929 sizeof(struct sctp_event_subscribe
));
938 * Disable partial delivery by setting the min size larger than the max
939 * record size of 2^14 + 2048 + 13
942 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
952 int BIO_dgram_is_sctp(BIO
*bio
)
954 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
957 static int dgram_sctp_new(BIO
*bi
)
959 bio_dgram_sctp_data
*data
= NULL
;
963 data
= OPENSSL_zalloc(sizeof(*data
));
966 # ifdef SCTP_PR_SCTP_NONE
967 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
975 static int dgram_sctp_free(BIO
*a
)
977 bio_dgram_sctp_data
*data
;
984 data
= (bio_dgram_sctp_data
*) a
->ptr
;
991 # ifdef SCTP_AUTHENTICATION_EVENT
992 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
993 union sctp_notification
*snp
)
996 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
998 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
999 struct sctp_authkeyid authkeyid
;
1002 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1003 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1004 &authkeyid
, sizeof(struct sctp_authkeyid
));
1009 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1011 int ret
= 0, n
= 0, i
, optval
;
1013 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1014 union sctp_notification
*snp
;
1017 struct cmsghdr
*cmsg
;
1021 clear_socket_error();
1024 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1027 msg
.msg_name
= NULL
;
1028 msg
.msg_namelen
= 0;
1031 msg
.msg_control
= cmsgbuf
;
1032 msg
.msg_controllen
= 512;
1034 n
= recvmsg(b
->num
, &msg
, 0);
1042 if (msg
.msg_controllen
> 0) {
1043 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1044 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1045 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1047 # ifdef SCTP_RCVINFO
1048 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1049 struct sctp_rcvinfo
*rcvinfo
;
1051 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1052 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1053 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1054 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1055 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1056 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1057 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1058 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1062 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1063 struct sctp_sndrcvinfo
*sndrcvinfo
;
1066 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1067 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1068 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1069 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1070 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1071 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1072 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1073 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1079 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1080 snp
= (union sctp_notification
*)out
;
1081 if (snp
->sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1083 struct sctp_event event
;
1085 struct sctp_event_subscribe event
;
1086 socklen_t eventsize
;
1089 /* disable sender dry event */
1091 memset(&event
, 0, sizeof(event
));
1092 event
.se_assoc_id
= 0;
1093 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1095 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1096 sizeof(struct sctp_event
));
1102 eventsize
= sizeof(struct sctp_event_subscribe
);
1103 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1110 event
.sctp_sender_dry_event
= 0;
1112 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1113 sizeof(struct sctp_event_subscribe
));
1120 # ifdef SCTP_AUTHENTICATION_EVENT
1121 if (snp
->sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1122 dgram_sctp_handle_auth_free_key_event(b
, snp
);
1125 if (data
->handle_notifications
!= NULL
)
1126 data
->handle_notifications(b
, data
->notification_context
,
1129 memset(out
, 0, outl
);
1133 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
1136 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
1137 /* Partial message read, this should never happen! */
1140 * The buffer was too small, this means the peer sent a message
1141 * that was larger than allowed.
1147 * Test if socket buffer can handle max record size (2^14 + 2048
1150 optlen
= (socklen_t
) sizeof(int);
1151 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
1153 OPENSSL_assert(optval
>= 18445);
1156 * Test if SCTP doesn't partially deliver below max record size
1157 * (2^14 + 2048 + 13)
1159 optlen
= (socklen_t
) sizeof(int);
1161 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
1164 OPENSSL_assert(optval
>= 18445);
1167 * Partially delivered notification??? Probably a bug....
1169 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
1172 * Everything seems ok till now, so it's most likely a message
1173 * dropped by PR-SCTP.
1175 memset(out
, 0, outl
);
1176 BIO_set_retry_read(b
);
1180 BIO_clear_retry_flags(b
);
1182 if (BIO_dgram_should_retry(ret
)) {
1183 BIO_set_retry_read(b
);
1184 data
->_errno
= get_last_socket_error();
1188 /* Test if peer uses SCTP-AUTH before continuing */
1189 if (!data
->peer_auth_tested
) {
1190 int ii
, auth_data
= 0, auth_forward
= 0;
1192 struct sctp_authchunks
*authchunks
;
1195 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1196 authchunks
= OPENSSL_malloc(optlen
);
1197 if (authchunks
== NULL
) {
1198 BIOerr(BIO_F_DGRAM_SCTP_READ
, ERR_R_MALLOC_FAILURE
);
1201 memset(authchunks
, 0, optlen
);
1202 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
1203 authchunks
, &optlen
);
1206 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1207 p
< (unsigned char *)authchunks
+ optlen
;
1208 p
+= sizeof(uint8_t)) {
1209 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1211 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1215 OPENSSL_free(authchunks
);
1217 if (!auth_data
|| !auth_forward
) {
1218 BIOerr(BIO_F_DGRAM_SCTP_READ
, BIO_R_CONNECT_ERROR
);
1222 data
->peer_auth_tested
= 1;
1229 * dgram_sctp_write - send message on SCTP socket
1230 * @b: BIO to write to
1232 * @inl: amount of bytes in @in to send
1234 * Returns -1 on error or the sent amount of bytes on success
1236 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
1239 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1240 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
1241 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
1242 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
1243 struct iovec iov
[1];
1245 struct cmsghdr
*cmsg
;
1246 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1247 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
1248 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
1249 struct sctp_sndinfo
*sndinfo
;
1250 struct sctp_prinfo
*prinfo
;
1252 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
1253 struct sctp_sndrcvinfo
*sndrcvinfo
;
1256 clear_socket_error();
1259 * If we're send anything else than application data, disable all user
1260 * parameters and flags.
1263 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
1264 # ifdef SCTP_SACK_IMMEDIATELY
1265 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
1267 sinfo
= &handshake_sinfo
;
1270 /* We can only send a shutdown alert if the socket is dry */
1271 if (data
->save_shutdown
) {
1272 ret
= BIO_dgram_sctp_wait_for_dry(b
);
1276 BIO_clear_retry_flags(b
);
1277 BIO_set_retry_write(b
);
1282 iov
[0].iov_base
= (char *)in
;
1283 iov
[0].iov_len
= inl
;
1284 msg
.msg_name
= NULL
;
1285 msg
.msg_namelen
= 0;
1288 msg
.msg_control
= (caddr_t
) cmsgbuf
;
1289 msg
.msg_controllen
= 0;
1291 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1292 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1293 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1294 cmsg
->cmsg_type
= SCTP_SNDINFO
;
1295 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
1296 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
1297 memset(sndinfo
, 0, sizeof(*sndinfo
));
1298 sndinfo
->snd_sid
= sinfo
->snd_sid
;
1299 sndinfo
->snd_flags
= sinfo
->snd_flags
;
1300 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
1301 sndinfo
->snd_context
= sinfo
->snd_context
;
1302 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
1305 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
1306 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1307 cmsg
->cmsg_type
= SCTP_PRINFO
;
1308 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
1309 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
1310 memset(prinfo
, 0, sizeof(*prinfo
));
1311 prinfo
->pr_policy
= pinfo
->pr_policy
;
1312 prinfo
->pr_value
= pinfo
->pr_value
;
1313 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
1315 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1316 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1317 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1318 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1319 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1320 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
1321 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
1322 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
1324 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
1326 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
1327 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
1328 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
1329 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
1332 ret
= sendmsg(b
->num
, &msg
, 0);
1334 BIO_clear_retry_flags(b
);
1336 if (BIO_dgram_should_retry(ret
)) {
1337 BIO_set_retry_write(b
);
1338 data
->_errno
= get_last_socket_error();
1344 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
1347 bio_dgram_sctp_data
*data
= NULL
;
1348 socklen_t sockopt_len
= 0;
1349 struct sctp_authkeyid authkeyid
;
1350 struct sctp_authkey
*authkey
= NULL
;
1352 data
= (bio_dgram_sctp_data
*) b
->ptr
;
1355 case BIO_CTRL_DGRAM_QUERY_MTU
:
1357 * Set to maximum (2^14) and ignore user input to enable transport
1358 * protocol fragmentation. Returns always 2^14.
1363 case BIO_CTRL_DGRAM_SET_MTU
:
1365 * Set to maximum (2^14) and ignore input to enable transport
1366 * protocol fragmentation. Returns always 2^14.
1371 case BIO_CTRL_DGRAM_SET_CONNECTED
:
1372 case BIO_CTRL_DGRAM_CONNECT
:
1373 /* Returns always -1. */
1376 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
1378 * SCTP doesn't need the DTLS timer Returns always 1.
1381 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
1383 * We allow transport protocol fragmentation so this is irrelevant
1387 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
1389 data
->in_handshake
= 1;
1391 data
->in_handshake
= 0;
1394 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
1395 &data
->in_handshake
, sizeof(int));
1397 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
1399 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1402 /* Get active key */
1403 sockopt_len
= sizeof(struct sctp_authkeyid
);
1405 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1411 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
1412 authkey
= OPENSSL_malloc(sockopt_len
);
1413 if (authkey
== NULL
) {
1417 memset(authkey
, 0, sockopt_len
);
1418 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
1419 # ifndef __FreeBSD__
1421 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1422 * and higher work without it.
1424 authkey
->sca_keylength
= 64;
1426 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
1429 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
1431 OPENSSL_free(authkey
);
1436 /* Reset active key */
1437 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1438 &authkeyid
, sizeof(struct sctp_authkeyid
));
1443 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
1444 /* Returns 0 on success, -1 otherwise. */
1446 /* Get active key */
1447 sockopt_len
= sizeof(struct sctp_authkeyid
);
1449 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1454 /* Set active key */
1455 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
1456 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1457 &authkeyid
, sizeof(struct sctp_authkeyid
));
1462 * CCS has been sent, so remember that and fall through to check if
1463 * we need to deactivate an old key
1467 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
1468 /* Returns 0 on success, -1 otherwise. */
1471 * Has this command really been called or is this just a
1474 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
1478 * CSS has been both, received and sent, so deactivate an old key
1480 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
1481 /* Get active key */
1482 sockopt_len
= sizeof(struct sctp_authkeyid
);
1484 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1485 &authkeyid
, &sockopt_len
);
1490 * Deactivate key or delete second last key if
1491 * SCTP_AUTHENTICATION_EVENT is not available.
1493 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1494 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1495 sockopt_len
= sizeof(struct sctp_authkeyid
);
1496 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
1497 &authkeyid
, sockopt_len
);
1501 # ifndef SCTP_AUTHENTICATION_EVENT
1502 if (authkeyid
.scact_keynumber
> 0) {
1503 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1504 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1505 &authkeyid
, sizeof(struct sctp_authkeyid
));
1515 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
1516 /* Returns the size of the copied struct. */
1517 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1518 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1520 memcpy(ptr
, &(data
->sndinfo
), num
);
1523 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
1524 /* Returns the size of the copied struct. */
1525 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1526 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1528 memcpy(&(data
->sndinfo
), ptr
, num
);
1530 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
1531 /* Returns the size of the copied struct. */
1532 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1533 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1535 memcpy(ptr
, &data
->rcvinfo
, num
);
1539 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
1540 /* Returns the size of the copied struct. */
1541 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1542 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1544 memcpy(&(data
->rcvinfo
), ptr
, num
);
1546 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
1547 /* Returns the size of the copied struct. */
1548 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1549 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1551 memcpy(ptr
, &(data
->prinfo
), num
);
1554 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
1555 /* Returns the size of the copied struct. */
1556 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1557 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1559 memcpy(&(data
->prinfo
), ptr
, num
);
1561 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
1562 /* Returns always 1. */
1564 data
->save_shutdown
= 1;
1566 data
->save_shutdown
= 0;
1571 * Pass to default ctrl function to process SCTP unspecific commands
1573 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
1579 int BIO_dgram_sctp_notification_cb(BIO
*b
,
1580 void (*handle_notifications
) (BIO
*bio
,
1586 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1588 if (handle_notifications
!= NULL
) {
1589 data
->handle_notifications
= handle_notifications
;
1590 data
->notification_context
= context
;
1598 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1599 * @b: The BIO to check for the dry event
1601 * Wait until the peer confirms all packets have been received, and so that
1602 * our kernel doesn't have anything to send anymore. This is only received by
1603 * the peer's kernel, not the application.
1607 * 0 when not dry yet
1610 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
1615 union sctp_notification snp
;
1619 struct sctp_event event
;
1621 struct sctp_event_subscribe event
;
1622 socklen_t eventsize
;
1624 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1626 /* set sender dry event */
1628 memset(&event
, 0, sizeof(event
));
1629 event
.se_assoc_id
= 0;
1630 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1633 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1634 sizeof(struct sctp_event
));
1636 eventsize
= sizeof(struct sctp_event_subscribe
);
1637 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
1641 event
.sctp_sender_dry_event
= 1;
1644 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1645 sizeof(struct sctp_event_subscribe
));
1650 /* peek for notification */
1651 memset(&snp
, 0, sizeof(snp
));
1652 iov
.iov_base
= (char *)&snp
;
1653 iov
.iov_len
= sizeof(union sctp_notification
);
1654 msg
.msg_name
= NULL
;
1655 msg
.msg_namelen
= 0;
1658 msg
.msg_control
= NULL
;
1659 msg
.msg_controllen
= 0;
1662 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1664 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1665 && (get_last_socket_error() != EWOULDBLOCK
))
1671 /* if we find a notification, process it and try again if necessary */
1672 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
1673 memset(&snp
, 0, sizeof(snp
));
1674 iov
.iov_base
= (char *)&snp
;
1675 iov
.iov_len
= sizeof(union sctp_notification
);
1676 msg
.msg_name
= NULL
;
1677 msg
.msg_namelen
= 0;
1680 msg
.msg_control
= NULL
;
1681 msg
.msg_controllen
= 0;
1684 n
= recvmsg(b
->num
, &msg
, 0);
1686 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1687 && (get_last_socket_error() != EWOULDBLOCK
))
1693 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1696 /* disable sender dry event */
1698 memset(&event
, 0, sizeof(event
));
1699 event
.se_assoc_id
= 0;
1700 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1703 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1704 sizeof(struct sctp_event
));
1706 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1708 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1713 event
.sctp_sender_dry_event
= 0;
1716 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1717 sizeof(struct sctp_event_subscribe
));
1722 # ifdef SCTP_AUTHENTICATION_EVENT
1723 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1724 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1727 if (data
->handle_notifications
!= NULL
)
1728 data
->handle_notifications(b
, data
->notification_context
,
1731 /* found notification, peek again */
1732 memset(&snp
, 0, sizeof(snp
));
1733 iov
.iov_base
= (char *)&snp
;
1734 iov
.iov_len
= sizeof(union sctp_notification
);
1735 msg
.msg_name
= NULL
;
1736 msg
.msg_namelen
= 0;
1739 msg
.msg_control
= NULL
;
1740 msg
.msg_controllen
= 0;
1743 /* if we have seen the dry already, don't wait */
1745 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1746 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1749 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1752 fcntl(b
->num
, F_SETFL
, sockflags
);
1756 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1757 && (get_last_socket_error() != EWOULDBLOCK
))
1764 /* read anything else */
1768 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
1771 union sctp_notification snp
;
1774 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1776 /* Check if there are any messages waiting to be read */
1778 memset(&snp
, 0, sizeof(snp
));
1779 iov
.iov_base
= (char *)&snp
;
1780 iov
.iov_len
= sizeof(union sctp_notification
);
1781 msg
.msg_name
= NULL
;
1782 msg
.msg_namelen
= 0;
1785 msg
.msg_control
= NULL
;
1786 msg
.msg_controllen
= 0;
1789 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1790 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1791 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1792 fcntl(b
->num
, F_SETFL
, sockflags
);
1794 /* if notification, process and try again */
1795 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
1796 # ifdef SCTP_AUTHENTICATION_EVENT
1797 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1798 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1801 memset(&snp
, 0, sizeof(snp
));
1802 iov
.iov_base
= (char *)&snp
;
1803 iov
.iov_len
= sizeof(union sctp_notification
);
1804 msg
.msg_name
= NULL
;
1805 msg
.msg_namelen
= 0;
1808 msg
.msg_control
= NULL
;
1809 msg
.msg_controllen
= 0;
1811 n
= recvmsg(b
->num
, &msg
, 0);
1813 if (data
->handle_notifications
!= NULL
)
1814 data
->handle_notifications(b
, data
->notification_context
,
1818 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
1820 /* Return 1 if there is a message to be read, return 0 otherwise. */
1827 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
1832 ret
= dgram_sctp_write(bp
, str
, n
);
1837 static int BIO_dgram_should_retry(int i
)
1841 if ((i
== 0) || (i
== -1)) {
1842 err
= get_last_socket_error();
1844 # if defined(OPENSSL_SYS_WINDOWS)
1846 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1847 * this point, the error code was overwritten by another system call
1848 * before this error handling is called.
1852 return BIO_dgram_non_fatal_error(err
);
1857 int BIO_dgram_non_fatal_error(int err
)
1860 # if defined(OPENSSL_SYS_WINDOWS)
1861 # if defined(WSAEWOULDBLOCK)
1862 case WSAEWOULDBLOCK
:
1867 # ifdef WSAEWOULDBLOCK
1868 # if WSAEWOULDBLOCK != EWOULDBLOCK
1881 # if EWOULDBLOCK != EAGAIN
1905 static void get_current_time(struct timeval
*t
)
1907 # if defined(_WIN32)
1910 unsigned __int64 ul
;
1915 SystemTimeToFileTime(&st
, &now
.ft
);
1917 now
.ul
-= 116444736000000000ULL;
1919 now
.ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
1921 t
->tv_sec
= (long)(now
.ul
/ 10000000);
1922 t
->tv_usec
= ((int)(now
.ul
% 10000000)) / 10;
1924 gettimeofday(t
, NULL
);