Coverage Report

Created: 2025-10-28 06:56

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