1 /* crypto/bio/bio_dgram.c */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include "internal/cryptlib.h"
65 #include <openssl/bio.h>
66 #ifndef OPENSSL_NO_DGRAM
68 # if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
69 # include <sys/time.h>
71 # if defined(OPENSSL_SYS_VMS)
72 # include <sys/timeb.h>
75 # ifndef OPENSSL_NO_SCTP
76 # include <netinet/sctp.h>
78 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
79 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
82 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
83 # define IP_MTU 14 /* linux is lame */
86 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
87 # define IPPROTO_IPV6 41 /* windows is lame */
90 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
91 /* Standard definition causes type-punning problems. */
92 # undef IN6_IS_ADDR_V4MAPPED
93 # define s6_addr32 __u6_addr.__u6_addr32
94 # define IN6_IS_ADDR_V4MAPPED(a) \
95 (((a)->s6_addr32[0] == 0) && \
96 ((a)->s6_addr32[1] == 0) && \
97 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
101 # define sock_write SockWrite /* Watt-32 uses same names */
102 # define sock_read SockRead
103 # define sock_puts SockPuts
106 static int dgram_write(BIO *h, const char *buf, int num);
107 static int dgram_read(BIO *h, char *buf, int size);
108 static int dgram_puts(BIO *h, const char *str);
109 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
110 static int dgram_new(BIO *h);
111 static int dgram_free(BIO *data);
112 static int dgram_clear(BIO *bio);
114 # ifndef OPENSSL_NO_SCTP
115 static int dgram_sctp_write(BIO *h, const char *buf, int num);
116 static int dgram_sctp_read(BIO *h, char *buf, int size);
117 static int dgram_sctp_puts(BIO *h, const char *str);
118 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
119 static int dgram_sctp_new(BIO *h);
120 static int dgram_sctp_free(BIO *data);
121 # ifdef SCTP_AUTHENTICATION_EVENT
122 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
127 static int BIO_dgram_should_retry(int s);
129 static void get_current_time(struct timeval *t);
131 static BIO_METHOD methods_dgramp = {
137 NULL, /* dgram_gets, */
144 # ifndef OPENSSL_NO_SCTP
145 static BIO_METHOD methods_dgramp_sctp = {
147 "datagram sctp socket",
151 NULL, /* dgram_gets, */
159 typedef struct bio_dgram_data_st {
162 struct sockaddr_in sa_in;
163 # if OPENSSL_USE_IPV6
164 struct sockaddr_in6 sa_in6;
167 unsigned int connected;
170 struct timeval next_timeout;
171 struct timeval socket_timeout;
174 # ifndef OPENSSL_NO_SCTP
175 typedef struct bio_dgram_sctp_save_message_st {
179 } bio_dgram_sctp_save_message;
181 typedef struct bio_dgram_sctp_data_st {
184 struct sockaddr_in sa_in;
185 # if OPENSSL_USE_IPV6
186 struct sockaddr_in6 sa_in6;
189 unsigned int connected;
192 struct bio_dgram_sctp_sndinfo sndinfo;
193 struct bio_dgram_sctp_rcvinfo rcvinfo;
194 struct bio_dgram_sctp_prinfo prinfo;
195 void (*handle_notifications) (BIO *bio, void *context, void *buf);
196 void *notification_context;
201 int peer_auth_tested;
202 bio_dgram_sctp_save_message saved_message;
203 } bio_dgram_sctp_data;
206 BIO_METHOD *BIO_s_datagram(void)
208 return (&methods_dgramp);
211 BIO *BIO_new_dgram(int fd, int close_flag)
215 ret = BIO_new(BIO_s_datagram());
218 BIO_set_fd(ret, fd, close_flag);
222 static int dgram_new(BIO *bi)
224 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
235 static int dgram_free(BIO *a)
237 bio_dgram_data *data;
244 data = (bio_dgram_data *)a->ptr;
250 static int dgram_clear(BIO *a)
264 static void dgram_adjust_rcv_timeout(BIO *b)
266 # if defined(SO_RCVTIMEO)
267 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
275 /* Is a timer active? */
276 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
277 struct timeval timenow, timeleft;
279 /* Read current socket timeout */
280 # ifdef OPENSSL_SYS_WINDOWS
283 sz.i = sizeof(timeout);
284 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
285 (void *)&timeout, &sz.i) < 0) {
286 perror("getsockopt");
288 data->socket_timeout.tv_sec = timeout / 1000;
289 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
292 sz.i = sizeof(data->socket_timeout);
293 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
294 &(data->socket_timeout), (void *)&sz) < 0) {
295 perror("getsockopt");
296 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
297 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
300 /* Get current time */
301 get_current_time(&timenow);
303 /* Calculate time left until timer expires */
304 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
305 if (timeleft.tv_usec < timenow.tv_usec) {
306 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
309 timeleft.tv_usec -= timenow.tv_usec;
311 if (timeleft.tv_sec < timenow.tv_sec) {
313 timeleft.tv_usec = 1;
315 timeleft.tv_sec -= timenow.tv_sec;
319 * Adjust socket timeout if next handhake message timer will expire
322 if ((data->socket_timeout.tv_sec == 0
323 && data->socket_timeout.tv_usec == 0)
324 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
325 || (data->socket_timeout.tv_sec == timeleft.tv_sec
326 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
327 # ifdef OPENSSL_SYS_WINDOWS
328 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
329 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
330 (void *)&timeout, sizeof(timeout)) < 0) {
331 perror("setsockopt");
334 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
335 sizeof(struct timeval)) < 0) {
336 perror("setsockopt");
344 static void dgram_reset_rcv_timeout(BIO *b)
346 # if defined(SO_RCVTIMEO)
347 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
349 /* Is a timer active? */
350 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
351 # ifdef OPENSSL_SYS_WINDOWS
352 int timeout = data->socket_timeout.tv_sec * 1000 +
353 data->socket_timeout.tv_usec / 1000;
354 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
355 (void *)&timeout, sizeof(timeout)) < 0) {
356 perror("setsockopt");
360 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
361 sizeof(struct timeval)) < 0) {
362 perror("setsockopt");
369 static int dgram_read(BIO *b, char *out, int outl)
372 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
376 * See commentary in b_sock.c. <appro>
384 struct sockaddr_in sa_in;
385 # if OPENSSL_USE_IPV6
386 struct sockaddr_in6 sa_in6;
392 sa.len.i = sizeof(sa.peer);
395 clear_socket_error();
396 memset(&sa.peer, 0, sizeof(sa.peer));
397 dgram_adjust_rcv_timeout(b);
398 ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, (void *)&sa.len);
399 if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
400 OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
401 sa.len.i = (int)sa.len.s;
404 if (!data->connected && ret >= 0)
405 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
407 BIO_clear_retry_flags(b);
409 if (BIO_dgram_should_retry(ret)) {
410 BIO_set_retry_read(b);
411 data->_errno = get_last_socket_error();
415 dgram_reset_rcv_timeout(b);
420 static int dgram_write(BIO *b, const char *in, int inl)
423 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
424 clear_socket_error();
427 ret = writesocket(b->num, in, inl);
429 int peerlen = sizeof(data->peer);
431 if (data->peer.sa.sa_family == AF_INET)
432 peerlen = sizeof(data->peer.sa_in);
433 # if OPENSSL_USE_IPV6
434 else if (data->peer.sa.sa_family == AF_INET6)
435 peerlen = sizeof(data->peer.sa_in6);
437 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
438 ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
440 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
444 BIO_clear_retry_flags(b);
446 if (BIO_dgram_should_retry(ret)) {
447 BIO_set_retry_write(b);
448 data->_errno = get_last_socket_error();
454 static long dgram_get_mtu_overhead(bio_dgram_data *data)
458 switch (data->peer.sa.sa_family) {
461 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
465 # if OPENSSL_USE_IPV6
467 # ifdef IN6_IS_ADDR_V4MAPPED
468 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
470 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
476 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
482 /* We don't know. Go with the historical default */
489 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
493 struct sockaddr *to = NULL;
494 bio_dgram_data *data = NULL;
496 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
497 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
498 * modern enough to define socklen_t */
502 struct sockaddr_in s4;
503 # if OPENSSL_USE_IPV6
504 struct sockaddr_in6 s6;
509 data = (bio_dgram_data *)b->ptr;
514 case BIO_C_FILE_SEEK:
517 case BIO_C_FILE_TELL:
523 b->num = *((int *)ptr);
524 b->shutdown = (int)num;
536 case BIO_CTRL_GET_CLOSE:
539 case BIO_CTRL_SET_CLOSE:
540 b->shutdown = (int)num;
542 case BIO_CTRL_PENDING:
543 case BIO_CTRL_WPENDING:
550 case BIO_CTRL_DGRAM_CONNECT:
551 to = (struct sockaddr *)ptr;
552 switch (to->sa_family) {
554 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
556 # if OPENSSL_USE_IPV6
558 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
562 memcpy(&data->peer, to, sizeof(data->peer.sa));
566 /* (Linux)kernel sets DF bit on outgoing IP packets */
567 case BIO_CTRL_DGRAM_MTU_DISCOVER:
568 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
569 addr_len = (socklen_t) sizeof(addr);
570 memset(&addr, 0, sizeof(addr));
571 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
575 switch (addr.sa.sa_family) {
577 sockopt_val = IP_PMTUDISC_DO;
578 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
579 &sockopt_val, sizeof(sockopt_val))) < 0)
580 perror("setsockopt");
582 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
584 sockopt_val = IPV6_PMTUDISC_DO;
585 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
586 &sockopt_val, sizeof(sockopt_val))) < 0)
587 perror("setsockopt");
598 case BIO_CTRL_DGRAM_QUERY_MTU:
599 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
600 addr_len = (socklen_t) sizeof(addr);
601 memset(&addr, 0, sizeof(addr));
602 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
606 sockopt_len = sizeof(sockopt_val);
607 switch (addr.sa.sa_family) {
610 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
611 &sockopt_len)) < 0 || sockopt_val < 0) {
615 * we assume that the transport protocol is UDP and no IP
618 data->mtu = sockopt_val - 8 - 20;
622 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
625 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
626 (void *)&sockopt_val, &sockopt_len)) < 0
627 || sockopt_val < 0) {
631 * we assume that the transport protocol is UDP and no IPV6
634 data->mtu = sockopt_val - 8 - 40;
647 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
648 ret = -dgram_get_mtu_overhead(data);
649 switch (data->peer.sa.sa_family) {
653 # if OPENSSL_USE_IPV6
655 # ifdef IN6_IS_ADDR_V4MAPPED
656 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
668 case BIO_CTRL_DGRAM_GET_MTU:
670 case BIO_CTRL_DGRAM_SET_MTU:
674 case BIO_CTRL_DGRAM_SET_CONNECTED:
675 to = (struct sockaddr *)ptr;
679 switch (to->sa_family) {
681 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
683 # if OPENSSL_USE_IPV6
685 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
689 memcpy(&data->peer, to, sizeof(data->peer.sa));
694 memset(&data->peer, 0, sizeof(data->peer));
697 case BIO_CTRL_DGRAM_GET_PEER:
698 switch (data->peer.sa.sa_family) {
700 ret = sizeof(data->peer.sa_in);
702 # if OPENSSL_USE_IPV6
704 ret = sizeof(data->peer.sa_in6);
708 ret = sizeof(data->peer.sa);
711 if (num == 0 || num > ret)
713 memcpy(ptr, &data->peer, (ret = num));
715 case BIO_CTRL_DGRAM_SET_PEER:
716 to = (struct sockaddr *)ptr;
717 switch (to->sa_family) {
719 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
721 # if OPENSSL_USE_IPV6
723 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
727 memcpy(&data->peer, to, sizeof(data->peer.sa));
731 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
732 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
734 # if defined(SO_RCVTIMEO)
735 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
736 # ifdef OPENSSL_SYS_WINDOWS
738 struct timeval *tv = (struct timeval *)ptr;
739 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
740 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
741 (void *)&timeout, sizeof(timeout)) < 0) {
742 perror("setsockopt");
747 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
748 sizeof(struct timeval)) < 0) {
749 perror("setsockopt");
754 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
762 # ifdef OPENSSL_SYS_WINDOWS
764 struct timeval *tv = (struct timeval *)ptr;
766 sz.i = sizeof(timeout);
767 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
768 (void *)&timeout, &sz.i) < 0) {
769 perror("getsockopt");
772 tv->tv_sec = timeout / 1000;
773 tv->tv_usec = (timeout % 1000) * 1000;
777 sz.i = sizeof(struct timeval);
778 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
779 ptr, (void *)&sz) < 0) {
780 perror("getsockopt");
782 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
783 OPENSSL_assert(sz.s <= sizeof(struct timeval));
791 # if defined(SO_SNDTIMEO)
792 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
793 # ifdef OPENSSL_SYS_WINDOWS
795 struct timeval *tv = (struct timeval *)ptr;
796 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
797 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
798 (void *)&timeout, sizeof(timeout)) < 0) {
799 perror("setsockopt");
804 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
805 sizeof(struct timeval)) < 0) {
806 perror("setsockopt");
811 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
819 # ifdef OPENSSL_SYS_WINDOWS
821 struct timeval *tv = (struct timeval *)ptr;
823 sz.i = sizeof(timeout);
824 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
825 (void *)&timeout, &sz.i) < 0) {
826 perror("getsockopt");
829 tv->tv_sec = timeout / 1000;
830 tv->tv_usec = (timeout % 1000) * 1000;
834 sz.i = sizeof(struct timeval);
835 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
836 ptr, (void *)&sz) < 0) {
837 perror("getsockopt");
839 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
840 OPENSSL_assert(sz.s <= sizeof(struct timeval));
848 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
850 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
851 # ifdef OPENSSL_SYS_WINDOWS
852 if (data->_errno == WSAETIMEDOUT)
854 if (data->_errno == EAGAIN)
863 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
864 if (data->_errno == EMSGSIZE) {
871 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
872 sockopt_val = num ? 1 : 0;
874 switch (data->peer.sa.sa_family) {
876 # if defined(IP_DONTFRAG)
877 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
878 &sockopt_val, sizeof(sockopt_val))) < 0) {
879 perror("setsockopt");
882 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
883 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
884 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
885 &sockopt_val, sizeof(sockopt_val))) < 0) {
886 perror("setsockopt");
889 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
890 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
891 (const char *)&sockopt_val,
892 sizeof(sockopt_val))) < 0) {
893 perror("setsockopt");
900 # if OPENSSL_USE_IPV6
902 # if defined(IPV6_DONTFRAG)
903 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
904 (const void *)&sockopt_val,
905 sizeof(sockopt_val))) < 0) {
906 perror("setsockopt");
909 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
910 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
911 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
912 &sockopt_val, sizeof(sockopt_val))) < 0) {
913 perror("setsockopt");
926 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
927 ret = dgram_get_mtu_overhead(data);
936 static int dgram_puts(BIO *bp, const char *str)
941 ret = dgram_write(bp, str, n);
945 # ifndef OPENSSL_NO_SCTP
946 BIO_METHOD *BIO_s_datagram_sctp(void)
948 return (&methods_dgramp_sctp);
951 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
954 int ret, optval = 20000;
955 int auth_data = 0, auth_forward = 0;
957 struct sctp_authchunk auth;
958 struct sctp_authchunks *authchunks;
959 socklen_t sockopt_len;
960 # ifdef SCTP_AUTHENTICATION_EVENT
962 struct sctp_event event;
964 struct sctp_event_subscribe event;
968 bio = BIO_new(BIO_s_datagram_sctp());
971 BIO_set_fd(bio, fd, close_flag);
973 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
974 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
976 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
977 sizeof(struct sctp_authchunk));
982 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
984 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
985 sizeof(struct sctp_authchunk));
992 * Test if activation was successful. When using accept(), SCTP-AUTH has
993 * to be activated for the listening socket already, otherwise the
994 * connected socket won't use it.
996 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
997 authchunks = OPENSSL_zalloc(sockopt_len);
1002 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1005 OPENSSL_free(authchunks);
1010 for (p = (unsigned char *)authchunks->gauth_chunks;
1011 p < (unsigned char *)authchunks + sockopt_len;
1012 p += sizeof(uint8_t)) {
1013 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1015 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1019 OPENSSL_free(authchunks);
1021 OPENSSL_assert(auth_data);
1022 OPENSSL_assert(auth_forward);
1024 # ifdef SCTP_AUTHENTICATION_EVENT
1026 memset(&event, 0, sizeof(event));
1027 event.se_assoc_id = 0;
1028 event.se_type = SCTP_AUTHENTICATION_EVENT;
1031 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1032 sizeof(struct sctp_event));
1038 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1039 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1045 event.sctp_authentication_event = 1;
1048 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1049 sizeof(struct sctp_event_subscribe));
1058 * Disable partial delivery by setting the min size larger than the max
1059 * record size of 2^14 + 2048 + 13
1062 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1072 int BIO_dgram_is_sctp(BIO *bio)
1074 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1077 static int dgram_sctp_new(BIO *bi)
1079 bio_dgram_sctp_data *data = NULL;
1083 data = OPENSSL_zalloc(sizeof(*data));
1086 # ifdef SCTP_PR_SCTP_NONE
1087 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1095 static int dgram_sctp_free(BIO *a)
1097 bio_dgram_sctp_data *data;
1101 if (!dgram_clear(a))
1104 data = (bio_dgram_sctp_data *) a->ptr;
1106 OPENSSL_free(data->saved_message.data);
1113 # ifdef SCTP_AUTHENTICATION_EVENT
1114 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1115 union sctp_notification *snp)
1118 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1120 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1121 struct sctp_authkeyid authkeyid;
1124 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1125 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1126 &authkeyid, sizeof(struct sctp_authkeyid));
1131 static int dgram_sctp_read(BIO *b, char *out, int outl)
1133 int ret = 0, n = 0, i, optval;
1135 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1136 union sctp_notification *snp;
1139 struct cmsghdr *cmsg;
1143 clear_socket_error();
1146 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1149 msg.msg_name = NULL;
1150 msg.msg_namelen = 0;
1153 msg.msg_control = cmsgbuf;
1154 msg.msg_controllen = 512;
1156 n = recvmsg(b->num, &msg, 0);
1164 if (msg.msg_controllen > 0) {
1165 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1166 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1167 if (cmsg->cmsg_level != IPPROTO_SCTP)
1169 # ifdef SCTP_RCVINFO
1170 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1171 struct sctp_rcvinfo *rcvinfo;
1173 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1174 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1175 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1176 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1177 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1178 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1179 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1180 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1184 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1185 struct sctp_sndrcvinfo *sndrcvinfo;
1188 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1189 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1190 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1191 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1192 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1193 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1194 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1195 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1201 if (msg.msg_flags & MSG_NOTIFICATION) {
1202 snp = (union sctp_notification *)out;
1203 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1205 struct sctp_event event;
1207 struct sctp_event_subscribe event;
1208 socklen_t eventsize;
1211 * If a message has been delayed until the socket is dry,
1212 * it can be sent now.
1214 if (data->saved_message.length > 0) {
1215 i = dgram_sctp_write(data->saved_message.bio,
1216 data->saved_message.data,
1217 data->saved_message.length);
1222 OPENSSL_free(data->saved_message.data);
1223 data->saved_message.data = NULL;
1224 data->saved_message.length = 0;
1227 /* disable sender dry event */
1229 memset(&event, 0, sizeof(event));
1230 event.se_assoc_id = 0;
1231 event.se_type = SCTP_SENDER_DRY_EVENT;
1233 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1234 sizeof(struct sctp_event));
1240 eventsize = sizeof(struct sctp_event_subscribe);
1241 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1248 event.sctp_sender_dry_event = 0;
1250 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1251 sizeof(struct sctp_event_subscribe));
1258 # ifdef SCTP_AUTHENTICATION_EVENT
1259 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1260 dgram_sctp_handle_auth_free_key_event(b, snp);
1263 if (data->handle_notifications != NULL)
1264 data->handle_notifications(b, data->notification_context,
1267 memset(out, 0, outl);
1271 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1274 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1275 /* Partial message read, this should never happen! */
1278 * The buffer was too small, this means the peer sent a message
1279 * that was larger than allowed.
1285 * Test if socket buffer can handle max record size (2^14 + 2048
1288 optlen = (socklen_t) sizeof(int);
1289 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1291 OPENSSL_assert(optval >= 18445);
1294 * Test if SCTP doesn't partially deliver below max record size
1295 * (2^14 + 2048 + 13)
1297 optlen = (socklen_t) sizeof(int);
1299 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1302 OPENSSL_assert(optval >= 18445);
1305 * Partially delivered notification??? Probably a bug....
1307 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1310 * Everything seems ok till now, so it's most likely a message
1311 * dropped by PR-SCTP.
1313 memset(out, 0, outl);
1314 BIO_set_retry_read(b);
1318 BIO_clear_retry_flags(b);
1320 if (BIO_dgram_should_retry(ret)) {
1321 BIO_set_retry_read(b);
1322 data->_errno = get_last_socket_error();
1326 /* Test if peer uses SCTP-AUTH before continuing */
1327 if (!data->peer_auth_tested) {
1328 int ii, auth_data = 0, auth_forward = 0;
1330 struct sctp_authchunks *authchunks;
1333 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1334 authchunks = OPENSSL_malloc(optlen);
1336 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1339 memset(authchunks, 0, optlen);
1340 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1341 authchunks, &optlen);
1344 for (p = (unsigned char *)authchunks->gauth_chunks;
1345 p < (unsigned char *)authchunks + optlen;
1346 p += sizeof(uint8_t)) {
1347 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1349 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1353 OPENSSL_free(authchunks);
1355 if (!auth_data || !auth_forward) {
1356 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1360 data->peer_auth_tested = 1;
1367 * dgram_sctp_write - send message on SCTP socket
1368 * @b: BIO to write to
1370 * @inl: amount of bytes in @in to send
1372 * Returns -1 on error or the sent amount of bytes on success
1374 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1377 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1378 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1379 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1380 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1381 struct iovec iov[1];
1383 struct cmsghdr *cmsg;
1384 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1385 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1386 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1387 struct sctp_sndinfo *sndinfo;
1388 struct sctp_prinfo *prinfo;
1390 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1391 struct sctp_sndrcvinfo *sndrcvinfo;
1394 clear_socket_error();
1397 * If we're send anything else than application data, disable all user
1398 * parameters and flags.
1401 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1402 # ifdef SCTP_SACK_IMMEDIATELY
1403 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1405 sinfo = &handshake_sinfo;
1409 * If we have to send a shutdown alert message and the socket is not dry
1410 * yet, we have to save it and send it as soon as the socket gets dry.
1412 if (data->save_shutdown) {
1413 ret = BIO_dgram_sctp_wait_for_dry(b);
1419 data->saved_message.bio = b;
1420 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1421 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1424 OPENSSL_free(data->saved_message.data);
1425 data->saved_message.data = tmp;
1426 memcpy(data->saved_message.data, in, inl);
1427 data->saved_message.length = inl;
1432 iov[0].iov_base = (char *)in;
1433 iov[0].iov_len = inl;
1434 msg.msg_name = NULL;
1435 msg.msg_namelen = 0;
1438 msg.msg_control = (caddr_t) cmsgbuf;
1439 msg.msg_controllen = 0;
1441 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1442 cmsg = (struct cmsghdr *)cmsgbuf;
1443 cmsg->cmsg_level = IPPROTO_SCTP;
1444 cmsg->cmsg_type = SCTP_SNDINFO;
1445 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1446 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1447 memset(sndinfo, 0, sizeof(*sndinfo));
1448 sndinfo->snd_sid = sinfo->snd_sid;
1449 sndinfo->snd_flags = sinfo->snd_flags;
1450 sndinfo->snd_ppid = sinfo->snd_ppid;
1451 sndinfo->snd_context = sinfo->snd_context;
1452 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1455 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1456 cmsg->cmsg_level = IPPROTO_SCTP;
1457 cmsg->cmsg_type = SCTP_PRINFO;
1458 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1459 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1460 memset(prinfo, 0, sizeof(*prinfo));
1461 prinfo->pr_policy = pinfo->pr_policy;
1462 prinfo->pr_value = pinfo->pr_value;
1463 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1465 cmsg = (struct cmsghdr *)cmsgbuf;
1466 cmsg->cmsg_level = IPPROTO_SCTP;
1467 cmsg->cmsg_type = SCTP_SNDRCV;
1468 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1469 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1470 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1471 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1472 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1474 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1476 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1477 sndrcvinfo->sinfo_context = sinfo->snd_context;
1478 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1479 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1482 ret = sendmsg(b->num, &msg, 0);
1484 BIO_clear_retry_flags(b);
1486 if (BIO_dgram_should_retry(ret)) {
1487 BIO_set_retry_write(b);
1488 data->_errno = get_last_socket_error();
1494 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1497 bio_dgram_sctp_data *data = NULL;
1498 socklen_t sockopt_len = 0;
1499 struct sctp_authkeyid authkeyid;
1500 struct sctp_authkey *authkey = NULL;
1502 data = (bio_dgram_sctp_data *) b->ptr;
1505 case BIO_CTRL_DGRAM_QUERY_MTU:
1507 * Set to maximum (2^14) and ignore user input to enable transport
1508 * protocol fragmentation. Returns always 2^14.
1513 case BIO_CTRL_DGRAM_SET_MTU:
1515 * Set to maximum (2^14) and ignore input to enable transport
1516 * protocol fragmentation. Returns always 2^14.
1521 case BIO_CTRL_DGRAM_SET_CONNECTED:
1522 case BIO_CTRL_DGRAM_CONNECT:
1523 /* Returns always -1. */
1526 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1528 * SCTP doesn't need the DTLS timer Returns always 1.
1531 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1533 * We allow transport protocol fragmentation so this is irrelevant
1537 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1539 data->in_handshake = 1;
1541 data->in_handshake = 0;
1544 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1545 &data->in_handshake, sizeof(int));
1547 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1549 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1552 /* Get active key */
1553 sockopt_len = sizeof(struct sctp_authkeyid);
1555 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1561 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1562 authkey = OPENSSL_malloc(sockopt_len);
1563 if (authkey == NULL) {
1567 memset(authkey, 0, sockopt_len);
1568 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1569 # ifndef __FreeBSD__
1571 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1572 * and higher work without it.
1574 authkey->sca_keylength = 64;
1576 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1579 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1581 OPENSSL_free(authkey);
1586 /* Reset active key */
1587 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1588 &authkeyid, sizeof(struct sctp_authkeyid));
1593 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1594 /* Returns 0 on success, -1 otherwise. */
1596 /* Get active key */
1597 sockopt_len = sizeof(struct sctp_authkeyid);
1599 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1604 /* Set active key */
1605 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1606 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1607 &authkeyid, sizeof(struct sctp_authkeyid));
1612 * CCS has been sent, so remember that and fall through to check if
1613 * we need to deactivate an old key
1617 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1618 /* Returns 0 on success, -1 otherwise. */
1621 * Has this command really been called or is this just a
1624 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1628 * CSS has been both, received and sent, so deactivate an old key
1630 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1631 /* Get active key */
1632 sockopt_len = sizeof(struct sctp_authkeyid);
1634 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1635 &authkeyid, &sockopt_len);
1640 * Deactivate key or delete second last key if
1641 * SCTP_AUTHENTICATION_EVENT is not available.
1643 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1644 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1645 sockopt_len = sizeof(struct sctp_authkeyid);
1646 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1647 &authkeyid, sockopt_len);
1651 # ifndef SCTP_AUTHENTICATION_EVENT
1652 if (authkeyid.scact_keynumber > 0) {
1653 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1654 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1655 &authkeyid, sizeof(struct sctp_authkeyid));
1665 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1666 /* Returns the size of the copied struct. */
1667 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1668 num = sizeof(struct bio_dgram_sctp_sndinfo);
1670 memcpy(ptr, &(data->sndinfo), num);
1673 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1674 /* Returns the size of the copied struct. */
1675 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1676 num = sizeof(struct bio_dgram_sctp_sndinfo);
1678 memcpy(&(data->sndinfo), ptr, num);
1680 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1681 /* Returns the size of the copied struct. */
1682 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1683 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1685 memcpy(ptr, &data->rcvinfo, num);
1689 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1690 /* Returns the size of the copied struct. */
1691 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1692 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1694 memcpy(&(data->rcvinfo), ptr, num);
1696 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1697 /* Returns the size of the copied struct. */
1698 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1699 num = sizeof(struct bio_dgram_sctp_prinfo);
1701 memcpy(ptr, &(data->prinfo), num);
1704 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1705 /* Returns the size of the copied struct. */
1706 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1707 num = sizeof(struct bio_dgram_sctp_prinfo);
1709 memcpy(&(data->prinfo), ptr, num);
1711 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1712 /* Returns always 1. */
1714 data->save_shutdown = 1;
1716 data->save_shutdown = 0;
1721 * Pass to default ctrl function to process SCTP unspecific commands
1723 ret = dgram_ctrl(b, cmd, num, ptr);
1729 int BIO_dgram_sctp_notification_cb(BIO *b,
1730 void (*handle_notifications) (BIO *bio,
1736 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1738 if (handle_notifications != NULL) {
1739 data->handle_notifications = handle_notifications;
1740 data->notification_context = context;
1748 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1749 * @b: The BIO to check for the dry event
1751 * Wait until the peer confirms all packets have been received, and so that
1752 * our kernel doesn't have anything to send anymore. This is only received by
1753 * the peer's kernel, not the application.
1757 * 0 when not dry yet
1760 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1763 int n, sockflags, ret;
1764 union sctp_notification snp;
1768 struct sctp_event event;
1770 struct sctp_event_subscribe event;
1771 socklen_t eventsize;
1773 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1775 /* set sender dry event */
1777 memset(&event, 0, sizeof(event));
1778 event.se_assoc_id = 0;
1779 event.se_type = SCTP_SENDER_DRY_EVENT;
1782 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1783 sizeof(struct sctp_event));
1785 eventsize = sizeof(struct sctp_event_subscribe);
1786 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1790 event.sctp_sender_dry_event = 1;
1793 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1794 sizeof(struct sctp_event_subscribe));
1799 /* peek for notification */
1800 memset(&snp, 0, sizeof(snp));
1801 iov.iov_base = (char *)&snp;
1802 iov.iov_len = sizeof(union sctp_notification);
1803 msg.msg_name = NULL;
1804 msg.msg_namelen = 0;
1807 msg.msg_control = NULL;
1808 msg.msg_controllen = 0;
1811 n = recvmsg(b->num, &msg, MSG_PEEK);
1813 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1814 && (get_last_socket_error() != EWOULDBLOCK))
1820 /* if we find a notification, process it and try again if necessary */
1821 while (msg.msg_flags & MSG_NOTIFICATION) {
1822 memset(&snp, 0, sizeof(snp));
1823 iov.iov_base = (char *)&snp;
1824 iov.iov_len = sizeof(union sctp_notification);
1825 msg.msg_name = NULL;
1826 msg.msg_namelen = 0;
1829 msg.msg_control = NULL;
1830 msg.msg_controllen = 0;
1833 n = recvmsg(b->num, &msg, 0);
1835 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1836 && (get_last_socket_error() != EWOULDBLOCK))
1842 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1845 /* disable sender dry event */
1847 memset(&event, 0, sizeof(event));
1848 event.se_assoc_id = 0;
1849 event.se_type = SCTP_SENDER_DRY_EVENT;
1852 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1853 sizeof(struct sctp_event));
1855 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1857 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1862 event.sctp_sender_dry_event = 0;
1865 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1866 sizeof(struct sctp_event_subscribe));
1871 # ifdef SCTP_AUTHENTICATION_EVENT
1872 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1873 dgram_sctp_handle_auth_free_key_event(b, &snp);
1876 if (data->handle_notifications != NULL)
1877 data->handle_notifications(b, data->notification_context,
1880 /* found notification, peek again */
1881 memset(&snp, 0, sizeof(snp));
1882 iov.iov_base = (char *)&snp;
1883 iov.iov_len = sizeof(union sctp_notification);
1884 msg.msg_name = NULL;
1885 msg.msg_namelen = 0;
1888 msg.msg_control = NULL;
1889 msg.msg_controllen = 0;
1892 /* if we have seen the dry already, don't wait */
1894 sockflags = fcntl(b->num, F_GETFL, 0);
1895 fcntl(b->num, F_SETFL, O_NONBLOCK);
1898 n = recvmsg(b->num, &msg, MSG_PEEK);
1901 fcntl(b->num, F_SETFL, sockflags);
1905 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1906 && (get_last_socket_error() != EWOULDBLOCK))
1913 /* read anything else */
1917 int BIO_dgram_sctp_msg_waiting(BIO *b)
1920 union sctp_notification snp;
1923 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1925 /* Check if there are any messages waiting to be read */
1927 memset(&snp, 0, sizeof(snp));
1928 iov.iov_base = (char *)&snp;
1929 iov.iov_len = sizeof(union sctp_notification);
1930 msg.msg_name = NULL;
1931 msg.msg_namelen = 0;
1934 msg.msg_control = NULL;
1935 msg.msg_controllen = 0;
1938 sockflags = fcntl(b->num, F_GETFL, 0);
1939 fcntl(b->num, F_SETFL, O_NONBLOCK);
1940 n = recvmsg(b->num, &msg, MSG_PEEK);
1941 fcntl(b->num, F_SETFL, sockflags);
1943 /* if notification, process and try again */
1944 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1945 # ifdef SCTP_AUTHENTICATION_EVENT
1946 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1947 dgram_sctp_handle_auth_free_key_event(b, &snp);
1950 memset(&snp, 0, sizeof(snp));
1951 iov.iov_base = (char *)&snp;
1952 iov.iov_len = sizeof(union sctp_notification);
1953 msg.msg_name = NULL;
1954 msg.msg_namelen = 0;
1957 msg.msg_control = NULL;
1958 msg.msg_controllen = 0;
1960 n = recvmsg(b->num, &msg, 0);
1962 if (data->handle_notifications != NULL)
1963 data->handle_notifications(b, data->notification_context,
1967 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1969 /* Return 1 if there is a message to be read, return 0 otherwise. */
1976 static int dgram_sctp_puts(BIO *bp, const char *str)
1981 ret = dgram_sctp_write(bp, str, n);
1986 static int BIO_dgram_should_retry(int i)
1990 if ((i == 0) || (i == -1)) {
1991 err = get_last_socket_error();
1993 # if defined(OPENSSL_SYS_WINDOWS)
1995 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1996 * this point, the error code was overwritten by another system call
1997 * before this error handling is called.
2001 return (BIO_dgram_non_fatal_error(err));
2006 int BIO_dgram_non_fatal_error(int err)
2009 # if defined(OPENSSL_SYS_WINDOWS)
2010 # if defined(WSAEWOULDBLOCK)
2011 case WSAEWOULDBLOCK:
2016 # ifdef WSAEWOULDBLOCK
2017 # if WSAEWOULDBLOCK != EWOULDBLOCK
2030 # if EWOULDBLOCK != EAGAIN
2055 static void get_current_time(struct timeval *t)
2057 # if defined(_WIN32)
2060 unsigned __int64 ul;
2065 SystemTimeToFileTime(&st, &now.ft);
2067 now.ul -= 116444736000000000ULL;
2069 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2071 t->tv_sec = (long)(now.ul / 10000000);
2072 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2073 # elif defined(OPENSSL_SYS_VMS)
2076 t->tv_sec = (long)tb.time;
2077 t->tv_usec = (long)tb.millitm * 1000;
2079 gettimeofday(t, NULL);