Coverage Report

Created: 2023-09-25 06:41

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