2 * Copyright 2005-2016 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
17 # include <sys/time.h>
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 static int dgram_write(BIO
*h
, const char *buf
, int num
);
46 static int dgram_read(BIO
*h
, char *buf
, int size
);
47 static int dgram_puts(BIO
*h
, const char *str
);
48 static long dgram_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
49 static int dgram_new(BIO
*h
);
50 static int dgram_free(BIO
*data
);
51 static int dgram_clear(BIO
*bio
);
53 # ifndef OPENSSL_NO_SCTP
54 static int dgram_sctp_write(BIO
*h
, const char *buf
, int num
);
55 static int dgram_sctp_read(BIO
*h
, char *buf
, int size
);
56 static int dgram_sctp_puts(BIO
*h
, const char *str
);
57 static long dgram_sctp_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
58 static int dgram_sctp_new(BIO
*h
);
59 static int dgram_sctp_free(BIO
*data
);
60 # ifdef SCTP_AUTHENTICATION_EVENT
61 static void dgram_sctp_handle_auth_free_key_event(BIO
*b
, union sctp_notification
66 static int BIO_dgram_should_retry(int s
);
68 static void get_current_time(struct timeval
*t
);
70 static const BIO_METHOD methods_dgramp
= {
73 /* TODO: Convert to new style write function */
76 /* TODO: Convert to new style read function */
80 NULL
, /* dgram_gets, */
87 # ifndef OPENSSL_NO_SCTP
88 static const BIO_METHOD methods_dgramp_sctp
= {
90 "datagram sctp socket",
91 /* TODO: Convert to new style write function */
94 /* TODO: Convert to new style write function */
98 NULL
, /* dgram_gets, */
106 typedef struct bio_dgram_data_st
{
108 unsigned int connected
;
111 struct timeval next_timeout
;
112 struct timeval socket_timeout
;
113 unsigned int peekmode
;
116 # ifndef OPENSSL_NO_SCTP
117 typedef struct bio_dgram_sctp_save_message_st
{
121 } bio_dgram_sctp_save_message
;
123 typedef struct bio_dgram_sctp_data_st
{
125 unsigned int connected
;
128 struct bio_dgram_sctp_sndinfo sndinfo
;
129 struct bio_dgram_sctp_rcvinfo rcvinfo
;
130 struct bio_dgram_sctp_prinfo prinfo
;
131 void (*handle_notifications
) (BIO
*bio
, void *context
, void *buf
);
132 void *notification_context
;
137 int peer_auth_tested
;
138 bio_dgram_sctp_save_message saved_message
;
139 } bio_dgram_sctp_data
;
142 const BIO_METHOD
*BIO_s_datagram(void)
144 return (&methods_dgramp
);
147 BIO
*BIO_new_dgram(int fd
, int close_flag
)
151 ret
= BIO_new(BIO_s_datagram());
154 BIO_set_fd(ret
, fd
, close_flag
);
158 static int dgram_new(BIO
*bi
)
160 bio_dgram_data
*data
= OPENSSL_zalloc(sizeof(*data
));
168 static int dgram_free(BIO
*a
)
170 bio_dgram_data
*data
;
177 data
= (bio_dgram_data
*)a
->ptr
;
183 static int dgram_clear(BIO
*a
)
189 BIO_closesocket(a
->num
);
197 static void dgram_adjust_rcv_timeout(BIO
*b
)
199 # if defined(SO_RCVTIMEO)
200 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
208 /* Is a timer active? */
209 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
210 struct timeval timenow
, timeleft
;
212 /* Read current socket timeout */
213 # ifdef OPENSSL_SYS_WINDOWS
216 sz
.i
= sizeof(timeout
);
217 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
218 (void *)&timeout
, &sz
.i
) < 0) {
219 perror("getsockopt");
221 data
->socket_timeout
.tv_sec
= timeout
/ 1000;
222 data
->socket_timeout
.tv_usec
= (timeout
% 1000) * 1000;
225 sz
.i
= sizeof(data
->socket_timeout
);
226 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
227 &(data
->socket_timeout
), (void *)&sz
) < 0) {
228 perror("getsockopt");
229 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0)
230 OPENSSL_assert(sz
.s
<= sizeof(data
->socket_timeout
));
233 /* Get current time */
234 get_current_time(&timenow
);
236 /* Calculate time left until timer expires */
237 memcpy(&timeleft
, &(data
->next_timeout
), sizeof(struct timeval
));
238 if (timeleft
.tv_usec
< timenow
.tv_usec
) {
239 timeleft
.tv_usec
= 1000000 - timenow
.tv_usec
+ timeleft
.tv_usec
;
242 timeleft
.tv_usec
-= timenow
.tv_usec
;
244 if (timeleft
.tv_sec
< timenow
.tv_sec
) {
246 timeleft
.tv_usec
= 1;
248 timeleft
.tv_sec
-= timenow
.tv_sec
;
252 * Adjust socket timeout if next handshake message timer will expire
255 if ((data
->socket_timeout
.tv_sec
== 0
256 && data
->socket_timeout
.tv_usec
== 0)
257 || (data
->socket_timeout
.tv_sec
> timeleft
.tv_sec
)
258 || (data
->socket_timeout
.tv_sec
== timeleft
.tv_sec
259 && data
->socket_timeout
.tv_usec
>= timeleft
.tv_usec
)) {
260 # ifdef OPENSSL_SYS_WINDOWS
261 timeout
= timeleft
.tv_sec
* 1000 + timeleft
.tv_usec
/ 1000;
262 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
263 (void *)&timeout
, sizeof(timeout
)) < 0) {
264 perror("setsockopt");
267 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &timeleft
,
268 sizeof(struct timeval
)) < 0) {
269 perror("setsockopt");
277 static void dgram_reset_rcv_timeout(BIO
*b
)
279 # if defined(SO_RCVTIMEO)
280 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
282 /* Is a timer active? */
283 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
284 # ifdef OPENSSL_SYS_WINDOWS
285 int timeout
= data
->socket_timeout
.tv_sec
* 1000 +
286 data
->socket_timeout
.tv_usec
/ 1000;
287 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
288 (void *)&timeout
, sizeof(timeout
)) < 0) {
289 perror("setsockopt");
293 (b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &(data
->socket_timeout
),
294 sizeof(struct timeval
)) < 0) {
295 perror("setsockopt");
302 static int dgram_read(BIO
*b
, char *out
, int outl
)
305 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
309 socklen_t len
= sizeof(peer
);
312 clear_socket_error();
313 memset(&peer
, 0, sizeof(peer
));
314 dgram_adjust_rcv_timeout(b
);
317 ret
= recvfrom(b
->num
, out
, outl
, flags
,
318 BIO_ADDR_sockaddr_noconst(&peer
), &len
);
320 if (!data
->connected
&& ret
>= 0)
321 BIO_ctrl(b
, BIO_CTRL_DGRAM_SET_PEER
, 0, &peer
);
323 BIO_clear_retry_flags(b
);
325 if (BIO_dgram_should_retry(ret
)) {
326 BIO_set_retry_read(b
);
327 data
->_errno
= get_last_socket_error();
331 dgram_reset_rcv_timeout(b
);
336 static int dgram_write(BIO
*b
, const char *in
, int inl
)
339 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
340 clear_socket_error();
343 ret
= writesocket(b
->num
, in
, inl
);
345 int peerlen
= BIO_ADDR_sockaddr_size(&data
->peer
);
347 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
348 ret
= sendto(b
->num
, (char *)in
, inl
, 0,
349 BIO_ADDR_sockaddr(&data
->peer
), peerlen
);
351 ret
= sendto(b
->num
, in
, inl
, 0,
352 BIO_ADDR_sockaddr(&data
->peer
), peerlen
);
356 BIO_clear_retry_flags(b
);
358 if (BIO_dgram_should_retry(ret
)) {
359 BIO_set_retry_write(b
);
360 data
->_errno
= get_last_socket_error();
366 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
370 switch (BIO_ADDR_family(&data
->peer
)) {
373 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
380 # ifdef IN6_IS_ADDR_V4MAPPED
381 struct in6_addr tmp_addr
;
382 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
383 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
385 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
391 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
398 /* We don't know. Go with the historical default */
405 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
409 bio_dgram_data
*data
= NULL
;
412 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
413 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
414 * modern enough to define socklen_t */
419 data
= (bio_dgram_data
*)b
->ptr
;
431 b
->num
= *((int *)ptr
);
432 b
->shutdown
= (int)num
;
444 case BIO_CTRL_GET_CLOSE
:
447 case BIO_CTRL_SET_CLOSE
:
448 b
->shutdown
= (int)num
;
450 case BIO_CTRL_PENDING
:
451 case BIO_CTRL_WPENDING
:
458 case BIO_CTRL_DGRAM_CONNECT
:
459 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
461 /* (Linux)kernel sets DF bit on outgoing IP packets */
462 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
463 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
464 addr_len
= (socklen_t
) sizeof(addr
);
465 memset(&addr
, 0, sizeof(addr
));
466 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
470 switch (addr
.sa
.sa_family
) {
472 sockopt_val
= IP_PMTUDISC_DO
;
473 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
474 &sockopt_val
, sizeof(sockopt_val
))) < 0)
475 perror("setsockopt");
477 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
479 sockopt_val
= IPV6_PMTUDISC_DO
;
480 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
481 &sockopt_val
, sizeof(sockopt_val
))) < 0)
482 perror("setsockopt");
493 case BIO_CTRL_DGRAM_QUERY_MTU
:
494 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
495 addr_len
= (socklen_t
) sizeof(addr
);
496 memset(&addr
, 0, sizeof(addr
));
497 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
501 sockopt_len
= sizeof(sockopt_val
);
502 switch (addr
.sa
.sa_family
) {
505 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
506 &sockopt_len
)) < 0 || sockopt_val
< 0) {
510 * we assume that the transport protocol is UDP and no IP
513 data
->mtu
= sockopt_val
- 8 - 20;
517 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
520 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
521 (void *)&sockopt_val
, &sockopt_len
)) < 0
522 || sockopt_val
< 0) {
526 * we assume that the transport protocol is UDP and no IPV6
529 data
->mtu
= sockopt_val
- 8 - 40;
542 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
543 ret
= -dgram_get_mtu_overhead(data
);
544 switch (BIO_ADDR_family(&data
->peer
)) {
548 # if OPENSSL_USE_IPV6
551 # ifdef IN6_IS_ADDR_V4MAPPED
552 struct in6_addr tmp_addr
;
553 if (BIO_ADDR_rawaddress(&data
->peer
, &tmp_addr
, NULL
)
554 && IN6_IS_ADDR_V4MAPPED(&tmp_addr
))
567 case BIO_CTRL_DGRAM_GET_MTU
:
569 case BIO_CTRL_DGRAM_SET_MTU
:
573 case BIO_CTRL_DGRAM_SET_CONNECTED
:
576 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
579 memset(&data
->peer
, 0, sizeof(data
->peer
));
582 case BIO_CTRL_DGRAM_GET_PEER
:
583 ret
= BIO_ADDR_sockaddr_size(&data
->peer
);
584 /* FIXME: if num < ret, we will only return part of an address.
585 That should bee an error, no? */
586 if (num
== 0 || num
> ret
)
588 memcpy(ptr
, &data
->peer
, (ret
= num
));
590 case BIO_CTRL_DGRAM_SET_PEER
:
591 BIO_ADDR_make(&data
->peer
, BIO_ADDR_sockaddr((BIO_ADDR
*)ptr
));
593 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
594 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
596 # if defined(SO_RCVTIMEO)
597 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
598 # ifdef OPENSSL_SYS_WINDOWS
600 struct timeval
*tv
= (struct timeval
*)ptr
;
601 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
602 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
603 (void *)&timeout
, sizeof(timeout
)) < 0) {
604 perror("setsockopt");
609 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
610 sizeof(struct timeval
)) < 0) {
611 perror("setsockopt");
616 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
624 # ifdef OPENSSL_SYS_WINDOWS
626 struct timeval
*tv
= (struct timeval
*)ptr
;
628 sz
.i
= sizeof(timeout
);
629 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
630 (void *)&timeout
, &sz
.i
) < 0) {
631 perror("getsockopt");
634 tv
->tv_sec
= timeout
/ 1000;
635 tv
->tv_usec
= (timeout
% 1000) * 1000;
639 sz
.i
= sizeof(struct timeval
);
640 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
641 ptr
, (void *)&sz
) < 0) {
642 perror("getsockopt");
644 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
645 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
653 # if defined(SO_SNDTIMEO)
654 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
655 # ifdef OPENSSL_SYS_WINDOWS
657 struct timeval
*tv
= (struct timeval
*)ptr
;
658 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
659 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
660 (void *)&timeout
, sizeof(timeout
)) < 0) {
661 perror("setsockopt");
666 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
667 sizeof(struct timeval
)) < 0) {
668 perror("setsockopt");
673 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
681 # ifdef OPENSSL_SYS_WINDOWS
683 struct timeval
*tv
= (struct timeval
*)ptr
;
685 sz
.i
= sizeof(timeout
);
686 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
687 (void *)&timeout
, &sz
.i
) < 0) {
688 perror("getsockopt");
691 tv
->tv_sec
= timeout
/ 1000;
692 tv
->tv_usec
= (timeout
% 1000) * 1000;
696 sz
.i
= sizeof(struct timeval
);
697 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
698 ptr
, (void *)&sz
) < 0) {
699 perror("getsockopt");
701 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
702 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
710 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
712 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
713 # ifdef OPENSSL_SYS_WINDOWS
714 d_errno
= (data
->_errno
== WSAETIMEDOUT
);
716 d_errno
= (data
->_errno
== EAGAIN
);
725 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
726 if (data
->_errno
== EMSGSIZE
) {
733 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
734 sockopt_val
= num
? 1 : 0;
736 switch (data
->peer
.sa
.sa_family
) {
738 # if defined(IP_DONTFRAG)
739 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
740 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
741 perror("setsockopt");
744 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
745 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
746 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
747 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
748 perror("setsockopt");
751 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
752 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
753 (const char *)&sockopt_val
,
754 sizeof(sockopt_val
))) < 0) {
755 perror("setsockopt");
762 # if OPENSSL_USE_IPV6
764 # if defined(IPV6_DONTFRAG)
765 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
766 (const void *)&sockopt_val
,
767 sizeof(sockopt_val
))) < 0) {
768 perror("setsockopt");
771 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
772 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
773 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
774 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
775 perror("setsockopt");
788 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
789 ret
= dgram_get_mtu_overhead(data
);
791 case BIO_CTRL_DGRAM_SET_PEEK_MODE
:
792 data
->peekmode
= (unsigned int)num
;
801 static int dgram_puts(BIO
*bp
, const char *str
)
806 ret
= dgram_write(bp
, str
, n
);
810 # ifndef OPENSSL_NO_SCTP
811 const BIO_METHOD
*BIO_s_datagram_sctp(void)
813 return (&methods_dgramp_sctp
);
816 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
819 int ret
, optval
= 20000;
820 int auth_data
= 0, auth_forward
= 0;
822 struct sctp_authchunk auth
;
823 struct sctp_authchunks
*authchunks
;
824 socklen_t sockopt_len
;
825 # ifdef SCTP_AUTHENTICATION_EVENT
827 struct sctp_event event
;
829 struct sctp_event_subscribe event
;
833 bio
= BIO_new(BIO_s_datagram_sctp());
836 BIO_set_fd(bio
, fd
, close_flag
);
838 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
839 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
841 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
842 sizeof(struct sctp_authchunk
));
847 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
849 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
850 sizeof(struct sctp_authchunk
));
857 * Test if activation was successful. When using accept(), SCTP-AUTH has
858 * to be activated for the listening socket already, otherwise the
859 * connected socket won't use it.
861 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
862 authchunks
= OPENSSL_zalloc(sockopt_len
);
863 if (authchunks
== NULL
) {
867 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
870 OPENSSL_free(authchunks
);
875 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
876 p
< (unsigned char *)authchunks
+ sockopt_len
;
877 p
+= sizeof(uint8_t)) {
878 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
880 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
884 OPENSSL_free(authchunks
);
886 OPENSSL_assert(auth_data
);
887 OPENSSL_assert(auth_forward
);
889 # ifdef SCTP_AUTHENTICATION_EVENT
891 memset(&event
, 0, sizeof(event
));
892 event
.se_assoc_id
= 0;
893 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
896 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
897 sizeof(struct sctp_event
));
903 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
904 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
910 event
.sctp_authentication_event
= 1;
913 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
914 sizeof(struct sctp_event_subscribe
));
923 * Disable partial delivery by setting the min size larger than the max
924 * record size of 2^14 + 2048 + 13
927 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
937 int BIO_dgram_is_sctp(BIO
*bio
)
939 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
942 static int dgram_sctp_new(BIO
*bi
)
944 bio_dgram_sctp_data
*data
= NULL
;
948 data
= OPENSSL_zalloc(sizeof(*data
));
951 # ifdef SCTP_PR_SCTP_NONE
952 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
960 static int dgram_sctp_free(BIO
*a
)
962 bio_dgram_sctp_data
*data
;
969 data
= (bio_dgram_sctp_data
*) a
->ptr
;
971 OPENSSL_free(data
->saved_message
.data
);
978 # ifdef SCTP_AUTHENTICATION_EVENT
979 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
980 union sctp_notification
*snp
)
983 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
985 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
986 struct sctp_authkeyid authkeyid
;
989 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
990 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
991 &authkeyid
, sizeof(struct sctp_authkeyid
));
996 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
998 int ret
= 0, n
= 0, i
, optval
;
1000 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1001 union sctp_notification
*snp
;
1004 struct cmsghdr
*cmsg
;
1008 clear_socket_error();
1011 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1014 msg
.msg_name
= NULL
;
1015 msg
.msg_namelen
= 0;
1018 msg
.msg_control
= cmsgbuf
;
1019 msg
.msg_controllen
= 512;
1021 n
= recvmsg(b
->num
, &msg
, 0);
1029 if (msg
.msg_controllen
> 0) {
1030 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1031 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1032 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1034 # ifdef SCTP_RCVINFO
1035 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1036 struct sctp_rcvinfo
*rcvinfo
;
1038 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1039 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1040 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1041 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1042 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1043 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1044 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1045 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1049 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1050 struct sctp_sndrcvinfo
*sndrcvinfo
;
1053 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1054 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1055 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1056 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1057 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1058 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1059 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1060 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1066 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1067 snp
= (union sctp_notification
*)out
;
1068 if (snp
->sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1070 struct sctp_event event
;
1072 struct sctp_event_subscribe event
;
1073 socklen_t eventsize
;
1076 * If a message has been delayed until the socket is dry,
1077 * it can be sent now.
1079 if (data
->saved_message
.length
> 0) {
1080 i
= dgram_sctp_write(data
->saved_message
.bio
,
1081 data
->saved_message
.data
,
1082 data
->saved_message
.length
);
1087 OPENSSL_free(data
->saved_message
.data
);
1088 data
->saved_message
.data
= NULL
;
1089 data
->saved_message
.length
= 0;
1092 /* disable sender dry event */
1094 memset(&event
, 0, sizeof(event
));
1095 event
.se_assoc_id
= 0;
1096 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1098 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1099 sizeof(struct sctp_event
));
1105 eventsize
= sizeof(struct sctp_event_subscribe
);
1106 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1113 event
.sctp_sender_dry_event
= 0;
1115 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1116 sizeof(struct sctp_event_subscribe
));
1123 # ifdef SCTP_AUTHENTICATION_EVENT
1124 if (snp
->sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1125 dgram_sctp_handle_auth_free_key_event(b
, snp
);
1128 if (data
->handle_notifications
!= NULL
)
1129 data
->handle_notifications(b
, data
->notification_context
,
1132 memset(out
, 0, outl
);
1136 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
1139 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
1140 /* Partial message read, this should never happen! */
1143 * The buffer was too small, this means the peer sent a message
1144 * that was larger than allowed.
1150 * Test if socket buffer can handle max record size (2^14 + 2048
1153 optlen
= (socklen_t
) sizeof(int);
1154 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
1156 OPENSSL_assert(optval
>= 18445);
1159 * Test if SCTP doesn't partially deliver below max record size
1160 * (2^14 + 2048 + 13)
1162 optlen
= (socklen_t
) sizeof(int);
1164 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
1167 OPENSSL_assert(optval
>= 18445);
1170 * Partially delivered notification??? Probably a bug....
1172 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
1175 * Everything seems ok till now, so it's most likely a message
1176 * dropped by PR-SCTP.
1178 memset(out
, 0, outl
);
1179 BIO_set_retry_read(b
);
1183 BIO_clear_retry_flags(b
);
1185 if (BIO_dgram_should_retry(ret
)) {
1186 BIO_set_retry_read(b
);
1187 data
->_errno
= get_last_socket_error();
1191 /* Test if peer uses SCTP-AUTH before continuing */
1192 if (!data
->peer_auth_tested
) {
1193 int ii
, auth_data
= 0, auth_forward
= 0;
1195 struct sctp_authchunks
*authchunks
;
1198 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1199 authchunks
= OPENSSL_malloc(optlen
);
1200 if (authchunks
== NULL
) {
1201 BIOerr(BIO_F_DGRAM_SCTP_READ
, ERR_R_MALLOC_FAILURE
);
1204 memset(authchunks
, 0, optlen
);
1205 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
1206 authchunks
, &optlen
);
1209 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1210 p
< (unsigned char *)authchunks
+ optlen
;
1211 p
+= sizeof(uint8_t)) {
1212 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1214 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1218 OPENSSL_free(authchunks
);
1220 if (!auth_data
|| !auth_forward
) {
1221 BIOerr(BIO_F_DGRAM_SCTP_READ
, BIO_R_CONNECT_ERROR
);
1225 data
->peer_auth_tested
= 1;
1232 * dgram_sctp_write - send message on SCTP socket
1233 * @b: BIO to write to
1235 * @inl: amount of bytes in @in to send
1237 * Returns -1 on error or the sent amount of bytes on success
1239 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
1242 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1243 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
1244 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
1245 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
1246 struct iovec iov
[1];
1248 struct cmsghdr
*cmsg
;
1249 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1250 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
1251 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
1252 struct sctp_sndinfo
*sndinfo
;
1253 struct sctp_prinfo
*prinfo
;
1255 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
1256 struct sctp_sndrcvinfo
*sndrcvinfo
;
1259 clear_socket_error();
1262 * If we're send anything else than application data, disable all user
1263 * parameters and flags.
1266 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
1267 # ifdef SCTP_SACK_IMMEDIATELY
1268 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
1270 sinfo
= &handshake_sinfo
;
1274 * If we have to send a shutdown alert message and the socket is not dry
1275 * yet, we have to save it and send it as soon as the socket gets dry.
1277 if (data
->save_shutdown
) {
1278 ret
= BIO_dgram_sctp_wait_for_dry(b
);
1284 data
->saved_message
.bio
= b
;
1285 if ((tmp
= OPENSSL_malloc(inl
)) == NULL
) {
1286 BIOerr(BIO_F_DGRAM_SCTP_WRITE
, ERR_R_MALLOC_FAILURE
);
1289 OPENSSL_free(data
->saved_message
.data
);
1290 data
->saved_message
.data
= tmp
;
1291 memcpy(data
->saved_message
.data
, in
, inl
);
1292 data
->saved_message
.length
= inl
;
1297 iov
[0].iov_base
= (char *)in
;
1298 iov
[0].iov_len
= inl
;
1299 msg
.msg_name
= NULL
;
1300 msg
.msg_namelen
= 0;
1303 msg
.msg_control
= (caddr_t
) cmsgbuf
;
1304 msg
.msg_controllen
= 0;
1306 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1307 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1308 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1309 cmsg
->cmsg_type
= SCTP_SNDINFO
;
1310 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
1311 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
1312 memset(sndinfo
, 0, sizeof(*sndinfo
));
1313 sndinfo
->snd_sid
= sinfo
->snd_sid
;
1314 sndinfo
->snd_flags
= sinfo
->snd_flags
;
1315 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
1316 sndinfo
->snd_context
= sinfo
->snd_context
;
1317 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
1320 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
1321 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1322 cmsg
->cmsg_type
= SCTP_PRINFO
;
1323 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
1324 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
1325 memset(prinfo
, 0, sizeof(*prinfo
));
1326 prinfo
->pr_policy
= pinfo
->pr_policy
;
1327 prinfo
->pr_value
= pinfo
->pr_value
;
1328 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
1330 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1331 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1332 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1333 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1334 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1335 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
1336 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
1337 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
1339 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
1341 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
1342 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
1343 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
1344 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
1347 ret
= sendmsg(b
->num
, &msg
, 0);
1349 BIO_clear_retry_flags(b
);
1351 if (BIO_dgram_should_retry(ret
)) {
1352 BIO_set_retry_write(b
);
1353 data
->_errno
= get_last_socket_error();
1359 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
1362 bio_dgram_sctp_data
*data
= NULL
;
1363 socklen_t sockopt_len
= 0;
1364 struct sctp_authkeyid authkeyid
;
1365 struct sctp_authkey
*authkey
= NULL
;
1367 data
= (bio_dgram_sctp_data
*) b
->ptr
;
1370 case BIO_CTRL_DGRAM_QUERY_MTU
:
1372 * Set to maximum (2^14) and ignore user input to enable transport
1373 * protocol fragmentation. Returns always 2^14.
1378 case BIO_CTRL_DGRAM_SET_MTU
:
1380 * Set to maximum (2^14) and ignore input to enable transport
1381 * protocol fragmentation. Returns always 2^14.
1386 case BIO_CTRL_DGRAM_SET_CONNECTED
:
1387 case BIO_CTRL_DGRAM_CONNECT
:
1388 /* Returns always -1. */
1391 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
1393 * SCTP doesn't need the DTLS timer Returns always 1.
1396 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
1398 * We allow transport protocol fragmentation so this is irrelevant
1402 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
1404 data
->in_handshake
= 1;
1406 data
->in_handshake
= 0;
1409 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
1410 &data
->in_handshake
, sizeof(int));
1412 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
1414 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1417 /* Get active key */
1418 sockopt_len
= sizeof(struct sctp_authkeyid
);
1420 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1426 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
1427 authkey
= OPENSSL_malloc(sockopt_len
);
1428 if (authkey
== NULL
) {
1432 memset(authkey
, 0, sockopt_len
);
1433 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
1434 # ifndef __FreeBSD__
1436 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1437 * and higher work without it.
1439 authkey
->sca_keylength
= 64;
1441 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
1444 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
1446 OPENSSL_free(authkey
);
1451 /* Reset active key */
1452 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1453 &authkeyid
, sizeof(struct sctp_authkeyid
));
1458 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
1459 /* Returns 0 on success, -1 otherwise. */
1461 /* Get active key */
1462 sockopt_len
= sizeof(struct sctp_authkeyid
);
1464 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1469 /* Set active key */
1470 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
1471 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1472 &authkeyid
, sizeof(struct sctp_authkeyid
));
1477 * CCS has been sent, so remember that and fall through to check if
1478 * we need to deactivate an old key
1482 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
1483 /* Returns 0 on success, -1 otherwise. */
1486 * Has this command really been called or is this just a
1489 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
1493 * CSS has been both, received and sent, so deactivate an old key
1495 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
1496 /* Get active key */
1497 sockopt_len
= sizeof(struct sctp_authkeyid
);
1499 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1500 &authkeyid
, &sockopt_len
);
1505 * Deactivate key or delete second last key if
1506 * SCTP_AUTHENTICATION_EVENT is not available.
1508 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1509 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1510 sockopt_len
= sizeof(struct sctp_authkeyid
);
1511 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
1512 &authkeyid
, sockopt_len
);
1516 # ifndef SCTP_AUTHENTICATION_EVENT
1517 if (authkeyid
.scact_keynumber
> 0) {
1518 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1519 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1520 &authkeyid
, sizeof(struct sctp_authkeyid
));
1530 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
1531 /* Returns the size of the copied struct. */
1532 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1533 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1535 memcpy(ptr
, &(data
->sndinfo
), num
);
1538 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
1539 /* Returns the size of the copied struct. */
1540 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1541 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1543 memcpy(&(data
->sndinfo
), ptr
, num
);
1545 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
1546 /* Returns the size of the copied struct. */
1547 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1548 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1550 memcpy(ptr
, &data
->rcvinfo
, num
);
1554 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
1555 /* Returns the size of the copied struct. */
1556 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1557 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1559 memcpy(&(data
->rcvinfo
), ptr
, num
);
1561 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
1562 /* Returns the size of the copied struct. */
1563 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1564 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1566 memcpy(ptr
, &(data
->prinfo
), num
);
1569 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
1570 /* Returns the size of the copied struct. */
1571 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1572 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1574 memcpy(&(data
->prinfo
), ptr
, num
);
1576 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
1577 /* Returns always 1. */
1579 data
->save_shutdown
= 1;
1581 data
->save_shutdown
= 0;
1586 * Pass to default ctrl function to process SCTP unspecific commands
1588 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
1594 int BIO_dgram_sctp_notification_cb(BIO
*b
,
1595 void (*handle_notifications
) (BIO
*bio
,
1601 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1603 if (handle_notifications
!= NULL
) {
1604 data
->handle_notifications
= handle_notifications
;
1605 data
->notification_context
= context
;
1613 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1614 * @b: The BIO to check for the dry event
1616 * Wait until the peer confirms all packets have been received, and so that
1617 * our kernel doesn't have anything to send anymore. This is only received by
1618 * the peer's kernel, not the application.
1622 * 0 when not dry yet
1625 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
1630 union sctp_notification snp
;
1634 struct sctp_event event
;
1636 struct sctp_event_subscribe event
;
1637 socklen_t eventsize
;
1639 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1641 /* set sender dry event */
1643 memset(&event
, 0, sizeof(event
));
1644 event
.se_assoc_id
= 0;
1645 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1648 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1649 sizeof(struct sctp_event
));
1651 eventsize
= sizeof(struct sctp_event_subscribe
);
1652 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
1656 event
.sctp_sender_dry_event
= 1;
1659 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1660 sizeof(struct sctp_event_subscribe
));
1665 /* peek for notification */
1666 memset(&snp
, 0, sizeof(snp
));
1667 iov
.iov_base
= (char *)&snp
;
1668 iov
.iov_len
= sizeof(union sctp_notification
);
1669 msg
.msg_name
= NULL
;
1670 msg
.msg_namelen
= 0;
1673 msg
.msg_control
= NULL
;
1674 msg
.msg_controllen
= 0;
1677 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1679 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1680 && (get_last_socket_error() != EWOULDBLOCK
))
1686 /* if we find a notification, process it and try again if necessary */
1687 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
1688 memset(&snp
, 0, sizeof(snp
));
1689 iov
.iov_base
= (char *)&snp
;
1690 iov
.iov_len
= sizeof(union sctp_notification
);
1691 msg
.msg_name
= NULL
;
1692 msg
.msg_namelen
= 0;
1695 msg
.msg_control
= NULL
;
1696 msg
.msg_controllen
= 0;
1699 n
= recvmsg(b
->num
, &msg
, 0);
1701 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1702 && (get_last_socket_error() != EWOULDBLOCK
))
1708 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1711 /* disable sender dry event */
1713 memset(&event
, 0, sizeof(event
));
1714 event
.se_assoc_id
= 0;
1715 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1718 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1719 sizeof(struct sctp_event
));
1721 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1723 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1728 event
.sctp_sender_dry_event
= 0;
1731 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1732 sizeof(struct sctp_event_subscribe
));
1737 # ifdef SCTP_AUTHENTICATION_EVENT
1738 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1739 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1742 if (data
->handle_notifications
!= NULL
)
1743 data
->handle_notifications(b
, data
->notification_context
,
1746 /* found notification, peek again */
1747 memset(&snp
, 0, sizeof(snp
));
1748 iov
.iov_base
= (char *)&snp
;
1749 iov
.iov_len
= sizeof(union sctp_notification
);
1750 msg
.msg_name
= NULL
;
1751 msg
.msg_namelen
= 0;
1754 msg
.msg_control
= NULL
;
1755 msg
.msg_controllen
= 0;
1758 /* if we have seen the dry already, don't wait */
1760 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1761 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1764 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1767 fcntl(b
->num
, F_SETFL
, sockflags
);
1771 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1772 && (get_last_socket_error() != EWOULDBLOCK
))
1779 /* read anything else */
1783 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
1786 union sctp_notification snp
;
1789 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1791 /* Check if there are any messages waiting to be read */
1793 memset(&snp
, 0, sizeof(snp
));
1794 iov
.iov_base
= (char *)&snp
;
1795 iov
.iov_len
= sizeof(union sctp_notification
);
1796 msg
.msg_name
= NULL
;
1797 msg
.msg_namelen
= 0;
1800 msg
.msg_control
= NULL
;
1801 msg
.msg_controllen
= 0;
1804 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1805 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1806 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1807 fcntl(b
->num
, F_SETFL
, sockflags
);
1809 /* if notification, process and try again */
1810 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
1811 # ifdef SCTP_AUTHENTICATION_EVENT
1812 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1813 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1816 memset(&snp
, 0, sizeof(snp
));
1817 iov
.iov_base
= (char *)&snp
;
1818 iov
.iov_len
= sizeof(union sctp_notification
);
1819 msg
.msg_name
= NULL
;
1820 msg
.msg_namelen
= 0;
1823 msg
.msg_control
= NULL
;
1824 msg
.msg_controllen
= 0;
1826 n
= recvmsg(b
->num
, &msg
, 0);
1828 if (data
->handle_notifications
!= NULL
)
1829 data
->handle_notifications(b
, data
->notification_context
,
1833 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
1835 /* Return 1 if there is a message to be read, return 0 otherwise. */
1842 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
1847 ret
= dgram_sctp_write(bp
, str
, n
);
1852 static int BIO_dgram_should_retry(int i
)
1856 if ((i
== 0) || (i
== -1)) {
1857 err
= get_last_socket_error();
1859 # if defined(OPENSSL_SYS_WINDOWS)
1861 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1862 * this point, the error code was overwritten by another system call
1863 * before this error handling is called.
1867 return (BIO_dgram_non_fatal_error(err
));
1872 int BIO_dgram_non_fatal_error(int err
)
1875 # if defined(OPENSSL_SYS_WINDOWS)
1876 # if defined(WSAEWOULDBLOCK)
1877 case WSAEWOULDBLOCK
:
1882 # ifdef WSAEWOULDBLOCK
1883 # if WSAEWOULDBLOCK != EWOULDBLOCK
1896 # if EWOULDBLOCK != EAGAIN
1920 static void get_current_time(struct timeval
*t
)
1922 # if defined(_WIN32)
1925 unsigned __int64 ul
;
1930 SystemTimeToFileTime(&st
, &now
.ft
);
1932 now
.ul
-= 116444736000000000ULL;
1934 now
.ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
1936 t
->tv_sec
= (long)(now
.ul
/ 10000000);
1937 t
->tv_usec
= ((int)(now
.ul
% 10000000)) / 10;
1939 gettimeofday(t
, NULL
);