Coverage Report

Created: 2025-07-11 06:57

/src/openssl/crypto/bio/bss_dgram.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2024 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_dgram_data *data)
468
0
{
469
0
    long ret;
470
471
0
    switch (BIO_ADDR_family(&data->peer)) {
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
0
            if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
484
0
                && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
485
                /*
486
                 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
487
                 */
488
0
                ret = 28;
489
0
            else
490
0
#  endif
491
            /*
492
             * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
493
             */
494
0
            ret = 48;
495
0
        }
496
0
        break;
497
0
# endif
498
0
    default:
499
        /* We don't know. Go with the historical default */
500
0
        ret = 28;
501
0
        break;
502
0
    }
503
0
    return ret;
504
0
}
505
506
/* Enables appropriate destination address reception option on the socket. */
507
# if defined(SUPPORT_LOCAL_ADDR)
508
0
static int enable_local_addr(BIO *b, int enable) {
509
0
    int af = dgram_get_sock_family(b);
510
511
0
    if (af == AF_INET) {
512
0
#  if defined(IP_PKTINFO)
513
        /* IP_PKTINFO is preferred */
514
0
        if (setsockopt(b->num, IPPROTO_IP, IP_PKTINFO,
515
0
                       (void *)&enable, sizeof(enable)) < 0)
516
0
            return 0;
517
518
0
        return 1;
519
520
#  elif defined(IP_RECVDSTADDR)
521
        /* Fall back to IP_RECVDSTADDR */
522
523
        if (setsockopt(b->num, IPPROTO_IP, IP_RECVDSTADDR,
524
                       &enable, sizeof(enable)) < 0)
525
            return 0;
526
527
        return 1;
528
#  endif
529
0
    }
530
531
0
#  if OPENSSL_USE_IPV6
532
0
    if (af == AF_INET6) {
533
0
#   if defined(IPV6_RECVPKTINFO)
534
0
        if (setsockopt(b->num, IPPROTO_IPV6, IPV6_RECVPKTINFO,
535
0
                       &enable, sizeof(enable)) < 0)
536
0
            return 0;
537
538
0
        return 1;
539
0
#   endif
540
0
    }
541
0
#  endif
542
543
0
    return 0;
544
0
}
545
# endif
546
547
static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
548
0
{
549
0
    long ret = 1;
550
0
    int *ip;
551
0
    bio_dgram_data *data = NULL;
552
0
# ifndef __DJGPP__
553
    /* There are currently no cases where this is used on djgpp/watt32. */
554
0
    int sockopt_val = 0;
555
0
# endif
556
0
    int d_errno;
557
0
# if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
558
0
    socklen_t sockopt_len;      /* assume that system supporting IP_MTU is
559
                                 * modern enough to define socklen_t */
560
0
    socklen_t addr_len;
561
0
    BIO_ADDR addr;
562
0
# endif
563
0
    struct sockaddr_storage ss;
564
0
    socklen_t ss_len = sizeof(ss);
565
566
0
    data = (bio_dgram_data *)b->ptr;
567
568
0
    switch (cmd) {
569
0
    case BIO_CTRL_RESET:
570
0
        num = 0;
571
0
        ret = 0;
572
0
        break;
573
0
    case BIO_CTRL_INFO:
574
0
        ret = 0;
575
0
        break;
576
0
    case BIO_C_SET_FD:
577
0
        dgram_clear(b);
578
0
        b->num = *((int *)ptr);
579
0
        b->shutdown = (int)num;
580
0
        b->init = 1;
581
0
        dgram_update_local_addr(b);
582
0
        if (getpeername(b->num, (struct sockaddr *)&ss, &ss_len) == 0) {
583
0
            BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)&ss));
584
0
            data->connected = 1;
585
0
        }
586
0
# if defined(SUPPORT_LOCAL_ADDR)
587
0
        if (data->local_addr_enabled) {
588
0
            if (enable_local_addr(b, 1) < 1)
589
0
                data->local_addr_enabled = 0;
590
0
        }
591
0
# endif
592
0
        break;
593
0
    case BIO_C_GET_FD:
594
0
        if (b->init) {
595
0
            ip = (int *)ptr;
596
0
            if (ip != NULL)
597
0
                *ip = b->num;
598
0
            ret = b->num;
599
0
        } else
600
0
            ret = -1;
601
0
        break;
602
0
    case BIO_CTRL_GET_CLOSE:
603
0
        ret = b->shutdown;
604
0
        break;
605
0
    case BIO_CTRL_SET_CLOSE:
606
0
        b->shutdown = (int)num;
607
0
        break;
608
0
    case BIO_CTRL_PENDING:
609
0
    case BIO_CTRL_WPENDING:
610
0
        ret = 0;
611
0
        break;
612
0
    case BIO_CTRL_DUP:
613
0
    case BIO_CTRL_FLUSH:
614
0
        ret = 1;
615
0
        break;
616
0
    case BIO_CTRL_DGRAM_CONNECT:
617
0
        BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
618
0
        break;
619
        /* (Linux)kernel sets DF bit on outgoing IP packets */
620
0
    case BIO_CTRL_DGRAM_MTU_DISCOVER:
621
0
# if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
622
0
        addr_len = (socklen_t) sizeof(addr);
623
0
        BIO_ADDR_clear(&addr);
624
0
        if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
625
0
            ret = 0;
626
0
            break;
627
0
        }
628
0
        switch (addr.sa.sa_family) {
629
0
        case AF_INET:
630
0
            sockopt_val = IP_PMTUDISC_DO;
631
0
            if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
632
0
                                  &sockopt_val, sizeof(sockopt_val))) < 0)
633
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
634
0
                               "calling setsockopt()");
635
0
            break;
636
0
#  if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
637
0
        case AF_INET6:
638
0
            sockopt_val = IPV6_PMTUDISC_DO;
639
0
            if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
640
0
                                  &sockopt_val, sizeof(sockopt_val))) < 0)
641
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
642
0
                               "calling setsockopt()");
643
0
            break;
644
0
#  endif
645
0
        default:
646
0
            ret = -1;
647
0
            break;
648
0
        }
649
# else
650
        ret = -1;
651
# endif
652
0
        break;
653
0
    case BIO_CTRL_DGRAM_QUERY_MTU:
654
0
# if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
655
0
        addr_len = (socklen_t) sizeof(addr);
656
0
        BIO_ADDR_clear(&addr);
657
0
        if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
658
0
            ret = 0;
659
0
            break;
660
0
        }
661
0
        sockopt_len = sizeof(sockopt_val);
662
0
        switch (addr.sa.sa_family) {
663
0
        case AF_INET:
664
0
            if ((ret =
665
0
                 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
666
0
                            &sockopt_len)) < 0 || sockopt_val < 0) {
667
0
                ret = 0;
668
0
            } else {
669
                /*
670
                 * we assume that the transport protocol is UDP and no IP
671
                 * options are used.
672
                 */
673
0
                data->mtu = sockopt_val - 8 - 20;
674
0
                ret = data->mtu;
675
0
            }
676
0
            break;
677
0
#  if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
678
0
        case AF_INET6:
679
0
            if ((ret =
680
0
                 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
681
0
                            (void *)&sockopt_val, &sockopt_len)) < 0
682
0
                || sockopt_val < 0) {
683
0
                ret = 0;
684
0
            } else {
685
                /*
686
                 * we assume that the transport protocol is UDP and no IPV6
687
                 * options are used.
688
                 */
689
0
                data->mtu = sockopt_val - 8 - 40;
690
0
                ret = data->mtu;
691
0
            }
692
0
            break;
693
0
#  endif
694
0
        default:
695
0
            ret = 0;
696
0
            break;
697
0
        }
698
# else
699
        ret = 0;
700
# endif
701
0
        break;
702
0
    case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
703
0
        ret = -dgram_get_mtu_overhead(data);
704
0
        switch (BIO_ADDR_family(&data->peer)) {
705
0
        case AF_INET:
706
0
            ret += 576;
707
0
            break;
708
0
# if OPENSSL_USE_IPV6
709
0
        case AF_INET6:
710
0
            {
711
0
#  ifdef IN6_IS_ADDR_V4MAPPED
712
0
                struct in6_addr tmp_addr;
713
0
                if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
714
0
                    && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
715
0
                    ret += 576;
716
0
                else
717
0
#  endif
718
0
                    ret += 1280;
719
0
            }
720
0
            break;
721
0
# endif
722
0
        default:
723
0
            ret += 576;
724
0
            break;
725
0
        }
726
0
        break;
727
0
    case BIO_CTRL_DGRAM_GET_MTU:
728
0
        return data->mtu;
729
0
    case BIO_CTRL_DGRAM_SET_MTU:
730
0
        data->mtu = num;
731
0
        ret = num;
732
0
        break;
733
0
    case BIO_CTRL_DGRAM_SET_CONNECTED:
734
0
        if (ptr != NULL) {
735
0
            data->connected = 1;
736
0
            BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
737
0
        } else {
738
0
            data->connected = 0;
739
0
            BIO_ADDR_clear(&data->peer);
740
0
        }
741
0
        break;
742
0
    case BIO_CTRL_DGRAM_GET_PEER:
743
0
        ret = BIO_ADDR_sockaddr_size(&data->peer);
744
        /* FIXME: if num < ret, we will only return part of an address.
745
           That should bee an error, no? */
746
0
        if (num == 0 || num > ret)
747
0
            num = ret;
748
0
        memcpy(ptr, &data->peer, (ret = num));
749
0
        break;
750
0
    case BIO_CTRL_DGRAM_SET_PEER:
751
0
        BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
752
0
        break;
753
0
    case BIO_CTRL_DGRAM_DETECT_PEER_ADDR:
754
0
        {
755
0
            BIO_ADDR xaddr, *p = &data->peer;
756
0
            socklen_t xaddr_len = sizeof(xaddr.sa);
757
758
0
            if (BIO_ADDR_family(p) == AF_UNSPEC) {
759
0
                if (getpeername(b->num, (void *)&xaddr.sa, &xaddr_len) == 0
760
0
                    && BIO_ADDR_family(&xaddr) != AF_UNSPEC) {
761
0
                    p = &xaddr;
762
0
                } else {
763
0
                    ret = 0;
764
0
                    break;
765
0
                }
766
0
            }
767
768
0
            ret = BIO_ADDR_sockaddr_size(p);
769
0
            if (num == 0 || num > ret)
770
0
                num = ret;
771
772
0
            memcpy(ptr, p, (ret = num));
773
0
        }
774
0
        break;
775
0
    case BIO_C_SET_NBIO:
776
0
        if (!BIO_socket_nbio(b->num, num != 0))
777
0
            ret = 0;
778
0
        break;
779
0
    case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
780
0
        data->next_timeout = ossl_time_from_timeval(*(struct timeval *)ptr);
781
0
        break;
782
0
# if defined(SO_RCVTIMEO)
783
0
    case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
784
#  ifdef OPENSSL_SYS_WINDOWS
785
        {
786
            struct timeval *tv = (struct timeval *)ptr;
787
            int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
788
789
            if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
790
                                  (void *)&timeout, sizeof(timeout))) < 0)
791
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
792
                               "calling setsockopt()");
793
        }
794
#  else
795
0
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
796
0
                              sizeof(struct timeval))) < 0)
797
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
798
0
                           "calling setsockopt()");
799
0
#  endif
800
0
        break;
801
0
    case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
802
0
        {
803
#  ifdef OPENSSL_SYS_WINDOWS
804
            int sz = 0;
805
            int timeout;
806
            struct timeval *tv = (struct timeval *)ptr;
807
808
            sz = sizeof(timeout);
809
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
810
                                  (void *)&timeout, &sz)) < 0) {
811
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
812
                               "calling getsockopt()");
813
            } else {
814
                tv->tv_sec = timeout / 1000;
815
                tv->tv_usec = (timeout % 1000) * 1000;
816
                ret = sizeof(*tv);
817
            }
818
#  else
819
0
            socklen_t sz = sizeof(struct timeval);
820
0
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
821
0
                                  ptr, &sz)) < 0) {
822
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
823
0
                               "calling getsockopt()");
824
0
            } else {
825
0
                OPENSSL_assert((size_t)sz <= sizeof(struct timeval));
826
0
                ret = (int)sz;
827
0
            }
828
0
#  endif
829
0
        }
830
0
        break;
831
0
# endif
832
0
# if defined(SO_SNDTIMEO)
833
0
    case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
834
#  ifdef OPENSSL_SYS_WINDOWS
835
        {
836
            struct timeval *tv = (struct timeval *)ptr;
837
            int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
838
839
            if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
840
                                  (void *)&timeout, sizeof(timeout))) < 0)
841
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
842
                               "calling setsockopt()");
843
        }
844
#  else
845
0
        if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
846
0
                              sizeof(struct timeval))) < 0)
847
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
848
0
                           "calling setsockopt()");
849
0
#  endif
850
0
        break;
851
0
    case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
852
0
        {
853
#  ifdef OPENSSL_SYS_WINDOWS
854
            int sz = 0;
855
            int timeout;
856
            struct timeval *tv = (struct timeval *)ptr;
857
858
            sz = sizeof(timeout);
859
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
860
                                  (void *)&timeout, &sz)) < 0) {
861
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
862
                               "calling getsockopt()");
863
            } else {
864
                tv->tv_sec = timeout / 1000;
865
                tv->tv_usec = (timeout % 1000) * 1000;
866
                ret = sizeof(*tv);
867
            }
868
#  else
869
0
            socklen_t sz = sizeof(struct timeval);
870
871
0
            if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
872
0
                                  ptr, &sz)) < 0) {
873
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
874
0
                               "calling getsockopt()");
875
0
            } else {
876
0
                OPENSSL_assert((size_t)sz <= sizeof(struct timeval));
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_MTUDISCOVER)
943
            sockopt_val = num ? IP_PMTUDISC_PROBE : IP_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);
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(msg->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
        return 0;
1746
    }
1747
1748
    msg[0].data_len = ret;
1749
    msg[0].flags    = 0;
1750
    *num_processed = 1;
1751
    return 1;
1752
1753
# else
1754
    ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
1755
    *num_processed = 0;
1756
    return 0;
1757
# endif
1758
0
}
1759
1760
# ifndef OPENSSL_NO_SCTP
1761
const BIO_METHOD *BIO_s_datagram_sctp(void)
1762
{
1763
    return &methods_dgramp_sctp;
1764
}
1765
1766
BIO *BIO_new_dgram_sctp(int fd, int close_flag)
1767
{
1768
    BIO *bio;
1769
    int ret, optval = 20000;
1770
    int auth_data = 0, auth_forward = 0;
1771
    unsigned char *p;
1772
    struct sctp_authchunk auth;
1773
    struct sctp_authchunks *authchunks;
1774
    socklen_t sockopt_len;
1775
#  ifdef SCTP_AUTHENTICATION_EVENT
1776
#   ifdef SCTP_EVENT
1777
    struct sctp_event event;
1778
#   else
1779
    struct sctp_event_subscribe event;
1780
#   endif
1781
#  endif
1782
1783
    bio = BIO_new(BIO_s_datagram_sctp());
1784
    if (bio == NULL)
1785
        return NULL;
1786
    BIO_set_fd(bio, fd, close_flag);
1787
1788
    /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
1789
    auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
1790
    ret =
1791
        setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1792
                   sizeof(struct sctp_authchunk));
1793
    if (ret < 0) {
1794
        BIO_vfree(bio);
1795
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1796
                       "Ensure SCTP AUTH chunks are enabled in kernel");
1797
        return NULL;
1798
    }
1799
    auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
1800
    ret =
1801
        setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1802
                   sizeof(struct sctp_authchunk));
1803
    if (ret < 0) {
1804
        BIO_vfree(bio);
1805
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1806
                       "Ensure SCTP AUTH chunks are enabled in kernel");
1807
        return NULL;
1808
    }
1809
1810
    /*
1811
     * Test if activation was successful. When using accept(), SCTP-AUTH has
1812
     * to be activated for the listening socket already, otherwise the
1813
     * connected socket won't use it. Similarly with connect(): the socket
1814
     * prior to connection must be activated for SCTP-AUTH
1815
     */
1816
    sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1817
    authchunks = OPENSSL_zalloc(sockopt_len);
1818
    if (authchunks == NULL) {
1819
        BIO_vfree(bio);
1820
        return NULL;
1821
    }
1822
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1823
                   &sockopt_len);
1824
    if (ret < 0) {
1825
        OPENSSL_free(authchunks);
1826
        BIO_vfree(bio);
1827
        return NULL;
1828
    }
1829
1830
    for (p = (unsigned char *)authchunks->gauth_chunks;
1831
         p < (unsigned char *)authchunks + sockopt_len;
1832
         p += sizeof(uint8_t)) {
1833
        if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1834
            auth_data = 1;
1835
        if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1836
            auth_forward = 1;
1837
    }
1838
1839
    OPENSSL_free(authchunks);
1840
1841
    if (!auth_data || !auth_forward) {
1842
        BIO_vfree(bio);
1843
        ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
1844
                       "Ensure SCTP AUTH chunks are enabled on the "
1845
                       "underlying socket");
1846
        return NULL;
1847
    }
1848
1849
#  ifdef SCTP_AUTHENTICATION_EVENT
1850
#   ifdef SCTP_EVENT
1851
    memset(&event, 0, sizeof(event));
1852
    event.se_assoc_id = 0;
1853
    event.se_type = SCTP_AUTHENTICATION_EVENT;
1854
    event.se_on = 1;
1855
    ret =
1856
        setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1857
                   sizeof(struct sctp_event));
1858
    if (ret < 0) {
1859
        BIO_vfree(bio);
1860
        return NULL;
1861
    }
1862
#   else
1863
    sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1864
    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1865
    if (ret < 0) {
1866
        BIO_vfree(bio);
1867
        return NULL;
1868
    }
1869
1870
    event.sctp_authentication_event = 1;
1871
1872
    ret =
1873
        setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1874
                   sizeof(struct sctp_event_subscribe));
1875
    if (ret < 0) {
1876
        BIO_vfree(bio);
1877
        return NULL;
1878
    }
1879
#   endif
1880
#  endif
1881
1882
    /*
1883
     * Disable partial delivery by setting the min size larger than the max
1884
     * record size of 2^14 + 2048 + 13
1885
     */
1886
    ret =
1887
        setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1888
                   sizeof(optval));
1889
    if (ret < 0) {
1890
        BIO_vfree(bio);
1891
        return NULL;
1892
    }
1893
1894
    return bio;
1895
}
1896
1897
int BIO_dgram_is_sctp(BIO *bio)
1898
{
1899
    return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1900
}
1901
1902
static int dgram_sctp_new(BIO *bi)
1903
{
1904
    bio_dgram_sctp_data *data = NULL;
1905
1906
    bi->init = 0;
1907
    bi->num = 0;
1908
    if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
1909
        return 0;
1910
#  ifdef SCTP_PR_SCTP_NONE
1911
    data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1912
#  endif
1913
    bi->ptr = data;
1914
1915
    bi->flags = 0;
1916
    return 1;
1917
}
1918
1919
static int dgram_sctp_free(BIO *a)
1920
{
1921
    bio_dgram_sctp_data *data;
1922
1923
    if (a == NULL)
1924
        return 0;
1925
    if (!dgram_clear(a))
1926
        return 0;
1927
1928
    data = (bio_dgram_sctp_data *) a->ptr;
1929
    if (data != NULL)
1930
        OPENSSL_free(data);
1931
1932
    return 1;
1933
}
1934
1935
#  ifdef SCTP_AUTHENTICATION_EVENT
1936
void dgram_sctp_handle_auth_free_key_event(BIO *b,
1937
                                           union sctp_notification *snp)
1938
{
1939
    int ret;
1940
    struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1941
1942
    if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1943
        struct sctp_authkeyid authkeyid;
1944
1945
        /* delete key */
1946
        authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1947
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1948
                         &authkeyid, sizeof(struct sctp_authkeyid));
1949
    }
1950
}
1951
#  endif
1952
1953
static int dgram_sctp_read(BIO *b, char *out, int outl)
1954
{
1955
    int ret = 0, n = 0, i, optval;
1956
    socklen_t optlen;
1957
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1958
    struct msghdr msg;
1959
    struct iovec iov;
1960
    struct cmsghdr *cmsg;
1961
    char cmsgbuf[512];
1962
1963
    if (out != NULL) {
1964
        clear_socket_error();
1965
1966
        do {
1967
            memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1968
            iov.iov_base = out;
1969
            iov.iov_len = outl;
1970
            msg.msg_name = NULL;
1971
            msg.msg_namelen = 0;
1972
            msg.msg_iov = &iov;
1973
            msg.msg_iovlen = 1;
1974
            msg.msg_control = cmsgbuf;
1975
            msg.msg_controllen = 512;
1976
            msg.msg_flags = 0;
1977
            n = recvmsg(b->num, &msg, 0);
1978
1979
            if (n <= 0) {
1980
                if (n < 0)
1981
                    ret = n;
1982
                break;
1983
            }
1984
1985
            if (msg.msg_controllen > 0) {
1986
                for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1987
                     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1988
                    if (cmsg->cmsg_level != IPPROTO_SCTP)
1989
                        continue;
1990
#  ifdef SCTP_RCVINFO
1991
                    if (cmsg->cmsg_type == SCTP_RCVINFO) {
1992
                        struct sctp_rcvinfo *rcvinfo;
1993
1994
                        rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1995
                        data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1996
                        data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1997
                        data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1998
                        data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1999
                        data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
2000
                        data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
2001
                        data->rcvinfo.rcv_context = rcvinfo->rcv_context;
2002
                    }
2003
#  endif
2004
#  ifdef SCTP_SNDRCV
2005
                    if (cmsg->cmsg_type == SCTP_SNDRCV) {
2006
                        struct sctp_sndrcvinfo *sndrcvinfo;
2007
2008
                        sndrcvinfo =
2009
                            (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2010
                        data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
2011
                        data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
2012
                        data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
2013
                        data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
2014
                        data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
2015
                        data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
2016
                        data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
2017
                    }
2018
#  endif
2019
                }
2020
            }
2021
2022
            if (msg.msg_flags & MSG_NOTIFICATION) {
2023
                union sctp_notification snp;
2024
2025
                memcpy(&snp, out, sizeof(snp));
2026
                if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2027
#  ifdef SCTP_EVENT
2028
                    struct sctp_event event;
2029
#  else
2030
                    struct sctp_event_subscribe event;
2031
                    socklen_t eventsize;
2032
#  endif
2033
2034
                    /* disable sender dry event */
2035
#  ifdef SCTP_EVENT
2036
                    memset(&event, 0, sizeof(event));
2037
                    event.se_assoc_id = 0;
2038
                    event.se_type = SCTP_SENDER_DRY_EVENT;
2039
                    event.se_on = 0;
2040
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2041
                                   sizeof(struct sctp_event));
2042
                    if (i < 0) {
2043
                        ret = i;
2044
                        break;
2045
                    }
2046
#  else
2047
                    eventsize = sizeof(struct sctp_event_subscribe);
2048
                    i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2049
                                   &eventsize);
2050
                    if (i < 0) {
2051
                        ret = i;
2052
                        break;
2053
                    }
2054
2055
                    event.sctp_sender_dry_event = 0;
2056
2057
                    i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2058
                                   sizeof(struct sctp_event_subscribe));
2059
                    if (i < 0) {
2060
                        ret = i;
2061
                        break;
2062
                    }
2063
#  endif
2064
                }
2065
#  ifdef SCTP_AUTHENTICATION_EVENT
2066
                if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2067
                    dgram_sctp_handle_auth_free_key_event(b, &snp);
2068
#  endif
2069
2070
                if (data->handle_notifications != NULL)
2071
                    data->handle_notifications(b, data->notification_context,
2072
                                               (void *)out);
2073
2074
                memset(&snp, 0, sizeof(snp));
2075
                memset(out, 0, outl);
2076
            } else {
2077
                ret += n;
2078
            }
2079
        }
2080
        while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
2081
               && (ret < outl));
2082
2083
        if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
2084
            /* Partial message read, this should never happen! */
2085
2086
            /*
2087
             * The buffer was too small, this means the peer sent a message
2088
             * that was larger than allowed.
2089
             */
2090
            if (ret == outl)
2091
                return -1;
2092
2093
            /*
2094
             * Test if socket buffer can handle max record size (2^14 + 2048
2095
             * + 13)
2096
             */
2097
            optlen = (socklen_t) sizeof(int);
2098
            ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
2099
            if (ret >= 0)
2100
                OPENSSL_assert(optval >= 18445);
2101
2102
            /*
2103
             * Test if SCTP doesn't partially deliver below max record size
2104
             * (2^14 + 2048 + 13)
2105
             */
2106
            optlen = (socklen_t) sizeof(int);
2107
            ret =
2108
                getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
2109
                           &optval, &optlen);
2110
            if (ret >= 0)
2111
                OPENSSL_assert(optval >= 18445);
2112
2113
            /*
2114
             * Partially delivered notification??? Probably a bug....
2115
             */
2116
            OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
2117
2118
            /*
2119
             * Everything seems ok till now, so it's most likely a message
2120
             * dropped by PR-SCTP.
2121
             */
2122
            memset(out, 0, outl);
2123
            BIO_set_retry_read(b);
2124
            return -1;
2125
        }
2126
2127
        BIO_clear_retry_flags(b);
2128
        if (ret < 0) {
2129
            if (BIO_dgram_should_retry(ret)) {
2130
                BIO_set_retry_read(b);
2131
                data->dgram._errno = get_last_socket_error();
2132
            }
2133
        }
2134
2135
        /* Test if peer uses SCTP-AUTH before continuing */
2136
        if (!data->peer_auth_tested) {
2137
            int ii, auth_data = 0, auth_forward = 0;
2138
            unsigned char *p;
2139
            struct sctp_authchunks *authchunks;
2140
2141
            optlen =
2142
                (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
2143
            authchunks = OPENSSL_malloc(optlen);
2144
            if (authchunks == NULL)
2145
                return -1;
2146
            memset(authchunks, 0, optlen);
2147
            ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
2148
                            authchunks, &optlen);
2149
2150
            if (ii >= 0)
2151
                for (p = (unsigned char *)authchunks->gauth_chunks;
2152
                     p < (unsigned char *)authchunks + optlen;
2153
                     p += sizeof(uint8_t)) {
2154
                    if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
2155
                        auth_data = 1;
2156
                    if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
2157
                        auth_forward = 1;
2158
                }
2159
2160
            OPENSSL_free(authchunks);
2161
2162
            if (!auth_data || !auth_forward) {
2163
                ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
2164
                return -1;
2165
            }
2166
2167
            data->peer_auth_tested = 1;
2168
        }
2169
    }
2170
    return ret;
2171
}
2172
2173
/*
2174
 * dgram_sctp_write - send message on SCTP socket
2175
 * @b: BIO to write to
2176
 * @in: data to send
2177
 * @inl: amount of bytes in @in to send
2178
 *
2179
 * Returns -1 on error or the sent amount of bytes on success
2180
 */
2181
static int dgram_sctp_write(BIO *b, const char *in, int inl)
2182
{
2183
    int ret;
2184
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2185
    struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
2186
    struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
2187
    struct bio_dgram_sctp_sndinfo handshake_sinfo;
2188
    struct iovec iov[1];
2189
    struct msghdr msg;
2190
    struct cmsghdr *cmsg;
2191
#  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2192
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
2193
                 CMSG_SPACE(sizeof(struct sctp_prinfo))];
2194
    struct sctp_sndinfo *sndinfo;
2195
    struct sctp_prinfo *prinfo;
2196
#  else
2197
    char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
2198
    struct sctp_sndrcvinfo *sndrcvinfo;
2199
#  endif
2200
2201
    clear_socket_error();
2202
2203
    /*
2204
     * If we're send anything else than application data, disable all user
2205
     * parameters and flags.
2206
     */
2207
    if (in[0] != 23) {
2208
        memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
2209
#  ifdef SCTP_SACK_IMMEDIATELY
2210
        handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
2211
#  endif
2212
        sinfo = &handshake_sinfo;
2213
    }
2214
2215
    /* We can only send a shutdown alert if the socket is dry */
2216
    if (data->save_shutdown) {
2217
        ret = BIO_dgram_sctp_wait_for_dry(b);
2218
        if (ret < 0)
2219
            return -1;
2220
        if (ret == 0) {
2221
            BIO_clear_retry_flags(b);
2222
            BIO_set_retry_write(b);
2223
            return -1;
2224
        }
2225
    }
2226
2227
    iov[0].iov_base = (char *)in;
2228
    iov[0].iov_len = inl;
2229
    msg.msg_name = NULL;
2230
    msg.msg_namelen = 0;
2231
    msg.msg_iov = iov;
2232
    msg.msg_iovlen = 1;
2233
    msg.msg_control = (caddr_t) cmsgbuf;
2234
    msg.msg_controllen = 0;
2235
    msg.msg_flags = 0;
2236
#  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
2237
    cmsg = (struct cmsghdr *)cmsgbuf;
2238
    cmsg->cmsg_level = IPPROTO_SCTP;
2239
    cmsg->cmsg_type = SCTP_SNDINFO;
2240
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
2241
    sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
2242
    memset(sndinfo, 0, sizeof(*sndinfo));
2243
    sndinfo->snd_sid = sinfo->snd_sid;
2244
    sndinfo->snd_flags = sinfo->snd_flags;
2245
    sndinfo->snd_ppid = sinfo->snd_ppid;
2246
    sndinfo->snd_context = sinfo->snd_context;
2247
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
2248
2249
    cmsg =
2250
        (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
2251
    cmsg->cmsg_level = IPPROTO_SCTP;
2252
    cmsg->cmsg_type = SCTP_PRINFO;
2253
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
2254
    prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
2255
    memset(prinfo, 0, sizeof(*prinfo));
2256
    prinfo->pr_policy = pinfo->pr_policy;
2257
    prinfo->pr_value = pinfo->pr_value;
2258
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
2259
#  else
2260
    cmsg = (struct cmsghdr *)cmsgbuf;
2261
    cmsg->cmsg_level = IPPROTO_SCTP;
2262
    cmsg->cmsg_type = SCTP_SNDRCV;
2263
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
2264
    sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
2265
    memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
2266
    sndrcvinfo->sinfo_stream = sinfo->snd_sid;
2267
    sndrcvinfo->sinfo_flags = sinfo->snd_flags;
2268
#   ifdef __FreeBSD__
2269
    sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
2270
#   endif
2271
    sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
2272
    sndrcvinfo->sinfo_context = sinfo->snd_context;
2273
    sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
2274
    msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
2275
#  endif
2276
2277
    ret = sendmsg(b->num, &msg, 0);
2278
2279
    BIO_clear_retry_flags(b);
2280
    if (ret <= 0) {
2281
        if (BIO_dgram_should_retry(ret)) {
2282
            BIO_set_retry_write(b);
2283
            data->dgram._errno = get_last_socket_error();
2284
        }
2285
    }
2286
    return ret;
2287
}
2288
2289
static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
2290
{
2291
    long ret = 1;
2292
    bio_dgram_sctp_data *data = NULL;
2293
    socklen_t sockopt_len = 0;
2294
    struct sctp_authkeyid authkeyid;
2295
    struct sctp_authkey *authkey = NULL;
2296
2297
    data = (bio_dgram_sctp_data *) b->ptr;
2298
2299
    switch (cmd) {
2300
    case BIO_CTRL_DGRAM_QUERY_MTU:
2301
        /*
2302
         * Set to maximum (2^14) and ignore user input to enable transport
2303
         * protocol fragmentation. Returns always 2^14.
2304
         */
2305
        data->dgram.mtu = 16384;
2306
        ret = data->dgram.mtu;
2307
        break;
2308
    case BIO_CTRL_DGRAM_SET_MTU:
2309
        /*
2310
         * Set to maximum (2^14) and ignore input to enable transport
2311
         * protocol fragmentation. Returns always 2^14.
2312
         */
2313
        data->dgram.mtu = 16384;
2314
        ret = data->dgram.mtu;
2315
        break;
2316
    case BIO_CTRL_DGRAM_SET_CONNECTED:
2317
    case BIO_CTRL_DGRAM_CONNECT:
2318
        /* Returns always -1. */
2319
        ret = -1;
2320
        break;
2321
    case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
2322
        /*
2323
         * SCTP doesn't need the DTLS timer Returns always 1.
2324
         */
2325
        break;
2326
    case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
2327
        /*
2328
         * We allow transport protocol fragmentation so this is irrelevant
2329
         */
2330
        ret = 0;
2331
        break;
2332
    case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
2333
        if (num > 0)
2334
            data->in_handshake = 1;
2335
        else
2336
            data->in_handshake = 0;
2337
2338
        ret =
2339
            setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
2340
                       &data->in_handshake, sizeof(int));
2341
        break;
2342
    case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
2343
        /*
2344
         * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
2345
         */
2346
2347
        /* Get active key */
2348
        sockopt_len = sizeof(struct sctp_authkeyid);
2349
        ret =
2350
            getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2351
                       &sockopt_len);
2352
        if (ret < 0)
2353
            break;
2354
2355
        /* Add new key */
2356
        sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
2357
        authkey = OPENSSL_malloc(sockopt_len);
2358
        if (authkey == NULL) {
2359
            ret = -1;
2360
            break;
2361
        }
2362
        memset(authkey, 0, sockopt_len);
2363
        authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
2364
#  ifndef __FreeBSD__
2365
        /*
2366
         * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
2367
         * and higher work without it.
2368
         */
2369
        authkey->sca_keylength = 64;
2370
#  endif
2371
        memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
2372
2373
        ret =
2374
            setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
2375
                       sockopt_len);
2376
        OPENSSL_free(authkey);
2377
        authkey = NULL;
2378
        if (ret < 0)
2379
            break;
2380
2381
        /* Reset active key */
2382
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2383
                         &authkeyid, sizeof(struct sctp_authkeyid));
2384
        if (ret < 0)
2385
            break;
2386
2387
        break;
2388
    case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
2389
        /* Returns 0 on success, -1 otherwise. */
2390
2391
        /* Get active key */
2392
        sockopt_len = sizeof(struct sctp_authkeyid);
2393
        ret =
2394
            getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
2395
                       &sockopt_len);
2396
        if (ret < 0)
2397
            break;
2398
2399
        /* Set active key */
2400
        authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
2401
        ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2402
                         &authkeyid, sizeof(struct sctp_authkeyid));
2403
        if (ret < 0)
2404
            break;
2405
2406
        /*
2407
         * CCS has been sent, so remember that and fall through to check if
2408
         * we need to deactivate an old key
2409
         */
2410
        data->ccs_sent = 1;
2411
        /* fall-through */
2412
2413
    case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
2414
        /* Returns 0 on success, -1 otherwise. */
2415
2416
        /*
2417
         * Has this command really been called or is this just a
2418
         * fall-through?
2419
         */
2420
        if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
2421
            data->ccs_rcvd = 1;
2422
2423
        /*
2424
         * CSS has been both, received and sent, so deactivate an old key
2425
         */
2426
        if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
2427
            /* Get active key */
2428
            sockopt_len = sizeof(struct sctp_authkeyid);
2429
            ret =
2430
                getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
2431
                           &authkeyid, &sockopt_len);
2432
            if (ret < 0)
2433
                break;
2434
2435
            /*
2436
             * Deactivate key or delete second last key if
2437
             * SCTP_AUTHENTICATION_EVENT is not available.
2438
             */
2439
            authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2440
#  ifdef SCTP_AUTH_DEACTIVATE_KEY
2441
            sockopt_len = sizeof(struct sctp_authkeyid);
2442
            ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
2443
                             &authkeyid, sockopt_len);
2444
            if (ret < 0)
2445
                break;
2446
#  endif
2447
#  ifndef SCTP_AUTHENTICATION_EVENT
2448
            if (authkeyid.scact_keynumber > 0) {
2449
                authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
2450
                ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
2451
                                 &authkeyid, sizeof(struct sctp_authkeyid));
2452
                if (ret < 0)
2453
                    break;
2454
            }
2455
#  endif
2456
2457
            data->ccs_rcvd = 0;
2458
            data->ccs_sent = 0;
2459
        }
2460
        break;
2461
    case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
2462
        /* Returns the size of the copied struct. */
2463
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2464
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2465
2466
        memcpy(ptr, &(data->sndinfo), num);
2467
        ret = num;
2468
        break;
2469
    case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
2470
        /* Returns the size of the copied struct. */
2471
        if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
2472
            num = sizeof(struct bio_dgram_sctp_sndinfo);
2473
2474
        memcpy(&(data->sndinfo), ptr, num);
2475
        break;
2476
    case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
2477
        /* Returns the size of the copied struct. */
2478
        if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
2479
            num = sizeof(struct bio_dgram_sctp_rcvinfo);
2480
2481
        memcpy(ptr, &data->rcvinfo, num);
2482
2483
        ret = num;
2484
        break;
2485
    case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
2486
        /* Returns the size of the copied struct. */
2487
        if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
2488
            num = sizeof(struct bio_dgram_sctp_rcvinfo);
2489
2490
        memcpy(&(data->rcvinfo), ptr, num);
2491
        break;
2492
    case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
2493
        /* Returns the size of the copied struct. */
2494
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2495
            num = sizeof(struct bio_dgram_sctp_prinfo);
2496
2497
        memcpy(ptr, &(data->prinfo), num);
2498
        ret = num;
2499
        break;
2500
    case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
2501
        /* Returns the size of the copied struct. */
2502
        if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
2503
            num = sizeof(struct bio_dgram_sctp_prinfo);
2504
2505
        memcpy(&(data->prinfo), ptr, num);
2506
        break;
2507
    case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
2508
        /* Returns always 1. */
2509
        if (num > 0)
2510
            data->save_shutdown = 1;
2511
        else
2512
            data->save_shutdown = 0;
2513
        break;
2514
    case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
2515
        return dgram_sctp_wait_for_dry(b);
2516
    case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
2517
        return dgram_sctp_msg_waiting(b);
2518
2519
    default:
2520
        /*
2521
         * Pass to default ctrl function to process SCTP unspecific commands
2522
         */
2523
        ret = dgram_ctrl(b, cmd, num, ptr);
2524
        break;
2525
    }
2526
    return ret;
2527
}
2528
2529
int BIO_dgram_sctp_notification_cb(BIO *b,
2530
                BIO_dgram_sctp_notification_handler_fn handle_notifications,
2531
                void *context)
2532
{
2533
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2534
2535
    if (handle_notifications != NULL) {
2536
        data->handle_notifications = handle_notifications;
2537
        data->notification_context = context;
2538
    } else
2539
        return -1;
2540
2541
    return 0;
2542
}
2543
2544
/*
2545
 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
2546
 * @b: The BIO to check for the dry event
2547
 *
2548
 * Wait until the peer confirms all packets have been received, and so that
2549
 * our kernel doesn't have anything to send anymore.  This is only received by
2550
 * the peer's kernel, not the application.
2551
 *
2552
 * Returns:
2553
 * -1 on error
2554
 *  0 when not dry yet
2555
 *  1 when dry
2556
 */
2557
int BIO_dgram_sctp_wait_for_dry(BIO *b)
2558
{
2559
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
2560
}
2561
2562
static int dgram_sctp_wait_for_dry(BIO *b)
2563
{
2564
    int is_dry = 0;
2565
    int sockflags = 0;
2566
    int n, ret;
2567
    union sctp_notification snp;
2568
    struct msghdr msg;
2569
    struct iovec iov;
2570
#  ifdef SCTP_EVENT
2571
    struct sctp_event event;
2572
#  else
2573
    struct sctp_event_subscribe event;
2574
    socklen_t eventsize;
2575
#  endif
2576
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2577
2578
    /* set sender dry event */
2579
#  ifdef SCTP_EVENT
2580
    memset(&event, 0, sizeof(event));
2581
    event.se_assoc_id = 0;
2582
    event.se_type = SCTP_SENDER_DRY_EVENT;
2583
    event.se_on = 1;
2584
    ret =
2585
        setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2586
                   sizeof(struct sctp_event));
2587
#  else
2588
    eventsize = sizeof(struct sctp_event_subscribe);
2589
    ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
2590
    if (ret < 0)
2591
        return -1;
2592
2593
    event.sctp_sender_dry_event = 1;
2594
2595
    ret =
2596
        setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2597
                   sizeof(struct sctp_event_subscribe));
2598
#  endif
2599
    if (ret < 0)
2600
        return -1;
2601
2602
    /* peek for notification */
2603
    memset(&snp, 0, sizeof(snp));
2604
    iov.iov_base = (char *)&snp;
2605
    iov.iov_len = sizeof(union sctp_notification);
2606
    msg.msg_name = NULL;
2607
    msg.msg_namelen = 0;
2608
    msg.msg_iov = &iov;
2609
    msg.msg_iovlen = 1;
2610
    msg.msg_control = NULL;
2611
    msg.msg_controllen = 0;
2612
    msg.msg_flags = 0;
2613
2614
    n = recvmsg(b->num, &msg, MSG_PEEK);
2615
    if (n <= 0) {
2616
        if ((n < 0) && (get_last_socket_error() != EAGAIN)
2617
            && (get_last_socket_error() != EWOULDBLOCK))
2618
            return -1;
2619
        else
2620
            return 0;
2621
    }
2622
2623
    /* if we find a notification, process it and try again if necessary */
2624
    while (msg.msg_flags & MSG_NOTIFICATION) {
2625
        memset(&snp, 0, sizeof(snp));
2626
        iov.iov_base = (char *)&snp;
2627
        iov.iov_len = sizeof(union sctp_notification);
2628
        msg.msg_name = NULL;
2629
        msg.msg_namelen = 0;
2630
        msg.msg_iov = &iov;
2631
        msg.msg_iovlen = 1;
2632
        msg.msg_control = NULL;
2633
        msg.msg_controllen = 0;
2634
        msg.msg_flags = 0;
2635
2636
        n = recvmsg(b->num, &msg, 0);
2637
        if (n <= 0) {
2638
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2639
                && (get_last_socket_error() != EWOULDBLOCK))
2640
                return -1;
2641
            else
2642
                return is_dry;
2643
        }
2644
2645
        if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
2646
            is_dry = 1;
2647
2648
            /* disable sender dry event */
2649
#  ifdef SCTP_EVENT
2650
            memset(&event, 0, sizeof(event));
2651
            event.se_assoc_id = 0;
2652
            event.se_type = SCTP_SENDER_DRY_EVENT;
2653
            event.se_on = 0;
2654
            ret =
2655
                setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
2656
                           sizeof(struct sctp_event));
2657
#  else
2658
            eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
2659
            ret =
2660
                getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2661
                           &eventsize);
2662
            if (ret < 0)
2663
                return -1;
2664
2665
            event.sctp_sender_dry_event = 0;
2666
2667
            ret =
2668
                setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
2669
                           sizeof(struct sctp_event_subscribe));
2670
#  endif
2671
            if (ret < 0)
2672
                return -1;
2673
        }
2674
#  ifdef SCTP_AUTHENTICATION_EVENT
2675
        if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2676
            dgram_sctp_handle_auth_free_key_event(b, &snp);
2677
#  endif
2678
2679
        if (data->handle_notifications != NULL)
2680
            data->handle_notifications(b, data->notification_context,
2681
                                       (void *)&snp);
2682
2683
        /* found notification, peek again */
2684
        memset(&snp, 0, sizeof(snp));
2685
        iov.iov_base = (char *)&snp;
2686
        iov.iov_len = sizeof(union sctp_notification);
2687
        msg.msg_name = NULL;
2688
        msg.msg_namelen = 0;
2689
        msg.msg_iov = &iov;
2690
        msg.msg_iovlen = 1;
2691
        msg.msg_control = NULL;
2692
        msg.msg_controllen = 0;
2693
        msg.msg_flags = 0;
2694
2695
        /* if we have seen the dry already, don't wait */
2696
        if (is_dry) {
2697
            sockflags = fcntl(b->num, F_GETFL, 0);
2698
            fcntl(b->num, F_SETFL, O_NONBLOCK);
2699
        }
2700
2701
        n = recvmsg(b->num, &msg, MSG_PEEK);
2702
2703
        if (is_dry) {
2704
            fcntl(b->num, F_SETFL, sockflags);
2705
        }
2706
2707
        if (n <= 0) {
2708
            if ((n < 0) && (get_last_socket_error() != EAGAIN)
2709
                && (get_last_socket_error() != EWOULDBLOCK))
2710
                return -1;
2711
            else
2712
                return is_dry;
2713
        }
2714
    }
2715
2716
    /* read anything else */
2717
    return is_dry;
2718
}
2719
2720
int BIO_dgram_sctp_msg_waiting(BIO *b)
2721
{
2722
    return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
2723
}
2724
2725
static int dgram_sctp_msg_waiting(BIO *b)
2726
{
2727
    int n, sockflags;
2728
    union sctp_notification snp;
2729
    struct msghdr msg;
2730
    struct iovec iov;
2731
    bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
2732
2733
    /* Check if there are any messages waiting to be read */
2734
    do {
2735
        memset(&snp, 0, sizeof(snp));
2736
        iov.iov_base = (char *)&snp;
2737
        iov.iov_len = sizeof(union sctp_notification);
2738
        msg.msg_name = NULL;
2739
        msg.msg_namelen = 0;
2740
        msg.msg_iov = &iov;
2741
        msg.msg_iovlen = 1;
2742
        msg.msg_control = NULL;
2743
        msg.msg_controllen = 0;
2744
        msg.msg_flags = 0;
2745
2746
        sockflags = fcntl(b->num, F_GETFL, 0);
2747
        fcntl(b->num, F_SETFL, O_NONBLOCK);
2748
        n = recvmsg(b->num, &msg, MSG_PEEK);
2749
        fcntl(b->num, F_SETFL, sockflags);
2750
2751
        /* if notification, process and try again */
2752
        if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
2753
#  ifdef SCTP_AUTHENTICATION_EVENT
2754
            if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
2755
                dgram_sctp_handle_auth_free_key_event(b, &snp);
2756
#  endif
2757
2758
            memset(&snp, 0, sizeof(snp));
2759
            iov.iov_base = (char *)&snp;
2760
            iov.iov_len = sizeof(union sctp_notification);
2761
            msg.msg_name = NULL;
2762
            msg.msg_namelen = 0;
2763
            msg.msg_iov = &iov;
2764
            msg.msg_iovlen = 1;
2765
            msg.msg_control = NULL;
2766
            msg.msg_controllen = 0;
2767
            msg.msg_flags = 0;
2768
            n = recvmsg(b->num, &msg, 0);
2769
2770
            if (data->handle_notifications != NULL)
2771
                data->handle_notifications(b, data->notification_context,
2772
                                           (void *)&snp);
2773
        }
2774
2775
    } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
2776
2777
    /* Return 1 if there is a message to be read, return 0 otherwise. */
2778
    if (n > 0)
2779
        return 1;
2780
    else
2781
        return 0;
2782
}
2783
2784
static int dgram_sctp_puts(BIO *bp, const char *str)
2785
{
2786
    int n, ret;
2787
2788
    n = strlen(str);
2789
    ret = dgram_sctp_write(bp, str, n);
2790
    return ret;
2791
}
2792
# endif
2793
2794
static int BIO_dgram_should_retry(int i)
2795
0
{
2796
0
    int err;
2797
2798
0
    if ((i == 0) || (i == -1)) {
2799
0
        err = get_last_socket_error();
2800
2801
# if defined(OPENSSL_SYS_WINDOWS)
2802
        /*
2803
         * If the socket return value (i) is -1 and err is unexpectedly 0 at
2804
         * this point, the error code was overwritten by another system call
2805
         * before this error handling is called.
2806
         */
2807
# endif
2808
2809
0
        return BIO_dgram_non_fatal_error(err);
2810
0
    }
2811
0
    return 0;
2812
0
}
2813
2814
int BIO_dgram_non_fatal_error(int err)
2815
0
{
2816
0
    switch (err) {
2817
# if defined(OPENSSL_SYS_WINDOWS)
2818
#  if defined(WSAEWOULDBLOCK)
2819
    case WSAEWOULDBLOCK:
2820
#  endif
2821
# endif
2822
2823
0
# ifdef EWOULDBLOCK
2824
#  ifdef WSAEWOULDBLOCK
2825
#   if WSAEWOULDBLOCK != EWOULDBLOCK
2826
    case EWOULDBLOCK:
2827
#   endif
2828
#  else
2829
0
    case EWOULDBLOCK:
2830
0
#  endif
2831
0
# endif
2832
2833
0
# ifdef EINTR
2834
0
    case EINTR:
2835
0
# endif
2836
2837
0
# ifdef EAGAIN
2838
#  if EWOULDBLOCK != EAGAIN
2839
    case EAGAIN:
2840
#  endif
2841
0
# endif
2842
2843
0
# ifdef EPROTO
2844
0
    case EPROTO:
2845
0
# endif
2846
2847
0
# ifdef EINPROGRESS
2848
0
    case EINPROGRESS:
2849
0
# endif
2850
2851
0
# ifdef EALREADY
2852
0
    case EALREADY:
2853
0
# endif
2854
2855
0
        return 1;
2856
0
    default:
2857
0
        break;
2858
0
    }
2859
0
    return 0;
2860
0
}
2861
2862
#endif