source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/bio/bss_dgram.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 56.9 KB
Line 
1/*
2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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
8 */
9
10#include <stdio.h>
11#include <errno.h>
12
13#include "bio_lcl.h"
14#ifndef OPENSSL_NO_DGRAM
15
16# if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
17# include <sys/time.h>
18# endif
19# if defined(OPENSSL_SYS_VMS)
20# include <sys/timeb.h>
21# endif
22
23# ifndef OPENSSL_NO_SCTP
24# include <netinet/sctp.h>
25# include <fcntl.h>
26# define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
27# define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
28# endif
29
30# if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
31# define IP_MTU 14 /* linux is lame */
32# endif
33
34# if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
35# define IPPROTO_IPV6 41 /* windows is lame */
36# endif
37
38# if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
39/* Standard definition causes type-punning problems. */
40# undef IN6_IS_ADDR_V4MAPPED
41# define s6_addr32 __u6_addr.__u6_addr32
42# define IN6_IS_ADDR_V4MAPPED(a) \
43 (((a)->s6_addr32[0] == 0) && \
44 ((a)->s6_addr32[1] == 0) && \
45 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
46# endif
47
48static int dgram_write(BIO *h, const char *buf, int num);
49static int dgram_read(BIO *h, char *buf, int size);
50static int dgram_puts(BIO *h, const char *str);
51static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
52static int dgram_new(BIO *h);
53static int dgram_free(BIO *data);
54static int dgram_clear(BIO *bio);
55
56# ifndef OPENSSL_NO_SCTP
57static int dgram_sctp_write(BIO *h, const char *buf, int num);
58static int dgram_sctp_read(BIO *h, char *buf, int size);
59static int dgram_sctp_puts(BIO *h, const char *str);
60static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
61static int dgram_sctp_new(BIO *h);
62static int dgram_sctp_free(BIO *data);
63# ifdef SCTP_AUTHENTICATION_EVENT
64static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
65 *snp);
66# endif
67# endif
68
69static int BIO_dgram_should_retry(int s);
70
71static void get_current_time(struct timeval *t);
72
73static const BIO_METHOD methods_dgramp = {
74 BIO_TYPE_DGRAM,
75 "datagram socket",
76 dgram_write,
77 dgram_read,
78 dgram_puts,
79 NULL, /* dgram_gets, */
80 dgram_ctrl,
81 dgram_new,
82 dgram_free,
83 NULL,
84};
85
86# ifndef OPENSSL_NO_SCTP
87static const BIO_METHOD methods_dgramp_sctp = {
88 BIO_TYPE_DGRAM_SCTP,
89 "datagram sctp socket",
90 dgram_sctp_write,
91 dgram_sctp_read,
92 dgram_sctp_puts,
93 NULL, /* dgram_gets, */
94 dgram_sctp_ctrl,
95 dgram_sctp_new,
96 dgram_sctp_free,
97 NULL,
98};
99# endif
100
101typedef struct bio_dgram_data_st {
102 BIO_ADDR peer;
103 unsigned int connected;
104 unsigned int _errno;
105 unsigned int mtu;
106 struct timeval next_timeout;
107 struct timeval socket_timeout;
108 unsigned int peekmode;
109} bio_dgram_data;
110
111# ifndef OPENSSL_NO_SCTP
112typedef struct bio_dgram_sctp_save_message_st {
113 BIO *bio;
114 char *data;
115 int length;
116} bio_dgram_sctp_save_message;
117
118typedef struct bio_dgram_sctp_data_st {
119 BIO_ADDR peer;
120 unsigned int connected;
121 unsigned int _errno;
122 unsigned int mtu;
123 struct bio_dgram_sctp_sndinfo sndinfo;
124 struct bio_dgram_sctp_rcvinfo rcvinfo;
125 struct bio_dgram_sctp_prinfo prinfo;
126 void (*handle_notifications) (BIO *bio, void *context, void *buf);
127 void *notification_context;
128 int in_handshake;
129 int ccs_rcvd;
130 int ccs_sent;
131 int save_shutdown;
132 int peer_auth_tested;
133 bio_dgram_sctp_save_message saved_message;
134} bio_dgram_sctp_data;
135# endif
136
137const BIO_METHOD *BIO_s_datagram(void)
138{
139 return (&methods_dgramp);
140}
141
142BIO *BIO_new_dgram(int fd, int close_flag)
143{
144 BIO *ret;
145
146 ret = BIO_new(BIO_s_datagram());
147 if (ret == NULL)
148 return (NULL);
149 BIO_set_fd(ret, fd, close_flag);
150 return (ret);
151}
152
153static int dgram_new(BIO *bi)
154{
155 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
156
157 if (data == NULL)
158 return 0;
159 bi->ptr = data;
160 return (1);
161}
162
163static int dgram_free(BIO *a)
164{
165 bio_dgram_data *data;
166
167 if (a == NULL)
168 return (0);
169 if (!dgram_clear(a))
170 return 0;
171
172 data = (bio_dgram_data *)a->ptr;
173 OPENSSL_free(data);
174
175 return (1);
176}
177
178static int dgram_clear(BIO *a)
179{
180 if (a == NULL)
181 return (0);
182 if (a->shutdown) {
183 if (a->init) {
184 BIO_closesocket(a->num);
185 }
186 a->init = 0;
187 a->flags = 0;
188 }
189 return (1);
190}
191
192static void dgram_adjust_rcv_timeout(BIO *b)
193{
194# if defined(SO_RCVTIMEO)
195 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
196 union {
197 size_t s;
198 int i;
199 } sz = {
200 0
201 };
202
203 /* Is a timer active? */
204 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
205 struct timeval timenow, timeleft;
206
207 /* Read current socket timeout */
208# ifdef OPENSSL_SYS_WINDOWS
209 int timeout;
210
211 sz.i = sizeof(timeout);
212 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
213 (void *)&timeout, &sz.i) < 0) {
214 perror("getsockopt");
215 } else {
216 data->socket_timeout.tv_sec = timeout / 1000;
217 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
218 }
219# else
220 sz.i = sizeof(data->socket_timeout);
221 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
222 &(data->socket_timeout), (void *)&sz) < 0) {
223 perror("getsockopt");
224 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
225 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
226# endif
227
228 /* Get current time */
229 get_current_time(&timenow);
230
231 /* Calculate time left until timer expires */
232 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
233 if (timeleft.tv_usec < timenow.tv_usec) {
234 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
235 timeleft.tv_sec--;
236 } else {
237 timeleft.tv_usec -= timenow.tv_usec;
238 }
239 if (timeleft.tv_sec < timenow.tv_sec) {
240 timeleft.tv_sec = 0;
241 timeleft.tv_usec = 1;
242 } else {
243 timeleft.tv_sec -= timenow.tv_sec;
244 }
245
246 /*
247 * Adjust socket timeout if next handshake message timer will expire
248 * earlier.
249 */
250 if ((data->socket_timeout.tv_sec == 0
251 && data->socket_timeout.tv_usec == 0)
252 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
253 || (data->socket_timeout.tv_sec == timeleft.tv_sec
254 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
255# ifdef OPENSSL_SYS_WINDOWS
256 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
257 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
258 (void *)&timeout, sizeof(timeout)) < 0) {
259 perror("setsockopt");
260 }
261# else
262 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
263 sizeof(struct timeval)) < 0) {
264 perror("setsockopt");
265 }
266# endif
267 }
268 }
269# endif
270}
271
272static void dgram_reset_rcv_timeout(BIO *b)
273{
274# if defined(SO_RCVTIMEO)
275 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
276
277 /* Is a timer active? */
278 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
279# ifdef OPENSSL_SYS_WINDOWS
280 int timeout = data->socket_timeout.tv_sec * 1000 +
281 data->socket_timeout.tv_usec / 1000;
282 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
283 (void *)&timeout, sizeof(timeout)) < 0) {
284 perror("setsockopt");
285 }
286# else
287 if (setsockopt
288 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
289 sizeof(struct timeval)) < 0) {
290 perror("setsockopt");
291 }
292# endif
293 }
294# endif
295}
296
297static int dgram_read(BIO *b, char *out, int outl)
298{
299 int ret = 0;
300 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
301 int flags = 0;
302
303 BIO_ADDR peer;
304 socklen_t len = sizeof(peer);
305
306 if (out != NULL) {
307 clear_socket_error();
308 memset(&peer, 0, sizeof(peer));
309 dgram_adjust_rcv_timeout(b);
310 if (data->peekmode)
311 flags = MSG_PEEK;
312 ret = recvfrom(b->num, out, outl, flags,
313 BIO_ADDR_sockaddr_noconst(&peer), &len);
314
315 if (!data->connected && ret >= 0)
316 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
317
318 BIO_clear_retry_flags(b);
319 if (ret < 0) {
320 if (BIO_dgram_should_retry(ret)) {
321 BIO_set_retry_read(b);
322 data->_errno = get_last_socket_error();
323 }
324 }
325
326 dgram_reset_rcv_timeout(b);
327 }
328 return (ret);
329}
330
331static int dgram_write(BIO *b, const char *in, int inl)
332{
333 int ret;
334 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
335 clear_socket_error();
336
337 if (data->connected)
338 ret = writesocket(b->num, in, inl);
339 else {
340 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
341
342# if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
343 ret = sendto(b->num, (char *)in, inl, 0,
344 BIO_ADDR_sockaddr(&data->peer), peerlen);
345# else
346 ret = sendto(b->num, in, inl, 0,
347 BIO_ADDR_sockaddr(&data->peer), peerlen);
348# endif
349 }
350
351 BIO_clear_retry_flags(b);
352 if (ret <= 0) {
353 if (BIO_dgram_should_retry(ret)) {
354 BIO_set_retry_write(b);
355 data->_errno = get_last_socket_error();
356 }
357 }
358 return (ret);
359}
360
361static long dgram_get_mtu_overhead(bio_dgram_data *data)
362{
363 long ret;
364
365 switch (BIO_ADDR_family(&data->peer)) {
366 case AF_INET:
367 /*
368 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
369 */
370 ret = 28;
371 break;
372# ifdef AF_INET6
373 case AF_INET6:
374 {
375# ifdef IN6_IS_ADDR_V4MAPPED
376 struct in6_addr tmp_addr;
377 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
378 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
379 /*
380 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
381 */
382 ret = 28;
383 else
384# endif
385 /*
386 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
387 */
388 ret = 48;
389 }
390 break;
391# endif
392 default:
393 /* We don't know. Go with the historical default */
394 ret = 28;
395 break;
396 }
397 return ret;
398}
399
400static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
401{
402 long ret = 1;
403 int *ip;
404 bio_dgram_data *data = NULL;
405 int sockopt_val = 0;
406 int d_errno;
407# if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
408 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
409 * modern enough to define socklen_t */
410 socklen_t addr_len;
411 BIO_ADDR addr;
412# endif
413
414 data = (bio_dgram_data *)b->ptr;
415
416 switch (cmd) {
417 case BIO_CTRL_RESET:
418 num = 0;
419 ret = 0;
420 break;
421 case BIO_CTRL_INFO:
422 ret = 0;
423 break;
424 case BIO_C_SET_FD:
425 dgram_clear(b);
426 b->num = *((int *)ptr);
427 b->shutdown = (int)num;
428 b->init = 1;
429 break;
430 case BIO_C_GET_FD:
431 if (b->init) {
432 ip = (int *)ptr;
433 if (ip != NULL)
434 *ip = b->num;
435 ret = b->num;
436 } else
437 ret = -1;
438 break;
439 case BIO_CTRL_GET_CLOSE:
440 ret = b->shutdown;
441 break;
442 case BIO_CTRL_SET_CLOSE:
443 b->shutdown = (int)num;
444 break;
445 case BIO_CTRL_PENDING:
446 case BIO_CTRL_WPENDING:
447 ret = 0;
448 break;
449 case BIO_CTRL_DUP:
450 case BIO_CTRL_FLUSH:
451 ret = 1;
452 break;
453 case BIO_CTRL_DGRAM_CONNECT:
454 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
455 break;
456 /* (Linux)kernel sets DF bit on outgoing IP packets */
457 case BIO_CTRL_DGRAM_MTU_DISCOVER:
458# if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
459 addr_len = (socklen_t) sizeof(addr);
460 memset(&addr, 0, sizeof(addr));
461 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
462 ret = 0;
463 break;
464 }
465 switch (addr.sa.sa_family) {
466 case AF_INET:
467 sockopt_val = IP_PMTUDISC_DO;
468 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
469 &sockopt_val, sizeof(sockopt_val))) < 0)
470 perror("setsockopt");
471 break;
472# if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
473 case AF_INET6:
474 sockopt_val = IPV6_PMTUDISC_DO;
475 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
476 &sockopt_val, sizeof(sockopt_val))) < 0)
477 perror("setsockopt");
478 break;
479# endif
480 default:
481 ret = -1;
482 break;
483 }
484# else
485 ret = -1;
486# endif
487 break;
488 case BIO_CTRL_DGRAM_QUERY_MTU:
489# if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
490 addr_len = (socklen_t) sizeof(addr);
491 memset(&addr, 0, sizeof(addr));
492 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
493 ret = 0;
494 break;
495 }
496 sockopt_len = sizeof(sockopt_val);
497 switch (addr.sa.sa_family) {
498 case AF_INET:
499 if ((ret =
500 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
501 &sockopt_len)) < 0 || sockopt_val < 0) {
502 ret = 0;
503 } else {
504 /*
505 * we assume that the transport protocol is UDP and no IP
506 * options are used.
507 */
508 data->mtu = sockopt_val - 8 - 20;
509 ret = data->mtu;
510 }
511 break;
512# if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
513 case AF_INET6:
514 if ((ret =
515 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
516 (void *)&sockopt_val, &sockopt_len)) < 0
517 || sockopt_val < 0) {
518 ret = 0;
519 } else {
520 /*
521 * we assume that the transport protocol is UDP and no IPV6
522 * options are used.
523 */
524 data->mtu = sockopt_val - 8 - 40;
525 ret = data->mtu;
526 }
527 break;
528# endif
529 default:
530 ret = 0;
531 break;
532 }
533# else
534 ret = 0;
535# endif
536 break;
537 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
538 ret = -dgram_get_mtu_overhead(data);
539 switch (BIO_ADDR_family(&data->peer)) {
540 case AF_INET:
541 ret += 576;
542 break;
543# if OPENSSL_USE_IPV6
544 case AF_INET6:
545 {
546# ifdef IN6_IS_ADDR_V4MAPPED
547 struct in6_addr tmp_addr;
548 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
549 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
550 ret += 576;
551 else
552# endif
553 ret += 1280;
554 }
555 break;
556# endif
557 default:
558 ret += 576;
559 break;
560 }
561 break;
562 case BIO_CTRL_DGRAM_GET_MTU:
563 return data->mtu;
564 case BIO_CTRL_DGRAM_SET_MTU:
565 data->mtu = num;
566 ret = num;
567 break;
568 case BIO_CTRL_DGRAM_SET_CONNECTED:
569 if (ptr != NULL) {
570 data->connected = 1;
571 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
572 } else {
573 data->connected = 0;
574 memset(&data->peer, 0, sizeof(data->peer));
575 }
576 break;
577 case BIO_CTRL_DGRAM_GET_PEER:
578 ret = BIO_ADDR_sockaddr_size(&data->peer);
579 /* FIXME: if num < ret, we will only return part of an address.
580 That should bee an error, no? */
581 if (num == 0 || num > ret)
582 num = ret;
583 memcpy(ptr, &data->peer, (ret = num));
584 break;
585 case BIO_CTRL_DGRAM_SET_PEER:
586 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
587 break;
588 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
589 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
590 break;
591# if defined(SO_RCVTIMEO)
592 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
593# ifdef OPENSSL_SYS_WINDOWS
594 {
595 struct timeval *tv = (struct timeval *)ptr;
596 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
597 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
598 (void *)&timeout, sizeof(timeout)) < 0) {
599 perror("setsockopt");
600 ret = -1;
601 }
602 }
603# else
604 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
605 sizeof(struct timeval)) < 0) {
606 perror("setsockopt");
607 ret = -1;
608 }
609# endif
610 break;
611 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
612 {
613 union {
614 size_t s;
615 int i;
616 } sz = {
617 0
618 };
619# ifdef OPENSSL_SYS_WINDOWS
620 int timeout;
621 struct timeval *tv = (struct timeval *)ptr;
622
623 sz.i = sizeof(timeout);
624 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
625 (void *)&timeout, &sz.i) < 0) {
626 perror("getsockopt");
627 ret = -1;
628 } else {
629 tv->tv_sec = timeout / 1000;
630 tv->tv_usec = (timeout % 1000) * 1000;
631 ret = sizeof(*tv);
632 }
633# else
634 sz.i = sizeof(struct timeval);
635 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
636 ptr, (void *)&sz) < 0) {
637 perror("getsockopt");
638 ret = -1;
639 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
640 OPENSSL_assert(sz.s <= sizeof(struct timeval));
641 ret = (int)sz.s;
642 } else
643 ret = sz.i;
644# endif
645 }
646 break;
647# endif
648# if defined(SO_SNDTIMEO)
649 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
650# ifdef OPENSSL_SYS_WINDOWS
651 {
652 struct timeval *tv = (struct timeval *)ptr;
653 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
654 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
655 (void *)&timeout, sizeof(timeout)) < 0) {
656 perror("setsockopt");
657 ret = -1;
658 }
659 }
660# else
661 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
662 sizeof(struct timeval)) < 0) {
663 perror("setsockopt");
664 ret = -1;
665 }
666# endif
667 break;
668 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
669 {
670 union {
671 size_t s;
672 int i;
673 } sz = {
674 0
675 };
676# ifdef OPENSSL_SYS_WINDOWS
677 int timeout;
678 struct timeval *tv = (struct timeval *)ptr;
679
680 sz.i = sizeof(timeout);
681 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
682 (void *)&timeout, &sz.i) < 0) {
683 perror("getsockopt");
684 ret = -1;
685 } else {
686 tv->tv_sec = timeout / 1000;
687 tv->tv_usec = (timeout % 1000) * 1000;
688 ret = sizeof(*tv);
689 }
690# else
691 sz.i = sizeof(struct timeval);
692 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
693 ptr, (void *)&sz) < 0) {
694 perror("getsockopt");
695 ret = -1;
696 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
697 OPENSSL_assert(sz.s <= sizeof(struct timeval));
698 ret = (int)sz.s;
699 } else
700 ret = sz.i;
701# endif
702 }
703 break;
704# endif
705 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
706 /* fall-through */
707 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
708# ifdef OPENSSL_SYS_WINDOWS
709 d_errno = (data->_errno == WSAETIMEDOUT);
710# else
711 d_errno = (data->_errno == EAGAIN);
712# endif
713 if (d_errno) {
714 ret = 1;
715 data->_errno = 0;
716 } else
717 ret = 0;
718 break;
719# ifdef EMSGSIZE
720 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
721 if (data->_errno == EMSGSIZE) {
722 ret = 1;
723 data->_errno = 0;
724 } else
725 ret = 0;
726 break;
727# endif
728 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
729 sockopt_val = num ? 1 : 0;
730
731 switch (data->peer.sa.sa_family) {
732 case AF_INET:
733# if defined(IP_DONTFRAG)
734 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
735 &sockopt_val, sizeof(sockopt_val))) < 0) {
736 perror("setsockopt");
737 ret = -1;
738 }
739# elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
740 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
741 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
742 &sockopt_val, sizeof(sockopt_val))) < 0) {
743 perror("setsockopt");
744 ret = -1;
745 }
746# elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
747 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
748 (const char *)&sockopt_val,
749 sizeof(sockopt_val))) < 0) {
750 perror("setsockopt");
751 ret = -1;
752 }
753# else
754 ret = -1;
755# endif
756 break;
757# if OPENSSL_USE_IPV6
758 case AF_INET6:
759# if defined(IPV6_DONTFRAG)
760 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
761 (const void *)&sockopt_val,
762 sizeof(sockopt_val))) < 0) {
763 perror("setsockopt");
764 ret = -1;
765 }
766# elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
767 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
768 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
769 &sockopt_val, sizeof(sockopt_val))) < 0) {
770 perror("setsockopt");
771 ret = -1;
772 }
773# else
774 ret = -1;
775# endif
776 break;
777# endif
778 default:
779 ret = -1;
780 break;
781 }
782 break;
783 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
784 ret = dgram_get_mtu_overhead(data);
785 break;
786 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
787 data->peekmode = (unsigned int)num;
788 break;
789 default:
790 ret = 0;
791 break;
792 }
793 return (ret);
794}
795
796static int dgram_puts(BIO *bp, const char *str)
797{
798 int n, ret;
799
800 n = strlen(str);
801 ret = dgram_write(bp, str, n);
802 return (ret);
803}
804
805# ifndef OPENSSL_NO_SCTP
806const BIO_METHOD *BIO_s_datagram_sctp(void)
807{
808 return (&methods_dgramp_sctp);
809}
810
811BIO *BIO_new_dgram_sctp(int fd, int close_flag)
812{
813 BIO *bio;
814 int ret, optval = 20000;
815 int auth_data = 0, auth_forward = 0;
816 unsigned char *p;
817 struct sctp_authchunk auth;
818 struct sctp_authchunks *authchunks;
819 socklen_t sockopt_len;
820# ifdef SCTP_AUTHENTICATION_EVENT
821# ifdef SCTP_EVENT
822 struct sctp_event event;
823# else
824 struct sctp_event_subscribe event;
825# endif
826# endif
827
828 bio = BIO_new(BIO_s_datagram_sctp());
829 if (bio == NULL)
830 return (NULL);
831 BIO_set_fd(bio, fd, close_flag);
832
833 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
834 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
835 ret =
836 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
837 sizeof(struct sctp_authchunk));
838 if (ret < 0) {
839 BIO_vfree(bio);
840 return (NULL);
841 }
842 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
843 ret =
844 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
845 sizeof(struct sctp_authchunk));
846 if (ret < 0) {
847 BIO_vfree(bio);
848 return (NULL);
849 }
850
851 /*
852 * Test if activation was successful. When using accept(), SCTP-AUTH has
853 * to be activated for the listening socket already, otherwise the
854 * connected socket won't use it.
855 */
856 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
857 authchunks = OPENSSL_zalloc(sockopt_len);
858 if (authchunks == NULL) {
859 BIO_vfree(bio);
860 return (NULL);
861 }
862 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
863 &sockopt_len);
864 if (ret < 0) {
865 OPENSSL_free(authchunks);
866 BIO_vfree(bio);
867 return (NULL);
868 }
869
870 for (p = (unsigned char *)authchunks->gauth_chunks;
871 p < (unsigned char *)authchunks + sockopt_len;
872 p += sizeof(uint8_t)) {
873 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
874 auth_data = 1;
875 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
876 auth_forward = 1;
877 }
878
879 OPENSSL_free(authchunks);
880
881 OPENSSL_assert(auth_data);
882 OPENSSL_assert(auth_forward);
883
884# ifdef SCTP_AUTHENTICATION_EVENT
885# ifdef SCTP_EVENT
886 memset(&event, 0, sizeof(event));
887 event.se_assoc_id = 0;
888 event.se_type = SCTP_AUTHENTICATION_EVENT;
889 event.se_on = 1;
890 ret =
891 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
892 sizeof(struct sctp_event));
893 if (ret < 0) {
894 BIO_vfree(bio);
895 return (NULL);
896 }
897# else
898 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
899 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
900 if (ret < 0) {
901 BIO_vfree(bio);
902 return (NULL);
903 }
904
905 event.sctp_authentication_event = 1;
906
907 ret =
908 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
909 sizeof(struct sctp_event_subscribe));
910 if (ret < 0) {
911 BIO_vfree(bio);
912 return (NULL);
913 }
914# endif
915# endif
916
917 /*
918 * Disable partial delivery by setting the min size larger than the max
919 * record size of 2^14 + 2048 + 13
920 */
921 ret =
922 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
923 sizeof(optval));
924 if (ret < 0) {
925 BIO_vfree(bio);
926 return (NULL);
927 }
928
929 return (bio);
930}
931
932int BIO_dgram_is_sctp(BIO *bio)
933{
934 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
935}
936
937static int dgram_sctp_new(BIO *bi)
938{
939 bio_dgram_sctp_data *data = NULL;
940
941 bi->init = 0;
942 bi->num = 0;
943 data = OPENSSL_zalloc(sizeof(*data));
944 if (data == NULL)
945 return 0;
946# ifdef SCTP_PR_SCTP_NONE
947 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
948# endif
949 bi->ptr = data;
950
951 bi->flags = 0;
952 return (1);
953}
954
955static int dgram_sctp_free(BIO *a)
956{
957 bio_dgram_sctp_data *data;
958
959 if (a == NULL)
960 return (0);
961 if (!dgram_clear(a))
962 return 0;
963
964 data = (bio_dgram_sctp_data *) a->ptr;
965 if (data != NULL) {
966 OPENSSL_free(data->saved_message.data);
967 OPENSSL_free(data);
968 }
969
970 return (1);
971}
972
973# ifdef SCTP_AUTHENTICATION_EVENT
974void dgram_sctp_handle_auth_free_key_event(BIO *b,
975 union sctp_notification *snp)
976{
977 int ret;
978 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
979
980 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
981 struct sctp_authkeyid authkeyid;
982
983 /* delete key */
984 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
985 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
986 &authkeyid, sizeof(struct sctp_authkeyid));
987 }
988}
989# endif
990
991static int dgram_sctp_read(BIO *b, char *out, int outl)
992{
993 int ret = 0, n = 0, i, optval;
994 socklen_t optlen;
995 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
996 union sctp_notification *snp;
997 struct msghdr msg;
998 struct iovec iov;
999 struct cmsghdr *cmsg;
1000 char cmsgbuf[512];
1001
1002 if (out != NULL) {
1003 clear_socket_error();
1004
1005 do {
1006 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1007 iov.iov_base = out;
1008 iov.iov_len = outl;
1009 msg.msg_name = NULL;
1010 msg.msg_namelen = 0;
1011 msg.msg_iov = &iov;
1012 msg.msg_iovlen = 1;
1013 msg.msg_control = cmsgbuf;
1014 msg.msg_controllen = 512;
1015 msg.msg_flags = 0;
1016 n = recvmsg(b->num, &msg, 0);
1017
1018 if (n <= 0) {
1019 if (n < 0)
1020 ret = n;
1021 break;
1022 }
1023
1024 if (msg.msg_controllen > 0) {
1025 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1026 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1027 if (cmsg->cmsg_level != IPPROTO_SCTP)
1028 continue;
1029# ifdef SCTP_RCVINFO
1030 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1031 struct sctp_rcvinfo *rcvinfo;
1032
1033 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1034 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1035 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1036 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1037 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1038 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1039 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1040 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1041 }
1042# endif
1043# ifdef SCTP_SNDRCV
1044 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1045 struct sctp_sndrcvinfo *sndrcvinfo;
1046
1047 sndrcvinfo =
1048 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1049 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1050 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1051 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1052 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1053 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1054 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1055 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1056 }
1057# endif
1058 }
1059 }
1060
1061 if (msg.msg_flags & MSG_NOTIFICATION) {
1062 snp = (union sctp_notification *)out;
1063 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1064# ifdef SCTP_EVENT
1065 struct sctp_event event;
1066# else
1067 struct sctp_event_subscribe event;
1068 socklen_t eventsize;
1069# endif
1070 /*
1071 * If a message has been delayed until the socket is dry,
1072 * it can be sent now.
1073 */
1074 if (data->saved_message.length > 0) {
1075 i = dgram_sctp_write(data->saved_message.bio,
1076 data->saved_message.data,
1077 data->saved_message.length);
1078 if (i < 0) {
1079 ret = i;
1080 break;
1081 }
1082 OPENSSL_free(data->saved_message.data);
1083 data->saved_message.data = NULL;
1084 data->saved_message.length = 0;
1085 }
1086
1087 /* disable sender dry event */
1088# ifdef SCTP_EVENT
1089 memset(&event, 0, sizeof(event));
1090 event.se_assoc_id = 0;
1091 event.se_type = SCTP_SENDER_DRY_EVENT;
1092 event.se_on = 0;
1093 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1094 sizeof(struct sctp_event));
1095 if (i < 0) {
1096 ret = i;
1097 break;
1098 }
1099# else
1100 eventsize = sizeof(struct sctp_event_subscribe);
1101 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1102 &eventsize);
1103 if (i < 0) {
1104 ret = i;
1105 break;
1106 }
1107
1108 event.sctp_sender_dry_event = 0;
1109
1110 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1111 sizeof(struct sctp_event_subscribe));
1112 if (i < 0) {
1113 ret = i;
1114 break;
1115 }
1116# endif
1117 }
1118# ifdef SCTP_AUTHENTICATION_EVENT
1119 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1120 dgram_sctp_handle_auth_free_key_event(b, snp);
1121# endif
1122
1123 if (data->handle_notifications != NULL)
1124 data->handle_notifications(b, data->notification_context,
1125 (void *)out);
1126
1127 memset(out, 0, outl);
1128 } else
1129 ret += n;
1130 }
1131 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1132 && (ret < outl));
1133
1134 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1135 /* Partial message read, this should never happen! */
1136
1137 /*
1138 * The buffer was too small, this means the peer sent a message
1139 * that was larger than allowed.
1140 */
1141 if (ret == outl)
1142 return -1;
1143
1144 /*
1145 * Test if socket buffer can handle max record size (2^14 + 2048
1146 * + 13)
1147 */
1148 optlen = (socklen_t) sizeof(int);
1149 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1150 if (ret >= 0)
1151 OPENSSL_assert(optval >= 18445);
1152
1153 /*
1154 * Test if SCTP doesn't partially deliver below max record size
1155 * (2^14 + 2048 + 13)
1156 */
1157 optlen = (socklen_t) sizeof(int);
1158 ret =
1159 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1160 &optval, &optlen);
1161 if (ret >= 0)
1162 OPENSSL_assert(optval >= 18445);
1163
1164 /*
1165 * Partially delivered notification??? Probably a bug....
1166 */
1167 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1168
1169 /*
1170 * Everything seems ok till now, so it's most likely a message
1171 * dropped by PR-SCTP.
1172 */
1173 memset(out, 0, outl);
1174 BIO_set_retry_read(b);
1175 return -1;
1176 }
1177
1178 BIO_clear_retry_flags(b);
1179 if (ret < 0) {
1180 if (BIO_dgram_should_retry(ret)) {
1181 BIO_set_retry_read(b);
1182 data->_errno = get_last_socket_error();
1183 }
1184 }
1185
1186 /* Test if peer uses SCTP-AUTH before continuing */
1187 if (!data->peer_auth_tested) {
1188 int ii, auth_data = 0, auth_forward = 0;
1189 unsigned char *p;
1190 struct sctp_authchunks *authchunks;
1191
1192 optlen =
1193 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1194 authchunks = OPENSSL_malloc(optlen);
1195 if (authchunks == NULL) {
1196 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1197 return -1;
1198 }
1199 memset(authchunks, 0, optlen);
1200 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1201 authchunks, &optlen);
1202
1203 if (ii >= 0)
1204 for (p = (unsigned char *)authchunks->gauth_chunks;
1205 p < (unsigned char *)authchunks + optlen;
1206 p += sizeof(uint8_t)) {
1207 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1208 auth_data = 1;
1209 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1210 auth_forward = 1;
1211 }
1212
1213 OPENSSL_free(authchunks);
1214
1215 if (!auth_data || !auth_forward) {
1216 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1217 return -1;
1218 }
1219
1220 data->peer_auth_tested = 1;
1221 }
1222 }
1223 return (ret);
1224}
1225
1226/*
1227 * dgram_sctp_write - send message on SCTP socket
1228 * @b: BIO to write to
1229 * @in: data to send
1230 * @inl: amount of bytes in @in to send
1231 *
1232 * Returns -1 on error or the sent amount of bytes on success
1233 */
1234static int dgram_sctp_write(BIO *b, const char *in, int inl)
1235{
1236 int ret;
1237 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1238 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1239 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1240 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1241 struct iovec iov[1];
1242 struct msghdr msg;
1243 struct cmsghdr *cmsg;
1244# if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1245 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1246 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1247 struct sctp_sndinfo *sndinfo;
1248 struct sctp_prinfo *prinfo;
1249# else
1250 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1251 struct sctp_sndrcvinfo *sndrcvinfo;
1252# endif
1253
1254 clear_socket_error();
1255
1256 /*
1257 * If we're send anything else than application data, disable all user
1258 * parameters and flags.
1259 */
1260 if (in[0] != 23) {
1261 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1262# ifdef SCTP_SACK_IMMEDIATELY
1263 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1264# endif
1265 sinfo = &handshake_sinfo;
1266 }
1267
1268 /*
1269 * If we have to send a shutdown alert message and the socket is not dry
1270 * yet, we have to save it and send it as soon as the socket gets dry.
1271 */
1272 if (data->save_shutdown) {
1273 ret = BIO_dgram_sctp_wait_for_dry(b);
1274 if (ret < 0) {
1275 return -1;
1276 }
1277 if (ret == 0) {
1278 char *tmp;
1279 data->saved_message.bio = b;
1280 if ((tmp = OPENSSL_malloc(inl)) == NULL) {
1281 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1282 return -1;
1283 }
1284 OPENSSL_free(data->saved_message.data);
1285 data->saved_message.data = tmp;
1286 memcpy(data->saved_message.data, in, inl);
1287 data->saved_message.length = inl;
1288 return inl;
1289 }
1290 }
1291
1292 iov[0].iov_base = (char *)in;
1293 iov[0].iov_len = inl;
1294 msg.msg_name = NULL;
1295 msg.msg_namelen = 0;
1296 msg.msg_iov = iov;
1297 msg.msg_iovlen = 1;
1298 msg.msg_control = (caddr_t) cmsgbuf;
1299 msg.msg_controllen = 0;
1300 msg.msg_flags = 0;
1301# if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1302 cmsg = (struct cmsghdr *)cmsgbuf;
1303 cmsg->cmsg_level = IPPROTO_SCTP;
1304 cmsg->cmsg_type = SCTP_SNDINFO;
1305 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1306 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1307 memset(sndinfo, 0, sizeof(*sndinfo));
1308 sndinfo->snd_sid = sinfo->snd_sid;
1309 sndinfo->snd_flags = sinfo->snd_flags;
1310 sndinfo->snd_ppid = sinfo->snd_ppid;
1311 sndinfo->snd_context = sinfo->snd_context;
1312 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1313
1314 cmsg =
1315 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1316 cmsg->cmsg_level = IPPROTO_SCTP;
1317 cmsg->cmsg_type = SCTP_PRINFO;
1318 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1319 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1320 memset(prinfo, 0, sizeof(*prinfo));
1321 prinfo->pr_policy = pinfo->pr_policy;
1322 prinfo->pr_value = pinfo->pr_value;
1323 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1324# else
1325 cmsg = (struct cmsghdr *)cmsgbuf;
1326 cmsg->cmsg_level = IPPROTO_SCTP;
1327 cmsg->cmsg_type = SCTP_SNDRCV;
1328 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1329 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1330 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1331 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1332 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1333# ifdef __FreeBSD__
1334 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1335# endif
1336 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1337 sndrcvinfo->sinfo_context = sinfo->snd_context;
1338 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1339 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1340# endif
1341
1342 ret = sendmsg(b->num, &msg, 0);
1343
1344 BIO_clear_retry_flags(b);
1345 if (ret <= 0) {
1346 if (BIO_dgram_should_retry(ret)) {
1347 BIO_set_retry_write(b);
1348 data->_errno = get_last_socket_error();
1349 }
1350 }
1351 return (ret);
1352}
1353
1354static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1355{
1356 long ret = 1;
1357 bio_dgram_sctp_data *data = NULL;
1358 socklen_t sockopt_len = 0;
1359 struct sctp_authkeyid authkeyid;
1360 struct sctp_authkey *authkey = NULL;
1361
1362 data = (bio_dgram_sctp_data *) b->ptr;
1363
1364 switch (cmd) {
1365 case BIO_CTRL_DGRAM_QUERY_MTU:
1366 /*
1367 * Set to maximum (2^14) and ignore user input to enable transport
1368 * protocol fragmentation. Returns always 2^14.
1369 */
1370 data->mtu = 16384;
1371 ret = data->mtu;
1372 break;
1373 case BIO_CTRL_DGRAM_SET_MTU:
1374 /*
1375 * Set to maximum (2^14) and ignore input to enable transport
1376 * protocol fragmentation. Returns always 2^14.
1377 */
1378 data->mtu = 16384;
1379 ret = data->mtu;
1380 break;
1381 case BIO_CTRL_DGRAM_SET_CONNECTED:
1382 case BIO_CTRL_DGRAM_CONNECT:
1383 /* Returns always -1. */
1384 ret = -1;
1385 break;
1386 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1387 /*
1388 * SCTP doesn't need the DTLS timer Returns always 1.
1389 */
1390 break;
1391 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1392 /*
1393 * We allow transport protocol fragmentation so this is irrelevant
1394 */
1395 ret = 0;
1396 break;
1397 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1398 if (num > 0)
1399 data->in_handshake = 1;
1400 else
1401 data->in_handshake = 0;
1402
1403 ret =
1404 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1405 &data->in_handshake, sizeof(int));
1406 break;
1407 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1408 /*
1409 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1410 */
1411
1412 /* Get active key */
1413 sockopt_len = sizeof(struct sctp_authkeyid);
1414 ret =
1415 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1416 &sockopt_len);
1417 if (ret < 0)
1418 break;
1419
1420 /* Add new key */
1421 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1422 authkey = OPENSSL_malloc(sockopt_len);
1423 if (authkey == NULL) {
1424 ret = -1;
1425 break;
1426 }
1427 memset(authkey, 0, sockopt_len);
1428 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1429# ifndef __FreeBSD__
1430 /*
1431 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1432 * and higher work without it.
1433 */
1434 authkey->sca_keylength = 64;
1435# endif
1436 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1437
1438 ret =
1439 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1440 sockopt_len);
1441 OPENSSL_free(authkey);
1442 authkey = NULL;
1443 if (ret < 0)
1444 break;
1445
1446 /* Reset active key */
1447 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1448 &authkeyid, sizeof(struct sctp_authkeyid));
1449 if (ret < 0)
1450 break;
1451
1452 break;
1453 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1454 /* Returns 0 on success, -1 otherwise. */
1455
1456 /* Get active key */
1457 sockopt_len = sizeof(struct sctp_authkeyid);
1458 ret =
1459 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1460 &sockopt_len);
1461 if (ret < 0)
1462 break;
1463
1464 /* Set active key */
1465 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1466 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1467 &authkeyid, sizeof(struct sctp_authkeyid));
1468 if (ret < 0)
1469 break;
1470
1471 /*
1472 * CCS has been sent, so remember that and fall through to check if
1473 * we need to deactivate an old key
1474 */
1475 data->ccs_sent = 1;
1476
1477 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1478 /* Returns 0 on success, -1 otherwise. */
1479
1480 /*
1481 * Has this command really been called or is this just a
1482 * fall-through?
1483 */
1484 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1485 data->ccs_rcvd = 1;
1486
1487 /*
1488 * CSS has been both, received and sent, so deactivate an old key
1489 */
1490 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1491 /* Get active key */
1492 sockopt_len = sizeof(struct sctp_authkeyid);
1493 ret =
1494 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1495 &authkeyid, &sockopt_len);
1496 if (ret < 0)
1497 break;
1498
1499 /*
1500 * Deactivate key or delete second last key if
1501 * SCTP_AUTHENTICATION_EVENT is not available.
1502 */
1503 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1504# ifdef SCTP_AUTH_DEACTIVATE_KEY
1505 sockopt_len = sizeof(struct sctp_authkeyid);
1506 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1507 &authkeyid, sockopt_len);
1508 if (ret < 0)
1509 break;
1510# endif
1511# ifndef SCTP_AUTHENTICATION_EVENT
1512 if (authkeyid.scact_keynumber > 0) {
1513 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1514 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1515 &authkeyid, sizeof(struct sctp_authkeyid));
1516 if (ret < 0)
1517 break;
1518 }
1519# endif
1520
1521 data->ccs_rcvd = 0;
1522 data->ccs_sent = 0;
1523 }
1524 break;
1525 case BIO_CTRL_DGRAM_SCTP_GET_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);
1529
1530 memcpy(ptr, &(data->sndinfo), num);
1531 ret = num;
1532 break;
1533 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1534 /* Returns the size of the copied struct. */
1535 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1536 num = sizeof(struct bio_dgram_sctp_sndinfo);
1537
1538 memcpy(&(data->sndinfo), ptr, num);
1539 break;
1540 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1541 /* Returns the size of the copied struct. */
1542 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1543 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1544
1545 memcpy(ptr, &data->rcvinfo, num);
1546
1547 ret = num;
1548 break;
1549 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1550 /* Returns the size of the copied struct. */
1551 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1552 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1553
1554 memcpy(&(data->rcvinfo), ptr, num);
1555 break;
1556 case BIO_CTRL_DGRAM_SCTP_GET_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);
1560
1561 memcpy(ptr, &(data->prinfo), num);
1562 ret = num;
1563 break;
1564 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1565 /* Returns the size of the copied struct. */
1566 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1567 num = sizeof(struct bio_dgram_sctp_prinfo);
1568
1569 memcpy(&(data->prinfo), ptr, num);
1570 break;
1571 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1572 /* Returns always 1. */
1573 if (num > 0)
1574 data->save_shutdown = 1;
1575 else
1576 data->save_shutdown = 0;
1577 break;
1578
1579 default:
1580 /*
1581 * Pass to default ctrl function to process SCTP unspecific commands
1582 */
1583 ret = dgram_ctrl(b, cmd, num, ptr);
1584 break;
1585 }
1586 return (ret);
1587}
1588
1589int BIO_dgram_sctp_notification_cb(BIO *b,
1590 void (*handle_notifications) (BIO *bio,
1591 void
1592 *context,
1593 void *buf),
1594 void *context)
1595{
1596 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1597
1598 if (handle_notifications != NULL) {
1599 data->handle_notifications = handle_notifications;
1600 data->notification_context = context;
1601 } else
1602 return -1;
1603
1604 return 0;
1605}
1606
1607/*
1608 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1609 * @b: The BIO to check for the dry event
1610 *
1611 * Wait until the peer confirms all packets have been received, and so that
1612 * our kernel doesn't have anything to send anymore. This is only received by
1613 * the peer's kernel, not the application.
1614 *
1615 * Returns:
1616 * -1 on error
1617 * 0 when not dry yet
1618 * 1 when dry
1619 */
1620int BIO_dgram_sctp_wait_for_dry(BIO *b)
1621{
1622 int is_dry = 0;
1623 int sockflags = 0;
1624 int n, ret;
1625 union sctp_notification snp;
1626 struct msghdr msg;
1627 struct iovec iov;
1628# ifdef SCTP_EVENT
1629 struct sctp_event event;
1630# else
1631 struct sctp_event_subscribe event;
1632 socklen_t eventsize;
1633# endif
1634 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1635
1636 /* set sender dry event */
1637# ifdef SCTP_EVENT
1638 memset(&event, 0, sizeof(event));
1639 event.se_assoc_id = 0;
1640 event.se_type = SCTP_SENDER_DRY_EVENT;
1641 event.se_on = 1;
1642 ret =
1643 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1644 sizeof(struct sctp_event));
1645# else
1646 eventsize = sizeof(struct sctp_event_subscribe);
1647 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1648 if (ret < 0)
1649 return -1;
1650
1651 event.sctp_sender_dry_event = 1;
1652
1653 ret =
1654 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1655 sizeof(struct sctp_event_subscribe));
1656# endif
1657 if (ret < 0)
1658 return -1;
1659
1660 /* peek for notification */
1661 memset(&snp, 0, sizeof(snp));
1662 iov.iov_base = (char *)&snp;
1663 iov.iov_len = sizeof(union sctp_notification);
1664 msg.msg_name = NULL;
1665 msg.msg_namelen = 0;
1666 msg.msg_iov = &iov;
1667 msg.msg_iovlen = 1;
1668 msg.msg_control = NULL;
1669 msg.msg_controllen = 0;
1670 msg.msg_flags = 0;
1671
1672 n = recvmsg(b->num, &msg, MSG_PEEK);
1673 if (n <= 0) {
1674 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1675 && (get_last_socket_error() != EWOULDBLOCK))
1676 return -1;
1677 else
1678 return 0;
1679 }
1680
1681 /* if we find a notification, process it and try again if necessary */
1682 while (msg.msg_flags & MSG_NOTIFICATION) {
1683 memset(&snp, 0, sizeof(snp));
1684 iov.iov_base = (char *)&snp;
1685 iov.iov_len = sizeof(union sctp_notification);
1686 msg.msg_name = NULL;
1687 msg.msg_namelen = 0;
1688 msg.msg_iov = &iov;
1689 msg.msg_iovlen = 1;
1690 msg.msg_control = NULL;
1691 msg.msg_controllen = 0;
1692 msg.msg_flags = 0;
1693
1694 n = recvmsg(b->num, &msg, 0);
1695 if (n <= 0) {
1696 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1697 && (get_last_socket_error() != EWOULDBLOCK))
1698 return -1;
1699 else
1700 return is_dry;
1701 }
1702
1703 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1704 is_dry = 1;
1705
1706 /* disable sender dry event */
1707# ifdef SCTP_EVENT
1708 memset(&event, 0, sizeof(event));
1709 event.se_assoc_id = 0;
1710 event.se_type = SCTP_SENDER_DRY_EVENT;
1711 event.se_on = 0;
1712 ret =
1713 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1714 sizeof(struct sctp_event));
1715# else
1716 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1717 ret =
1718 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1719 &eventsize);
1720 if (ret < 0)
1721 return -1;
1722
1723 event.sctp_sender_dry_event = 0;
1724
1725 ret =
1726 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1727 sizeof(struct sctp_event_subscribe));
1728# endif
1729 if (ret < 0)
1730 return -1;
1731 }
1732# ifdef SCTP_AUTHENTICATION_EVENT
1733 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1734 dgram_sctp_handle_auth_free_key_event(b, &snp);
1735# endif
1736
1737 if (data->handle_notifications != NULL)
1738 data->handle_notifications(b, data->notification_context,
1739 (void *)&snp);
1740
1741 /* found notification, peek again */
1742 memset(&snp, 0, sizeof(snp));
1743 iov.iov_base = (char *)&snp;
1744 iov.iov_len = sizeof(union sctp_notification);
1745 msg.msg_name = NULL;
1746 msg.msg_namelen = 0;
1747 msg.msg_iov = &iov;
1748 msg.msg_iovlen = 1;
1749 msg.msg_control = NULL;
1750 msg.msg_controllen = 0;
1751 msg.msg_flags = 0;
1752
1753 /* if we have seen the dry already, don't wait */
1754 if (is_dry) {
1755 sockflags = fcntl(b->num, F_GETFL, 0);
1756 fcntl(b->num, F_SETFL, O_NONBLOCK);
1757 }
1758
1759 n = recvmsg(b->num, &msg, MSG_PEEK);
1760
1761 if (is_dry) {
1762 fcntl(b->num, F_SETFL, sockflags);
1763 }
1764
1765 if (n <= 0) {
1766 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1767 && (get_last_socket_error() != EWOULDBLOCK))
1768 return -1;
1769 else
1770 return is_dry;
1771 }
1772 }
1773
1774 /* read anything else */
1775 return is_dry;
1776}
1777
1778int BIO_dgram_sctp_msg_waiting(BIO *b)
1779{
1780 int n, sockflags;
1781 union sctp_notification snp;
1782 struct msghdr msg;
1783 struct iovec iov;
1784 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1785
1786 /* Check if there are any messages waiting to be read */
1787 do {
1788 memset(&snp, 0, sizeof(snp));
1789 iov.iov_base = (char *)&snp;
1790 iov.iov_len = sizeof(union sctp_notification);
1791 msg.msg_name = NULL;
1792 msg.msg_namelen = 0;
1793 msg.msg_iov = &iov;
1794 msg.msg_iovlen = 1;
1795 msg.msg_control = NULL;
1796 msg.msg_controllen = 0;
1797 msg.msg_flags = 0;
1798
1799 sockflags = fcntl(b->num, F_GETFL, 0);
1800 fcntl(b->num, F_SETFL, O_NONBLOCK);
1801 n = recvmsg(b->num, &msg, MSG_PEEK);
1802 fcntl(b->num, F_SETFL, sockflags);
1803
1804 /* if notification, process and try again */
1805 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1806# ifdef SCTP_AUTHENTICATION_EVENT
1807 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1808 dgram_sctp_handle_auth_free_key_event(b, &snp);
1809# endif
1810
1811 memset(&snp, 0, sizeof(snp));
1812 iov.iov_base = (char *)&snp;
1813 iov.iov_len = sizeof(union sctp_notification);
1814 msg.msg_name = NULL;
1815 msg.msg_namelen = 0;
1816 msg.msg_iov = &iov;
1817 msg.msg_iovlen = 1;
1818 msg.msg_control = NULL;
1819 msg.msg_controllen = 0;
1820 msg.msg_flags = 0;
1821 n = recvmsg(b->num, &msg, 0);
1822
1823 if (data->handle_notifications != NULL)
1824 data->handle_notifications(b, data->notification_context,
1825 (void *)&snp);
1826 }
1827
1828 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1829
1830 /* Return 1 if there is a message to be read, return 0 otherwise. */
1831 if (n > 0)
1832 return 1;
1833 else
1834 return 0;
1835}
1836
1837static int dgram_sctp_puts(BIO *bp, const char *str)
1838{
1839 int n, ret;
1840
1841 n = strlen(str);
1842 ret = dgram_sctp_write(bp, str, n);
1843 return (ret);
1844}
1845# endif
1846
1847static int BIO_dgram_should_retry(int i)
1848{
1849 int err;
1850
1851 if ((i == 0) || (i == -1)) {
1852 err = get_last_socket_error();
1853
1854# if defined(OPENSSL_SYS_WINDOWS)
1855 /*
1856 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1857 * this point, the error code was overwritten by another system call
1858 * before this error handling is called.
1859 */
1860# endif
1861
1862 return (BIO_dgram_non_fatal_error(err));
1863 }
1864 return (0);
1865}
1866
1867int BIO_dgram_non_fatal_error(int err)
1868{
1869 switch (err) {
1870# if defined(OPENSSL_SYS_WINDOWS)
1871# if defined(WSAEWOULDBLOCK)
1872 case WSAEWOULDBLOCK:
1873# endif
1874# endif
1875
1876# ifdef EWOULDBLOCK
1877# ifdef WSAEWOULDBLOCK
1878# if WSAEWOULDBLOCK != EWOULDBLOCK
1879 case EWOULDBLOCK:
1880# endif
1881# else
1882 case EWOULDBLOCK:
1883# endif
1884# endif
1885
1886# ifdef EINTR
1887 case EINTR:
1888# endif
1889
1890# ifdef EAGAIN
1891# if EWOULDBLOCK != EAGAIN
1892 case EAGAIN:
1893# endif
1894# endif
1895
1896# ifdef EPROTO
1897 case EPROTO:
1898# endif
1899
1900# ifdef EINPROGRESS
1901 case EINPROGRESS:
1902# endif
1903
1904# ifdef EALREADY
1905 case EALREADY:
1906# endif
1907
1908 return (1);
1909 /* break; */
1910 default:
1911 break;
1912 }
1913 return (0);
1914}
1915
1916static void get_current_time(struct timeval *t)
1917{
1918# if defined(_WIN32)
1919 SYSTEMTIME st;
1920 union {
1921 unsigned __int64 ul;
1922 FILETIME ft;
1923 } now;
1924
1925 GetSystemTime(&st);
1926 SystemTimeToFileTime(&st, &now.ft);
1927# ifdef __MINGW32__
1928 now.ul -= 116444736000000000ULL;
1929# else
1930 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1931# endif
1932 t->tv_sec = (long)(now.ul / 10000000);
1933 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1934# elif defined(OPENSSL_SYS_VMS)
1935 struct timeb tb;
1936 ftime(&tb);
1937 t->tv_sec = (long)tb.time;
1938 t->tv_usec = (long)tb.millitm * 1000;
1939# else
1940 gettimeofday(t, NULL);
1941# endif
1942}
1943
1944#endif
Note: See TracBrowser for help on using the repository browser.