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).
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(bio_dgram_data
));
231 memset(data
, 0x00, sizeof(bio_dgram_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 timeleft
.tv_sec
-= timenow
.tv_sec
;
309 timeleft
.tv_usec
-= timenow
.tv_usec
;
310 if (timeleft
.tv_usec
< 0) {
312 timeleft
.tv_usec
+= 1000000;
315 if (timeleft
.tv_sec
< 0) {
317 timeleft
.tv_usec
= 1;
321 * Adjust socket timeout if next handhake message timer will expire
324 if ((data
->socket_timeout
.tv_sec
== 0
325 && data
->socket_timeout
.tv_usec
== 0)
326 || (data
->socket_timeout
.tv_sec
> timeleft
.tv_sec
)
327 || (data
->socket_timeout
.tv_sec
== timeleft
.tv_sec
328 && data
->socket_timeout
.tv_usec
>= timeleft
.tv_usec
)) {
329 # ifdef OPENSSL_SYS_WINDOWS
330 timeout
= timeleft
.tv_sec
* 1000 + timeleft
.tv_usec
/ 1000;
331 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
332 (void *)&timeout
, sizeof(timeout
)) < 0) {
333 perror("setsockopt");
336 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &timeleft
,
337 sizeof(struct timeval
)) < 0) {
338 perror("setsockopt");
346 static void dgram_reset_rcv_timeout(BIO
*b
)
348 # if defined(SO_RCVTIMEO)
349 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
351 /* Is a timer active? */
352 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
353 # ifdef OPENSSL_SYS_WINDOWS
354 int timeout
= data
->socket_timeout
.tv_sec
* 1000 +
355 data
->socket_timeout
.tv_usec
/ 1000;
356 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
357 (void *)&timeout
, sizeof(timeout
)) < 0) {
358 perror("setsockopt");
362 (b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, &(data
->socket_timeout
),
363 sizeof(struct timeval
)) < 0) {
364 perror("setsockopt");
371 static int dgram_read(BIO
*b
, char *out
, int outl
)
374 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
378 * See commentary in b_sock.c. <appro>
386 struct sockaddr_in sa_in
;
387 # if OPENSSL_USE_IPV6
388 struct sockaddr_in6 sa_in6
;
394 sa
.len
.i
= sizeof(sa
.peer
);
397 clear_socket_error();
398 memset(&sa
.peer
, 0x00, sizeof(sa
.peer
));
399 dgram_adjust_rcv_timeout(b
);
400 ret
= recvfrom(b
->num
, out
, outl
, 0, &sa
.peer
.sa
, (void *)&sa
.len
);
401 if (sizeof(sa
.len
.i
) != sizeof(sa
.len
.s
) && sa
.len
.i
== 0) {
402 OPENSSL_assert(sa
.len
.s
<= sizeof(sa
.peer
));
403 sa
.len
.i
= (int)sa
.len
.s
;
406 if (!data
->connected
&& ret
>= 0)
407 BIO_ctrl(b
, BIO_CTRL_DGRAM_SET_PEER
, 0, &sa
.peer
);
409 BIO_clear_retry_flags(b
);
411 if (BIO_dgram_should_retry(ret
)) {
412 BIO_set_retry_read(b
);
413 data
->_errno
= get_last_socket_error();
417 dgram_reset_rcv_timeout(b
);
422 static int dgram_write(BIO
*b
, const char *in
, int inl
)
425 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
426 clear_socket_error();
429 ret
= writesocket(b
->num
, in
, inl
);
431 int peerlen
= sizeof(data
->peer
);
433 if (data
->peer
.sa
.sa_family
== AF_INET
)
434 peerlen
= sizeof(data
->peer
.sa_in
);
435 # if OPENSSL_USE_IPV6
436 else if (data
->peer
.sa
.sa_family
== AF_INET6
)
437 peerlen
= sizeof(data
->peer
.sa_in6
);
439 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
440 ret
= sendto(b
->num
, (char *)in
, inl
, 0, &data
->peer
.sa
, peerlen
);
442 ret
= sendto(b
->num
, in
, inl
, 0, &data
->peer
.sa
, peerlen
);
446 BIO_clear_retry_flags(b
);
448 if (BIO_dgram_should_retry(ret
)) {
449 BIO_set_retry_write(b
);
450 data
->_errno
= get_last_socket_error();
456 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
460 switch (data
->peer
.sa
.sa_family
) {
463 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
467 # if OPENSSL_USE_IPV6
469 # ifdef IN6_IS_ADDR_V4MAPPED
470 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
472 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
478 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
484 /* We don't know. Go with the historical default */
491 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
495 struct sockaddr
*to
= NULL
;
496 bio_dgram_data
*data
= NULL
;
498 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
499 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
500 * modern enough to define socklen_t */
504 struct sockaddr_in s4
;
505 # if OPENSSL_USE_IPV6
506 struct sockaddr_in6 s6
;
511 data
= (bio_dgram_data
*)b
->ptr
;
516 case BIO_C_FILE_SEEK
:
519 case BIO_C_FILE_TELL
:
525 b
->num
= *((int *)ptr
);
526 b
->shutdown
= (int)num
;
538 case BIO_CTRL_GET_CLOSE
:
541 case BIO_CTRL_SET_CLOSE
:
542 b
->shutdown
= (int)num
;
544 case BIO_CTRL_PENDING
:
545 case BIO_CTRL_WPENDING
:
552 case BIO_CTRL_DGRAM_CONNECT
:
553 to
= (struct sockaddr
*)ptr
;
554 switch (to
->sa_family
) {
556 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
558 # if OPENSSL_USE_IPV6
560 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
564 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
568 /* (Linux)kernel sets DF bit on outgoing IP packets */
569 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
570 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
571 addr_len
= (socklen_t
) sizeof(addr
);
572 memset((void *)&addr
, 0, sizeof(addr
));
573 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
577 switch (addr
.sa
.sa_family
) {
579 sockopt_val
= IP_PMTUDISC_DO
;
580 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
581 &sockopt_val
, sizeof(sockopt_val
))) < 0)
582 perror("setsockopt");
584 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
586 sockopt_val
= IPV6_PMTUDISC_DO
;
587 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
588 &sockopt_val
, sizeof(sockopt_val
))) < 0)
589 perror("setsockopt");
600 case BIO_CTRL_DGRAM_QUERY_MTU
:
601 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
602 addr_len
= (socklen_t
) sizeof(addr
);
603 memset((void *)&addr
, 0, sizeof(addr
));
604 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
608 sockopt_len
= sizeof(sockopt_val
);
609 switch (addr
.sa
.sa_family
) {
612 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
613 &sockopt_len
)) < 0 || sockopt_val
< 0) {
617 * we assume that the transport protocol is UDP and no IP
620 data
->mtu
= sockopt_val
- 8 - 20;
624 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
627 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
628 (void *)&sockopt_val
, &sockopt_len
)) < 0
629 || sockopt_val
< 0) {
633 * we assume that the transport protocol is UDP and no IPV6
636 data
->mtu
= sockopt_val
- 8 - 40;
649 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
650 ret
= -dgram_get_mtu_overhead(data
);
651 switch (data
->peer
.sa
.sa_family
) {
655 # if OPENSSL_USE_IPV6
657 # ifdef IN6_IS_ADDR_V4MAPPED
658 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
670 case BIO_CTRL_DGRAM_GET_MTU
:
672 case BIO_CTRL_DGRAM_SET_MTU
:
676 case BIO_CTRL_DGRAM_SET_CONNECTED
:
677 to
= (struct sockaddr
*)ptr
;
681 switch (to
->sa_family
) {
683 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
685 # if OPENSSL_USE_IPV6
687 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
691 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
696 memset(&(data
->peer
), 0x00, sizeof(data
->peer
));
699 case BIO_CTRL_DGRAM_GET_PEER
:
700 switch (data
->peer
.sa
.sa_family
) {
702 ret
= sizeof(data
->peer
.sa_in
);
704 # if OPENSSL_USE_IPV6
706 ret
= sizeof(data
->peer
.sa_in6
);
710 ret
= sizeof(data
->peer
.sa
);
713 if (num
== 0 || num
> ret
)
715 memcpy(ptr
, &data
->peer
, (ret
= num
));
717 case BIO_CTRL_DGRAM_SET_PEER
:
718 to
= (struct sockaddr
*)ptr
;
719 switch (to
->sa_family
) {
721 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
723 # if OPENSSL_USE_IPV6
725 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
729 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
733 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
734 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
736 # if defined(SO_RCVTIMEO)
737 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
738 # ifdef OPENSSL_SYS_WINDOWS
740 struct timeval
*tv
= (struct timeval
*)ptr
;
741 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
742 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
743 (void *)&timeout
, sizeof(timeout
)) < 0) {
744 perror("setsockopt");
749 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
750 sizeof(struct timeval
)) < 0) {
751 perror("setsockopt");
756 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
764 # ifdef OPENSSL_SYS_WINDOWS
766 struct timeval
*tv
= (struct timeval
*)ptr
;
768 sz
.i
= sizeof(timeout
);
769 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
770 (void *)&timeout
, &sz
.i
) < 0) {
771 perror("getsockopt");
774 tv
->tv_sec
= timeout
/ 1000;
775 tv
->tv_usec
= (timeout
% 1000) * 1000;
779 sz
.i
= sizeof(struct timeval
);
780 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
781 ptr
, (void *)&sz
) < 0) {
782 perror("getsockopt");
784 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
785 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
793 # if defined(SO_SNDTIMEO)
794 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
795 # ifdef OPENSSL_SYS_WINDOWS
797 struct timeval
*tv
= (struct timeval
*)ptr
;
798 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
799 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
800 (void *)&timeout
, sizeof(timeout
)) < 0) {
801 perror("setsockopt");
806 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
807 sizeof(struct timeval
)) < 0) {
808 perror("setsockopt");
813 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
821 # ifdef OPENSSL_SYS_WINDOWS
823 struct timeval
*tv
= (struct timeval
*)ptr
;
825 sz
.i
= sizeof(timeout
);
826 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
827 (void *)&timeout
, &sz
.i
) < 0) {
828 perror("getsockopt");
831 tv
->tv_sec
= timeout
/ 1000;
832 tv
->tv_usec
= (timeout
% 1000) * 1000;
836 sz
.i
= sizeof(struct timeval
);
837 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
838 ptr
, (void *)&sz
) < 0) {
839 perror("getsockopt");
841 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
842 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
850 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
852 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
853 # ifdef OPENSSL_SYS_WINDOWS
854 if (data
->_errno
== WSAETIMEDOUT
)
856 if (data
->_errno
== EAGAIN
)
865 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
866 if (data
->_errno
== EMSGSIZE
) {
873 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
874 sockopt_val
= num
? 1 : 0;
876 switch (data
->peer
.sa
.sa_family
) {
878 # if defined(IP_DONTFRAG)
879 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
880 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
881 perror("setsockopt");
884 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTUDISCOVER)
885 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
886 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
887 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
888 perror("setsockopt");
891 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
892 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
893 (const char *)&sockopt_val
,
894 sizeof(sockopt_val
))) < 0) {
895 perror("setsockopt");
902 # if OPENSSL_USE_IPV6
904 # if defined(IPV6_DONTFRAG)
905 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
906 (const void *)&sockopt_val
,
907 sizeof(sockopt_val
))) < 0) {
908 perror("setsockopt");
911 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
912 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
913 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
914 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
915 perror("setsockopt");
928 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
929 ret
= dgram_get_mtu_overhead(data
);
938 static int dgram_puts(BIO
*bp
, const char *str
)
943 ret
= dgram_write(bp
, str
, n
);
947 # ifndef OPENSSL_NO_SCTP
948 BIO_METHOD
*BIO_s_datagram_sctp(void)
950 return (&methods_dgramp_sctp
);
953 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
956 int ret
, optval
= 20000;
957 int auth_data
= 0, auth_forward
= 0;
959 struct sctp_authchunk auth
;
960 struct sctp_authchunks
*authchunks
;
961 socklen_t sockopt_len
;
962 # ifdef SCTP_AUTHENTICATION_EVENT
964 struct sctp_event event
;
966 struct sctp_event_subscribe event
;
970 bio
= BIO_new(BIO_s_datagram_sctp());
973 BIO_set_fd(bio
, fd
, close_flag
);
975 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
976 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
978 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
979 sizeof(struct sctp_authchunk
));
984 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
986 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
987 sizeof(struct sctp_authchunk
));
994 * Test if activation was successful. When using accept(), SCTP-AUTH has
995 * to be activated for the listening socket already, otherwise the
996 * connected socket won't use it.
998 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
999 authchunks
= OPENSSL_malloc(sockopt_len
);
1004 memset(authchunks
, 0, sockopt_len
);
1006 getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1010 OPENSSL_free(authchunks
);
1015 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1016 p
< (unsigned char *)authchunks
+ sockopt_len
;
1017 p
+= sizeof(uint8_t)) {
1018 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1020 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1024 OPENSSL_free(authchunks
);
1026 OPENSSL_assert(auth_data
);
1027 OPENSSL_assert(auth_forward
);
1029 # ifdef SCTP_AUTHENTICATION_EVENT
1031 memset(&event
, 0, sizeof(struct sctp_event
));
1032 event
.se_assoc_id
= 0;
1033 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1036 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1037 sizeof(struct sctp_event
));
1043 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1044 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1050 event
.sctp_authentication_event
= 1;
1053 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1054 sizeof(struct sctp_event_subscribe
));
1063 * Disable partial delivery by setting the min size larger than the max
1064 * record size of 2^14 + 2048 + 13
1067 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1077 int BIO_dgram_is_sctp(BIO
*bio
)
1079 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1082 static int dgram_sctp_new(BIO
*bi
)
1084 bio_dgram_sctp_data
*data
= NULL
;
1088 data
= OPENSSL_malloc(sizeof(bio_dgram_sctp_data
));
1091 memset(data
, 0x00, sizeof(bio_dgram_sctp_data
));
1092 # ifdef SCTP_PR_SCTP_NONE
1093 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1101 static int dgram_sctp_free(BIO
*a
)
1103 bio_dgram_sctp_data
*data
;
1107 if (!dgram_clear(a
))
1110 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1112 OPENSSL_free(data
->saved_message
.data
);
1119 # ifdef SCTP_AUTHENTICATION_EVENT
1120 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1121 union sctp_notification
*snp
)
1124 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1126 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1127 struct sctp_authkeyid authkeyid
;
1130 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1131 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1132 &authkeyid
, sizeof(struct sctp_authkeyid
));
1137 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1139 int ret
= 0, n
= 0, i
, optval
;
1141 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1142 union sctp_notification
*snp
;
1145 struct cmsghdr
*cmsg
;
1149 clear_socket_error();
1152 memset(&data
->rcvinfo
, 0x00,
1153 sizeof(struct bio_dgram_sctp_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 dgram_sctp_write(data
->saved_message
.bio
,
1223 data
->saved_message
.data
,
1224 data
->saved_message
.length
);
1225 OPENSSL_free(data
->saved_message
.data
);
1226 data
->saved_message
.data
= NULL
;
1227 data
->saved_message
.length
= 0;
1230 /* disable sender dry event */
1232 memset(&event
, 0, sizeof(struct sctp_event
));
1233 event
.se_assoc_id
= 0;
1234 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1236 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1237 sizeof(struct sctp_event
));
1243 eventsize
= sizeof(struct sctp_event_subscribe
);
1244 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1251 event
.sctp_sender_dry_event
= 0;
1253 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1254 sizeof(struct sctp_event_subscribe
));
1261 # ifdef SCTP_AUTHENTICATION_EVENT
1262 if (snp
->sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1263 dgram_sctp_handle_auth_free_key_event(b
, snp
);
1266 if (data
->handle_notifications
!= NULL
)
1267 data
->handle_notifications(b
, data
->notification_context
,
1270 memset(out
, 0, outl
);
1274 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
1277 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
1278 /* Partial message read, this should never happen! */
1281 * The buffer was too small, this means the peer sent a message
1282 * that was larger than allowed.
1288 * Test if socket buffer can handle max record size (2^14 + 2048
1291 optlen
= (socklen_t
) sizeof(int);
1292 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
1294 OPENSSL_assert(optval
>= 18445);
1297 * Test if SCTP doesn't partially deliver below max record size
1298 * (2^14 + 2048 + 13)
1300 optlen
= (socklen_t
) sizeof(int);
1302 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
1305 OPENSSL_assert(optval
>= 18445);
1308 * Partially delivered notification??? Probably a bug....
1310 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
1313 * Everything seems ok till now, so it's most likely a message
1314 * dropped by PR-SCTP.
1316 memset(out
, 0, outl
);
1317 BIO_set_retry_read(b
);
1321 BIO_clear_retry_flags(b
);
1323 if (BIO_dgram_should_retry(ret
)) {
1324 BIO_set_retry_read(b
);
1325 data
->_errno
= get_last_socket_error();
1329 /* Test if peer uses SCTP-AUTH before continuing */
1330 if (!data
->peer_auth_tested
) {
1331 int ii
, auth_data
= 0, auth_forward
= 0;
1333 struct sctp_authchunks
*authchunks
;
1336 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1337 authchunks
= OPENSSL_malloc(optlen
);
1339 BIOerr(BIO_F_DGRAM_SCTP_READ
, ERR_R_MALLOC_FAILURE
);
1342 memset(authchunks
, 0, optlen
);
1343 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
1344 authchunks
, &optlen
);
1347 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1348 p
< (unsigned char *)authchunks
+ optlen
;
1349 p
+= sizeof(uint8_t)) {
1350 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1352 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1356 OPENSSL_free(authchunks
);
1358 if (!auth_data
|| !auth_forward
) {
1359 BIOerr(BIO_F_DGRAM_SCTP_READ
, BIO_R_CONNECT_ERROR
);
1363 data
->peer_auth_tested
= 1;
1369 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
1372 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1373 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
1374 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
1375 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
1376 struct iovec iov
[1];
1378 struct cmsghdr
*cmsg
;
1379 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1380 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
1381 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
1382 struct sctp_sndinfo
*sndinfo
;
1383 struct sctp_prinfo
*prinfo
;
1385 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
1386 struct sctp_sndrcvinfo
*sndrcvinfo
;
1389 clear_socket_error();
1392 * If we're send anything else than application data, disable all user
1393 * parameters and flags.
1396 memset(&handshake_sinfo
, 0x00, sizeof(struct bio_dgram_sctp_sndinfo
));
1397 # ifdef SCTP_SACK_IMMEDIATELY
1398 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
1400 sinfo
= &handshake_sinfo
;
1404 * If we have to send a shutdown alert message and the socket is not dry
1405 * yet, we have to save it and send it as soon as the socket gets dry.
1407 if (data
->save_shutdown
&& !BIO_dgram_sctp_wait_for_dry(b
)) {
1409 data
->saved_message
.bio
= b
;
1410 if (!(tmp
= OPENSSL_malloc(inl
))) {
1411 BIOerr(BIO_F_DGRAM_SCTP_WRITE
, ERR_R_MALLOC_FAILURE
);
1414 OPENSSL_free(data
->saved_message
.data
);
1415 data
->saved_message
.data
= tmp
;
1416 memcpy(data
->saved_message
.data
, in
, inl
);
1417 data
->saved_message
.length
= inl
;
1421 iov
[0].iov_base
= (char *)in
;
1422 iov
[0].iov_len
= inl
;
1423 msg
.msg_name
= NULL
;
1424 msg
.msg_namelen
= 0;
1427 msg
.msg_control
= (caddr_t
) cmsgbuf
;
1428 msg
.msg_controllen
= 0;
1430 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1431 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1432 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1433 cmsg
->cmsg_type
= SCTP_SNDINFO
;
1434 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
1435 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
1436 memset(sndinfo
, 0, sizeof(struct sctp_sndinfo
));
1437 sndinfo
->snd_sid
= sinfo
->snd_sid
;
1438 sndinfo
->snd_flags
= sinfo
->snd_flags
;
1439 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
1440 sndinfo
->snd_context
= sinfo
->snd_context
;
1441 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
1444 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
1445 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1446 cmsg
->cmsg_type
= SCTP_PRINFO
;
1447 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
1448 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
1449 memset(prinfo
, 0, sizeof(struct sctp_prinfo
));
1450 prinfo
->pr_policy
= pinfo
->pr_policy
;
1451 prinfo
->pr_value
= pinfo
->pr_value
;
1452 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
1454 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1455 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1456 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1457 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1458 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1459 memset(sndrcvinfo
, 0, sizeof(struct sctp_sndrcvinfo
));
1460 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
1461 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
1463 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
1465 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
1466 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
1467 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
1468 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
1471 ret
= sendmsg(b
->num
, &msg
, 0);
1473 BIO_clear_retry_flags(b
);
1475 if (BIO_dgram_should_retry(ret
)) {
1476 BIO_set_retry_write(b
);
1477 data
->_errno
= get_last_socket_error();
1483 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
1486 bio_dgram_sctp_data
*data
= NULL
;
1487 socklen_t sockopt_len
= 0;
1488 struct sctp_authkeyid authkeyid
;
1489 struct sctp_authkey
*authkey
= NULL
;
1491 data
= (bio_dgram_sctp_data
*) b
->ptr
;
1494 case BIO_CTRL_DGRAM_QUERY_MTU
:
1496 * Set to maximum (2^14) and ignore user input to enable transport
1497 * protocol fragmentation. Returns always 2^14.
1502 case BIO_CTRL_DGRAM_SET_MTU
:
1504 * Set to maximum (2^14) and ignore input to enable transport
1505 * protocol fragmentation. Returns always 2^14.
1510 case BIO_CTRL_DGRAM_SET_CONNECTED
:
1511 case BIO_CTRL_DGRAM_CONNECT
:
1512 /* Returns always -1. */
1515 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
1517 * SCTP doesn't need the DTLS timer Returns always 1.
1520 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
1522 * We allow transport protocol fragmentation so this is irrelevant
1526 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
1528 data
->in_handshake
= 1;
1530 data
->in_handshake
= 0;
1533 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
1534 &data
->in_handshake
, sizeof(int));
1536 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
1538 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1541 /* Get active key */
1542 sockopt_len
= sizeof(struct sctp_authkeyid
);
1544 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1550 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
1551 authkey
= OPENSSL_malloc(sockopt_len
);
1552 if (authkey
== NULL
) {
1556 memset(authkey
, 0x00, sockopt_len
);
1557 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
1558 # ifndef __FreeBSD__
1560 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1561 * and higher work without it.
1563 authkey
->sca_keylength
= 64;
1565 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
1568 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
1570 OPENSSL_free(authkey
);
1575 /* Reset active key */
1576 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1577 &authkeyid
, sizeof(struct sctp_authkeyid
));
1582 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
1583 /* Returns 0 on success, -1 otherwise. */
1585 /* Get active key */
1586 sockopt_len
= sizeof(struct sctp_authkeyid
);
1588 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1593 /* Set active key */
1594 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
1595 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1596 &authkeyid
, sizeof(struct sctp_authkeyid
));
1601 * CCS has been sent, so remember that and fall through to check if
1602 * we need to deactivate an old key
1606 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
1607 /* Returns 0 on success, -1 otherwise. */
1610 * Has this command really been called or is this just a
1613 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
1617 * CSS has been both, received and sent, so deactivate an old key
1619 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
1620 /* Get active key */
1621 sockopt_len
= sizeof(struct sctp_authkeyid
);
1623 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1624 &authkeyid
, &sockopt_len
);
1629 * Deactivate key or delete second last key if
1630 * SCTP_AUTHENTICATION_EVENT is not available.
1632 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1633 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1634 sockopt_len
= sizeof(struct sctp_authkeyid
);
1635 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
1636 &authkeyid
, sockopt_len
);
1640 # ifndef SCTP_AUTHENTICATION_EVENT
1641 if (authkeyid
.scact_keynumber
> 0) {
1642 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1643 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1644 &authkeyid
, sizeof(struct sctp_authkeyid
));
1654 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
1655 /* Returns the size of the copied struct. */
1656 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1657 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1659 memcpy(ptr
, &(data
->sndinfo
), num
);
1662 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
1663 /* Returns the size of the copied struct. */
1664 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1665 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1667 memcpy(&(data
->sndinfo
), ptr
, num
);
1669 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
1670 /* Returns the size of the copied struct. */
1671 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1672 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1674 memcpy(ptr
, &data
->rcvinfo
, num
);
1678 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
1679 /* Returns the size of the copied struct. */
1680 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1681 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1683 memcpy(&(data
->rcvinfo
), ptr
, num
);
1685 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
1686 /* Returns the size of the copied struct. */
1687 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1688 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1690 memcpy(ptr
, &(data
->prinfo
), num
);
1693 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
1694 /* Returns the size of the copied struct. */
1695 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1696 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1698 memcpy(&(data
->prinfo
), ptr
, num
);
1700 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
1701 /* Returns always 1. */
1703 data
->save_shutdown
= 1;
1705 data
->save_shutdown
= 0;
1710 * Pass to default ctrl function to process SCTP unspecific commands
1712 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
1718 int BIO_dgram_sctp_notification_cb(BIO
*b
,
1719 void (*handle_notifications
) (BIO
*bio
,
1725 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1727 if (handle_notifications
!= NULL
) {
1728 data
->handle_notifications
= handle_notifications
;
1729 data
->notification_context
= context
;
1736 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
1739 int n
, sockflags
, ret
;
1740 union sctp_notification snp
;
1744 struct sctp_event event
;
1746 struct sctp_event_subscribe event
;
1747 socklen_t eventsize
;
1749 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1751 /* set sender dry event */
1753 memset(&event
, 0, sizeof(struct sctp_event
));
1754 event
.se_assoc_id
= 0;
1755 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1758 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1759 sizeof(struct sctp_event
));
1761 eventsize
= sizeof(struct sctp_event_subscribe
);
1762 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
1766 event
.sctp_sender_dry_event
= 1;
1769 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1770 sizeof(struct sctp_event_subscribe
));
1775 /* peek for notification */
1776 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1777 iov
.iov_base
= (char *)&snp
;
1778 iov
.iov_len
= sizeof(union sctp_notification
);
1779 msg
.msg_name
= NULL
;
1780 msg
.msg_namelen
= 0;
1783 msg
.msg_control
= NULL
;
1784 msg
.msg_controllen
= 0;
1787 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1789 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1790 && (get_last_socket_error() != EWOULDBLOCK
))
1796 /* if we find a notification, process it and try again if necessary */
1797 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
1798 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1799 iov
.iov_base
= (char *)&snp
;
1800 iov
.iov_len
= sizeof(union sctp_notification
);
1801 msg
.msg_name
= NULL
;
1802 msg
.msg_namelen
= 0;
1805 msg
.msg_control
= NULL
;
1806 msg
.msg_controllen
= 0;
1809 n
= recvmsg(b
->num
, &msg
, 0);
1811 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1812 && (get_last_socket_error() != EWOULDBLOCK
))
1818 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1821 /* disable sender dry event */
1823 memset(&event
, 0, sizeof(struct sctp_event
));
1824 event
.se_assoc_id
= 0;
1825 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1828 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1829 sizeof(struct sctp_event
));
1831 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1833 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1838 event
.sctp_sender_dry_event
= 0;
1841 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1842 sizeof(struct sctp_event_subscribe
));
1847 # ifdef SCTP_AUTHENTICATION_EVENT
1848 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1849 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1852 if (data
->handle_notifications
!= NULL
)
1853 data
->handle_notifications(b
, data
->notification_context
,
1856 /* found notification, peek again */
1857 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1858 iov
.iov_base
= (char *)&snp
;
1859 iov
.iov_len
= sizeof(union sctp_notification
);
1860 msg
.msg_name
= NULL
;
1861 msg
.msg_namelen
= 0;
1864 msg
.msg_control
= NULL
;
1865 msg
.msg_controllen
= 0;
1868 /* if we have seen the dry already, don't wait */
1870 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1871 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1874 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1877 fcntl(b
->num
, F_SETFL
, sockflags
);
1881 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1882 && (get_last_socket_error() != EWOULDBLOCK
))
1889 /* read anything else */
1893 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
1896 union sctp_notification snp
;
1899 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1901 /* Check if there are any messages waiting to be read */
1903 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1904 iov
.iov_base
= (char *)&snp
;
1905 iov
.iov_len
= sizeof(union sctp_notification
);
1906 msg
.msg_name
= NULL
;
1907 msg
.msg_namelen
= 0;
1910 msg
.msg_control
= NULL
;
1911 msg
.msg_controllen
= 0;
1914 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1915 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1916 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1917 fcntl(b
->num
, F_SETFL
, sockflags
);
1919 /* if notification, process and try again */
1920 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
1921 # ifdef SCTP_AUTHENTICATION_EVENT
1922 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1923 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1926 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1927 iov
.iov_base
= (char *)&snp
;
1928 iov
.iov_len
= sizeof(union sctp_notification
);
1929 msg
.msg_name
= NULL
;
1930 msg
.msg_namelen
= 0;
1933 msg
.msg_control
= NULL
;
1934 msg
.msg_controllen
= 0;
1936 n
= recvmsg(b
->num
, &msg
, 0);
1938 if (data
->handle_notifications
!= NULL
)
1939 data
->handle_notifications(b
, data
->notification_context
,
1943 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
1945 /* Return 1 if there is a message to be read, return 0 otherwise. */
1952 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
1957 ret
= dgram_sctp_write(bp
, str
, n
);
1962 static int BIO_dgram_should_retry(int i
)
1966 if ((i
== 0) || (i
== -1)) {
1967 err
= get_last_socket_error();
1969 # if defined(OPENSSL_SYS_WINDOWS)
1971 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1972 * this point, the error code was overwritten by another system call
1973 * before this error handling is called.
1977 return (BIO_dgram_non_fatal_error(err
));
1982 int BIO_dgram_non_fatal_error(int err
)
1985 # if defined(OPENSSL_SYS_WINDOWS)
1986 # if defined(WSAEWOULDBLOCK)
1987 case WSAEWOULDBLOCK
:
1992 # ifdef WSAEWOULDBLOCK
1993 # if WSAEWOULDBLOCK != EWOULDBLOCK
2006 # if EWOULDBLOCK != EAGAIN
2031 static void get_current_time(struct timeval
*t
)
2033 # if defined(_WIN32)
2036 unsigned __int64 ul
;
2041 SystemTimeToFileTime(&st
, &now
.ft
);
2043 now
.ul
-= 116444736000000000ULL;
2045 now
.ul
-= 116444736000000000UI
64; /* re-bias to 1/1/1970 */
2047 t
->tv_sec
= (long)(now
.ul
/ 10000000);
2048 t
->tv_usec
= ((int)(now
.ul
% 10000000)) / 10;
2049 # elif defined(OPENSSL_SYS_VMS)
2052 t
->tv_sec
= (long)tb
.time
;
2053 t
->tv_usec
= (long)tb
.millitm
* 1000;
2055 gettimeofday(t
, NULL
);