Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/bio/bss_dgram.c
Line
Count
Source
1
/*
2
 * Copyright 2005-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#ifndef _GNU_SOURCE
11
# define _GNU_SOURCE
12
#endif
13
14
#include <stdio.h>
15
#include <errno.h>
16
17
#include "internal/time.h"
18
#include "bio_local.h"
19
#ifndef OPENSSL_NO_DGRAM
20
21
# ifndef OPENSSL_NO_SCTP
22
#  include <netinet/sctp.h>
23
#  include <fcntl.h>
24
#  define OPENSSL_SCTP_DATA_CHUNK_TYPE            0x00
25
#  define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
26
# endif
27
28
# if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
29
#  define IP_MTU      14        /* linux is lame */
30
# endif
31
32
# if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
33
#  define IPPROTO_IPV6 41       /* windows is lame */
34
# endif
35
36
# if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
37
/* Standard definition causes type-punning problems. */
38
#  undef IN6_IS_ADDR_V4MAPPED
39
#  define s6_addr32 __u6_addr.__u6_addr32
40
#  define IN6_IS_ADDR_V4MAPPED(a)               \
41
        (((a)->s6_addr32[0] == 0) &&          \
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
814
0
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
815
0
                                  ptr, &sz)) < 0) {
816
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
817
0
                               "calling getsockopt()");
818
0
            } else if (!ossl_assert((size_t)sz == sizeof(struct timeval))) {
819
0
                ERR_raise_data(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR,
820
0
                               "Unexpected getsockopt(SO_RCVTIMEO) return size");
821
0
                ret = -1;
822
0
            } else {
823
0
                ret = (int)sz;
824
0
            }
825
0
#  endif
826
0
        }
827
0
        break;
828
0
# endif
829
0
# if defined(SO_SNDTIMEO)
830
0
    case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
831
#  ifdef OPENSSL_SYS_WINDOWS
832
        {
833
            struct timeval *tv = (struct timeval *)ptr;
834
            int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
835
836
            if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
837
                                  (void *)&timeout, sizeof(timeout))) < 0)
838
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
839
                               "calling setsockopt()");
840
        }
841
#  else
842
0
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
843
0
                              sizeof(struct timeval))) < 0)
844
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
845
0
                           "calling setsockopt()");
846
0
#  endif
847
0
        break;
848
0
    case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
849
0
        {
850
#  ifdef OPENSSL_SYS_WINDOWS
851
            int sz = 0;
852
            int timeout;
853
            struct timeval *tv = (struct timeval *)ptr;
854
855
            sz = sizeof(timeout);
856
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
857
                                  (void *)&timeout, &sz)) < 0) {
858
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
859
                               "calling getsockopt()");
860
            } else {
861
                tv->tv_sec = timeout / 1000;
862
                tv->tv_usec = (timeout % 1000) * 1000;
863
                ret = sizeof(*tv);
864
            }
865
#  else
866
0
            socklen_t sz = sizeof(struct timeval);
867
868
0
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
869
0
                                  ptr, &sz)) < 0) {
870
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
871
0
                               "calling getsockopt()");
872
0
            } else if (!ossl_assert((size_t)sz == sizeof(struct timeval))) {
873
0
                ERR_raise_data(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR,
874
0
                               "Unexpected getsockopt(SO_SNDTIMEO) return size");
875
0
                ret = -1;
876
0
            } else {
877
0
                ret = (int)sz;
878
0
            }
879
0
#  endif
880
0
        }
881
0
        break;
882
0
# endif
883
0
    case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
884
        /* fall-through */
885
0
    case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
886
# ifdef OPENSSL_SYS_WINDOWS
887
        d_errno = (data->_errno == WSAETIMEDOUT);
888
# else
889
0
        d_errno = (data->_errno == EAGAIN);
890
0
# endif
891
0
        if (d_errno) {
892
0
            ret = 1;
893
0
            data->_errno = 0;
894
0
        } else
895
0
            ret = 0;
896
0
        break;
897
0
# ifdef EMSGSIZE
898
0
    case BIO_CTRL_DGRAM_MTU_EXCEEDED:
899
0
        if (data->_errno == EMSGSIZE) {
900
0
            ret = 1;
901
0
            data->_errno = 0;
902
0
        } else
903
0
            ret = 0;
904
0
        break;
905
0
# endif
906
0
    case BIO_CTRL_DGRAM_SET_DONT_FRAG:
907
0
        switch (data->peer.sa.sa_family) {
908
0
        case AF_INET:
909
# if defined(IP_DONTFRAG)
910
            sockopt_val = num ? 1 : 0;
911
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
912
                                  &sockopt_val, sizeof(sockopt_val))) < 0)
913
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
914
                               "calling setsockopt()");
915
# elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
916
0
            sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT;
917
0
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
918
0
                                  &sockopt_val, sizeof(sockopt_val))) < 0)
919
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
920
0
                               "calling setsockopt()");
921
# elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
922
            sockopt_val = num ? 1 : 0;
923
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
924
                                  (const char *)&sockopt_val,
925
                                  sizeof(sockopt_val))) < 0)
926
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
927
                               "calling setsockopt()");
928
# else
929
            ret = -1;
930
# endif
931
0
            break;
932
0
# if OPENSSL_USE_IPV6
933
0
        case AF_INET6:
934
0
#  if defined(IPV6_DONTFRAG)
935
0
            sockopt_val = num ? 1 : 0;
936
0
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
937
0
                                  (const void *)&sockopt_val,
938
0
                                  sizeof(sockopt_val))) < 0)
939
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
940
0
                               "calling setsockopt()");
941
942
#  elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTU_DISCOVER)
943
            sockopt_val = num ? IPV6_PMTUDISC_PROBE : IPV6_PMTUDISC_DONT;
944
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
945
                                  &sockopt_val, sizeof(sockopt_val))) < 0)
946
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
947
                               "calling setsockopt()");
948
#  else
949
            ret = -1;
950
#  endif
951
0
            break;
952
0
# endif
953
0
        default:
954
0
            ret = -1;
955
0
            break;
956
0
        }
957
0
        break;
958
0
    case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
959
0
        ret = dgram_get_mtu_overhead(&data->peer);
960
0
        break;
961
962
    /*
963
     * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
964
     * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
965
     * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
966
     * value has been updated to a non-clashing value. However to preserve
967
     * binary compatibility we now respond to both the old value and the new one
968
     */
969
0
    case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
970
0
    case BIO_CTRL_DGRAM_SET_PEEK_MODE:
971
0
        data->peekmode = (unsigned int)num;
972
0
        break;
973
974
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP:
975
0
# if defined(SUPPORT_LOCAL_ADDR)
976
0
        ret = 1;
977
# else
978
        ret = 0;
979
# endif
980
0
        break;
981
982
0
    case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE:
983
0
# if defined(SUPPORT_LOCAL_ADDR)
984
0
        num = num > 0;
985
0
        if (num != data->local_addr_enabled) {
986
0
            if (enable_local_addr(b, num) < 1) {
987
0
                ret = 0;
988
0
                break;
989
0
            }
990
991
0
            data->local_addr_enabled = (char)num;
992
0
        }
993
# else
994
        ret = 0;
995
# endif
996
0
        break;
997
998
0
    case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE:
999
0
        *(int *)ptr = data->local_addr_enabled;
1000
0
        break;
1001
1002
0
    case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS:
1003
0
        ret = (long)(BIO_DGRAM_CAP_HANDLES_DST_ADDR
1004
0
                     | BIO_DGRAM_CAP_HANDLES_SRC_ADDR
1005
0
                     | BIO_DGRAM_CAP_PROVIDES_DST_ADDR
1006
0
                     | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR);
1007
0
        break;
1008
1009
0
    case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
1010
0
    case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
1011
0
        {
1012
0
            BIO_POLL_DESCRIPTOR *pd = ptr;
1013
1014
0
            pd->type        = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
1015
0
            pd->value.fd    = b->num;
1016
0
        }
1017
0
        break;
1018
1019
0
    default:
1020
0
        ret = 0;
1021
0
        break;
1022
0
    }
1023
    /* Normalize if error */
1024
0
    if (ret < 0)
1025
0
        ret = -1;
1026
0
    return ret;
1027
0
}
1028
1029
static int dgram_puts(BIO *bp, const char *str)
1030
0
{
1031
0
    int n, ret;
1032
1033
0
    n = strlen(str);
1034
0
    ret = dgram_write(bp, str, n);
1035
0
    return ret;
1036
0
}
1037
1038
# if M_METHOD == M_METHOD_WSARECVMSG
1039
static void translate_msg_win(BIO *b, WSAMSG *mh, WSABUF *iov,
1040
                              unsigned char *control, BIO_MSG *msg)
1041
{
1042
    iov->len = msg->data_len;
1043
    iov->buf = msg->data;
1044
1045
    /* Windows requires namelen to be set exactly */
1046
    mh->name = msg->peer != NULL ? &msg->peer->sa : NULL;
1047
    if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
1048
        mh->namelen = sizeof(struct sockaddr_in);
1049
#  if OPENSSL_USE_IPV6
1050
    else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
1051
        mh->namelen = sizeof(struct sockaddr_in6);
1052
#  endif
1053
    else
1054
        mh->namelen = 0;
1055
1056
    /*
1057
     * When local address reception (IP_PKTINFO, etc.) is enabled, on Windows
1058
     * this causes WSARecvMsg to fail if the control buffer is too small to hold
1059
     * the structure, or if no control buffer is passed. So we need to give it
1060
     * the control buffer even if we aren't actually going to examine the
1061
     * result.
1062
     */
1063
    mh->lpBuffers       = iov;
1064
    mh->dwBufferCount   = 1;
1065
    mh->Control.len     = BIO_CMSG_ALLOC_LEN;
1066
    mh->Control.buf     = control;
1067
    mh->dwFlags         = 0;
1068
}
1069
# endif
1070
1071
# if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG
1072
/* Translates a BIO_MSG to a msghdr and iovec. */
1073
static void translate_msg(BIO *b, struct msghdr *mh, struct iovec *iov,
1074
                          unsigned char *control, BIO_MSG *msg)
1075
0
{
1076
0
    bio_dgram_data *data;
1077
1078
0
    iov->iov_base = msg->data;
1079
0
    iov->iov_len  = msg->data_len;
1080
1081
0
    data = (bio_dgram_data *)b->ptr;
1082
0
    if (data->connected == 0) {
1083
        /* macOS requires msg_namelen be 0 if msg_name is NULL */
1084
0
        mh->msg_name = msg->peer != NULL ? &msg->peer->sa : NULL;
1085
0
        if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
1086
0
            mh->msg_namelen = sizeof(struct sockaddr_in);
1087
0
#  if OPENSSL_USE_IPV6
1088
0
        else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
1089
0
            mh->msg_namelen = sizeof(struct sockaddr_in6);
1090
0
#  endif
1091
0
        else
1092
0
            mh->msg_namelen = 0;
1093
0
    } else {
1094
0
        mh->msg_name = NULL;
1095
0
        mh->msg_namelen = 0;
1096
0
    }
1097
1098
0
    mh->msg_iov         = iov;
1099
0
    mh->msg_iovlen      = 1;
1100
0
    mh->msg_control     = msg->local != NULL ? control : NULL;
1101
0
    mh->msg_controllen  = msg->local != NULL ? BIO_CMSG_ALLOC_LEN : 0;
1102
0
    mh->msg_flags       = 0;
1103
0
}
1104
# endif
1105
1106
# if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
1107
/* Extracts destination address from the control buffer. */
1108
0
static int extract_local(BIO *b, MSGHDR_TYPE *mh, BIO_ADDR *local) {
1109
0
#  if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1110
0
    CMSGHDR_TYPE *cmsg;
1111
0
    int af = dgram_get_sock_family(b);
1112
1113
0
    for (cmsg = BIO_CMSG_FIRSTHDR(mh); cmsg != NULL;
1114
0
         cmsg = BIO_CMSG_NXTHDR(mh, cmsg)) {
1115
0
        if (af == AF_INET) {
1116
0
            if (cmsg->cmsg_level != IPPROTO_IP)
1117
0
                continue;
1118
1119
0
#   if defined(IP_PKTINFO)
1120
0
            if (cmsg->cmsg_type != IP_PKTINFO)
1121
0
                continue;
1122
1123
0
            local->s_in.sin_addr =
1124
0
                ((struct in_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi_addr;
1125
1126
#   elif defined(IP_RECVDSTADDR)
1127
            if (cmsg->cmsg_type != IP_RECVDSTADDR)
1128
                continue;
1129
1130
            local->s_in.sin_addr = *(struct in_addr *)BIO_CMSG_DATA(cmsg);
1131
#   endif
1132
1133
0
#   if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
1134
0
            {
1135
0
                bio_dgram_data *data = b->ptr;
1136
1137
0
                local->s_in.sin_family = AF_INET;
1138
0
                local->s_in.sin_port   = data->local_addr.s_in.sin_port;
1139
0
            }
1140
0
            return 1;
1141
0
#   endif
1142
0
        }
1143
0
#   if OPENSSL_USE_IPV6
1144
0
        else if (af == AF_INET6) {
1145
0
            if (cmsg->cmsg_level != IPPROTO_IPV6)
1146
0
                continue;
1147
1148
0
#    if defined(IPV6_RECVPKTINFO)
1149
0
            if (cmsg->cmsg_type != IPV6_PKTINFO)
1150
0
                continue;
1151
1152
0
            {
1153
0
                bio_dgram_data *data = b->ptr;
1154
1155
0
                local->s_in6.sin6_addr     =
1156
0
                    ((struct in6_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi6_addr;
1157
0
                local->s_in6.sin6_family   = AF_INET6;
1158
0
                local->s_in6.sin6_port     = data->local_addr.s_in6.sin6_port;
1159
0
                local->s_in6.sin6_scope_id =
1160
0
                    data->local_addr.s_in6.sin6_scope_id;
1161
0
                local->s_in6.sin6_flowinfo = 0;
1162
0
            }
1163
0
            return 1;
1164
0
#    endif
1165
0
        }
1166
0
#   endif
1167
0
    }
1168
0
#  endif
1169
1170
0
    return 0;
1171
0
}
1172
1173
0
static int pack_local(BIO *b, MSGHDR_TYPE *mh, const BIO_ADDR *local) {
1174
0
    int af = dgram_get_sock_family(b);
1175
0
#  if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
1176
0
    CMSGHDR_TYPE *cmsg;
1177
0
    bio_dgram_data *data = b->ptr;
1178
0
#  endif
1179
1180
0
    if (af == AF_INET) {
1181
0
#  if defined(IP_PKTINFO)
1182
0
        struct in_pktinfo *info;
1183
1184
#   if defined(OPENSSL_SYS_WINDOWS)
1185
        cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
1186
#   else
1187
0
        cmsg = (CMSGHDR_TYPE *)mh->msg_control;
1188
0
#   endif
1189
1190
0
        cmsg->cmsg_len   = BIO_CMSG_LEN(sizeof(struct in_pktinfo));
1191
0
        cmsg->cmsg_level = IPPROTO_IP;
1192
0
        cmsg->cmsg_type  = IP_PKTINFO;
1193
1194
0
        info = (struct in_pktinfo *)BIO_CMSG_DATA(cmsg);
1195
0
#   if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_CYGWIN) && !defined(__FreeBSD__) && !defined(__QNX__)
1196
0
        info->ipi_spec_dst      = local->s_in.sin_addr;
1197
0
#   endif
1198
0
        info->ipi_addr.s_addr   = 0;
1199
0
        info->ipi_ifindex       = 0;
1200
1201
        /*
1202
         * We cannot override source port using this API, therefore
1203
         * ensure the application specified a source port of 0
1204
         * or the one we are bound to. (Better to error than silently
1205
         * ignore this.)
1206
         */
1207
0
        if (local->s_in.sin_port != 0
1208
0
            && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
1209
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1210
0
            return 0;
1211
0
        }
1212
1213
#   if defined(OPENSSL_SYS_WINDOWS)
1214
        mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
1215
#   else
1216
0
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
1217
0
#   endif
1218
0
        return 1;
1219
1220
#  elif defined(IP_SENDSRCADDR)
1221
        struct in_addr *info;
1222
1223
        /*
1224
         * At least FreeBSD is very pedantic about using IP_SENDSRCADDR when we
1225
         * are not bound to 0.0.0.0 or ::, even if the address matches what we
1226
         * bound to. Support this by not packing the structure if the address
1227
         * matches our understanding of our local address. IP_SENDSRCADDR is a
1228
         * BSD thing, so we don't need an explicit test for BSD here.
1229
         */
1230
        if (local->s_in.sin_addr.s_addr == data->local_addr.s_in.sin_addr.s_addr) {
1231
            mh->msg_control    = NULL;
1232
            mh->msg_controllen = 0;
1233
            return 1;
1234
        }
1235
1236
        cmsg = (struct cmsghdr *)mh->msg_control;
1237
        cmsg->cmsg_len   = BIO_CMSG_LEN(sizeof(struct in_addr));
1238
        cmsg->cmsg_level = IPPROTO_IP;
1239
        cmsg->cmsg_type  = IP_SENDSRCADDR;
1240
1241
        info = (struct in_addr *)BIO_CMSG_DATA(cmsg);
1242
        *info = local->s_in.sin_addr;
1243
1244
        /* See comment above. */
1245
        if (local->s_in.sin_port != 0
1246
            && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
1247
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1248
            return 0;
1249
        }
1250
1251
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_addr));
1252
        return 1;
1253
#  endif
1254
0
    }
1255
0
#  if OPENSSL_USE_IPV6
1256
0
    else if (af == AF_INET6) {
1257
0
#   if defined(IPV6_PKTINFO)
1258
0
        struct in6_pktinfo *info;
1259
1260
#    if defined(OPENSSL_SYS_WINDOWS)
1261
        cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
1262
#    else
1263
0
        cmsg = (CMSGHDR_TYPE *)mh->msg_control;
1264
0
#    endif
1265
0
        cmsg->cmsg_len   = BIO_CMSG_LEN(sizeof(struct in6_pktinfo));
1266
0
        cmsg->cmsg_level = IPPROTO_IPV6;
1267
0
        cmsg->cmsg_type  = IPV6_PKTINFO;
1268
1269
0
        info = (struct in6_pktinfo *)BIO_CMSG_DATA(cmsg);
1270
0
        info->ipi6_addr     = local->s_in6.sin6_addr;
1271
0
        info->ipi6_ifindex  = 0;
1272
1273
        /*
1274
         * See comment above, but also applies to the other fields
1275
         * in sockaddr_in6.
1276
         */
1277
0
        if (local->s_in6.sin6_port != 0
1278
0
            && data->local_addr.s_in6.sin6_port != local->s_in6.sin6_port) {
1279
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1280
0
            return 0;
1281
0
        }
1282
1283
0
        if (local->s_in6.sin6_scope_id != 0
1284
0
            && data->local_addr.s_in6.sin6_scope_id != local->s_in6.sin6_scope_id) {
1285
0
            ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
1286
0
            return 0;
1287
0
        }
1288
1289
#    if defined(OPENSSL_SYS_WINDOWS)
1290
        mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
1291
#    else
1292
0
        mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
1293
0
#    endif
1294
0
        return 1;
1295
0
#   endif
1296
0
    }
1297
0
#  endif
1298
1299
0
    return 0;
1300
0
}
1301
# endif
1302
1303
/*
1304
 * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
1305
 * should mask out any system flags returned by this function you cannot support
1306
 * in a particular circumstance. Currently no flags are defined.
1307
 */
1308
# if M_METHOD != M_METHOD_NONE
1309
0
static int translate_flags(uint64_t flags) {
1310
0
    return 0;
1311
0
}
1312
# endif
1313
1314
static int dgram_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
1315
                          size_t num_msg, uint64_t flags, size_t *num_processed)
1316
0
{
1317
0
# if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1318
0
    int ret;
1319
0
# endif
1320
0
# if M_METHOD == M_METHOD_RECVMMSG
1321
0
#  define BIO_MAX_MSGS_PER_CALL   64
1322
0
    int sysflags;
1323
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1324
0
    size_t i;
1325
0
    struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
1326
0
    struct iovec iov[BIO_MAX_MSGS_PER_CALL];
1327
0
    unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
1328
0
    int have_local_enabled = data->local_addr_enabled;
1329
# elif M_METHOD == M_METHOD_RECVMSG
1330
    int sysflags;
1331
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1332
    ossl_ssize_t l;
1333
    struct msghdr mh;
1334
    struct iovec iov;
1335
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1336
    int have_local_enabled = data->local_addr_enabled;
1337
# elif M_METHOD == M_METHOD_WSARECVMSG
1338
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1339
    int have_local_enabled = data->local_addr_enabled;
1340
    WSAMSG wmsg;
1341
    WSABUF wbuf;
1342
    DWORD num_bytes_sent = 0;
1343
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1344
# endif
1345
# if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1346
    int sysflags;
1347
# endif
1348
1349
0
    if (num_msg == 0) {
1350
0
        *num_processed = 0;
1351
0
        return 1;
1352
0
    }
1353
1354
0
    if (num_msg > OSSL_SSIZE_MAX)
1355
0
        num_msg = OSSL_SSIZE_MAX;
1356
1357
0
# if M_METHOD != M_METHOD_NONE
1358
0
    sysflags = translate_flags(flags);
1359
0
# endif
1360
1361
0
# if M_METHOD == M_METHOD_RECVMMSG
1362
    /*
1363
     * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1364
     * msghdr and struct iovec on the stack to support multithreaded use. Thus
1365
     * we place a fixed limit on the number of messages per call, in the
1366
     * expectation that we will be called again if there were more messages to
1367
     * be sent.
1368
     */
1369
0
    if (num_msg > BIO_MAX_MSGS_PER_CALL)
1370
0
        num_msg = BIO_MAX_MSGS_PER_CALL;
1371
1372
0
    for (i = 0; i < num_msg; ++i) {
1373
0
        translate_msg(b, &mh[i].msg_hdr, &iov[i],
1374
0
                      control[i], &BIO_MSG_N(msg, stride, i));
1375
1376
        /* If local address was requested, it must have been enabled */
1377
0
        if (BIO_MSG_N(msg, stride, i).local != NULL) {
1378
0
            if (!have_local_enabled) {
1379
0
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1380
0
                *num_processed = 0;
1381
0
                return 0;
1382
0
            }
1383
1384
0
            if (pack_local(b, &mh[i].msg_hdr,
1385
0
                           BIO_MSG_N(msg, stride, i).local) < 1) {
1386
0
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1387
0
                *num_processed = 0;
1388
0
                return 0;
1389
0
            }
1390
0
        }
1391
0
    }
1392
1393
    /* Do the batch */
1394
0
    ret = sendmmsg(b->num, mh, num_msg, sysflags);
1395
0
    if (ret < 0) {
1396
0
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1397
0
        *num_processed = 0;
1398
0
        return 0;
1399
0
    }
1400
1401
0
    for (i = 0; i < (size_t)ret; ++i) {
1402
0
        BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
1403
0
        BIO_MSG_N(msg, stride, i).flags    = 0;
1404
0
    }
1405
1406
0
    *num_processed = (size_t)ret;
1407
0
    return 1;
1408
1409
# elif M_METHOD == M_METHOD_RECVMSG
1410
    /*
1411
     * If sendmsg is available, use it.
1412
     */
1413
    translate_msg(b, &mh, &iov, control, msg);
1414
1415
    if (msg->local != NULL) {
1416
        if (!have_local_enabled) {
1417
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1418
            *num_processed = 0;
1419
            return 0;
1420
        }
1421
1422
        if (pack_local(b, &mh, msg->local) < 1) {
1423
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1424
            *num_processed = 0;
1425
            return 0;
1426
        }
1427
    }
1428
1429
    l = sendmsg(b->num, &mh, sysflags);
1430
    if (l < 0) {
1431
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1432
        *num_processed = 0;
1433
        return 0;
1434
    }
1435
1436
    msg->data_len   = (size_t)l;
1437
    msg->flags      = 0;
1438
    *num_processed  = 1;
1439
    return 1;
1440
1441
# elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
1442
#  if M_METHOD == M_METHOD_WSARECVMSG
1443
    if (bio_WSASendMsg != NULL) {
1444
        /* WSASendMsg-based implementation for Windows. */
1445
        translate_msg_win(b, &wmsg, &wbuf, control, msg);
1446
1447
        if (msg[0].local != NULL) {
1448
            if (!have_local_enabled) {
1449
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1450
                *num_processed = 0;
1451
                return 0;
1452
            }
1453
1454
            if (pack_local(b, &wmsg, msg[0].local) < 1) {
1455
                ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1456
                *num_processed = 0;
1457
                return 0;
1458
            }
1459
        }
1460
1461
        ret = WSASendMsg((SOCKET)b->num, &wmsg, 0, &num_bytes_sent, NULL, NULL);
1462
        if (ret < 0) {
1463
            ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1464
            *num_processed = 0;
1465
            return 0;
1466
        }
1467
1468
        msg[0].data_len = num_bytes_sent;
1469
        msg[0].flags    = 0;
1470
        *num_processed  = 1;
1471
        return 1;
1472
    }
1473
#  endif
1474
1475
    /*
1476
     * Fallback to sendto and send a single message.
1477
     */
1478
    if (msg[0].local != NULL) {
1479
        /*
1480
         * We cannot set the local address if using sendto
1481
         * so fail in this case
1482
         */
1483
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1484
        *num_processed = 0;
1485
        return 0;
1486
    }
1487
1488
    ret = sendto(b->num, msg[0].data,
1489
#  if defined(OPENSSL_SYS_WINDOWS)
1490
                 (int)msg[0].data_len,
1491
#  else
1492
                 msg[0].data_len,
1493
#  endif
1494
                 sysflags,
1495
                 msg[0].peer != NULL ? BIO_ADDR_sockaddr(msg[0].peer) : NULL,
1496
                 msg[0].peer != NULL ? BIO_ADDR_sockaddr_size(msg[0].peer) : 0);
1497
    if (ret <= 0) {
1498
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1499
        *num_processed = 0;
1500
        return 0;
1501
    }
1502
1503
    msg[0].data_len = ret;
1504
    msg[0].flags    = 0;
1505
    *num_processed  = 1;
1506
    return 1;
1507
1508
# else
1509
    ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
1510
    *num_processed = 0;
1511
    return 0;
1512
# endif
1513
0
}
1514
1515
static int dgram_recvmmsg(BIO *b, BIO_MSG *msg,
1516
                          size_t stride, size_t num_msg,
1517
                          uint64_t flags, size_t *num_processed)
1518
0
{
1519
0
# if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
1520
0
    int ret;
1521
0
# endif
1522
0
# if M_METHOD == M_METHOD_RECVMMSG
1523
0
    int sysflags;
1524
0
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1525
0
    size_t i;
1526
0
    struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
1527
0
    struct iovec iov[BIO_MAX_MSGS_PER_CALL];
1528
0
    unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
1529
0
    int have_local_enabled = data->local_addr_enabled;
1530
# elif M_METHOD == M_METHOD_RECVMSG
1531
    int sysflags;
1532
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1533
    ossl_ssize_t l;
1534
    struct msghdr mh;
1535
    struct iovec iov;
1536
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1537
    int have_local_enabled = data->local_addr_enabled;
1538
# elif M_METHOD == M_METHOD_WSARECVMSG
1539
    bio_dgram_data *data = (bio_dgram_data *)b->ptr;
1540
    int have_local_enabled = data->local_addr_enabled;
1541
    WSAMSG wmsg;
1542
    WSABUF wbuf;
1543
    DWORD num_bytes_received = 0;
1544
    unsigned char control[BIO_CMSG_ALLOC_LEN];
1545
# endif
1546
# if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1547
    int sysflags;
1548
    socklen_t slen;
1549
# endif
1550
1551
0
    if (num_msg == 0) {
1552
0
        *num_processed = 0;
1553
0
        return 1;
1554
0
    }
1555
1556
0
    if (num_msg > OSSL_SSIZE_MAX)
1557
0
        num_msg = OSSL_SSIZE_MAX;
1558
1559
0
# if M_METHOD != M_METHOD_NONE
1560
0
    sysflags = translate_flags(flags);
1561
0
# endif
1562
1563
0
# if M_METHOD == M_METHOD_RECVMMSG
1564
    /*
1565
     * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
1566
     * msghdr and struct iovec on the stack to support multithreaded use. Thus
1567
     * we place a fixed limit on the number of messages per call, in the
1568
     * expectation that we will be called again if there were more messages to
1569
     * be sent.
1570
     */
1571
0
    if (num_msg > BIO_MAX_MSGS_PER_CALL)
1572
0
        num_msg = BIO_MAX_MSGS_PER_CALL;
1573
1574
0
    for (i = 0; i < num_msg; ++i) {
1575
0
        translate_msg(b, &mh[i].msg_hdr, &iov[i],
1576
0
                      control[i], &BIO_MSG_N(msg, stride, i));
1577
1578
        /* If local address was requested, it must have been enabled */
1579
0
        if (BIO_MSG_N(msg, stride, i).local != NULL && !have_local_enabled) {
1580
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1581
0
            *num_processed = 0;
1582
0
            return 0;
1583
0
        }
1584
0
    }
1585
1586
    /* Do the batch */
1587
0
    ret = recvmmsg(b->num, mh, num_msg, sysflags, NULL);
1588
0
    if (ret < 0) {
1589
0
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1590
0
        *num_processed = 0;
1591
0
        return 0;
1592
0
    }
1593
1594
0
    for (i = 0; i < (size_t)ret; ++i) {
1595
0
        BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
1596
0
        BIO_MSG_N(msg, stride, i).flags    = 0;
1597
        /*
1598
         * *(msg->peer) will have been filled in by recvmmsg;
1599
         * for msg->local we parse the control data returned
1600
         */
1601
0
        if (BIO_MSG_N(msg, stride, i).local != NULL)
1602
0
            if (extract_local(b, &mh[i].msg_hdr,
1603
0
                              BIO_MSG_N(msg, stride, i).local) < 1)
1604
                /*
1605
                 * It appears BSDs do not support local addresses for
1606
                 * loopback sockets. In this case, just clear the local
1607
                 * address, as for OS X and Windows in some circumstances
1608
                 * (see below).
1609
                 */
1610
0
                BIO_ADDR_clear(BIO_MSG_N(msg, stride, i).local);
1611
0
    }
1612
1613
0
    *num_processed = (size_t)ret;
1614
0
    return 1;
1615
1616
# elif M_METHOD == M_METHOD_RECVMSG
1617
    /*
1618
     * If recvmsg is available, use it.
1619
     */
1620
    translate_msg(b, &mh, &iov, control, msg);
1621
1622
    /* If local address was requested, it must have been enabled */
1623
    if (msg->local != NULL && !have_local_enabled) {
1624
        /*
1625
         * If we have done at least one message, we must return the
1626
         * count; if we haven't done any, we can give an error code
1627
         */
1628
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1629
        *num_processed = 0;
1630
        return 0;
1631
    }
1632
1633
    l = recvmsg(b->num, &mh, sysflags);
1634
    if (l < 0) {
1635
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1636
        *num_processed = 0;
1637
        return 0;
1638
    }
1639
1640
    msg->data_len   = (size_t)l;
1641
    msg->flags      = 0;
1642
1643
    if (msg->local != NULL)
1644
        if (extract_local(b, &mh, msg->local) < 1)
1645
            /*
1646
             * OS X exhibits odd behaviour where it appears that if a packet is
1647
             * sent before the receiving interface enables IP_PKTINFO, it will
1648
             * sometimes not have any control data returned even if the
1649
             * receiving interface enables IP_PKTINFO before calling recvmsg().
1650
             * This appears to occur non-deterministically. Presumably, OS X
1651
             * handles IP_PKTINFO at the time the packet is enqueued into a
1652
             * socket's receive queue, rather than at the time recvmsg() is
1653
             * called, unlike most other operating systems. Thus (if this
1654
             * hypothesis is correct) there is a race between where IP_PKTINFO
1655
             * is enabled by the process and when the kernel's network stack
1656
             * queues the incoming message.
1657
             *
1658
             * We cannot return the local address if we do not have it, but this
1659
             * is not a caller error either, so just return a zero address
1660
             * structure. This is similar to how we handle Windows loopback
1661
             * interfaces (see below). We enable this workaround for all
1662
             * platforms, not just Apple, as this kind of quirk in OS networking
1663
             * stacks seems to be common enough that failing hard if a local
1664
             * address is not provided appears to be too brittle.
1665
             */
1666
            BIO_ADDR_clear(msg->local);
1667
1668
    *num_processed = 1;
1669
    return 1;
1670
1671
# elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
1672
#  if M_METHOD == M_METHOD_WSARECVMSG
1673
    if (bio_WSARecvMsg != NULL) {
1674
        /* WSARecvMsg-based implementation for Windows. */
1675
        translate_msg_win(b, &wmsg, &wbuf, control, msg);
1676
1677
        /* If local address was requested, it must have been enabled */
1678
        if (msg[0].local != NULL && !have_local_enabled) {
1679
            ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1680
            *num_processed = 0;
1681
            return 0;
1682
        }
1683
1684
        ret = WSARecvMsg((SOCKET)b->num, &wmsg, &num_bytes_received, NULL, NULL);
1685
        if (ret < 0) {
1686
            ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1687
            *num_processed = 0;
1688
            return 0;
1689
        }
1690
1691
        msg[0].data_len = num_bytes_received;
1692
        msg[0].flags    = 0;
1693
        if (msg[0].local != NULL)
1694
            if (extract_local(b, &wmsg, msg[0].local) < 1)
1695
                /*
1696
                 * On Windows, loopback is not a "proper" interface and it works
1697
                 * differently; packets are essentially short-circuited and
1698
                 * don't go through all of the normal processing. A consequence
1699
                 * of this is that packets sent from the local machine to the
1700
                 * local machine _will not have IP_PKTINFO_ even if the
1701
                 * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
1702
                 * Control.len to 0 on returning.
1703
                 *
1704
                 * This applies regardless of whether the loopback address,
1705
                 * 127.0.0.1 is used, or a local interface address (e.g.
1706
                 * 192.168.1.1); in both cases IP_PKTINFO will not be present.
1707
                 *
1708
                 * We report this condition by setting the local BIO_ADDR's
1709
                 * family to 0.
1710
                 */
1711
                BIO_ADDR_clear(msg[0].local);
1712
1713
        *num_processed = 1;
1714
        return 1;
1715
    }
1716
#  endif
1717
1718
    /*
1719
     * Fallback to recvfrom and receive a single message.
1720
     */
1721
    if (msg[0].local != NULL) {
1722
        /*
1723
         * We cannot determine the local address if using recvfrom
1724
         * so fail in this case
1725
         */
1726
        ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
1727
        *num_processed = 0;
1728
        return 0;
1729
    }
1730
1731
    slen = sizeof(*msg[0].peer);
1732
    ret = recvfrom(b->num, msg[0].data,
1733
#  if defined(OPENSSL_SYS_WINDOWS)
1734
                   (int)msg[0].data_len,
1735
#  else
1736
                   msg[0].data_len,
1737
#  endif
1738
                   sysflags,
1739
                   msg[0].peer != NULL ? &msg[0].peer->sa : NULL,
1740
                   msg[0].peer != NULL ? &slen : NULL);
1741
    if (ret <= 0) {
1742
        ERR_raise(ERR_LIB_SYS, get_last_socket_error());
1743
        *num_processed = 0;
1744
        return 0;
1745
    }
1746
1747
    msg[0].data_len = ret;
1748
    msg[0].flags    = 0;
1749
    *num_processed = 1;
1750
    return 1;
1751
1752
# else
1753
    ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
1754
    *num_processed = 0;
1755
    return 0;
1756
# endif
1757
0
}
1758
1759
# ifndef OPENSSL_NO_SCTP
1760
const BIO_METHOD *BIO_s_datagram_sctp(void)
1761
{
1762
    return &methods_dgramp_sctp;
1763
}
1764
1765
BIO *BIO_new_dgram_sctp(int fd, int close_flag)
1766
{
1767
    BIO *bio;
1768
    int ret, optval = 20000;
1769
    int auth_data = 0, auth_forward = 0;
1770
    unsigned char *p;
1771
    struct sctp_authchunk auth;
1772
    struct sctp_authchunks *authchunks;
1773
    socklen_t sockopt_len;
1774
#  ifdef SCTP_AUTHENTICATION_EVENT
1775
#   ifdef SCTP_EVENT
1776
    struct sctp_event event;
1777
#   else
1778
    struct sctp_event_subscribe event;
1779
#   endif
1780
#  endif
1781
1782
    bio = BIO_new(BIO_s_datagram_sctp());
1783
    if (bio == NULL)
1784
        return NULL;
1785
    BIO_set_fd(bio, fd, close_flag);
1786
1787
    /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1788
    auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
1789
    ret =
1790
        setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1791
                   sizeof(struct sctp_authchunk));
1792
    if (ret < 0) {
1793
        BIO_vfree(bio);
1794
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1795
                       "Ensure SCTP AUTH chunks are enabled in kernel");
1796
        return NULL;
1797
    }
1798
    auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
1799
    ret =
1800
        setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1801
                   sizeof(struct sctp_authchunk));
1802
    if (ret < 0) {
1803
        BIO_vfree(bio);
1804
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1805
                       "Ensure SCTP AUTH chunks are enabled in kernel");
1806
        return NULL;
1807
    }
1808
1809
    /*
1810
     * Test if activation was successful. When using accept(), SCTP-AUTH has
1811
     * to be activated for the listening socket already, otherwise the
1812
     * connected socket won't use it. Similarly with connect(): the socket
1813
     * prior to connection must be activated for SCTP-AUTH
1814
     */
1815
    sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1816
    authchunks = OPENSSL_zalloc(sockopt_len);
1817
    if (authchunks == NULL) {
1818
        BIO_vfree(bio);
1819
        return NULL;
1820
    }
1821
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1822
                   &sockopt_len);
1823
    if (ret < 0) {
1824
        OPENSSL_free(authchunks);
1825
        BIO_vfree(bio);
1826
        return NULL;
1827
    }
1828
1829
    for (p = (unsigned char *)authchunks->gauth_chunks;
1830
         p < (unsigned char *)authchunks + sockopt_len;
1831
         p += sizeof(uint8_t)) {
1832
        if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1833
            auth_data = 1;
1834
        if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1835
            auth_forward = 1;
1836
    }
1837
1838
    OPENSSL_free(authchunks);
1839
1840
    if (!auth_data || !auth_forward) {
1841
        BIO_vfree(bio);
1842
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1843
                       "Ensure SCTP AUTH chunks are enabled on the "
1844
                       "underlying socket");
1845
        return NULL;
1846
    }
1847
1848
#  ifdef SCTP_AUTHENTICATION_EVENT
1849
#   ifdef SCTP_EVENT
1850
    memset(&event, 0, sizeof(event));
1851
    event.se_assoc_id = 0;
1852
    event.se_type = SCTP_AUTHENTICATION_EVENT;
1853
    event.se_on = 1;
1854
    ret =
1855
        setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1856
                   sizeof(struct sctp_event));
1857
    if (ret < 0) {
1858
        BIO_vfree(bio);
1859
        return NULL;
1860
    }
1861
#   else
1862
    sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1863
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1864
    if (ret < 0) {
1865
        BIO_vfree(bio);
1866
        return NULL;
1867
    }
1868
1869
    event.sctp_authentication_event = 1;
1870
1871
    ret =
1872
        setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1873
                   sizeof(struct sctp_event_subscribe));
1874
    if (ret < 0) {
1875
        BIO_vfree(bio);
1876
        return NULL;
1877
    }
1878
#   endif
1879
#  endif
1880
1881
    /*
1882
     * Disable partial delivery by setting the min size larger than the max
1883
     * record size of 2^14 + 2048 + 13
1884
     */
1885
    ret =
1886
        setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1887
                   sizeof(optval));
1888
    if (ret < 0) {
1889
        BIO_vfree(bio);
1890
        return NULL;
1891
    }
1892
1893
    return bio;
1894
}
1895
1896
int BIO_dgram_is_sctp(BIO *bio)
1897
{
1898
    return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1899
}
1900
1901
static int dgram_sctp_new(BIO *bi)
1902
{
1903
    bio_dgram_sctp_data *data = NULL;
1904
1905
    bi->init = 0;
1906
    bi->num = 0;
1907
    if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
1908
        return 0;
1909
#  ifdef SCTP_PR_SCTP_NONE
1910
    data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1911
#  endif
1912
    bi->ptr = data;
1913
1914
    bi->flags = 0;
1915
    return 1;
1916
}
1917
1918
static int dgram_sctp_free(BIO *a)
1919
{
1920
    bio_dgram_sctp_data *data;
1921
1922
    if (a == NULL)
1923
        return 0;
1924
    if (!dgram_clear(a))
1925
        return 0;
1926
1927
    data = (bio_dgram_sctp_data *) a->ptr;
1928
    if (data != NULL)
1929
        OPENSSL_free(data);
1930
1931
    return 1;
1932
}
1933
1934
#  ifdef SCTP_AUTHENTICATION_EVENT
1935
void dgram_sctp_handle_auth_free_key_event(BIO *b,
1936
                                           union sctp_notification *snp)
1937
{
1938
    int ret;
1939
    struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1940
1941
    if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1942
        struct sctp_authkeyid authkeyid;
1943
1944
        /* delete key */
1945
        authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1946
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1947
                         &authkeyid, sizeof(struct sctp_authkeyid));
1948
    }
1949
}
1950
#  endif
1951
1952
static int dgram_sctp_read(BIO *b, char *out, int outl)
1953
{
1954
    int ret = 0, n = 0, i, optval;
1955
    socklen_t optlen;
1956
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1957
    struct msghdr msg;
1958
    struct iovec iov;
1959
    struct cmsghdr *cmsg;
1960
    char cmsgbuf[512];
1961
1962
    if (out != NULL) {
1963
        clear_socket_error();
1964
1965
        do {
1966
            memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1967
            iov.iov_base = out;
1968
            iov.iov_len = outl;
1969
            msg.msg_name = NULL;
1970
            msg.msg_namelen = 0;
1971
            msg.msg_iov = &iov;
1972
            msg.msg_iovlen = 1;
1973
            msg.msg_control = cmsgbuf;
1974
            msg.msg_controllen = 512;
1975
            msg.msg_flags = 0;
1976
            n = recvmsg(b->num, &msg, 0);
1977
1978
            if (n <= 0) {
1979
                if (n < 0)
1980
                    ret = n;
1981
                break;
1982
            }
1983
1984
            if (msg.msg_controllen > 0) {
1985
                for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1986
                     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1987
                    if (cmsg->cmsg_level != IPPROTO_SCTP)
1988
                        continue;
1989
#  ifdef SCTP_RCVINFO
1990
                    if (cmsg->cmsg_type == SCTP_RCVINFO) {
1991
                        struct sctp_rcvinfo *rcvinfo;
1992
1993
                        rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1994
                        data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1995
                        data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1996
                        data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1997
                        data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1998
                        data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1999
                        data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
2000
                        data->rcvinfo.rcv_context = rcvinfo->rcv_context;
2001
                    }
2002
#  endif
2003
#  ifdef SCTP_SNDRCV
2004
                    if (cmsg->cmsg_type == SCTP_SNDRCV) {
2005
                        struct sctp_sndrcvinfo *sndrcvinfo;
2006
2007
                        sndrcvinfo =
2008
                            (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2009
                        data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
2010
                        data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
2011
                        data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
2012
                        data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
2013
                        data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
2014
                        data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
2015
                        data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
2016
                    }
2017
#  endif
2018
                }
2019
            }
2020
2021
            if (msg.msg_flags & MSG_NOTIFICATION) {
2022
                union sctp_notification snp;
2023
2024
                if (n < (int)sizeof(snp.sn_header))
2025
                    return -1;
2026
                memset(&snp, 0, sizeof(snp));
2027
                memcpy(&snp, out, (size_t)n < sizeof(snp) ? (size_t)n : sizeof(snp));
2028
                if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2029
#  ifdef SCTP_EVENT
2030
                    struct sctp_event event;
2031
#  else
2032
                    struct sctp_event_subscribe event;
2033
                    socklen_t eventsize;
2034
#  endif
2035
2036
                    /* disable sender dry event */
2037
#  ifdef SCTP_EVENT
2038
                    memset(&event, 0, sizeof(event));
2039
                    event.se_assoc_id = 0;
2040
                    event.se_type = SCTP_SENDER_DRY_EVENT;
2041
                    event.se_on = 0;
2042
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2043
                                   sizeof(struct sctp_event));
2044
                    if (i < 0) {
2045
                        ret = i;
2046
                        break;
2047
                    }
2048
#  else
2049
                    eventsize = sizeof(struct sctp_event_subscribe);
2050
                    i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2051
                                   &eventsize);
2052
                    if (i < 0) {
2053
                        ret = i;
2054
                        break;
2055
                    }
2056
2057
                    event.sctp_sender_dry_event = 0;
2058
2059
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2060
                                   sizeof(struct sctp_event_subscribe));
2061
                    if (i < 0) {
2062
                        ret = i;
2063
                        break;
2064
                    }
2065
#  endif
2066
                }
2067
#  ifdef SCTP_AUTHENTICATION_EVENT
2068
                if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2069
                    dgram_sctp_handle_auth_free_key_event(b, &snp);
2070
#  endif
2071
2072
                if (data->handle_notifications != NULL)
2073
                    data->handle_notifications(b, data->notification_context,
2074
                                               (void *)out);
2075
2076
                memset(out, 0, outl);
2077
            } else {
2078
                ret += n;
2079
            }
2080
        }
2081
        while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
2082
               && (ret < outl));
2083
2084
        if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
2085
            /* Partial message read, this should never happen! */
2086
2087
            /*
2088
             * The buffer was too small, this means the peer sent a message
2089
             * that was larger than allowed.
2090
             */
2091
            if (ret == outl)
2092
                return -1;
2093
2094
            /*
2095
             * Test if socket buffer can handle max record size (2^14 + 2048
2096
             * + 13)
2097
             */
2098
            optlen = (socklen_t) sizeof(int);
2099
            ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
2100
            if (ret >= 0 && !ossl_assert(optval >= 18445))
2101
                return -1;
2102
2103
            /*
2104
             * Test if SCTP doesn't partially deliver below max record size
2105
             * (2^14 + 2048 + 13)
2106
             */
2107
            optlen = (socklen_t) sizeof(int);
2108
            ret =
2109
                getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
2110
                           &optval, &optlen);
2111
            if (ret >= 0 && !ossl_assert(optval >= 18445))
2112
                return -1;
2113
2114
            /*
2115
             * Partially delivered notification??? Probably a bug....
2116
             */
2117
            if (!ossl_assert((msg.msg_flags & MSG_NOTIFICATION) == 0))
2118
                return -1;
2119
2120
            /*
2121
             * Everything seems ok till now, so it's most likely a message
2122
             * dropped by PR-SCTP.
2123
             */
2124
            memset(out, 0, outl);
2125
            BIO_set_retry_read(b);
2126
            return -1;
2127
        }
2128
2129
        BIO_clear_retry_flags(b);
2130
        if (ret < 0) {
2131
            if (BIO_dgram_should_retry(ret)) {
2132
                BIO_set_retry_read(b);
2133
                data->dgram._errno = get_last_socket_error();
2134
            }
2135
        }
2136
2137
        /* Test if peer uses SCTP-AUTH before continuing */
2138
        if (!data->peer_auth_tested) {
2139
            int ii, auth_data = 0, auth_forward = 0;
2140
            unsigned char *p;
2141
            struct sctp_authchunks *authchunks;
2142
2143
            optlen =
2144
                (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
2145
            authchunks = OPENSSL_malloc(optlen);
2146
            if (authchunks == NULL)
2147
                return -1;
2148
            memset(authchunks, 0, optlen);
2149
            ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
2150
                            authchunks, &optlen);
2151
2152
            if (ii >= 0)
2153
                for (p = (unsigned char *)authchunks->gauth_chunks;
2154
                     p < (unsigned char *)authchunks + optlen;
2155
                     p += sizeof(uint8_t)) {
2156
                    if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
2157
                        auth_data = 1;
2158
                    if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
2159
                        auth_forward = 1;
2160
                }
2161
2162
            OPENSSL_free(authchunks);
2163
2164
            if (!auth_data || !auth_forward) {
2165
                ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
2166
                return -1;
2167
            }
2168
2169
            data->peer_auth_tested = 1;
2170
        }
2171
    }
2172
    return ret;
2173
}
2174
2175
/*
2176
 * dgram_sctp_write - send message on SCTP socket
2177
 * @b: BIO to write to
2178
 * @in: data to send
2179
 * @inl: amount of bytes in @in to send
2180
 *
2181
 * Returns -1 on error or the sent amount of bytes on success
2182
 */
2183
static int dgram_sctp_write(BIO *b, const char *in, int inl)
2184
{
2185
    int ret;
2186
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2187
    struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
2188
    struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
2189
    struct bio_dgram_sctp_sndinfo handshake_sinfo;
2190
    struct iovec iov[1];
2191
    struct msghdr msg;
2192
    struct cmsghdr *cmsg;
2193
#  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2194
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
2195
                 CMSG_SPACE(sizeof(struct sctp_prinfo))];
2196
    struct sctp_sndinfo *sndinfo;
2197
    struct sctp_prinfo *prinfo;
2198
#  else
2199
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
2200
    struct sctp_sndrcvinfo *sndrcvinfo;
2201
#  endif
2202
2203
    clear_socket_error();
2204
2205
    /*
2206
     * If we're send anything else than application data, disable all user
2207
     * parameters and flags.
2208
     */
2209
    if (in[0] != 23) {
2210
        memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
2211
#  ifdef SCTP_SACK_IMMEDIATELY
2212
        handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
2213
#  endif
2214
        sinfo = &handshake_sinfo;
2215
    }
2216
2217
    /* We can only send a shutdown alert if the socket is dry */
2218
    if (data->save_shutdown) {
2219
        ret = BIO_dgram_sctp_wait_for_dry(b);
2220
        if (ret < 0)
2221
            return -1;
2222
        if (ret == 0) {
2223
            BIO_clear_retry_flags(b);
2224
            BIO_set_retry_write(b);
2225
            return -1;
2226
        }
2227
    }
2228
2229
    iov[0].iov_base = (char *)in;
2230
    iov[0].iov_len = inl;
2231
    msg.msg_name = NULL;
2232
    msg.msg_namelen = 0;
2233
    msg.msg_iov = iov;
2234
    msg.msg_iovlen = 1;
2235
    msg.msg_control = (caddr_t) cmsgbuf;
2236
    msg.msg_controllen = 0;
2237
    msg.msg_flags = 0;
2238
#  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2239
    cmsg = (struct cmsghdr *)cmsgbuf;
2240
    cmsg->cmsg_level = IPPROTO_SCTP;
2241
    cmsg->cmsg_type = SCTP_SNDINFO;
2242
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
2243
    sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
2244
    memset(sndinfo, 0, sizeof(*sndinfo));
2245
    sndinfo->snd_sid = sinfo->snd_sid;
2246
    sndinfo->snd_flags = sinfo->snd_flags;
2247
    sndinfo->snd_ppid = sinfo->snd_ppid;
2248
    sndinfo->snd_context = sinfo->snd_context;
2249
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
2250
2251
    cmsg =
2252
        (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
2253
    cmsg->cmsg_level = IPPROTO_SCTP;
2254
    cmsg->cmsg_type = SCTP_PRINFO;
2255
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
2256
    prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
2257
    memset(prinfo, 0, sizeof(*prinfo));
2258
    prinfo->pr_policy = pinfo->pr_policy;
2259
    prinfo->pr_value = pinfo->pr_value;
2260
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
2261
#  else
2262
    cmsg = (struct cmsghdr *)cmsgbuf;
2263
    cmsg->cmsg_level = IPPROTO_SCTP;
2264
    cmsg->cmsg_type = SCTP_SNDRCV;
2265
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
2266
    sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2267
    memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
2268
    sndrcvinfo->sinfo_stream = sinfo->snd_sid;
2269
    sndrcvinfo->sinfo_flags = sinfo->snd_flags;
2270
#   ifdef __FreeBSD__
2271
    sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
2272
#   endif
2273
    sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
2274
    sndrcvinfo->sinfo_context = sinfo->snd_context;
2275
    sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
2276
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
2277
#  endif
2278
2279
    ret = sendmsg(b->num, &msg, 0);
2280
2281
    BIO_clear_retry_flags(b);
2282
    if (ret <= 0) {
2283
        if (BIO_dgram_should_retry(ret)) {
2284
            BIO_set_retry_write(b);
2285
            data->dgram._errno = get_last_socket_error();
2286
        }
2287
    }
2288
    return ret;
2289
}
2290
2291
static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
2292
{
2293
    long ret = 1;
2294
    bio_dgram_sctp_data *data = NULL;
2295
    socklen_t sockopt_len = 0;
2296
    struct sctp_authkeyid authkeyid;
2297
    struct sctp_authkey *authkey = NULL;
2298
2299
    data = (bio_dgram_sctp_data *) b->ptr;
2300
2301
    switch (cmd) {
2302
    case BIO_CTRL_DGRAM_QUERY_MTU:
2303
        /*
2304
         * Set to maximum (2^14) and ignore user input to enable transport
2305
         * protocol fragmentation. Returns always 2^14.
2306
         */
2307
        data->dgram.mtu = 16384;
2308
        ret = data->dgram.mtu;
2309
        break;
2310
    case BIO_CTRL_DGRAM_SET_MTU:
2311
        /*
2312
         * Set to maximum (2^14) and ignore input to enable transport
2313
         * protocol fragmentation. Returns always 2^14.
2314
         */
2315
        data->dgram.mtu = 16384;
2316
        ret = data->dgram.mtu;
2317
        break;
2318
    case BIO_CTRL_DGRAM_SET_CONNECTED:
2319
    case BIO_CTRL_DGRAM_CONNECT:
2320
        /* Returns always -1. */
2321
        ret = -1;
2322
        break;
2323
    case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
2324
        /*
2325
         * SCTP doesn't need the DTLS timer Returns always 1.
2326
         */
2327
        break;
2328
    case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
2329
        /*
2330
         * We allow transport protocol fragmentation so this is irrelevant
2331
         */
2332
        ret = 0;
2333
        break;
2334
    case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
2335
        if (num > 0)
2336
            data->in_handshake = 1;
2337
        else
2338
            data->in_handshake = 0;
2339
2340
        ret =
2341
            setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
2342
                       &data->in_handshake, sizeof(int));
2343
        break;
2344
    case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
2345
        /*
2346
         * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2347
         */
2348
2349
        /* Get active key */
2350
        sockopt_len = sizeof(struct sctp_authkeyid);
2351
        ret =
2352
            getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2353
                       &sockopt_len);
2354
        if (ret < 0)
2355
            break;
2356
2357
        /* Add new key */
2358
        sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
2359
        authkey = OPENSSL_malloc(sockopt_len);
2360
        if (authkey == NULL) {
2361
            ret = -1;
2362
            break;
2363
        }
2364
        memset(authkey, 0, sockopt_len);
2365
        authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
2366
#  ifndef __FreeBSD__
2367
        /*
2368
         * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2369
         * and higher work without it.
2370
         */
2371
        authkey->sca_keylength = 64;
2372
#  endif
2373
        memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
2374
2375
        ret =
2376
            setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
2377
                       sockopt_len);
2378
        OPENSSL_free(authkey);
2379
        authkey = NULL;
2380
        if (ret < 0)
2381
            break;
2382
2383
        /* Reset active key */
2384
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2385
                         &authkeyid, sizeof(struct sctp_authkeyid));
2386
        if (ret < 0)
2387
            break;
2388
2389
        break;
2390
    case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
2391
        /* Returns 0 on success, -1 otherwise. */
2392
2393
        /* Get active key */
2394
        sockopt_len = sizeof(struct sctp_authkeyid);
2395
        ret =
2396
            getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2397
                       &sockopt_len);
2398
        if (ret < 0)
2399
            break;
2400
2401
        /* Set active key */
2402
        authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
2403
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2404
                         &authkeyid, sizeof(struct sctp_authkeyid));
2405
        if (ret < 0)
2406
            break;
2407
2408
        /*
2409
         * CCS has been sent, so remember that and fall through to check if
2410
         * we need to deactivate an old key
2411
         */
2412
        data->ccs_sent = 1;
2413
        /* fall-through */
2414
2415
    case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
2416
        /* Returns 0 on success, -1 otherwise. */
2417
2418
        /*
2419
         * Has this command really been called or is this just a
2420
         * fall-through?
2421
         */
2422
        if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
2423
            data->ccs_rcvd = 1;
2424
2425
        /*
2426
         * CSS has been both, received and sent, so deactivate an old key
2427
         */
2428
        if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
2429
            /* Get active key */
2430
            sockopt_len = sizeof(struct sctp_authkeyid);
2431
            ret =
2432
                getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2433
                           &authkeyid, &sockopt_len);
2434
            if (ret < 0)
2435
                break;
2436
2437
            /*
2438
             * Deactivate key or delete second last key if
2439
             * SCTP_AUTHENTICATION_EVENT is not available.
2440
             */
2441
            authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2442
#  ifdef SCTP_AUTH_DEACTIVATE_KEY
2443
            sockopt_len = sizeof(struct sctp_authkeyid);
2444
            ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
2445
                             &authkeyid, sockopt_len);
2446
            if (ret < 0)
2447
                break;
2448
#  endif
2449
#  ifndef SCTP_AUTHENTICATION_EVENT
2450
            if (authkeyid.scact_keynumber > 0) {
2451
                authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2452
                ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
2453
                                 &authkeyid, sizeof(struct sctp_authkeyid));
2454
                if (ret < 0)
2455
                    break;
2456
            }
2457
#  endif
2458
2459
            data->ccs_rcvd = 0;
2460
            data->ccs_sent = 0;
2461
        }
2462
        break;
2463
    case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
2464
        /* Returns the size of the copied struct. */
2465
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2466
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2467
2468
        memcpy(ptr, &(data->sndinfo), num);
2469
        ret = num;
2470
        break;
2471
    case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
2472
        /* Returns the size of the copied struct. */
2473
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2474
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2475
2476
        memcpy(&(data->sndinfo), ptr, num);
2477
        break;
2478
    case BIO_CTRL_DGRAM_SCTP_GET_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(ptr, &data->rcvinfo, num);
2484
2485
        ret = num;
2486
        break;
2487
    case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
2488
        /* Returns the size of the copied struct. */
2489
        if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
2490
            num = sizeof(struct bio_dgram_sctp_rcvinfo);
2491
2492
        memcpy(&(data->rcvinfo), ptr, num);
2493
        break;
2494
    case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
2495
        /* Returns the size of the copied struct. */
2496
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2497
            num = sizeof(struct bio_dgram_sctp_prinfo);
2498
2499
        memcpy(ptr, &(data->prinfo), num);
2500
        ret = num;
2501
        break;
2502
    case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
2503
        /* Returns the size of the copied struct. */
2504
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2505
            num = sizeof(struct bio_dgram_sctp_prinfo);
2506
2507
        memcpy(&(data->prinfo), ptr, num);
2508
        break;
2509
    case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
2510
        /* Returns always 1. */
2511
        if (num > 0)
2512
            data->save_shutdown = 1;
2513
        else
2514
            data->save_shutdown = 0;
2515
        break;
2516
    case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
2517
        return dgram_sctp_wait_for_dry(b);
2518
    case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
2519
        return dgram_sctp_msg_waiting(b);
2520
2521
    default:
2522
        /*
2523
         * Pass to default ctrl function to process SCTP unspecific commands
2524
         */
2525
        ret = dgram_ctrl(b, cmd, num, ptr);
2526
        break;
2527
    }
2528
    return ret;
2529
}
2530
2531
int BIO_dgram_sctp_notification_cb(BIO *b,
2532
                BIO_dgram_sctp_notification_handler_fn handle_notifications,
2533
                void *context)
2534
{
2535
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2536
2537
    if (handle_notifications != NULL) {
2538
        data->handle_notifications = handle_notifications;
2539
        data->notification_context = context;
2540
    } else
2541
        return -1;
2542
2543
    return 0;
2544
}
2545
2546
/*
2547
 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2548
 * @b: The BIO to check for the dry event
2549
 *
2550
 * Wait until the peer confirms all packets have been received, and so that
2551
 * our kernel doesn't have anything to send anymore.  This is only received by
2552
 * the peer's kernel, not the application.
2553
 *
2554
 * Returns:
2555
 * -1 on error
2556
 *  0 when not dry yet
2557
 *  1 when dry
2558
 */
2559
int BIO_dgram_sctp_wait_for_dry(BIO *b)
2560
{
2561
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
2562
}
2563
2564
static int dgram_sctp_wait_for_dry(BIO *b)
2565
{
2566
    int is_dry = 0;
2567
    int sockflags = 0;
2568
    int n, ret;
2569
    union sctp_notification snp;
2570
    struct msghdr msg;
2571
    struct iovec iov;
2572
#  ifdef SCTP_EVENT
2573
    struct sctp_event event;
2574
#  else
2575
    struct sctp_event_subscribe event;
2576
    socklen_t eventsize;
2577
#  endif
2578
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2579
2580
    /* set sender dry event */
2581
#  ifdef SCTP_EVENT
2582
    memset(&event, 0, sizeof(event));
2583
    event.se_assoc_id = 0;
2584
    event.se_type = SCTP_SENDER_DRY_EVENT;
2585
    event.se_on = 1;
2586
    ret =
2587
        setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2588
                   sizeof(struct sctp_event));
2589
#  else
2590
    eventsize = sizeof(struct sctp_event_subscribe);
2591
    ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
2592
    if (ret < 0)
2593
        return -1;
2594
2595
    event.sctp_sender_dry_event = 1;
2596
2597
    ret =
2598
        setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2599
                   sizeof(struct sctp_event_subscribe));
2600
#  endif
2601
    if (ret < 0)
2602
        return -1;
2603
2604
    /* peek for notification */
2605
    memset(&snp, 0, sizeof(snp));
2606
    iov.iov_base = (char *)&snp;
2607
    iov.iov_len = sizeof(union sctp_notification);
2608
    msg.msg_name = NULL;
2609
    msg.msg_namelen = 0;
2610
    msg.msg_iov = &iov;
2611
    msg.msg_iovlen = 1;
2612
    msg.msg_control = NULL;
2613
    msg.msg_controllen = 0;
2614
    msg.msg_flags = 0;
2615
2616
    n = recvmsg(b->num, &msg, MSG_PEEK);
2617
    if (n <= 0) {
2618
        if ((n < 0) && (get_last_socket_error() != EAGAIN)
2619
            && (get_last_socket_error() != EWOULDBLOCK))
2620
            return -1;
2621
        else
2622
            return 0;
2623
    }
2624
2625
    /* if we find a notification, process it and try again if necessary */
2626
    while (msg.msg_flags & MSG_NOTIFICATION) {
2627
        memset(&snp, 0, sizeof(snp));
2628
        iov.iov_base = (char *)&snp;
2629
        iov.iov_len = sizeof(union sctp_notification);
2630
        msg.msg_name = NULL;
2631
        msg.msg_namelen = 0;
2632
        msg.msg_iov = &iov;
2633
        msg.msg_iovlen = 1;
2634
        msg.msg_control = NULL;
2635
        msg.msg_controllen = 0;
2636
        msg.msg_flags = 0;
2637
2638
        n = recvmsg(b->num, &msg, 0);
2639
        if (n <= 0) {
2640
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2641
                && (get_last_socket_error() != EWOULDBLOCK))
2642
                return -1;
2643
            else
2644
                return is_dry;
2645
        }
2646
2647
        if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2648
            is_dry = 1;
2649
2650
            /* disable sender dry event */
2651
#  ifdef SCTP_EVENT
2652
            memset(&event, 0, sizeof(event));
2653
            event.se_assoc_id = 0;
2654
            event.se_type = SCTP_SENDER_DRY_EVENT;
2655
            event.se_on = 0;
2656
            ret =
2657
                setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2658
                           sizeof(struct sctp_event));
2659
#  else
2660
            eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
2661
            ret =
2662
                getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2663
                           &eventsize);
2664
            if (ret < 0)
2665
                return -1;
2666
2667
            event.sctp_sender_dry_event = 0;
2668
2669
            ret =
2670
                setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2671
                           sizeof(struct sctp_event_subscribe));
2672
#  endif
2673
            if (ret < 0)
2674
                return -1;
2675
        }
2676
#  ifdef SCTP_AUTHENTICATION_EVENT
2677
        if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2678
            dgram_sctp_handle_auth_free_key_event(b, &snp);
2679
#  endif
2680
2681
        if (data->handle_notifications != NULL)
2682
            data->handle_notifications(b, data->notification_context,
2683
                                       (void *)&snp);
2684
2685
        /* found notification, peek again */
2686
        memset(&snp, 0, sizeof(snp));
2687
        iov.iov_base = (char *)&snp;
2688
        iov.iov_len = sizeof(union sctp_notification);
2689
        msg.msg_name = NULL;
2690
        msg.msg_namelen = 0;
2691
        msg.msg_iov = &iov;
2692
        msg.msg_iovlen = 1;
2693
        msg.msg_control = NULL;
2694
        msg.msg_controllen = 0;
2695
        msg.msg_flags = 0;
2696
2697
        /* if we have seen the dry already, don't wait */
2698
        if (is_dry) {
2699
            sockflags = fcntl(b->num, F_GETFL, 0);
2700
            fcntl(b->num, F_SETFL, O_NONBLOCK);
2701
        }
2702
2703
        n = recvmsg(b->num, &msg, MSG_PEEK);
2704
2705
        if (is_dry) {
2706
            fcntl(b->num, F_SETFL, sockflags);
2707
        }
2708
2709
        if (n <= 0) {
2710
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2711
                && (get_last_socket_error() != EWOULDBLOCK))
2712
                return -1;
2713
            else
2714
                return is_dry;
2715
        }
2716
    }
2717
2718
    /* read anything else */
2719
    return is_dry;
2720
}
2721
2722
int BIO_dgram_sctp_msg_waiting(BIO *b)
2723
{
2724
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
2725
}
2726
2727
static int dgram_sctp_msg_waiting(BIO *b)
2728
{
2729
    int n, sockflags;
2730
    union sctp_notification snp;
2731
    struct msghdr msg;
2732
    struct iovec iov;
2733
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2734
2735
    /* Check if there are any messages waiting to be read */
2736
    do {
2737
        memset(&snp, 0, sizeof(snp));
2738
        iov.iov_base = (char *)&snp;
2739
        iov.iov_len = sizeof(union sctp_notification);
2740
        msg.msg_name = NULL;
2741
        msg.msg_namelen = 0;
2742
        msg.msg_iov = &iov;
2743
        msg.msg_iovlen = 1;
2744
        msg.msg_control = NULL;
2745
        msg.msg_controllen = 0;
2746
        msg.msg_flags = 0;
2747
2748
        sockflags = fcntl(b->num, F_GETFL, 0);
2749
        fcntl(b->num, F_SETFL, O_NONBLOCK);
2750
        n = recvmsg(b->num, &msg, MSG_PEEK);
2751
        fcntl(b->num, F_SETFL, sockflags);
2752
2753
        /* if notification, process and try again */
2754
        if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
2755
#  ifdef SCTP_AUTHENTICATION_EVENT
2756
            if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2757
                dgram_sctp_handle_auth_free_key_event(b, &snp);
2758
#  endif
2759
2760
            memset(&snp, 0, sizeof(snp));
2761
            iov.iov_base = (char *)&snp;
2762
            iov.iov_len = sizeof(union sctp_notification);
2763
            msg.msg_name = NULL;
2764
            msg.msg_namelen = 0;
2765
            msg.msg_iov = &iov;
2766
            msg.msg_iovlen = 1;
2767
            msg.msg_control = NULL;
2768
            msg.msg_controllen = 0;
2769
            msg.msg_flags = 0;
2770
            n = recvmsg(b->num, &msg, 0);
2771
2772
            if (data->handle_notifications != NULL)
2773
                data->handle_notifications(b, data->notification_context,
2774
                                           (void *)&snp);
2775
        }
2776
2777
    } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
2778
2779
    /* Return 1 if there is a message to be read, return 0 otherwise. */
2780
    if (n > 0)
2781
        return 1;
2782
    else
2783
        return 0;
2784
}
2785
2786
static int dgram_sctp_puts(BIO *bp, const char *str)
2787
{
2788
    int n, ret;
2789
2790
    n = strlen(str);
2791
    ret = dgram_sctp_write(bp, str, n);
2792
    return ret;
2793
}
2794
# endif
2795
2796
static int BIO_dgram_should_retry(int i)
2797
0
{
2798
0
    int err;
2799
2800
0
    if ((i == 0) || (i == -1)) {
2801
0
        err = get_last_socket_error();
2802
2803
# if defined(OPENSSL_SYS_WINDOWS)
2804
        /*
2805
         * If the socket return value (i) is -1 and err is unexpectedly 0 at
2806
         * this point, the error code was overwritten by another system call
2807
         * before this error handling is called.
2808
         */
2809
# endif
2810
2811
0
        return BIO_dgram_non_fatal_error(err);
2812
0
    }
2813
0
    return 0;
2814
0
}
2815
2816
int BIO_dgram_non_fatal_error(int err)
2817
0
{
2818
0
    switch (err) {
2819
# if defined(OPENSSL_SYS_WINDOWS)
2820
#  if defined(WSAEWOULDBLOCK)
2821
    case WSAEWOULDBLOCK:
2822
#  endif
2823
# endif
2824
2825
0
# ifdef EWOULDBLOCK
2826
#  ifdef WSAEWOULDBLOCK
2827
#   if WSAEWOULDBLOCK != EWOULDBLOCK
2828
    case EWOULDBLOCK:
2829
#   endif
2830
#  else
2831
0
    case EWOULDBLOCK:
2832
0
#  endif
2833
0
# endif
2834
2835
0
# ifdef EINTR
2836
0
    case EINTR:
2837
0
# endif
2838
2839
0
# ifdef EAGAIN
2840
#  if EWOULDBLOCK != EAGAIN
2841
    case EAGAIN:
2842
#  endif
2843
0
# endif
2844
2845
0
# ifdef EPROTO
2846
0
    case EPROTO:
2847
0
# endif
2848
2849
0
# ifdef EINPROGRESS
2850
0
    case EINPROGRESS:
2851
0
# endif
2852
2853
0
# ifdef EALREADY
2854
0
    case EALREADY:
2855
0
# endif
2856
2857
0
        return 1;
2858
0
    default:
2859
0
        break;
2860
0
    }
2861
0
    return 0;
2862
0
}
2863
2864
#endif