Coverage Report

Created: 2026-07-16 06:59

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