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