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));
232 static int dgram_free(BIO *a)
234 bio_dgram_data *data;
241 data = (bio_dgram_data *)a->ptr;
247 static int dgram_clear(BIO *a)
261 static void dgram_adjust_rcv_timeout(BIO *b)
263 # if defined(SO_RCVTIMEO)
264 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
272 /* Is a timer active? */
273 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
274 struct timeval timenow, timeleft;
276 /* Read current socket timeout */
277 # ifdef OPENSSL_SYS_WINDOWS
280 sz.i = sizeof(timeout);
281 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
282 (void *)&timeout, &sz.i) < 0) {
283 perror("getsockopt");
285 data->socket_timeout.tv_sec = timeout / 1000;
286 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
289 sz.i = sizeof(data->socket_timeout);
290 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
291 &(data->socket_timeout), (void *)&sz) < 0) {
292 perror("getsockopt");
293 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
294 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
297 /* Get current time */
298 get_current_time(&timenow);
300 /* Calculate time left until timer expires */
301 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
302 if (timeleft.tv_usec < timenow.tv_usec) {
303 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
306 timeleft.tv_usec -= timenow.tv_usec;
308 if (timeleft.tv_sec < timenow.tv_sec) {
310 timeleft.tv_usec = 1;
312 timeleft.tv_sec -= timenow.tv_sec;
316 * Adjust socket timeout if next handhake message timer will expire
319 if ((data->socket_timeout.tv_sec == 0
320 && data->socket_timeout.tv_usec == 0)
321 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
322 || (data->socket_timeout.tv_sec == timeleft.tv_sec
323 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
324 # ifdef OPENSSL_SYS_WINDOWS
325 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
326 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
327 (void *)&timeout, sizeof(timeout)) < 0) {
328 perror("setsockopt");
331 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
332 sizeof(struct timeval)) < 0) {
333 perror("setsockopt");
341 static void dgram_reset_rcv_timeout(BIO *b)
343 # if defined(SO_RCVTIMEO)
344 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
346 /* Is a timer active? */
347 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
348 # ifdef OPENSSL_SYS_WINDOWS
349 int timeout = data->socket_timeout.tv_sec * 1000 +
350 data->socket_timeout.tv_usec / 1000;
351 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
352 (void *)&timeout, sizeof(timeout)) < 0) {
353 perror("setsockopt");
357 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
358 sizeof(struct timeval)) < 0) {
359 perror("setsockopt");
366 static int dgram_read(BIO *b, char *out, int outl)
369 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
373 * See commentary in b_sock.c. <appro>
381 struct sockaddr_in sa_in;
382 # if OPENSSL_USE_IPV6
383 struct sockaddr_in6 sa_in6;
389 sa.len.i = sizeof(sa.peer);
392 clear_socket_error();
393 memset(&sa.peer, 0, sizeof(sa.peer));
394 dgram_adjust_rcv_timeout(b);
395 ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, (void *)&sa.len);
396 if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
397 OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
398 sa.len.i = (int)sa.len.s;
401 if (!data->connected && ret >= 0)
402 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
404 BIO_clear_retry_flags(b);
406 if (BIO_dgram_should_retry(ret)) {
407 BIO_set_retry_read(b);
408 data->_errno = get_last_socket_error();
412 dgram_reset_rcv_timeout(b);
417 static int dgram_write(BIO *b, const char *in, int inl)
420 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
421 clear_socket_error();
424 ret = writesocket(b->num, in, inl);
426 int peerlen = sizeof(data->peer);
428 if (data->peer.sa.sa_family == AF_INET)
429 peerlen = sizeof(data->peer.sa_in);
430 # if OPENSSL_USE_IPV6
431 else if (data->peer.sa.sa_family == AF_INET6)
432 peerlen = sizeof(data->peer.sa_in6);
434 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
435 ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
437 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
441 BIO_clear_retry_flags(b);
443 if (BIO_dgram_should_retry(ret)) {
444 BIO_set_retry_write(b);
445 data->_errno = get_last_socket_error();
451 static long dgram_get_mtu_overhead(bio_dgram_data *data)
455 switch (data->peer.sa.sa_family) {
458 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
462 # if OPENSSL_USE_IPV6
464 # ifdef IN6_IS_ADDR_V4MAPPED
465 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
467 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
473 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
479 /* We don't know. Go with the historical default */
486 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
490 struct sockaddr *to = NULL;
491 bio_dgram_data *data = NULL;
493 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
494 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
495 * modern enough to define socklen_t */
499 struct sockaddr_in s4;
500 # if OPENSSL_USE_IPV6
501 struct sockaddr_in6 s6;
506 data = (bio_dgram_data *)b->ptr;
511 case BIO_C_FILE_SEEK:
514 case BIO_C_FILE_TELL:
520 b->num = *((int *)ptr);
521 b->shutdown = (int)num;
533 case BIO_CTRL_GET_CLOSE:
536 case BIO_CTRL_SET_CLOSE:
537 b->shutdown = (int)num;
539 case BIO_CTRL_PENDING:
540 case BIO_CTRL_WPENDING:
547 case BIO_CTRL_DGRAM_CONNECT:
548 to = (struct sockaddr *)ptr;
549 switch (to->sa_family) {
551 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
553 # if OPENSSL_USE_IPV6
555 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
559 memcpy(&data->peer, to, sizeof(data->peer.sa));
563 /* (Linux)kernel sets DF bit on outgoing IP packets */
564 case BIO_CTRL_DGRAM_MTU_DISCOVER:
565 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
566 addr_len = (socklen_t) sizeof(addr);
567 memset(&addr, 0, sizeof(addr));
568 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
572 switch (addr.sa.sa_family) {
574 sockopt_val = IP_PMTUDISC_DO;
575 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
576 &sockopt_val, sizeof(sockopt_val))) < 0)
577 perror("setsockopt");
579 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
581 sockopt_val = IPV6_PMTUDISC_DO;
582 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
583 &sockopt_val, sizeof(sockopt_val))) < 0)
584 perror("setsockopt");
595 case BIO_CTRL_DGRAM_QUERY_MTU:
596 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
597 addr_len = (socklen_t) sizeof(addr);
598 memset(&addr, 0, sizeof(addr));
599 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
603 sockopt_len = sizeof(sockopt_val);
604 switch (addr.sa.sa_family) {
607 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
608 &sockopt_len)) < 0 || sockopt_val < 0) {
612 * we assume that the transport protocol is UDP and no IP
615 data->mtu = sockopt_val - 8 - 20;
619 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
622 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
623 (void *)&sockopt_val, &sockopt_len)) < 0
624 || sockopt_val < 0) {
628 * we assume that the transport protocol is UDP and no IPV6
631 data->mtu = sockopt_val - 8 - 40;
644 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
645 ret = -dgram_get_mtu_overhead(data);
646 switch (data->peer.sa.sa_family) {
650 # if OPENSSL_USE_IPV6
652 # ifdef IN6_IS_ADDR_V4MAPPED
653 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
665 case BIO_CTRL_DGRAM_GET_MTU:
667 case BIO_CTRL_DGRAM_SET_MTU:
671 case BIO_CTRL_DGRAM_SET_CONNECTED:
672 to = (struct sockaddr *)ptr;
676 switch (to->sa_family) {
678 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
680 # if OPENSSL_USE_IPV6
682 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
686 memcpy(&data->peer, to, sizeof(data->peer.sa));
691 memset(&data->peer, 0, sizeof(data->peer));
694 case BIO_CTRL_DGRAM_GET_PEER:
695 switch (data->peer.sa.sa_family) {
697 ret = sizeof(data->peer.sa_in);
699 # if OPENSSL_USE_IPV6
701 ret = sizeof(data->peer.sa_in6);
705 ret = sizeof(data->peer.sa);
708 if (num == 0 || num > ret)
710 memcpy(ptr, &data->peer, (ret = num));
712 case BIO_CTRL_DGRAM_SET_PEER:
713 to = (struct sockaddr *)ptr;
714 switch (to->sa_family) {
716 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
718 # if OPENSSL_USE_IPV6
720 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
724 memcpy(&data->peer, to, sizeof(data->peer.sa));
728 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
729 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
731 # if defined(SO_RCVTIMEO)
732 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
733 # ifdef OPENSSL_SYS_WINDOWS
735 struct timeval *tv = (struct timeval *)ptr;
736 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
737 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
738 (void *)&timeout, sizeof(timeout)) < 0) {
739 perror("setsockopt");
744 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
745 sizeof(struct timeval)) < 0) {
746 perror("setsockopt");
751 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
759 # ifdef OPENSSL_SYS_WINDOWS
761 struct timeval *tv = (struct timeval *)ptr;
763 sz.i = sizeof(timeout);
764 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
765 (void *)&timeout, &sz.i) < 0) {
766 perror("getsockopt");
769 tv->tv_sec = timeout / 1000;
770 tv->tv_usec = (timeout % 1000) * 1000;
774 sz.i = sizeof(struct timeval);
775 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
776 ptr, (void *)&sz) < 0) {
777 perror("getsockopt");
779 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
780 OPENSSL_assert(sz.s <= sizeof(struct timeval));
788 # if defined(SO_SNDTIMEO)
789 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
790 # ifdef OPENSSL_SYS_WINDOWS
792 struct timeval *tv = (struct timeval *)ptr;
793 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
794 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
795 (void *)&timeout, sizeof(timeout)) < 0) {
796 perror("setsockopt");
801 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
802 sizeof(struct timeval)) < 0) {
803 perror("setsockopt");
808 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
816 # ifdef OPENSSL_SYS_WINDOWS
818 struct timeval *tv = (struct timeval *)ptr;
820 sz.i = sizeof(timeout);
821 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
822 (void *)&timeout, &sz.i) < 0) {
823 perror("getsockopt");
826 tv->tv_sec = timeout / 1000;
827 tv->tv_usec = (timeout % 1000) * 1000;
831 sz.i = sizeof(struct timeval);
832 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
833 ptr, (void *)&sz) < 0) {
834 perror("getsockopt");
836 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
837 OPENSSL_assert(sz.s <= sizeof(struct timeval));
845 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
847 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
848 # ifdef OPENSSL_SYS_WINDOWS
849 if (data->_errno == WSAETIMEDOUT)
851 if (data->_errno == EAGAIN)
860 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
861 if (data->_errno == EMSGSIZE) {
868 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
869 sockopt_val = num ? 1 : 0;
871 switch (data->peer.sa.sa_family) {
873 # if defined(IP_DONTFRAG)
874 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
875 &sockopt_val, sizeof(sockopt_val))) < 0) {
876 perror("setsockopt");
879 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
880 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
881 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
882 &sockopt_val, sizeof(sockopt_val))) < 0) {
883 perror("setsockopt");
886 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
887 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
888 (const char *)&sockopt_val,
889 sizeof(sockopt_val))) < 0) {
890 perror("setsockopt");
897 # if OPENSSL_USE_IPV6
899 # if defined(IPV6_DONTFRAG)
900 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
901 (const void *)&sockopt_val,
902 sizeof(sockopt_val))) < 0) {
903 perror("setsockopt");
906 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
907 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
908 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
909 &sockopt_val, sizeof(sockopt_val))) < 0) {
910 perror("setsockopt");
923 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
924 ret = dgram_get_mtu_overhead(data);
933 static int dgram_puts(BIO *bp, const char *str)
938 ret = dgram_write(bp, str, n);
942 # ifndef OPENSSL_NO_SCTP
943 BIO_METHOD *BIO_s_datagram_sctp(void)
945 return (&methods_dgramp_sctp);
948 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
951 int ret, optval = 20000;
952 int auth_data = 0, auth_forward = 0;
954 struct sctp_authchunk auth;
955 struct sctp_authchunks *authchunks;
956 socklen_t sockopt_len;
957 # ifdef SCTP_AUTHENTICATION_EVENT
959 struct sctp_event event;
961 struct sctp_event_subscribe event;
965 bio = BIO_new(BIO_s_datagram_sctp());
968 BIO_set_fd(bio, fd, close_flag);
970 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
971 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
973 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
974 sizeof(struct sctp_authchunk));
979 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
981 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
982 sizeof(struct sctp_authchunk));
989 * Test if activation was successful. When using accept(), SCTP-AUTH has
990 * to be activated for the listening socket already, otherwise the
991 * connected socket won't use it.
993 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
994 authchunks = OPENSSL_zalloc(sockopt_len);
999 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1002 OPENSSL_free(authchunks);
1007 for (p = (unsigned char *)authchunks->gauth_chunks;
1008 p < (unsigned char *)authchunks + sockopt_len;
1009 p += sizeof(uint8_t)) {
1010 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1012 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1016 OPENSSL_free(authchunks);
1018 OPENSSL_assert(auth_data);
1019 OPENSSL_assert(auth_forward);
1021 # ifdef SCTP_AUTHENTICATION_EVENT
1023 memset(&event, 0, sizeof(event));
1024 event.se_assoc_id = 0;
1025 event.se_type = SCTP_AUTHENTICATION_EVENT;
1028 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1029 sizeof(struct sctp_event));
1035 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1036 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1042 event.sctp_authentication_event = 1;
1045 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1046 sizeof(struct sctp_event_subscribe));
1055 * Disable partial delivery by setting the min size larger than the max
1056 * record size of 2^14 + 2048 + 13
1059 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1069 int BIO_dgram_is_sctp(BIO *bio)
1071 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1074 static int dgram_sctp_new(BIO *bi)
1076 bio_dgram_sctp_data *data = NULL;
1080 data = OPENSSL_zalloc(sizeof(*data));
1083 # ifdef SCTP_PR_SCTP_NONE
1084 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1092 static int dgram_sctp_free(BIO *a)
1094 bio_dgram_sctp_data *data;
1098 if (!dgram_clear(a))
1101 data = (bio_dgram_sctp_data *) a->ptr;
1103 OPENSSL_free(data->saved_message.data);
1110 # ifdef SCTP_AUTHENTICATION_EVENT
1111 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1112 union sctp_notification *snp)
1115 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1117 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1118 struct sctp_authkeyid authkeyid;
1121 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1122 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1123 &authkeyid, sizeof(struct sctp_authkeyid));
1128 static int dgram_sctp_read(BIO *b, char *out, int outl)
1130 int ret = 0, n = 0, i, optval;
1132 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1133 union sctp_notification *snp;
1136 struct cmsghdr *cmsg;
1140 clear_socket_error();
1143 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1146 msg.msg_name = NULL;
1147 msg.msg_namelen = 0;
1150 msg.msg_control = cmsgbuf;
1151 msg.msg_controllen = 512;
1153 n = recvmsg(b->num, &msg, 0);
1161 if (msg.msg_controllen > 0) {
1162 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1163 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1164 if (cmsg->cmsg_level != IPPROTO_SCTP)
1166 # ifdef SCTP_RCVINFO
1167 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1168 struct sctp_rcvinfo *rcvinfo;
1170 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1171 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1172 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1173 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1174 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1175 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1176 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1177 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1181 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1182 struct sctp_sndrcvinfo *sndrcvinfo;
1185 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1186 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1187 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1188 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1189 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1190 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1191 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1192 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1198 if (msg.msg_flags & MSG_NOTIFICATION) {
1199 snp = (union sctp_notification *)out;
1200 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1202 struct sctp_event event;
1204 struct sctp_event_subscribe event;
1205 socklen_t eventsize;
1208 * If a message has been delayed until the socket is dry,
1209 * it can be sent now.
1211 if (data->saved_message.length > 0) {
1212 i = dgram_sctp_write(data->saved_message.bio,
1213 data->saved_message.data,
1214 data->saved_message.length);
1219 OPENSSL_free(data->saved_message.data);
1220 data->saved_message.data = NULL;
1221 data->saved_message.length = 0;
1224 /* disable sender dry event */
1226 memset(&event, 0, sizeof(event));
1227 event.se_assoc_id = 0;
1228 event.se_type = SCTP_SENDER_DRY_EVENT;
1230 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1231 sizeof(struct sctp_event));
1237 eventsize = sizeof(struct sctp_event_subscribe);
1238 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1245 event.sctp_sender_dry_event = 0;
1247 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1248 sizeof(struct sctp_event_subscribe));
1255 # ifdef SCTP_AUTHENTICATION_EVENT
1256 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1257 dgram_sctp_handle_auth_free_key_event(b, snp);
1260 if (data->handle_notifications != NULL)
1261 data->handle_notifications(b, data->notification_context,
1264 memset(out, 0, outl);
1268 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1271 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1272 /* Partial message read, this should never happen! */
1275 * The buffer was too small, this means the peer sent a message
1276 * that was larger than allowed.
1282 * Test if socket buffer can handle max record size (2^14 + 2048
1285 optlen = (socklen_t) sizeof(int);
1286 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1288 OPENSSL_assert(optval >= 18445);
1291 * Test if SCTP doesn't partially deliver below max record size
1292 * (2^14 + 2048 + 13)
1294 optlen = (socklen_t) sizeof(int);
1296 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1299 OPENSSL_assert(optval >= 18445);
1302 * Partially delivered notification??? Probably a bug....
1304 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1307 * Everything seems ok till now, so it's most likely a message
1308 * dropped by PR-SCTP.
1310 memset(out, 0, outl);
1311 BIO_set_retry_read(b);
1315 BIO_clear_retry_flags(b);
1317 if (BIO_dgram_should_retry(ret)) {
1318 BIO_set_retry_read(b);
1319 data->_errno = get_last_socket_error();
1323 /* Test if peer uses SCTP-AUTH before continuing */
1324 if (!data->peer_auth_tested) {
1325 int ii, auth_data = 0, auth_forward = 0;
1327 struct sctp_authchunks *authchunks;
1330 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1331 authchunks = OPENSSL_malloc(optlen);
1333 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1336 memset(authchunks, 0, optlen);
1337 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1338 authchunks, &optlen);
1341 for (p = (unsigned char *)authchunks->gauth_chunks;
1342 p < (unsigned char *)authchunks + optlen;
1343 p += sizeof(uint8_t)) {
1344 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1346 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1350 OPENSSL_free(authchunks);
1352 if (!auth_data || !auth_forward) {
1353 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1357 data->peer_auth_tested = 1;
1364 * dgram_sctp_write - send message on SCTP socket
1365 * @b: BIO to write to
1367 * @inl: amount of bytes in @in to send
1369 * Returns -1 on error or the sent amount of bytes on success
1371 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1374 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1375 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1376 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1377 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1378 struct iovec iov[1];
1380 struct cmsghdr *cmsg;
1381 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1382 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1383 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1384 struct sctp_sndinfo *sndinfo;
1385 struct sctp_prinfo *prinfo;
1387 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1388 struct sctp_sndrcvinfo *sndrcvinfo;
1391 clear_socket_error();
1394 * If we're send anything else than application data, disable all user
1395 * parameters and flags.
1398 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1399 # ifdef SCTP_SACK_IMMEDIATELY
1400 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1402 sinfo = &handshake_sinfo;
1406 * If we have to send a shutdown alert message and the socket is not dry
1407 * yet, we have to save it and send it as soon as the socket gets dry.
1409 if (data->save_shutdown) {
1410 ret = BIO_dgram_sctp_wait_for_dry(b);
1416 data->saved_message.bio = b;
1417 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1418 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1421 OPENSSL_free(data->saved_message.data);
1422 data->saved_message.data = tmp;
1423 memcpy(data->saved_message.data, in, inl);
1424 data->saved_message.length = inl;
1429 iov[0].iov_base = (char *)in;
1430 iov[0].iov_len = inl;
1431 msg.msg_name = NULL;
1432 msg.msg_namelen = 0;
1435 msg.msg_control = (caddr_t) cmsgbuf;
1436 msg.msg_controllen = 0;
1438 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1439 cmsg = (struct cmsghdr *)cmsgbuf;
1440 cmsg->cmsg_level = IPPROTO_SCTP;
1441 cmsg->cmsg_type = SCTP_SNDINFO;
1442 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1443 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1444 memset(sndinfo, 0, sizeof(*sndinfo));
1445 sndinfo->snd_sid = sinfo->snd_sid;
1446 sndinfo->snd_flags = sinfo->snd_flags;
1447 sndinfo->snd_ppid = sinfo->snd_ppid;
1448 sndinfo->snd_context = sinfo->snd_context;
1449 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1452 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1453 cmsg->cmsg_level = IPPROTO_SCTP;
1454 cmsg->cmsg_type = SCTP_PRINFO;
1455 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1456 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1457 memset(prinfo, 0, sizeof(*prinfo));
1458 prinfo->pr_policy = pinfo->pr_policy;
1459 prinfo->pr_value = pinfo->pr_value;
1460 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1462 cmsg = (struct cmsghdr *)cmsgbuf;
1463 cmsg->cmsg_level = IPPROTO_SCTP;
1464 cmsg->cmsg_type = SCTP_SNDRCV;
1465 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1466 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1467 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1468 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1469 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1471 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1473 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1474 sndrcvinfo->sinfo_context = sinfo->snd_context;
1475 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1476 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1479 ret = sendmsg(b->num, &msg, 0);
1481 BIO_clear_retry_flags(b);
1483 if (BIO_dgram_should_retry(ret)) {
1484 BIO_set_retry_write(b);
1485 data->_errno = get_last_socket_error();
1491 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1494 bio_dgram_sctp_data *data = NULL;
1495 socklen_t sockopt_len = 0;
1496 struct sctp_authkeyid authkeyid;
1497 struct sctp_authkey *authkey = NULL;
1499 data = (bio_dgram_sctp_data *) b->ptr;
1502 case BIO_CTRL_DGRAM_QUERY_MTU:
1504 * Set to maximum (2^14) and ignore user input to enable transport
1505 * protocol fragmentation. Returns always 2^14.
1510 case BIO_CTRL_DGRAM_SET_MTU:
1512 * Set to maximum (2^14) and ignore input to enable transport
1513 * protocol fragmentation. Returns always 2^14.
1518 case BIO_CTRL_DGRAM_SET_CONNECTED:
1519 case BIO_CTRL_DGRAM_CONNECT:
1520 /* Returns always -1. */
1523 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1525 * SCTP doesn't need the DTLS timer Returns always 1.
1528 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1530 * We allow transport protocol fragmentation so this is irrelevant
1534 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1536 data->in_handshake = 1;
1538 data->in_handshake = 0;
1541 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1542 &data->in_handshake, sizeof(int));
1544 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1546 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1549 /* Get active key */
1550 sockopt_len = sizeof(struct sctp_authkeyid);
1552 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1558 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1559 authkey = OPENSSL_malloc(sockopt_len);
1560 if (authkey == NULL) {
1564 memset(authkey, 0, sockopt_len);
1565 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1566 # ifndef __FreeBSD__
1568 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1569 * and higher work without it.
1571 authkey->sca_keylength = 64;
1573 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1576 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1578 OPENSSL_free(authkey);
1583 /* Reset active key */
1584 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1585 &authkeyid, sizeof(struct sctp_authkeyid));
1590 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1591 /* Returns 0 on success, -1 otherwise. */
1593 /* Get active key */
1594 sockopt_len = sizeof(struct sctp_authkeyid);
1596 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1601 /* Set active key */
1602 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1603 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1604 &authkeyid, sizeof(struct sctp_authkeyid));
1609 * CCS has been sent, so remember that and fall through to check if
1610 * we need to deactivate an old key
1614 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1615 /* Returns 0 on success, -1 otherwise. */
1618 * Has this command really been called or is this just a
1621 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1625 * CSS has been both, received and sent, so deactivate an old key
1627 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1628 /* Get active key */
1629 sockopt_len = sizeof(struct sctp_authkeyid);
1631 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1632 &authkeyid, &sockopt_len);
1637 * Deactivate key or delete second last key if
1638 * SCTP_AUTHENTICATION_EVENT is not available.
1640 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1641 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1642 sockopt_len = sizeof(struct sctp_authkeyid);
1643 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1644 &authkeyid, sockopt_len);
1648 # ifndef SCTP_AUTHENTICATION_EVENT
1649 if (authkeyid.scact_keynumber > 0) {
1650 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1651 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1652 &authkeyid, sizeof(struct sctp_authkeyid));
1662 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1663 /* Returns the size of the copied struct. */
1664 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1665 num = sizeof(struct bio_dgram_sctp_sndinfo);
1667 memcpy(ptr, &(data->sndinfo), num);
1670 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1671 /* Returns the size of the copied struct. */
1672 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1673 num = sizeof(struct bio_dgram_sctp_sndinfo);
1675 memcpy(&(data->sndinfo), ptr, num);
1677 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1678 /* Returns the size of the copied struct. */
1679 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1680 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1682 memcpy(ptr, &data->rcvinfo, num);
1686 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1687 /* Returns the size of the copied struct. */
1688 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1689 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1691 memcpy(&(data->rcvinfo), ptr, num);
1693 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1694 /* Returns the size of the copied struct. */
1695 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1696 num = sizeof(struct bio_dgram_sctp_prinfo);
1698 memcpy(ptr, &(data->prinfo), num);
1701 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1702 /* Returns the size of the copied struct. */
1703 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1704 num = sizeof(struct bio_dgram_sctp_prinfo);
1706 memcpy(&(data->prinfo), ptr, num);
1708 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1709 /* Returns always 1. */
1711 data->save_shutdown = 1;
1713 data->save_shutdown = 0;
1718 * Pass to default ctrl function to process SCTP unspecific commands
1720 ret = dgram_ctrl(b, cmd, num, ptr);
1726 int BIO_dgram_sctp_notification_cb(BIO *b,
1727 void (*handle_notifications) (BIO *bio,
1733 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1735 if (handle_notifications != NULL) {
1736 data->handle_notifications = handle_notifications;
1737 data->notification_context = context;
1745 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1746 * @b: The BIO to check for the dry event
1748 * Wait until the peer confirms all packets have been received, and so that
1749 * our kernel doesn't have anything to send anymore. This is only received by
1750 * the peer's kernel, not the application.
1754 * 0 when not dry yet
1757 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1760 int n, sockflags, ret;
1761 union sctp_notification snp;
1765 struct sctp_event event;
1767 struct sctp_event_subscribe event;
1768 socklen_t eventsize;
1770 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1772 /* set sender dry event */
1774 memset(&event, 0, sizeof(event));
1775 event.se_assoc_id = 0;
1776 event.se_type = SCTP_SENDER_DRY_EVENT;
1779 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1780 sizeof(struct sctp_event));
1782 eventsize = sizeof(struct sctp_event_subscribe);
1783 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1787 event.sctp_sender_dry_event = 1;
1790 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1791 sizeof(struct sctp_event_subscribe));
1796 /* peek for notification */
1797 memset(&snp, 0, sizeof(snp));
1798 iov.iov_base = (char *)&snp;
1799 iov.iov_len = sizeof(union sctp_notification);
1800 msg.msg_name = NULL;
1801 msg.msg_namelen = 0;
1804 msg.msg_control = NULL;
1805 msg.msg_controllen = 0;
1808 n = recvmsg(b->num, &msg, MSG_PEEK);
1810 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1811 && (get_last_socket_error() != EWOULDBLOCK))
1817 /* if we find a notification, process it and try again if necessary */
1818 while (msg.msg_flags & MSG_NOTIFICATION) {
1819 memset(&snp, 0, sizeof(snp));
1820 iov.iov_base = (char *)&snp;
1821 iov.iov_len = sizeof(union sctp_notification);
1822 msg.msg_name = NULL;
1823 msg.msg_namelen = 0;
1826 msg.msg_control = NULL;
1827 msg.msg_controllen = 0;
1830 n = recvmsg(b->num, &msg, 0);
1832 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1833 && (get_last_socket_error() != EWOULDBLOCK))
1839 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1842 /* disable sender dry event */
1844 memset(&event, 0, sizeof(event));
1845 event.se_assoc_id = 0;
1846 event.se_type = SCTP_SENDER_DRY_EVENT;
1849 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1850 sizeof(struct sctp_event));
1852 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1854 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1859 event.sctp_sender_dry_event = 0;
1862 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1863 sizeof(struct sctp_event_subscribe));
1868 # ifdef SCTP_AUTHENTICATION_EVENT
1869 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1870 dgram_sctp_handle_auth_free_key_event(b, &snp);
1873 if (data->handle_notifications != NULL)
1874 data->handle_notifications(b, data->notification_context,
1877 /* found notification, peek again */
1878 memset(&snp, 0, sizeof(snp));
1879 iov.iov_base = (char *)&snp;
1880 iov.iov_len = sizeof(union sctp_notification);
1881 msg.msg_name = NULL;
1882 msg.msg_namelen = 0;
1885 msg.msg_control = NULL;
1886 msg.msg_controllen = 0;
1889 /* if we have seen the dry already, don't wait */
1891 sockflags = fcntl(b->num, F_GETFL, 0);
1892 fcntl(b->num, F_SETFL, O_NONBLOCK);
1895 n = recvmsg(b->num, &msg, MSG_PEEK);
1898 fcntl(b->num, F_SETFL, sockflags);
1902 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1903 && (get_last_socket_error() != EWOULDBLOCK))
1910 /* read anything else */
1914 int BIO_dgram_sctp_msg_waiting(BIO *b)
1917 union sctp_notification snp;
1920 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1922 /* Check if there are any messages waiting to be read */
1924 memset(&snp, 0, sizeof(snp));
1925 iov.iov_base = (char *)&snp;
1926 iov.iov_len = sizeof(union sctp_notification);
1927 msg.msg_name = NULL;
1928 msg.msg_namelen = 0;
1931 msg.msg_control = NULL;
1932 msg.msg_controllen = 0;
1935 sockflags = fcntl(b->num, F_GETFL, 0);
1936 fcntl(b->num, F_SETFL, O_NONBLOCK);
1937 n = recvmsg(b->num, &msg, MSG_PEEK);
1938 fcntl(b->num, F_SETFL, sockflags);
1940 /* if notification, process and try again */
1941 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1942 # ifdef SCTP_AUTHENTICATION_EVENT
1943 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1944 dgram_sctp_handle_auth_free_key_event(b, &snp);
1947 memset(&snp, 0, sizeof(snp));
1948 iov.iov_base = (char *)&snp;
1949 iov.iov_len = sizeof(union sctp_notification);
1950 msg.msg_name = NULL;
1951 msg.msg_namelen = 0;
1954 msg.msg_control = NULL;
1955 msg.msg_controllen = 0;
1957 n = recvmsg(b->num, &msg, 0);
1959 if (data->handle_notifications != NULL)
1960 data->handle_notifications(b, data->notification_context,
1964 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1966 /* Return 1 if there is a message to be read, return 0 otherwise. */
1973 static int dgram_sctp_puts(BIO *bp, const char *str)
1978 ret = dgram_sctp_write(bp, str, n);
1983 static int BIO_dgram_should_retry(int i)
1987 if ((i == 0) || (i == -1)) {
1988 err = get_last_socket_error();
1990 # if defined(OPENSSL_SYS_WINDOWS)
1992 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1993 * this point, the error code was overwritten by another system call
1994 * before this error handling is called.
1998 return (BIO_dgram_non_fatal_error(err));
2003 int BIO_dgram_non_fatal_error(int err)
2006 # if defined(OPENSSL_SYS_WINDOWS)
2007 # if defined(WSAEWOULDBLOCK)
2008 case WSAEWOULDBLOCK:
2013 # ifdef WSAEWOULDBLOCK
2014 # if WSAEWOULDBLOCK != EWOULDBLOCK
2027 # if EWOULDBLOCK != EAGAIN
2052 static void get_current_time(struct timeval *t)
2054 # if defined(_WIN32)
2057 unsigned __int64 ul;
2062 SystemTimeToFileTime(&st, &now.ft);
2064 now.ul -= 116444736000000000ULL;
2066 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2068 t->tv_sec = (long)(now.ul / 10000000);
2069 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2070 # elif defined(OPENSSL_SYS_VMS)
2073 t->tv_sec = (long)tb.time;
2074 t->tv_usec = (long)tb.millitm * 1000;
2076 gettimeofday(t, NULL);