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;
172 unsigned int peekmode;
175 # ifndef OPENSSL_NO_SCTP
176 typedef struct bio_dgram_sctp_save_message_st {
180 } bio_dgram_sctp_save_message;
182 typedef struct bio_dgram_sctp_data_st {
185 struct sockaddr_in sa_in;
186 # if OPENSSL_USE_IPV6
187 struct sockaddr_in6 sa_in6;
190 unsigned int connected;
193 struct bio_dgram_sctp_sndinfo sndinfo;
194 struct bio_dgram_sctp_rcvinfo rcvinfo;
195 struct bio_dgram_sctp_prinfo prinfo;
196 void (*handle_notifications) (BIO *bio, void *context, void *buf);
197 void *notification_context;
202 int peer_auth_tested;
203 bio_dgram_sctp_save_message saved_message;
204 } bio_dgram_sctp_data;
207 BIO_METHOD *BIO_s_datagram(void)
209 return (&methods_dgramp);
212 BIO *BIO_new_dgram(int fd, int close_flag)
216 ret = BIO_new(BIO_s_datagram());
219 BIO_set_fd(ret, fd, close_flag);
223 static int dgram_new(BIO *bi)
225 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
233 static int dgram_free(BIO *a)
235 bio_dgram_data *data;
242 data = (bio_dgram_data *)a->ptr;
248 static int dgram_clear(BIO *a)
262 static void dgram_adjust_rcv_timeout(BIO *b)
264 # if defined(SO_RCVTIMEO)
265 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
273 /* Is a timer active? */
274 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
275 struct timeval timenow, timeleft;
277 /* Read current socket timeout */
278 # ifdef OPENSSL_SYS_WINDOWS
281 sz.i = sizeof(timeout);
282 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
283 (void *)&timeout, &sz.i) < 0) {
284 perror("getsockopt");
286 data->socket_timeout.tv_sec = timeout / 1000;
287 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
290 sz.i = sizeof(data->socket_timeout);
291 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
292 &(data->socket_timeout), (void *)&sz) < 0) {
293 perror("getsockopt");
294 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
295 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
298 /* Get current time */
299 get_current_time(&timenow);
301 /* Calculate time left until timer expires */
302 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
303 if (timeleft.tv_usec < timenow.tv_usec) {
304 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
307 timeleft.tv_usec -= timenow.tv_usec;
309 if (timeleft.tv_sec < timenow.tv_sec) {
311 timeleft.tv_usec = 1;
313 timeleft.tv_sec -= timenow.tv_sec;
317 * Adjust socket timeout if next handhake message timer will expire
320 if ((data->socket_timeout.tv_sec == 0
321 && data->socket_timeout.tv_usec == 0)
322 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
323 || (data->socket_timeout.tv_sec == timeleft.tv_sec
324 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
325 # ifdef OPENSSL_SYS_WINDOWS
326 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
327 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
328 (void *)&timeout, sizeof(timeout)) < 0) {
329 perror("setsockopt");
332 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
333 sizeof(struct timeval)) < 0) {
334 perror("setsockopt");
342 static void dgram_reset_rcv_timeout(BIO *b)
344 # if defined(SO_RCVTIMEO)
345 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
347 /* Is a timer active? */
348 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
349 # ifdef OPENSSL_SYS_WINDOWS
350 int timeout = data->socket_timeout.tv_sec * 1000 +
351 data->socket_timeout.tv_usec / 1000;
352 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
353 (void *)&timeout, sizeof(timeout)) < 0) {
354 perror("setsockopt");
358 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
359 sizeof(struct timeval)) < 0) {
360 perror("setsockopt");
367 static int dgram_read(BIO *b, char *out, int outl)
370 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
375 * See commentary in b_sock.c. <appro>
383 struct sockaddr_in sa_in;
384 # if OPENSSL_USE_IPV6
385 struct sockaddr_in6 sa_in6;
391 sa.len.i = sizeof(sa.peer);
394 clear_socket_error();
395 memset(&sa.peer, 0, sizeof(sa.peer));
396 dgram_adjust_rcv_timeout(b);
399 ret = recvfrom(b->num, out, outl, flags, &sa.peer.sa, (void *)&sa.len);
400 if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
401 OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
402 sa.len.i = (int)sa.len.s;
405 if (!data->connected && ret >= 0)
406 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
408 BIO_clear_retry_flags(b);
410 if (BIO_dgram_should_retry(ret)) {
411 BIO_set_retry_read(b);
412 data->_errno = get_last_socket_error();
416 dgram_reset_rcv_timeout(b);
421 static int dgram_write(BIO *b, const char *in, int inl)
424 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
425 clear_socket_error();
428 ret = writesocket(b->num, in, inl);
430 int peerlen = sizeof(data->peer);
432 if (data->peer.sa.sa_family == AF_INET)
433 peerlen = sizeof(data->peer.sa_in);
434 # if OPENSSL_USE_IPV6
435 else if (data->peer.sa.sa_family == AF_INET6)
436 peerlen = sizeof(data->peer.sa_in6);
438 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
439 ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
441 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
445 BIO_clear_retry_flags(b);
447 if (BIO_dgram_should_retry(ret)) {
448 BIO_set_retry_write(b);
449 data->_errno = get_last_socket_error();
455 static long dgram_get_mtu_overhead(bio_dgram_data *data)
459 switch (data->peer.sa.sa_family) {
462 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
466 # if OPENSSL_USE_IPV6
468 # ifdef IN6_IS_ADDR_V4MAPPED
469 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
471 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
477 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
483 /* We don't know. Go with the historical default */
490 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
494 struct sockaddr *to = NULL;
495 bio_dgram_data *data = NULL;
497 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
498 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
499 * modern enough to define socklen_t */
503 struct sockaddr_in s4;
504 # if OPENSSL_USE_IPV6
505 struct sockaddr_in6 s6;
510 data = (bio_dgram_data *)b->ptr;
515 case BIO_C_FILE_SEEK:
518 case BIO_C_FILE_TELL:
524 b->num = *((int *)ptr);
525 b->shutdown = (int)num;
537 case BIO_CTRL_GET_CLOSE:
540 case BIO_CTRL_SET_CLOSE:
541 b->shutdown = (int)num;
543 case BIO_CTRL_PENDING:
544 case BIO_CTRL_WPENDING:
551 case BIO_CTRL_DGRAM_CONNECT:
552 to = (struct sockaddr *)ptr;
553 switch (to->sa_family) {
555 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
557 # if OPENSSL_USE_IPV6
559 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
563 memcpy(&data->peer, to, sizeof(data->peer.sa));
567 /* (Linux)kernel sets DF bit on outgoing IP packets */
568 case BIO_CTRL_DGRAM_MTU_DISCOVER:
569 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
570 addr_len = (socklen_t) sizeof(addr);
571 memset(&addr, 0, sizeof(addr));
572 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
576 switch (addr.sa.sa_family) {
578 sockopt_val = IP_PMTUDISC_DO;
579 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
580 &sockopt_val, sizeof(sockopt_val))) < 0)
581 perror("setsockopt");
583 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
585 sockopt_val = IPV6_PMTUDISC_DO;
586 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
587 &sockopt_val, sizeof(sockopt_val))) < 0)
588 perror("setsockopt");
599 case BIO_CTRL_DGRAM_QUERY_MTU:
600 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
601 addr_len = (socklen_t) sizeof(addr);
602 memset(&addr, 0, sizeof(addr));
603 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
607 sockopt_len = sizeof(sockopt_val);
608 switch (addr.sa.sa_family) {
611 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
612 &sockopt_len)) < 0 || sockopt_val < 0) {
616 * we assume that the transport protocol is UDP and no IP
619 data->mtu = sockopt_val - 8 - 20;
623 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
626 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
627 (void *)&sockopt_val, &sockopt_len)) < 0
628 || sockopt_val < 0) {
632 * we assume that the transport protocol is UDP and no IPV6
635 data->mtu = sockopt_val - 8 - 40;
648 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
649 ret = -dgram_get_mtu_overhead(data);
650 switch (data->peer.sa.sa_family) {
654 # if OPENSSL_USE_IPV6
656 # ifdef IN6_IS_ADDR_V4MAPPED
657 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
669 case BIO_CTRL_DGRAM_GET_MTU:
671 case BIO_CTRL_DGRAM_SET_MTU:
675 case BIO_CTRL_DGRAM_SET_CONNECTED:
676 to = (struct sockaddr *)ptr;
680 switch (to->sa_family) {
682 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
684 # if OPENSSL_USE_IPV6
686 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
690 memcpy(&data->peer, to, sizeof(data->peer.sa));
695 memset(&data->peer, 0, sizeof(data->peer));
698 case BIO_CTRL_DGRAM_GET_PEER:
699 switch (data->peer.sa.sa_family) {
701 ret = sizeof(data->peer.sa_in);
703 # if OPENSSL_USE_IPV6
705 ret = sizeof(data->peer.sa_in6);
709 ret = sizeof(data->peer.sa);
712 if (num == 0 || num > ret)
714 memcpy(ptr, &data->peer, (ret = num));
716 case BIO_CTRL_DGRAM_SET_PEER:
717 to = (struct sockaddr *)ptr;
718 switch (to->sa_family) {
720 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
722 # if OPENSSL_USE_IPV6
724 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
728 memcpy(&data->peer, to, sizeof(data->peer.sa));
732 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
733 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
735 # if defined(SO_RCVTIMEO)
736 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
737 # ifdef OPENSSL_SYS_WINDOWS
739 struct timeval *tv = (struct timeval *)ptr;
740 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
741 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
742 (void *)&timeout, sizeof(timeout)) < 0) {
743 perror("setsockopt");
748 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
749 sizeof(struct timeval)) < 0) {
750 perror("setsockopt");
755 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
763 # ifdef OPENSSL_SYS_WINDOWS
765 struct timeval *tv = (struct timeval *)ptr;
767 sz.i = sizeof(timeout);
768 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
769 (void *)&timeout, &sz.i) < 0) {
770 perror("getsockopt");
773 tv->tv_sec = timeout / 1000;
774 tv->tv_usec = (timeout % 1000) * 1000;
778 sz.i = sizeof(struct timeval);
779 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
780 ptr, (void *)&sz) < 0) {
781 perror("getsockopt");
783 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
784 OPENSSL_assert(sz.s <= sizeof(struct timeval));
792 # if defined(SO_SNDTIMEO)
793 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
794 # ifdef OPENSSL_SYS_WINDOWS
796 struct timeval *tv = (struct timeval *)ptr;
797 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
798 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
799 (void *)&timeout, sizeof(timeout)) < 0) {
800 perror("setsockopt");
805 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
806 sizeof(struct timeval)) < 0) {
807 perror("setsockopt");
812 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
820 # ifdef OPENSSL_SYS_WINDOWS
822 struct timeval *tv = (struct timeval *)ptr;
824 sz.i = sizeof(timeout);
825 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
826 (void *)&timeout, &sz.i) < 0) {
827 perror("getsockopt");
830 tv->tv_sec = timeout / 1000;
831 tv->tv_usec = (timeout % 1000) * 1000;
835 sz.i = sizeof(struct timeval);
836 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
837 ptr, (void *)&sz) < 0) {
838 perror("getsockopt");
840 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
841 OPENSSL_assert(sz.s <= sizeof(struct timeval));
849 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
851 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
852 # ifdef OPENSSL_SYS_WINDOWS
853 if (data->_errno == WSAETIMEDOUT)
855 if (data->_errno == EAGAIN)
864 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
865 if (data->_errno == EMSGSIZE) {
872 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
873 sockopt_val = num ? 1 : 0;
875 switch (data->peer.sa.sa_family) {
877 # if defined(IP_DONTFRAG)
878 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
879 &sockopt_val, sizeof(sockopt_val))) < 0) {
880 perror("setsockopt");
883 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
884 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
885 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
886 &sockopt_val, sizeof(sockopt_val))) < 0) {
887 perror("setsockopt");
890 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
891 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
892 (const char *)&sockopt_val,
893 sizeof(sockopt_val))) < 0) {
894 perror("setsockopt");
901 # if OPENSSL_USE_IPV6
903 # if defined(IPV6_DONTFRAG)
904 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
905 (const void *)&sockopt_val,
906 sizeof(sockopt_val))) < 0) {
907 perror("setsockopt");
910 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
911 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
912 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
913 &sockopt_val, sizeof(sockopt_val))) < 0) {
914 perror("setsockopt");
927 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
928 ret = dgram_get_mtu_overhead(data);
930 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
931 data->peekmode = (unsigned int)num;
940 static int dgram_puts(BIO *bp, const char *str)
945 ret = dgram_write(bp, str, n);
949 # ifndef OPENSSL_NO_SCTP
950 BIO_METHOD *BIO_s_datagram_sctp(void)
952 return (&methods_dgramp_sctp);
955 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
958 int ret, optval = 20000;
959 int auth_data = 0, auth_forward = 0;
961 struct sctp_authchunk auth;
962 struct sctp_authchunks *authchunks;
963 socklen_t sockopt_len;
964 # ifdef SCTP_AUTHENTICATION_EVENT
966 struct sctp_event event;
968 struct sctp_event_subscribe event;
972 bio = BIO_new(BIO_s_datagram_sctp());
975 BIO_set_fd(bio, fd, close_flag);
977 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
978 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
980 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
981 sizeof(struct sctp_authchunk));
986 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
988 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
989 sizeof(struct sctp_authchunk));
996 * Test if activation was successful. When using accept(), SCTP-AUTH has
997 * to be activated for the listening socket already, otherwise the
998 * connected socket won't use it.
1000 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1001 authchunks = OPENSSL_zalloc(sockopt_len);
1006 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1009 OPENSSL_free(authchunks);
1014 for (p = (unsigned char *)authchunks->gauth_chunks;
1015 p < (unsigned char *)authchunks + sockopt_len;
1016 p += sizeof(uint8_t)) {
1017 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1019 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1023 OPENSSL_free(authchunks);
1025 OPENSSL_assert(auth_data);
1026 OPENSSL_assert(auth_forward);
1028 # ifdef SCTP_AUTHENTICATION_EVENT
1030 memset(&event, 0, sizeof(event));
1031 event.se_assoc_id = 0;
1032 event.se_type = SCTP_AUTHENTICATION_EVENT;
1035 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1036 sizeof(struct sctp_event));
1042 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1043 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1049 event.sctp_authentication_event = 1;
1052 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1053 sizeof(struct sctp_event_subscribe));
1062 * Disable partial delivery by setting the min size larger than the max
1063 * record size of 2^14 + 2048 + 13
1066 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1076 int BIO_dgram_is_sctp(BIO *bio)
1078 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1081 static int dgram_sctp_new(BIO *bi)
1083 bio_dgram_sctp_data *data = NULL;
1087 data = OPENSSL_zalloc(sizeof(*data));
1090 # ifdef SCTP_PR_SCTP_NONE
1091 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1099 static int dgram_sctp_free(BIO *a)
1101 bio_dgram_sctp_data *data;
1105 if (!dgram_clear(a))
1108 data = (bio_dgram_sctp_data *) a->ptr;
1110 OPENSSL_free(data->saved_message.data);
1117 # ifdef SCTP_AUTHENTICATION_EVENT
1118 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1119 union sctp_notification *snp)
1122 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1124 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1125 struct sctp_authkeyid authkeyid;
1128 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1129 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1130 &authkeyid, sizeof(struct sctp_authkeyid));
1135 static int dgram_sctp_read(BIO *b, char *out, int outl)
1137 int ret = 0, n = 0, i, optval;
1139 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1140 union sctp_notification *snp;
1143 struct cmsghdr *cmsg;
1147 clear_socket_error();
1150 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1153 msg.msg_name = NULL;
1154 msg.msg_namelen = 0;
1157 msg.msg_control = cmsgbuf;
1158 msg.msg_controllen = 512;
1160 n = recvmsg(b->num, &msg, 0);
1168 if (msg.msg_controllen > 0) {
1169 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1170 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1171 if (cmsg->cmsg_level != IPPROTO_SCTP)
1173 # ifdef SCTP_RCVINFO
1174 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1175 struct sctp_rcvinfo *rcvinfo;
1177 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1178 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1179 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1180 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1181 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1182 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1183 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1184 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1188 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1189 struct sctp_sndrcvinfo *sndrcvinfo;
1192 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1193 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1194 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1195 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1196 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1197 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1198 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1199 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1205 if (msg.msg_flags & MSG_NOTIFICATION) {
1206 snp = (union sctp_notification *)out;
1207 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1209 struct sctp_event event;
1211 struct sctp_event_subscribe event;
1212 socklen_t eventsize;
1215 * If a message has been delayed until the socket is dry,
1216 * it can be sent now.
1218 if (data->saved_message.length > 0) {
1219 i = dgram_sctp_write(data->saved_message.bio,
1220 data->saved_message.data,
1221 data->saved_message.length);
1226 OPENSSL_free(data->saved_message.data);
1227 data->saved_message.data = NULL;
1228 data->saved_message.length = 0;
1231 /* disable sender dry event */
1233 memset(&event, 0, sizeof(event));
1234 event.se_assoc_id = 0;
1235 event.se_type = SCTP_SENDER_DRY_EVENT;
1237 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1238 sizeof(struct sctp_event));
1244 eventsize = sizeof(struct sctp_event_subscribe);
1245 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1252 event.sctp_sender_dry_event = 0;
1254 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1255 sizeof(struct sctp_event_subscribe));
1262 # ifdef SCTP_AUTHENTICATION_EVENT
1263 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1264 dgram_sctp_handle_auth_free_key_event(b, snp);
1267 if (data->handle_notifications != NULL)
1268 data->handle_notifications(b, data->notification_context,
1271 memset(out, 0, outl);
1275 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1278 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1279 /* Partial message read, this should never happen! */
1282 * The buffer was too small, this means the peer sent a message
1283 * that was larger than allowed.
1289 * Test if socket buffer can handle max record size (2^14 + 2048
1292 optlen = (socklen_t) sizeof(int);
1293 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1295 OPENSSL_assert(optval >= 18445);
1298 * Test if SCTP doesn't partially deliver below max record size
1299 * (2^14 + 2048 + 13)
1301 optlen = (socklen_t) sizeof(int);
1303 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1306 OPENSSL_assert(optval >= 18445);
1309 * Partially delivered notification??? Probably a bug....
1311 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1314 * Everything seems ok till now, so it's most likely a message
1315 * dropped by PR-SCTP.
1317 memset(out, 0, outl);
1318 BIO_set_retry_read(b);
1322 BIO_clear_retry_flags(b);
1324 if (BIO_dgram_should_retry(ret)) {
1325 BIO_set_retry_read(b);
1326 data->_errno = get_last_socket_error();
1330 /* Test if peer uses SCTP-AUTH before continuing */
1331 if (!data->peer_auth_tested) {
1332 int ii, auth_data = 0, auth_forward = 0;
1334 struct sctp_authchunks *authchunks;
1337 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1338 authchunks = OPENSSL_malloc(optlen);
1340 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1343 memset(authchunks, 0, optlen);
1344 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1345 authchunks, &optlen);
1348 for (p = (unsigned char *)authchunks->gauth_chunks;
1349 p < (unsigned char *)authchunks + optlen;
1350 p += sizeof(uint8_t)) {
1351 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1353 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1357 OPENSSL_free(authchunks);
1359 if (!auth_data || !auth_forward) {
1360 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1364 data->peer_auth_tested = 1;
1371 * dgram_sctp_write - send message on SCTP socket
1372 * @b: BIO to write to
1374 * @inl: amount of bytes in @in to send
1376 * Returns -1 on error or the sent amount of bytes on success
1378 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1381 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1382 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1383 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1384 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1385 struct iovec iov[1];
1387 struct cmsghdr *cmsg;
1388 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1389 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1390 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1391 struct sctp_sndinfo *sndinfo;
1392 struct sctp_prinfo *prinfo;
1394 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1395 struct sctp_sndrcvinfo *sndrcvinfo;
1398 clear_socket_error();
1401 * If we're send anything else than application data, disable all user
1402 * parameters and flags.
1405 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1406 # ifdef SCTP_SACK_IMMEDIATELY
1407 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1409 sinfo = &handshake_sinfo;
1413 * If we have to send a shutdown alert message and the socket is not dry
1414 * yet, we have to save it and send it as soon as the socket gets dry.
1416 if (data->save_shutdown) {
1417 ret = BIO_dgram_sctp_wait_for_dry(b);
1423 data->saved_message.bio = b;
1424 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1425 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1428 OPENSSL_free(data->saved_message.data);
1429 data->saved_message.data = tmp;
1430 memcpy(data->saved_message.data, in, inl);
1431 data->saved_message.length = inl;
1436 iov[0].iov_base = (char *)in;
1437 iov[0].iov_len = inl;
1438 msg.msg_name = NULL;
1439 msg.msg_namelen = 0;
1442 msg.msg_control = (caddr_t) cmsgbuf;
1443 msg.msg_controllen = 0;
1445 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1446 cmsg = (struct cmsghdr *)cmsgbuf;
1447 cmsg->cmsg_level = IPPROTO_SCTP;
1448 cmsg->cmsg_type = SCTP_SNDINFO;
1449 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1450 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1451 memset(sndinfo, 0, sizeof(*sndinfo));
1452 sndinfo->snd_sid = sinfo->snd_sid;
1453 sndinfo->snd_flags = sinfo->snd_flags;
1454 sndinfo->snd_ppid = sinfo->snd_ppid;
1455 sndinfo->snd_context = sinfo->snd_context;
1456 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1459 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1460 cmsg->cmsg_level = IPPROTO_SCTP;
1461 cmsg->cmsg_type = SCTP_PRINFO;
1462 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1463 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1464 memset(prinfo, 0, sizeof(*prinfo));
1465 prinfo->pr_policy = pinfo->pr_policy;
1466 prinfo->pr_value = pinfo->pr_value;
1467 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1469 cmsg = (struct cmsghdr *)cmsgbuf;
1470 cmsg->cmsg_level = IPPROTO_SCTP;
1471 cmsg->cmsg_type = SCTP_SNDRCV;
1472 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1473 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1474 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1475 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1476 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1478 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1480 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1481 sndrcvinfo->sinfo_context = sinfo->snd_context;
1482 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1483 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1486 ret = sendmsg(b->num, &msg, 0);
1488 BIO_clear_retry_flags(b);
1490 if (BIO_dgram_should_retry(ret)) {
1491 BIO_set_retry_write(b);
1492 data->_errno = get_last_socket_error();
1498 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1501 bio_dgram_sctp_data *data = NULL;
1502 socklen_t sockopt_len = 0;
1503 struct sctp_authkeyid authkeyid;
1504 struct sctp_authkey *authkey = NULL;
1506 data = (bio_dgram_sctp_data *) b->ptr;
1509 case BIO_CTRL_DGRAM_QUERY_MTU:
1511 * Set to maximum (2^14) and ignore user input to enable transport
1512 * protocol fragmentation. Returns always 2^14.
1517 case BIO_CTRL_DGRAM_SET_MTU:
1519 * Set to maximum (2^14) and ignore input to enable transport
1520 * protocol fragmentation. Returns always 2^14.
1525 case BIO_CTRL_DGRAM_SET_CONNECTED:
1526 case BIO_CTRL_DGRAM_CONNECT:
1527 /* Returns always -1. */
1530 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1532 * SCTP doesn't need the DTLS timer Returns always 1.
1535 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1537 * We allow transport protocol fragmentation so this is irrelevant
1541 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1543 data->in_handshake = 1;
1545 data->in_handshake = 0;
1548 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1549 &data->in_handshake, sizeof(int));
1551 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1553 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1556 /* Get active key */
1557 sockopt_len = sizeof(struct sctp_authkeyid);
1559 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1565 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1566 authkey = OPENSSL_malloc(sockopt_len);
1567 if (authkey == NULL) {
1571 memset(authkey, 0, sockopt_len);
1572 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1573 # ifndef __FreeBSD__
1575 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1576 * and higher work without it.
1578 authkey->sca_keylength = 64;
1580 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1583 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1585 OPENSSL_free(authkey);
1590 /* Reset active key */
1591 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1592 &authkeyid, sizeof(struct sctp_authkeyid));
1597 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1598 /* Returns 0 on success, -1 otherwise. */
1600 /* Get active key */
1601 sockopt_len = sizeof(struct sctp_authkeyid);
1603 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1608 /* Set active key */
1609 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1610 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1611 &authkeyid, sizeof(struct sctp_authkeyid));
1616 * CCS has been sent, so remember that and fall through to check if
1617 * we need to deactivate an old key
1621 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1622 /* Returns 0 on success, -1 otherwise. */
1625 * Has this command really been called or is this just a
1628 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1632 * CSS has been both, received and sent, so deactivate an old key
1634 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1635 /* Get active key */
1636 sockopt_len = sizeof(struct sctp_authkeyid);
1638 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1639 &authkeyid, &sockopt_len);
1644 * Deactivate key or delete second last key if
1645 * SCTP_AUTHENTICATION_EVENT is not available.
1647 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1648 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1649 sockopt_len = sizeof(struct sctp_authkeyid);
1650 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1651 &authkeyid, sockopt_len);
1655 # ifndef SCTP_AUTHENTICATION_EVENT
1656 if (authkeyid.scact_keynumber > 0) {
1657 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1658 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1659 &authkeyid, sizeof(struct sctp_authkeyid));
1669 case BIO_CTRL_DGRAM_SCTP_GET_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(ptr, &(data->sndinfo), num);
1677 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1678 /* Returns the size of the copied struct. */
1679 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1680 num = sizeof(struct bio_dgram_sctp_sndinfo);
1682 memcpy(&(data->sndinfo), ptr, num);
1684 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1685 /* Returns the size of the copied struct. */
1686 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1687 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1689 memcpy(ptr, &data->rcvinfo, num);
1693 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1694 /* Returns the size of the copied struct. */
1695 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1696 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1698 memcpy(&(data->rcvinfo), ptr, num);
1700 case BIO_CTRL_DGRAM_SCTP_GET_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(ptr, &(data->prinfo), num);
1708 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1709 /* Returns the size of the copied struct. */
1710 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1711 num = sizeof(struct bio_dgram_sctp_prinfo);
1713 memcpy(&(data->prinfo), ptr, num);
1715 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1716 /* Returns always 1. */
1718 data->save_shutdown = 1;
1720 data->save_shutdown = 0;
1725 * Pass to default ctrl function to process SCTP unspecific commands
1727 ret = dgram_ctrl(b, cmd, num, ptr);
1733 int BIO_dgram_sctp_notification_cb(BIO *b,
1734 void (*handle_notifications) (BIO *bio,
1740 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1742 if (handle_notifications != NULL) {
1743 data->handle_notifications = handle_notifications;
1744 data->notification_context = context;
1752 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1753 * @b: The BIO to check for the dry event
1755 * Wait until the peer confirms all packets have been received, and so that
1756 * our kernel doesn't have anything to send anymore. This is only received by
1757 * the peer's kernel, not the application.
1761 * 0 when not dry yet
1764 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1767 int n, sockflags, ret;
1768 union sctp_notification snp;
1772 struct sctp_event event;
1774 struct sctp_event_subscribe event;
1775 socklen_t eventsize;
1777 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1779 /* set sender dry event */
1781 memset(&event, 0, sizeof(event));
1782 event.se_assoc_id = 0;
1783 event.se_type = SCTP_SENDER_DRY_EVENT;
1786 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1787 sizeof(struct sctp_event));
1789 eventsize = sizeof(struct sctp_event_subscribe);
1790 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1794 event.sctp_sender_dry_event = 1;
1797 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1798 sizeof(struct sctp_event_subscribe));
1803 /* peek for notification */
1804 memset(&snp, 0, sizeof(snp));
1805 iov.iov_base = (char *)&snp;
1806 iov.iov_len = sizeof(union sctp_notification);
1807 msg.msg_name = NULL;
1808 msg.msg_namelen = 0;
1811 msg.msg_control = NULL;
1812 msg.msg_controllen = 0;
1815 n = recvmsg(b->num, &msg, MSG_PEEK);
1817 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1818 && (get_last_socket_error() != EWOULDBLOCK))
1824 /* if we find a notification, process it and try again if necessary */
1825 while (msg.msg_flags & MSG_NOTIFICATION) {
1826 memset(&snp, 0, sizeof(snp));
1827 iov.iov_base = (char *)&snp;
1828 iov.iov_len = sizeof(union sctp_notification);
1829 msg.msg_name = NULL;
1830 msg.msg_namelen = 0;
1833 msg.msg_control = NULL;
1834 msg.msg_controllen = 0;
1837 n = recvmsg(b->num, &msg, 0);
1839 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1840 && (get_last_socket_error() != EWOULDBLOCK))
1846 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1849 /* disable sender dry event */
1851 memset(&event, 0, sizeof(event));
1852 event.se_assoc_id = 0;
1853 event.se_type = SCTP_SENDER_DRY_EVENT;
1856 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1857 sizeof(struct sctp_event));
1859 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1861 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1866 event.sctp_sender_dry_event = 0;
1869 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1870 sizeof(struct sctp_event_subscribe));
1875 # ifdef SCTP_AUTHENTICATION_EVENT
1876 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1877 dgram_sctp_handle_auth_free_key_event(b, &snp);
1880 if (data->handle_notifications != NULL)
1881 data->handle_notifications(b, data->notification_context,
1884 /* found notification, peek again */
1885 memset(&snp, 0, sizeof(snp));
1886 iov.iov_base = (char *)&snp;
1887 iov.iov_len = sizeof(union sctp_notification);
1888 msg.msg_name = NULL;
1889 msg.msg_namelen = 0;
1892 msg.msg_control = NULL;
1893 msg.msg_controllen = 0;
1896 /* if we have seen the dry already, don't wait */
1898 sockflags = fcntl(b->num, F_GETFL, 0);
1899 fcntl(b->num, F_SETFL, O_NONBLOCK);
1902 n = recvmsg(b->num, &msg, MSG_PEEK);
1905 fcntl(b->num, F_SETFL, sockflags);
1909 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1910 && (get_last_socket_error() != EWOULDBLOCK))
1917 /* read anything else */
1921 int BIO_dgram_sctp_msg_waiting(BIO *b)
1924 union sctp_notification snp;
1927 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1929 /* Check if there are any messages waiting to be read */
1931 memset(&snp, 0, sizeof(snp));
1932 iov.iov_base = (char *)&snp;
1933 iov.iov_len = sizeof(union sctp_notification);
1934 msg.msg_name = NULL;
1935 msg.msg_namelen = 0;
1938 msg.msg_control = NULL;
1939 msg.msg_controllen = 0;
1942 sockflags = fcntl(b->num, F_GETFL, 0);
1943 fcntl(b->num, F_SETFL, O_NONBLOCK);
1944 n = recvmsg(b->num, &msg, MSG_PEEK);
1945 fcntl(b->num, F_SETFL, sockflags);
1947 /* if notification, process and try again */
1948 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1949 # ifdef SCTP_AUTHENTICATION_EVENT
1950 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1951 dgram_sctp_handle_auth_free_key_event(b, &snp);
1954 memset(&snp, 0, sizeof(snp));
1955 iov.iov_base = (char *)&snp;
1956 iov.iov_len = sizeof(union sctp_notification);
1957 msg.msg_name = NULL;
1958 msg.msg_namelen = 0;
1961 msg.msg_control = NULL;
1962 msg.msg_controllen = 0;
1964 n = recvmsg(b->num, &msg, 0);
1966 if (data->handle_notifications != NULL)
1967 data->handle_notifications(b, data->notification_context,
1971 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1973 /* Return 1 if there is a message to be read, return 0 otherwise. */
1980 static int dgram_sctp_puts(BIO *bp, const char *str)
1985 ret = dgram_sctp_write(bp, str, n);
1990 static int BIO_dgram_should_retry(int i)
1994 if ((i == 0) || (i == -1)) {
1995 err = get_last_socket_error();
1997 # if defined(OPENSSL_SYS_WINDOWS)
1999 * If the socket return value (i) is -1 and err is unexpectedly 0 at
2000 * this point, the error code was overwritten by another system call
2001 * before this error handling is called.
2005 return (BIO_dgram_non_fatal_error(err));
2010 int BIO_dgram_non_fatal_error(int err)
2013 # if defined(OPENSSL_SYS_WINDOWS)
2014 # if defined(WSAEWOULDBLOCK)
2015 case WSAEWOULDBLOCK:
2020 # ifdef WSAEWOULDBLOCK
2021 # if WSAEWOULDBLOCK != EWOULDBLOCK
2034 # if EWOULDBLOCK != EAGAIN
2059 static void get_current_time(struct timeval *t)
2061 # if defined(_WIN32)
2064 unsigned __int64 ul;
2069 SystemTimeToFileTime(&st, &now.ft);
2071 now.ul -= 116444736000000000ULL;
2073 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2075 t->tv_sec = (long)(now.ul / 10000000);
2076 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2077 # elif defined(OPENSSL_SYS_VMS)
2080 t->tv_sec = (long)tb.time;
2081 t->tv_usec = (long)tb.millitm * 1000;
2083 gettimeofday(t, NULL);