2 * Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include "bio_local.h"
14 #ifndef OPENSSL_NO_DGRAM
16 # ifndef OPENSSL_NO_SCTP
17 # include <netinet/sctp.h>
19 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
20 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
23 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
24 # define IP_MTU 14 /* linux is lame */
27 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
28 # define IPPROTO_IPV6 41 /* windows is lame */
31 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
32 /* Standard definition causes type-punning problems. */
33 # undef IN6_IS_ADDR_V4MAPPED
34 # define s6_addr32 __u6_addr.__u6_addr32
35 # define IN6_IS_ADDR_V4MAPPED(a) \
36 (((a)->s6_addr32[0] == 0) && \
37 ((a)->s6_addr32[1] == 0) && \
38 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
41 static int dgram_write(BIO *h, const char *buf, int num);
42 static int dgram_read(BIO *h, char *buf, int size);
43 static int dgram_puts(BIO *h, const char *str);
44 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
45 static int dgram_new(BIO *h);
46 static int dgram_free(BIO *data);
47 static int dgram_clear(BIO *bio);
49 # ifndef OPENSSL_NO_SCTP
50 static int dgram_sctp_write(BIO *h, const char *buf, int num);
51 static int dgram_sctp_read(BIO *h, char *buf, int size);
52 static int dgram_sctp_puts(BIO *h, const char *str);
53 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
54 static int dgram_sctp_new(BIO *h);
55 static int dgram_sctp_free(BIO *data);
56 static int dgram_sctp_wait_for_dry(BIO *b);
57 static int dgram_sctp_msg_waiting(BIO *b);
58 # ifdef SCTP_AUTHENTICATION_EVENT
59 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
64 static int BIO_dgram_should_retry(int s);
66 static void get_current_time(struct timeval *t);
68 static const BIO_METHOD methods_dgramp = {
76 NULL, /* dgram_gets, */
80 NULL, /* dgram_callback_ctrl */
83 # ifndef OPENSSL_NO_SCTP
84 static const BIO_METHOD methods_dgramp_sctp = {
86 "datagram sctp socket",
92 NULL, /* dgram_gets, */
96 NULL, /* dgram_callback_ctrl */
100 typedef struct bio_dgram_data_st {
102 unsigned int connected;
105 struct timeval next_timeout;
106 struct timeval socket_timeout;
107 unsigned int peekmode;
110 # ifndef OPENSSL_NO_SCTP
111 typedef struct bio_dgram_sctp_save_message_st {
115 } bio_dgram_sctp_save_message;
117 typedef struct bio_dgram_sctp_data_st {
119 unsigned int connected;
122 struct bio_dgram_sctp_sndinfo sndinfo;
123 struct bio_dgram_sctp_rcvinfo rcvinfo;
124 struct bio_dgram_sctp_prinfo prinfo;
125 BIO_dgram_sctp_notification_handler_fn handle_notifications;
126 void *notification_context;
131 int peer_auth_tested;
132 } bio_dgram_sctp_data;
135 const BIO_METHOD *BIO_s_datagram(void)
137 return &methods_dgramp;
140 BIO *BIO_new_dgram(int fd, int close_flag)
144 ret = BIO_new(BIO_s_datagram());
147 BIO_set_fd(ret, fd, close_flag);
151 static int dgram_new(BIO *bi)
153 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
161 static int dgram_free(BIO *a)
163 bio_dgram_data *data;
170 data = (bio_dgram_data *)a->ptr;
176 static int dgram_clear(BIO *a)
182 BIO_closesocket(a->num);
190 static void dgram_adjust_rcv_timeout(BIO *b)
192 # if defined(SO_RCVTIMEO)
193 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
201 /* Is a timer active? */
202 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
203 struct timeval timenow, timeleft;
205 /* Read current socket timeout */
206 # ifdef OPENSSL_SYS_WINDOWS
209 sz.i = sizeof(timeout);
210 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
211 (void *)&timeout, &sz.i) < 0) {
212 perror("getsockopt");
214 data->socket_timeout.tv_sec = timeout / 1000;
215 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
218 sz.i = sizeof(data->socket_timeout);
219 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
220 &(data->socket_timeout), (void *)&sz) < 0) {
221 perror("getsockopt");
222 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
223 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
226 /* Get current time */
227 get_current_time(&timenow);
229 /* Calculate time left until timer expires */
230 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
231 if (timeleft.tv_usec < timenow.tv_usec) {
232 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
235 timeleft.tv_usec -= timenow.tv_usec;
237 if (timeleft.tv_sec < timenow.tv_sec) {
239 timeleft.tv_usec = 1;
241 timeleft.tv_sec -= timenow.tv_sec;
245 * Adjust socket timeout if next handshake message timer will expire
248 if ((data->socket_timeout.tv_sec == 0
249 && data->socket_timeout.tv_usec == 0)
250 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
251 || (data->socket_timeout.tv_sec == timeleft.tv_sec
252 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
253 # ifdef OPENSSL_SYS_WINDOWS
254 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
255 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
256 (void *)&timeout, sizeof(timeout)) < 0) {
257 perror("setsockopt");
260 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
261 sizeof(struct timeval)) < 0) {
262 perror("setsockopt");
270 static void dgram_reset_rcv_timeout(BIO *b)
272 # if defined(SO_RCVTIMEO)
273 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
275 /* Is a timer active? */
276 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
277 # ifdef OPENSSL_SYS_WINDOWS
278 int timeout = data->socket_timeout.tv_sec * 1000 +
279 data->socket_timeout.tv_usec / 1000;
280 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
281 (void *)&timeout, sizeof(timeout)) < 0) {
282 perror("setsockopt");
286 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
287 sizeof(struct timeval)) < 0) {
288 perror("setsockopt");
295 static int dgram_read(BIO *b, char *out, int outl)
298 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
302 socklen_t len = sizeof(peer);
305 clear_socket_error();
306 memset(&peer, 0, sizeof(peer));
307 dgram_adjust_rcv_timeout(b);
310 ret = recvfrom(b->num, out, outl, flags,
311 BIO_ADDR_sockaddr_noconst(&peer), &len);
313 if (!data->connected && ret >= 0)
314 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
316 BIO_clear_retry_flags(b);
318 if (BIO_dgram_should_retry(ret)) {
319 BIO_set_retry_read(b);
320 data->_errno = get_last_socket_error();
324 dgram_reset_rcv_timeout(b);
329 static int dgram_write(BIO *b, const char *in, int inl)
332 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
333 clear_socket_error();
336 ret = writesocket(b->num, in, inl);
338 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
340 ret = sendto(b->num, in, inl, 0,
341 BIO_ADDR_sockaddr(&data->peer), peerlen);
344 BIO_clear_retry_flags(b);
346 if (BIO_dgram_should_retry(ret)) {
347 BIO_set_retry_write(b);
348 data->_errno = get_last_socket_error();
354 static long dgram_get_mtu_overhead(bio_dgram_data *data)
358 switch (BIO_ADDR_family(&data->peer)) {
361 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
365 # if OPENSSL_USE_IPV6
368 # ifdef IN6_IS_ADDR_V4MAPPED
369 struct in6_addr tmp_addr;
370 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
371 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
373 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
379 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
386 /* We don't know. Go with the historical default */
393 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
397 bio_dgram_data *data = NULL;
400 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
401 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
402 * modern enough to define socklen_t */
407 data = (bio_dgram_data *)b->ptr;
419 b->num = *((int *)ptr);
420 b->shutdown = (int)num;
432 case BIO_CTRL_GET_CLOSE:
435 case BIO_CTRL_SET_CLOSE:
436 b->shutdown = (int)num;
438 case BIO_CTRL_PENDING:
439 case BIO_CTRL_WPENDING:
446 case BIO_CTRL_DGRAM_CONNECT:
447 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
449 /* (Linux)kernel sets DF bit on outgoing IP packets */
450 case BIO_CTRL_DGRAM_MTU_DISCOVER:
451 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
452 addr_len = (socklen_t) sizeof(addr);
453 memset(&addr, 0, sizeof(addr));
454 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
458 switch (addr.sa.sa_family) {
460 sockopt_val = IP_PMTUDISC_DO;
461 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
462 &sockopt_val, sizeof(sockopt_val))) < 0)
463 perror("setsockopt");
465 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
467 sockopt_val = IPV6_PMTUDISC_DO;
468 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
469 &sockopt_val, sizeof(sockopt_val))) < 0)
470 perror("setsockopt");
481 case BIO_CTRL_DGRAM_QUERY_MTU:
482 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
483 addr_len = (socklen_t) sizeof(addr);
484 memset(&addr, 0, sizeof(addr));
485 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
489 sockopt_len = sizeof(sockopt_val);
490 switch (addr.sa.sa_family) {
493 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
494 &sockopt_len)) < 0 || sockopt_val < 0) {
498 * we assume that the transport protocol is UDP and no IP
501 data->mtu = sockopt_val - 8 - 20;
505 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
508 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
509 (void *)&sockopt_val, &sockopt_len)) < 0
510 || sockopt_val < 0) {
514 * we assume that the transport protocol is UDP and no IPV6
517 data->mtu = sockopt_val - 8 - 40;
530 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
531 ret = -dgram_get_mtu_overhead(data);
532 switch (BIO_ADDR_family(&data->peer)) {
536 # if OPENSSL_USE_IPV6
539 # ifdef IN6_IS_ADDR_V4MAPPED
540 struct in6_addr tmp_addr;
541 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
542 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
555 case BIO_CTRL_DGRAM_GET_MTU:
557 case BIO_CTRL_DGRAM_SET_MTU:
561 case BIO_CTRL_DGRAM_SET_CONNECTED:
564 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
567 memset(&data->peer, 0, sizeof(data->peer));
570 case BIO_CTRL_DGRAM_GET_PEER:
571 ret = BIO_ADDR_sockaddr_size(&data->peer);
572 /* FIXME: if num < ret, we will only return part of an address.
573 That should bee an error, no? */
574 if (num == 0 || num > ret)
576 memcpy(ptr, &data->peer, (ret = num));
578 case BIO_CTRL_DGRAM_SET_PEER:
579 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
581 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
582 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
584 # if defined(SO_RCVTIMEO)
585 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
586 # ifdef OPENSSL_SYS_WINDOWS
588 struct timeval *tv = (struct timeval *)ptr;
589 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
590 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
591 (void *)&timeout, sizeof(timeout)) < 0) {
592 perror("setsockopt");
597 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
598 sizeof(struct timeval)) < 0) {
599 perror("setsockopt");
604 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
612 # ifdef OPENSSL_SYS_WINDOWS
614 struct timeval *tv = (struct timeval *)ptr;
616 sz.i = sizeof(timeout);
617 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
618 (void *)&timeout, &sz.i) < 0) {
619 perror("getsockopt");
622 tv->tv_sec = timeout / 1000;
623 tv->tv_usec = (timeout % 1000) * 1000;
627 sz.i = sizeof(struct timeval);
628 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
629 ptr, (void *)&sz) < 0) {
630 perror("getsockopt");
632 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
633 OPENSSL_assert(sz.s <= sizeof(struct timeval));
641 # if defined(SO_SNDTIMEO)
642 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
643 # ifdef OPENSSL_SYS_WINDOWS
645 struct timeval *tv = (struct timeval *)ptr;
646 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
647 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
648 (void *)&timeout, sizeof(timeout)) < 0) {
649 perror("setsockopt");
654 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
655 sizeof(struct timeval)) < 0) {
656 perror("setsockopt");
661 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
669 # ifdef OPENSSL_SYS_WINDOWS
671 struct timeval *tv = (struct timeval *)ptr;
673 sz.i = sizeof(timeout);
674 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
675 (void *)&timeout, &sz.i) < 0) {
676 perror("getsockopt");
679 tv->tv_sec = timeout / 1000;
680 tv->tv_usec = (timeout % 1000) * 1000;
684 sz.i = sizeof(struct timeval);
685 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
686 ptr, (void *)&sz) < 0) {
687 perror("getsockopt");
689 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
690 OPENSSL_assert(sz.s <= sizeof(struct timeval));
698 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
700 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
701 # ifdef OPENSSL_SYS_WINDOWS
702 d_errno = (data->_errno == WSAETIMEDOUT);
704 d_errno = (data->_errno == EAGAIN);
713 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
714 if (data->_errno == EMSGSIZE) {
721 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
722 sockopt_val = num ? 1 : 0;
724 switch (data->peer.sa.sa_family) {
726 # if defined(IP_DONTFRAG)
727 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
728 &sockopt_val, sizeof(sockopt_val))) < 0) {
729 perror("setsockopt");
732 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
733 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
734 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
735 &sockopt_val, sizeof(sockopt_val))) < 0) {
736 perror("setsockopt");
739 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
740 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
741 (const char *)&sockopt_val,
742 sizeof(sockopt_val))) < 0) {
743 perror("setsockopt");
750 # if OPENSSL_USE_IPV6
752 # if defined(IPV6_DONTFRAG)
753 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
754 (const void *)&sockopt_val,
755 sizeof(sockopt_val))) < 0) {
756 perror("setsockopt");
759 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
760 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
761 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
762 &sockopt_val, sizeof(sockopt_val))) < 0) {
763 perror("setsockopt");
776 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
777 ret = dgram_get_mtu_overhead(data);
781 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
782 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
783 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
784 * value has been updated to a non-clashing value. However to preserve
785 * binary compatibility we now respond to both the old value and the new one
787 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
788 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
789 data->peekmode = (unsigned int)num;
798 static int dgram_puts(BIO *bp, const char *str)
803 ret = dgram_write(bp, str, n);
807 # ifndef OPENSSL_NO_SCTP
808 const BIO_METHOD *BIO_s_datagram_sctp(void)
810 return &methods_dgramp_sctp;
813 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
816 int ret, optval = 20000;
817 int auth_data = 0, auth_forward = 0;
819 struct sctp_authchunk auth;
820 struct sctp_authchunks *authchunks;
821 socklen_t sockopt_len;
822 # ifdef SCTP_AUTHENTICATION_EVENT
824 struct sctp_event event;
826 struct sctp_event_subscribe event;
830 bio = BIO_new(BIO_s_datagram_sctp());
833 BIO_set_fd(bio, fd, close_flag);
835 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
836 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
838 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
839 sizeof(struct sctp_authchunk));
842 ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
843 "Ensure SCTP AUTH chunks are enabled in kernel");
846 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
848 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
849 sizeof(struct sctp_authchunk));
852 ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
853 "Ensure SCTP AUTH chunks are enabled in kernel");
858 * Test if activation was successful. When using accept(), SCTP-AUTH has
859 * to be activated for the listening socket already, otherwise the
860 * connected socket won't use it. Similarly with connect(): the socket
861 * prior to connection must be activated for SCTP-AUTH
863 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
864 authchunks = OPENSSL_zalloc(sockopt_len);
865 if (authchunks == NULL) {
869 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
872 OPENSSL_free(authchunks);
877 for (p = (unsigned char *)authchunks->gauth_chunks;
878 p < (unsigned char *)authchunks + sockopt_len;
879 p += sizeof(uint8_t)) {
880 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
882 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
886 OPENSSL_free(authchunks);
888 if (!auth_data || !auth_forward) {
890 ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
891 "Ensure SCTP AUTH chunks are enabled on the "
892 "underlying socket");
896 # ifdef SCTP_AUTHENTICATION_EVENT
898 memset(&event, 0, sizeof(event));
899 event.se_assoc_id = 0;
900 event.se_type = SCTP_AUTHENTICATION_EVENT;
903 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
904 sizeof(struct sctp_event));
910 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
911 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
917 event.sctp_authentication_event = 1;
920 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
921 sizeof(struct sctp_event_subscribe));
930 * Disable partial delivery by setting the min size larger than the max
931 * record size of 2^14 + 2048 + 13
934 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
944 int BIO_dgram_is_sctp(BIO *bio)
946 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
949 static int dgram_sctp_new(BIO *bi)
951 bio_dgram_sctp_data *data = NULL;
955 if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL) {
956 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
959 # ifdef SCTP_PR_SCTP_NONE
960 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
968 static int dgram_sctp_free(BIO *a)
970 bio_dgram_sctp_data *data;
977 data = (bio_dgram_sctp_data *) a->ptr;
984 # ifdef SCTP_AUTHENTICATION_EVENT
985 void dgram_sctp_handle_auth_free_key_event(BIO *b,
986 union sctp_notification *snp)
989 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
991 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
992 struct sctp_authkeyid authkeyid;
995 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
996 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
997 &authkeyid, sizeof(struct sctp_authkeyid));
1002 static int dgram_sctp_read(BIO *b, char *out, int outl)
1004 int ret = 0, n = 0, i, optval;
1006 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1009 struct cmsghdr *cmsg;
1013 clear_socket_error();
1016 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1019 msg.msg_name = NULL;
1020 msg.msg_namelen = 0;
1023 msg.msg_control = cmsgbuf;
1024 msg.msg_controllen = 512;
1026 n = recvmsg(b->num, &msg, 0);
1034 if (msg.msg_controllen > 0) {
1035 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1036 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1037 if (cmsg->cmsg_level != IPPROTO_SCTP)
1039 # ifdef SCTP_RCVINFO
1040 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1041 struct sctp_rcvinfo *rcvinfo;
1043 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1044 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1045 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1046 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1047 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1048 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1049 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1050 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1054 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1055 struct sctp_sndrcvinfo *sndrcvinfo;
1058 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1059 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1060 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1061 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1062 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1063 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1064 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1065 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1071 if (msg.msg_flags & MSG_NOTIFICATION) {
1072 union sctp_notification snp;
1074 memcpy(&snp, out, sizeof(snp));
1075 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1077 struct sctp_event event;
1079 struct sctp_event_subscribe event;
1080 socklen_t eventsize;
1083 /* disable sender dry event */
1085 memset(&event, 0, sizeof(event));
1086 event.se_assoc_id = 0;
1087 event.se_type = SCTP_SENDER_DRY_EVENT;
1089 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1090 sizeof(struct sctp_event));
1096 eventsize = sizeof(struct sctp_event_subscribe);
1097 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1104 event.sctp_sender_dry_event = 0;
1106 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1107 sizeof(struct sctp_event_subscribe));
1114 # ifdef SCTP_AUTHENTICATION_EVENT
1115 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1116 dgram_sctp_handle_auth_free_key_event(b, &snp);
1119 if (data->handle_notifications != NULL)
1120 data->handle_notifications(b, data->notification_context,
1123 memset(&snp, 0, sizeof(snp));
1124 memset(out, 0, outl);
1129 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1132 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1133 /* Partial message read, this should never happen! */
1136 * The buffer was too small, this means the peer sent a message
1137 * that was larger than allowed.
1143 * Test if socket buffer can handle max record size (2^14 + 2048
1146 optlen = (socklen_t) sizeof(int);
1147 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1149 OPENSSL_assert(optval >= 18445);
1152 * Test if SCTP doesn't partially deliver below max record size
1153 * (2^14 + 2048 + 13)
1155 optlen = (socklen_t) sizeof(int);
1157 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1160 OPENSSL_assert(optval >= 18445);
1163 * Partially delivered notification??? Probably a bug....
1165 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1168 * Everything seems ok till now, so it's most likely a message
1169 * dropped by PR-SCTP.
1171 memset(out, 0, outl);
1172 BIO_set_retry_read(b);
1176 BIO_clear_retry_flags(b);
1178 if (BIO_dgram_should_retry(ret)) {
1179 BIO_set_retry_read(b);
1180 data->_errno = get_last_socket_error();
1184 /* Test if peer uses SCTP-AUTH before continuing */
1185 if (!data->peer_auth_tested) {
1186 int ii, auth_data = 0, auth_forward = 0;
1188 struct sctp_authchunks *authchunks;
1191 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1192 authchunks = OPENSSL_malloc(optlen);
1193 if (authchunks == NULL) {
1194 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
1197 memset(authchunks, 0, optlen);
1198 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1199 authchunks, &optlen);
1202 for (p = (unsigned char *)authchunks->gauth_chunks;
1203 p < (unsigned char *)authchunks + optlen;
1204 p += sizeof(uint8_t)) {
1205 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1207 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1211 OPENSSL_free(authchunks);
1213 if (!auth_data || !auth_forward) {
1214 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
1218 data->peer_auth_tested = 1;
1225 * dgram_sctp_write - send message on SCTP socket
1226 * @b: BIO to write to
1228 * @inl: amount of bytes in @in to send
1230 * Returns -1 on error or the sent amount of bytes on success
1232 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1235 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1236 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1237 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1238 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1239 struct iovec iov[1];
1241 struct cmsghdr *cmsg;
1242 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1243 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1244 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1245 struct sctp_sndinfo *sndinfo;
1246 struct sctp_prinfo *prinfo;
1248 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1249 struct sctp_sndrcvinfo *sndrcvinfo;
1252 clear_socket_error();
1255 * If we're send anything else than application data, disable all user
1256 * parameters and flags.
1259 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1260 # ifdef SCTP_SACK_IMMEDIATELY
1261 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1263 sinfo = &handshake_sinfo;
1266 /* We can only send a shutdown alert if the socket is dry */
1267 if (data->save_shutdown) {
1268 ret = BIO_dgram_sctp_wait_for_dry(b);
1272 BIO_clear_retry_flags(b);
1273 BIO_set_retry_write(b);
1278 iov[0].iov_base = (char *)in;
1279 iov[0].iov_len = inl;
1280 msg.msg_name = NULL;
1281 msg.msg_namelen = 0;
1284 msg.msg_control = (caddr_t) cmsgbuf;
1285 msg.msg_controllen = 0;
1287 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1288 cmsg = (struct cmsghdr *)cmsgbuf;
1289 cmsg->cmsg_level = IPPROTO_SCTP;
1290 cmsg->cmsg_type = SCTP_SNDINFO;
1291 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1292 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1293 memset(sndinfo, 0, sizeof(*sndinfo));
1294 sndinfo->snd_sid = sinfo->snd_sid;
1295 sndinfo->snd_flags = sinfo->snd_flags;
1296 sndinfo->snd_ppid = sinfo->snd_ppid;
1297 sndinfo->snd_context = sinfo->snd_context;
1298 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1301 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1302 cmsg->cmsg_level = IPPROTO_SCTP;
1303 cmsg->cmsg_type = SCTP_PRINFO;
1304 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1305 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1306 memset(prinfo, 0, sizeof(*prinfo));
1307 prinfo->pr_policy = pinfo->pr_policy;
1308 prinfo->pr_value = pinfo->pr_value;
1309 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1311 cmsg = (struct cmsghdr *)cmsgbuf;
1312 cmsg->cmsg_level = IPPROTO_SCTP;
1313 cmsg->cmsg_type = SCTP_SNDRCV;
1314 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1315 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1316 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1317 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1318 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1320 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1322 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1323 sndrcvinfo->sinfo_context = sinfo->snd_context;
1324 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1325 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1328 ret = sendmsg(b->num, &msg, 0);
1330 BIO_clear_retry_flags(b);
1332 if (BIO_dgram_should_retry(ret)) {
1333 BIO_set_retry_write(b);
1334 data->_errno = get_last_socket_error();
1340 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1343 bio_dgram_sctp_data *data = NULL;
1344 socklen_t sockopt_len = 0;
1345 struct sctp_authkeyid authkeyid;
1346 struct sctp_authkey *authkey = NULL;
1348 data = (bio_dgram_sctp_data *) b->ptr;
1351 case BIO_CTRL_DGRAM_QUERY_MTU:
1353 * Set to maximum (2^14) and ignore user input to enable transport
1354 * protocol fragmentation. Returns always 2^14.
1359 case BIO_CTRL_DGRAM_SET_MTU:
1361 * Set to maximum (2^14) and ignore input to enable transport
1362 * protocol fragmentation. Returns always 2^14.
1367 case BIO_CTRL_DGRAM_SET_CONNECTED:
1368 case BIO_CTRL_DGRAM_CONNECT:
1369 /* Returns always -1. */
1372 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1374 * SCTP doesn't need the DTLS timer Returns always 1.
1377 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1379 * We allow transport protocol fragmentation so this is irrelevant
1383 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1385 data->in_handshake = 1;
1387 data->in_handshake = 0;
1390 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1391 &data->in_handshake, sizeof(int));
1393 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1395 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1398 /* Get active key */
1399 sockopt_len = sizeof(struct sctp_authkeyid);
1401 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1407 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1408 authkey = OPENSSL_malloc(sockopt_len);
1409 if (authkey == NULL) {
1413 memset(authkey, 0, sockopt_len);
1414 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1415 # ifndef __FreeBSD__
1417 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1418 * and higher work without it.
1420 authkey->sca_keylength = 64;
1422 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1425 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1427 OPENSSL_free(authkey);
1432 /* Reset active key */
1433 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1434 &authkeyid, sizeof(struct sctp_authkeyid));
1439 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1440 /* Returns 0 on success, -1 otherwise. */
1442 /* Get active key */
1443 sockopt_len = sizeof(struct sctp_authkeyid);
1445 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1450 /* Set active key */
1451 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1452 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1453 &authkeyid, sizeof(struct sctp_authkeyid));
1458 * CCS has been sent, so remember that and fall through to check if
1459 * we need to deactivate an old key
1464 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1465 /* Returns 0 on success, -1 otherwise. */
1468 * Has this command really been called or is this just a
1471 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1475 * CSS has been both, received and sent, so deactivate an old key
1477 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1478 /* Get active key */
1479 sockopt_len = sizeof(struct sctp_authkeyid);
1481 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1482 &authkeyid, &sockopt_len);
1487 * Deactivate key or delete second last key if
1488 * SCTP_AUTHENTICATION_EVENT is not available.
1490 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1491 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1492 sockopt_len = sizeof(struct sctp_authkeyid);
1493 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1494 &authkeyid, sockopt_len);
1498 # ifndef SCTP_AUTHENTICATION_EVENT
1499 if (authkeyid.scact_keynumber > 0) {
1500 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1501 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1502 &authkeyid, sizeof(struct sctp_authkeyid));
1512 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1513 /* Returns the size of the copied struct. */
1514 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1515 num = sizeof(struct bio_dgram_sctp_sndinfo);
1517 memcpy(ptr, &(data->sndinfo), num);
1520 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1521 /* Returns the size of the copied struct. */
1522 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1523 num = sizeof(struct bio_dgram_sctp_sndinfo);
1525 memcpy(&(data->sndinfo), ptr, num);
1527 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1528 /* Returns the size of the copied struct. */
1529 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1530 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1532 memcpy(ptr, &data->rcvinfo, num);
1536 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1537 /* Returns the size of the copied struct. */
1538 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1539 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1541 memcpy(&(data->rcvinfo), ptr, num);
1543 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1544 /* Returns the size of the copied struct. */
1545 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1546 num = sizeof(struct bio_dgram_sctp_prinfo);
1548 memcpy(ptr, &(data->prinfo), num);
1551 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1552 /* Returns the size of the copied struct. */
1553 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1554 num = sizeof(struct bio_dgram_sctp_prinfo);
1556 memcpy(&(data->prinfo), ptr, num);
1558 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1559 /* Returns always 1. */
1561 data->save_shutdown = 1;
1563 data->save_shutdown = 0;
1565 case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
1566 return dgram_sctp_wait_for_dry(b);
1567 case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
1568 return dgram_sctp_msg_waiting(b);
1572 * Pass to default ctrl function to process SCTP unspecific commands
1574 ret = dgram_ctrl(b, cmd, num, ptr);
1580 int BIO_dgram_sctp_notification_cb(BIO *b,
1581 BIO_dgram_sctp_notification_handler_fn handle_notifications,
1584 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1586 if (handle_notifications != NULL) {
1587 data->handle_notifications = handle_notifications;
1588 data->notification_context = context;
1596 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1597 * @b: The BIO to check for the dry event
1599 * Wait until the peer confirms all packets have been received, and so that
1600 * our kernel doesn't have anything to send anymore. This is only received by
1601 * the peer's kernel, not the application.
1605 * 0 when not dry yet
1608 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1610 return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
1613 static int dgram_sctp_wait_for_dry(BIO *b)
1618 union sctp_notification snp;
1622 struct sctp_event event;
1624 struct sctp_event_subscribe event;
1625 socklen_t eventsize;
1627 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1629 /* set sender dry event */
1631 memset(&event, 0, sizeof(event));
1632 event.se_assoc_id = 0;
1633 event.se_type = SCTP_SENDER_DRY_EVENT;
1636 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1637 sizeof(struct sctp_event));
1639 eventsize = sizeof(struct sctp_event_subscribe);
1640 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1644 event.sctp_sender_dry_event = 1;
1647 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1648 sizeof(struct sctp_event_subscribe));
1653 /* peek for notification */
1654 memset(&snp, 0, sizeof(snp));
1655 iov.iov_base = (char *)&snp;
1656 iov.iov_len = sizeof(union sctp_notification);
1657 msg.msg_name = NULL;
1658 msg.msg_namelen = 0;
1661 msg.msg_control = NULL;
1662 msg.msg_controllen = 0;
1665 n = recvmsg(b->num, &msg, MSG_PEEK);
1667 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1668 && (get_last_socket_error() != EWOULDBLOCK))
1674 /* if we find a notification, process it and try again if necessary */
1675 while (msg.msg_flags & MSG_NOTIFICATION) {
1676 memset(&snp, 0, sizeof(snp));
1677 iov.iov_base = (char *)&snp;
1678 iov.iov_len = sizeof(union sctp_notification);
1679 msg.msg_name = NULL;
1680 msg.msg_namelen = 0;
1683 msg.msg_control = NULL;
1684 msg.msg_controllen = 0;
1687 n = recvmsg(b->num, &msg, 0);
1689 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1690 && (get_last_socket_error() != EWOULDBLOCK))
1696 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1699 /* disable sender dry event */
1701 memset(&event, 0, sizeof(event));
1702 event.se_assoc_id = 0;
1703 event.se_type = SCTP_SENDER_DRY_EVENT;
1706 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1707 sizeof(struct sctp_event));
1709 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1711 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1716 event.sctp_sender_dry_event = 0;
1719 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1720 sizeof(struct sctp_event_subscribe));
1725 # ifdef SCTP_AUTHENTICATION_EVENT
1726 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1727 dgram_sctp_handle_auth_free_key_event(b, &snp);
1730 if (data->handle_notifications != NULL)
1731 data->handle_notifications(b, data->notification_context,
1734 /* found notification, peek again */
1735 memset(&snp, 0, sizeof(snp));
1736 iov.iov_base = (char *)&snp;
1737 iov.iov_len = sizeof(union sctp_notification);
1738 msg.msg_name = NULL;
1739 msg.msg_namelen = 0;
1742 msg.msg_control = NULL;
1743 msg.msg_controllen = 0;
1746 /* if we have seen the dry already, don't wait */
1748 sockflags = fcntl(b->num, F_GETFL, 0);
1749 fcntl(b->num, F_SETFL, O_NONBLOCK);
1752 n = recvmsg(b->num, &msg, MSG_PEEK);
1755 fcntl(b->num, F_SETFL, sockflags);
1759 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1760 && (get_last_socket_error() != EWOULDBLOCK))
1767 /* read anything else */
1771 int BIO_dgram_sctp_msg_waiting(BIO *b)
1773 return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
1776 static int dgram_sctp_msg_waiting(BIO *b)
1779 union sctp_notification snp;
1782 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1784 /* Check if there are any messages waiting to be read */
1786 memset(&snp, 0, sizeof(snp));
1787 iov.iov_base = (char *)&snp;
1788 iov.iov_len = sizeof(union sctp_notification);
1789 msg.msg_name = NULL;
1790 msg.msg_namelen = 0;
1793 msg.msg_control = NULL;
1794 msg.msg_controllen = 0;
1797 sockflags = fcntl(b->num, F_GETFL, 0);
1798 fcntl(b->num, F_SETFL, O_NONBLOCK);
1799 n = recvmsg(b->num, &msg, MSG_PEEK);
1800 fcntl(b->num, F_SETFL, sockflags);
1802 /* if notification, process and try again */
1803 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1804 # ifdef SCTP_AUTHENTICATION_EVENT
1805 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1806 dgram_sctp_handle_auth_free_key_event(b, &snp);
1809 memset(&snp, 0, sizeof(snp));
1810 iov.iov_base = (char *)&snp;
1811 iov.iov_len = sizeof(union sctp_notification);
1812 msg.msg_name = NULL;
1813 msg.msg_namelen = 0;
1816 msg.msg_control = NULL;
1817 msg.msg_controllen = 0;
1819 n = recvmsg(b->num, &msg, 0);
1821 if (data->handle_notifications != NULL)
1822 data->handle_notifications(b, data->notification_context,
1826 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1828 /* Return 1 if there is a message to be read, return 0 otherwise. */
1835 static int dgram_sctp_puts(BIO *bp, const char *str)
1840 ret = dgram_sctp_write(bp, str, n);
1845 static int BIO_dgram_should_retry(int i)
1849 if ((i == 0) || (i == -1)) {
1850 err = get_last_socket_error();
1852 # if defined(OPENSSL_SYS_WINDOWS)
1854 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1855 * this point, the error code was overwritten by another system call
1856 * before this error handling is called.
1860 return BIO_dgram_non_fatal_error(err);
1865 int BIO_dgram_non_fatal_error(int err)
1868 # if defined(OPENSSL_SYS_WINDOWS)
1869 # if defined(WSAEWOULDBLOCK)
1870 case WSAEWOULDBLOCK:
1875 # ifdef WSAEWOULDBLOCK
1876 # if WSAEWOULDBLOCK != EWOULDBLOCK
1889 # if EWOULDBLOCK != EAGAIN
1913 static void get_current_time(struct timeval *t)
1915 # if defined(_WIN32)
1918 unsigned __int64 ul;
1923 SystemTimeToFileTime(&st, &now.ft);
1925 now.ul -= 116444736000000000ULL;
1927 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1929 t->tv_sec = (long)(now.ul / 10000000);
1930 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1932 gettimeofday(t, NULL);