Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bss_dgram.c
Line
Count
Source
1
/*
2
 * Copyright 2005-2025 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
#ifndef _GNU_SOURCE
11
#define _GNU_SOURCE
12
#endif
13
14
#include <stdio.h>
15
#include <errno.h>
16
17
#include "internal/time.h"
18
#include "bio_local.h"
19
#ifndef OPENSSL_NO_DGRAM
20
21
#ifndef OPENSSL_NO_SCTP
22
#include <netinet/sctp.h>
23
#include <fcntl.h>
24
#define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
25
#define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
26
#endif
27
28
#if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
29
#define IP_MTU 14 /* linux is lame */
30
#endif
31
32
#if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
33
#define IPPROTO_IPV6 41 /* windows is lame */
34
#endif
35
36
#if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
37
/* Standard definition causes type-punning problems. */
38
#undef IN6_IS_ADDR_V4MAPPED
39
#define s6_addr32 __u6_addr.__u6_addr32
40
#define IN6_IS_ADDR_V4MAPPED(a) \
41
    (((a)->s6_addr32[0] == 0) && ((a)->s6_addr32[1] == 0) && ((a)->s6_addr32[2] == htonl(0x0000ffff)))
42
#endif
43
44
/* Determine what method to use for BIO_sendmmsg and BIO_recvmmsg. */
45
#define M_METHOD_NONE 0
46
#define M_METHOD_RECVMMSG 1
47
#define M_METHOD_RECVMSG 2
48
#define M_METHOD_RECVFROM 3
49
#define M_METHOD_WSARECVMSG 4
50
51
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
52
#if !(__GLIBC_PREREQ(2, 14))
53
#undef NO_RECVMMSG
54
/*
55
 * Some old glibc versions may have recvmmsg and MSG_WAITFORONE flag, but
56
 * not sendmmsg. We need both so force this to be disabled on these old
57
 * versions
58
 */
59
#define NO_RECVMMSG
60
#endif
61
#endif
62
#if defined(__GNU__)
63
/* GNU/Hurd does not have IP_PKTINFO yet */
64
#undef NO_RECVMSG
65
#define NO_RECVMSG
66
#endif
67
#if (defined(__ANDROID_API__) && __ANDROID_API__ < 21) || defined(_AIX)
68
#undef NO_RECVMMSG
69
#define NO_RECVMMSG
70
#endif
71
#if !defined(M_METHOD)
72
#if defined(OPENSSL_SYS_WINDOWS) && defined(BIO_HAVE_WSAMSG) && !defined(NO_WSARECVMSG)
73
#define M_METHOD M_METHOD_WSARECVMSG
74
#elif !defined(OPENSSL_SYS_WINDOWS) && defined(MSG_WAITFORONE) && !defined(NO_RECVMMSG)
75
#define M_METHOD M_METHOD_RECVMMSG
76
#elif !defined(OPENSSL_SYS_WINDOWS) && defined(CMSG_LEN) && !defined(NO_RECVMSG)
77
#define M_METHOD M_METHOD_RECVMSG
78
#elif !defined(NO_RECVFROM)
79
#define M_METHOD M_METHOD_RECVFROM
80
#else
81
#define M_METHOD M_METHOD_NONE
82
#endif
83
#endif
84
85
#if defined(OPENSSL_SYS_WINDOWS)
86
#define BIO_CMSG_SPACE(x) WSA_CMSG_SPACE(x)
87
#define BIO_CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
88
#define BIO_CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
89
#define BIO_CMSG_DATA(x) WSA_CMSG_DATA(x)
90
#define BIO_CMSG_LEN(x) WSA_CMSG_LEN(x)
91
#define MSGHDR_TYPE WSAMSG
92
#define CMSGHDR_TYPE WSACMSGHDR
93
#else
94
#define MSGHDR_TYPE struct msghdr
95
0
#define CMSGHDR_TYPE struct cmsghdr
96
0
#define BIO_CMSG_SPACE(x) CMSG_SPACE(x)
97
0
#define BIO_CMSG_FIRSTHDR(x) CMSG_FIRSTHDR(x)
98
0
#define BIO_CMSG_NXTHDR(x, y) CMSG_NXTHDR(x, y)
99
0
#define BIO_CMSG_DATA(x) CMSG_DATA(x)
100
0
#define BIO_CMSG_LEN(x) CMSG_LEN(x)
101
#endif
102
103
#if M_METHOD == M_METHOD_RECVMMSG   \
104
    || M_METHOD == M_METHOD_RECVMSG \
105
    || M_METHOD == M_METHOD_WSARECVMSG
106
#if defined(__APPLE__)
107
/*
108
 * CMSG_SPACE is not a constant expression on OSX even though POSIX
109
 * says it's supposed to be. This should be adequate.
110
 */
111
#define BIO_CMSG_ALLOC_LEN 64
112
#else
113
#if defined(IPV6_PKTINFO)
114
#define BIO_CMSG_ALLOC_LEN_1 BIO_CMSG_SPACE(sizeof(struct in6_pktinfo))
115
#else
116
#define BIO_CMSG_ALLOC_LEN_1 0
117
#endif
118
#if defined(IP_PKTINFO)
119
#define BIO_CMSG_ALLOC_LEN_2 BIO_CMSG_SPACE(sizeof(struct in_pktinfo))
120
#else
121
#define BIO_CMSG_ALLOC_LEN_2 0
122
#endif
123
#if defined(IP_RECVDSTADDR)
124
#define BIO_CMSG_ALLOC_LEN_3 BIO_CMSG_SPACE(sizeof(struct in_addr))
125
#else
126
#define BIO_CMSG_ALLOC_LEN_3 0
127
#endif
128
0
#define BIO_MAX(X, Y) ((X) > (Y) ? (X) : (Y))
129
#define BIO_CMSG_ALLOC_LEN        \
130
0
    BIO_MAX(BIO_CMSG_ALLOC_LEN_1, \
131
0
        BIO_MAX(BIO_CMSG_ALLOC_LEN_2, BIO_CMSG_ALLOC_LEN_3))
132
#endif
133
#if (defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)) && defined(IPV6_RECVPKTINFO)
134
#define SUPPORT_LOCAL_ADDR
135
#endif
136
#endif
137
138
0
#define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n) * (stride)))
139
140
static int dgram_write(BIO *h, const char *buf, int num);
141
static int dgram_read(BIO *h, char *buf, int size);
142
static int dgram_puts(BIO *h, const char *str);
143
static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
144
static int dgram_new(BIO *h);
145
static int dgram_free(BIO *data);
146
static int dgram_clear(BIO *bio);
147
static int dgram_sendmmsg(BIO *b, BIO_MSG *msg,
148
    size_t stride, size_t num_msg,
149
    uint64_t flags, size_t *num_processed);
150
static int dgram_recvmmsg(BIO *b, BIO_MSG *msg,
151
    size_t stride, size_t num_msg,
152
    uint64_t flags, size_t *num_processed);
153
154
#ifndef OPENSSL_NO_SCTP
155
static int dgram_sctp_write(BIO *h, const char *buf, int num);
156
static int dgram_sctp_read(BIO *h, char *buf, int size);
157
static int dgram_sctp_puts(BIO *h, const char *str);
158
static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
159
static int dgram_sctp_new(BIO *h);
160
static int dgram_sctp_free(BIO *data);
161
static int dgram_sctp_wait_for_dry(BIO *b);
162
static int dgram_sctp_msg_waiting(BIO *b);
163
#ifdef SCTP_AUTHENTICATION_EVENT
164
static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp);
165
#endif
166
#endif
167
168
static int BIO_dgram_should_retry(int s);
169
170
static const BIO_METHOD methods_dgramp = {
171
    BIO_TYPE_DGRAM,
172
    "datagram socket",
173
    bwrite_conv,
174
    dgram_write,
175
    bread_conv,
176
    dgram_read,
177
    dgram_puts,
178
    NULL, /* dgram_gets,         */
179
    dgram_ctrl,
180
    dgram_new,
181
    dgram_free,
182
    NULL, /* dgram_callback_ctrl */
183
    dgram_sendmmsg,
184
    dgram_recvmmsg,
185
};
186
187
#ifndef OPENSSL_NO_SCTP
188
static const BIO_METHOD methods_dgramp_sctp = {
189
    BIO_TYPE_DGRAM_SCTP,
190
    "datagram sctp socket",
191
    bwrite_conv,
192
    dgram_sctp_write,
193
    bread_conv,
194
    dgram_sctp_read,
195
    dgram_sctp_puts,
196
    NULL, /* dgram_gets,         */
197
    dgram_sctp_ctrl,
198
    dgram_sctp_new,
199
    dgram_sctp_free,
200
    NULL, /* dgram_callback_ctrl */
201
    NULL, /* sendmmsg */
202
    NULL, /* recvmmsg */
203
};
204
#endif
205
206
typedef struct bio_dgram_data_st {
207
    BIO_ADDR peer;
208
    BIO_ADDR local_addr;
209
    unsigned int connected;
210
    unsigned int _errno;
211
    unsigned int mtu;
212
    OSSL_TIME next_timeout;
213
    OSSL_TIME socket_timeout;
214
    unsigned int peekmode;
215
    char local_addr_enabled;
216
} bio_dgram_data;
217
218
#ifndef OPENSSL_NO_SCTP
219
typedef struct bio_dgram_sctp_save_message_st {
220
    BIO *bio;
221
    char *data;
222
    int length;
223
} bio_dgram_sctp_save_message;
224
225
/*
226
 * Note: bio_dgram_data must be first here
227
 * as we use dgram_ctrl for underlying dgram operations
228
 * which will cast this struct to a bio_dgram_data
229
 */
230
typedef struct bio_dgram_sctp_data_st {
231
    bio_dgram_data dgram;
232
    struct bio_dgram_sctp_sndinfo sndinfo;
233
    struct bio_dgram_sctp_rcvinfo rcvinfo;
234
    struct bio_dgram_sctp_prinfo prinfo;
235
    BIO_dgram_sctp_notification_handler_fn handle_notifications;
236
    void *notification_context;
237
    int in_handshake;
238
    int ccs_rcvd;
239
    int ccs_sent;
240
    int save_shutdown;
241
    int peer_auth_tested;
242
} bio_dgram_sctp_data;
243
#endif
244
245
const BIO_METHOD *BIO_s_datagram(void)
246
0
{
247
0
    return &methods_dgramp;
248
0
}
249
250
BIO *BIO_new_dgram(int fd, int close_flag)
251
0
{
252
0
    BIO *ret;
253
254
0
    ret = BIO_new(BIO_s_datagram());
255
0
    if (ret == NULL)
256
0
        return NULL;
257
0
    BIO_set_fd(ret, fd, close_flag);
258
0
    return ret;
259
0
}
260
261
static int dgram_new(BIO *bi)
262
0
{
263
0
    bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
264
265
0
    if (data == NULL)
266
0
        return 0;
267
0
    bi->ptr = data;
268
0
    return 1;
269
0
}
270
271
static int dgram_free(BIO *a)
272
0
{
273
0
    bio_dgram_data *data;
274
275
0
    if (a == NULL)
276
0
        return 0;
277
0
    if (!dgram_clear(a))
278
0
        return 0;
279
280
0
    data = (bio_dgram_data *)a->ptr;
281
0
    OPENSSL_free(data);
282
283
0
    return 1;
284
0
}
285
286
static int dgram_clear(BIO *a)
287
0
{
288
0
    if (a == NULL)
289
0
        return 0;
290
0
    if (a->shutdown) {
291
0
        if (a->init) {
292
0
            BIO_closesocket(a->num);
293
0
        }
294
0
        a->init = 0;
295
0
        a->flags = 0;
296
0
    }
297
0
    return 1;
298
0
}
299
300
static void dgram_adjust_rcv_timeout(BIO *b)
301
0
{
302
0
#if defined(SO_RCVTIMEO)
303
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
304
0
    OSSL_TIME timeleft;
305
306
    /* Is a timer active? */
307
0
    if (!ossl_time_is_zero(data->next_timeout)) {
308
        /* Read current socket timeout */
309
#ifdef OPENSSL_SYS_WINDOWS
310
        int timeout;
311
        int sz = sizeof(timeout);
312
313
        if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
314
                (void *)&timeout, &sz)
315
            < 0)
316
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
317
                "calling getsockopt()");
318
        else
319
            data->socket_timeout = ossl_ms2time(timeout);
320
#else
321
0
        struct timeval tv;
322
0
        socklen_t sz = sizeof(tv);
323
324
0
        if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv, &sz) < 0)
325
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
326
0
                "calling getsockopt()");
327
0
        else
328
0
            data->socket_timeout = ossl_time_from_timeval(tv);
329
0
#endif
330
331
        /* Calculate time left until timer expires */
332
0
        timeleft = ossl_time_subtract(data->next_timeout, ossl_time_now());
333
0
        if (ossl_time_compare(timeleft, ossl_ticks2time(OSSL_TIME_US)) < 0)
334
0
            timeleft = ossl_ticks2time(OSSL_TIME_US);
335
336
        /*
337
         * Adjust socket timeout if next handshake message timer will expire
338
         * earlier.
339
         */
340
0
        if (ossl_time_is_zero(data->socket_timeout)
341
0
            || ossl_time_compare(data->socket_timeout, timeleft) >= 0) {
342
#ifdef OPENSSL_SYS_WINDOWS
343
            timeout = (int)ossl_time2ms(timeleft);
344
            if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
345
                    (void *)&timeout, sizeof(timeout))
346
                < 0)
347
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
348
                    "calling setsockopt()");
349
#else
350
0
            tv = ossl_time_to_timeval(timeleft);
351
0
            if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv,
352
0
                    sizeof(tv))
353
0
                < 0)
354
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
355
0
                    "calling setsockopt()");
356
0
#endif
357
0
        }
358
0
    }
359
0
#endif
360
0
}
361
362
static void dgram_update_local_addr(BIO *b)
363
0
{
364
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
365
0
    socklen_t addr_len = sizeof(data->local_addr);
366
367
0
    if (getsockname(b->num, &data->local_addr.sa, &addr_len) < 0)
368
        /*
369
         * This should not be possible, but zero-initialize and return
370
         * anyway.
371
         */
372
0
        BIO_ADDR_clear(&data->local_addr);
373
0
}
374
375
#if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
376
static int dgram_get_sock_family(BIO *b)
377
0
{
378
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
379
0
    return data->local_addr.sa.sa_family;
380
0
}
381
#endif
382
383
static void dgram_reset_rcv_timeout(BIO *b)
384
0
{
385
0
#if defined(SO_RCVTIMEO)
386
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
387
388
    /* Is a timer active? */
389
0
    if (!ossl_time_is_zero(data->next_timeout)) {
390
#ifdef OPENSSL_SYS_WINDOWS
391
        int timeout = (int)ossl_time2ms(data->socket_timeout);
392
393
        if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
394
                (void *)&timeout, sizeof(timeout))
395
            < 0)
396
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
397
                "calling setsockopt()");
398
#else
399
0
        struct timeval tv = ossl_time_to_timeval(data->socket_timeout);
400
401
0
        if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
402
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
403
0
                "calling setsockopt()");
404
0
#endif
405
0
    }
406
0
#endif
407
0
}
408
409
static int dgram_read(BIO *b, char *out, int outl)
410
0
{
411
0
    int ret = 0;
412
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
413
0
    int flags = 0;
414
415
0
    BIO_ADDR peer;
416
0
    socklen_t len = sizeof(peer);
417
418
0
    if (out != NULL) {
419
0
        clear_socket_error();
420
0
        BIO_ADDR_clear(&peer);
421
0
        dgram_adjust_rcv_timeout(b);
422
0
        if (data->peekmode)
423
0
            flags = MSG_PEEK;
424
0
        ret = recvfrom(b->num, out, outl, flags,
425
0
            BIO_ADDR_sockaddr_noconst(&peer), &len);
426
427
0
        if (!data->connected && ret >= 0)
428
0
            BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
429
430
0
        BIO_clear_retry_flags(b);
431
0
        if (ret < 0) {
432
0
            if (BIO_dgram_should_retry(ret)) {
433
0
                BIO_set_retry_read(b);
434
0
                data->_errno = get_last_socket_error();
435
0
            }
436
0
        }
437
438
0
        dgram_reset_rcv_timeout(b);
439
0
    }
440
0
    return ret;
441
0
}
442
443
static int dgram_write(BIO *b, const char *in, int inl)
444
0
{
445
0
    int ret;
446
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
447
0
    clear_socket_error();
448
449
0
    if (data->connected)
450
0
        ret = writesocket(b->num, in, inl);
451
0
    else {
452
0
        int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
453
454
0
        ret = sendto(b->num, in, inl, 0,
455
0
            BIO_ADDR_sockaddr(&data->peer), peerlen);
456
0
    }
457
458
0
    BIO_clear_retry_flags(b);
459
0
    if (ret <= 0) {
460
0
        if (BIO_dgram_should_retry(ret)) {
461
0
            BIO_set_retry_write(b);
462
0
            data->_errno = get_last_socket_error();
463
0
        }
464
0
    }
465
0
    return ret;
466
0
}
467
468
static long dgram_get_mtu_overhead(BIO_ADDR *addr)
469
0
{
470
0
    long ret;
471
472
0
    switch (BIO_ADDR_family(addr)) {
473
0
    case AF_INET:
474
        /*
475
         * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
476
         */
477
0
        ret = 28;
478
0
        break;
479
0
#if OPENSSL_USE_IPV6
480
0
    case AF_INET6: {
481
0
#ifdef IN6_IS_ADDR_V4MAPPED
482
0
        struct in6_addr tmp_addr;
483
484
0
        if (BIO_ADDR_rawaddress(addr, &tmp_addr, NULL)
485
0
            && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
486
            /*
487
             * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
488
             */
489
0
            ret = 28;
490
0
        else
491
0
#endif
492
            /*
493
             * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
494
             */
495
0
            ret = 48;
496
0
    } break;
497
0
#endif
498
0
    default:
499
        /* We don't know. Go with the historical default */
500
0
        ret = 28;
501
0
        break;
502
0
    }
503
0
    return ret;
504
0
}
505
506
/* Enables appropriate destination address reception option on the socket. */
507
#if defined(SUPPORT_LOCAL_ADDR)
508
static int enable_local_addr(BIO *b, int enable)
509
0
{
510
0
    int af = dgram_get_sock_family(b);
511
512
0
    if (af == AF_INET) {
513
0
#if defined(IP_PKTINFO)
514
        /* IP_PKTINFO is preferred */
515
0
        if (setsockopt(b->num, IPPROTO_IP, IP_PKTINFO,
516
0
                (void *)&enable, sizeof(enable))
517
0
            < 0)
518
0
            return 0;
519
520
0
        return 1;
521
522
#elif defined(IP_RECVDSTADDR)
523
        /* Fall back to IP_RECVDSTADDR */
524
525
        if (setsockopt(b->num, IPPROTO_IP, IP_RECVDSTADDR,
526
                &enable, sizeof(enable))
527
            < 0)
528
            return 0;
529
530
        return 1;
531
#endif
532
0
    }
533
534
0
#if OPENSSL_USE_IPV6
535
0
    if (af == AF_INET6) {
536
0
#if defined(IPV6_RECVPKTINFO)
537
0
        if (setsockopt(b->num, IPPROTO_IPV6, IPV6_RECVPKTINFO,
538
0
                &enable, sizeof(enable))
539
0
            < 0)
540
0
            return 0;
541
542
0
        return 1;
543
0
#endif
544
0
    }
545
0
#endif
546
547
0
    return 0;
548
0
}
549
#endif
550
551
static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
552
0
{
553
0
    long ret = 1;
554
0
    int *ip;
555
0
    bio_dgram_data *data = NULL;
556
0
#ifndef __DJGPP__
557
    /* There are currently no cases where this is used on djgpp/watt32. */
558
0
    int sockopt_val = 0;
559
0
#endif
560
0
    int d_errno;
561
0
#if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
562
0
    socklen_t sockopt_len; /* assume that system supporting IP_MTU is
563
                            * modern enough to define socklen_t */
564
0
    socklen_t addr_len;
565
0
    BIO_ADDR addr;
566
0
#endif
567
0
    struct sockaddr_storage ss;
568
0
    socklen_t ss_len = sizeof(ss);
569
570
0
    data = (bio_dgram_data *)b->ptr;
571
572
0
    switch (cmd) {
573
0
    case BIO_CTRL_RESET:
574
0
        num = 0;
575
0
        ret = 0;
576
0
        break;
577
0
    case BIO_CTRL_INFO:
578
0
        ret = 0;
579
0
        break;
580
0
    case BIO_C_SET_FD:
581
0
        dgram_clear(b);
582
0
        b->num = *((int *)ptr);
583
0
        b->shutdown = (int)num;
584
0
        b->init = 1;
585
0
        dgram_update_local_addr(b);
586
0
        if (getpeername(b->num, (struct sockaddr *)&ss, &ss_len) == 0) {
587
0
            BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)&ss));
588
0
            data->connected = 1;
589
0
        }
590
0
#if defined(SUPPORT_LOCAL_ADDR)
591
0
        if (data->local_addr_enabled) {
592
0
            if (enable_local_addr(b, 1) < 1)
593
0
                data->local_addr_enabled = 0;
594
0
        }
595
0
#endif
596
0
        break;
597
0
    case BIO_C_GET_FD:
598
0
        if (b->init) {
599
0
            ip = (int *)ptr;
600
0
            if (ip != NULL)
601
0
                *ip = b->num;
602
0
            ret = b->num;
603
0
        } else
604
0
            ret = -1;
605
0
        break;
606
0
    case BIO_CTRL_GET_CLOSE:
607
0
        ret = b->shutdown;
608
0
        break;
609
0
    case BIO_CTRL_SET_CLOSE:
610
0
        b->shutdown = (int)num;
611
0
        break;
612
0
    case BIO_CTRL_PENDING:
613
0
    case BIO_CTRL_WPENDING:
614
0
        ret = 0;
615
0
        break;
616
0
    case BIO_CTRL_DUP:
617
0
    case BIO_CTRL_FLUSH:
618
0
        ret = 1;
619
0
        break;
620
0
    case BIO_CTRL_DGRAM_CONNECT:
621
0
        BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
622
0
        break;
623
        /* (Linux)kernel sets DF bit on outgoing IP packets */
624
0
    case BIO_CTRL_DGRAM_MTU_DISCOVER:
625
0
#if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
626
0
        addr_len = (socklen_t)sizeof(addr);
627
0
        BIO_ADDR_clear(&addr);
628
0
        if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
629
0
            ret = 0;
630
0
            break;
631
0
        }
632
0
        switch (addr.sa.sa_family) {
633
0
        case AF_INET:
634
0
            sockopt_val = IP_PMTUDISC_DO;
635
0
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
636
0
                     &sockopt_val, sizeof(sockopt_val)))
637
0
                < 0)
638
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
639
0
                    "calling setsockopt()");
640
0
            break;
641
0
#if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
642
0
        case AF_INET6:
643
0
            sockopt_val = IPV6_PMTUDISC_DO;
644
0
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
645
0
                     &sockopt_val, sizeof(sockopt_val)))
646
0
                < 0)
647
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
648
0
                    "calling setsockopt()");
649
0
            break;
650
0
#endif
651
0
        default:
652
0
            ret = -1;
653
0
            break;
654
0
        }
655
#else
656
        ret = -1;
657
#endif
658
0
        break;
659
0
    case BIO_CTRL_DGRAM_QUERY_MTU:
660
0
#if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
661
0
        addr_len = (socklen_t)sizeof(addr);
662
0
        BIO_ADDR_clear(&addr);
663
0
        if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
664
0
            ret = 0;
665
0
            break;
666
0
        }
667
0
        sockopt_len = sizeof(sockopt_val);
668
0
        switch (addr.sa.sa_family) {
669
0
        case AF_INET:
670
0
            if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
671
0
                     &sockopt_len))
672
0
                    < 0
673
0
                || sockopt_val < 0) {
674
0
                ret = 0;
675
0
            } else {
676
0
                data->mtu = sockopt_val - dgram_get_mtu_overhead(&addr);
677
0
                ret = data->mtu;
678
0
            }
679
0
            break;
680
0
#if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
681
0
        case AF_INET6:
682
0
            if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
683
0
                     (void *)&sockopt_val, &sockopt_len))
684
0
                    < 0
685
0
                || sockopt_val < 0) {
686
0
                ret = 0;
687
0
            } else {
688
0
                data->mtu = sockopt_val - dgram_get_mtu_overhead(&addr);
689
0
                ret = data->mtu;
690
0
            }
691
0
            break;
692
0
#endif
693
0
        default:
694
0
            ret = 0;
695
0
            break;
696
0
        }
697
#else
698
        ret = 0;
699
#endif
700
0
        break;
701
0
    case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
702
0
        ret = -dgram_get_mtu_overhead(&data->peer);
703
0
        switch (BIO_ADDR_family(&data->peer)) {
704
0
        case AF_INET:
705
0
            ret += 576;
706
0
            break;
707
0
#if OPENSSL_USE_IPV6
708
0
        case AF_INET6: {
709
0
#ifdef IN6_IS_ADDR_V4MAPPED
710
0
            struct in6_addr tmp_addr;
711
0
            if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
712
0
                && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
713
0
                ret += 576;
714
0
            else
715
0
#endif
716
0
                ret += 1280;
717
0
        } break;
718
0
#endif
719
0
        default:
720
0
            ret += 576;
721
0
            break;
722
0
        }
723
0
        break;
724
0
    case BIO_CTRL_DGRAM_GET_MTU:
725
0
        return data->mtu;
726
0
    case BIO_CTRL_DGRAM_SET_MTU:
727
0
        data->mtu = num;
728
0
        ret = num;
729
0
        break;
730
0
    case BIO_CTRL_DGRAM_SET_CONNECTED:
731
0
        if (ptr != NULL) {
732
0
            data->connected = 1;
733
0
            BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
734
0
        } else {
735
0
            data->connected = 0;
736
0
            BIO_ADDR_clear(&data->peer);
737
0
        }
738
0
        break;
739
0
    case BIO_CTRL_DGRAM_GET_PEER:
740
0
        ret = BIO_ADDR_sockaddr_size(&data->peer);
741
        /* FIXME: if num < ret, we will only return part of an address.
742
           That should bee an error, no? */
743
0
        if (num == 0 || num > ret)
744
0
            num = ret;
745
0
        memcpy(ptr, &data->peer, (ret = num));
746
0
        break;
747
0
    case BIO_CTRL_DGRAM_SET_PEER:
748
0
        BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
749
0
        break;
750
0
    case BIO_CTRL_DGRAM_DETECT_PEER_ADDR: {
751
0
        BIO_ADDR xaddr, *p = &data->peer;
752
0
        socklen_t xaddr_len = sizeof(xaddr.sa);
753
754
0
        if (BIO_ADDR_family(p) == AF_UNSPEC) {
755
0
            if (getpeername(b->num, (void *)&xaddr.sa, &xaddr_len) == 0
756
0
                && BIO_ADDR_family(&xaddr) != AF_UNSPEC) {
757
0
                p = &xaddr;
758
0
            } else {
759
0
                ret = 0;
760
0
                break;
761
0
            }
762
0
        }
763
764
0
        ret = BIO_ADDR_sockaddr_size(p);
765
0
        if (num == 0 || num > ret)
766
0
            num = ret;
767
768
0
        memcpy(ptr, p, (ret = num));
769
0
    } break;
770
0
    case BIO_C_SET_NBIO:
771
0
        if (!BIO_socket_nbio(b->num, num != 0))
772
0
            ret = 0;
773
0
        break;
774
0
    case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
775
0
        data->next_timeout = ossl_time_from_timeval(*(struct timeval *)ptr);
776
0
        break;
777
0
#if defined(SO_RCVTIMEO)
778
0
    case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
779
#ifdef OPENSSL_SYS_WINDOWS
780
    {
781
        struct timeval *tv = (struct timeval *)ptr;
782
        int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
783
784
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
785
                 (void *)&timeout, sizeof(timeout)))
786
            < 0)
787
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
788
                "calling setsockopt()");
789
    }
790
#else
791
0
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
792
0
                 sizeof(struct timeval)))
793
0
            < 0)
794
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
795
0
                "calling setsockopt()");
796
0
#endif
797
0
    break;
798
0
    case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: {
799
#ifdef OPENSSL_SYS_WINDOWS
800
        int sz = 0;
801
        int timeout;
802
        struct timeval *tv = (struct timeval *)ptr;
803
804
        sz = sizeof(timeout);
805
        if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
806
                 (void *)&timeout, &sz))
807
            < 0) {
808
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
809
                "calling getsockopt()");
810
        } else {
811
            tv->tv_sec = timeout / 1000;
812
            tv->tv_usec = (timeout % 1000) * 1000;
813
            ret = sizeof(*tv);
814
        }
815
#else
816
0
        socklen_t sz = sizeof(struct timeval);
817
818
0
        if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
819
0
                 ptr, &sz))
820
0
            < 0) {
821
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
822
0
                "calling getsockopt()");
823
0
        } else if (!ossl_assert((size_t)sz == sizeof(struct timeval))) {
824
0
            ERR_raise_data(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR,
825
0
                "Unexpected getsockopt(SO_RCVTIMEO) return size");
826
0
            ret = -1;
827
0
        } else {
828
0
            ret = (int)sz;
829
0
        }
830
0
#endif
831
0
    } break;
832
0
#endif
833
0
#if defined(SO_SNDTIMEO)
834
0
    case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
835
#ifdef OPENSSL_SYS_WINDOWS
836
    {
837
        struct timeval *tv = (struct timeval *)ptr;
838
        int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
839
840
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
841
                 (void *)&timeout, sizeof(timeout)))
842
            < 0)
843
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
844
                "calling setsockopt()");
845
    }
846
#else
847
0
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
848
0
                 sizeof(struct timeval)))
849
0
            < 0)
850
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
851
0
                "calling setsockopt()");
852
0
#endif
853
0
    break;
854
0
    case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: {
855
#ifdef OPENSSL_SYS_WINDOWS
856
        int sz = 0;
857
        int timeout;
858
        struct timeval *tv = (struct timeval *)ptr;
859
860
        sz = sizeof(timeout);
861
        if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
862
                 (void *)&timeout, &sz))
863
            < 0) {
864
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
865
                "calling getsockopt()");
866
        } else {
867
            tv->tv_sec = timeout / 1000;
868
            tv->tv_usec = (timeout % 1000) * 1000;
869
            ret = sizeof(*tv);
870
        }
871
#else
872
0
        socklen_t sz = sizeof(struct timeval);
873
874
0
        if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
875
0
                 ptr, &sz))
876
0
            < 0) {
877
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
878
0
                "calling getsockopt()");
879
0
        } else if (!ossl_assert((size_t)sz == sizeof(struct timeval))) {
880
0
            ERR_raise_data(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR,
881
0
                "Unexpected getsockopt(SO_SNDTIMEO) return size");
882
0
            ret = -1;
883
0
        } else {
884
0
            ret = (int)sz;
885
0
        }
886
0
#endif
887
0
    } break;
888
0
#endif
889
0
    case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
890
        /* fall-through */
891
0
    case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
892
#ifdef OPENSSL_SYS_WINDOWS
893
        d_errno = (data->_errno == WSAETIMEDOUT);
894
#else
895
0
        d_errno = (data->_errno == EAGAIN);
896
0
#endif
897
0
        if (d_errno) {
898
0
            ret = 1;
899
0
            data->_errno = 0;
900
0
        } else
901
0
            ret = 0;
902
0
        break;
903
0
#ifdef EMSGSIZE
904
0
    case BIO_CTRL_DGRAM_MTU_EXCEEDED:
905
0
        if (data->_errno == EMSGSIZE) {
906
0
            ret = 1;
907
0
            data->_errno = 0;
908
0
        } else
909
0
            ret = 0;
910
0
        break;
911
0
#endif
912
0
    case BIO_CTRL_DGRAM_SET_DONT_FRAG:
913
0
        switch (data->peer.sa.sa_family) {
914
0
        case AF_INET:
915
#if defined(IP_DONTFRAG)
916
            sockopt_val = num ? 1 : 0;
917
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
918
                     &sockopt_val, sizeof(sockopt_val)))
919
                < 0)
920
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
921
                    "calling setsockopt()");
922
#elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_PROBE)
923
0
            sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT;
924
0
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
925
0
                     &sockopt_val, sizeof(sockopt_val)))
926
0
                < 0)
927
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
928
0
                    "calling setsockopt()");
929
#elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
930
            sockopt_val = num ? 1 : 0;
931
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
932
                     (const char *)&sockopt_val,
933
                     sizeof(sockopt_val)))
934
                < 0)
935
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
936
                    "calling setsockopt()");
937
#else
938
            ret = -1;
939
#endif
940
0
            break;
941
0
#if OPENSSL_USE_IPV6
942
0
        case AF_INET6:
943
0
#if defined(IPV6_DONTFRAG)
944
0
            sockopt_val = num ? 1 : 0;
945
0
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
946
0
                     (const void *)&sockopt_val,
947
0
                     sizeof(sockopt_val)))
948
0
                < 0)
949
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
950
0
                    "calling setsockopt()");
951
952
#elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTU_DISCOVER)
953
            sockopt_val = num ? IPV6_PMTUDISC_PROBE : IPV6_PMTUDISC_DONT;
954
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
955
                     &sockopt_val, sizeof(sockopt_val)))
956
                < 0)
957
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
958
                    "calling setsockopt()");
959
#else
960
            ret = -1;
961
#endif
962
0
            break;
963
0
#endif
964
0
        default:
965
0
            ret = -1;
966
0
            break;
967
0
        }
968
0
        break;
969
0
    case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
970
0
        ret = dgram_get_mtu_overhead(&data->peer);
971
0
        break;
972
973
    /*
974
     * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
975
     * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
976
     * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
977
     * value has been updated to a non-clashing value. However to preserve
978
     * binary compatibility we now respond to both the old value and the new one
979
     */
980
0
    case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
981
0
    case BIO_CTRL_DGRAM_SET_PEEK_MODE:
982
0
        data->peekmode = (unsigned int)num;
983
0
        break;
984
985
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP:
986
0
#if defined(SUPPORT_LOCAL_ADDR)
987
0
        ret = 1;
988
#else
989
        ret = 0;
990
#endif
991
0
        break;
992
993
0
    case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE:
994
0
#if defined(SUPPORT_LOCAL_ADDR)
995
0
        num = num > 0;
996
0
        if (num != data->local_addr_enabled) {
997
0
            if (enable_local_addr(b, num) < 1) {
998
0
                ret = 0;
999
0
                break;
1000
0
            }
1001
1002
0
            data->local_addr_enabled = (char)num;
1003
0
        }
1004
#else
1005
        ret = 0;
1006
#endif
1007
0
        break;
1008
1009
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE:
1010
0
        *(int *)ptr = data->local_addr_enabled;
1011
0
        break;
1012
1013
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS:
1014
0
        ret = (long)(BIO_DGRAM_CAP_HANDLES_DST_ADDR
1015
0
            | BIO_DGRAM_CAP_HANDLES_SRC_ADDR
1016
0
            | BIO_DGRAM_CAP_PROVIDES_DST_ADDR
1017
0
            | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR);
1018
0
        break;
1019
1020
0
    case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
1021
0
    case BIO_CTRL_GET_WPOLL_DESCRIPTOR: {
1022
0
        BIO_POLL_DESCRIPTOR *pd = ptr;
1023
1024
0
        pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
1025
0
        pd->value.fd = b->num;
1026
0
    } break;
1027
1028
0
    default:
1029
0
        ret = 0;
1030
0
        break;
1031
0
    }
1032
    /* Normalize if error */
1033
0
    if (ret < 0)
1034
0
        ret = -1;
1035
0
    return ret;
1036
0
}
1037
1038
static int dgram_puts(BIO *bp, const char *str)
1039
0
{
1040
0
    int ret;
1041
0
    size_t n = strlen(str);
1042
1043
0
    if (n > INT_MAX)
1044
0
        return -1;
1045
0
    ret = dgram_write(bp, str, (int)n);
1046
0
    return ret;
1047
0
}
1048
1049
#if M_METHOD == M_METHOD_WSARECVMSG
1050
static void translate_msg_win(BIO *b, WSAMSG *mh, WSABUF *iov,
1051
    unsigned char *control, BIO_MSG *msg)
1052
{
1053
    iov->len = msg->data_len;
1054
    iov->buf = msg->data;
1055
1056
    /* Windows requires namelen to be set exactly */
1057
    mh->name = msg->peer != NULL ? &msg->peer->sa : NULL;
1058
    if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
1059
        mh->namelen = sizeof(struct sockaddr_in);
1060
#if OPENSSL_USE_IPV6
1061
    else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
1062
        mh->namelen = sizeof(struct sockaddr_in6);
1063
#endif
1064
    else
1065
        mh->namelen = 0;
1066
1067
    /*
1068
     * When local address reception (IP_PKTINFO, etc.) is enabled, on Windows
1069
     * this causes WSARecvMsg to fail if the control buffer is too small to hold
1070
     * the structure, or if no control buffer is passed. So we need to give it
1071
     * the control buffer even if we aren't actually going to examine the
1072
     * result.
1073
     */
1074
    mh->lpBuffers = iov;
1075
    mh->dwBufferCount = 1;
1076
    mh->Control.len = BIO_CMSG_ALLOC_LEN;
1077
    mh->Control.buf = control;
1078
    mh->dwFlags = 0;
1079
}
1080
#endif
1081
1082
#if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG
1083
/* Translates a BIO_MSG to a msghdr and iovec. */
1084
static void translate_msg(BIO *b, struct msghdr *mh, struct iovec *iov,
1085
    unsigned char *control, BIO_MSG *msg)
1086
0
{
1087
0
    bio_dgram_data *data;
1088
1089
0
    iov->iov_base = msg->data;
1090
0
    iov->iov_len = msg->data_len;
1091
1092
0
    data = (bio_dgram_data *)b->ptr;
1093
0
    if (data->connected == 0) {
1094
        /* macOS requires msg_namelen be 0 if msg_name is NULL */
1095
0
        mh->msg_name = msg->peer != NULL ? &msg->peer->sa : NULL;
1096
0
        if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
1097
0
            mh->msg_namelen = sizeof(struct sockaddr_in);
1098
0
#if OPENSSL_USE_IPV6
1099
0
        else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
1100
0
            mh->msg_namelen = sizeof(struct sockaddr_in6);
1101
0
#endif
1102
0
        else
1103
0
            mh->msg_namelen = 0;
1104
0
    } else {
1105
0
        mh->msg_name = NULL;
1106
0
        mh->msg_namelen = 0;
1107
0
    }
1108
1109
0
    mh->msg_iov = iov;
1110
0
    mh->msg_iovlen = 1;
1111
0
    mh->msg_control = msg->local != NULL ? control : NULL;
1112
0
    mh->msg_controllen = msg->local != NULL ? BIO_CMSG_ALLOC_LEN : 0;
1113
0
    mh->msg_flags = 0;
1114
0
}
1115
#endif
1116
1117
#if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
1118
/* Extracts destination address from the control buffer. */
1119
static int extract_local(BIO *b, MSGHDR_TYPE *mh, BIO_ADDR *local)
1120
0
{
1121
0
#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1122
0
    CMSGHDR_TYPE *cmsg;
1123
0
    int af = dgram_get_sock_family(b);
1124
1125
0
    for (cmsg = BIO_CMSG_FIRSTHDR(mh); cmsg != NULL;
1126
0
        cmsg = BIO_CMSG_NXTHDR(mh, cmsg)) {
1127
0
        if (af == AF_INET) {
1128
0
            if (cmsg->cmsg_level != IPPROTO_IP)
1129
0
                continue;
1130
1131
0
#if defined(IP_PKTINFO)
1132
0
            if (cmsg->cmsg_type != IP_PKTINFO)
1133
0
                continue;
1134
1135
0
            local->s_in.sin_addr = ((struct in_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi_addr;
1136
1137
#elif defined(IP_RECVDSTADDR)
1138
            if (cmsg->cmsg_type != IP_RECVDSTADDR)
1139
                continue;
1140
1141
            local->s_in.sin_addr = *(struct in_addr *)BIO_CMSG_DATA(cmsg);
1142
#endif
1143
1144
0
#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
1145
0
            {
1146
0
                bio_dgram_data *data = b->ptr;
1147
1148
0
                local->s_in.sin_family = AF_INET;
1149
0
                local->s_in.sin_port = data->local_addr.s_in.sin_port;
1150
0
            }
1151
0
            return 1;
1152
0
#endif
1153
0
        }
1154
0
#if OPENSSL_USE_IPV6
1155
0
        else if (af == AF_INET6) {
1156
0
            if (cmsg->cmsg_level != IPPROTO_IPV6)
1157
0
                continue;
1158
1159
0
#if defined(IPV6_RECVPKTINFO)
1160
0
            if (cmsg->cmsg_type != IPV6_PKTINFO)
1161
0
                continue;
1162
1163
0
            {
1164
0
                bio_dgram_data *data = b->ptr;
1165
1166
0
                local->s_in6.sin6_addr = ((struct in6_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi6_addr;
1167
0
                local->s_in6.sin6_family = AF_INET6;
1168
0
                local->s_in6.sin6_port = data->local_addr.s_in6.sin6_port;
1169
0
                local->s_in6.sin6_scope_id = data->local_addr.s_in6.sin6_scope_id;
1170
0
                local->s_in6.sin6_flowinfo = 0;
1171
0
            }
1172
0
            return 1;
1173
0
#endif
1174
0
        }
1175
0
#endif
1176
0
    }
1177
0
#endif
1178
1179
0
    return 0;
1180
0
}
1181
1182
static int pack_local(BIO *b, MSGHDR_TYPE *mh, const BIO_ADDR *local)
1183
0
{
1184
0
    int af = dgram_get_sock_family(b);
1185
0
#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1186
0
    CMSGHDR_TYPE *cmsg;
1187
0
    bio_dgram_data *data = b->ptr;
1188
0
#endif
1189
1190
0
    if (af == AF_INET) {
1191
0
#if defined(IP_PKTINFO)
1192
0
        struct in_pktinfo *info;
1193
1194
#if defined(OPENSSL_SYS_WINDOWS)
1195
        cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
1196
#else
1197
0
        cmsg = (CMSGHDR_TYPE *)mh->msg_control;
1198
0
#endif
1199
1200
0
        cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in_pktinfo));
1201
0
        cmsg->cmsg_level = IPPROTO_IP;
1202
0
        cmsg->cmsg_type = IP_PKTINFO;
1203
1204
0
        info = (struct in_pktinfo *)BIO_CMSG_DATA(cmsg);
1205
0
#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_CYGWIN) && !defined(__FreeBSD__) && !defined(__QNX__)
1206
0
        info->ipi_spec_dst = local->s_in.sin_addr;
1207
0
#endif
1208
0
        info->ipi_addr.s_addr = 0;
1209
0
        info->ipi_ifindex = 0;
1210
1211
        /*
1212
         * We cannot override source port using this API, therefore
1213
         * ensure the application specified a source port of 0
1214
         * or the one we are bound to. (Better to error than silently
1215
         * ignore this.)
1216
         */
1217
0
        if (local->s_in.sin_port != 0
1218
0
            && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
1219
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1220
0
            return 0;
1221
0
        }
1222
1223
#if defined(OPENSSL_SYS_WINDOWS)
1224
        mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
1225
#else
1226
0
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
1227
0
#endif
1228
0
        return 1;
1229
1230
#elif defined(IP_SENDSRCADDR)
1231
        struct in_addr *info;
1232
1233
        /*
1234
         * At least FreeBSD is very pedantic about using IP_SENDSRCADDR when we
1235
         * are not bound to 0.0.0.0 or ::, even if the address matches what we
1236
         * bound to. Support this by not packing the structure if the address
1237
         * matches our understanding of our local address. IP_SENDSRCADDR is a
1238
         * BSD thing, so we don't need an explicit test for BSD here.
1239
         */
1240
        if (local->s_in.sin_addr.s_addr == data->local_addr.s_in.sin_addr.s_addr) {
1241
            mh->msg_control = NULL;
1242
            mh->msg_controllen = 0;
1243
            return 1;
1244
        }
1245
1246
        cmsg = (struct cmsghdr *)mh->msg_control;
1247
        cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in_addr));
1248
        cmsg->cmsg_level = IPPROTO_IP;
1249
        cmsg->cmsg_type = IP_SENDSRCADDR;
1250
1251
        info = (struct in_addr *)BIO_CMSG_DATA(cmsg);
1252
        *info = local->s_in.sin_addr;
1253
1254
        /* See comment above. */
1255
        if (local->s_in.sin_port != 0
1256
            && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
1257
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1258
            return 0;
1259
        }
1260
1261
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_addr));
1262
        return 1;
1263
#endif
1264
0
    }
1265
0
#if OPENSSL_USE_IPV6
1266
0
    else if (af == AF_INET6) {
1267
0
#if defined(IPV6_PKTINFO)
1268
0
        struct in6_pktinfo *info;
1269
1270
#if defined(OPENSSL_SYS_WINDOWS)
1271
        cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
1272
#else
1273
0
        cmsg = (CMSGHDR_TYPE *)mh->msg_control;
1274
0
#endif
1275
0
        cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in6_pktinfo));
1276
0
        cmsg->cmsg_level = IPPROTO_IPV6;
1277
0
        cmsg->cmsg_type = IPV6_PKTINFO;
1278
1279
0
        info = (struct in6_pktinfo *)BIO_CMSG_DATA(cmsg);
1280
0
        info->ipi6_addr = local->s_in6.sin6_addr;
1281
0
        info->ipi6_ifindex = 0;
1282
1283
        /*
1284
         * See comment above, but also applies to the other fields
1285
         * in sockaddr_in6.
1286
         */
1287
0
        if (local->s_in6.sin6_port != 0
1288
0
            && data->local_addr.s_in6.sin6_port != local->s_in6.sin6_port) {
1289
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1290
0
            return 0;
1291
0
        }
1292
1293
0
        if (local->s_in6.sin6_scope_id != 0
1294
0
            && data->local_addr.s_in6.sin6_scope_id != local->s_in6.sin6_scope_id) {
1295
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1296
0
            return 0;
1297
0
        }
1298
1299
#if defined(OPENSSL_SYS_WINDOWS)
1300
        mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
1301
#else
1302
0
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
1303
0
#endif
1304
0
        return 1;
1305
0
#endif
1306
0
    }
1307
0
#endif
1308
1309
0
    return 0;
1310
0
}
1311
#endif
1312
1313
/*
1314
 * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
1315
 * should mask out any system flags returned by this function you cannot support
1316
 * in a particular circumstance. Currently no flags are defined.
1317
 */
1318
#if M_METHOD != M_METHOD_NONE
1319
static int translate_flags(uint64_t flags)
1320
0
{
1321
0
    return 0;
1322
0
}
1323
#endif
1324
1325
static int dgram_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
1326
    size_t num_msg, uint64_t flags, size_t *num_processed)
1327
0
{
1328
0
#if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1329
0
    int ret;
1330
0
#endif
1331
0
#if M_METHOD == M_METHOD_RECVMMSG
1332
0
#define BIO_MAX_MSGS_PER_CALL 64
1333
0
    int sysflags;
1334
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1335
0
    size_t i;
1336
0
    struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
1337
0
    struct iovec iov[BIO_MAX_MSGS_PER_CALL];
1338
0
    unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
1339
0
    int have_local_enabled = data->local_addr_enabled;
1340
#elif M_METHOD == M_METHOD_RECVMSG
1341
    int sysflags;
1342
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1343
    ossl_ssize_t l;
1344
    struct msghdr mh;
1345
    struct iovec iov;
1346
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1347
    int have_local_enabled = data->local_addr_enabled;
1348
#elif M_METHOD == M_METHOD_WSARECVMSG
1349
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1350
    int have_local_enabled = data->local_addr_enabled;
1351
    WSAMSG wmsg;
1352
    WSABUF wbuf;
1353
    DWORD num_bytes_sent = 0;
1354
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1355
#endif
1356
#if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1357
    int sysflags;
1358
#endif
1359
1360
0
    if (num_msg == 0) {
1361
0
        *num_processed = 0;
1362
0
        return 1;
1363
0
    }
1364
1365
0
    if (num_msg > OSSL_SSIZE_MAX)
1366
0
        num_msg = OSSL_SSIZE_MAX;
1367
1368
0
#if M_METHOD != M_METHOD_NONE
1369
0
    sysflags = translate_flags(flags);
1370
0
#endif
1371
1372
0
#if M_METHOD == M_METHOD_RECVMMSG
1373
    /*
1374
     * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1375
     * msghdr and struct iovec on the stack to support multithreaded use. Thus
1376
     * we place a fixed limit on the number of messages per call, in the
1377
     * expectation that we will be called again if there were more messages to
1378
     * be sent.
1379
     */
1380
0
    if (num_msg > BIO_MAX_MSGS_PER_CALL)
1381
0
        num_msg = BIO_MAX_MSGS_PER_CALL;
1382
1383
0
    for (i = 0; i < num_msg; ++i) {
1384
0
        translate_msg(b, &mh[i].msg_hdr, &iov[i],
1385
0
            control[i], &BIO_MSG_N(msg, stride, i));
1386
1387
        /* If local address was requested, it must have been enabled */
1388
0
        if (BIO_MSG_N(msg, stride, i).local != NULL) {
1389
0
            if (!have_local_enabled) {
1390
0
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1391
0
                *num_processed = 0;
1392
0
                return 0;
1393
0
            }
1394
1395
0
            if (pack_local(b, &mh[i].msg_hdr,
1396
0
                    BIO_MSG_N(msg, stride, i).local)
1397
0
                < 1) {
1398
0
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1399
0
                *num_processed = 0;
1400
0
                return 0;
1401
0
            }
1402
0
        }
1403
0
    }
1404
1405
    /* Do the batch */
1406
0
    ret = sendmmsg(b->num, mh, num_msg, sysflags);
1407
0
    if (ret < 0) {
1408
0
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1409
0
        *num_processed = 0;
1410
0
        return 0;
1411
0
    }
1412
1413
0
    for (i = 0; i < (size_t)ret; ++i) {
1414
0
        BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
1415
0
        BIO_MSG_N(msg, stride, i).flags = 0;
1416
0
    }
1417
1418
0
    *num_processed = (size_t)ret;
1419
0
    return 1;
1420
1421
#elif M_METHOD == M_METHOD_RECVMSG
1422
    /*
1423
     * If sendmsg is available, use it.
1424
     */
1425
    translate_msg(b, &mh, &iov, control, msg);
1426
1427
    if (msg->local != NULL) {
1428
        if (!have_local_enabled) {
1429
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1430
            *num_processed = 0;
1431
            return 0;
1432
        }
1433
1434
        if (pack_local(b, &mh, msg->local) < 1) {
1435
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1436
            *num_processed = 0;
1437
            return 0;
1438
        }
1439
    }
1440
1441
    l = sendmsg(b->num, &mh, sysflags);
1442
    if (l < 0) {
1443
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1444
        *num_processed = 0;
1445
        return 0;
1446
    }
1447
1448
    msg->data_len = (size_t)l;
1449
    msg->flags = 0;
1450
    *num_processed = 1;
1451
    return 1;
1452
1453
#elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
1454
#if M_METHOD == M_METHOD_WSARECVMSG
1455
    if (bio_WSASendMsg != NULL) {
1456
        /* WSASendMsg-based implementation for Windows. */
1457
        translate_msg_win(b, &wmsg, &wbuf, control, msg);
1458
1459
        if (msg[0].local != NULL) {
1460
            if (!have_local_enabled) {
1461
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1462
                *num_processed = 0;
1463
                return 0;
1464
            }
1465
1466
            if (pack_local(b, &wmsg, msg[0].local) < 1) {
1467
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1468
                *num_processed = 0;
1469
                return 0;
1470
            }
1471
        }
1472
1473
        ret = WSASendMsg((SOCKET)b->num, &wmsg, 0, &num_bytes_sent, NULL, NULL);
1474
        if (ret < 0) {
1475
            ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1476
            *num_processed = 0;
1477
            return 0;
1478
        }
1479
1480
        msg[0].data_len = num_bytes_sent;
1481
        msg[0].flags = 0;
1482
        *num_processed = 1;
1483
        return 1;
1484
    }
1485
#endif
1486
1487
    /*
1488
     * Fallback to sendto and send a single message.
1489
     */
1490
    if (msg[0].local != NULL) {
1491
        /*
1492
         * We cannot set the local address if using sendto
1493
         * so fail in this case
1494
         */
1495
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1496
        *num_processed = 0;
1497
        return 0;
1498
    }
1499
1500
    ret = sendto(b->num, msg[0].data,
1501
#if defined(OPENSSL_SYS_WINDOWS)
1502
        (int)msg[0].data_len,
1503
#else
1504
        msg[0].data_len,
1505
#endif
1506
        sysflags,
1507
        msg[0].peer != NULL ? BIO_ADDR_sockaddr(msg[0].peer) : NULL,
1508
        msg[0].peer != NULL ? BIO_ADDR_sockaddr_size(msg[0].peer) : 0);
1509
    if (ret <= 0) {
1510
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1511
        *num_processed = 0;
1512
        return 0;
1513
    }
1514
1515
    msg[0].data_len = ret;
1516
    msg[0].flags = 0;
1517
    *num_processed = 1;
1518
    return 1;
1519
1520
#else
1521
    ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
1522
    *num_processed = 0;
1523
    return 0;
1524
#endif
1525
0
}
1526
1527
static int dgram_recvmmsg(BIO *b, BIO_MSG *msg,
1528
    size_t stride, size_t num_msg,
1529
    uint64_t flags, size_t *num_processed)
1530
0
{
1531
0
#if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1532
0
    int ret;
1533
0
#endif
1534
0
#if M_METHOD == M_METHOD_RECVMMSG
1535
0
    int sysflags;
1536
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1537
0
    size_t i;
1538
0
    struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
1539
0
    struct iovec iov[BIO_MAX_MSGS_PER_CALL];
1540
0
    unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
1541
0
    int have_local_enabled = data->local_addr_enabled;
1542
#elif M_METHOD == M_METHOD_RECVMSG
1543
    int sysflags;
1544
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1545
    ossl_ssize_t l;
1546
    struct msghdr mh;
1547
    struct iovec iov;
1548
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1549
    int have_local_enabled = data->local_addr_enabled;
1550
#elif M_METHOD == M_METHOD_WSARECVMSG
1551
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1552
    int have_local_enabled = data->local_addr_enabled;
1553
    WSAMSG wmsg;
1554
    WSABUF wbuf;
1555
    DWORD num_bytes_received = 0;
1556
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1557
#endif
1558
#if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1559
    int sysflags;
1560
    socklen_t slen;
1561
#endif
1562
1563
0
    if (num_msg == 0) {
1564
0
        *num_processed = 0;
1565
0
        return 1;
1566
0
    }
1567
1568
0
    if (num_msg > OSSL_SSIZE_MAX)
1569
0
        num_msg = OSSL_SSIZE_MAX;
1570
1571
0
#if M_METHOD != M_METHOD_NONE
1572
0
    sysflags = translate_flags(flags);
1573
0
#endif
1574
1575
0
#if M_METHOD == M_METHOD_RECVMMSG
1576
    /*
1577
     * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1578
     * msghdr and struct iovec on the stack to support multithreaded use. Thus
1579
     * we place a fixed limit on the number of messages per call, in the
1580
     * expectation that we will be called again if there were more messages to
1581
     * be sent.
1582
     */
1583
0
    if (num_msg > BIO_MAX_MSGS_PER_CALL)
1584
0
        num_msg = BIO_MAX_MSGS_PER_CALL;
1585
1586
0
    for (i = 0; i < num_msg; ++i) {
1587
0
        translate_msg(b, &mh[i].msg_hdr, &iov[i],
1588
0
            control[i], &BIO_MSG_N(msg, stride, i));
1589
1590
        /* If local address was requested, it must have been enabled */
1591
0
        if (BIO_MSG_N(msg, stride, i).local != NULL && !have_local_enabled) {
1592
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1593
0
            *num_processed = 0;
1594
0
            return 0;
1595
0
        }
1596
0
    }
1597
1598
    /* Do the batch */
1599
0
    ret = recvmmsg(b->num, mh, num_msg, sysflags, NULL);
1600
0
    if (ret < 0) {
1601
0
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1602
0
        *num_processed = 0;
1603
0
        return 0;
1604
0
    }
1605
1606
0
    for (i = 0; i < (size_t)ret; ++i) {
1607
0
        BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
1608
0
        BIO_MSG_N(msg, stride, i).flags = 0;
1609
        /*
1610
         * *(msg->peer) will have been filled in by recvmmsg;
1611
         * for msg->local we parse the control data returned
1612
         */
1613
0
        if (BIO_MSG_N(msg, stride, i).local != NULL)
1614
0
            if (extract_local(b, &mh[i].msg_hdr,
1615
0
                    BIO_MSG_N(msg, stride, i).local)
1616
0
                < 1)
1617
                /*
1618
                 * It appears BSDs do not support local addresses for
1619
                 * loopback sockets. In this case, just clear the local
1620
                 * address, as for OS X and Windows in some circumstances
1621
                 * (see below).
1622
                 */
1623
0
                BIO_ADDR_clear(BIO_MSG_N(msg, stride, i).local);
1624
0
    }
1625
1626
0
    *num_processed = (size_t)ret;
1627
0
    return 1;
1628
1629
#elif M_METHOD == M_METHOD_RECVMSG
1630
    /*
1631
     * If recvmsg is available, use it.
1632
     */
1633
    translate_msg(b, &mh, &iov, control, msg);
1634
1635
    /* If local address was requested, it must have been enabled */
1636
    if (msg->local != NULL && !have_local_enabled) {
1637
        /*
1638
         * If we have done at least one message, we must return the
1639
         * count; if we haven't done any, we can give an error code
1640
         */
1641
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1642
        *num_processed = 0;
1643
        return 0;
1644
    }
1645
1646
    l = recvmsg(b->num, &mh, sysflags);
1647
    if (l < 0) {
1648
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1649
        *num_processed = 0;
1650
        return 0;
1651
    }
1652
1653
    msg->data_len = (size_t)l;
1654
    msg->flags = 0;
1655
1656
    if (msg->local != NULL)
1657
        if (extract_local(b, &mh, msg->local) < 1)
1658
            /*
1659
             * OS X exhibits odd behaviour where it appears that if a packet is
1660
             * sent before the receiving interface enables IP_PKTINFO, it will
1661
             * sometimes not have any control data returned even if the
1662
             * receiving interface enables IP_PKTINFO before calling recvmsg().
1663
             * This appears to occur non-deterministically. Presumably, OS X
1664
             * handles IP_PKTINFO at the time the packet is enqueued into a
1665
             * socket's receive queue, rather than at the time recvmsg() is
1666
             * called, unlike most other operating systems. Thus (if this
1667
             * hypothesis is correct) there is a race between where IP_PKTINFO
1668
             * is enabled by the process and when the kernel's network stack
1669
             * queues the incoming message.
1670
             *
1671
             * We cannot return the local address if we do not have it, but this
1672
             * is not a caller error either, so just return a zero address
1673
             * structure. This is similar to how we handle Windows loopback
1674
             * interfaces (see below). We enable this workaround for all
1675
             * platforms, not just Apple, as this kind of quirk in OS networking
1676
             * stacks seems to be common enough that failing hard if a local
1677
             * address is not provided appears to be too brittle.
1678
             */
1679
            BIO_ADDR_clear(msg->local);
1680
1681
    *num_processed = 1;
1682
    return 1;
1683
1684
#elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1685
#if M_METHOD == M_METHOD_WSARECVMSG
1686
    if (bio_WSARecvMsg != NULL) {
1687
        /* WSARecvMsg-based implementation for Windows. */
1688
        translate_msg_win(b, &wmsg, &wbuf, control, msg);
1689
1690
        /* If local address was requested, it must have been enabled */
1691
        if (msg[0].local != NULL && !have_local_enabled) {
1692
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1693
            *num_processed = 0;
1694
            return 0;
1695
        }
1696
1697
        ret = WSARecvMsg((SOCKET)b->num, &wmsg, &num_bytes_received, NULL, NULL);
1698
        if (ret < 0) {
1699
            ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1700
            *num_processed = 0;
1701
            return 0;
1702
        }
1703
1704
        msg[0].data_len = num_bytes_received;
1705
        msg[0].flags = 0;
1706
        if (msg[0].local != NULL)
1707
            if (extract_local(b, &wmsg, msg[0].local) < 1)
1708
                /*
1709
                 * On Windows, loopback is not a "proper" interface and it works
1710
                 * differently; packets are essentially short-circuited and
1711
                 * don't go through all of the normal processing. A consequence
1712
                 * of this is that packets sent from the local machine to the
1713
                 * local machine _will not have IP_PKTINFO_ even if the
1714
                 * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
1715
                 * Control.len to 0 on returning.
1716
                 *
1717
                 * This applies regardless of whether the loopback address,
1718
                 * 127.0.0.1 is used, or a local interface address (e.g.
1719
                 * 192.168.1.1); in both cases IP_PKTINFO will not be present.
1720
                 *
1721
                 * We report this condition by setting the local BIO_ADDR's
1722
                 * family to 0.
1723
                 */
1724
                BIO_ADDR_clear(msg[0].local);
1725
1726
        *num_processed = 1;
1727
        return 1;
1728
    }
1729
#endif
1730
1731
    /*
1732
     * Fallback to recvfrom and receive a single message.
1733
     */
1734
    if (msg[0].local != NULL) {
1735
        /*
1736
         * We cannot determine the local address if using recvfrom
1737
         * so fail in this case
1738
         */
1739
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1740
        *num_processed = 0;
1741
        return 0;
1742
    }
1743
1744
    slen = sizeof(*msg[0].peer);
1745
    ret = recvfrom(b->num, msg[0].data,
1746
#if defined(OPENSSL_SYS_WINDOWS)
1747
        (int)msg[0].data_len,
1748
#else
1749
        msg[0].data_len,
1750
#endif
1751
        sysflags,
1752
        msg[0].peer != NULL ? &msg[0].peer->sa : NULL,
1753
        msg[0].peer != NULL ? &slen : NULL);
1754
    if (ret <= 0) {
1755
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1756
        *num_processed = 0;
1757
        return 0;
1758
    }
1759
1760
    msg[0].data_len = ret;
1761
    msg[0].flags = 0;
1762
    *num_processed = 1;
1763
    return 1;
1764
1765
#else
1766
    ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
1767
    *num_processed = 0;
1768
    return 0;
1769
#endif
1770
0
}
1771
1772
#ifndef OPENSSL_NO_SCTP
1773
const BIO_METHOD *BIO_s_datagram_sctp(void)
1774
{
1775
    return &methods_dgramp_sctp;
1776
}
1777
1778
BIO *BIO_new_dgram_sctp(int fd, int close_flag)
1779
{
1780
    BIO *bio;
1781
    int ret, optval = 20000;
1782
    int auth_data = 0, auth_forward = 0;
1783
    unsigned char *p;
1784
    struct sctp_authchunk auth;
1785
    struct sctp_authchunks *authchunks;
1786
    socklen_t sockopt_len;
1787
#ifdef SCTP_AUTHENTICATION_EVENT
1788
#ifdef SCTP_EVENT
1789
    struct sctp_event event;
1790
#else
1791
    struct sctp_event_subscribe event;
1792
#endif
1793
#endif
1794
1795
    bio = BIO_new(BIO_s_datagram_sctp());
1796
    if (bio == NULL)
1797
        return NULL;
1798
    BIO_set_fd(bio, fd, close_flag);
1799
1800
    /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1801
    auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
1802
    ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1803
        sizeof(struct sctp_authchunk));
1804
    if (ret < 0) {
1805
        BIO_vfree(bio);
1806
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1807
            "Ensure SCTP AUTH chunks are enabled in kernel");
1808
        return NULL;
1809
    }
1810
    auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
1811
    ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1812
        sizeof(struct sctp_authchunk));
1813
    if (ret < 0) {
1814
        BIO_vfree(bio);
1815
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1816
            "Ensure SCTP AUTH chunks are enabled in kernel");
1817
        return NULL;
1818
    }
1819
1820
    /*
1821
     * Test if activation was successful. When using accept(), SCTP-AUTH has
1822
     * to be activated for the listening socket already, otherwise the
1823
     * connected socket won't use it. Similarly with connect(): the socket
1824
     * prior to connection must be activated for SCTP-AUTH
1825
     */
1826
    sockopt_len = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1827
    authchunks = OPENSSL_zalloc(sockopt_len);
1828
    if (authchunks == NULL) {
1829
        BIO_vfree(bio);
1830
        return NULL;
1831
    }
1832
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1833
        &sockopt_len);
1834
    if (ret < 0) {
1835
        OPENSSL_free(authchunks);
1836
        BIO_vfree(bio);
1837
        return NULL;
1838
    }
1839
1840
    for (p = (unsigned char *)authchunks->gauth_chunks;
1841
        p < (unsigned char *)authchunks + sockopt_len;
1842
        p += sizeof(uint8_t)) {
1843
        if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1844
            auth_data = 1;
1845
        if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1846
            auth_forward = 1;
1847
    }
1848
1849
    OPENSSL_free(authchunks);
1850
1851
    if (!auth_data || !auth_forward) {
1852
        BIO_vfree(bio);
1853
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1854
            "Ensure SCTP AUTH chunks are enabled on the "
1855
            "underlying socket");
1856
        return NULL;
1857
    }
1858
1859
#ifdef SCTP_AUTHENTICATION_EVENT
1860
#ifdef SCTP_EVENT
1861
    memset(&event, 0, sizeof(event));
1862
    event.se_assoc_id = 0;
1863
    event.se_type = SCTP_AUTHENTICATION_EVENT;
1864
    event.se_on = 1;
1865
    ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1866
        sizeof(struct sctp_event));
1867
    if (ret < 0) {
1868
        BIO_vfree(bio);
1869
        return NULL;
1870
    }
1871
#else
1872
    sockopt_len = (socklen_t)sizeof(struct sctp_event_subscribe);
1873
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1874
    if (ret < 0) {
1875
        BIO_vfree(bio);
1876
        return NULL;
1877
    }
1878
1879
    event.sctp_authentication_event = 1;
1880
1881
    ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1882
        sizeof(struct sctp_event_subscribe));
1883
    if (ret < 0) {
1884
        BIO_vfree(bio);
1885
        return NULL;
1886
    }
1887
#endif
1888
#endif
1889
1890
    /*
1891
     * Disable partial delivery by setting the min size larger than the max
1892
     * record size of 2^14 + 2048 + 13
1893
     */
1894
    ret = setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1895
        sizeof(optval));
1896
    if (ret < 0) {
1897
        BIO_vfree(bio);
1898
        return NULL;
1899
    }
1900
1901
    return bio;
1902
}
1903
1904
int BIO_dgram_is_sctp(BIO *bio)
1905
{
1906
    return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1907
}
1908
1909
static int dgram_sctp_new(BIO *bi)
1910
{
1911
    bio_dgram_sctp_data *data = NULL;
1912
1913
    bi->init = 0;
1914
    bi->num = 0;
1915
    if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
1916
        return 0;
1917
#ifdef SCTP_PR_SCTP_NONE
1918
    data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1919
#endif
1920
    bi->ptr = data;
1921
1922
    bi->flags = 0;
1923
    return 1;
1924
}
1925
1926
static int dgram_sctp_free(BIO *a)
1927
{
1928
    bio_dgram_sctp_data *data;
1929
1930
    if (a == NULL)
1931
        return 0;
1932
    if (!dgram_clear(a))
1933
        return 0;
1934
1935
    data = (bio_dgram_sctp_data *)a->ptr;
1936
    if (data != NULL)
1937
        OPENSSL_free(data);
1938
1939
    return 1;
1940
}
1941
1942
#ifdef SCTP_AUTHENTICATION_EVENT
1943
void dgram_sctp_handle_auth_free_key_event(BIO *b,
1944
    union sctp_notification *snp)
1945
{
1946
    int ret;
1947
    struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1948
1949
    if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1950
        struct sctp_authkeyid authkeyid;
1951
1952
        /* delete key */
1953
        authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1954
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1955
            &authkeyid, sizeof(struct sctp_authkeyid));
1956
    }
1957
}
1958
#endif
1959
1960
static int dgram_sctp_read(BIO *b, char *out, int outl)
1961
{
1962
    int ret = 0, n = 0, i, optval;
1963
    socklen_t optlen;
1964
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1965
    struct msghdr msg;
1966
    struct iovec iov;
1967
    struct cmsghdr *cmsg;
1968
    char cmsgbuf[512];
1969
1970
    if (out != NULL) {
1971
        clear_socket_error();
1972
1973
        do {
1974
            memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1975
            iov.iov_base = out;
1976
            iov.iov_len = outl;
1977
            msg.msg_name = NULL;
1978
            msg.msg_namelen = 0;
1979
            msg.msg_iov = &iov;
1980
            msg.msg_iovlen = 1;
1981
            msg.msg_control = cmsgbuf;
1982
            msg.msg_controllen = 512;
1983
            msg.msg_flags = 0;
1984
            n = recvmsg(b->num, &msg, 0);
1985
1986
            if (n <= 0) {
1987
                if (n < 0)
1988
                    ret = n;
1989
                break;
1990
            }
1991
1992
            if (msg.msg_controllen > 0) {
1993
                for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1994
                    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1995
                    if (cmsg->cmsg_level != IPPROTO_SCTP)
1996
                        continue;
1997
#ifdef SCTP_RCVINFO
1998
                    if (cmsg->cmsg_type == SCTP_RCVINFO) {
1999
                        struct sctp_rcvinfo *rcvinfo;
2000
2001
                        rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
2002
                        data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
2003
                        data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
2004
                        data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
2005
                        data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
2006
                        data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
2007
                        data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
2008
                        data->rcvinfo.rcv_context = rcvinfo->rcv_context;
2009
                    }
2010
#endif
2011
#ifdef SCTP_SNDRCV
2012
                    if (cmsg->cmsg_type == SCTP_SNDRCV) {
2013
                        struct sctp_sndrcvinfo *sndrcvinfo;
2014
2015
                        sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2016
                        data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
2017
                        data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
2018
                        data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
2019
                        data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
2020
                        data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
2021
                        data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
2022
                        data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
2023
                    }
2024
#endif
2025
                }
2026
            }
2027
2028
            if (msg.msg_flags & MSG_NOTIFICATION) {
2029
                union sctp_notification snp;
2030
2031
                if (n < (int)sizeof(snp.sn_header))
2032
                    return -1;
2033
                memset(&snp, 0, sizeof(snp));
2034
                memcpy(&snp, out, (size_t)n < sizeof(snp) ? (size_t)n : sizeof(snp));
2035
                if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2036
#ifdef SCTP_EVENT
2037
                    struct sctp_event event;
2038
#else
2039
                    struct sctp_event_subscribe event;
2040
                    socklen_t eventsize;
2041
#endif
2042
2043
                    /* disable sender dry event */
2044
#ifdef SCTP_EVENT
2045
                    memset(&event, 0, sizeof(event));
2046
                    event.se_assoc_id = 0;
2047
                    event.se_type = SCTP_SENDER_DRY_EVENT;
2048
                    event.se_on = 0;
2049
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2050
                        sizeof(struct sctp_event));
2051
                    if (i < 0) {
2052
                        ret = i;
2053
                        break;
2054
                    }
2055
#else
2056
                    eventsize = sizeof(struct sctp_event_subscribe);
2057
                    i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2058
                        &eventsize);
2059
                    if (i < 0) {
2060
                        ret = i;
2061
                        break;
2062
                    }
2063
2064
                    event.sctp_sender_dry_event = 0;
2065
2066
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2067
                        sizeof(struct sctp_event_subscribe));
2068
                    if (i < 0) {
2069
                        ret = i;
2070
                        break;
2071
                    }
2072
#endif
2073
                }
2074
#ifdef SCTP_AUTHENTICATION_EVENT
2075
                if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2076
                    dgram_sctp_handle_auth_free_key_event(b, &snp);
2077
#endif
2078
2079
                if (data->handle_notifications != NULL)
2080
                    data->handle_notifications(b, data->notification_context,
2081
                        (void *)out);
2082
2083
                memset(out, 0, outl);
2084
            } else {
2085
                ret += n;
2086
            }
2087
        } while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
2088
            && (ret < outl));
2089
2090
        if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
2091
            /* Partial message read, this should never happen! */
2092
2093
            /*
2094
             * The buffer was too small, this means the peer sent a message
2095
             * that was larger than allowed.
2096
             */
2097
            if (ret == outl)
2098
                return -1;
2099
2100
            /*
2101
             * Test if socket buffer can handle max record size (2^14 + 2048
2102
             * + 13)
2103
             */
2104
            optlen = (socklen_t)sizeof(int);
2105
            ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
2106
            if (ret >= 0 && !ossl_assert(optval >= 18445))
2107
                return -1;
2108
2109
            /*
2110
             * Test if SCTP doesn't partially deliver below max record size
2111
             * (2^14 + 2048 + 13)
2112
             */
2113
            optlen = (socklen_t)sizeof(int);
2114
            ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
2115
                &optval, &optlen);
2116
            if (ret >= 0 && !ossl_assert(optval >= 18445))
2117
                return -1;
2118
2119
            /*
2120
             * Partially delivered notification??? Probably a bug....
2121
             */
2122
            if (!ossl_assert((msg.msg_flags & MSG_NOTIFICATION) == 0))
2123
                return -1;
2124
2125
            /*
2126
             * Everything seems ok till now, so it's most likely a message
2127
             * dropped by PR-SCTP.
2128
             */
2129
            memset(out, 0, outl);
2130
            BIO_set_retry_read(b);
2131
            return -1;
2132
        }
2133
2134
        BIO_clear_retry_flags(b);
2135
        if (ret < 0) {
2136
            if (BIO_dgram_should_retry(ret)) {
2137
                BIO_set_retry_read(b);
2138
                data->dgram._errno = get_last_socket_error();
2139
            }
2140
        }
2141
2142
        /* Test if peer uses SCTP-AUTH before continuing */
2143
        if (!data->peer_auth_tested) {
2144
            int ii, auth_data = 0, auth_forward = 0;
2145
            unsigned char *p;
2146
            struct sctp_authchunks *authchunks;
2147
2148
            optlen = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
2149
            authchunks = OPENSSL_malloc(optlen);
2150
            if (authchunks == NULL)
2151
                return -1;
2152
            memset(authchunks, 0, optlen);
2153
            ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
2154
                authchunks, &optlen);
2155
2156
            if (ii >= 0)
2157
                for (p = (unsigned char *)authchunks->gauth_chunks;
2158
                    p < (unsigned char *)authchunks + optlen;
2159
                    p += sizeof(uint8_t)) {
2160
                    if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
2161
                        auth_data = 1;
2162
                    if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
2163
                        auth_forward = 1;
2164
                }
2165
2166
            OPENSSL_free(authchunks);
2167
2168
            if (!auth_data || !auth_forward) {
2169
                ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
2170
                return -1;
2171
            }
2172
2173
            data->peer_auth_tested = 1;
2174
        }
2175
    }
2176
    return ret;
2177
}
2178
2179
/*
2180
 * dgram_sctp_write - send message on SCTP socket
2181
 * @b: BIO to write to
2182
 * @in: data to send
2183
 * @inl: amount of bytes in @in to send
2184
 *
2185
 * Returns -1 on error or the sent amount of bytes on success
2186
 */
2187
static int dgram_sctp_write(BIO *b, const char *in, int inl)
2188
{
2189
    int ret;
2190
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
2191
    struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
2192
    struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
2193
    struct bio_dgram_sctp_sndinfo handshake_sinfo;
2194
    struct iovec iov[1];
2195
    struct msghdr msg;
2196
    struct cmsghdr *cmsg;
2197
#if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2198
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) + CMSG_SPACE(sizeof(struct sctp_prinfo))];
2199
    struct sctp_sndinfo *sndinfo;
2200
    struct sctp_prinfo *prinfo;
2201
#else
2202
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
2203
    struct sctp_sndrcvinfo *sndrcvinfo;
2204
#endif
2205
2206
    clear_socket_error();
2207
2208
    /*
2209
     * If we're send anything else than application data, disable all user
2210
     * parameters and flags.
2211
     */
2212
    if (in[0] != 23) {
2213
        memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
2214
#ifdef SCTP_SACK_IMMEDIATELY
2215
        handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
2216
#endif
2217
        sinfo = &handshake_sinfo;
2218
    }
2219
2220
    /* We can only send a shutdown alert if the socket is dry */
2221
    if (data->save_shutdown) {
2222
        ret = BIO_dgram_sctp_wait_for_dry(b);
2223
        if (ret < 0)
2224
            return -1;
2225
        if (ret == 0) {
2226
            BIO_clear_retry_flags(b);
2227
            BIO_set_retry_write(b);
2228
            return -1;
2229
        }
2230
    }
2231
2232
    iov[0].iov_base = (char *)in;
2233
    iov[0].iov_len = inl;
2234
    msg.msg_name = NULL;
2235
    msg.msg_namelen = 0;
2236
    msg.msg_iov = iov;
2237
    msg.msg_iovlen = 1;
2238
    msg.msg_control = (caddr_t)cmsgbuf;
2239
    msg.msg_controllen = 0;
2240
    msg.msg_flags = 0;
2241
#if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2242
    cmsg = (struct cmsghdr *)cmsgbuf;
2243
    cmsg->cmsg_level = IPPROTO_SCTP;
2244
    cmsg->cmsg_type = SCTP_SNDINFO;
2245
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
2246
    sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
2247
    memset(sndinfo, 0, sizeof(*sndinfo));
2248
    sndinfo->snd_sid = sinfo->snd_sid;
2249
    sndinfo->snd_flags = sinfo->snd_flags;
2250
    sndinfo->snd_ppid = sinfo->snd_ppid;
2251
    sndinfo->snd_context = sinfo->snd_context;
2252
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
2253
2254
    cmsg = (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
2255
    cmsg->cmsg_level = IPPROTO_SCTP;
2256
    cmsg->cmsg_type = SCTP_PRINFO;
2257
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
2258
    prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
2259
    memset(prinfo, 0, sizeof(*prinfo));
2260
    prinfo->pr_policy = pinfo->pr_policy;
2261
    prinfo->pr_value = pinfo->pr_value;
2262
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
2263
#else
2264
    cmsg = (struct cmsghdr *)cmsgbuf;
2265
    cmsg->cmsg_level = IPPROTO_SCTP;
2266
    cmsg->cmsg_type = SCTP_SNDRCV;
2267
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
2268
    sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2269
    memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
2270
    sndrcvinfo->sinfo_stream = sinfo->snd_sid;
2271
    sndrcvinfo->sinfo_flags = sinfo->snd_flags;
2272
#ifdef __FreeBSD__
2273
    sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
2274
#endif
2275
    sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
2276
    sndrcvinfo->sinfo_context = sinfo->snd_context;
2277
    sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
2278
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
2279
#endif
2280
2281
    ret = sendmsg(b->num, &msg, 0);
2282
2283
    BIO_clear_retry_flags(b);
2284
    if (ret <= 0) {
2285
        if (BIO_dgram_should_retry(ret)) {
2286
            BIO_set_retry_write(b);
2287
            data->dgram._errno = get_last_socket_error();
2288
        }
2289
    }
2290
    return ret;
2291
}
2292
2293
static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
2294
{
2295
    long ret = 1;
2296
    bio_dgram_sctp_data *data = NULL;
2297
    socklen_t sockopt_len = 0;
2298
    struct sctp_authkeyid authkeyid;
2299
    struct sctp_authkey *authkey = NULL;
2300
2301
    data = (bio_dgram_sctp_data *)b->ptr;
2302
2303
    switch (cmd) {
2304
    case BIO_CTRL_DGRAM_QUERY_MTU:
2305
        /*
2306
         * Set to maximum (2^14) and ignore user input to enable transport
2307
         * protocol fragmentation. Returns always 2^14.
2308
         */
2309
        data->dgram.mtu = 16384;
2310
        ret = data->dgram.mtu;
2311
        break;
2312
    case BIO_CTRL_DGRAM_SET_MTU:
2313
        /*
2314
         * Set to maximum (2^14) and ignore input to enable transport
2315
         * protocol fragmentation. Returns always 2^14.
2316
         */
2317
        data->dgram.mtu = 16384;
2318
        ret = data->dgram.mtu;
2319
        break;
2320
    case BIO_CTRL_DGRAM_SET_CONNECTED:
2321
    case BIO_CTRL_DGRAM_CONNECT:
2322
        /* Returns always -1. */
2323
        ret = -1;
2324
        break;
2325
    case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
2326
        /*
2327
         * SCTP doesn't need the DTLS timer Returns always 1.
2328
         */
2329
        break;
2330
    case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
2331
        /*
2332
         * We allow transport protocol fragmentation so this is irrelevant
2333
         */
2334
        ret = 0;
2335
        break;
2336
    case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
2337
        if (num > 0)
2338
            data->in_handshake = 1;
2339
        else
2340
            data->in_handshake = 0;
2341
2342
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
2343
            &data->in_handshake, sizeof(int));
2344
        break;
2345
    case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
2346
        /*
2347
         * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2348
         */
2349
2350
        /* Get active key */
2351
        sockopt_len = sizeof(struct sctp_authkeyid);
2352
        ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2353
            &sockopt_len);
2354
        if (ret < 0)
2355
            break;
2356
2357
        /* Add new key */
2358
        sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
2359
        authkey = OPENSSL_malloc(sockopt_len);
2360
        if (authkey == NULL) {
2361
            ret = -1;
2362
            break;
2363
        }
2364
        memset(authkey, 0, sockopt_len);
2365
        authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
2366
#ifndef __FreeBSD__
2367
        /*
2368
         * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2369
         * and higher work without it.
2370
         */
2371
        authkey->sca_keylength = 64;
2372
#endif
2373
        memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
2374
2375
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
2376
            sockopt_len);
2377
        OPENSSL_free(authkey);
2378
        authkey = NULL;
2379
        if (ret < 0)
2380
            break;
2381
2382
        /* Reset active key */
2383
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2384
            &authkeyid, sizeof(struct sctp_authkeyid));
2385
        if (ret < 0)
2386
            break;
2387
2388
        break;
2389
    case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
2390
        /* Returns 0 on success, -1 otherwise. */
2391
2392
        /* Get active key */
2393
        sockopt_len = sizeof(struct sctp_authkeyid);
2394
        ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2395
            &sockopt_len);
2396
        if (ret < 0)
2397
            break;
2398
2399
        /* Set active key */
2400
        authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
2401
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2402
            &authkeyid, sizeof(struct sctp_authkeyid));
2403
        if (ret < 0)
2404
            break;
2405
2406
        /*
2407
         * CCS has been sent, so remember that and fall through to check if
2408
         * we need to deactivate an old key
2409
         */
2410
        data->ccs_sent = 1;
2411
        /* fall-through */
2412
2413
    case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
2414
        /* Returns 0 on success, -1 otherwise. */
2415
2416
        /*
2417
         * Has this command really been called or is this just a
2418
         * fall-through?
2419
         */
2420
        if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
2421
            data->ccs_rcvd = 1;
2422
2423
        /*
2424
         * CSS has been both, received and sent, so deactivate an old key
2425
         */
2426
        if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
2427
            /* Get active key */
2428
            sockopt_len = sizeof(struct sctp_authkeyid);
2429
            ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2430
                &authkeyid, &sockopt_len);
2431
            if (ret < 0)
2432
                break;
2433
2434
            /*
2435
             * Deactivate key or delete second last key if
2436
             * SCTP_AUTHENTICATION_EVENT is not available.
2437
             */
2438
            authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2439
#ifdef SCTP_AUTH_DEACTIVATE_KEY
2440
            sockopt_len = sizeof(struct sctp_authkeyid);
2441
            ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
2442
                &authkeyid, sockopt_len);
2443
            if (ret < 0)
2444
                break;
2445
#endif
2446
#ifndef SCTP_AUTHENTICATION_EVENT
2447
            if (authkeyid.scact_keynumber > 0) {
2448
                authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2449
                ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
2450
                    &authkeyid, sizeof(struct sctp_authkeyid));
2451
                if (ret < 0)
2452
                    break;
2453
            }
2454
#endif
2455
2456
            data->ccs_rcvd = 0;
2457
            data->ccs_sent = 0;
2458
        }
2459
        break;
2460
    case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
2461
        /* Returns the size of the copied struct. */
2462
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2463
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2464
2465
        memcpy(ptr, &(data->sndinfo), num);
2466
        ret = num;
2467
        break;
2468
    case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
2469
        /* Returns the size of the copied struct. */
2470
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2471
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2472
2473
        memcpy(&(data->sndinfo), ptr, num);
2474
        break;
2475
    case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
2476
        /* Returns the size of the copied struct. */
2477
        if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
2478
            num = sizeof(struct bio_dgram_sctp_rcvinfo);
2479
2480
        memcpy(ptr, &data->rcvinfo, num);
2481
2482
        ret = num;
2483
        break;
2484
    case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
2485
        /* Returns the size of the copied struct. */
2486
        if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
2487
            num = sizeof(struct bio_dgram_sctp_rcvinfo);
2488
2489
        memcpy(&(data->rcvinfo), ptr, num);
2490
        break;
2491
    case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
2492
        /* Returns the size of the copied struct. */
2493
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2494
            num = sizeof(struct bio_dgram_sctp_prinfo);
2495
2496
        memcpy(ptr, &(data->prinfo), num);
2497
        ret = num;
2498
        break;
2499
    case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
2500
        /* Returns the size of the copied struct. */
2501
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2502
            num = sizeof(struct bio_dgram_sctp_prinfo);
2503
2504
        memcpy(&(data->prinfo), ptr, num);
2505
        break;
2506
    case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
2507
        /* Returns always 1. */
2508
        if (num > 0)
2509
            data->save_shutdown = 1;
2510
        else
2511
            data->save_shutdown = 0;
2512
        break;
2513
    case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
2514
        return dgram_sctp_wait_for_dry(b);
2515
    case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
2516
        return dgram_sctp_msg_waiting(b);
2517
2518
    default:
2519
        /*
2520
         * Pass to default ctrl function to process SCTP unspecific commands
2521
         */
2522
        ret = dgram_ctrl(b, cmd, num, ptr);
2523
        break;
2524
    }
2525
    return ret;
2526
}
2527
2528
int BIO_dgram_sctp_notification_cb(BIO *b,
2529
    BIO_dgram_sctp_notification_handler_fn handle_notifications,
2530
    void *context)
2531
{
2532
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
2533
2534
    if (handle_notifications != NULL) {
2535
        data->handle_notifications = handle_notifications;
2536
        data->notification_context = context;
2537
    } else
2538
        return -1;
2539
2540
    return 0;
2541
}
2542
2543
/*
2544
 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2545
 * @b: The BIO to check for the dry event
2546
 *
2547
 * Wait until the peer confirms all packets have been received, and so that
2548
 * our kernel doesn't have anything to send anymore.  This is only received by
2549
 * the peer's kernel, not the application.
2550
 *
2551
 * Returns:
2552
 * -1 on error
2553
 *  0 when not dry yet
2554
 *  1 when dry
2555
 */
2556
int BIO_dgram_sctp_wait_for_dry(BIO *b)
2557
{
2558
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
2559
}
2560
2561
static int dgram_sctp_wait_for_dry(BIO *b)
2562
{
2563
    int is_dry = 0;
2564
    int sockflags = 0;
2565
    int n, ret;
2566
    union sctp_notification snp;
2567
    struct msghdr msg;
2568
    struct iovec iov;
2569
#ifdef SCTP_EVENT
2570
    struct sctp_event event;
2571
#else
2572
    struct sctp_event_subscribe event;
2573
    socklen_t eventsize;
2574
#endif
2575
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
2576
2577
    /* set sender dry event */
2578
#ifdef SCTP_EVENT
2579
    memset(&event, 0, sizeof(event));
2580
    event.se_assoc_id = 0;
2581
    event.se_type = SCTP_SENDER_DRY_EVENT;
2582
    event.se_on = 1;
2583
    ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2584
        sizeof(struct sctp_event));
2585
#else
2586
    eventsize = sizeof(struct sctp_event_subscribe);
2587
    ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
2588
    if (ret < 0)
2589
        return -1;
2590
2591
    event.sctp_sender_dry_event = 1;
2592
2593
    ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2594
        sizeof(struct sctp_event_subscribe));
2595
#endif
2596
    if (ret < 0)
2597
        return -1;
2598
2599
    /* peek for notification */
2600
    memset(&snp, 0, sizeof(snp));
2601
    iov.iov_base = (char *)&snp;
2602
    iov.iov_len = sizeof(union sctp_notification);
2603
    msg.msg_name = NULL;
2604
    msg.msg_namelen = 0;
2605
    msg.msg_iov = &iov;
2606
    msg.msg_iovlen = 1;
2607
    msg.msg_control = NULL;
2608
    msg.msg_controllen = 0;
2609
    msg.msg_flags = 0;
2610
2611
    n = recvmsg(b->num, &msg, MSG_PEEK);
2612
    if (n <= 0) {
2613
        if ((n < 0) && (get_last_socket_error() != EAGAIN)
2614
            && (get_last_socket_error() != EWOULDBLOCK))
2615
            return -1;
2616
        else
2617
            return 0;
2618
    }
2619
2620
    /* if we find a notification, process it and try again if necessary */
2621
    while (msg.msg_flags & MSG_NOTIFICATION) {
2622
        memset(&snp, 0, sizeof(snp));
2623
        iov.iov_base = (char *)&snp;
2624
        iov.iov_len = sizeof(union sctp_notification);
2625
        msg.msg_name = NULL;
2626
        msg.msg_namelen = 0;
2627
        msg.msg_iov = &iov;
2628
        msg.msg_iovlen = 1;
2629
        msg.msg_control = NULL;
2630
        msg.msg_controllen = 0;
2631
        msg.msg_flags = 0;
2632
2633
        n = recvmsg(b->num, &msg, 0);
2634
        if (n <= 0) {
2635
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2636
                && (get_last_socket_error() != EWOULDBLOCK))
2637
                return -1;
2638
            else
2639
                return is_dry;
2640
        }
2641
2642
        if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2643
            is_dry = 1;
2644
2645
            /* disable sender dry event */
2646
#ifdef SCTP_EVENT
2647
            memset(&event, 0, sizeof(event));
2648
            event.se_assoc_id = 0;
2649
            event.se_type = SCTP_SENDER_DRY_EVENT;
2650
            event.se_on = 0;
2651
            ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2652
                sizeof(struct sctp_event));
2653
#else
2654
            eventsize = (socklen_t)sizeof(struct sctp_event_subscribe);
2655
            ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2656
                &eventsize);
2657
            if (ret < 0)
2658
                return -1;
2659
2660
            event.sctp_sender_dry_event = 0;
2661
2662
            ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2663
                sizeof(struct sctp_event_subscribe));
2664
#endif
2665
            if (ret < 0)
2666
                return -1;
2667
        }
2668
#ifdef SCTP_AUTHENTICATION_EVENT
2669
        if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2670
            dgram_sctp_handle_auth_free_key_event(b, &snp);
2671
#endif
2672
2673
        if (data->handle_notifications != NULL)
2674
            data->handle_notifications(b, data->notification_context,
2675
                (void *)&snp);
2676
2677
        /* found notification, peek again */
2678
        memset(&snp, 0, sizeof(snp));
2679
        iov.iov_base = (char *)&snp;
2680
        iov.iov_len = sizeof(union sctp_notification);
2681
        msg.msg_name = NULL;
2682
        msg.msg_namelen = 0;
2683
        msg.msg_iov = &iov;
2684
        msg.msg_iovlen = 1;
2685
        msg.msg_control = NULL;
2686
        msg.msg_controllen = 0;
2687
        msg.msg_flags = 0;
2688
2689
        /* if we have seen the dry already, don't wait */
2690
        if (is_dry) {
2691
            sockflags = fcntl(b->num, F_GETFL, 0);
2692
            fcntl(b->num, F_SETFL, O_NONBLOCK);
2693
        }
2694
2695
        n = recvmsg(b->num, &msg, MSG_PEEK);
2696
2697
        if (is_dry) {
2698
            fcntl(b->num, F_SETFL, sockflags);
2699
        }
2700
2701
        if (n <= 0) {
2702
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2703
                && (get_last_socket_error() != EWOULDBLOCK))
2704
                return -1;
2705
            else
2706
                return is_dry;
2707
        }
2708
    }
2709
2710
    /* read anything else */
2711
    return is_dry;
2712
}
2713
2714
int BIO_dgram_sctp_msg_waiting(BIO *b)
2715
{
2716
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
2717
}
2718
2719
static int dgram_sctp_msg_waiting(BIO *b)
2720
{
2721
    int n, sockflags;
2722
    union sctp_notification snp;
2723
    struct msghdr msg;
2724
    struct iovec iov;
2725
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
2726
2727
    /* Check if there are any messages waiting to be read */
2728
    do {
2729
        memset(&snp, 0, sizeof(snp));
2730
        iov.iov_base = (char *)&snp;
2731
        iov.iov_len = sizeof(union sctp_notification);
2732
        msg.msg_name = NULL;
2733
        msg.msg_namelen = 0;
2734
        msg.msg_iov = &iov;
2735
        msg.msg_iovlen = 1;
2736
        msg.msg_control = NULL;
2737
        msg.msg_controllen = 0;
2738
        msg.msg_flags = 0;
2739
2740
        sockflags = fcntl(b->num, F_GETFL, 0);
2741
        fcntl(b->num, F_SETFL, O_NONBLOCK);
2742
        n = recvmsg(b->num, &msg, MSG_PEEK);
2743
        fcntl(b->num, F_SETFL, sockflags);
2744
2745
        /* if notification, process and try again */
2746
        if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
2747
#ifdef SCTP_AUTHENTICATION_EVENT
2748
            if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2749
                dgram_sctp_handle_auth_free_key_event(b, &snp);
2750
#endif
2751
2752
            memset(&snp, 0, sizeof(snp));
2753
            iov.iov_base = (char *)&snp;
2754
            iov.iov_len = sizeof(union sctp_notification);
2755
            msg.msg_name = NULL;
2756
            msg.msg_namelen = 0;
2757
            msg.msg_iov = &iov;
2758
            msg.msg_iovlen = 1;
2759
            msg.msg_control = NULL;
2760
            msg.msg_controllen = 0;
2761
            msg.msg_flags = 0;
2762
            n = recvmsg(b->num, &msg, 0);
2763
2764
            if (data->handle_notifications != NULL)
2765
                data->handle_notifications(b, data->notification_context,
2766
                    (void *)&snp);
2767
        }
2768
2769
    } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
2770
2771
    /* Return 1 if there is a message to be read, return 0 otherwise. */
2772
    if (n > 0)
2773
        return 1;
2774
    else
2775
        return 0;
2776
}
2777
2778
static int dgram_sctp_puts(BIO *bp, const char *str)
2779
{
2780
    int n, ret;
2781
2782
    n = strlen(str);
2783
    ret = dgram_sctp_write(bp, str, n);
2784
    return ret;
2785
}
2786
#endif
2787
2788
static int BIO_dgram_should_retry(int i)
2789
0
{
2790
0
    int err;
2791
2792
0
    if ((i == 0) || (i == -1)) {
2793
0
        err = get_last_socket_error();
2794
2795
#if defined(OPENSSL_SYS_WINDOWS)
2796
        /*
2797
         * If the socket return value (i) is -1 and err is unexpectedly 0 at
2798
         * this point, the error code was overwritten by another system call
2799
         * before this error handling is called.
2800
         */
2801
#endif
2802
2803
0
        return BIO_dgram_non_fatal_error(err);
2804
0
    }
2805
0
    return 0;
2806
0
}
2807
2808
int BIO_dgram_non_fatal_error(int err)
2809
0
{
2810
0
    switch (err) {
2811
#if defined(OPENSSL_SYS_WINDOWS)
2812
#if defined(WSAEWOULDBLOCK)
2813
    case WSAEWOULDBLOCK:
2814
#endif
2815
#endif
2816
2817
0
#ifdef EWOULDBLOCK
2818
#ifdef WSAEWOULDBLOCK
2819
#if WSAEWOULDBLOCK != EWOULDBLOCK
2820
    case EWOULDBLOCK:
2821
#endif
2822
#else
2823
0
    case EWOULDBLOCK:
2824
0
#endif
2825
0
#endif
2826
2827
0
#ifdef EINTR
2828
0
    case EINTR:
2829
0
#endif
2830
2831
0
#ifdef EAGAIN
2832
#if EWOULDBLOCK != EAGAIN
2833
    case EAGAIN:
2834
#endif
2835
0
#endif
2836
2837
0
#ifdef EPROTO
2838
0
    case EPROTO:
2839
0
#endif
2840
2841
0
#ifdef EINPROGRESS
2842
0
    case EINPROGRESS:
2843
0
#endif
2844
2845
0
#ifdef EALREADY
2846
0
    case EALREADY:
2847
0
#endif
2848
2849
0
        return 1;
2850
0
    default:
2851
0
        break;
2852
0
    }
2853
0
    return 0;
2854
0
}
2855
2856
#endif