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)))
97 static int dgram_write(BIO *h, const char *buf, int num);
98 static int dgram_read(BIO *h, char *buf, int size);
99 static int dgram_puts(BIO *h, const char *str);
100 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
101 static int dgram_new(BIO *h);
102 static int dgram_free(BIO *data);
103 static int dgram_clear(BIO *bio);
105 # ifndef OPENSSL_NO_SCTP
106 static int dgram_sctp_write(BIO *h, const char *buf, int num);
107 static int dgram_sctp_read(BIO *h, char *buf, int size);
108 static int dgram_sctp_puts(BIO *h, const char *str);
109 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
110 static int dgram_sctp_new(BIO *h);
111 static int dgram_sctp_free(BIO *data);
112 # ifdef SCTP_AUTHENTICATION_EVENT
113 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
118 static int BIO_dgram_should_retry(int s);
120 static void get_current_time(struct timeval *t);
122 static const BIO_METHOD methods_dgramp = {
128 NULL, /* dgram_gets, */
135 # ifndef OPENSSL_NO_SCTP
136 static const BIO_METHOD methods_dgramp_sctp = {
138 "datagram sctp socket",
142 NULL, /* dgram_gets, */
150 typedef struct bio_dgram_data_st {
152 unsigned int connected;
155 struct timeval next_timeout;
156 struct timeval socket_timeout;
157 unsigned int peekmode;
160 # ifndef OPENSSL_NO_SCTP
161 typedef struct bio_dgram_sctp_save_message_st {
165 } bio_dgram_sctp_save_message;
167 typedef struct bio_dgram_sctp_data_st {
169 unsigned int connected;
172 struct bio_dgram_sctp_sndinfo sndinfo;
173 struct bio_dgram_sctp_rcvinfo rcvinfo;
174 struct bio_dgram_sctp_prinfo prinfo;
175 void (*handle_notifications) (BIO *bio, void *context, void *buf);
176 void *notification_context;
181 int peer_auth_tested;
182 bio_dgram_sctp_save_message saved_message;
183 } bio_dgram_sctp_data;
186 const BIO_METHOD *BIO_s_datagram(void)
188 return (&methods_dgramp);
191 BIO *BIO_new_dgram(int fd, int close_flag)
195 ret = BIO_new(BIO_s_datagram());
198 BIO_set_fd(ret, fd, close_flag);
202 static int dgram_new(BIO *bi)
204 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
212 static int dgram_free(BIO *a)
214 bio_dgram_data *data;
221 data = (bio_dgram_data *)a->ptr;
227 static int dgram_clear(BIO *a)
233 BIO_closesocket(a->num);
241 static void dgram_adjust_rcv_timeout(BIO *b)
243 # if defined(SO_RCVTIMEO)
244 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
252 /* Is a timer active? */
253 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
254 struct timeval timenow, timeleft;
256 /* Read current socket timeout */
257 # ifdef OPENSSL_SYS_WINDOWS
260 sz.i = sizeof(timeout);
261 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
262 (void *)&timeout, &sz.i) < 0) {
263 perror("getsockopt");
265 data->socket_timeout.tv_sec = timeout / 1000;
266 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
269 sz.i = sizeof(data->socket_timeout);
270 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
271 &(data->socket_timeout), (void *)&sz) < 0) {
272 perror("getsockopt");
273 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
274 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
277 /* Get current time */
278 get_current_time(&timenow);
280 /* Calculate time left until timer expires */
281 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
282 if (timeleft.tv_usec < timenow.tv_usec) {
283 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
286 timeleft.tv_usec -= timenow.tv_usec;
288 if (timeleft.tv_sec < timenow.tv_sec) {
290 timeleft.tv_usec = 1;
292 timeleft.tv_sec -= timenow.tv_sec;
296 * Adjust socket timeout if next handshake message timer will expire
299 if ((data->socket_timeout.tv_sec == 0
300 && data->socket_timeout.tv_usec == 0)
301 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
302 || (data->socket_timeout.tv_sec == timeleft.tv_sec
303 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
304 # ifdef OPENSSL_SYS_WINDOWS
305 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
306 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
307 (void *)&timeout, sizeof(timeout)) < 0) {
308 perror("setsockopt");
311 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
312 sizeof(struct timeval)) < 0) {
313 perror("setsockopt");
321 static void dgram_reset_rcv_timeout(BIO *b)
323 # if defined(SO_RCVTIMEO)
324 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
326 /* Is a timer active? */
327 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
328 # ifdef OPENSSL_SYS_WINDOWS
329 int timeout = data->socket_timeout.tv_sec * 1000 +
330 data->socket_timeout.tv_usec / 1000;
331 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
332 (void *)&timeout, sizeof(timeout)) < 0) {
333 perror("setsockopt");
337 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
338 sizeof(struct timeval)) < 0) {
339 perror("setsockopt");
346 static int dgram_read(BIO *b, char *out, int outl)
349 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
353 socklen_t len = sizeof(peer);
356 clear_socket_error();
357 memset(&peer, 0, sizeof(peer));
358 dgram_adjust_rcv_timeout(b);
361 ret = recvfrom(b->num, out, outl, flags,
362 BIO_ADDR_sockaddr_noconst(&peer), &len);
364 if (!data->connected && ret >= 0)
365 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
367 BIO_clear_retry_flags(b);
369 if (BIO_dgram_should_retry(ret)) {
370 BIO_set_retry_read(b);
371 data->_errno = get_last_socket_error();
375 dgram_reset_rcv_timeout(b);
380 static int dgram_write(BIO *b, const char *in, int inl)
383 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
384 clear_socket_error();
387 ret = writesocket(b->num, in, inl);
389 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
391 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
392 ret = sendto(b->num, (char *)in, inl, 0,
393 BIO_ADDR_sockaddr(&data->peer), peerlen);
395 ret = sendto(b->num, in, inl, 0,
396 BIO_ADDR_sockaddr(&data->peer), peerlen);
400 BIO_clear_retry_flags(b);
402 if (BIO_dgram_should_retry(ret)) {
403 BIO_set_retry_write(b);
404 data->_errno = get_last_socket_error();
410 static long dgram_get_mtu_overhead(bio_dgram_data *data)
414 switch (BIO_ADDR_family(&data->peer)) {
417 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
424 # ifdef IN6_IS_ADDR_V4MAPPED
425 struct in6_addr tmp_addr;
426 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
427 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
429 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
435 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
442 /* We don't know. Go with the historical default */
449 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
453 bio_dgram_data *data = NULL;
456 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
457 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
458 * modern enough to define socklen_t */
463 data = (bio_dgram_data *)b->ptr;
475 b->num = *((int *)ptr);
476 b->shutdown = (int)num;
488 case BIO_CTRL_GET_CLOSE:
491 case BIO_CTRL_SET_CLOSE:
492 b->shutdown = (int)num;
494 case BIO_CTRL_PENDING:
495 case BIO_CTRL_WPENDING:
502 case BIO_CTRL_DGRAM_CONNECT:
503 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
505 /* (Linux)kernel sets DF bit on outgoing IP packets */
506 case BIO_CTRL_DGRAM_MTU_DISCOVER:
507 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
508 addr_len = (socklen_t) sizeof(addr);
509 memset(&addr, 0, sizeof(addr));
510 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
514 switch (addr.sa.sa_family) {
516 sockopt_val = IP_PMTUDISC_DO;
517 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
518 &sockopt_val, sizeof(sockopt_val))) < 0)
519 perror("setsockopt");
521 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
523 sockopt_val = IPV6_PMTUDISC_DO;
524 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
525 &sockopt_val, sizeof(sockopt_val))) < 0)
526 perror("setsockopt");
537 case BIO_CTRL_DGRAM_QUERY_MTU:
538 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
539 addr_len = (socklen_t) sizeof(addr);
540 memset(&addr, 0, sizeof(addr));
541 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
545 sockopt_len = sizeof(sockopt_val);
546 switch (addr.sa.sa_family) {
549 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
550 &sockopt_len)) < 0 || sockopt_val < 0) {
554 * we assume that the transport protocol is UDP and no IP
557 data->mtu = sockopt_val - 8 - 20;
561 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
564 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
565 (void *)&sockopt_val, &sockopt_len)) < 0
566 || sockopt_val < 0) {
570 * we assume that the transport protocol is UDP and no IPV6
573 data->mtu = sockopt_val - 8 - 40;
586 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
587 ret = -dgram_get_mtu_overhead(data);
588 switch (BIO_ADDR_family(&data->peer)) {
592 # if OPENSSL_USE_IPV6
595 # ifdef IN6_IS_ADDR_V4MAPPED
596 struct in6_addr tmp_addr;
597 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
598 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
611 case BIO_CTRL_DGRAM_GET_MTU:
613 case BIO_CTRL_DGRAM_SET_MTU:
617 case BIO_CTRL_DGRAM_SET_CONNECTED:
620 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
623 memset(&data->peer, 0, sizeof(data->peer));
626 case BIO_CTRL_DGRAM_GET_PEER:
627 ret = BIO_ADDR_sockaddr_size(&data->peer);
628 /* FIXME: if num < ret, we will only return part of an address.
629 That should bee an error, no? */
630 if (num == 0 || num > ret)
632 memcpy(ptr, &data->peer, (ret = num));
634 case BIO_CTRL_DGRAM_SET_PEER:
635 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
637 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
638 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
640 # if defined(SO_RCVTIMEO)
641 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
642 # ifdef OPENSSL_SYS_WINDOWS
644 struct timeval *tv = (struct timeval *)ptr;
645 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
646 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
647 (void *)&timeout, sizeof(timeout)) < 0) {
648 perror("setsockopt");
653 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
654 sizeof(struct timeval)) < 0) {
655 perror("setsockopt");
660 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
668 # ifdef OPENSSL_SYS_WINDOWS
670 struct timeval *tv = (struct timeval *)ptr;
672 sz.i = sizeof(timeout);
673 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
674 (void *)&timeout, &sz.i) < 0) {
675 perror("getsockopt");
678 tv->tv_sec = timeout / 1000;
679 tv->tv_usec = (timeout % 1000) * 1000;
683 sz.i = sizeof(struct timeval);
684 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
685 ptr, (void *)&sz) < 0) {
686 perror("getsockopt");
688 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
689 OPENSSL_assert(sz.s <= sizeof(struct timeval));
697 # if defined(SO_SNDTIMEO)
698 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
699 # ifdef OPENSSL_SYS_WINDOWS
701 struct timeval *tv = (struct timeval *)ptr;
702 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
703 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
704 (void *)&timeout, sizeof(timeout)) < 0) {
705 perror("setsockopt");
710 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
711 sizeof(struct timeval)) < 0) {
712 perror("setsockopt");
717 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
725 # ifdef OPENSSL_SYS_WINDOWS
727 struct timeval *tv = (struct timeval *)ptr;
729 sz.i = sizeof(timeout);
730 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
731 (void *)&timeout, &sz.i) < 0) {
732 perror("getsockopt");
735 tv->tv_sec = timeout / 1000;
736 tv->tv_usec = (timeout % 1000) * 1000;
740 sz.i = sizeof(struct timeval);
741 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
742 ptr, (void *)&sz) < 0) {
743 perror("getsockopt");
745 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
746 OPENSSL_assert(sz.s <= sizeof(struct timeval));
754 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
756 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
757 # ifdef OPENSSL_SYS_WINDOWS
758 d_errno = (data->_errno == WSAETIMEDOUT);
760 d_errno = (data->_errno == EAGAIN);
769 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
770 if (data->_errno == EMSGSIZE) {
777 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
778 sockopt_val = num ? 1 : 0;
780 switch (data->peer.sa.sa_family) {
782 # if defined(IP_DONTFRAG)
783 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
784 &sockopt_val, sizeof(sockopt_val))) < 0) {
785 perror("setsockopt");
788 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
789 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
790 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
791 &sockopt_val, sizeof(sockopt_val))) < 0) {
792 perror("setsockopt");
795 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
796 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
797 (const char *)&sockopt_val,
798 sizeof(sockopt_val))) < 0) {
799 perror("setsockopt");
806 # if OPENSSL_USE_IPV6
808 # if defined(IPV6_DONTFRAG)
809 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
810 (const void *)&sockopt_val,
811 sizeof(sockopt_val))) < 0) {
812 perror("setsockopt");
815 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
816 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
817 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
818 &sockopt_val, sizeof(sockopt_val))) < 0) {
819 perror("setsockopt");
832 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
833 ret = dgram_get_mtu_overhead(data);
835 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
836 data->peekmode = (unsigned int)num;
845 static int dgram_puts(BIO *bp, const char *str)
850 ret = dgram_write(bp, str, n);
854 # ifndef OPENSSL_NO_SCTP
855 const BIO_METHOD *BIO_s_datagram_sctp(void)
857 return (&methods_dgramp_sctp);
860 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
863 int ret, optval = 20000;
864 int auth_data = 0, auth_forward = 0;
866 struct sctp_authchunk auth;
867 struct sctp_authchunks *authchunks;
868 socklen_t sockopt_len;
869 # ifdef SCTP_AUTHENTICATION_EVENT
871 struct sctp_event event;
873 struct sctp_event_subscribe event;
877 bio = BIO_new(BIO_s_datagram_sctp());
880 BIO_set_fd(bio, fd, close_flag);
882 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
883 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
885 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
886 sizeof(struct sctp_authchunk));
891 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
893 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
894 sizeof(struct sctp_authchunk));
901 * Test if activation was successful. When using accept(), SCTP-AUTH has
902 * to be activated for the listening socket already, otherwise the
903 * connected socket won't use it.
905 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
906 authchunks = OPENSSL_zalloc(sockopt_len);
907 if (authchunks == NULL) {
911 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
914 OPENSSL_free(authchunks);
919 for (p = (unsigned char *)authchunks->gauth_chunks;
920 p < (unsigned char *)authchunks + sockopt_len;
921 p += sizeof(uint8_t)) {
922 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
924 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
928 OPENSSL_free(authchunks);
930 OPENSSL_assert(auth_data);
931 OPENSSL_assert(auth_forward);
933 # ifdef SCTP_AUTHENTICATION_EVENT
935 memset(&event, 0, sizeof(event));
936 event.se_assoc_id = 0;
937 event.se_type = SCTP_AUTHENTICATION_EVENT;
940 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
941 sizeof(struct sctp_event));
947 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
948 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
954 event.sctp_authentication_event = 1;
957 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
958 sizeof(struct sctp_event_subscribe));
967 * Disable partial delivery by setting the min size larger than the max
968 * record size of 2^14 + 2048 + 13
971 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
981 int BIO_dgram_is_sctp(BIO *bio)
983 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
986 static int dgram_sctp_new(BIO *bi)
988 bio_dgram_sctp_data *data = NULL;
992 data = OPENSSL_zalloc(sizeof(*data));
995 # ifdef SCTP_PR_SCTP_NONE
996 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1004 static int dgram_sctp_free(BIO *a)
1006 bio_dgram_sctp_data *data;
1010 if (!dgram_clear(a))
1013 data = (bio_dgram_sctp_data *) a->ptr;
1015 OPENSSL_free(data->saved_message.data);
1022 # ifdef SCTP_AUTHENTICATION_EVENT
1023 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1024 union sctp_notification *snp)
1027 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1029 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1030 struct sctp_authkeyid authkeyid;
1033 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1034 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1035 &authkeyid, sizeof(struct sctp_authkeyid));
1040 static int dgram_sctp_read(BIO *b, char *out, int outl)
1042 int ret = 0, n = 0, i, optval;
1044 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1045 union sctp_notification *snp;
1048 struct cmsghdr *cmsg;
1052 clear_socket_error();
1055 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1058 msg.msg_name = NULL;
1059 msg.msg_namelen = 0;
1062 msg.msg_control = cmsgbuf;
1063 msg.msg_controllen = 512;
1065 n = recvmsg(b->num, &msg, 0);
1073 if (msg.msg_controllen > 0) {
1074 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1075 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1076 if (cmsg->cmsg_level != IPPROTO_SCTP)
1078 # ifdef SCTP_RCVINFO
1079 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1080 struct sctp_rcvinfo *rcvinfo;
1082 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1083 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1084 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1085 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1086 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1087 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1088 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1089 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1093 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1094 struct sctp_sndrcvinfo *sndrcvinfo;
1097 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1098 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1099 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1100 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1101 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1102 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1103 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1104 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1110 if (msg.msg_flags & MSG_NOTIFICATION) {
1111 snp = (union sctp_notification *)out;
1112 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1114 struct sctp_event event;
1116 struct sctp_event_subscribe event;
1117 socklen_t eventsize;
1120 * If a message has been delayed until the socket is dry,
1121 * it can be sent now.
1123 if (data->saved_message.length > 0) {
1124 i = dgram_sctp_write(data->saved_message.bio,
1125 data->saved_message.data,
1126 data->saved_message.length);
1131 OPENSSL_free(data->saved_message.data);
1132 data->saved_message.data = NULL;
1133 data->saved_message.length = 0;
1136 /* disable sender dry event */
1138 memset(&event, 0, sizeof(event));
1139 event.se_assoc_id = 0;
1140 event.se_type = SCTP_SENDER_DRY_EVENT;
1142 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1143 sizeof(struct sctp_event));
1149 eventsize = sizeof(struct sctp_event_subscribe);
1150 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1157 event.sctp_sender_dry_event = 0;
1159 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1160 sizeof(struct sctp_event_subscribe));
1167 # ifdef SCTP_AUTHENTICATION_EVENT
1168 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1169 dgram_sctp_handle_auth_free_key_event(b, snp);
1172 if (data->handle_notifications != NULL)
1173 data->handle_notifications(b, data->notification_context,
1176 memset(out, 0, outl);
1180 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1183 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1184 /* Partial message read, this should never happen! */
1187 * The buffer was too small, this means the peer sent a message
1188 * that was larger than allowed.
1194 * Test if socket buffer can handle max record size (2^14 + 2048
1197 optlen = (socklen_t) sizeof(int);
1198 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1200 OPENSSL_assert(optval >= 18445);
1203 * Test if SCTP doesn't partially deliver below max record size
1204 * (2^14 + 2048 + 13)
1206 optlen = (socklen_t) sizeof(int);
1208 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1211 OPENSSL_assert(optval >= 18445);
1214 * Partially delivered notification??? Probably a bug....
1216 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1219 * Everything seems ok till now, so it's most likely a message
1220 * dropped by PR-SCTP.
1222 memset(out, 0, outl);
1223 BIO_set_retry_read(b);
1227 BIO_clear_retry_flags(b);
1229 if (BIO_dgram_should_retry(ret)) {
1230 BIO_set_retry_read(b);
1231 data->_errno = get_last_socket_error();
1235 /* Test if peer uses SCTP-AUTH before continuing */
1236 if (!data->peer_auth_tested) {
1237 int ii, auth_data = 0, auth_forward = 0;
1239 struct sctp_authchunks *authchunks;
1242 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1243 authchunks = OPENSSL_malloc(optlen);
1244 if (authchunks == NULL) {
1245 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1248 memset(authchunks, 0, optlen);
1249 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1250 authchunks, &optlen);
1253 for (p = (unsigned char *)authchunks->gauth_chunks;
1254 p < (unsigned char *)authchunks + optlen;
1255 p += sizeof(uint8_t)) {
1256 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1258 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1262 OPENSSL_free(authchunks);
1264 if (!auth_data || !auth_forward) {
1265 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1269 data->peer_auth_tested = 1;
1276 * dgram_sctp_write - send message on SCTP socket
1277 * @b: BIO to write to
1279 * @inl: amount of bytes in @in to send
1281 * Returns -1 on error or the sent amount of bytes on success
1283 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1286 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1287 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1288 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1289 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1290 struct iovec iov[1];
1292 struct cmsghdr *cmsg;
1293 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1294 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1295 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1296 struct sctp_sndinfo *sndinfo;
1297 struct sctp_prinfo *prinfo;
1299 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1300 struct sctp_sndrcvinfo *sndrcvinfo;
1303 clear_socket_error();
1306 * If we're send anything else than application data, disable all user
1307 * parameters and flags.
1310 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1311 # ifdef SCTP_SACK_IMMEDIATELY
1312 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1314 sinfo = &handshake_sinfo;
1318 * If we have to send a shutdown alert message and the socket is not dry
1319 * yet, we have to save it and send it as soon as the socket gets dry.
1321 if (data->save_shutdown) {
1322 ret = BIO_dgram_sctp_wait_for_dry(b);
1328 data->saved_message.bio = b;
1329 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1330 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1333 OPENSSL_free(data->saved_message.data);
1334 data->saved_message.data = tmp;
1335 memcpy(data->saved_message.data, in, inl);
1336 data->saved_message.length = inl;
1341 iov[0].iov_base = (char *)in;
1342 iov[0].iov_len = inl;
1343 msg.msg_name = NULL;
1344 msg.msg_namelen = 0;
1347 msg.msg_control = (caddr_t) cmsgbuf;
1348 msg.msg_controllen = 0;
1350 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1351 cmsg = (struct cmsghdr *)cmsgbuf;
1352 cmsg->cmsg_level = IPPROTO_SCTP;
1353 cmsg->cmsg_type = SCTP_SNDINFO;
1354 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1355 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1356 memset(sndinfo, 0, sizeof(*sndinfo));
1357 sndinfo->snd_sid = sinfo->snd_sid;
1358 sndinfo->snd_flags = sinfo->snd_flags;
1359 sndinfo->snd_ppid = sinfo->snd_ppid;
1360 sndinfo->snd_context = sinfo->snd_context;
1361 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1364 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1365 cmsg->cmsg_level = IPPROTO_SCTP;
1366 cmsg->cmsg_type = SCTP_PRINFO;
1367 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1368 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1369 memset(prinfo, 0, sizeof(*prinfo));
1370 prinfo->pr_policy = pinfo->pr_policy;
1371 prinfo->pr_value = pinfo->pr_value;
1372 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1374 cmsg = (struct cmsghdr *)cmsgbuf;
1375 cmsg->cmsg_level = IPPROTO_SCTP;
1376 cmsg->cmsg_type = SCTP_SNDRCV;
1377 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1378 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1379 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1380 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1381 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1383 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1385 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1386 sndrcvinfo->sinfo_context = sinfo->snd_context;
1387 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1388 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1391 ret = sendmsg(b->num, &msg, 0);
1393 BIO_clear_retry_flags(b);
1395 if (BIO_dgram_should_retry(ret)) {
1396 BIO_set_retry_write(b);
1397 data->_errno = get_last_socket_error();
1403 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1406 bio_dgram_sctp_data *data = NULL;
1407 socklen_t sockopt_len = 0;
1408 struct sctp_authkeyid authkeyid;
1409 struct sctp_authkey *authkey = NULL;
1411 data = (bio_dgram_sctp_data *) b->ptr;
1414 case BIO_CTRL_DGRAM_QUERY_MTU:
1416 * Set to maximum (2^14) and ignore user input to enable transport
1417 * protocol fragmentation. Returns always 2^14.
1422 case BIO_CTRL_DGRAM_SET_MTU:
1424 * Set to maximum (2^14) and ignore input to enable transport
1425 * protocol fragmentation. Returns always 2^14.
1430 case BIO_CTRL_DGRAM_SET_CONNECTED:
1431 case BIO_CTRL_DGRAM_CONNECT:
1432 /* Returns always -1. */
1435 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1437 * SCTP doesn't need the DTLS timer Returns always 1.
1440 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1442 * We allow transport protocol fragmentation so this is irrelevant
1446 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1448 data->in_handshake = 1;
1450 data->in_handshake = 0;
1453 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1454 &data->in_handshake, sizeof(int));
1456 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1458 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1461 /* Get active key */
1462 sockopt_len = sizeof(struct sctp_authkeyid);
1464 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1470 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1471 authkey = OPENSSL_malloc(sockopt_len);
1472 if (authkey == NULL) {
1476 memset(authkey, 0, sockopt_len);
1477 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1478 # ifndef __FreeBSD__
1480 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1481 * and higher work without it.
1483 authkey->sca_keylength = 64;
1485 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1488 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1490 OPENSSL_free(authkey);
1495 /* Reset active key */
1496 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1497 &authkeyid, sizeof(struct sctp_authkeyid));
1502 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1503 /* Returns 0 on success, -1 otherwise. */
1505 /* Get active key */
1506 sockopt_len = sizeof(struct sctp_authkeyid);
1508 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1513 /* Set active key */
1514 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1515 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1516 &authkeyid, sizeof(struct sctp_authkeyid));
1521 * CCS has been sent, so remember that and fall through to check if
1522 * we need to deactivate an old key
1526 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1527 /* Returns 0 on success, -1 otherwise. */
1530 * Has this command really been called or is this just a
1533 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1537 * CSS has been both, received and sent, so deactivate an old key
1539 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1540 /* Get active key */
1541 sockopt_len = sizeof(struct sctp_authkeyid);
1543 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1544 &authkeyid, &sockopt_len);
1549 * Deactivate key or delete second last key if
1550 * SCTP_AUTHENTICATION_EVENT is not available.
1552 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1553 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1554 sockopt_len = sizeof(struct sctp_authkeyid);
1555 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1556 &authkeyid, sockopt_len);
1560 # ifndef SCTP_AUTHENTICATION_EVENT
1561 if (authkeyid.scact_keynumber > 0) {
1562 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1563 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1564 &authkeyid, sizeof(struct sctp_authkeyid));
1574 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1575 /* Returns the size of the copied struct. */
1576 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1577 num = sizeof(struct bio_dgram_sctp_sndinfo);
1579 memcpy(ptr, &(data->sndinfo), num);
1582 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1583 /* Returns the size of the copied struct. */
1584 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1585 num = sizeof(struct bio_dgram_sctp_sndinfo);
1587 memcpy(&(data->sndinfo), ptr, num);
1589 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1590 /* Returns the size of the copied struct. */
1591 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1592 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1594 memcpy(ptr, &data->rcvinfo, num);
1598 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1599 /* Returns the size of the copied struct. */
1600 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1601 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1603 memcpy(&(data->rcvinfo), ptr, num);
1605 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1606 /* Returns the size of the copied struct. */
1607 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1608 num = sizeof(struct bio_dgram_sctp_prinfo);
1610 memcpy(ptr, &(data->prinfo), num);
1613 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1614 /* Returns the size of the copied struct. */
1615 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1616 num = sizeof(struct bio_dgram_sctp_prinfo);
1618 memcpy(&(data->prinfo), ptr, num);
1620 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1621 /* Returns always 1. */
1623 data->save_shutdown = 1;
1625 data->save_shutdown = 0;
1630 * Pass to default ctrl function to process SCTP unspecific commands
1632 ret = dgram_ctrl(b, cmd, num, ptr);
1638 int BIO_dgram_sctp_notification_cb(BIO *b,
1639 void (*handle_notifications) (BIO *bio,
1645 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1647 if (handle_notifications != NULL) {
1648 data->handle_notifications = handle_notifications;
1649 data->notification_context = context;
1657 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1658 * @b: The BIO to check for the dry event
1660 * Wait until the peer confirms all packets have been received, and so that
1661 * our kernel doesn't have anything to send anymore. This is only received by
1662 * the peer's kernel, not the application.
1666 * 0 when not dry yet
1669 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1674 union sctp_notification snp;
1678 struct sctp_event event;
1680 struct sctp_event_subscribe event;
1681 socklen_t eventsize;
1683 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1685 /* set sender dry event */
1687 memset(&event, 0, sizeof(event));
1688 event.se_assoc_id = 0;
1689 event.se_type = SCTP_SENDER_DRY_EVENT;
1692 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1693 sizeof(struct sctp_event));
1695 eventsize = sizeof(struct sctp_event_subscribe);
1696 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1700 event.sctp_sender_dry_event = 1;
1703 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1704 sizeof(struct sctp_event_subscribe));
1709 /* peek for notification */
1710 memset(&snp, 0, sizeof(snp));
1711 iov.iov_base = (char *)&snp;
1712 iov.iov_len = sizeof(union sctp_notification);
1713 msg.msg_name = NULL;
1714 msg.msg_namelen = 0;
1717 msg.msg_control = NULL;
1718 msg.msg_controllen = 0;
1721 n = recvmsg(b->num, &msg, MSG_PEEK);
1723 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1724 && (get_last_socket_error() != EWOULDBLOCK))
1730 /* if we find a notification, process it and try again if necessary */
1731 while (msg.msg_flags & MSG_NOTIFICATION) {
1732 memset(&snp, 0, sizeof(snp));
1733 iov.iov_base = (char *)&snp;
1734 iov.iov_len = sizeof(union sctp_notification);
1735 msg.msg_name = NULL;
1736 msg.msg_namelen = 0;
1739 msg.msg_control = NULL;
1740 msg.msg_controllen = 0;
1743 n = recvmsg(b->num, &msg, 0);
1745 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1746 && (get_last_socket_error() != EWOULDBLOCK))
1752 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1755 /* disable sender dry event */
1757 memset(&event, 0, sizeof(event));
1758 event.se_assoc_id = 0;
1759 event.se_type = SCTP_SENDER_DRY_EVENT;
1762 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1763 sizeof(struct sctp_event));
1765 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1767 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1772 event.sctp_sender_dry_event = 0;
1775 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1776 sizeof(struct sctp_event_subscribe));
1781 # ifdef SCTP_AUTHENTICATION_EVENT
1782 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1783 dgram_sctp_handle_auth_free_key_event(b, &snp);
1786 if (data->handle_notifications != NULL)
1787 data->handle_notifications(b, data->notification_context,
1790 /* found notification, peek again */
1791 memset(&snp, 0, sizeof(snp));
1792 iov.iov_base = (char *)&snp;
1793 iov.iov_len = sizeof(union sctp_notification);
1794 msg.msg_name = NULL;
1795 msg.msg_namelen = 0;
1798 msg.msg_control = NULL;
1799 msg.msg_controllen = 0;
1802 /* if we have seen the dry already, don't wait */
1804 sockflags = fcntl(b->num, F_GETFL, 0);
1805 fcntl(b->num, F_SETFL, O_NONBLOCK);
1808 n = recvmsg(b->num, &msg, MSG_PEEK);
1811 fcntl(b->num, F_SETFL, sockflags);
1815 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1816 && (get_last_socket_error() != EWOULDBLOCK))
1823 /* read anything else */
1827 int BIO_dgram_sctp_msg_waiting(BIO *b)
1830 union sctp_notification snp;
1833 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1835 /* Check if there are any messages waiting to be read */
1837 memset(&snp, 0, sizeof(snp));
1838 iov.iov_base = (char *)&snp;
1839 iov.iov_len = sizeof(union sctp_notification);
1840 msg.msg_name = NULL;
1841 msg.msg_namelen = 0;
1844 msg.msg_control = NULL;
1845 msg.msg_controllen = 0;
1848 sockflags = fcntl(b->num, F_GETFL, 0);
1849 fcntl(b->num, F_SETFL, O_NONBLOCK);
1850 n = recvmsg(b->num, &msg, MSG_PEEK);
1851 fcntl(b->num, F_SETFL, sockflags);
1853 /* if notification, process and try again */
1854 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1855 # ifdef SCTP_AUTHENTICATION_EVENT
1856 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1857 dgram_sctp_handle_auth_free_key_event(b, &snp);
1860 memset(&snp, 0, sizeof(snp));
1861 iov.iov_base = (char *)&snp;
1862 iov.iov_len = sizeof(union sctp_notification);
1863 msg.msg_name = NULL;
1864 msg.msg_namelen = 0;
1867 msg.msg_control = NULL;
1868 msg.msg_controllen = 0;
1870 n = recvmsg(b->num, &msg, 0);
1872 if (data->handle_notifications != NULL)
1873 data->handle_notifications(b, data->notification_context,
1877 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1879 /* Return 1 if there is a message to be read, return 0 otherwise. */
1886 static int dgram_sctp_puts(BIO *bp, const char *str)
1891 ret = dgram_sctp_write(bp, str, n);
1896 static int BIO_dgram_should_retry(int i)
1900 if ((i == 0) || (i == -1)) {
1901 err = get_last_socket_error();
1903 # if defined(OPENSSL_SYS_WINDOWS)
1905 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1906 * this point, the error code was overwritten by another system call
1907 * before this error handling is called.
1911 return (BIO_dgram_non_fatal_error(err));
1916 int BIO_dgram_non_fatal_error(int err)
1919 # if defined(OPENSSL_SYS_WINDOWS)
1920 # if defined(WSAEWOULDBLOCK)
1921 case WSAEWOULDBLOCK:
1926 # ifdef WSAEWOULDBLOCK
1927 # if WSAEWOULDBLOCK != EWOULDBLOCK
1940 # if EWOULDBLOCK != EAGAIN
1965 static void get_current_time(struct timeval *t)
1967 # if defined(_WIN32)
1970 unsigned __int64 ul;
1975 SystemTimeToFileTime(&st, &now.ft);
1977 now.ul -= 116444736000000000ULL;
1979 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1981 t->tv_sec = (long)(now.ul / 10000000);
1982 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1983 # elif defined(OPENSSL_SYS_VMS)
1986 t->tv_sec = (long)tb.time;
1987 t->tv_usec = (long)tb.millitm * 1000;
1989 gettimeofday(t, NULL);