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
;
254 static int dgram_clear(BIO
*a
)
268 static void dgram_adjust_rcv_timeout(BIO
*b
)
270 # if defined(SO_RCVTIMEO)
271 bio_dgram_data
*data
= (bio_dgram_data
*)b
->ptr
;
279 /* Is a timer active? */
280 if (data
->next_timeout
.tv_sec
> 0 || data
->next_timeout
.tv_usec
> 0) {
281 struct timeval timenow
, timeleft
;
283 /* Read current socket timeout */
284 # ifdef OPENSSL_SYS_WINDOWS
287 sz
.i
= sizeof(timeout
);
288 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
289 (void *)&timeout
, &sz
.i
) < 0) {
290 perror("getsockopt");
292 data
->socket_timeout
.tv_sec
= timeout
/ 1000;
293 data
->socket_timeout
.tv_usec
= (timeout
% 1000) * 1000;
296 sz
.i
= sizeof(data
->socket_timeout
);
297 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
298 &(data
->socket_timeout
), (void *)&sz
) < 0) {
299 perror("getsockopt");
300 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0)
301 OPENSSL_assert(sz
.s
<= sizeof(data
->socket_timeout
));
304 /* Get current time */
305 get_current_time(&timenow
);
307 /* Calculate time left until timer expires */
308 memcpy(&timeleft
, &(data
->next_timeout
), sizeof(struct timeval
));
309 timeleft
.tv_sec
-= timenow
.tv_sec
;
310 timeleft
.tv_usec
-= timenow
.tv_usec
;
311 if (timeleft
.tv_usec
< 0) {
313 timeleft
.tv_usec
+= 1000000;
316 if (timeleft
.tv_sec
< 0) {
318 timeleft
.tv_usec
= 1;
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
, 0x00, 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();
453 # if 0 /* higher layers are responsible for querying
454 * MTU, if necessary */
455 if (data
->_errno
== EMSGSIZE
)
456 /* retrieve the new MTU */
457 BIO_ctrl(b
, BIO_CTRL_DGRAM_QUERY_MTU
, 0, NULL
);
464 static long dgram_get_mtu_overhead(bio_dgram_data
*data
)
468 switch (data
->peer
.sa
.sa_family
) {
471 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
475 # if OPENSSL_USE_IPV6
477 # ifdef IN6_IS_ADDR_V4MAPPED
478 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
480 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
486 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
492 /* We don't know. Go with the historical default */
499 static long dgram_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
503 struct sockaddr
*to
= NULL
;
504 bio_dgram_data
*data
= NULL
;
506 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
507 socklen_t sockopt_len
; /* assume that system supporting IP_MTU is
508 * modern enough to define socklen_t */
512 struct sockaddr_in s4
;
513 # if OPENSSL_USE_IPV6
514 struct sockaddr_in6 s6
;
519 data
= (bio_dgram_data
*)b
->ptr
;
524 case BIO_C_FILE_SEEK
:
527 case BIO_C_FILE_TELL
:
533 b
->num
= *((int *)ptr
);
534 b
->shutdown
= (int)num
;
546 case BIO_CTRL_GET_CLOSE
:
549 case BIO_CTRL_SET_CLOSE
:
550 b
->shutdown
= (int)num
;
552 case BIO_CTRL_PENDING
:
553 case BIO_CTRL_WPENDING
:
560 case BIO_CTRL_DGRAM_CONNECT
:
561 to
= (struct sockaddr
*)ptr
;
563 if (connect(b
->num
, to
, sizeof(struct sockaddr
)) < 0) {
568 switch (to
->sa_family
) {
570 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
572 # if OPENSSL_USE_IPV6
574 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
578 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
585 /* (Linux)kernel sets DF bit on outgoing IP packets */
586 case BIO_CTRL_DGRAM_MTU_DISCOVER
:
587 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
588 addr_len
= (socklen_t
) sizeof(addr
);
589 memset((void *)&addr
, 0, sizeof(addr
));
590 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
594 switch (addr
.sa
.sa_family
) {
596 sockopt_val
= IP_PMTUDISC_DO
;
597 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
598 &sockopt_val
, sizeof(sockopt_val
))) < 0)
599 perror("setsockopt");
601 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
603 sockopt_val
= IPV6_PMTUDISC_DO
;
604 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
605 &sockopt_val
, sizeof(sockopt_val
))) < 0)
606 perror("setsockopt");
617 case BIO_CTRL_DGRAM_QUERY_MTU
:
618 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
619 addr_len
= (socklen_t
) sizeof(addr
);
620 memset((void *)&addr
, 0, sizeof(addr
));
621 if (getsockname(b
->num
, &addr
.sa
, &addr_len
) < 0) {
625 sockopt_len
= sizeof(sockopt_val
);
626 switch (addr
.sa
.sa_family
) {
629 getsockopt(b
->num
, IPPROTO_IP
, IP_MTU
, (void *)&sockopt_val
,
630 &sockopt_len
)) < 0 || sockopt_val
< 0) {
634 * we assume that the transport protocol is UDP and no IP
637 data
->mtu
= sockopt_val
- 8 - 20;
641 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
644 getsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU
,
645 (void *)&sockopt_val
, &sockopt_len
)) < 0
646 || sockopt_val
< 0) {
650 * we assume that the transport protocol is UDP and no IPV6
653 data
->mtu
= sockopt_val
- 8 - 40;
666 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
667 ret
= -dgram_get_mtu_overhead(data
);
668 switch (data
->peer
.sa
.sa_family
) {
672 # if OPENSSL_USE_IPV6
674 # ifdef IN6_IS_ADDR_V4MAPPED
675 if (IN6_IS_ADDR_V4MAPPED(&data
->peer
.sa_in6
.sin6_addr
))
687 case BIO_CTRL_DGRAM_GET_MTU
:
690 case BIO_CTRL_DGRAM_SET_MTU
:
694 case BIO_CTRL_DGRAM_SET_CONNECTED
:
695 to
= (struct sockaddr
*)ptr
;
699 switch (to
->sa_family
) {
701 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
703 # if OPENSSL_USE_IPV6
705 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
709 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
714 memset(&(data
->peer
), 0x00, sizeof(data
->peer
));
717 case BIO_CTRL_DGRAM_GET_PEER
:
718 switch (data
->peer
.sa
.sa_family
) {
720 ret
= sizeof(data
->peer
.sa_in
);
722 # if OPENSSL_USE_IPV6
724 ret
= sizeof(data
->peer
.sa_in6
);
728 ret
= sizeof(data
->peer
.sa
);
731 if (num
== 0 || num
> ret
)
733 memcpy(ptr
, &data
->peer
, (ret
= num
));
735 case BIO_CTRL_DGRAM_SET_PEER
:
736 to
= (struct sockaddr
*)ptr
;
737 switch (to
->sa_family
) {
739 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in
));
741 # if OPENSSL_USE_IPV6
743 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa_in6
));
747 memcpy(&data
->peer
, to
, sizeof(data
->peer
.sa
));
751 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
752 memcpy(&(data
->next_timeout
), ptr
, sizeof(struct timeval
));
754 # if defined(SO_RCVTIMEO)
755 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
:
756 # ifdef OPENSSL_SYS_WINDOWS
758 struct timeval
*tv
= (struct timeval
*)ptr
;
759 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
760 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
761 (void *)&timeout
, sizeof(timeout
)) < 0) {
762 perror("setsockopt");
767 if (setsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
, ptr
,
768 sizeof(struct timeval
)) < 0) {
769 perror("setsockopt");
774 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT
:
782 # ifdef OPENSSL_SYS_WINDOWS
784 struct timeval
*tv
= (struct timeval
*)ptr
;
786 sz
.i
= sizeof(timeout
);
787 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
788 (void *)&timeout
, &sz
.i
) < 0) {
789 perror("getsockopt");
792 tv
->tv_sec
= timeout
/ 1000;
793 tv
->tv_usec
= (timeout
% 1000) * 1000;
797 sz
.i
= sizeof(struct timeval
);
798 if (getsockopt(b
->num
, SOL_SOCKET
, SO_RCVTIMEO
,
799 ptr
, (void *)&sz
) < 0) {
800 perror("getsockopt");
802 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
803 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
811 # if defined(SO_SNDTIMEO)
812 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
:
813 # ifdef OPENSSL_SYS_WINDOWS
815 struct timeval
*tv
= (struct timeval
*)ptr
;
816 int timeout
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
817 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
818 (void *)&timeout
, sizeof(timeout
)) < 0) {
819 perror("setsockopt");
824 if (setsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
, ptr
,
825 sizeof(struct timeval
)) < 0) {
826 perror("setsockopt");
831 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT
:
839 # ifdef OPENSSL_SYS_WINDOWS
841 struct timeval
*tv
= (struct timeval
*)ptr
;
843 sz
.i
= sizeof(timeout
);
844 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
845 (void *)&timeout
, &sz
.i
) < 0) {
846 perror("getsockopt");
849 tv
->tv_sec
= timeout
/ 1000;
850 tv
->tv_usec
= (timeout
% 1000) * 1000;
854 sz
.i
= sizeof(struct timeval
);
855 if (getsockopt(b
->num
, SOL_SOCKET
, SO_SNDTIMEO
,
856 ptr
, (void *)&sz
) < 0) {
857 perror("getsockopt");
859 } else if (sizeof(sz
.s
) != sizeof(sz
.i
) && sz
.i
== 0) {
860 OPENSSL_assert(sz
.s
<= sizeof(struct timeval
));
868 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP
:
870 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP
:
871 # ifdef OPENSSL_SYS_WINDOWS
872 if (data
->_errno
== WSAETIMEDOUT
)
874 if (data
->_errno
== EAGAIN
)
883 case BIO_CTRL_DGRAM_MTU_EXCEEDED
:
884 if (data
->_errno
== EMSGSIZE
) {
891 case BIO_CTRL_DGRAM_SET_DONT_FRAG
:
892 sockopt_val
= num
? 1 : 0;
894 switch (data
->peer
.sa
.sa_family
) {
896 # if defined(IP_DONTFRAG)
897 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAG
,
898 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
899 perror("setsockopt");
902 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTUDISCOVER)
903 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
904 (ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_MTU_DISCOVER
,
905 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
906 perror("setsockopt");
909 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
910 if ((ret
= setsockopt(b
->num
, IPPROTO_IP
, IP_DONTFRAGMENT
,
911 (const char *)&sockopt_val
,
912 sizeof(sockopt_val
))) < 0) {
913 perror("setsockopt");
920 # if OPENSSL_USE_IPV6
922 # if defined(IPV6_DONTFRAG)
923 if ((ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
924 (const void *)&sockopt_val
,
925 sizeof(sockopt_val
))) < 0) {
926 perror("setsockopt");
929 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
930 if ((sockopt_val
= num
? IP_PMTUDISC_PROBE
: IP_PMTUDISC_DONT
),
931 (ret
= setsockopt(b
->num
, IPPROTO_IPV6
, IPV6_MTU_DISCOVER
,
932 &sockopt_val
, sizeof(sockopt_val
))) < 0) {
933 perror("setsockopt");
946 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
947 ret
= dgram_get_mtu_overhead(data
);
956 static int dgram_puts(BIO
*bp
, const char *str
)
961 ret
= dgram_write(bp
, str
, n
);
965 # ifndef OPENSSL_NO_SCTP
966 BIO_METHOD
*BIO_s_datagram_sctp(void)
968 return (&methods_dgramp_sctp
);
971 BIO
*BIO_new_dgram_sctp(int fd
, int close_flag
)
974 int ret
, optval
= 20000;
975 int auth_data
= 0, auth_forward
= 0;
977 struct sctp_authchunk auth
;
978 struct sctp_authchunks
*authchunks
;
979 socklen_t sockopt_len
;
980 # ifdef SCTP_AUTHENTICATION_EVENT
982 struct sctp_event event
;
984 struct sctp_event_subscribe event
;
988 bio
= BIO_new(BIO_s_datagram_sctp());
991 BIO_set_fd(bio
, fd
, close_flag
);
993 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
994 auth
.sauth_chunk
= OPENSSL_SCTP_DATA_CHUNK_TYPE
;
996 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
997 sizeof(struct sctp_authchunk
));
1002 auth
.sauth_chunk
= OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
;
1004 setsockopt(fd
, IPPROTO_SCTP
, SCTP_AUTH_CHUNK
, &auth
,
1005 sizeof(struct sctp_authchunk
));
1012 * Test if activation was successful. When using accept(), SCTP-AUTH has
1013 * to be activated for the listening socket already, otherwise the
1014 * connected socket won't use it.
1016 sockopt_len
= (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1017 authchunks
= OPENSSL_malloc(sockopt_len
);
1018 memset(authchunks
, 0, sockopt_len
);
1020 getsockopt(fd
, IPPROTO_SCTP
, SCTP_LOCAL_AUTH_CHUNKS
, authchunks
,
1024 OPENSSL_free(authchunks
);
1029 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1030 p
< (unsigned char *)authchunks
+ sockopt_len
;
1031 p
+= sizeof(uint8_t)) {
1032 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1034 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1038 OPENSSL_free(authchunks
);
1040 OPENSSL_assert(auth_data
);
1041 OPENSSL_assert(auth_forward
);
1043 # ifdef SCTP_AUTHENTICATION_EVENT
1045 memset(&event
, 0, sizeof(struct sctp_event
));
1046 event
.se_assoc_id
= 0;
1047 event
.se_type
= SCTP_AUTHENTICATION_EVENT
;
1050 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1051 sizeof(struct sctp_event
));
1057 sockopt_len
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1058 ret
= getsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &sockopt_len
);
1064 event
.sctp_authentication_event
= 1;
1067 setsockopt(fd
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1068 sizeof(struct sctp_event_subscribe
));
1077 * Disable partial delivery by setting the min size larger than the max
1078 * record size of 2^14 + 2048 + 13
1081 setsockopt(fd
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
, &optval
,
1091 int BIO_dgram_is_sctp(BIO
*bio
)
1093 return (BIO_method_type(bio
) == BIO_TYPE_DGRAM_SCTP
);
1096 static int dgram_sctp_new(BIO
*bi
)
1098 bio_dgram_sctp_data
*data
= NULL
;
1102 data
= OPENSSL_malloc(sizeof(bio_dgram_sctp_data
));
1105 memset(data
, 0x00, sizeof(bio_dgram_sctp_data
));
1106 # ifdef SCTP_PR_SCTP_NONE
1107 data
->prinfo
.pr_policy
= SCTP_PR_SCTP_NONE
;
1115 static int dgram_sctp_free(BIO
*a
)
1117 bio_dgram_sctp_data
*data
;
1121 if (!dgram_clear(a
))
1124 data
= (bio_dgram_sctp_data
*) a
->ptr
;
1126 if (data
->saved_message
.data
!= NULL
)
1127 OPENSSL_free(data
->saved_message
.data
);
1134 # ifdef SCTP_AUTHENTICATION_EVENT
1135 void dgram_sctp_handle_auth_free_key_event(BIO
*b
,
1136 union sctp_notification
*snp
)
1139 struct sctp_authkey_event
*authkeyevent
= &snp
->sn_auth_event
;
1141 if (authkeyevent
->auth_indication
== SCTP_AUTH_FREE_KEY
) {
1142 struct sctp_authkeyid authkeyid
;
1145 authkeyid
.scact_keynumber
= authkeyevent
->auth_keynumber
;
1146 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1147 &authkeyid
, sizeof(struct sctp_authkeyid
));
1152 static int dgram_sctp_read(BIO
*b
, char *out
, int outl
)
1154 int ret
= 0, n
= 0, i
, optval
;
1156 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1157 union sctp_notification
*snp
;
1160 struct cmsghdr
*cmsg
;
1164 clear_socket_error();
1167 memset(&data
->rcvinfo
, 0x00,
1168 sizeof(struct bio_dgram_sctp_rcvinfo
));
1171 msg
.msg_name
= NULL
;
1172 msg
.msg_namelen
= 0;
1175 msg
.msg_control
= cmsgbuf
;
1176 msg
.msg_controllen
= 512;
1178 n
= recvmsg(b
->num
, &msg
, 0);
1186 if (msg
.msg_controllen
> 0) {
1187 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1188 cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
1189 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
1191 # ifdef SCTP_RCVINFO
1192 if (cmsg
->cmsg_type
== SCTP_RCVINFO
) {
1193 struct sctp_rcvinfo
*rcvinfo
;
1195 rcvinfo
= (struct sctp_rcvinfo
*)CMSG_DATA(cmsg
);
1196 data
->rcvinfo
.rcv_sid
= rcvinfo
->rcv_sid
;
1197 data
->rcvinfo
.rcv_ssn
= rcvinfo
->rcv_ssn
;
1198 data
->rcvinfo
.rcv_flags
= rcvinfo
->rcv_flags
;
1199 data
->rcvinfo
.rcv_ppid
= rcvinfo
->rcv_ppid
;
1200 data
->rcvinfo
.rcv_tsn
= rcvinfo
->rcv_tsn
;
1201 data
->rcvinfo
.rcv_cumtsn
= rcvinfo
->rcv_cumtsn
;
1202 data
->rcvinfo
.rcv_context
= rcvinfo
->rcv_context
;
1206 if (cmsg
->cmsg_type
== SCTP_SNDRCV
) {
1207 struct sctp_sndrcvinfo
*sndrcvinfo
;
1210 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1211 data
->rcvinfo
.rcv_sid
= sndrcvinfo
->sinfo_stream
;
1212 data
->rcvinfo
.rcv_ssn
= sndrcvinfo
->sinfo_ssn
;
1213 data
->rcvinfo
.rcv_flags
= sndrcvinfo
->sinfo_flags
;
1214 data
->rcvinfo
.rcv_ppid
= sndrcvinfo
->sinfo_ppid
;
1215 data
->rcvinfo
.rcv_tsn
= sndrcvinfo
->sinfo_tsn
;
1216 data
->rcvinfo
.rcv_cumtsn
= sndrcvinfo
->sinfo_cumtsn
;
1217 data
->rcvinfo
.rcv_context
= sndrcvinfo
->sinfo_context
;
1223 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
1224 snp
= (union sctp_notification
*)out
;
1225 if (snp
->sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1227 struct sctp_event event
;
1229 struct sctp_event_subscribe event
;
1230 socklen_t eventsize
;
1233 * If a message has been delayed until the socket is dry,
1234 * it can be sent now.
1236 if (data
->saved_message
.length
> 0) {
1237 dgram_sctp_write(data
->saved_message
.bio
,
1238 data
->saved_message
.data
,
1239 data
->saved_message
.length
);
1240 OPENSSL_free(data
->saved_message
.data
);
1241 data
->saved_message
.data
= NULL
;
1242 data
->saved_message
.length
= 0;
1245 /* disable sender dry event */
1247 memset(&event
, 0, sizeof(struct sctp_event
));
1248 event
.se_assoc_id
= 0;
1249 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1251 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1252 sizeof(struct sctp_event
));
1258 eventsize
= sizeof(struct sctp_event_subscribe
);
1259 i
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1266 event
.sctp_sender_dry_event
= 0;
1268 i
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1269 sizeof(struct sctp_event_subscribe
));
1276 # ifdef SCTP_AUTHENTICATION_EVENT
1277 if (snp
->sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1278 dgram_sctp_handle_auth_free_key_event(b
, snp
);
1281 if (data
->handle_notifications
!= NULL
)
1282 data
->handle_notifications(b
, data
->notification_context
,
1285 memset(out
, 0, outl
);
1289 while ((msg
.msg_flags
& MSG_NOTIFICATION
) && (msg
.msg_flags
& MSG_EOR
)
1292 if (ret
> 0 && !(msg
.msg_flags
& MSG_EOR
)) {
1293 /* Partial message read, this should never happen! */
1296 * The buffer was too small, this means the peer sent a message
1297 * that was larger than allowed.
1303 * Test if socket buffer can handle max record size (2^14 + 2048
1306 optlen
= (socklen_t
) sizeof(int);
1307 ret
= getsockopt(b
->num
, SOL_SOCKET
, SO_RCVBUF
, &optval
, &optlen
);
1309 OPENSSL_assert(optval
>= 18445);
1312 * Test if SCTP doesn't partially deliver below max record size
1313 * (2^14 + 2048 + 13)
1315 optlen
= (socklen_t
) sizeof(int);
1317 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PARTIAL_DELIVERY_POINT
,
1320 OPENSSL_assert(optval
>= 18445);
1323 * Partially delivered notification??? Probably a bug....
1325 OPENSSL_assert(!(msg
.msg_flags
& MSG_NOTIFICATION
));
1328 * Everything seems ok till now, so it's most likely a message
1329 * dropped by PR-SCTP.
1331 memset(out
, 0, outl
);
1332 BIO_set_retry_read(b
);
1336 BIO_clear_retry_flags(b
);
1338 if (BIO_dgram_should_retry(ret
)) {
1339 BIO_set_retry_read(b
);
1340 data
->_errno
= get_last_socket_error();
1344 /* Test if peer uses SCTP-AUTH before continuing */
1345 if (!data
->peer_auth_tested
) {
1346 int ii
, auth_data
= 0, auth_forward
= 0;
1348 struct sctp_authchunks
*authchunks
;
1351 (socklen_t
) (sizeof(sctp_assoc_t
) + 256 * sizeof(uint8_t));
1352 authchunks
= OPENSSL_malloc(optlen
);
1353 memset(authchunks
, 0, optlen
);
1354 ii
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_PEER_AUTH_CHUNKS
,
1355 authchunks
, &optlen
);
1358 for (p
= (unsigned char *)authchunks
->gauth_chunks
;
1359 p
< (unsigned char *)authchunks
+ optlen
;
1360 p
+= sizeof(uint8_t)) {
1361 if (*p
== OPENSSL_SCTP_DATA_CHUNK_TYPE
)
1363 if (*p
== OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE
)
1367 OPENSSL_free(authchunks
);
1369 if (!auth_data
|| !auth_forward
) {
1370 BIOerr(BIO_F_DGRAM_SCTP_READ
, BIO_R_CONNECT_ERROR
);
1374 data
->peer_auth_tested
= 1;
1380 static int dgram_sctp_write(BIO
*b
, const char *in
, int inl
)
1383 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1384 struct bio_dgram_sctp_sndinfo
*sinfo
= &(data
->sndinfo
);
1385 struct bio_dgram_sctp_prinfo
*pinfo
= &(data
->prinfo
);
1386 struct bio_dgram_sctp_sndinfo handshake_sinfo
;
1387 struct iovec iov
[1];
1389 struct cmsghdr
*cmsg
;
1390 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1391 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
)) +
1392 CMSG_SPACE(sizeof(struct sctp_prinfo
))];
1393 struct sctp_sndinfo
*sndinfo
;
1394 struct sctp_prinfo
*prinfo
;
1396 char cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
1397 struct sctp_sndrcvinfo
*sndrcvinfo
;
1400 clear_socket_error();
1403 * If we're send anything else than application data, disable all user
1404 * parameters and flags.
1407 memset(&handshake_sinfo
, 0x00, sizeof(struct bio_dgram_sctp_sndinfo
));
1408 # ifdef SCTP_SACK_IMMEDIATELY
1409 handshake_sinfo
.snd_flags
= SCTP_SACK_IMMEDIATELY
;
1411 sinfo
= &handshake_sinfo
;
1415 * If we have to send a shutdown alert message and the socket is not dry
1416 * yet, we have to save it and send it as soon as the socket gets dry.
1418 if (data
->save_shutdown
&& !BIO_dgram_sctp_wait_for_dry(b
)) {
1419 data
->saved_message
.bio
= b
;
1420 if (data
->saved_message
.data
)
1421 OPENSSL_free(data
->saved_message
.data
);
1422 data
->saved_message
.data
= OPENSSL_malloc(inl
);
1423 memcpy(data
->saved_message
.data
, in
, inl
);
1424 data
->saved_message
.length
= inl
;
1428 iov
[0].iov_base
= (char *)in
;
1429 iov
[0].iov_len
= inl
;
1430 msg
.msg_name
= NULL
;
1431 msg
.msg_namelen
= 0;
1434 msg
.msg_control
= (caddr_t
) cmsgbuf
;
1435 msg
.msg_controllen
= 0;
1437 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1438 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1439 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1440 cmsg
->cmsg_type
= SCTP_SNDINFO
;
1441 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndinfo
));
1442 sndinfo
= (struct sctp_sndinfo
*)CMSG_DATA(cmsg
);
1443 memset(sndinfo
, 0, sizeof(struct sctp_sndinfo
));
1444 sndinfo
->snd_sid
= sinfo
->snd_sid
;
1445 sndinfo
->snd_flags
= sinfo
->snd_flags
;
1446 sndinfo
->snd_ppid
= sinfo
->snd_ppid
;
1447 sndinfo
->snd_context
= sinfo
->snd_context
;
1448 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndinfo
));
1451 (struct cmsghdr
*)&cmsgbuf
[CMSG_SPACE(sizeof(struct sctp_sndinfo
))];
1452 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1453 cmsg
->cmsg_type
= SCTP_PRINFO
;
1454 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_prinfo
));
1455 prinfo
= (struct sctp_prinfo
*)CMSG_DATA(cmsg
);
1456 memset(prinfo
, 0, sizeof(struct sctp_prinfo
));
1457 prinfo
->pr_policy
= pinfo
->pr_policy
;
1458 prinfo
->pr_value
= pinfo
->pr_value
;
1459 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_prinfo
));
1461 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1462 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1463 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1464 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1465 sndrcvinfo
= (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
1466 memset(sndrcvinfo
, 0, sizeof(struct sctp_sndrcvinfo
));
1467 sndrcvinfo
->sinfo_stream
= sinfo
->snd_sid
;
1468 sndrcvinfo
->sinfo_flags
= sinfo
->snd_flags
;
1470 sndrcvinfo
->sinfo_flags
|= pinfo
->pr_policy
;
1472 sndrcvinfo
->sinfo_ppid
= sinfo
->snd_ppid
;
1473 sndrcvinfo
->sinfo_context
= sinfo
->snd_context
;
1474 sndrcvinfo
->sinfo_timetolive
= pinfo
->pr_value
;
1475 msg
.msg_controllen
+= CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
));
1478 ret
= sendmsg(b
->num
, &msg
, 0);
1480 BIO_clear_retry_flags(b
);
1482 if (BIO_dgram_should_retry(ret
)) {
1483 BIO_set_retry_write(b
);
1484 data
->_errno
= get_last_socket_error();
1490 static long dgram_sctp_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
1493 bio_dgram_sctp_data
*data
= NULL
;
1494 socklen_t sockopt_len
= 0;
1495 struct sctp_authkeyid authkeyid
;
1496 struct sctp_authkey
*authkey
= NULL
;
1498 data
= (bio_dgram_sctp_data
*) b
->ptr
;
1501 case BIO_CTRL_DGRAM_QUERY_MTU
:
1503 * Set to maximum (2^14) and ignore user input to enable transport
1504 * protocol fragmentation. Returns always 2^14.
1509 case BIO_CTRL_DGRAM_SET_MTU
:
1511 * Set to maximum (2^14) and ignore input to enable transport
1512 * protocol fragmentation. Returns always 2^14.
1517 case BIO_CTRL_DGRAM_SET_CONNECTED
:
1518 case BIO_CTRL_DGRAM_CONNECT
:
1519 /* Returns always -1. */
1522 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT
:
1524 * SCTP doesn't need the DTLS timer Returns always 1.
1527 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD
:
1529 * We allow transport protocol fragmentation so this is irrelevant
1533 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
:
1535 data
->in_handshake
= 1;
1537 data
->in_handshake
= 0;
1540 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_NODELAY
,
1541 &data
->in_handshake
, sizeof(int));
1543 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
:
1545 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1548 /* Get active key */
1549 sockopt_len
= sizeof(struct sctp_authkeyid
);
1551 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1557 sockopt_len
= sizeof(struct sctp_authkey
) + 64 * sizeof(uint8_t);
1558 authkey
= OPENSSL_malloc(sockopt_len
);
1559 if (authkey
== NULL
) {
1563 memset(authkey
, 0x00, sockopt_len
);
1564 authkey
->sca_keynumber
= authkeyid
.scact_keynumber
+ 1;
1565 # ifndef __FreeBSD__
1567 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1568 * and higher work without it.
1570 authkey
->sca_keylength
= 64;
1572 memcpy(&authkey
->sca_key
[0], ptr
, 64 * sizeof(uint8_t));
1575 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_KEY
, authkey
,
1577 OPENSSL_free(authkey
);
1582 /* Reset active key */
1583 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1584 &authkeyid
, sizeof(struct sctp_authkeyid
));
1589 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
:
1590 /* Returns 0 on success, -1 otherwise. */
1592 /* Get active key */
1593 sockopt_len
= sizeof(struct sctp_authkeyid
);
1595 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
, &authkeyid
,
1600 /* Set active key */
1601 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
+ 1;
1602 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1603 &authkeyid
, sizeof(struct sctp_authkeyid
));
1608 * CCS has been sent, so remember that and fall through to check if
1609 * we need to deactivate an old key
1613 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
:
1614 /* Returns 0 on success, -1 otherwise. */
1617 * Has this command really been called or is this just a
1620 if (cmd
== BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
)
1624 * CSS has been both, received and sent, so deactivate an old key
1626 if (data
->ccs_rcvd
== 1 && data
->ccs_sent
== 1) {
1627 /* Get active key */
1628 sockopt_len
= sizeof(struct sctp_authkeyid
);
1630 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_ACTIVE_KEY
,
1631 &authkeyid
, &sockopt_len
);
1636 * Deactivate key or delete second last key if
1637 * SCTP_AUTHENTICATION_EVENT is not available.
1639 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1640 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1641 sockopt_len
= sizeof(struct sctp_authkeyid
);
1642 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DEACTIVATE_KEY
,
1643 &authkeyid
, sockopt_len
);
1647 # ifndef SCTP_AUTHENTICATION_EVENT
1648 if (authkeyid
.scact_keynumber
> 0) {
1649 authkeyid
.scact_keynumber
= authkeyid
.scact_keynumber
- 1;
1650 ret
= setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_AUTH_DELETE_KEY
,
1651 &authkeyid
, sizeof(struct sctp_authkeyid
));
1661 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO
:
1662 /* Returns the size of the copied struct. */
1663 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1664 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1666 memcpy(ptr
, &(data
->sndinfo
), num
);
1669 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO
:
1670 /* Returns the size of the copied struct. */
1671 if (num
> (long)sizeof(struct bio_dgram_sctp_sndinfo
))
1672 num
= sizeof(struct bio_dgram_sctp_sndinfo
);
1674 memcpy(&(data
->sndinfo
), ptr
, num
);
1676 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO
:
1677 /* Returns the size of the copied struct. */
1678 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1679 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1681 memcpy(ptr
, &data
->rcvinfo
, num
);
1685 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO
:
1686 /* Returns the size of the copied struct. */
1687 if (num
> (long)sizeof(struct bio_dgram_sctp_rcvinfo
))
1688 num
= sizeof(struct bio_dgram_sctp_rcvinfo
);
1690 memcpy(&(data
->rcvinfo
), ptr
, num
);
1692 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO
:
1693 /* Returns the size of the copied struct. */
1694 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1695 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1697 memcpy(ptr
, &(data
->prinfo
), num
);
1700 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO
:
1701 /* Returns the size of the copied struct. */
1702 if (num
> (long)sizeof(struct bio_dgram_sctp_prinfo
))
1703 num
= sizeof(struct bio_dgram_sctp_prinfo
);
1705 memcpy(&(data
->prinfo
), ptr
, num
);
1707 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN
:
1708 /* Returns always 1. */
1710 data
->save_shutdown
= 1;
1712 data
->save_shutdown
= 0;
1717 * Pass to default ctrl function to process SCTP unspecific commands
1719 ret
= dgram_ctrl(b
, cmd
, num
, ptr
);
1725 int BIO_dgram_sctp_notification_cb(BIO
*b
,
1726 void (*handle_notifications
) (BIO
*bio
,
1732 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1734 if (handle_notifications
!= NULL
) {
1735 data
->handle_notifications
= handle_notifications
;
1736 data
->notification_context
= context
;
1743 int BIO_dgram_sctp_wait_for_dry(BIO
*b
)
1746 int n
, sockflags
, ret
;
1747 union sctp_notification snp
;
1751 struct sctp_event event
;
1753 struct sctp_event_subscribe event
;
1754 socklen_t eventsize
;
1756 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1758 /* set sender dry event */
1760 memset(&event
, 0, sizeof(struct sctp_event
));
1761 event
.se_assoc_id
= 0;
1762 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1765 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1766 sizeof(struct sctp_event
));
1768 eventsize
= sizeof(struct sctp_event_subscribe
);
1769 ret
= getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
, &eventsize
);
1773 event
.sctp_sender_dry_event
= 1;
1776 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1777 sizeof(struct sctp_event_subscribe
));
1782 /* peek for notification */
1783 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1784 iov
.iov_base
= (char *)&snp
;
1785 iov
.iov_len
= sizeof(union sctp_notification
);
1786 msg
.msg_name
= NULL
;
1787 msg
.msg_namelen
= 0;
1790 msg
.msg_control
= NULL
;
1791 msg
.msg_controllen
= 0;
1794 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1796 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1797 && (get_last_socket_error() != EWOULDBLOCK
))
1803 /* if we find a notification, process it and try again if necessary */
1804 while (msg
.msg_flags
& MSG_NOTIFICATION
) {
1805 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1806 iov
.iov_base
= (char *)&snp
;
1807 iov
.iov_len
= sizeof(union sctp_notification
);
1808 msg
.msg_name
= NULL
;
1809 msg
.msg_namelen
= 0;
1812 msg
.msg_control
= NULL
;
1813 msg
.msg_controllen
= 0;
1816 n
= recvmsg(b
->num
, &msg
, 0);
1818 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1819 && (get_last_socket_error() != EWOULDBLOCK
))
1825 if (snp
.sn_header
.sn_type
== SCTP_SENDER_DRY_EVENT
) {
1828 /* disable sender dry event */
1830 memset(&event
, 0, sizeof(struct sctp_event
));
1831 event
.se_assoc_id
= 0;
1832 event
.se_type
= SCTP_SENDER_DRY_EVENT
;
1835 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENT
, &event
,
1836 sizeof(struct sctp_event
));
1838 eventsize
= (socklen_t
) sizeof(struct sctp_event_subscribe
);
1840 getsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1845 event
.sctp_sender_dry_event
= 0;
1848 setsockopt(b
->num
, IPPROTO_SCTP
, SCTP_EVENTS
, &event
,
1849 sizeof(struct sctp_event_subscribe
));
1854 # ifdef SCTP_AUTHENTICATION_EVENT
1855 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1856 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1859 if (data
->handle_notifications
!= NULL
)
1860 data
->handle_notifications(b
, data
->notification_context
,
1863 /* found notification, peek again */
1864 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1865 iov
.iov_base
= (char *)&snp
;
1866 iov
.iov_len
= sizeof(union sctp_notification
);
1867 msg
.msg_name
= NULL
;
1868 msg
.msg_namelen
= 0;
1871 msg
.msg_control
= NULL
;
1872 msg
.msg_controllen
= 0;
1875 /* if we have seen the dry already, don't wait */
1877 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1878 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1881 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1884 fcntl(b
->num
, F_SETFL
, sockflags
);
1888 if ((n
< 0) && (get_last_socket_error() != EAGAIN
)
1889 && (get_last_socket_error() != EWOULDBLOCK
))
1896 /* read anything else */
1900 int BIO_dgram_sctp_msg_waiting(BIO
*b
)
1903 union sctp_notification snp
;
1906 bio_dgram_sctp_data
*data
= (bio_dgram_sctp_data
*) b
->ptr
;
1908 /* Check if there are any messages waiting to be read */
1910 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1911 iov
.iov_base
= (char *)&snp
;
1912 iov
.iov_len
= sizeof(union sctp_notification
);
1913 msg
.msg_name
= NULL
;
1914 msg
.msg_namelen
= 0;
1917 msg
.msg_control
= NULL
;
1918 msg
.msg_controllen
= 0;
1921 sockflags
= fcntl(b
->num
, F_GETFL
, 0);
1922 fcntl(b
->num
, F_SETFL
, O_NONBLOCK
);
1923 n
= recvmsg(b
->num
, &msg
, MSG_PEEK
);
1924 fcntl(b
->num
, F_SETFL
, sockflags
);
1926 /* if notification, process and try again */
1927 if (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
)) {
1928 # ifdef SCTP_AUTHENTICATION_EVENT
1929 if (snp
.sn_header
.sn_type
== SCTP_AUTHENTICATION_EVENT
)
1930 dgram_sctp_handle_auth_free_key_event(b
, &snp
);
1933 memset(&snp
, 0x00, sizeof(union sctp_notification
));
1934 iov
.iov_base
= (char *)&snp
;
1935 iov
.iov_len
= sizeof(union sctp_notification
);
1936 msg
.msg_name
= NULL
;
1937 msg
.msg_namelen
= 0;
1940 msg
.msg_control
= NULL
;
1941 msg
.msg_controllen
= 0;
1943 n
= recvmsg(b
->num
, &msg
, 0);
1945 if (data
->handle_notifications
!= NULL
)
1946 data
->handle_notifications(b
, data
->notification_context
,
1950 } while (n
> 0 && (msg
.msg_flags
& MSG_NOTIFICATION
));
1952 /* Return 1 if there is a message to be read, return 0 otherwise. */
1959 static int dgram_sctp_puts(BIO
*bp
, const char *str
)
1964 ret
= dgram_sctp_write(bp
, str
, n
);
1969 static int BIO_dgram_should_retry(int i
)
1973 if ((i
== 0) || (i
== -1)) {
1974 err
= get_last_socket_error();
1976 # if defined(OPENSSL_SYS_WINDOWS)
1978 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1979 * this point, the error code was overwritten by another system call
1980 * before this error handling is called.
1984 return (BIO_dgram_non_fatal_error(err
));
1989 int BIO_dgram_non_fatal_error(int err
)
1992 # if defined(OPENSSL_SYS_WINDOWS)
1993 # if defined(WSAEWOULDBLOCK)
1994 case WSAEWOULDBLOCK
:
1997 # if 0 /* This appears to always be an error */
1998 # if defined(WSAENOTCONN)
2005 # ifdef WSAEWOULDBLOCK
2006 # if WSAEWOULDBLOCK != EWOULDBLOCK
2019 # if EWOULDBLOCK != EAGAIN
2044 static void get_current_time(struct timeval
*t
)
2046 # if defined(_WIN32)
2049 unsigned __int64 ul
;
2054 SystemTimeToFileTime(&st
, &now
.ft
);
2056 now
.ul
-= 116444736000000000ULL;
2058 now
.ul
-= 116444736000000000U I64
; /* re-bias to 1/1/1970 */
2060 t
->tv_sec
= (long)(now
.ul
/ 10000000);
2061 t
->tv_usec
= ((int)(now
.ul
% 10000000)) / 10;
2062 # elif defined(OPENSSL_SYS_VMS)
2065 t
->tv_sec
= (long)tb
.time
;
2066 t
->tv_usec
= (long)tb
.millitm
* 1000;
2068 gettimeofday(t
, NULL
);