1 /* crypto/bio/bio_dgram.c */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include "internal/cryptlib.h"
65 #include <openssl/bio.h>
66 #ifndef OPENSSL_NO_DGRAM
68 # if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
69 # include <sys/time.h>
71 # if defined(OPENSSL_SYS_VMS)
72 # include <sys/timeb.h>
75 # ifndef OPENSSL_NO_SCTP
76 # include <netinet/sctp.h>
78 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
79 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
82 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
83 # define IP_MTU 14 /* linux is lame */
86 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
87 # define IPPROTO_IPV6 41 /* windows is lame */
90 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
91 /* Standard definition causes type-punning problems. */
92 # undef IN6_IS_ADDR_V4MAPPED
93 # define s6_addr32 __u6_addr.__u6_addr32
94 # define IN6_IS_ADDR_V4MAPPED(a) \
95 (((a)->s6_addr32[0] == 0) && \
96 ((a)->s6_addr32[1] == 0) && \
97 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
101 # define sock_write SockWrite /* Watt-32 uses same names */
102 # define sock_read SockRead
103 # define sock_puts SockPuts
106 static int dgram_write(BIO
*h
, const char *buf
, int num
);
107 static int dgram_read(BIO
*h
, char *buf
, int size
);
108 static int dgram_puts(BIO
*h
, const char *str
);
109 static long dgram_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
110 static int dgram_new(BIO
*h
);
111 static int dgram_free(BIO
*data
);
112 static int dgram_clear(BIO
*bio
);
114 # ifndef OPENSSL_NO_SCTP
115 static int dgram_sctp_write(BIO
*h
, const char *buf
, int num
);
116 static int dgram_sctp_read(BIO
*h
, char *buf
, int size
);
117 static int dgram_sctp_puts(BIO
*h
, const char *str
);
118 static long dgram_sctp_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
119 static int dgram_sctp_new(BIO
*h
);
120 static int dgram_sctp_free(BIO
*data
);
121 # ifdef SCTP_AUTHENTICATION_EVENT
122 static void dgram_sctp_handle_auth_free_key_event(BIO
*b
, union sctp_notification
127 static int BIO_dgram_should_retry(int s
);
129 static void get_current_time(struct timeval
*t
);
131 static BIO_METHOD methods_dgramp
= {
137 NULL
, /* dgram_gets, */
144 # ifndef OPENSSL_NO_SCTP
145 static BIO_METHOD methods_dgramp_sctp
= {
147 "datagram sctp socket",
151 NULL
, /* dgram_gets, */
159 typedef struct bio_dgram_data_st
{
162 struct sockaddr_in sa_in
;
163 # if OPENSSL_USE_IPV6
164 struct sockaddr_in6 sa_in6
;
167 unsigned int connected
;
170 struct timeval next_timeout
;
171 struct timeval socket_timeout
;
174 # ifndef OPENSSL_NO_SCTP
175 typedef struct bio_dgram_sctp_save_message_st
{
179 } bio_dgram_sctp_save_message
;
181 typedef struct bio_dgram_sctp_data_st
{
184 struct sockaddr_in sa_in
;
185 # if OPENSSL_USE_IPV6
186 struct sockaddr_in6 sa_in6
;
189 unsigned int connected
;
192 struct bio_dgram_sctp_sndinfo sndinfo
;
193 struct bio_dgram_sctp_rcvinfo rcvinfo
;
194 struct bio_dgram_sctp_prinfo prinfo
;
195 void (*handle_notifications
) (BIO
*bio
, void *context
, void *buf
);
196 void *notification_context
;
201 int peer_auth_tested
;
202 bio_dgram_sctp_save_message saved_message
;
203 } bio_dgram_sctp_data
;
206 BIO_METHOD
*BIO_s_datagram(void)
208 return (&methods_dgramp
);
211 BIO
*BIO_new_dgram(int fd
, int close_flag
)
215 ret
= BIO_new(BIO_s_datagram());
218 BIO_set_fd(ret
, fd
, close_flag
);
222 static int dgram_new(BIO
*bi
)
224 bio_dgram_data
*data
= NULL
;
228 data
= OPENSSL_malloc(sizeof(*data
));
231 memset(data
, 0, sizeof(*data
));
238 static int dgram_free(BIO
*a
)
240 bio_dgram_data
*data
;
247 data
= (bio_dgram_data
*)a
->ptr
;
253 static int dgram_clear(BIO
*a
)
267 static void dgram_adjust_rcv_timeout(BIO
*b
)
269 # if defined(SO_RCVTIMEO)
270 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
278 /* Is a timer active? */
279 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
280 struct timeval timenow
, timeleft
;
282 /* Read current socket timeout */
283 # ifdef OPENSSL_SYS_WINDOWS
286 sz
.i
= sizeof(timeout
);
287 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
288 (void *)&timeout
, &sz
.i
) < 0) {
289 perror("getsockopt");
291 data
->socket_timeout
.tv_sec
= timeout
/ 1000;
292 data
->socket_timeout
.tv_usec
= (timeout
% 1000) * 1000;
295 sz
.i
= sizeof(data
->socket_timeout
);
296 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
297 &(data
->socket_timeout
), (void *)&sz
) < 0) {
298 perror("getsockopt");
299 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0)
300 OPENSSL_assert(sz
.s
<= sizeof(data
->socket_timeout
));
303 /* Get current time */
304 get_current_time(&timenow
);
306 /* Calculate time left until timer expires */
307 memcpy(&timeleft
, &(data
->next_timeout
), sizeof(struct timeval
));
308 if (timeleft
.tv_usec
< timenow
.tv_usec
) {
309 timeleft
.tv_usec
= 1000000 - timenow
.tv_usec
+ timeleft
.tv_usec
;
312 timeleft
.tv_usec
-= timenow
.tv_usec
;
314 if (timeleft
.tv_sec
< timenow
.tv_sec
) {
316 timeleft
.tv_usec
= 1;
318 timeleft
.tv_sec
-= timenow
.tv_sec
;
322 * Adjust socket timeout if next handhake message timer will expire
325 if ((data
->socket_timeout
.tv_sec
== 0
326 && data
->socket_timeout
.tv_usec
== 0)
327 || (data
->socket_timeout
.tv_sec
> timeleft
.tv_sec
)
328 || (data
->socket_timeout
.tv_sec
== timeleft
.tv_sec
329 && data
->socket_timeout
.tv_usec
>= timeleft
.tv_usec
)) {
330 # ifdef OPENSSL_SYS_WINDOWS
331 timeout
= timeleft
.tv_sec
* 1000 + timeleft
.tv_usec
/ 1000;
332 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
333 (void *)&timeout
, sizeof(timeout
)) < 0) {
334 perror("setsockopt");
337 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &timeleft
,
338 sizeof(struct timeval
)) < 0) {
339 perror("setsockopt");
347 static void dgram_reset_rcv_timeout(BIO
*b
)
349 # if defined(SO_RCVTIMEO)
350 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
352 /* Is a timer active? */
353 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
354 # ifdef OPENSSL_SYS_WINDOWS
355 int timeout
= data
->socket_timeout
.tv_sec
* 1000 +
356 data
->socket_timeout
.tv_usec
/ 1000;
357 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
358 (void *)&timeout
, sizeof(timeout
)) < 0) {
359 perror("setsockopt");
363 (b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &(data
->socket_timeout
),
364 sizeof(struct timeval
)) < 0) {
365 perror("setsockopt");
372 static int dgram_read(BIO
*b
, char *out
, int outl
)
375 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
379 * See commentary in b_sock.c. <appro>
387 struct sockaddr_in sa_in
;
388 # if OPENSSL_USE_IPV6
389 struct sockaddr_in6 sa_in6
;
395 sa
.len
.i
= sizeof(sa
.peer
);
398 clear_socket_error();
399 memset(&sa
.peer
, 0, sizeof(sa
.peer
));
400 dgram_adjust_rcv_timeout(b
);
401 ret
= recvfrom(b
->num
, out
, outl
, 0, &sa
.peer
.sa
, (void *)&sa
.len
);
402 if (sizeof(sa
.len
.i
) != sizeof(sa
.len
.s
) && sa
.len
.i
== 0) {
403 OPENSSL_assert(sa
.len
.s
<= sizeof(sa
.peer
));
404 sa
.len
.i
= (int)sa
.len
.s
;
407 if (!data
->connected
&& ret
>= 0)
408 BIO_ctrl(b
, BIO_CTRL_DGRAM_SET_PEER
, 0, &sa
.peer
);
410 BIO_clear_retry_flags(b
);
412 if (BIO_dgram_should_retry(ret
)) {
413 BIO_set_retry_read(b
);
414 data
->_errno
= get_last_socket_error();
418 dgram_reset_rcv_timeout(b
);
423 static int dgram_write(BIO
*b
, const char *in
, int inl
)
426 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
427 clear_socket_error();
430 ret
= writesocket(b
->num
, in
, inl
);
432 int peerlen
= sizeof(data
->peer
);
434 if (data
->peer
.sa
.sa_family
== AF_INET
)
435 peerlen
= sizeof(data
->peer
.sa_in
);
436 # if OPENSSL_USE_IPV6
437 else if (data
->peer
.sa
.sa_family
== AF_INET6
)
438 peerlen
= sizeof(data
->peer
.sa_in6
);
440 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
441 ret
= sendto(b
->num
, (char *)in
, inl
, 0, &data
->peer
.sa
, peerlen
);
443 ret
= sendto(b
->num
, in
, inl
, 0, &data
->peer
.sa
, peerlen
);
447 BIO_clear_retry_flags(b
);
449 if (BIO_dgram_should_retry(ret
)) {
450 BIO_set_retry_write(b
);
451 data
->_errno
= get_last_socket_error();
457 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
461 switch (data
->peer
.sa
.sa_family
) {
464 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
468 # if OPENSSL_USE_IPV6
470 # ifdef IN6_IS_ADDR_V4MAPPED
471 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
473 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
479 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
485 /* We don't know. Go with the historical default */
492 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
496 struct sockaddr
*to
= NULL
;
497 bio_dgram_data
*data
= NULL
;
499 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
500 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
501 * modern enough to define socklen_t */
505 struct sockaddr_in s4
;
506 # if OPENSSL_USE_IPV6
507 struct sockaddr_in6 s6
;
512 data
= (bio_dgram_data
*)b
->ptr
;
517 case BIO_C_FILE_SEEK
:
520 case BIO_C_FILE_TELL
:
526 b
->num
= *((int *)ptr
);
527 b
->shutdown
= (int)num
;
539 case BIO_CTRL_GET_CLOSE
:
542 case BIO_CTRL_SET_CLOSE
:
543 b
->shutdown
= (int)num
;
545 case BIO_CTRL_PENDING
:
546 case BIO_CTRL_WPENDING
:
553 case BIO_CTRL_DGRAM_CONNECT
:
554 to
= (struct sockaddr
*)ptr
;
555 switch (to
->sa_family
) {
557 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
559 # if OPENSSL_USE_IPV6
561 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
565 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
569 /* (Linux)kernel sets DF bit on outgoing IP packets */
570 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
571 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
572 addr_len
= (socklen_t
) sizeof(addr
);
573 memset(&addr
, 0, sizeof(addr
));
574 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
578 switch (addr
.sa
.sa_family
) {
580 sockopt_val
= IP_PMTUDISC_DO
;
581 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
582 &sockopt_val
, sizeof(sockopt_val
))) < 0)
583 perror("setsockopt");
585 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
587 sockopt_val
= IPV6_PMTUDISC_DO
;
588 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
589 &sockopt_val
, sizeof(sockopt_val
))) < 0)
590 perror("setsockopt");
601 case BIO_CTRL_DGRAM_QUERY_MTU
:
602 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
603 addr_len
= (socklen_t
) sizeof(addr
);
604 memset(&addr
, 0, sizeof(addr
));
605 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
609 sockopt_len
= sizeof(sockopt_val
);
610 switch (addr
.sa
.sa_family
) {
613 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
614 &sockopt_len
)) < 0 || sockopt_val
< 0) {
618 * we assume that the transport protocol is UDP and no IP
621 data
->mtu
= sockopt_val
- 8 - 20;
625 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
628 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
629 (void *)&sockopt_val
, &sockopt_len
)) < 0
630 || sockopt_val
< 0) {
634 * we assume that the transport protocol is UDP and no IPV6
637 data
->mtu
= sockopt_val
- 8 - 40;
650 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
651 ret
= -dgram_get_mtu_overhead(data
);
652 switch (data
->peer
.sa
.sa_family
) {
656 # if OPENSSL_USE_IPV6
658 # ifdef IN6_IS_ADDR_V4MAPPED
659 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
671 case BIO_CTRL_DGRAM_GET_MTU
:
673 case BIO_CTRL_DGRAM_SET_MTU
:
677 case BIO_CTRL_DGRAM_SET_CONNECTED
:
678 to
= (struct sockaddr
*)ptr
;
682 switch (to
->sa_family
) {
684 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
686 # if OPENSSL_USE_IPV6
688 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
692 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
697 memset(&data
->peer
, 0, sizeof(data
->peer
));
700 case BIO_CTRL_DGRAM_GET_PEER
:
701 switch (data
->peer
.sa
.sa_family
) {
703 ret
= sizeof(data
->peer
.sa_in
);
705 # if OPENSSL_USE_IPV6
707 ret
= sizeof(data
->peer
.sa_in6
);
711 ret
= sizeof(data
->peer
.sa
);
714 if (num
== 0 || num
> ret
)
716 memcpy(ptr
, &data
->peer
, (ret
= num
));
718 case BIO_CTRL_DGRAM_SET_PEER
:
719 to
= (struct sockaddr
*)ptr
;
720 switch (to
->sa_family
) {
722 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
724 # if OPENSSL_USE_IPV6
726 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
730 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
734 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
735 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
737 # if defined(SO_RCVTIMEO)
738 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
739 # ifdef OPENSSL_SYS_WINDOWS
741 struct timeval
*tv
= (struct timeval
*)ptr
;
742 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
743 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
744 (void *)&timeout
, sizeof(timeout
)) < 0) {
745 perror("setsockopt");
750 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
751 sizeof(struct timeval
)) < 0) {
752 perror("setsockopt");
757 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
765 # ifdef OPENSSL_SYS_WINDOWS
767 struct timeval
*tv
= (struct timeval
*)ptr
;
769 sz
.i
= sizeof(timeout
);
770 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
771 (void *)&timeout
, &sz
.i
) < 0) {
772 perror("getsockopt");
775 tv
->tv_sec
= timeout
/ 1000;
776 tv
->tv_usec
= (timeout
% 1000) * 1000;
780 sz
.i
= sizeof(struct timeval
);
781 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
782 ptr
, (void *)&sz
) < 0) {
783 perror("getsockopt");
785 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
786 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
794 # if defined(SO_SNDTIMEO)
795 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
796 # ifdef OPENSSL_SYS_WINDOWS
798 struct timeval
*tv
= (struct timeval
*)ptr
;
799 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
800 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
801 (void *)&timeout
, sizeof(timeout
)) < 0) {
802 perror("setsockopt");
807 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
808 sizeof(struct timeval
)) < 0) {
809 perror("setsockopt");
814 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
822 # ifdef OPENSSL_SYS_WINDOWS
824 struct timeval
*tv
= (struct timeval
*)ptr
;
826 sz
.i
= sizeof(timeout
);
827 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
828 (void *)&timeout
, &sz
.i
) < 0) {
829 perror("getsockopt");
832 tv
->tv_sec
= timeout
/ 1000;
833 tv
->tv_usec
= (timeout
% 1000) * 1000;
837 sz
.i
= sizeof(struct timeval
);
838 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
839 ptr
, (void *)&sz
) < 0) {
840 perror("getsockopt");
842 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
843 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
851 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
853 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
854 # ifdef OPENSSL_SYS_WINDOWS
855 if (data
->_errno
== WSAETIMEDOUT
)
857 if (data
->_errno
== EAGAIN
)
866 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
867 if (data
->_errno
== EMSGSIZE
) {
874 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
875 sockopt_val
= num
? 1 : 0;
877 switch (data
->peer
.sa
.sa_family
) {
879 # if defined(IP_DONTFRAG)
880 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
881 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
882 perror("setsockopt");
885 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
886 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
887 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
888 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
889 perror("setsockopt");
892 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
893 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
894 (const char *)&sockopt_val
,
895 sizeof(sockopt_val
))) < 0) {
896 perror("setsockopt");
903 # if OPENSSL_USE_IPV6
905 # if defined(IPV6_DONTFRAG)
906 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
907 (const void *)&sockopt_val
,
908 sizeof(sockopt_val
))) < 0) {
909 perror("setsockopt");
912 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
913 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
914 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
915 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
916 perror("setsockopt");
929 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
930 ret
= dgram_get_mtu_overhead(data
);
939 static int dgram_puts(BIO
*bp
, const char *str
)
944 ret
= dgram_write(bp
, str
, n
);
948 # ifndef OPENSSL_NO_SCTP
949 BIO_METHOD
*BIO_s_datagram_sctp(void)
951 return (&methods_dgramp_sctp
);
954 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
957 int ret
, optval
= 20000;
958 int auth_data
= 0, auth_forward
= 0;
960 struct sctp_authchunk auth
;
961 struct sctp_authchunks
*authchunks
;
962 socklen_t sockopt_len
;
963 # ifdef SCTP_AUTHENTICATION_EVENT
965 struct sctp_event event
;
967 struct sctp_event_subscribe event
;
971 bio
= BIO_new(BIO_s_datagram_sctp());
974 BIO_set_fd(bio
, fd
, close_flag
);
976 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
977 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
979 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
980 sizeof(struct sctp_authchunk
));
985 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
987 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
988 sizeof(struct sctp_authchunk
));
995 * Test if activation was successful. When using accept(), SCTP-AUTH has
996 * to be activated for the listening socket already, otherwise the
997 * connected socket won't use it.
999 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1000 authchunks
= OPENSSL_malloc(sockopt_len
);
1005 memset(authchunks
, 0, sockopt_len
);
1007 getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1011 OPENSSL_free(authchunks
);
1016 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1017 p
< (unsigned char *)authchunks
+ sockopt_len
;
1018 p
+= sizeof(uint8_t)) {
1019 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1021 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1025 OPENSSL_free(authchunks
);
1027 OPENSSL_assert(auth_data
);
1028 OPENSSL_assert(auth_forward
);
1030 # ifdef SCTP_AUTHENTICATION_EVENT
1032 memset(&event
, 0, sizeof(event
));
1033 event
.se_assoc_id
= 0;
1034 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1037 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1038 sizeof(struct sctp_event
));
1044 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1045 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1051 event
.sctp_authentication_event
= 1;
1054 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1055 sizeof(struct sctp_event_subscribe
));
1064 * Disable partial delivery by setting the min size larger than the max
1065 * record size of 2^14 + 2048 + 13
1068 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1078 int BIO_dgram_is_sctp(BIO
*bio
)
1080 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1083 static int dgram_sctp_new(BIO
*bi
)
1085 bio_dgram_sctp_data
*data
= NULL
;
1089 data
= OPENSSL_malloc(sizeof(*data
));
1092 memset(data
, 0, sizeof(*data
));
1093 # ifdef SCTP_PR_SCTP_NONE
1094 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1102 static int dgram_sctp_free(BIO
*a
)
1104 bio_dgram_sctp_data
*data
;
1108 if (!dgram_clear(a
))
1111 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1113 OPENSSL_free(data
->saved_message
.data
);
1120 # ifdef SCTP_AUTHENTICATION_EVENT
1121 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1122 union sctp_notification
*snp
)
1125 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1127 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1128 struct sctp_authkeyid authkeyid
;
1131 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1132 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1133 &authkeyid
, sizeof(struct sctp_authkeyid
));
1138 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1140 int ret
= 0, n
= 0, i
, optval
;
1142 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1143 union sctp_notification
*snp
;
1146 struct cmsghdr
*cmsg
;
1150 clear_socket_error();
1153 memset(&data
->rcvinfo
, 0, sizeof(data
->rcvinfo
));
1156 msg
.msg_name
= NULL
;
1157 msg
.msg_namelen
= 0;
1160 msg
.msg_control
= cmsgbuf
;
1161 msg
.msg_controllen
= 512;
1163 n
= recvmsg(b
->num
, &msg
, 0);
1171 if (msg
.msg_controllen
> 0) {
1172 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1173 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1174 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1176 # ifdef SCTP_RCVINFO
1177 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1178 struct sctp_rcvinfo
*rcvinfo
;
1180 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1181 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1182 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1183 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1184 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1185 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1186 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1187 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1191 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1192 struct sctp_sndrcvinfo
*sndrcvinfo
;
1195 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1196 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1197 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1198 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1199 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1200 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1201 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1202 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1208 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1209 snp
= (union sctp_notification
*)out
;
1210 if (snp
->sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1212 struct sctp_event event
;
1214 struct sctp_event_subscribe event
;
1215 socklen_t eventsize
;
1218 * If a message has been delayed until the socket is dry,
1219 * it can be sent now.
1221 if (data
->saved_message
.length
> 0) {
1222 i
= dgram_sctp_write(data
->saved_message
.bio
,
1223 data
->saved_message
.data
,
1224 data
->saved_message
.length
);
1229 OPENSSL_free(data
->saved_message
.data
);
1230 data
->saved_message
.data
= NULL
;
1231 data
->saved_message
.length
= 0;
1234 /* disable sender dry event */
1236 memset(&event
, 0, sizeof(event
));
1237 event
.se_assoc_id
= 0;
1238 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1240 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1241 sizeof(struct sctp_event
));
1247 eventsize
= sizeof(struct sctp_event_subscribe
);
1248 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1255 event
.sctp_sender_dry_event
= 0;
1257 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1258 sizeof(struct sctp_event_subscribe
));
1265 # ifdef SCTP_AUTHENTICATION_EVENT
1266 if (snp
->sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1267 dgram_sctp_handle_auth_free_key_event(b
, snp
);
1270 if (data
->handle_notifications
!= NULL
)
1271 data
->handle_notifications(b
, data
->notification_context
,
1274 memset(out
, 0, outl
);
1278 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
1281 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
1282 /* Partial message read, this should never happen! */
1285 * The buffer was too small, this means the peer sent a message
1286 * that was larger than allowed.
1292 * Test if socket buffer can handle max record size (2^14 + 2048
1295 optlen
= (socklen_t
) sizeof(int);
1296 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
1298 OPENSSL_assert(optval
>= 18445);
1301 * Test if SCTP doesn't partially deliver below max record size
1302 * (2^14 + 2048 + 13)
1304 optlen
= (socklen_t
) sizeof(int);
1306 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
1309 OPENSSL_assert(optval
>= 18445);
1312 * Partially delivered notification??? Probably a bug....
1314 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
1317 * Everything seems ok till now, so it's most likely a message
1318 * dropped by PR-SCTP.
1320 memset(out
, 0, outl
);
1321 BIO_set_retry_read(b
);
1325 BIO_clear_retry_flags(b
);
1327 if (BIO_dgram_should_retry(ret
)) {
1328 BIO_set_retry_read(b
);
1329 data
->_errno
= get_last_socket_error();
1333 /* Test if peer uses SCTP-AUTH before continuing */
1334 if (!data
->peer_auth_tested
) {
1335 int ii
, auth_data
= 0, auth_forward
= 0;
1337 struct sctp_authchunks
*authchunks
;
1340 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1341 authchunks
= OPENSSL_malloc(optlen
);
1343 BIOerr(BIO_F_DGRAM_SCTP_READ
, ERR_R_MALLOC_FAILURE
);
1346 memset(authchunks
, 0, optlen
);
1347 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
1348 authchunks
, &optlen
);
1351 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1352 p
< (unsigned char *)authchunks
+ optlen
;
1353 p
+= sizeof(uint8_t)) {
1354 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1356 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1360 OPENSSL_free(authchunks
);
1362 if (!auth_data
|| !auth_forward
) {
1363 BIOerr(BIO_F_DGRAM_SCTP_READ
, BIO_R_CONNECT_ERROR
);
1367 data
->peer_auth_tested
= 1;
1374 * dgram_sctp_write - send message on SCTP socket
1375 * @b: BIO to write to
1377 * @inl: amount of bytes in @in to send
1379 * Returns -1 on error or the sent amount of bytes on success
1381 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
1384 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1385 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
1386 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
1387 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
1388 struct iovec iov
[1];
1390 struct cmsghdr
*cmsg
;
1391 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1392 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
1393 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
1394 struct sctp_sndinfo
*sndinfo
;
1395 struct sctp_prinfo
*prinfo
;
1397 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
1398 struct sctp_sndrcvinfo
*sndrcvinfo
;
1401 clear_socket_error();
1404 * If we're send anything else than application data, disable all user
1405 * parameters and flags.
1408 memset(&handshake_sinfo
, 0, sizeof(handshake_sinfo
));
1409 # ifdef SCTP_SACK_IMMEDIATELY
1410 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
1412 sinfo
= &handshake_sinfo
;
1416 * If we have to send a shutdown alert message and the socket is not dry
1417 * yet, we have to save it and send it as soon as the socket gets dry.
1419 if (data
->save_shutdown
) {
1420 ret
= BIO_dgram_sctp_wait_for_dry(b
);
1426 data
->saved_message
.bio
= b
;
1427 if ((tmp
= OPENSSL_malloc(inl
)) == NULL
) {
1428 BIOerr(BIO_F_DGRAM_SCTP_WRITE
, ERR_R_MALLOC_FAILURE
);
1431 OPENSSL_free(data
->saved_message
.data
);
1432 data
->saved_message
.data
= tmp
;
1433 memcpy(data
->saved_message
.data
, in
, inl
);
1434 data
->saved_message
.length
= inl
;
1439 iov
[0].iov_base
= (char *)in
;
1440 iov
[0].iov_len
= inl
;
1441 msg
.msg_name
= NULL
;
1442 msg
.msg_namelen
= 0;
1445 msg
.msg_control
= (caddr_t
) cmsgbuf
;
1446 msg
.msg_controllen
= 0;
1448 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1449 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1450 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1451 cmsg
->cmsg_type
= SCTP_SNDINFO
;
1452 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
1453 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
1454 memset(sndinfo
, 0, sizeof(*sndinfo
));
1455 sndinfo
->snd_sid
= sinfo
->snd_sid
;
1456 sndinfo
->snd_flags
= sinfo
->snd_flags
;
1457 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
1458 sndinfo
->snd_context
= sinfo
->snd_context
;
1459 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
1462 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
1463 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1464 cmsg
->cmsg_type
= SCTP_PRINFO
;
1465 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
1466 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
1467 memset(prinfo
, 0, sizeof(*prinfo
));
1468 prinfo
->pr_policy
= pinfo
->pr_policy
;
1469 prinfo
->pr_value
= pinfo
->pr_value
;
1470 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
1472 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1473 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1474 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1475 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1476 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1477 memset(sndrcvinfo
, 0, sizeof(*sndrcvinfo
));
1478 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
1479 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
1481 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
1483 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
1484 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
1485 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
1486 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
1489 ret
= sendmsg(b
->num
, &msg
, 0);
1491 BIO_clear_retry_flags(b
);
1493 if (BIO_dgram_should_retry(ret
)) {
1494 BIO_set_retry_write(b
);
1495 data
->_errno
= get_last_socket_error();
1501 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
1504 bio_dgram_sctp_data
*data
= NULL
;
1505 socklen_t sockopt_len
= 0;
1506 struct sctp_authkeyid authkeyid
;
1507 struct sctp_authkey
*authkey
= NULL
;
1509 data
= (bio_dgram_sctp_data
*) b
->ptr
;
1512 case BIO_CTRL_DGRAM_QUERY_MTU
:
1514 * Set to maximum (2^14) and ignore user input to enable transport
1515 * protocol fragmentation. Returns always 2^14.
1520 case BIO_CTRL_DGRAM_SET_MTU
:
1522 * Set to maximum (2^14) and ignore input to enable transport
1523 * protocol fragmentation. Returns always 2^14.
1528 case BIO_CTRL_DGRAM_SET_CONNECTED
:
1529 case BIO_CTRL_DGRAM_CONNECT
:
1530 /* Returns always -1. */
1533 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
1535 * SCTP doesn't need the DTLS timer Returns always 1.
1538 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
1540 * We allow transport protocol fragmentation so this is irrelevant
1544 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
1546 data
->in_handshake
= 1;
1548 data
->in_handshake
= 0;
1551 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
1552 &data
->in_handshake
, sizeof(int));
1554 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
1556 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1559 /* Get active key */
1560 sockopt_len
= sizeof(struct sctp_authkeyid
);
1562 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1568 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
1569 authkey
= OPENSSL_malloc(sockopt_len
);
1570 if (authkey
== NULL
) {
1574 memset(authkey
, 0, sockopt_len
);
1575 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
1576 # ifndef __FreeBSD__
1578 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1579 * and higher work without it.
1581 authkey
->sca_keylength
= 64;
1583 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
1586 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
1588 OPENSSL_free(authkey
);
1593 /* Reset active key */
1594 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1595 &authkeyid
, sizeof(struct sctp_authkeyid
));
1600 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
1601 /* Returns 0 on success, -1 otherwise. */
1603 /* Get active key */
1604 sockopt_len
= sizeof(struct sctp_authkeyid
);
1606 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1611 /* Set active key */
1612 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
1613 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1614 &authkeyid
, sizeof(struct sctp_authkeyid
));
1619 * CCS has been sent, so remember that and fall through to check if
1620 * we need to deactivate an old key
1624 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
1625 /* Returns 0 on success, -1 otherwise. */
1628 * Has this command really been called or is this just a
1631 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
1635 * CSS has been both, received and sent, so deactivate an old key
1637 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
1638 /* Get active key */
1639 sockopt_len
= sizeof(struct sctp_authkeyid
);
1641 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1642 &authkeyid
, &sockopt_len
);
1647 * Deactivate key or delete second last key if
1648 * SCTP_AUTHENTICATION_EVENT is not available.
1650 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1651 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1652 sockopt_len
= sizeof(struct sctp_authkeyid
);
1653 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
1654 &authkeyid
, sockopt_len
);
1658 # ifndef SCTP_AUTHENTICATION_EVENT
1659 if (authkeyid
.scact_keynumber
> 0) {
1660 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1661 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1662 &authkeyid
, sizeof(struct sctp_authkeyid
));
1672 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
1673 /* Returns the size of the copied struct. */
1674 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1675 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1677 memcpy(ptr
, &(data
->sndinfo
), num
);
1680 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
1681 /* Returns the size of the copied struct. */
1682 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1683 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1685 memcpy(&(data
->sndinfo
), ptr
, num
);
1687 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
1688 /* Returns the size of the copied struct. */
1689 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1690 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1692 memcpy(ptr
, &data
->rcvinfo
, num
);
1696 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
1697 /* Returns the size of the copied struct. */
1698 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1699 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1701 memcpy(&(data
->rcvinfo
), ptr
, num
);
1703 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
1704 /* Returns the size of the copied struct. */
1705 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1706 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1708 memcpy(ptr
, &(data
->prinfo
), num
);
1711 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
1712 /* Returns the size of the copied struct. */
1713 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1714 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1716 memcpy(&(data
->prinfo
), ptr
, num
);
1718 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
1719 /* Returns always 1. */
1721 data
->save_shutdown
= 1;
1723 data
->save_shutdown
= 0;
1728 * Pass to default ctrl function to process SCTP unspecific commands
1730 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
1736 int BIO_dgram_sctp_notification_cb(BIO
*b
,
1737 void (*handle_notifications
) (BIO
*bio
,
1743 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1745 if (handle_notifications
!= NULL
) {
1746 data
->handle_notifications
= handle_notifications
;
1747 data
->notification_context
= context
;
1755 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1756 * @b: The BIO to check for the dry event
1758 * Wait until the peer confirms all packets have been received, and so that
1759 * our kernel doesn't have anything to send anymore. This is only received by
1760 * the peer's kernel, not the application.
1764 * 0 when not dry yet
1767 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
1770 int n
, sockflags
, ret
;
1771 union sctp_notification snp
;
1775 struct sctp_event event
;
1777 struct sctp_event_subscribe event
;
1778 socklen_t eventsize
;
1780 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1782 /* set sender dry event */
1784 memset(&event
, 0, sizeof(event
));
1785 event
.se_assoc_id
= 0;
1786 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1789 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1790 sizeof(struct sctp_event
));
1792 eventsize
= sizeof(struct sctp_event_subscribe
);
1793 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
1797 event
.sctp_sender_dry_event
= 1;
1800 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1801 sizeof(struct sctp_event_subscribe
));
1806 /* peek for notification */
1807 memset(&snp
, 0, sizeof(snp
));
1808 iov
.iov_base
= (char *)&snp
;
1809 iov
.iov_len
= sizeof(union sctp_notification
);
1810 msg
.msg_name
= NULL
;
1811 msg
.msg_namelen
= 0;
1814 msg
.msg_control
= NULL
;
1815 msg
.msg_controllen
= 0;
1818 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1820 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1821 && (get_last_socket_error() != EWOULDBLOCK
))
1827 /* if we find a notification, process it and try again if necessary */
1828 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
1829 memset(&snp
, 0, sizeof(snp
));
1830 iov
.iov_base
= (char *)&snp
;
1831 iov
.iov_len
= sizeof(union sctp_notification
);
1832 msg
.msg_name
= NULL
;
1833 msg
.msg_namelen
= 0;
1836 msg
.msg_control
= NULL
;
1837 msg
.msg_controllen
= 0;
1840 n
= recvmsg(b
->num
, &msg
, 0);
1842 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1843 && (get_last_socket_error() != EWOULDBLOCK
))
1849 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1852 /* disable sender dry event */
1854 memset(&event
, 0, sizeof(event
));
1855 event
.se_assoc_id
= 0;
1856 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1859 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1860 sizeof(struct sctp_event
));
1862 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1864 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1869 event
.sctp_sender_dry_event
= 0;
1872 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1873 sizeof(struct sctp_event_subscribe
));
1878 # ifdef SCTP_AUTHENTICATION_EVENT
1879 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1880 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1883 if (data
->handle_notifications
!= NULL
)
1884 data
->handle_notifications(b
, data
->notification_context
,
1887 /* found notification, peek again */
1888 memset(&snp
, 0, sizeof(snp
));
1889 iov
.iov_base
= (char *)&snp
;
1890 iov
.iov_len
= sizeof(union sctp_notification
);
1891 msg
.msg_name
= NULL
;
1892 msg
.msg_namelen
= 0;
1895 msg
.msg_control
= NULL
;
1896 msg
.msg_controllen
= 0;
1899 /* if we have seen the dry already, don't wait */
1901 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1902 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1905 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1908 fcntl(b
->num
, F_SETFL
, sockflags
);
1912 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1913 && (get_last_socket_error() != EWOULDBLOCK
))
1920 /* read anything else */
1924 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
1927 union sctp_notification snp
;
1930 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1932 /* Check if there are any messages waiting to be read */
1934 memset(&snp
, 0, sizeof(snp
));
1935 iov
.iov_base
= (char *)&snp
;
1936 iov
.iov_len
= sizeof(union sctp_notification
);
1937 msg
.msg_name
= NULL
;
1938 msg
.msg_namelen
= 0;
1941 msg
.msg_control
= NULL
;
1942 msg
.msg_controllen
= 0;
1945 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1946 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1947 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1948 fcntl(b
->num
, F_SETFL
, sockflags
);
1950 /* if notification, process and try again */
1951 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
1952 # ifdef SCTP_AUTHENTICATION_EVENT
1953 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1954 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1957 memset(&snp
, 0, sizeof(snp
));
1958 iov
.iov_base
= (char *)&snp
;
1959 iov
.iov_len
= sizeof(union sctp_notification
);
1960 msg
.msg_name
= NULL
;
1961 msg
.msg_namelen
= 0;
1964 msg
.msg_control
= NULL
;
1965 msg
.msg_controllen
= 0;
1967 n
= recvmsg(b
->num
, &msg
, 0);
1969 if (data
->handle_notifications
!= NULL
)
1970 data
->handle_notifications(b
, data
->notification_context
,
1974 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
1976 /* Return 1 if there is a message to be read, return 0 otherwise. */
1983 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
1988 ret
= dgram_sctp_write(bp
, str
, n
);
1993 static int BIO_dgram_should_retry(int i
)
1997 if ((i
== 0) || (i
== -1)) {
1998 err
= get_last_socket_error();
2000 # if defined(OPENSSL_SYS_WINDOWS)
2002 * If the socket return value (i) is -1 and err is unexpectedly 0 at
2003 * this point, the error code was overwritten by another system call
2004 * before this error handling is called.
2008 return (BIO_dgram_non_fatal_error(err
));
2013 int BIO_dgram_non_fatal_error(int err
)
2016 # if defined(OPENSSL_SYS_WINDOWS)
2017 # if defined(WSAEWOULDBLOCK)
2018 case WSAEWOULDBLOCK
:
2023 # ifdef WSAEWOULDBLOCK
2024 # if WSAEWOULDBLOCK != EWOULDBLOCK
2037 # if EWOULDBLOCK != EAGAIN
2062 static void get_current_time(struct timeval
*t
)
2064 # if defined(_WIN32)
2067 unsigned __int64 ul
;
2072 SystemTimeToFileTime(&st
, &now
.ft
);
2074 now
.ul
-= 116444736000000000ULL;
2076 now
.ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
2078 t
->tv_sec
= (long)(now
.ul
/ 10000000);
2079 t
->tv_usec
= ((int)(now
.ul
% 10000000)) / 10;
2080 # elif defined(OPENSSL_SYS_VMS)
2083 t
->tv_sec
= (long)tb
.time
;
2084 t
->tv_usec
= (long)tb
.millitm
* 1000;
2086 gettimeofday(t
, NULL
);