2 * Copyright 2005-2018 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 = {
71 /* TODO: Convert to new style write function */
74 /* TODO: Convert to new style read function */
78 NULL, /* dgram_gets, */
82 NULL, /* dgram_callback_ctrl */
85 # ifndef OPENSSL_NO_SCTP
86 static const BIO_METHOD methods_dgramp_sctp = {
88 "datagram sctp socket",
89 /* TODO: Convert to new style write function */
92 /* TODO: Convert to new style write function */
96 NULL, /* dgram_gets, */
100 NULL, /* dgram_callback_ctrl */
104 typedef struct bio_dgram_data_st {
106 unsigned int connected;
109 struct timeval next_timeout;
110 struct timeval socket_timeout;
111 unsigned int peekmode;
114 # ifndef OPENSSL_NO_SCTP
115 typedef struct bio_dgram_sctp_save_message_st {
119 } bio_dgram_sctp_save_message;
121 typedef struct bio_dgram_sctp_data_st {
123 unsigned int connected;
126 struct bio_dgram_sctp_sndinfo sndinfo;
127 struct bio_dgram_sctp_rcvinfo rcvinfo;
128 struct bio_dgram_sctp_prinfo prinfo;
129 BIO_dgram_sctp_notification_handler_fn handle_notifications;
130 void *notification_context;
135 int peer_auth_tested;
136 } bio_dgram_sctp_data;
139 const BIO_METHOD *BIO_s_datagram(void)
141 return &methods_dgramp;
144 BIO *BIO_new_dgram(int fd, int close_flag)
148 ret = BIO_new(BIO_s_datagram());
151 BIO_set_fd(ret, fd, close_flag);
155 static int dgram_new(BIO *bi)
157 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
165 static int dgram_free(BIO *a)
167 bio_dgram_data *data;
174 data = (bio_dgram_data *)a->ptr;
180 static int dgram_clear(BIO *a)
186 BIO_closesocket(a->num);
194 static void dgram_adjust_rcv_timeout(BIO *b)
196 # if defined(SO_RCVTIMEO)
197 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
205 /* Is a timer active? */
206 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
207 struct timeval timenow, timeleft;
209 /* Read current socket timeout */
210 # ifdef OPENSSL_SYS_WINDOWS
213 sz.i = sizeof(timeout);
214 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
215 (void *)&timeout, &sz.i) < 0) {
216 perror("getsockopt");
218 data->socket_timeout.tv_sec = timeout / 1000;
219 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
222 sz.i = sizeof(data->socket_timeout);
223 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
224 &(data->socket_timeout), (void *)&sz) < 0) {
225 perror("getsockopt");
226 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
227 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
230 /* Get current time */
231 get_current_time(&timenow);
233 /* Calculate time left until timer expires */
234 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
235 if (timeleft.tv_usec < timenow.tv_usec) {
236 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
239 timeleft.tv_usec -= timenow.tv_usec;
241 if (timeleft.tv_sec < timenow.tv_sec) {
243 timeleft.tv_usec = 1;
245 timeleft.tv_sec -= timenow.tv_sec;
249 * Adjust socket timeout if next handshake message timer will expire
252 if ((data->socket_timeout.tv_sec == 0
253 && data->socket_timeout.tv_usec == 0)
254 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
255 || (data->socket_timeout.tv_sec == timeleft.tv_sec
256 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
257 # ifdef OPENSSL_SYS_WINDOWS
258 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
259 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
260 (void *)&timeout, sizeof(timeout)) < 0) {
261 perror("setsockopt");
264 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
265 sizeof(struct timeval)) < 0) {
266 perror("setsockopt");
274 static void dgram_reset_rcv_timeout(BIO *b)
276 # if defined(SO_RCVTIMEO)
277 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
279 /* Is a timer active? */
280 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
281 # ifdef OPENSSL_SYS_WINDOWS
282 int timeout = data->socket_timeout.tv_sec * 1000 +
283 data->socket_timeout.tv_usec / 1000;
284 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
285 (void *)&timeout, sizeof(timeout)) < 0) {
286 perror("setsockopt");
290 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
291 sizeof(struct timeval)) < 0) {
292 perror("setsockopt");
299 static int dgram_read(BIO *b, char *out, int outl)
302 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
306 socklen_t len = sizeof(peer);
309 clear_socket_error();
310 memset(&peer, 0, sizeof(peer));
311 dgram_adjust_rcv_timeout(b);
314 ret = recvfrom(b->num, out, outl, flags,
315 BIO_ADDR_sockaddr_noconst(&peer), &len);
317 if (!data->connected && ret >= 0)
318 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
320 BIO_clear_retry_flags(b);
322 if (BIO_dgram_should_retry(ret)) {
323 BIO_set_retry_read(b);
324 data->_errno = get_last_socket_error();
328 dgram_reset_rcv_timeout(b);
333 static int dgram_write(BIO *b, const char *in, int inl)
336 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
337 clear_socket_error();
340 ret = writesocket(b->num, in, inl);
342 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
344 ret = sendto(b->num, in, inl, 0,
345 BIO_ADDR_sockaddr(&data->peer), peerlen);
348 BIO_clear_retry_flags(b);
350 if (BIO_dgram_should_retry(ret)) {
351 BIO_set_retry_write(b);
352 data->_errno = get_last_socket_error();
358 static long dgram_get_mtu_overhead(bio_dgram_data *data)
362 switch (BIO_ADDR_family(&data->peer)) {
365 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
369 # if OPENSSL_USE_IPV6
372 # ifdef IN6_IS_ADDR_V4MAPPED
373 struct in6_addr tmp_addr;
374 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
375 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
377 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
383 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
390 /* We don't know. Go with the historical default */
397 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
401 bio_dgram_data *data = NULL;
404 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
405 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
406 * modern enough to define socklen_t */
411 data = (bio_dgram_data *)b->ptr;
423 b->num = *((int *)ptr);
424 b->shutdown = (int)num;
436 case BIO_CTRL_GET_CLOSE:
439 case BIO_CTRL_SET_CLOSE:
440 b->shutdown = (int)num;
442 case BIO_CTRL_PENDING:
443 case BIO_CTRL_WPENDING:
450 case BIO_CTRL_DGRAM_CONNECT:
451 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
453 /* (Linux)kernel sets DF bit on outgoing IP packets */
454 case BIO_CTRL_DGRAM_MTU_DISCOVER:
455 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
456 addr_len = (socklen_t) sizeof(addr);
457 memset(&addr, 0, sizeof(addr));
458 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
462 switch (addr.sa.sa_family) {
464 sockopt_val = IP_PMTUDISC_DO;
465 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
466 &sockopt_val, sizeof(sockopt_val))) < 0)
467 perror("setsockopt");
469 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
471 sockopt_val = IPV6_PMTUDISC_DO;
472 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
473 &sockopt_val, sizeof(sockopt_val))) < 0)
474 perror("setsockopt");
485 case BIO_CTRL_DGRAM_QUERY_MTU:
486 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
487 addr_len = (socklen_t) sizeof(addr);
488 memset(&addr, 0, sizeof(addr));
489 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
493 sockopt_len = sizeof(sockopt_val);
494 switch (addr.sa.sa_family) {
497 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
498 &sockopt_len)) < 0 || sockopt_val < 0) {
502 * we assume that the transport protocol is UDP and no IP
505 data->mtu = sockopt_val - 8 - 20;
509 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
512 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
513 (void *)&sockopt_val, &sockopt_len)) < 0
514 || sockopt_val < 0) {
518 * we assume that the transport protocol is UDP and no IPV6
521 data->mtu = sockopt_val - 8 - 40;
534 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
535 ret = -dgram_get_mtu_overhead(data);
536 switch (BIO_ADDR_family(&data->peer)) {
540 # if OPENSSL_USE_IPV6
543 # ifdef IN6_IS_ADDR_V4MAPPED
544 struct in6_addr tmp_addr;
545 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
546 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
559 case BIO_CTRL_DGRAM_GET_MTU:
561 case BIO_CTRL_DGRAM_SET_MTU:
565 case BIO_CTRL_DGRAM_SET_CONNECTED:
568 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
571 memset(&data->peer, 0, sizeof(data->peer));
574 case BIO_CTRL_DGRAM_GET_PEER:
575 ret = BIO_ADDR_sockaddr_size(&data->peer);
576 /* FIXME: if num < ret, we will only return part of an address.
577 That should bee an error, no? */
578 if (num == 0 || num > ret)
580 memcpy(ptr, &data->peer, (ret = num));
582 case BIO_CTRL_DGRAM_SET_PEER:
583 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
585 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
586 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
588 # if defined(SO_RCVTIMEO)
589 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
590 # ifdef OPENSSL_SYS_WINDOWS
592 struct timeval *tv = (struct timeval *)ptr;
593 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
594 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
595 (void *)&timeout, sizeof(timeout)) < 0) {
596 perror("setsockopt");
601 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
602 sizeof(struct timeval)) < 0) {
603 perror("setsockopt");
608 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
616 # ifdef OPENSSL_SYS_WINDOWS
618 struct timeval *tv = (struct timeval *)ptr;
620 sz.i = sizeof(timeout);
621 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
622 (void *)&timeout, &sz.i) < 0) {
623 perror("getsockopt");
626 tv->tv_sec = timeout / 1000;
627 tv->tv_usec = (timeout % 1000) * 1000;
631 sz.i = sizeof(struct timeval);
632 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
633 ptr, (void *)&sz) < 0) {
634 perror("getsockopt");
636 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
637 OPENSSL_assert(sz.s <= sizeof(struct timeval));
645 # if defined(SO_SNDTIMEO)
646 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
647 # ifdef OPENSSL_SYS_WINDOWS
649 struct timeval *tv = (struct timeval *)ptr;
650 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
651 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
652 (void *)&timeout, sizeof(timeout)) < 0) {
653 perror("setsockopt");
658 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
659 sizeof(struct timeval)) < 0) {
660 perror("setsockopt");
665 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
673 # ifdef OPENSSL_SYS_WINDOWS
675 struct timeval *tv = (struct timeval *)ptr;
677 sz.i = sizeof(timeout);
678 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
679 (void *)&timeout, &sz.i) < 0) {
680 perror("getsockopt");
683 tv->tv_sec = timeout / 1000;
684 tv->tv_usec = (timeout % 1000) * 1000;
688 sz.i = sizeof(struct timeval);
689 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
690 ptr, (void *)&sz) < 0) {
691 perror("getsockopt");
693 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
694 OPENSSL_assert(sz.s <= sizeof(struct timeval));
702 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
704 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
705 # ifdef OPENSSL_SYS_WINDOWS
706 d_errno = (data->_errno == WSAETIMEDOUT);
708 d_errno = (data->_errno == EAGAIN);
717 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
718 if (data->_errno == EMSGSIZE) {
725 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
726 sockopt_val = num ? 1 : 0;
728 switch (data->peer.sa.sa_family) {
730 # if defined(IP_DONTFRAG)
731 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
732 &sockopt_val, sizeof(sockopt_val))) < 0) {
733 perror("setsockopt");
736 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
737 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
738 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
739 &sockopt_val, sizeof(sockopt_val))) < 0) {
740 perror("setsockopt");
743 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
744 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
745 (const char *)&sockopt_val,
746 sizeof(sockopt_val))) < 0) {
747 perror("setsockopt");
754 # if OPENSSL_USE_IPV6
756 # if defined(IPV6_DONTFRAG)
757 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
758 (const void *)&sockopt_val,
759 sizeof(sockopt_val))) < 0) {
760 perror("setsockopt");
763 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
764 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
765 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
766 &sockopt_val, sizeof(sockopt_val))) < 0) {
767 perror("setsockopt");
780 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
781 ret = dgram_get_mtu_overhead(data);
785 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
786 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
787 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
788 * value has been updated to a non-clashing value. However to preserve
789 * binary compatibility we now respond to both the old value and the new one
791 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
792 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
793 data->peekmode = (unsigned int)num;
802 static int dgram_puts(BIO *bp, const char *str)
807 ret = dgram_write(bp, str, n);
811 # ifndef OPENSSL_NO_SCTP
812 const BIO_METHOD *BIO_s_datagram_sctp(void)
814 return &methods_dgramp_sctp;
817 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
820 int ret, optval = 20000;
821 int auth_data = 0, auth_forward = 0;
823 struct sctp_authchunk auth;
824 struct sctp_authchunks *authchunks;
825 socklen_t sockopt_len;
826 # ifdef SCTP_AUTHENTICATION_EVENT
828 struct sctp_event event;
830 struct sctp_event_subscribe event;
834 bio = BIO_new(BIO_s_datagram_sctp());
837 BIO_set_fd(bio, fd, close_flag);
839 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
840 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
842 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
843 sizeof(struct sctp_authchunk));
846 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
847 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
850 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
852 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
853 sizeof(struct sctp_authchunk));
856 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
857 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
862 * Test if activation was successful. When using accept(), SCTP-AUTH has
863 * to be activated for the listening socket already, otherwise the
864 * connected socket won't use it. Similarly with connect(): the socket
865 * prior to connection must be activated for SCTP-AUTH
867 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
868 authchunks = OPENSSL_zalloc(sockopt_len);
869 if (authchunks == NULL) {
873 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
876 OPENSSL_free(authchunks);
881 for (p = (unsigned char *)authchunks->gauth_chunks;
882 p < (unsigned char *)authchunks + sockopt_len;
883 p += sizeof(uint8_t)) {
884 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
886 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
890 OPENSSL_free(authchunks);
892 if (!auth_data || !auth_forward) {
894 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
895 ERR_add_error_data(1,
896 "Ensure SCTP AUTH chunks are enabled on the "
897 "underlying socket");
901 # ifdef SCTP_AUTHENTICATION_EVENT
903 memset(&event, 0, sizeof(event));
904 event.se_assoc_id = 0;
905 event.se_type = SCTP_AUTHENTICATION_EVENT;
908 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
909 sizeof(struct sctp_event));
915 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
916 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
922 event.sctp_authentication_event = 1;
925 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
926 sizeof(struct sctp_event_subscribe));
935 * Disable partial delivery by setting the min size larger than the max
936 * record size of 2^14 + 2048 + 13
939 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
949 int BIO_dgram_is_sctp(BIO *bio)
951 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
954 static int dgram_sctp_new(BIO *bi)
956 bio_dgram_sctp_data *data = NULL;
960 if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL) {
961 BIOerr(BIO_F_DGRAM_SCTP_NEW, ERR_R_MALLOC_FAILURE);
964 # ifdef SCTP_PR_SCTP_NONE
965 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
973 static int dgram_sctp_free(BIO *a)
975 bio_dgram_sctp_data *data;
982 data = (bio_dgram_sctp_data *) a->ptr;
989 # ifdef SCTP_AUTHENTICATION_EVENT
990 void dgram_sctp_handle_auth_free_key_event(BIO *b,
991 union sctp_notification *snp)
994 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
996 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
997 struct sctp_authkeyid authkeyid;
1000 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1001 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1002 &authkeyid, sizeof(struct sctp_authkeyid));
1007 static int dgram_sctp_read(BIO *b, char *out, int outl)
1009 int ret = 0, n = 0, i, optval;
1011 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1014 struct cmsghdr *cmsg;
1018 clear_socket_error();
1021 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1024 msg.msg_name = NULL;
1025 msg.msg_namelen = 0;
1028 msg.msg_control = cmsgbuf;
1029 msg.msg_controllen = 512;
1031 n = recvmsg(b->num, &msg, 0);
1039 if (msg.msg_controllen > 0) {
1040 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1041 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1042 if (cmsg->cmsg_level != IPPROTO_SCTP)
1044 # ifdef SCTP_RCVINFO
1045 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1046 struct sctp_rcvinfo *rcvinfo;
1048 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1049 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1050 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1051 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1052 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1053 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1054 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1055 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1059 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1060 struct sctp_sndrcvinfo *sndrcvinfo;
1063 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1064 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1065 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1066 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1067 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1068 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1069 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1070 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1076 if (msg.msg_flags & MSG_NOTIFICATION) {
1077 union sctp_notification snp;
1079 memcpy(&snp, out, sizeof(snp));
1080 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1082 struct sctp_event event;
1084 struct sctp_event_subscribe event;
1085 socklen_t eventsize;
1088 /* disable sender dry event */
1090 memset(&event, 0, sizeof(event));
1091 event.se_assoc_id = 0;
1092 event.se_type = SCTP_SENDER_DRY_EVENT;
1094 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1095 sizeof(struct sctp_event));
1101 eventsize = sizeof(struct sctp_event_subscribe);
1102 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1109 event.sctp_sender_dry_event = 0;
1111 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1112 sizeof(struct sctp_event_subscribe));
1119 # ifdef SCTP_AUTHENTICATION_EVENT
1120 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1121 dgram_sctp_handle_auth_free_key_event(b, &snp);
1124 if (data->handle_notifications != NULL)
1125 data->handle_notifications(b, data->notification_context,
1128 memset(&snp, 0, sizeof(snp));
1129 memset(out, 0, outl);
1134 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1137 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1138 /* Partial message read, this should never happen! */
1141 * The buffer was too small, this means the peer sent a message
1142 * that was larger than allowed.
1148 * Test if socket buffer can handle max record size (2^14 + 2048
1151 optlen = (socklen_t) sizeof(int);
1152 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1154 OPENSSL_assert(optval >= 18445);
1157 * Test if SCTP doesn't partially deliver below max record size
1158 * (2^14 + 2048 + 13)
1160 optlen = (socklen_t) sizeof(int);
1162 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1165 OPENSSL_assert(optval >= 18445);
1168 * Partially delivered notification??? Probably a bug....
1170 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1173 * Everything seems ok till now, so it's most likely a message
1174 * dropped by PR-SCTP.
1176 memset(out, 0, outl);
1177 BIO_set_retry_read(b);
1181 BIO_clear_retry_flags(b);
1183 if (BIO_dgram_should_retry(ret)) {
1184 BIO_set_retry_read(b);
1185 data->_errno = get_last_socket_error();
1189 /* Test if peer uses SCTP-AUTH before continuing */
1190 if (!data->peer_auth_tested) {
1191 int ii, auth_data = 0, auth_forward = 0;
1193 struct sctp_authchunks *authchunks;
1196 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1197 authchunks = OPENSSL_malloc(optlen);
1198 if (authchunks == NULL) {
1199 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1202 memset(authchunks, 0, optlen);
1203 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1204 authchunks, &optlen);
1207 for (p = (unsigned char *)authchunks->gauth_chunks;
1208 p < (unsigned char *)authchunks + optlen;
1209 p += sizeof(uint8_t)) {
1210 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1212 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1216 OPENSSL_free(authchunks);
1218 if (!auth_data || !auth_forward) {
1219 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1223 data->peer_auth_tested = 1;
1230 * dgram_sctp_write - send message on SCTP socket
1231 * @b: BIO to write to
1233 * @inl: amount of bytes in @in to send
1235 * Returns -1 on error or the sent amount of bytes on success
1237 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1240 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1241 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1242 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1243 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1244 struct iovec iov[1];
1246 struct cmsghdr *cmsg;
1247 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1248 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1249 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1250 struct sctp_sndinfo *sndinfo;
1251 struct sctp_prinfo *prinfo;
1253 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1254 struct sctp_sndrcvinfo *sndrcvinfo;
1257 clear_socket_error();
1260 * If we're send anything else than application data, disable all user
1261 * parameters and flags.
1264 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1265 # ifdef SCTP_SACK_IMMEDIATELY
1266 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1268 sinfo = &handshake_sinfo;
1271 /* We can only send a shutdown alert if the socket is dry */
1272 if (data->save_shutdown) {
1273 ret = BIO_dgram_sctp_wait_for_dry(b);
1277 BIO_clear_retry_flags(b);
1278 BIO_set_retry_write(b);
1283 iov[0].iov_base = (char *)in;
1284 iov[0].iov_len = inl;
1285 msg.msg_name = NULL;
1286 msg.msg_namelen = 0;
1289 msg.msg_control = (caddr_t) cmsgbuf;
1290 msg.msg_controllen = 0;
1292 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1293 cmsg = (struct cmsghdr *)cmsgbuf;
1294 cmsg->cmsg_level = IPPROTO_SCTP;
1295 cmsg->cmsg_type = SCTP_SNDINFO;
1296 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1297 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1298 memset(sndinfo, 0, sizeof(*sndinfo));
1299 sndinfo->snd_sid = sinfo->snd_sid;
1300 sndinfo->snd_flags = sinfo->snd_flags;
1301 sndinfo->snd_ppid = sinfo->snd_ppid;
1302 sndinfo->snd_context = sinfo->snd_context;
1303 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1306 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1307 cmsg->cmsg_level = IPPROTO_SCTP;
1308 cmsg->cmsg_type = SCTP_PRINFO;
1309 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1310 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1311 memset(prinfo, 0, sizeof(*prinfo));
1312 prinfo->pr_policy = pinfo->pr_policy;
1313 prinfo->pr_value = pinfo->pr_value;
1314 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1316 cmsg = (struct cmsghdr *)cmsgbuf;
1317 cmsg->cmsg_level = IPPROTO_SCTP;
1318 cmsg->cmsg_type = SCTP_SNDRCV;
1319 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1320 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1321 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1322 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1323 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1325 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1327 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1328 sndrcvinfo->sinfo_context = sinfo->snd_context;
1329 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1330 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1333 ret = sendmsg(b->num, &msg, 0);
1335 BIO_clear_retry_flags(b);
1337 if (BIO_dgram_should_retry(ret)) {
1338 BIO_set_retry_write(b);
1339 data->_errno = get_last_socket_error();
1345 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1348 bio_dgram_sctp_data *data = NULL;
1349 socklen_t sockopt_len = 0;
1350 struct sctp_authkeyid authkeyid;
1351 struct sctp_authkey *authkey = NULL;
1353 data = (bio_dgram_sctp_data *) b->ptr;
1356 case BIO_CTRL_DGRAM_QUERY_MTU:
1358 * Set to maximum (2^14) and ignore user input to enable transport
1359 * protocol fragmentation. Returns always 2^14.
1364 case BIO_CTRL_DGRAM_SET_MTU:
1366 * Set to maximum (2^14) and ignore input to enable transport
1367 * protocol fragmentation. Returns always 2^14.
1372 case BIO_CTRL_DGRAM_SET_CONNECTED:
1373 case BIO_CTRL_DGRAM_CONNECT:
1374 /* Returns always -1. */
1377 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1379 * SCTP doesn't need the DTLS timer Returns always 1.
1382 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1384 * We allow transport protocol fragmentation so this is irrelevant
1388 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1390 data->in_handshake = 1;
1392 data->in_handshake = 0;
1395 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1396 &data->in_handshake, sizeof(int));
1398 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1400 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1403 /* Get active key */
1404 sockopt_len = sizeof(struct sctp_authkeyid);
1406 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1412 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1413 authkey = OPENSSL_malloc(sockopt_len);
1414 if (authkey == NULL) {
1418 memset(authkey, 0, sockopt_len);
1419 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1420 # ifndef __FreeBSD__
1422 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1423 * and higher work without it.
1425 authkey->sca_keylength = 64;
1427 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1430 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1432 OPENSSL_free(authkey);
1437 /* Reset active key */
1438 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1439 &authkeyid, sizeof(struct sctp_authkeyid));
1444 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1445 /* Returns 0 on success, -1 otherwise. */
1447 /* Get active key */
1448 sockopt_len = sizeof(struct sctp_authkeyid);
1450 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1455 /* Set active key */
1456 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1457 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1458 &authkeyid, sizeof(struct sctp_authkeyid));
1463 * CCS has been sent, so remember that and fall through to check if
1464 * we need to deactivate an old key
1469 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1470 /* Returns 0 on success, -1 otherwise. */
1473 * Has this command really been called or is this just a
1476 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1480 * CSS has been both, received and sent, so deactivate an old key
1482 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1483 /* Get active key */
1484 sockopt_len = sizeof(struct sctp_authkeyid);
1486 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1487 &authkeyid, &sockopt_len);
1492 * Deactivate key or delete second last key if
1493 * SCTP_AUTHENTICATION_EVENT is not available.
1495 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1496 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1497 sockopt_len = sizeof(struct sctp_authkeyid);
1498 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1499 &authkeyid, sockopt_len);
1503 # ifndef SCTP_AUTHENTICATION_EVENT
1504 if (authkeyid.scact_keynumber > 0) {
1505 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1506 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1507 &authkeyid, sizeof(struct sctp_authkeyid));
1517 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1518 /* Returns the size of the copied struct. */
1519 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1520 num = sizeof(struct bio_dgram_sctp_sndinfo);
1522 memcpy(ptr, &(data->sndinfo), num);
1525 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1526 /* Returns the size of the copied struct. */
1527 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1528 num = sizeof(struct bio_dgram_sctp_sndinfo);
1530 memcpy(&(data->sndinfo), ptr, num);
1532 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1533 /* Returns the size of the copied struct. */
1534 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1535 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1537 memcpy(ptr, &data->rcvinfo, num);
1541 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1542 /* Returns the size of the copied struct. */
1543 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1544 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1546 memcpy(&(data->rcvinfo), ptr, num);
1548 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1549 /* Returns the size of the copied struct. */
1550 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1551 num = sizeof(struct bio_dgram_sctp_prinfo);
1553 memcpy(ptr, &(data->prinfo), num);
1556 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1557 /* Returns the size of the copied struct. */
1558 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1559 num = sizeof(struct bio_dgram_sctp_prinfo);
1561 memcpy(&(data->prinfo), ptr, num);
1563 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1564 /* Returns always 1. */
1566 data->save_shutdown = 1;
1568 data->save_shutdown = 0;
1570 case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
1571 return dgram_sctp_wait_for_dry(b);
1572 case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
1573 return dgram_sctp_msg_waiting(b);
1577 * Pass to default ctrl function to process SCTP unspecific commands
1579 ret = dgram_ctrl(b, cmd, num, ptr);
1585 int BIO_dgram_sctp_notification_cb(BIO *b,
1586 BIO_dgram_sctp_notification_handler_fn handle_notifications,
1589 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1591 if (handle_notifications != NULL) {
1592 data->handle_notifications = handle_notifications;
1593 data->notification_context = context;
1601 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1602 * @b: The BIO to check for the dry event
1604 * Wait until the peer confirms all packets have been received, and so that
1605 * our kernel doesn't have anything to send anymore. This is only received by
1606 * the peer's kernel, not the application.
1610 * 0 when not dry yet
1613 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1615 return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
1618 static int dgram_sctp_wait_for_dry(BIO *b)
1623 union sctp_notification snp;
1627 struct sctp_event event;
1629 struct sctp_event_subscribe event;
1630 socklen_t eventsize;
1632 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1634 /* set sender dry event */
1636 memset(&event, 0, sizeof(event));
1637 event.se_assoc_id = 0;
1638 event.se_type = SCTP_SENDER_DRY_EVENT;
1641 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1642 sizeof(struct sctp_event));
1644 eventsize = sizeof(struct sctp_event_subscribe);
1645 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1649 event.sctp_sender_dry_event = 1;
1652 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1653 sizeof(struct sctp_event_subscribe));
1658 /* peek for notification */
1659 memset(&snp, 0, sizeof(snp));
1660 iov.iov_base = (char *)&snp;
1661 iov.iov_len = sizeof(union sctp_notification);
1662 msg.msg_name = NULL;
1663 msg.msg_namelen = 0;
1666 msg.msg_control = NULL;
1667 msg.msg_controllen = 0;
1670 n = recvmsg(b->num, &msg, MSG_PEEK);
1672 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1673 && (get_last_socket_error() != EWOULDBLOCK))
1679 /* if we find a notification, process it and try again if necessary */
1680 while (msg.msg_flags & MSG_NOTIFICATION) {
1681 memset(&snp, 0, sizeof(snp));
1682 iov.iov_base = (char *)&snp;
1683 iov.iov_len = sizeof(union sctp_notification);
1684 msg.msg_name = NULL;
1685 msg.msg_namelen = 0;
1688 msg.msg_control = NULL;
1689 msg.msg_controllen = 0;
1692 n = recvmsg(b->num, &msg, 0);
1694 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1695 && (get_last_socket_error() != EWOULDBLOCK))
1701 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1704 /* disable sender dry event */
1706 memset(&event, 0, sizeof(event));
1707 event.se_assoc_id = 0;
1708 event.se_type = SCTP_SENDER_DRY_EVENT;
1711 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1712 sizeof(struct sctp_event));
1714 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1716 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1721 event.sctp_sender_dry_event = 0;
1724 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1725 sizeof(struct sctp_event_subscribe));
1730 # ifdef SCTP_AUTHENTICATION_EVENT
1731 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1732 dgram_sctp_handle_auth_free_key_event(b, &snp);
1735 if (data->handle_notifications != NULL)
1736 data->handle_notifications(b, data->notification_context,
1739 /* found notification, peek again */
1740 memset(&snp, 0, sizeof(snp));
1741 iov.iov_base = (char *)&snp;
1742 iov.iov_len = sizeof(union sctp_notification);
1743 msg.msg_name = NULL;
1744 msg.msg_namelen = 0;
1747 msg.msg_control = NULL;
1748 msg.msg_controllen = 0;
1751 /* if we have seen the dry already, don't wait */
1753 sockflags = fcntl(b->num, F_GETFL, 0);
1754 fcntl(b->num, F_SETFL, O_NONBLOCK);
1757 n = recvmsg(b->num, &msg, MSG_PEEK);
1760 fcntl(b->num, F_SETFL, sockflags);
1764 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1765 && (get_last_socket_error() != EWOULDBLOCK))
1772 /* read anything else */
1776 int BIO_dgram_sctp_msg_waiting(BIO *b)
1778 return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
1781 static int dgram_sctp_msg_waiting(BIO *b)
1784 union sctp_notification snp;
1787 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1789 /* Check if there are any messages waiting to be read */
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 sockflags = fcntl(b->num, F_GETFL, 0);
1803 fcntl(b->num, F_SETFL, O_NONBLOCK);
1804 n = recvmsg(b->num, &msg, MSG_PEEK);
1805 fcntl(b->num, F_SETFL, sockflags);
1807 /* if notification, process and try again */
1808 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1809 # ifdef SCTP_AUTHENTICATION_EVENT
1810 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1811 dgram_sctp_handle_auth_free_key_event(b, &snp);
1814 memset(&snp, 0, sizeof(snp));
1815 iov.iov_base = (char *)&snp;
1816 iov.iov_len = sizeof(union sctp_notification);
1817 msg.msg_name = NULL;
1818 msg.msg_namelen = 0;
1821 msg.msg_control = NULL;
1822 msg.msg_controllen = 0;
1824 n = recvmsg(b->num, &msg, 0);
1826 if (data->handle_notifications != NULL)
1827 data->handle_notifications(b, data->notification_context,
1831 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1833 /* Return 1 if there is a message to be read, return 0 otherwise. */
1840 static int dgram_sctp_puts(BIO *bp, const char *str)
1845 ret = dgram_sctp_write(bp, str, n);
1850 static int BIO_dgram_should_retry(int i)
1854 if ((i == 0) || (i == -1)) {
1855 err = get_last_socket_error();
1857 # if defined(OPENSSL_SYS_WINDOWS)
1859 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1860 * this point, the error code was overwritten by another system call
1861 * before this error handling is called.
1865 return BIO_dgram_non_fatal_error(err);
1870 int BIO_dgram_non_fatal_error(int err)
1873 # if defined(OPENSSL_SYS_WINDOWS)
1874 # if defined(WSAEWOULDBLOCK)
1875 case WSAEWOULDBLOCK:
1880 # ifdef WSAEWOULDBLOCK
1881 # if WSAEWOULDBLOCK != EWOULDBLOCK
1894 # if EWOULDBLOCK != EAGAIN
1918 static void get_current_time(struct timeval *t)
1920 # if defined(_WIN32)
1923 unsigned __int64 ul;
1928 SystemTimeToFileTime(&st, &now.ft);
1930 now.ul -= 116444736000000000ULL;
1932 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1934 t->tv_sec = (long)(now.ul / 10000000);
1935 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1937 gettimeofday(t, NULL);