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;
522 b->num = *((int *)ptr);
523 b->shutdown = (int)num;
535 case BIO_CTRL_GET_CLOSE:
538 case BIO_CTRL_SET_CLOSE:
539 b->shutdown = (int)num;
541 case BIO_CTRL_PENDING:
542 case BIO_CTRL_WPENDING:
549 case BIO_CTRL_DGRAM_CONNECT:
550 to = (struct sockaddr *)ptr;
551 switch (to->sa_family) {
553 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
555 # if OPENSSL_USE_IPV6
557 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
561 memcpy(&data->peer, to, sizeof(data->peer.sa));
565 /* (Linux)kernel sets DF bit on outgoing IP packets */
566 case BIO_CTRL_DGRAM_MTU_DISCOVER:
567 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
568 addr_len = (socklen_t) sizeof(addr);
569 memset(&addr, 0, sizeof(addr));
570 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
574 switch (addr.sa.sa_family) {
576 sockopt_val = IP_PMTUDISC_DO;
577 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
578 &sockopt_val, sizeof(sockopt_val))) < 0)
579 perror("setsockopt");
581 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
583 sockopt_val = IPV6_PMTUDISC_DO;
584 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
585 &sockopt_val, sizeof(sockopt_val))) < 0)
586 perror("setsockopt");
597 case BIO_CTRL_DGRAM_QUERY_MTU:
598 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
599 addr_len = (socklen_t) sizeof(addr);
600 memset(&addr, 0, sizeof(addr));
601 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
605 sockopt_len = sizeof(sockopt_val);
606 switch (addr.sa.sa_family) {
609 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
610 &sockopt_len)) < 0 || sockopt_val < 0) {
614 * we assume that the transport protocol is UDP and no IP
617 data->mtu = sockopt_val - 8 - 20;
621 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
624 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
625 (void *)&sockopt_val, &sockopt_len)) < 0
626 || sockopt_val < 0) {
630 * we assume that the transport protocol is UDP and no IPV6
633 data->mtu = sockopt_val - 8 - 40;
646 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
647 ret = -dgram_get_mtu_overhead(data);
648 switch (data->peer.sa.sa_family) {
652 # if OPENSSL_USE_IPV6
654 # ifdef IN6_IS_ADDR_V4MAPPED
655 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
667 case BIO_CTRL_DGRAM_GET_MTU:
669 case BIO_CTRL_DGRAM_SET_MTU:
673 case BIO_CTRL_DGRAM_SET_CONNECTED:
674 to = (struct sockaddr *)ptr;
678 switch (to->sa_family) {
680 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
682 # if OPENSSL_USE_IPV6
684 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
688 memcpy(&data->peer, to, sizeof(data->peer.sa));
693 memset(&data->peer, 0, sizeof(data->peer));
696 case BIO_CTRL_DGRAM_GET_PEER:
697 switch (data->peer.sa.sa_family) {
699 ret = sizeof(data->peer.sa_in);
701 # if OPENSSL_USE_IPV6
703 ret = sizeof(data->peer.sa_in6);
707 ret = sizeof(data->peer.sa);
710 if (num == 0 || num > ret)
712 memcpy(ptr, &data->peer, (ret = num));
714 case BIO_CTRL_DGRAM_SET_PEER:
715 to = (struct sockaddr *)ptr;
716 switch (to->sa_family) {
718 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
720 # if OPENSSL_USE_IPV6
722 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
726 memcpy(&data->peer, to, sizeof(data->peer.sa));
730 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
731 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
733 # if defined(SO_RCVTIMEO)
734 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
735 # ifdef OPENSSL_SYS_WINDOWS
737 struct timeval *tv = (struct timeval *)ptr;
738 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
739 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
740 (void *)&timeout, sizeof(timeout)) < 0) {
741 perror("setsockopt");
746 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
747 sizeof(struct timeval)) < 0) {
748 perror("setsockopt");
753 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
761 # ifdef OPENSSL_SYS_WINDOWS
763 struct timeval *tv = (struct timeval *)ptr;
765 sz.i = sizeof(timeout);
766 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
767 (void *)&timeout, &sz.i) < 0) {
768 perror("getsockopt");
771 tv->tv_sec = timeout / 1000;
772 tv->tv_usec = (timeout % 1000) * 1000;
776 sz.i = sizeof(struct timeval);
777 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
778 ptr, (void *)&sz) < 0) {
779 perror("getsockopt");
781 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
782 OPENSSL_assert(sz.s <= sizeof(struct timeval));
790 # if defined(SO_SNDTIMEO)
791 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
792 # ifdef OPENSSL_SYS_WINDOWS
794 struct timeval *tv = (struct timeval *)ptr;
795 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
796 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
797 (void *)&timeout, sizeof(timeout)) < 0) {
798 perror("setsockopt");
803 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
804 sizeof(struct timeval)) < 0) {
805 perror("setsockopt");
810 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
818 # ifdef OPENSSL_SYS_WINDOWS
820 struct timeval *tv = (struct timeval *)ptr;
822 sz.i = sizeof(timeout);
823 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
824 (void *)&timeout, &sz.i) < 0) {
825 perror("getsockopt");
828 tv->tv_sec = timeout / 1000;
829 tv->tv_usec = (timeout % 1000) * 1000;
833 sz.i = sizeof(struct timeval);
834 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
835 ptr, (void *)&sz) < 0) {
836 perror("getsockopt");
838 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
839 OPENSSL_assert(sz.s <= sizeof(struct timeval));
847 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
849 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
850 # ifdef OPENSSL_SYS_WINDOWS
851 if (data->_errno == WSAETIMEDOUT)
853 if (data->_errno == EAGAIN)
862 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
863 if (data->_errno == EMSGSIZE) {
870 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
871 sockopt_val = num ? 1 : 0;
873 switch (data->peer.sa.sa_family) {
875 # if defined(IP_DONTFRAG)
876 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
877 &sockopt_val, sizeof(sockopt_val))) < 0) {
878 perror("setsockopt");
881 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
882 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
883 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
884 &sockopt_val, sizeof(sockopt_val))) < 0) {
885 perror("setsockopt");
888 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
889 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
890 (const char *)&sockopt_val,
891 sizeof(sockopt_val))) < 0) {
892 perror("setsockopt");
899 # if OPENSSL_USE_IPV6
901 # if defined(IPV6_DONTFRAG)
902 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
903 (const void *)&sockopt_val,
904 sizeof(sockopt_val))) < 0) {
905 perror("setsockopt");
908 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
909 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
910 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
911 &sockopt_val, sizeof(sockopt_val))) < 0) {
912 perror("setsockopt");
925 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
926 ret = dgram_get_mtu_overhead(data);
928 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
929 data->peekmode = (unsigned int)num;
938 static int dgram_puts(BIO *bp, const char *str)
943 ret = dgram_write(bp, str, n);
947 # ifndef OPENSSL_NO_SCTP
948 BIO_METHOD *BIO_s_datagram_sctp(void)
950 return (&methods_dgramp_sctp);
953 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
956 int ret, optval = 20000;
957 int auth_data = 0, auth_forward = 0;
959 struct sctp_authchunk auth;
960 struct sctp_authchunks *authchunks;
961 socklen_t sockopt_len;
962 # ifdef SCTP_AUTHENTICATION_EVENT
964 struct sctp_event event;
966 struct sctp_event_subscribe event;
970 bio = BIO_new(BIO_s_datagram_sctp());
973 BIO_set_fd(bio, fd, close_flag);
975 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
976 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
978 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
979 sizeof(struct sctp_authchunk));
984 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
986 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
987 sizeof(struct sctp_authchunk));
994 * Test if activation was successful. When using accept(), SCTP-AUTH has
995 * to be activated for the listening socket already, otherwise the
996 * connected socket won't use it.
998 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
999 authchunks = OPENSSL_zalloc(sockopt_len);
1000 if (authchunks == NULL) {
1004 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1007 OPENSSL_free(authchunks);
1012 for (p = (unsigned char *)authchunks->gauth_chunks;
1013 p < (unsigned char *)authchunks + sockopt_len;
1014 p += sizeof(uint8_t)) {
1015 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1017 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1021 OPENSSL_free(authchunks);
1023 OPENSSL_assert(auth_data);
1024 OPENSSL_assert(auth_forward);
1026 # ifdef SCTP_AUTHENTICATION_EVENT
1028 memset(&event, 0, sizeof(event));
1029 event.se_assoc_id = 0;
1030 event.se_type = SCTP_AUTHENTICATION_EVENT;
1033 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1034 sizeof(struct sctp_event));
1040 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1041 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1047 event.sctp_authentication_event = 1;
1050 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1051 sizeof(struct sctp_event_subscribe));
1060 * Disable partial delivery by setting the min size larger than the max
1061 * record size of 2^14 + 2048 + 13
1064 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1074 int BIO_dgram_is_sctp(BIO *bio)
1076 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1079 static int dgram_sctp_new(BIO *bi)
1081 bio_dgram_sctp_data *data = NULL;
1085 data = OPENSSL_zalloc(sizeof(*data));
1088 # ifdef SCTP_PR_SCTP_NONE
1089 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1097 static int dgram_sctp_free(BIO *a)
1099 bio_dgram_sctp_data *data;
1103 if (!dgram_clear(a))
1106 data = (bio_dgram_sctp_data *) a->ptr;
1108 OPENSSL_free(data->saved_message.data);
1115 # ifdef SCTP_AUTHENTICATION_EVENT
1116 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1117 union sctp_notification *snp)
1120 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1122 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1123 struct sctp_authkeyid authkeyid;
1126 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1127 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1128 &authkeyid, sizeof(struct sctp_authkeyid));
1133 static int dgram_sctp_read(BIO *b, char *out, int outl)
1135 int ret = 0, n = 0, i, optval;
1137 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1138 union sctp_notification *snp;
1141 struct cmsghdr *cmsg;
1145 clear_socket_error();
1148 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1151 msg.msg_name = NULL;
1152 msg.msg_namelen = 0;
1155 msg.msg_control = cmsgbuf;
1156 msg.msg_controllen = 512;
1158 n = recvmsg(b->num, &msg, 0);
1166 if (msg.msg_controllen > 0) {
1167 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1168 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1169 if (cmsg->cmsg_level != IPPROTO_SCTP)
1171 # ifdef SCTP_RCVINFO
1172 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1173 struct sctp_rcvinfo *rcvinfo;
1175 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1176 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1177 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1178 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1179 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1180 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1181 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1182 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1186 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1187 struct sctp_sndrcvinfo *sndrcvinfo;
1190 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1191 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1192 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1193 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1194 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1195 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1196 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1197 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1203 if (msg.msg_flags & MSG_NOTIFICATION) {
1204 snp = (union sctp_notification *)out;
1205 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1207 struct sctp_event event;
1209 struct sctp_event_subscribe event;
1210 socklen_t eventsize;
1213 * If a message has been delayed until the socket is dry,
1214 * it can be sent now.
1216 if (data->saved_message.length > 0) {
1217 i = dgram_sctp_write(data->saved_message.bio,
1218 data->saved_message.data,
1219 data->saved_message.length);
1224 OPENSSL_free(data->saved_message.data);
1225 data->saved_message.data = NULL;
1226 data->saved_message.length = 0;
1229 /* disable sender dry event */
1231 memset(&event, 0, sizeof(event));
1232 event.se_assoc_id = 0;
1233 event.se_type = SCTP_SENDER_DRY_EVENT;
1235 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1236 sizeof(struct sctp_event));
1242 eventsize = sizeof(struct sctp_event_subscribe);
1243 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1250 event.sctp_sender_dry_event = 0;
1252 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1253 sizeof(struct sctp_event_subscribe));
1260 # ifdef SCTP_AUTHENTICATION_EVENT
1261 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1262 dgram_sctp_handle_auth_free_key_event(b, snp);
1265 if (data->handle_notifications != NULL)
1266 data->handle_notifications(b, data->notification_context,
1269 memset(out, 0, outl);
1273 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1276 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1277 /* Partial message read, this should never happen! */
1280 * The buffer was too small, this means the peer sent a message
1281 * that was larger than allowed.
1287 * Test if socket buffer can handle max record size (2^14 + 2048
1290 optlen = (socklen_t) sizeof(int);
1291 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1293 OPENSSL_assert(optval >= 18445);
1296 * Test if SCTP doesn't partially deliver below max record size
1297 * (2^14 + 2048 + 13)
1299 optlen = (socklen_t) sizeof(int);
1301 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1304 OPENSSL_assert(optval >= 18445);
1307 * Partially delivered notification??? Probably a bug....
1309 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1312 * Everything seems ok till now, so it's most likely a message
1313 * dropped by PR-SCTP.
1315 memset(out, 0, outl);
1316 BIO_set_retry_read(b);
1320 BIO_clear_retry_flags(b);
1322 if (BIO_dgram_should_retry(ret)) {
1323 BIO_set_retry_read(b);
1324 data->_errno = get_last_socket_error();
1328 /* Test if peer uses SCTP-AUTH before continuing */
1329 if (!data->peer_auth_tested) {
1330 int ii, auth_data = 0, auth_forward = 0;
1332 struct sctp_authchunks *authchunks;
1335 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1336 authchunks = OPENSSL_malloc(optlen);
1337 if (authchunks == NULL) {
1338 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1341 memset(authchunks, 0, optlen);
1342 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1343 authchunks, &optlen);
1346 for (p = (unsigned char *)authchunks->gauth_chunks;
1347 p < (unsigned char *)authchunks + optlen;
1348 p += sizeof(uint8_t)) {
1349 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1351 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1355 OPENSSL_free(authchunks);
1357 if (!auth_data || !auth_forward) {
1358 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1362 data->peer_auth_tested = 1;
1369 * dgram_sctp_write - send message on SCTP socket
1370 * @b: BIO to write to
1372 * @inl: amount of bytes in @in to send
1374 * Returns -1 on error or the sent amount of bytes on success
1376 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1379 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1380 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1381 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1382 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1383 struct iovec iov[1];
1385 struct cmsghdr *cmsg;
1386 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1387 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1388 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1389 struct sctp_sndinfo *sndinfo;
1390 struct sctp_prinfo *prinfo;
1392 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1393 struct sctp_sndrcvinfo *sndrcvinfo;
1396 clear_socket_error();
1399 * If we're send anything else than application data, disable all user
1400 * parameters and flags.
1403 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1404 # ifdef SCTP_SACK_IMMEDIATELY
1405 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1407 sinfo = &handshake_sinfo;
1411 * If we have to send a shutdown alert message and the socket is not dry
1412 * yet, we have to save it and send it as soon as the socket gets dry.
1414 if (data->save_shutdown) {
1415 ret = BIO_dgram_sctp_wait_for_dry(b);
1421 data->saved_message.bio = b;
1422 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1423 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1426 OPENSSL_free(data->saved_message.data);
1427 data->saved_message.data = tmp;
1428 memcpy(data->saved_message.data, in, inl);
1429 data->saved_message.length = inl;
1434 iov[0].iov_base = (char *)in;
1435 iov[0].iov_len = inl;
1436 msg.msg_name = NULL;
1437 msg.msg_namelen = 0;
1440 msg.msg_control = (caddr_t) cmsgbuf;
1441 msg.msg_controllen = 0;
1443 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1444 cmsg = (struct cmsghdr *)cmsgbuf;
1445 cmsg->cmsg_level = IPPROTO_SCTP;
1446 cmsg->cmsg_type = SCTP_SNDINFO;
1447 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1448 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1449 memset(sndinfo, 0, sizeof(*sndinfo));
1450 sndinfo->snd_sid = sinfo->snd_sid;
1451 sndinfo->snd_flags = sinfo->snd_flags;
1452 sndinfo->snd_ppid = sinfo->snd_ppid;
1453 sndinfo->snd_context = sinfo->snd_context;
1454 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1457 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1458 cmsg->cmsg_level = IPPROTO_SCTP;
1459 cmsg->cmsg_type = SCTP_PRINFO;
1460 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1461 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1462 memset(prinfo, 0, sizeof(*prinfo));
1463 prinfo->pr_policy = pinfo->pr_policy;
1464 prinfo->pr_value = pinfo->pr_value;
1465 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1467 cmsg = (struct cmsghdr *)cmsgbuf;
1468 cmsg->cmsg_level = IPPROTO_SCTP;
1469 cmsg->cmsg_type = SCTP_SNDRCV;
1470 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1471 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1472 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1473 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1474 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1476 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1478 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1479 sndrcvinfo->sinfo_context = sinfo->snd_context;
1480 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1481 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1484 ret = sendmsg(b->num, &msg, 0);
1486 BIO_clear_retry_flags(b);
1488 if (BIO_dgram_should_retry(ret)) {
1489 BIO_set_retry_write(b);
1490 data->_errno = get_last_socket_error();
1496 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1499 bio_dgram_sctp_data *data = NULL;
1500 socklen_t sockopt_len = 0;
1501 struct sctp_authkeyid authkeyid;
1502 struct sctp_authkey *authkey = NULL;
1504 data = (bio_dgram_sctp_data *) b->ptr;
1507 case BIO_CTRL_DGRAM_QUERY_MTU:
1509 * Set to maximum (2^14) and ignore user input to enable transport
1510 * protocol fragmentation. Returns always 2^14.
1515 case BIO_CTRL_DGRAM_SET_MTU:
1517 * Set to maximum (2^14) and ignore input to enable transport
1518 * protocol fragmentation. Returns always 2^14.
1523 case BIO_CTRL_DGRAM_SET_CONNECTED:
1524 case BIO_CTRL_DGRAM_CONNECT:
1525 /* Returns always -1. */
1528 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1530 * SCTP doesn't need the DTLS timer Returns always 1.
1533 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1535 * We allow transport protocol fragmentation so this is irrelevant
1539 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1541 data->in_handshake = 1;
1543 data->in_handshake = 0;
1546 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1547 &data->in_handshake, sizeof(int));
1549 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1551 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1554 /* Get active key */
1555 sockopt_len = sizeof(struct sctp_authkeyid);
1557 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1563 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1564 authkey = OPENSSL_malloc(sockopt_len);
1565 if (authkey == NULL) {
1569 memset(authkey, 0, sockopt_len);
1570 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1571 # ifndef __FreeBSD__
1573 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1574 * and higher work without it.
1576 authkey->sca_keylength = 64;
1578 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1581 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1583 OPENSSL_free(authkey);
1588 /* Reset active key */
1589 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1590 &authkeyid, sizeof(struct sctp_authkeyid));
1595 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1596 /* Returns 0 on success, -1 otherwise. */
1598 /* Get active key */
1599 sockopt_len = sizeof(struct sctp_authkeyid);
1601 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1606 /* Set active key */
1607 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1608 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1609 &authkeyid, sizeof(struct sctp_authkeyid));
1614 * CCS has been sent, so remember that and fall through to check if
1615 * we need to deactivate an old key
1619 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1620 /* Returns 0 on success, -1 otherwise. */
1623 * Has this command really been called or is this just a
1626 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1630 * CSS has been both, received and sent, so deactivate an old key
1632 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1633 /* Get active key */
1634 sockopt_len = sizeof(struct sctp_authkeyid);
1636 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1637 &authkeyid, &sockopt_len);
1642 * Deactivate key or delete second last key if
1643 * SCTP_AUTHENTICATION_EVENT is not available.
1645 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1646 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1647 sockopt_len = sizeof(struct sctp_authkeyid);
1648 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1649 &authkeyid, sockopt_len);
1653 # ifndef SCTP_AUTHENTICATION_EVENT
1654 if (authkeyid.scact_keynumber > 0) {
1655 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1656 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1657 &authkeyid, sizeof(struct sctp_authkeyid));
1667 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1668 /* Returns the size of the copied struct. */
1669 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1670 num = sizeof(struct bio_dgram_sctp_sndinfo);
1672 memcpy(ptr, &(data->sndinfo), num);
1675 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1676 /* Returns the size of the copied struct. */
1677 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1678 num = sizeof(struct bio_dgram_sctp_sndinfo);
1680 memcpy(&(data->sndinfo), ptr, num);
1682 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1683 /* Returns the size of the copied struct. */
1684 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1685 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1687 memcpy(ptr, &data->rcvinfo, num);
1691 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1692 /* Returns the size of the copied struct. */
1693 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1694 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1696 memcpy(&(data->rcvinfo), ptr, num);
1698 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1699 /* Returns the size of the copied struct. */
1700 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1701 num = sizeof(struct bio_dgram_sctp_prinfo);
1703 memcpy(ptr, &(data->prinfo), num);
1706 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1707 /* Returns the size of the copied struct. */
1708 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1709 num = sizeof(struct bio_dgram_sctp_prinfo);
1711 memcpy(&(data->prinfo), ptr, num);
1713 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1714 /* Returns always 1. */
1716 data->save_shutdown = 1;
1718 data->save_shutdown = 0;
1723 * Pass to default ctrl function to process SCTP unspecific commands
1725 ret = dgram_ctrl(b, cmd, num, ptr);
1731 int BIO_dgram_sctp_notification_cb(BIO *b,
1732 void (*handle_notifications) (BIO *bio,
1738 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1740 if (handle_notifications != NULL) {
1741 data->handle_notifications = handle_notifications;
1742 data->notification_context = context;
1750 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1751 * @b: The BIO to check for the dry event
1753 * Wait until the peer confirms all packets have been received, and so that
1754 * our kernel doesn't have anything to send anymore. This is only received by
1755 * the peer's kernel, not the application.
1759 * 0 when not dry yet
1762 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1765 int n, sockflags, ret;
1766 union sctp_notification snp;
1770 struct sctp_event event;
1772 struct sctp_event_subscribe event;
1773 socklen_t eventsize;
1775 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1777 /* set sender dry event */
1779 memset(&event, 0, sizeof(event));
1780 event.se_assoc_id = 0;
1781 event.se_type = SCTP_SENDER_DRY_EVENT;
1784 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1785 sizeof(struct sctp_event));
1787 eventsize = sizeof(struct sctp_event_subscribe);
1788 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1792 event.sctp_sender_dry_event = 1;
1795 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1796 sizeof(struct sctp_event_subscribe));
1801 /* peek for notification */
1802 memset(&snp, 0, sizeof(snp));
1803 iov.iov_base = (char *)&snp;
1804 iov.iov_len = sizeof(union sctp_notification);
1805 msg.msg_name = NULL;
1806 msg.msg_namelen = 0;
1809 msg.msg_control = NULL;
1810 msg.msg_controllen = 0;
1813 n = recvmsg(b->num, &msg, MSG_PEEK);
1815 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1816 && (get_last_socket_error() != EWOULDBLOCK))
1822 /* if we find a notification, process it and try again if necessary */
1823 while (msg.msg_flags & MSG_NOTIFICATION) {
1824 memset(&snp, 0, sizeof(snp));
1825 iov.iov_base = (char *)&snp;
1826 iov.iov_len = sizeof(union sctp_notification);
1827 msg.msg_name = NULL;
1828 msg.msg_namelen = 0;
1831 msg.msg_control = NULL;
1832 msg.msg_controllen = 0;
1835 n = recvmsg(b->num, &msg, 0);
1837 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1838 && (get_last_socket_error() != EWOULDBLOCK))
1844 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1847 /* disable sender dry event */
1849 memset(&event, 0, sizeof(event));
1850 event.se_assoc_id = 0;
1851 event.se_type = SCTP_SENDER_DRY_EVENT;
1854 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1855 sizeof(struct sctp_event));
1857 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1859 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1864 event.sctp_sender_dry_event = 0;
1867 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1868 sizeof(struct sctp_event_subscribe));
1873 # ifdef SCTP_AUTHENTICATION_EVENT
1874 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1875 dgram_sctp_handle_auth_free_key_event(b, &snp);
1878 if (data->handle_notifications != NULL)
1879 data->handle_notifications(b, data->notification_context,
1882 /* found notification, peek again */
1883 memset(&snp, 0, sizeof(snp));
1884 iov.iov_base = (char *)&snp;
1885 iov.iov_len = sizeof(union sctp_notification);
1886 msg.msg_name = NULL;
1887 msg.msg_namelen = 0;
1890 msg.msg_control = NULL;
1891 msg.msg_controllen = 0;
1894 /* if we have seen the dry already, don't wait */
1896 sockflags = fcntl(b->num, F_GETFL, 0);
1897 fcntl(b->num, F_SETFL, O_NONBLOCK);
1900 n = recvmsg(b->num, &msg, MSG_PEEK);
1903 fcntl(b->num, F_SETFL, sockflags);
1907 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1908 && (get_last_socket_error() != EWOULDBLOCK))
1915 /* read anything else */
1919 int BIO_dgram_sctp_msg_waiting(BIO *b)
1922 union sctp_notification snp;
1925 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1927 /* Check if there are any messages waiting to be read */
1929 memset(&snp, 0, sizeof(snp));
1930 iov.iov_base = (char *)&snp;
1931 iov.iov_len = sizeof(union sctp_notification);
1932 msg.msg_name = NULL;
1933 msg.msg_namelen = 0;
1936 msg.msg_control = NULL;
1937 msg.msg_controllen = 0;
1940 sockflags = fcntl(b->num, F_GETFL, 0);
1941 fcntl(b->num, F_SETFL, O_NONBLOCK);
1942 n = recvmsg(b->num, &msg, MSG_PEEK);
1943 fcntl(b->num, F_SETFL, sockflags);
1945 /* if notification, process and try again */
1946 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1947 # ifdef SCTP_AUTHENTICATION_EVENT
1948 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1949 dgram_sctp_handle_auth_free_key_event(b, &snp);
1952 memset(&snp, 0, sizeof(snp));
1953 iov.iov_base = (char *)&snp;
1954 iov.iov_len = sizeof(union sctp_notification);
1955 msg.msg_name = NULL;
1956 msg.msg_namelen = 0;
1959 msg.msg_control = NULL;
1960 msg.msg_controllen = 0;
1962 n = recvmsg(b->num, &msg, 0);
1964 if (data->handle_notifications != NULL)
1965 data->handle_notifications(b, data->notification_context,
1969 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1971 /* Return 1 if there is a message to be read, return 0 otherwise. */
1978 static int dgram_sctp_puts(BIO *bp, const char *str)
1983 ret = dgram_sctp_write(bp, str, n);
1988 static int BIO_dgram_should_retry(int i)
1992 if ((i == 0) || (i == -1)) {
1993 err = get_last_socket_error();
1995 # if defined(OPENSSL_SYS_WINDOWS)
1997 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1998 * this point, the error code was overwritten by another system call
1999 * before this error handling is called.
2003 return (BIO_dgram_non_fatal_error(err));
2008 int BIO_dgram_non_fatal_error(int err)
2011 # if defined(OPENSSL_SYS_WINDOWS)
2012 # if defined(WSAEWOULDBLOCK)
2013 case WSAEWOULDBLOCK:
2018 # ifdef WSAEWOULDBLOCK
2019 # if WSAEWOULDBLOCK != EWOULDBLOCK
2032 # if EWOULDBLOCK != EAGAIN
2057 static void get_current_time(struct timeval *t)
2059 # if defined(_WIN32)
2062 unsigned __int64 ul;
2067 SystemTimeToFileTime(&st, &now.ft);
2069 now.ul -= 116444736000000000ULL;
2071 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2073 t->tv_sec = (long)(now.ul / 10000000);
2074 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2075 # elif defined(OPENSSL_SYS_VMS)
2078 t->tv_sec = (long)tb.time;
2079 t->tv_usec = (long)tb.millitm * 1000;
2081 gettimeofday(t, NULL);