2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 /* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #ifndef OPENSSL_NO_DGRAM
65 # if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
66 # include <sys/time.h>
68 # if defined(OPENSSL_SYS_VMS)
69 # include <sys/timeb.h>
72 # ifndef OPENSSL_NO_SCTP
73 # include <netinet/sctp.h>
75 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
76 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
79 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
80 # define IP_MTU 14 /* linux is lame */
83 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
84 # define IPPROTO_IPV6 41 /* windows is lame */
87 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
88 /* Standard definition causes type-punning problems. */
89 # undef IN6_IS_ADDR_V4MAPPED
90 # define s6_addr32 __u6_addr.__u6_addr32
91 # define IN6_IS_ADDR_V4MAPPED(a) \
92 (((a)->s6_addr32[0] == 0) && \
93 ((a)->s6_addr32[1] == 0) && \
94 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
98 # define sock_write SockWrite /* Watt-32 uses same names */
99 # define sock_read SockRead
100 # define sock_puts SockPuts
103 static int dgram_write(BIO *h, const char *buf, int num);
104 static int dgram_read(BIO *h, char *buf, int size);
105 static int dgram_puts(BIO *h, const char *str);
106 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
107 static int dgram_new(BIO *h);
108 static int dgram_free(BIO *data);
109 static int dgram_clear(BIO *bio);
111 # ifndef OPENSSL_NO_SCTP
112 static int dgram_sctp_write(BIO *h, const char *buf, int num);
113 static int dgram_sctp_read(BIO *h, char *buf, int size);
114 static int dgram_sctp_puts(BIO *h, const char *str);
115 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
116 static int dgram_sctp_new(BIO *h);
117 static int dgram_sctp_free(BIO *data);
118 # ifdef SCTP_AUTHENTICATION_EVENT
119 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
124 static int BIO_dgram_should_retry(int s);
126 static void get_current_time(struct timeval *t);
128 static BIO_METHOD methods_dgramp = {
134 NULL, /* dgram_gets, */
141 # ifndef OPENSSL_NO_SCTP
142 static BIO_METHOD methods_dgramp_sctp = {
144 "datagram sctp socket",
148 NULL, /* dgram_gets, */
156 typedef struct bio_dgram_data_st {
158 unsigned int connected;
161 struct timeval next_timeout;
162 struct timeval socket_timeout;
163 unsigned int peekmode;
166 # ifndef OPENSSL_NO_SCTP
167 typedef struct bio_dgram_sctp_save_message_st {
171 } bio_dgram_sctp_save_message;
173 typedef struct bio_dgram_sctp_data_st {
175 unsigned int connected;
178 struct bio_dgram_sctp_sndinfo sndinfo;
179 struct bio_dgram_sctp_rcvinfo rcvinfo;
180 struct bio_dgram_sctp_prinfo prinfo;
181 void (*handle_notifications) (BIO *bio, void *context, void *buf);
182 void *notification_context;
187 int peer_auth_tested;
188 bio_dgram_sctp_save_message saved_message;
189 } bio_dgram_sctp_data;
192 BIO_METHOD *BIO_s_datagram(void)
194 return (&methods_dgramp);
197 BIO *BIO_new_dgram(int fd, int close_flag)
201 ret = BIO_new(BIO_s_datagram());
204 BIO_set_fd(ret, fd, close_flag);
208 static int dgram_new(BIO *bi)
210 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
218 static int dgram_free(BIO *a)
220 bio_dgram_data *data;
227 data = (bio_dgram_data *)a->ptr;
233 static int dgram_clear(BIO *a)
247 static void dgram_adjust_rcv_timeout(BIO *b)
249 # if defined(SO_RCVTIMEO)
250 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
258 /* Is a timer active? */
259 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
260 struct timeval timenow, timeleft;
262 /* Read current socket timeout */
263 # ifdef OPENSSL_SYS_WINDOWS
266 sz.i = sizeof(timeout);
267 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
268 (void *)&timeout, &sz.i) < 0) {
269 perror("getsockopt");
271 data->socket_timeout.tv_sec = timeout / 1000;
272 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
275 sz.i = sizeof(data->socket_timeout);
276 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
277 &(data->socket_timeout), (void *)&sz) < 0) {
278 perror("getsockopt");
279 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
280 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
283 /* Get current time */
284 get_current_time(&timenow);
286 /* Calculate time left until timer expires */
287 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
288 if (timeleft.tv_usec < timenow.tv_usec) {
289 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
292 timeleft.tv_usec -= timenow.tv_usec;
294 if (timeleft.tv_sec < timenow.tv_sec) {
296 timeleft.tv_usec = 1;
298 timeleft.tv_sec -= timenow.tv_sec;
302 * Adjust socket timeout if next handshake message timer will expire
305 if ((data->socket_timeout.tv_sec == 0
306 && data->socket_timeout.tv_usec == 0)
307 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
308 || (data->socket_timeout.tv_sec == timeleft.tv_sec
309 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
310 # ifdef OPENSSL_SYS_WINDOWS
311 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
312 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
313 (void *)&timeout, sizeof(timeout)) < 0) {
314 perror("setsockopt");
317 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
318 sizeof(struct timeval)) < 0) {
319 perror("setsockopt");
327 static void dgram_reset_rcv_timeout(BIO *b)
329 # if defined(SO_RCVTIMEO)
330 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
332 /* Is a timer active? */
333 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
334 # ifdef OPENSSL_SYS_WINDOWS
335 int timeout = data->socket_timeout.tv_sec * 1000 +
336 data->socket_timeout.tv_usec / 1000;
337 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
338 (void *)&timeout, sizeof(timeout)) < 0) {
339 perror("setsockopt");
343 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
344 sizeof(struct timeval)) < 0) {
345 perror("setsockopt");
352 static int dgram_read(BIO *b, char *out, int outl)
355 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
359 socklen_t len = sizeof(peer);
362 clear_socket_error();
363 memset(&peer, 0, sizeof(peer));
364 dgram_adjust_rcv_timeout(b);
367 ret = recvfrom(b->num, out, outl, flags,
368 BIO_ADDR_sockaddr_noconst(&peer), &len);
370 if (!data->connected && ret >= 0)
371 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
373 BIO_clear_retry_flags(b);
375 if (BIO_dgram_should_retry(ret)) {
376 BIO_set_retry_read(b);
377 data->_errno = get_last_socket_error();
381 dgram_reset_rcv_timeout(b);
386 static int dgram_write(BIO *b, const char *in, int inl)
389 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
390 clear_socket_error();
393 ret = writesocket(b->num, in, inl);
395 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
397 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
398 ret = sendto(b->num, (char *)in, inl, 0,
399 BIO_ADDR_sockaddr(&data->peer), peerlen);
401 ret = sendto(b->num, in, inl, 0,
402 BIO_ADDR_sockaddr(&data->peer), peerlen);
406 BIO_clear_retry_flags(b);
408 if (BIO_dgram_should_retry(ret)) {
409 BIO_set_retry_write(b);
410 data->_errno = get_last_socket_error();
416 static long dgram_get_mtu_overhead(bio_dgram_data *data)
420 switch (BIO_ADDR_family(&data->peer)) {
423 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
430 # ifdef IN6_IS_ADDR_V4MAPPED
431 struct in6_addr tmp_addr;
432 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
433 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
435 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
441 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
448 /* We don't know. Go with the historical default */
455 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
459 bio_dgram_data *data = NULL;
462 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
463 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
464 * modern enough to define socklen_t */
469 data = (bio_dgram_data *)b->ptr;
481 b->num = *((int *)ptr);
482 b->shutdown = (int)num;
494 case BIO_CTRL_GET_CLOSE:
497 case BIO_CTRL_SET_CLOSE:
498 b->shutdown = (int)num;
500 case BIO_CTRL_PENDING:
501 case BIO_CTRL_WPENDING:
508 case BIO_CTRL_DGRAM_CONNECT:
509 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
511 /* (Linux)kernel sets DF bit on outgoing IP packets */
512 case BIO_CTRL_DGRAM_MTU_DISCOVER:
513 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
514 addr_len = (socklen_t) sizeof(addr);
515 memset(&addr, 0, sizeof(addr));
516 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
520 switch (addr.sa.sa_family) {
522 sockopt_val = IP_PMTUDISC_DO;
523 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
524 &sockopt_val, sizeof(sockopt_val))) < 0)
525 perror("setsockopt");
527 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
529 sockopt_val = IPV6_PMTUDISC_DO;
530 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
531 &sockopt_val, sizeof(sockopt_val))) < 0)
532 perror("setsockopt");
543 case BIO_CTRL_DGRAM_QUERY_MTU:
544 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
545 addr_len = (socklen_t) sizeof(addr);
546 memset(&addr, 0, sizeof(addr));
547 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
551 sockopt_len = sizeof(sockopt_val);
552 switch (addr.sa.sa_family) {
555 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
556 &sockopt_len)) < 0 || sockopt_val < 0) {
560 * we assume that the transport protocol is UDP and no IP
563 data->mtu = sockopt_val - 8 - 20;
567 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
570 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
571 (void *)&sockopt_val, &sockopt_len)) < 0
572 || sockopt_val < 0) {
576 * we assume that the transport protocol is UDP and no IPV6
579 data->mtu = sockopt_val - 8 - 40;
592 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
593 ret = -dgram_get_mtu_overhead(data);
594 switch (BIO_ADDR_family(&data->peer)) {
598 # if OPENSSL_USE_IPV6
601 # ifdef IN6_IS_ADDR_V4MAPPED
602 struct in6_addr tmp_addr;
603 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
604 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
617 case BIO_CTRL_DGRAM_GET_MTU:
619 case BIO_CTRL_DGRAM_SET_MTU:
623 case BIO_CTRL_DGRAM_SET_CONNECTED:
626 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
629 memset(&data->peer, 0, sizeof(data->peer));
632 case BIO_CTRL_DGRAM_GET_PEER:
633 ret = BIO_ADDR_sockaddr_size(&data->peer);
634 /* FIXME: if num < ret, we will only return part of an address.
635 That should bee an error, no? */
636 if (num == 0 || num > ret)
638 memcpy(ptr, &data->peer, (ret = num));
640 case BIO_CTRL_DGRAM_SET_PEER:
641 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
643 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
644 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
646 # if defined(SO_RCVTIMEO)
647 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
648 # ifdef OPENSSL_SYS_WINDOWS
650 struct timeval *tv = (struct timeval *)ptr;
651 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
652 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
653 (void *)&timeout, sizeof(timeout)) < 0) {
654 perror("setsockopt");
659 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
660 sizeof(struct timeval)) < 0) {
661 perror("setsockopt");
666 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
674 # ifdef OPENSSL_SYS_WINDOWS
676 struct timeval *tv = (struct timeval *)ptr;
678 sz.i = sizeof(timeout);
679 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
680 (void *)&timeout, &sz.i) < 0) {
681 perror("getsockopt");
684 tv->tv_sec = timeout / 1000;
685 tv->tv_usec = (timeout % 1000) * 1000;
689 sz.i = sizeof(struct timeval);
690 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
691 ptr, (void *)&sz) < 0) {
692 perror("getsockopt");
694 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
695 OPENSSL_assert(sz.s <= sizeof(struct timeval));
703 # if defined(SO_SNDTIMEO)
704 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
705 # ifdef OPENSSL_SYS_WINDOWS
707 struct timeval *tv = (struct timeval *)ptr;
708 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
709 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
710 (void *)&timeout, sizeof(timeout)) < 0) {
711 perror("setsockopt");
716 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
717 sizeof(struct timeval)) < 0) {
718 perror("setsockopt");
723 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
731 # ifdef OPENSSL_SYS_WINDOWS
733 struct timeval *tv = (struct timeval *)ptr;
735 sz.i = sizeof(timeout);
736 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
737 (void *)&timeout, &sz.i) < 0) {
738 perror("getsockopt");
741 tv->tv_sec = timeout / 1000;
742 tv->tv_usec = (timeout % 1000) * 1000;
746 sz.i = sizeof(struct timeval);
747 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
748 ptr, (void *)&sz) < 0) {
749 perror("getsockopt");
751 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
752 OPENSSL_assert(sz.s <= sizeof(struct timeval));
760 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
762 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
763 # ifdef OPENSSL_SYS_WINDOWS
764 d_errno = (data->_errno == WSAETIMEDOUT);
766 d_errno = (data->_errno == EAGAIN);
775 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
776 if (data->_errno == EMSGSIZE) {
783 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
784 sockopt_val = num ? 1 : 0;
786 switch (data->peer.sa.sa_family) {
788 # if defined(IP_DONTFRAG)
789 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
790 &sockopt_val, sizeof(sockopt_val))) < 0) {
791 perror("setsockopt");
794 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
795 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
796 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
797 &sockopt_val, sizeof(sockopt_val))) < 0) {
798 perror("setsockopt");
801 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
802 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
803 (const char *)&sockopt_val,
804 sizeof(sockopt_val))) < 0) {
805 perror("setsockopt");
812 # if OPENSSL_USE_IPV6
814 # if defined(IPV6_DONTFRAG)
815 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
816 (const void *)&sockopt_val,
817 sizeof(sockopt_val))) < 0) {
818 perror("setsockopt");
821 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
822 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
823 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
824 &sockopt_val, sizeof(sockopt_val))) < 0) {
825 perror("setsockopt");
838 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
839 ret = dgram_get_mtu_overhead(data);
841 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
842 data->peekmode = (unsigned int)num;
851 static int dgram_puts(BIO *bp, const char *str)
856 ret = dgram_write(bp, str, n);
860 # ifndef OPENSSL_NO_SCTP
861 BIO_METHOD *BIO_s_datagram_sctp(void)
863 return (&methods_dgramp_sctp);
866 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
869 int ret, optval = 20000;
870 int auth_data = 0, auth_forward = 0;
872 struct sctp_authchunk auth;
873 struct sctp_authchunks *authchunks;
874 socklen_t sockopt_len;
875 # ifdef SCTP_AUTHENTICATION_EVENT
877 struct sctp_event event;
879 struct sctp_event_subscribe event;
883 bio = BIO_new(BIO_s_datagram_sctp());
886 BIO_set_fd(bio, fd, close_flag);
888 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
889 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
891 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
892 sizeof(struct sctp_authchunk));
897 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
899 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
900 sizeof(struct sctp_authchunk));
907 * Test if activation was successful. When using accept(), SCTP-AUTH has
908 * to be activated for the listening socket already, otherwise the
909 * connected socket won't use it.
911 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
912 authchunks = OPENSSL_zalloc(sockopt_len);
913 if (authchunks == NULL) {
917 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
920 OPENSSL_free(authchunks);
925 for (p = (unsigned char *)authchunks->gauth_chunks;
926 p < (unsigned char *)authchunks + sockopt_len;
927 p += sizeof(uint8_t)) {
928 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
930 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
934 OPENSSL_free(authchunks);
936 OPENSSL_assert(auth_data);
937 OPENSSL_assert(auth_forward);
939 # ifdef SCTP_AUTHENTICATION_EVENT
941 memset(&event, 0, sizeof(event));
942 event.se_assoc_id = 0;
943 event.se_type = SCTP_AUTHENTICATION_EVENT;
946 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
947 sizeof(struct sctp_event));
953 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
954 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
960 event.sctp_authentication_event = 1;
963 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
964 sizeof(struct sctp_event_subscribe));
973 * Disable partial delivery by setting the min size larger than the max
974 * record size of 2^14 + 2048 + 13
977 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
987 int BIO_dgram_is_sctp(BIO *bio)
989 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
992 static int dgram_sctp_new(BIO *bi)
994 bio_dgram_sctp_data *data = NULL;
998 data = OPENSSL_zalloc(sizeof(*data));
1001 # ifdef SCTP_PR_SCTP_NONE
1002 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1010 static int dgram_sctp_free(BIO *a)
1012 bio_dgram_sctp_data *data;
1016 if (!dgram_clear(a))
1019 data = (bio_dgram_sctp_data *) a->ptr;
1021 OPENSSL_free(data->saved_message.data);
1028 # ifdef SCTP_AUTHENTICATION_EVENT
1029 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1030 union sctp_notification *snp)
1033 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1035 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1036 struct sctp_authkeyid authkeyid;
1039 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1040 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1041 &authkeyid, sizeof(struct sctp_authkeyid));
1046 static int dgram_sctp_read(BIO *b, char *out, int outl)
1048 int ret = 0, n = 0, i, optval;
1050 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1051 union sctp_notification *snp;
1054 struct cmsghdr *cmsg;
1058 clear_socket_error();
1061 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1064 msg.msg_name = NULL;
1065 msg.msg_namelen = 0;
1068 msg.msg_control = cmsgbuf;
1069 msg.msg_controllen = 512;
1071 n = recvmsg(b->num, &msg, 0);
1079 if (msg.msg_controllen > 0) {
1080 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1081 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1082 if (cmsg->cmsg_level != IPPROTO_SCTP)
1084 # ifdef SCTP_RCVINFO
1085 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1086 struct sctp_rcvinfo *rcvinfo;
1088 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1089 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1090 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1091 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1092 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1093 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1094 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1095 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1099 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1100 struct sctp_sndrcvinfo *sndrcvinfo;
1103 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1104 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1105 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1106 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1107 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1108 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1109 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1110 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1116 if (msg.msg_flags & MSG_NOTIFICATION) {
1117 snp = (union sctp_notification *)out;
1118 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1120 struct sctp_event event;
1122 struct sctp_event_subscribe event;
1123 socklen_t eventsize;
1126 * If a message has been delayed until the socket is dry,
1127 * it can be sent now.
1129 if (data->saved_message.length > 0) {
1130 i = dgram_sctp_write(data->saved_message.bio,
1131 data->saved_message.data,
1132 data->saved_message.length);
1137 OPENSSL_free(data->saved_message.data);
1138 data->saved_message.data = NULL;
1139 data->saved_message.length = 0;
1142 /* disable sender dry event */
1144 memset(&event, 0, sizeof(event));
1145 event.se_assoc_id = 0;
1146 event.se_type = SCTP_SENDER_DRY_EVENT;
1148 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1149 sizeof(struct sctp_event));
1155 eventsize = sizeof(struct sctp_event_subscribe);
1156 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1163 event.sctp_sender_dry_event = 0;
1165 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1166 sizeof(struct sctp_event_subscribe));
1173 # ifdef SCTP_AUTHENTICATION_EVENT
1174 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1175 dgram_sctp_handle_auth_free_key_event(b, snp);
1178 if (data->handle_notifications != NULL)
1179 data->handle_notifications(b, data->notification_context,
1182 memset(out, 0, outl);
1186 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1189 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1190 /* Partial message read, this should never happen! */
1193 * The buffer was too small, this means the peer sent a message
1194 * that was larger than allowed.
1200 * Test if socket buffer can handle max record size (2^14 + 2048
1203 optlen = (socklen_t) sizeof(int);
1204 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1206 OPENSSL_assert(optval >= 18445);
1209 * Test if SCTP doesn't partially deliver below max record size
1210 * (2^14 + 2048 + 13)
1212 optlen = (socklen_t) sizeof(int);
1214 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1217 OPENSSL_assert(optval >= 18445);
1220 * Partially delivered notification??? Probably a bug....
1222 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1225 * Everything seems ok till now, so it's most likely a message
1226 * dropped by PR-SCTP.
1228 memset(out, 0, outl);
1229 BIO_set_retry_read(b);
1233 BIO_clear_retry_flags(b);
1235 if (BIO_dgram_should_retry(ret)) {
1236 BIO_set_retry_read(b);
1237 data->_errno = get_last_socket_error();
1241 /* Test if peer uses SCTP-AUTH before continuing */
1242 if (!data->peer_auth_tested) {
1243 int ii, auth_data = 0, auth_forward = 0;
1245 struct sctp_authchunks *authchunks;
1248 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1249 authchunks = OPENSSL_malloc(optlen);
1250 if (authchunks == NULL) {
1251 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1254 memset(authchunks, 0, optlen);
1255 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1256 authchunks, &optlen);
1259 for (p = (unsigned char *)authchunks->gauth_chunks;
1260 p < (unsigned char *)authchunks + optlen;
1261 p += sizeof(uint8_t)) {
1262 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1264 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1268 OPENSSL_free(authchunks);
1270 if (!auth_data || !auth_forward) {
1271 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1275 data->peer_auth_tested = 1;
1282 * dgram_sctp_write - send message on SCTP socket
1283 * @b: BIO to write to
1285 * @inl: amount of bytes in @in to send
1287 * Returns -1 on error or the sent amount of bytes on success
1289 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1292 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1293 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1294 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1295 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1296 struct iovec iov[1];
1298 struct cmsghdr *cmsg;
1299 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1300 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1301 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1302 struct sctp_sndinfo *sndinfo;
1303 struct sctp_prinfo *prinfo;
1305 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1306 struct sctp_sndrcvinfo *sndrcvinfo;
1309 clear_socket_error();
1312 * If we're send anything else than application data, disable all user
1313 * parameters and flags.
1316 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1317 # ifdef SCTP_SACK_IMMEDIATELY
1318 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1320 sinfo = &handshake_sinfo;
1324 * If we have to send a shutdown alert message and the socket is not dry
1325 * yet, we have to save it and send it as soon as the socket gets dry.
1327 if (data->save_shutdown) {
1328 ret = BIO_dgram_sctp_wait_for_dry(b);
1334 data->saved_message.bio = b;
1335 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1336 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1339 OPENSSL_free(data->saved_message.data);
1340 data->saved_message.data = tmp;
1341 memcpy(data->saved_message.data, in, inl);
1342 data->saved_message.length = inl;
1347 iov[0].iov_base = (char *)in;
1348 iov[0].iov_len = inl;
1349 msg.msg_name = NULL;
1350 msg.msg_namelen = 0;
1353 msg.msg_control = (caddr_t) cmsgbuf;
1354 msg.msg_controllen = 0;
1356 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1357 cmsg = (struct cmsghdr *)cmsgbuf;
1358 cmsg->cmsg_level = IPPROTO_SCTP;
1359 cmsg->cmsg_type = SCTP_SNDINFO;
1360 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1361 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1362 memset(sndinfo, 0, sizeof(*sndinfo));
1363 sndinfo->snd_sid = sinfo->snd_sid;
1364 sndinfo->snd_flags = sinfo->snd_flags;
1365 sndinfo->snd_ppid = sinfo->snd_ppid;
1366 sndinfo->snd_context = sinfo->snd_context;
1367 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1370 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1371 cmsg->cmsg_level = IPPROTO_SCTP;
1372 cmsg->cmsg_type = SCTP_PRINFO;
1373 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1374 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1375 memset(prinfo, 0, sizeof(*prinfo));
1376 prinfo->pr_policy = pinfo->pr_policy;
1377 prinfo->pr_value = pinfo->pr_value;
1378 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1380 cmsg = (struct cmsghdr *)cmsgbuf;
1381 cmsg->cmsg_level = IPPROTO_SCTP;
1382 cmsg->cmsg_type = SCTP_SNDRCV;
1383 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1384 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1385 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1386 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1387 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1389 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1391 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1392 sndrcvinfo->sinfo_context = sinfo->snd_context;
1393 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1394 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1397 ret = sendmsg(b->num, &msg, 0);
1399 BIO_clear_retry_flags(b);
1401 if (BIO_dgram_should_retry(ret)) {
1402 BIO_set_retry_write(b);
1403 data->_errno = get_last_socket_error();
1409 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1412 bio_dgram_sctp_data *data = NULL;
1413 socklen_t sockopt_len = 0;
1414 struct sctp_authkeyid authkeyid;
1415 struct sctp_authkey *authkey = NULL;
1417 data = (bio_dgram_sctp_data *) b->ptr;
1420 case BIO_CTRL_DGRAM_QUERY_MTU:
1422 * Set to maximum (2^14) and ignore user input to enable transport
1423 * protocol fragmentation. Returns always 2^14.
1428 case BIO_CTRL_DGRAM_SET_MTU:
1430 * Set to maximum (2^14) and ignore input to enable transport
1431 * protocol fragmentation. Returns always 2^14.
1436 case BIO_CTRL_DGRAM_SET_CONNECTED:
1437 case BIO_CTRL_DGRAM_CONNECT:
1438 /* Returns always -1. */
1441 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1443 * SCTP doesn't need the DTLS timer Returns always 1.
1446 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1448 * We allow transport protocol fragmentation so this is irrelevant
1452 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1454 data->in_handshake = 1;
1456 data->in_handshake = 0;
1459 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1460 &data->in_handshake, sizeof(int));
1462 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1464 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1467 /* Get active key */
1468 sockopt_len = sizeof(struct sctp_authkeyid);
1470 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1476 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1477 authkey = OPENSSL_malloc(sockopt_len);
1478 if (authkey == NULL) {
1482 memset(authkey, 0, sockopt_len);
1483 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1484 # ifndef __FreeBSD__
1486 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1487 * and higher work without it.
1489 authkey->sca_keylength = 64;
1491 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1494 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1496 OPENSSL_free(authkey);
1501 /* Reset active key */
1502 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1503 &authkeyid, sizeof(struct sctp_authkeyid));
1508 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1509 /* Returns 0 on success, -1 otherwise. */
1511 /* Get active key */
1512 sockopt_len = sizeof(struct sctp_authkeyid);
1514 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1519 /* Set active key */
1520 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1521 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1522 &authkeyid, sizeof(struct sctp_authkeyid));
1527 * CCS has been sent, so remember that and fall through to check if
1528 * we need to deactivate an old key
1532 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1533 /* Returns 0 on success, -1 otherwise. */
1536 * Has this command really been called or is this just a
1539 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1543 * CSS has been both, received and sent, so deactivate an old key
1545 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1546 /* Get active key */
1547 sockopt_len = sizeof(struct sctp_authkeyid);
1549 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1550 &authkeyid, &sockopt_len);
1555 * Deactivate key or delete second last key if
1556 * SCTP_AUTHENTICATION_EVENT is not available.
1558 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1559 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1560 sockopt_len = sizeof(struct sctp_authkeyid);
1561 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1562 &authkeyid, sockopt_len);
1566 # ifndef SCTP_AUTHENTICATION_EVENT
1567 if (authkeyid.scact_keynumber > 0) {
1568 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1569 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1570 &authkeyid, sizeof(struct sctp_authkeyid));
1580 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1581 /* Returns the size of the copied struct. */
1582 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1583 num = sizeof(struct bio_dgram_sctp_sndinfo);
1585 memcpy(ptr, &(data->sndinfo), num);
1588 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1589 /* Returns the size of the copied struct. */
1590 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1591 num = sizeof(struct bio_dgram_sctp_sndinfo);
1593 memcpy(&(data->sndinfo), ptr, num);
1595 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1596 /* Returns the size of the copied struct. */
1597 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1598 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1600 memcpy(ptr, &data->rcvinfo, num);
1604 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1605 /* Returns the size of the copied struct. */
1606 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1607 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1609 memcpy(&(data->rcvinfo), ptr, num);
1611 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1612 /* Returns the size of the copied struct. */
1613 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1614 num = sizeof(struct bio_dgram_sctp_prinfo);
1616 memcpy(ptr, &(data->prinfo), num);
1619 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1620 /* Returns the size of the copied struct. */
1621 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1622 num = sizeof(struct bio_dgram_sctp_prinfo);
1624 memcpy(&(data->prinfo), ptr, num);
1626 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1627 /* Returns always 1. */
1629 data->save_shutdown = 1;
1631 data->save_shutdown = 0;
1636 * Pass to default ctrl function to process SCTP unspecific commands
1638 ret = dgram_ctrl(b, cmd, num, ptr);
1644 int BIO_dgram_sctp_notification_cb(BIO *b,
1645 void (*handle_notifications) (BIO *bio,
1651 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1653 if (handle_notifications != NULL) {
1654 data->handle_notifications = handle_notifications;
1655 data->notification_context = context;
1663 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1664 * @b: The BIO to check for the dry event
1666 * Wait until the peer confirms all packets have been received, and so that
1667 * our kernel doesn't have anything to send anymore. This is only received by
1668 * the peer's kernel, not the application.
1672 * 0 when not dry yet
1675 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1680 union sctp_notification snp;
1684 struct sctp_event event;
1686 struct sctp_event_subscribe event;
1687 socklen_t eventsize;
1689 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1691 /* set sender dry event */
1693 memset(&event, 0, sizeof(event));
1694 event.se_assoc_id = 0;
1695 event.se_type = SCTP_SENDER_DRY_EVENT;
1698 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1699 sizeof(struct sctp_event));
1701 eventsize = sizeof(struct sctp_event_subscribe);
1702 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1706 event.sctp_sender_dry_event = 1;
1709 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1710 sizeof(struct sctp_event_subscribe));
1715 /* peek for notification */
1716 memset(&snp, 0, sizeof(snp));
1717 iov.iov_base = (char *)&snp;
1718 iov.iov_len = sizeof(union sctp_notification);
1719 msg.msg_name = NULL;
1720 msg.msg_namelen = 0;
1723 msg.msg_control = NULL;
1724 msg.msg_controllen = 0;
1727 n = recvmsg(b->num, &msg, MSG_PEEK);
1729 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1730 && (get_last_socket_error() != EWOULDBLOCK))
1736 /* if we find a notification, process it and try again if necessary */
1737 while (msg.msg_flags & MSG_NOTIFICATION) {
1738 memset(&snp, 0, sizeof(snp));
1739 iov.iov_base = (char *)&snp;
1740 iov.iov_len = sizeof(union sctp_notification);
1741 msg.msg_name = NULL;
1742 msg.msg_namelen = 0;
1745 msg.msg_control = NULL;
1746 msg.msg_controllen = 0;
1749 n = recvmsg(b->num, &msg, 0);
1751 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1752 && (get_last_socket_error() != EWOULDBLOCK))
1758 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1761 /* disable sender dry event */
1763 memset(&event, 0, sizeof(event));
1764 event.se_assoc_id = 0;
1765 event.se_type = SCTP_SENDER_DRY_EVENT;
1768 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1769 sizeof(struct sctp_event));
1771 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1773 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1778 event.sctp_sender_dry_event = 0;
1781 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1782 sizeof(struct sctp_event_subscribe));
1787 # ifdef SCTP_AUTHENTICATION_EVENT
1788 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1789 dgram_sctp_handle_auth_free_key_event(b, &snp);
1792 if (data->handle_notifications != NULL)
1793 data->handle_notifications(b, data->notification_context,
1796 /* found notification, peek again */
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 /* if we have seen the dry already, don't wait */
1810 sockflags = fcntl(b->num, F_GETFL, 0);
1811 fcntl(b->num, F_SETFL, O_NONBLOCK);
1814 n = recvmsg(b->num, &msg, MSG_PEEK);
1817 fcntl(b->num, F_SETFL, sockflags);
1821 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1822 && (get_last_socket_error() != EWOULDBLOCK))
1829 /* read anything else */
1833 int BIO_dgram_sctp_msg_waiting(BIO *b)
1836 union sctp_notification snp;
1839 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1841 /* Check if there are any messages waiting to be read */
1843 memset(&snp, 0, sizeof(snp));
1844 iov.iov_base = (char *)&snp;
1845 iov.iov_len = sizeof(union sctp_notification);
1846 msg.msg_name = NULL;
1847 msg.msg_namelen = 0;
1850 msg.msg_control = NULL;
1851 msg.msg_controllen = 0;
1854 sockflags = fcntl(b->num, F_GETFL, 0);
1855 fcntl(b->num, F_SETFL, O_NONBLOCK);
1856 n = recvmsg(b->num, &msg, MSG_PEEK);
1857 fcntl(b->num, F_SETFL, sockflags);
1859 /* if notification, process and try again */
1860 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1861 # ifdef SCTP_AUTHENTICATION_EVENT
1862 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1863 dgram_sctp_handle_auth_free_key_event(b, &snp);
1866 memset(&snp, 0, sizeof(snp));
1867 iov.iov_base = (char *)&snp;
1868 iov.iov_len = sizeof(union sctp_notification);
1869 msg.msg_name = NULL;
1870 msg.msg_namelen = 0;
1873 msg.msg_control = NULL;
1874 msg.msg_controllen = 0;
1876 n = recvmsg(b->num, &msg, 0);
1878 if (data->handle_notifications != NULL)
1879 data->handle_notifications(b, data->notification_context,
1883 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1885 /* Return 1 if there is a message to be read, return 0 otherwise. */
1892 static int dgram_sctp_puts(BIO *bp, const char *str)
1897 ret = dgram_sctp_write(bp, str, n);
1902 static int BIO_dgram_should_retry(int i)
1906 if ((i == 0) || (i == -1)) {
1907 err = get_last_socket_error();
1909 # if defined(OPENSSL_SYS_WINDOWS)
1911 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1912 * this point, the error code was overwritten by another system call
1913 * before this error handling is called.
1917 return (BIO_dgram_non_fatal_error(err));
1922 int BIO_dgram_non_fatal_error(int err)
1925 # if defined(OPENSSL_SYS_WINDOWS)
1926 # if defined(WSAEWOULDBLOCK)
1927 case WSAEWOULDBLOCK:
1932 # ifdef WSAEWOULDBLOCK
1933 # if WSAEWOULDBLOCK != EWOULDBLOCK
1946 # if EWOULDBLOCK != EAGAIN
1971 static void get_current_time(struct timeval *t)
1973 # if defined(_WIN32)
1976 unsigned __int64 ul;
1981 SystemTimeToFileTime(&st, &now.ft);
1983 now.ul -= 116444736000000000ULL;
1985 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1987 t->tv_sec = (long)(now.ul / 10000000);
1988 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1989 # elif defined(OPENSSL_SYS_VMS)
1992 t->tv_sec = (long)tb.time;
1993 t->tv_usec = (long)tb.millitm * 1000;
1995 gettimeofday(t, NULL);