Coverage Report

Created: 2025-08-25 06:30

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