sha/asm/keccak1600-x86_64.pl: optimize by re-ordering instructions.
[openssl.git] / crypto / bio / bss_dgram.c
1 /*
2  * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include <errno.h>
12
13 #include "bio_lcl.h"
14 #ifndef OPENSSL_NO_DGRAM
15
16 # if !defined(_WIN32)
17 #  include <sys/time.h>
18 # endif
19
20 # ifndef OPENSSL_NO_SCTP
21 #  include <netinet/sctp.h>
22 #  include <fcntl.h>
23 #  define OPENSSL_SCTP_DATA_CHUNK_TYPE            0x00
24 #  define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
25 # endif
26
27 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
28 #  define IP_MTU      14        /* linux is lame */
29 # endif
30
31 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
32 #  define IPPROTO_IPV6 41       /* windows is lame */
33 # endif
34
35 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
36 /* Standard definition causes type-punning problems. */
37 #  undef IN6_IS_ADDR_V4MAPPED
38 #  define s6_addr32 __u6_addr.__u6_addr32
39 #  define IN6_IS_ADDR_V4MAPPED(a)               \
40         (((a)->s6_addr32[0] == 0) &&          \
41          ((a)->s6_addr32[1] == 0) &&          \
42          ((a)->s6_addr32[2] == htonl(0x0000ffff)))
43 # endif
44
45 static int dgram_write(BIO *h, const char *buf, int num);
46 static int dgram_read(BIO *h, char *buf, int size);
47 static int dgram_puts(BIO *h, const char *str);
48 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
49 static int dgram_new(BIO *h);
50 static int dgram_free(BIO *data);
51 static int dgram_clear(BIO *bio);
52
53 # ifndef OPENSSL_NO_SCTP
54 static int dgram_sctp_write(BIO *h, const char *buf, int num);
55 static int dgram_sctp_read(BIO *h, char *buf, int size);
56 static int dgram_sctp_puts(BIO *h, const char *str);
57 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
58 static int dgram_sctp_new(BIO *h);
59 static int dgram_sctp_free(BIO *data);
60 #  ifdef SCTP_AUTHENTICATION_EVENT
61 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
62                                                   *snp);
63 #  endif
64 # endif
65
66 static int BIO_dgram_should_retry(int s);
67
68 static void get_current_time(struct timeval *t);
69
70 static const BIO_METHOD methods_dgramp = {
71     BIO_TYPE_DGRAM,
72     "datagram socket",
73     /* TODO: Convert to new style write function */
74     bwrite_conv,
75     dgram_write,
76     /* TODO: Convert to new style read function */
77     bread_conv,
78     dgram_read,
79     dgram_puts,
80     NULL,                       /* dgram_gets, */
81     dgram_ctrl,
82     dgram_new,
83     dgram_free,
84     NULL,
85 };
86
87 # ifndef OPENSSL_NO_SCTP
88 static const BIO_METHOD methods_dgramp_sctp = {
89     BIO_TYPE_DGRAM_SCTP,
90     "datagram sctp socket",
91     /* TODO: Convert to new style write function */
92     bwrite_conv,
93     dgram_sctp_write,
94     /* TODO: Convert to new style write function */
95     bread_conv,
96     dgram_sctp_read,
97     dgram_sctp_puts,
98     NULL,                       /* dgram_gets, */
99     dgram_sctp_ctrl,
100     dgram_sctp_new,
101     dgram_sctp_free,
102     NULL,
103 };
104 # endif
105
106 typedef struct bio_dgram_data_st {
107     BIO_ADDR peer;
108     unsigned int connected;
109     unsigned int _errno;
110     unsigned int mtu;
111     struct timeval next_timeout;
112     struct timeval socket_timeout;
113     unsigned int peekmode;
114 } bio_dgram_data;
115
116 # ifndef OPENSSL_NO_SCTP
117 typedef struct bio_dgram_sctp_save_message_st {
118     BIO *bio;
119     char *data;
120     int length;
121 } bio_dgram_sctp_save_message;
122
123 typedef struct bio_dgram_sctp_data_st {
124     BIO_ADDR peer;
125     unsigned int connected;
126     unsigned int _errno;
127     unsigned int mtu;
128     struct bio_dgram_sctp_sndinfo sndinfo;
129     struct bio_dgram_sctp_rcvinfo rcvinfo;
130     struct bio_dgram_sctp_prinfo prinfo;
131     void (*handle_notifications) (BIO *bio, void *context, void *buf);
132     void *notification_context;
133     int in_handshake;
134     int ccs_rcvd;
135     int ccs_sent;
136     int save_shutdown;
137     int peer_auth_tested;
138 } bio_dgram_sctp_data;
139 # endif
140
141 const BIO_METHOD *BIO_s_datagram(void)
142 {
143     return (&methods_dgramp);
144 }
145
146 BIO *BIO_new_dgram(int fd, int close_flag)
147 {
148     BIO *ret;
149
150     ret = BIO_new(BIO_s_datagram());
151     if (ret == NULL)
152         return (NULL);
153     BIO_set_fd(ret, fd, close_flag);
154     return (ret);
155 }
156
157 static int dgram_new(BIO *bi)
158 {
159     bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
160
161     if (data == NULL)
162         return 0;
163     bi->ptr = data;
164     return (1);
165 }
166
167 static int dgram_free(BIO *a)
168 {
169     bio_dgram_data *data;
170
171     if (a == NULL)
172         return (0);
173     if (!dgram_clear(a))
174         return 0;
175
176     data = (bio_dgram_data *)a->ptr;
177     OPENSSL_free(data);
178
179     return (1);
180 }
181
182 static int dgram_clear(BIO *a)
183 {
184     if (a == NULL)
185         return (0);
186     if (a->shutdown) {
187         if (a->init) {
188             BIO_closesocket(a->num);
189         }
190         a->init = 0;
191         a->flags = 0;
192     }
193     return (1);
194 }
195
196 static void dgram_adjust_rcv_timeout(BIO *b)
197 {
198 # if defined(SO_RCVTIMEO)
199     bio_dgram_data *data = (bio_dgram_data *)b->ptr;
200     union {
201         size_t s;
202         int i;
203     } sz = {
204         0
205     };
206
207     /* Is a timer active? */
208     if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
209         struct timeval timenow, timeleft;
210
211         /* Read current socket timeout */
212 #  ifdef OPENSSL_SYS_WINDOWS
213         int timeout;
214
215         sz.i = sizeof(timeout);
216         if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
217                        (void *)&timeout, &sz.i) < 0) {
218             perror("getsockopt");
219         } else {
220             data->socket_timeout.tv_sec = timeout / 1000;
221             data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
222         }
223 #  else
224         sz.i = sizeof(data->socket_timeout);
225         if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
226                        &(data->socket_timeout), (void *)&sz) < 0) {
227             perror("getsockopt");
228         } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
229             OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
230 #  endif
231
232         /* Get current time */
233         get_current_time(&timenow);
234
235         /* Calculate time left until timer expires */
236         memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
237         if (timeleft.tv_usec < timenow.tv_usec) {
238             timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
239             timeleft.tv_sec--;
240         } else {
241             timeleft.tv_usec -= timenow.tv_usec;
242         }
243         if (timeleft.tv_sec < timenow.tv_sec) {
244             timeleft.tv_sec = 0;
245             timeleft.tv_usec = 1;
246         } else {
247             timeleft.tv_sec -= timenow.tv_sec;
248         }
249
250         /*
251          * Adjust socket timeout if next handshake message timer will expire
252          * earlier.
253          */
254         if ((data->socket_timeout.tv_sec == 0
255              && data->socket_timeout.tv_usec == 0)
256             || (data->socket_timeout.tv_sec > timeleft.tv_sec)
257             || (data->socket_timeout.tv_sec == timeleft.tv_sec
258                 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
259 #  ifdef OPENSSL_SYS_WINDOWS
260             timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
261             if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
262                            (void *)&timeout, sizeof(timeout)) < 0) {
263                 perror("setsockopt");
264             }
265 #  else
266             if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
267                            sizeof(struct timeval)) < 0) {
268                 perror("setsockopt");
269             }
270 #  endif
271         }
272     }
273 # endif
274 }
275
276 static void dgram_reset_rcv_timeout(BIO *b)
277 {
278 # if defined(SO_RCVTIMEO)
279     bio_dgram_data *data = (bio_dgram_data *)b->ptr;
280
281     /* Is a timer active? */
282     if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
283 #  ifdef OPENSSL_SYS_WINDOWS
284         int timeout = data->socket_timeout.tv_sec * 1000 +
285             data->socket_timeout.tv_usec / 1000;
286         if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
287                        (void *)&timeout, sizeof(timeout)) < 0) {
288             perror("setsockopt");
289         }
290 #  else
291         if (setsockopt
292             (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
293              sizeof(struct timeval)) < 0) {
294             perror("setsockopt");
295         }
296 #  endif
297     }
298 # endif
299 }
300
301 static int dgram_read(BIO *b, char *out, int outl)
302 {
303     int ret = 0;
304     bio_dgram_data *data = (bio_dgram_data *)b->ptr;
305     int flags = 0;
306
307     BIO_ADDR peer;
308     socklen_t len = sizeof(peer);
309
310     if (out != NULL) {
311         clear_socket_error();
312         memset(&peer, 0, sizeof(peer));
313         dgram_adjust_rcv_timeout(b);
314         if (data->peekmode)
315             flags = MSG_PEEK;
316         ret = recvfrom(b->num, out, outl, flags,
317                        BIO_ADDR_sockaddr_noconst(&peer), &len);
318
319         if (!data->connected && ret >= 0)
320             BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
321
322         BIO_clear_retry_flags(b);
323         if (ret < 0) {
324             if (BIO_dgram_should_retry(ret)) {
325                 BIO_set_retry_read(b);
326                 data->_errno = get_last_socket_error();
327             }
328         }
329
330         dgram_reset_rcv_timeout(b);
331     }
332     return (ret);
333 }
334
335 static int dgram_write(BIO *b, const char *in, int inl)
336 {
337     int ret;
338     bio_dgram_data *data = (bio_dgram_data *)b->ptr;
339     clear_socket_error();
340
341     if (data->connected)
342         ret = writesocket(b->num, in, inl);
343     else {
344         int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
345
346 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
347         ret = sendto(b->num, (char *)in, inl, 0,
348                      BIO_ADDR_sockaddr(&data->peer), peerlen);
349 # else
350         ret = sendto(b->num, in, inl, 0,
351                      BIO_ADDR_sockaddr(&data->peer), peerlen);
352 # endif
353     }
354
355     BIO_clear_retry_flags(b);
356     if (ret <= 0) {
357         if (BIO_dgram_should_retry(ret)) {
358             BIO_set_retry_write(b);
359             data->_errno = get_last_socket_error();
360         }
361     }
362     return (ret);
363 }
364
365 static long dgram_get_mtu_overhead(bio_dgram_data *data)
366 {
367     long ret;
368
369     switch (BIO_ADDR_family(&data->peer)) {
370     case AF_INET:
371         /*
372          * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
373          */
374         ret = 28;
375         break;
376 # ifdef AF_INET6
377     case AF_INET6:
378         {
379 #  ifdef IN6_IS_ADDR_V4MAPPED
380             struct in6_addr tmp_addr;
381             if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
382                 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
383                 /*
384                  * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
385                  */
386                 ret = 28;
387             else
388 #  endif
389             /*
390              * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
391              */
392             ret = 48;
393         }
394         break;
395 # endif
396     default:
397         /* We don't know. Go with the historical default */
398         ret = 28;
399         break;
400     }
401     return ret;
402 }
403
404 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
405 {
406     long ret = 1;
407     int *ip;
408     bio_dgram_data *data = NULL;
409     int sockopt_val = 0;
410     int d_errno;
411 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
412     socklen_t sockopt_len;      /* assume that system supporting IP_MTU is
413                                  * modern enough to define socklen_t */
414     socklen_t addr_len;
415     BIO_ADDR addr;
416 # endif
417
418     data = (bio_dgram_data *)b->ptr;
419
420     switch (cmd) {
421     case BIO_CTRL_RESET:
422         num = 0;
423         ret = 0;
424         break;
425     case BIO_CTRL_INFO:
426         ret = 0;
427         break;
428     case BIO_C_SET_FD:
429         dgram_clear(b);
430         b->num = *((int *)ptr);
431         b->shutdown = (int)num;
432         b->init = 1;
433         break;
434     case BIO_C_GET_FD:
435         if (b->init) {
436             ip = (int *)ptr;
437             if (ip != NULL)
438                 *ip = b->num;
439             ret = b->num;
440         } else
441             ret = -1;
442         break;
443     case BIO_CTRL_GET_CLOSE:
444         ret = b->shutdown;
445         break;
446     case BIO_CTRL_SET_CLOSE:
447         b->shutdown = (int)num;
448         break;
449     case BIO_CTRL_PENDING:
450     case BIO_CTRL_WPENDING:
451         ret = 0;
452         break;
453     case BIO_CTRL_DUP:
454     case BIO_CTRL_FLUSH:
455         ret = 1;
456         break;
457     case BIO_CTRL_DGRAM_CONNECT:
458         BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
459         break;
460         /* (Linux)kernel sets DF bit on outgoing IP packets */
461     case BIO_CTRL_DGRAM_MTU_DISCOVER:
462 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
463         addr_len = (socklen_t) sizeof(addr);
464         memset(&addr, 0, sizeof(addr));
465         if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
466             ret = 0;
467             break;
468         }
469         switch (addr.sa.sa_family) {
470         case AF_INET:
471             sockopt_val = IP_PMTUDISC_DO;
472             if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
473                                   &sockopt_val, sizeof(sockopt_val))) < 0)
474                 perror("setsockopt");
475             break;
476 #  if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
477         case AF_INET6:
478             sockopt_val = IPV6_PMTUDISC_DO;
479             if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
480                                   &sockopt_val, sizeof(sockopt_val))) < 0)
481                 perror("setsockopt");
482             break;
483 #  endif
484         default:
485             ret = -1;
486             break;
487         }
488 # else
489         ret = -1;
490 # endif
491         break;
492     case BIO_CTRL_DGRAM_QUERY_MTU:
493 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
494         addr_len = (socklen_t) sizeof(addr);
495         memset(&addr, 0, sizeof(addr));
496         if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
497             ret = 0;
498             break;
499         }
500         sockopt_len = sizeof(sockopt_val);
501         switch (addr.sa.sa_family) {
502         case AF_INET:
503             if ((ret =
504                  getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
505                             &sockopt_len)) < 0 || sockopt_val < 0) {
506                 ret = 0;
507             } else {
508                 /*
509                  * we assume that the transport protocol is UDP and no IP
510                  * options are used.
511                  */
512                 data->mtu = sockopt_val - 8 - 20;
513                 ret = data->mtu;
514             }
515             break;
516 #  if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
517         case AF_INET6:
518             if ((ret =
519                  getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
520                             (void *)&sockopt_val, &sockopt_len)) < 0
521                 || sockopt_val < 0) {
522                 ret = 0;
523             } else {
524                 /*
525                  * we assume that the transport protocol is UDP and no IPV6
526                  * options are used.
527                  */
528                 data->mtu = sockopt_val - 8 - 40;
529                 ret = data->mtu;
530             }
531             break;
532 #  endif
533         default:
534             ret = 0;
535             break;
536         }
537 # else
538         ret = 0;
539 # endif
540         break;
541     case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
542         ret = -dgram_get_mtu_overhead(data);
543         switch (BIO_ADDR_family(&data->peer)) {
544         case AF_INET:
545             ret += 576;
546             break;
547 # if OPENSSL_USE_IPV6
548         case AF_INET6:
549             {
550 #  ifdef IN6_IS_ADDR_V4MAPPED
551                 struct in6_addr tmp_addr;
552                 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
553                     && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
554                     ret += 576;
555                 else
556 #  endif
557                     ret += 1280;
558             }
559             break;
560 # endif
561         default:
562             ret += 576;
563             break;
564         }
565         break;
566     case BIO_CTRL_DGRAM_GET_MTU:
567         return data->mtu;
568     case BIO_CTRL_DGRAM_SET_MTU:
569         data->mtu = num;
570         ret = num;
571         break;
572     case BIO_CTRL_DGRAM_SET_CONNECTED:
573         if (ptr != NULL) {
574             data->connected = 1;
575             BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
576         } else {
577             data->connected = 0;
578             memset(&data->peer, 0, sizeof(data->peer));
579         }
580         break;
581     case BIO_CTRL_DGRAM_GET_PEER:
582         ret = BIO_ADDR_sockaddr_size(&data->peer);
583         /* FIXME: if num < ret, we will only return part of an address.
584            That should bee an error, no? */
585         if (num == 0 || num > ret)
586             num = ret;
587         memcpy(ptr, &data->peer, (ret = num));
588         break;
589     case BIO_CTRL_DGRAM_SET_PEER:
590         BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
591         break;
592     case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
593         memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
594         break;
595 # if defined(SO_RCVTIMEO)
596     case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
597 #  ifdef OPENSSL_SYS_WINDOWS
598         {
599             struct timeval *tv = (struct timeval *)ptr;
600             int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
601             if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
602                            (void *)&timeout, sizeof(timeout)) < 0) {
603                 perror("setsockopt");
604                 ret = -1;
605             }
606         }
607 #  else
608         if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
609                        sizeof(struct timeval)) < 0) {
610             perror("setsockopt");
611             ret = -1;
612         }
613 #  endif
614         break;
615     case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
616         {
617             union {
618                 size_t s;
619                 int i;
620             } sz = {
621                 0
622             };
623 #  ifdef OPENSSL_SYS_WINDOWS
624             int timeout;
625             struct timeval *tv = (struct timeval *)ptr;
626
627             sz.i = sizeof(timeout);
628             if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
629                            (void *)&timeout, &sz.i) < 0) {
630                 perror("getsockopt");
631                 ret = -1;
632             } else {
633                 tv->tv_sec = timeout / 1000;
634                 tv->tv_usec = (timeout % 1000) * 1000;
635                 ret = sizeof(*tv);
636             }
637 #  else
638             sz.i = sizeof(struct timeval);
639             if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
640                            ptr, (void *)&sz) < 0) {
641                 perror("getsockopt");
642                 ret = -1;
643             } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
644                 OPENSSL_assert(sz.s <= sizeof(struct timeval));
645                 ret = (int)sz.s;
646             } else
647                 ret = sz.i;
648 #  endif
649         }
650         break;
651 # endif
652 # if defined(SO_SNDTIMEO)
653     case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
654 #  ifdef OPENSSL_SYS_WINDOWS
655         {
656             struct timeval *tv = (struct timeval *)ptr;
657             int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
658             if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
659                            (void *)&timeout, sizeof(timeout)) < 0) {
660                 perror("setsockopt");
661                 ret = -1;
662             }
663         }
664 #  else
665         if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
666                        sizeof(struct timeval)) < 0) {
667             perror("setsockopt");
668             ret = -1;
669         }
670 #  endif
671         break;
672     case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
673         {
674             union {
675                 size_t s;
676                 int i;
677             } sz = {
678                 0
679             };
680 #  ifdef OPENSSL_SYS_WINDOWS
681             int timeout;
682             struct timeval *tv = (struct timeval *)ptr;
683
684             sz.i = sizeof(timeout);
685             if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
686                            (void *)&timeout, &sz.i) < 0) {
687                 perror("getsockopt");
688                 ret = -1;
689             } else {
690                 tv->tv_sec = timeout / 1000;
691                 tv->tv_usec = (timeout % 1000) * 1000;
692                 ret = sizeof(*tv);
693             }
694 #  else
695             sz.i = sizeof(struct timeval);
696             if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
697                            ptr, (void *)&sz) < 0) {
698                 perror("getsockopt");
699                 ret = -1;
700             } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
701                 OPENSSL_assert(sz.s <= sizeof(struct timeval));
702                 ret = (int)sz.s;
703             } else
704                 ret = sz.i;
705 #  endif
706         }
707         break;
708 # endif
709     case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
710         /* fall-through */
711     case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
712 # ifdef OPENSSL_SYS_WINDOWS
713         d_errno = (data->_errno == WSAETIMEDOUT);
714 # else
715         d_errno = (data->_errno == EAGAIN);
716 # endif
717         if (d_errno) {
718             ret = 1;
719             data->_errno = 0;
720         } else
721             ret = 0;
722         break;
723 # ifdef EMSGSIZE
724     case BIO_CTRL_DGRAM_MTU_EXCEEDED:
725         if (data->_errno == EMSGSIZE) {
726             ret = 1;
727             data->_errno = 0;
728         } else
729             ret = 0;
730         break;
731 # endif
732     case BIO_CTRL_DGRAM_SET_DONT_FRAG:
733         sockopt_val = num ? 1 : 0;
734
735         switch (data->peer.sa.sa_family) {
736         case AF_INET:
737 # if defined(IP_DONTFRAG)
738             if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
739                                   &sockopt_val, sizeof(sockopt_val))) < 0) {
740                 perror("setsockopt");
741                 ret = -1;
742             }
743 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
744             if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
745                 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
746                                   &sockopt_val, sizeof(sockopt_val))) < 0) {
747                 perror("setsockopt");
748                 ret = -1;
749             }
750 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
751             if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
752                                   (const char *)&sockopt_val,
753                                   sizeof(sockopt_val))) < 0) {
754                 perror("setsockopt");
755                 ret = -1;
756             }
757 # else
758             ret = -1;
759 # endif
760             break;
761 # if OPENSSL_USE_IPV6
762         case AF_INET6:
763 #  if defined(IPV6_DONTFRAG)
764             if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
765                                   (const void *)&sockopt_val,
766                                   sizeof(sockopt_val))) < 0) {
767                 perror("setsockopt");
768                 ret = -1;
769             }
770 #  elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
771             if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
772                 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
773                                   &sockopt_val, sizeof(sockopt_val))) < 0) {
774                 perror("setsockopt");
775                 ret = -1;
776             }
777 #  else
778             ret = -1;
779 #  endif
780             break;
781 # endif
782         default:
783             ret = -1;
784             break;
785         }
786         break;
787     case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
788         ret = dgram_get_mtu_overhead(data);
789         break;
790
791     /*
792      * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
793      * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
794      * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
795      * value has been updated to a non-clashing value. However to preserve
796      * binary compatiblity we now respond to both the old value and the new one
797      */
798     case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
799     case BIO_CTRL_DGRAM_SET_PEEK_MODE:
800         data->peekmode = (unsigned int)num;
801         break;
802     default:
803         ret = 0;
804         break;
805     }
806     return (ret);
807 }
808
809 static int dgram_puts(BIO *bp, const char *str)
810 {
811     int n, ret;
812
813     n = strlen(str);
814     ret = dgram_write(bp, str, n);
815     return (ret);
816 }
817
818 # ifndef OPENSSL_NO_SCTP
819 const BIO_METHOD *BIO_s_datagram_sctp(void)
820 {
821     return (&methods_dgramp_sctp);
822 }
823
824 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
825 {
826     BIO *bio;
827     int ret, optval = 20000;
828     int auth_data = 0, auth_forward = 0;
829     unsigned char *p;
830     struct sctp_authchunk auth;
831     struct sctp_authchunks *authchunks;
832     socklen_t sockopt_len;
833 #  ifdef SCTP_AUTHENTICATION_EVENT
834 #   ifdef SCTP_EVENT
835     struct sctp_event event;
836 #   else
837     struct sctp_event_subscribe event;
838 #   endif
839 #  endif
840
841     bio = BIO_new(BIO_s_datagram_sctp());
842     if (bio == NULL)
843         return (NULL);
844     BIO_set_fd(bio, fd, close_flag);
845
846     /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
847     auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
848     ret =
849         setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
850                    sizeof(struct sctp_authchunk));
851     if (ret < 0) {
852         BIO_vfree(bio);
853         BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
854         ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
855         return (NULL);
856     }
857     auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
858     ret =
859         setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
860                    sizeof(struct sctp_authchunk));
861     if (ret < 0) {
862         BIO_vfree(bio);
863         BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
864         ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
865         return (NULL);
866     }
867
868     /*
869      * Test if activation was successful. When using accept(), SCTP-AUTH has
870      * to be activated for the listening socket already, otherwise the
871      * connected socket won't use it. Similarly with connect(): the socket
872      * prior to connection must be activated for SCTP-AUTH
873      */
874     sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
875     authchunks = OPENSSL_zalloc(sockopt_len);
876     if (authchunks == NULL) {
877         BIO_vfree(bio);
878         return (NULL);
879     }
880     ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
881                    &sockopt_len);
882     if (ret < 0) {
883         OPENSSL_free(authchunks);
884         BIO_vfree(bio);
885         return (NULL);
886     }
887
888     for (p = (unsigned char *)authchunks->gauth_chunks;
889          p < (unsigned char *)authchunks + sockopt_len;
890          p += sizeof(uint8_t)) {
891         if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
892             auth_data = 1;
893         if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
894             auth_forward = 1;
895     }
896
897     OPENSSL_free(authchunks);
898
899     if (!auth_data || !auth_forward) {
900         BIO_vfree(bio);
901         BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
902         ERR_add_error_data(1,
903                            "Ensure SCTP AUTH chunks are enabled on the "
904                            "underlying socket");
905         return NULL;
906     }
907
908 #  ifdef SCTP_AUTHENTICATION_EVENT
909 #   ifdef SCTP_EVENT
910     memset(&event, 0, sizeof(event));
911     event.se_assoc_id = 0;
912     event.se_type = SCTP_AUTHENTICATION_EVENT;
913     event.se_on = 1;
914     ret =
915         setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
916                    sizeof(struct sctp_event));
917     if (ret < 0) {
918         BIO_vfree(bio);
919         return (NULL);
920     }
921 #   else
922     sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
923     ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
924     if (ret < 0) {
925         BIO_vfree(bio);
926         return (NULL);
927     }
928
929     event.sctp_authentication_event = 1;
930
931     ret =
932         setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
933                    sizeof(struct sctp_event_subscribe));
934     if (ret < 0) {
935         BIO_vfree(bio);
936         return (NULL);
937     }
938 #   endif
939 #  endif
940
941     /*
942      * Disable partial delivery by setting the min size larger than the max
943      * record size of 2^14 + 2048 + 13
944      */
945     ret =
946         setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
947                    sizeof(optval));
948     if (ret < 0) {
949         BIO_vfree(bio);
950         return (NULL);
951     }
952
953     return (bio);
954 }
955
956 int BIO_dgram_is_sctp(BIO *bio)
957 {
958     return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
959 }
960
961 static int dgram_sctp_new(BIO *bi)
962 {
963     bio_dgram_sctp_data *data = NULL;
964
965     bi->init = 0;
966     bi->num = 0;
967     data = OPENSSL_zalloc(sizeof(*data));
968     if (data == NULL)
969         return 0;
970 #  ifdef SCTP_PR_SCTP_NONE
971     data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
972 #  endif
973     bi->ptr = data;
974
975     bi->flags = 0;
976     return (1);
977 }
978
979 static int dgram_sctp_free(BIO *a)
980 {
981     bio_dgram_sctp_data *data;
982
983     if (a == NULL)
984         return (0);
985     if (!dgram_clear(a))
986         return 0;
987
988     data = (bio_dgram_sctp_data *) a->ptr;
989     if (data != NULL)
990         OPENSSL_free(data);
991
992     return (1);
993 }
994
995 #  ifdef SCTP_AUTHENTICATION_EVENT
996 void dgram_sctp_handle_auth_free_key_event(BIO *b,
997                                            union sctp_notification *snp)
998 {
999     int ret;
1000     struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1001
1002     if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1003         struct sctp_authkeyid authkeyid;
1004
1005         /* delete key */
1006         authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1007         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1008                          &authkeyid, sizeof(struct sctp_authkeyid));
1009     }
1010 }
1011 #  endif
1012
1013 static int dgram_sctp_read(BIO *b, char *out, int outl)
1014 {
1015     int ret = 0, n = 0, i, optval;
1016     socklen_t optlen;
1017     bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1018     union sctp_notification *snp;
1019     struct msghdr msg;
1020     struct iovec iov;
1021     struct cmsghdr *cmsg;
1022     char cmsgbuf[512];
1023
1024     if (out != NULL) {
1025         clear_socket_error();
1026
1027         do {
1028             memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1029             iov.iov_base = out;
1030             iov.iov_len = outl;
1031             msg.msg_name = NULL;
1032             msg.msg_namelen = 0;
1033             msg.msg_iov = &iov;
1034             msg.msg_iovlen = 1;
1035             msg.msg_control = cmsgbuf;
1036             msg.msg_controllen = 512;
1037             msg.msg_flags = 0;
1038             n = recvmsg(b->num, &msg, 0);
1039
1040             if (n <= 0) {
1041                 if (n < 0)
1042                     ret = n;
1043                 break;
1044             }
1045
1046             if (msg.msg_controllen > 0) {
1047                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1048                      cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1049                     if (cmsg->cmsg_level != IPPROTO_SCTP)
1050                         continue;
1051 #  ifdef SCTP_RCVINFO
1052                     if (cmsg->cmsg_type == SCTP_RCVINFO) {
1053                         struct sctp_rcvinfo *rcvinfo;
1054
1055                         rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1056                         data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1057                         data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1058                         data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1059                         data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1060                         data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1061                         data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1062                         data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1063                     }
1064 #  endif
1065 #  ifdef SCTP_SNDRCV
1066                     if (cmsg->cmsg_type == SCTP_SNDRCV) {
1067                         struct sctp_sndrcvinfo *sndrcvinfo;
1068
1069                         sndrcvinfo =
1070                             (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1071                         data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1072                         data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1073                         data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1074                         data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1075                         data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1076                         data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1077                         data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1078                     }
1079 #  endif
1080                 }
1081             }
1082
1083             if (msg.msg_flags & MSG_NOTIFICATION) {
1084                 snp = (union sctp_notification *)out;
1085                 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1086 #  ifdef SCTP_EVENT
1087                     struct sctp_event event;
1088 #  else
1089                     struct sctp_event_subscribe event;
1090                     socklen_t eventsize;
1091 #  endif
1092
1093                     /* disable sender dry event */
1094 #  ifdef SCTP_EVENT
1095                     memset(&event, 0, sizeof(event));
1096                     event.se_assoc_id = 0;
1097                     event.se_type = SCTP_SENDER_DRY_EVENT;
1098                     event.se_on = 0;
1099                     i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1100                                    sizeof(struct sctp_event));
1101                     if (i < 0) {
1102                         ret = i;
1103                         break;
1104                     }
1105 #  else
1106                     eventsize = sizeof(struct sctp_event_subscribe);
1107                     i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1108                                    &eventsize);
1109                     if (i < 0) {
1110                         ret = i;
1111                         break;
1112                     }
1113
1114                     event.sctp_sender_dry_event = 0;
1115
1116                     i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1117                                    sizeof(struct sctp_event_subscribe));
1118                     if (i < 0) {
1119                         ret = i;
1120                         break;
1121                     }
1122 #  endif
1123                 }
1124 #  ifdef SCTP_AUTHENTICATION_EVENT
1125                 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1126                     dgram_sctp_handle_auth_free_key_event(b, snp);
1127 #  endif
1128
1129                 if (data->handle_notifications != NULL)
1130                     data->handle_notifications(b, data->notification_context,
1131                                                (void *)out);
1132
1133                 memset(out, 0, outl);
1134             } else
1135                 ret += n;
1136         }
1137         while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1138                && (ret < outl));
1139
1140         if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1141             /* Partial message read, this should never happen! */
1142
1143             /*
1144              * The buffer was too small, this means the peer sent a message
1145              * that was larger than allowed.
1146              */
1147             if (ret == outl)
1148                 return -1;
1149
1150             /*
1151              * Test if socket buffer can handle max record size (2^14 + 2048
1152              * + 13)
1153              */
1154             optlen = (socklen_t) sizeof(int);
1155             ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1156             if (ret >= 0)
1157                 OPENSSL_assert(optval >= 18445);
1158
1159             /*
1160              * Test if SCTP doesn't partially deliver below max record size
1161              * (2^14 + 2048 + 13)
1162              */
1163             optlen = (socklen_t) sizeof(int);
1164             ret =
1165                 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1166                            &optval, &optlen);
1167             if (ret >= 0)
1168                 OPENSSL_assert(optval >= 18445);
1169
1170             /*
1171              * Partially delivered notification??? Probably a bug....
1172              */
1173             OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1174
1175             /*
1176              * Everything seems ok till now, so it's most likely a message
1177              * dropped by PR-SCTP.
1178              */
1179             memset(out, 0, outl);
1180             BIO_set_retry_read(b);
1181             return -1;
1182         }
1183
1184         BIO_clear_retry_flags(b);
1185         if (ret < 0) {
1186             if (BIO_dgram_should_retry(ret)) {
1187                 BIO_set_retry_read(b);
1188                 data->_errno = get_last_socket_error();
1189             }
1190         }
1191
1192         /* Test if peer uses SCTP-AUTH before continuing */
1193         if (!data->peer_auth_tested) {
1194             int ii, auth_data = 0, auth_forward = 0;
1195             unsigned char *p;
1196             struct sctp_authchunks *authchunks;
1197
1198             optlen =
1199                 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1200             authchunks = OPENSSL_malloc(optlen);
1201             if (authchunks == NULL) {
1202                 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1203                 return -1;
1204             }
1205             memset(authchunks, 0, optlen);
1206             ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1207                             authchunks, &optlen);
1208
1209             if (ii >= 0)
1210                 for (p = (unsigned char *)authchunks->gauth_chunks;
1211                      p < (unsigned char *)authchunks + optlen;
1212                      p += sizeof(uint8_t)) {
1213                     if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1214                         auth_data = 1;
1215                     if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1216                         auth_forward = 1;
1217                 }
1218
1219             OPENSSL_free(authchunks);
1220
1221             if (!auth_data || !auth_forward) {
1222                 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1223                 return -1;
1224             }
1225
1226             data->peer_auth_tested = 1;
1227         }
1228     }
1229     return (ret);
1230 }
1231
1232 /*
1233  * dgram_sctp_write - send message on SCTP socket
1234  * @b: BIO to write to
1235  * @in: data to send
1236  * @inl: amount of bytes in @in to send
1237  *
1238  * Returns -1 on error or the sent amount of bytes on success
1239  */
1240 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1241 {
1242     int ret;
1243     bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1244     struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1245     struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1246     struct bio_dgram_sctp_sndinfo handshake_sinfo;
1247     struct iovec iov[1];
1248     struct msghdr msg;
1249     struct cmsghdr *cmsg;
1250 #  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1251     char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1252                  CMSG_SPACE(sizeof(struct sctp_prinfo))];
1253     struct sctp_sndinfo *sndinfo;
1254     struct sctp_prinfo *prinfo;
1255 #  else
1256     char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1257     struct sctp_sndrcvinfo *sndrcvinfo;
1258 #  endif
1259
1260     clear_socket_error();
1261
1262     /*
1263      * If we're send anything else than application data, disable all user
1264      * parameters and flags.
1265      */
1266     if (in[0] != 23) {
1267         memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1268 #  ifdef SCTP_SACK_IMMEDIATELY
1269         handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1270 #  endif
1271         sinfo = &handshake_sinfo;
1272     }
1273
1274     /* We can only send a shutdown alert if the socket is dry */
1275     if (data->save_shutdown) {
1276         ret = BIO_dgram_sctp_wait_for_dry(b);
1277         if (ret < 0)
1278             return -1;
1279         if (ret == 0) {
1280             BIO_clear_retry_flags(b);
1281             BIO_set_retry_write(b);
1282             return -1;
1283         }
1284     }
1285
1286     iov[0].iov_base = (char *)in;
1287     iov[0].iov_len = inl;
1288     msg.msg_name = NULL;
1289     msg.msg_namelen = 0;
1290     msg.msg_iov = iov;
1291     msg.msg_iovlen = 1;
1292     msg.msg_control = (caddr_t) cmsgbuf;
1293     msg.msg_controllen = 0;
1294     msg.msg_flags = 0;
1295 #  if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1296     cmsg = (struct cmsghdr *)cmsgbuf;
1297     cmsg->cmsg_level = IPPROTO_SCTP;
1298     cmsg->cmsg_type = SCTP_SNDINFO;
1299     cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1300     sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1301     memset(sndinfo, 0, sizeof(*sndinfo));
1302     sndinfo->snd_sid = sinfo->snd_sid;
1303     sndinfo->snd_flags = sinfo->snd_flags;
1304     sndinfo->snd_ppid = sinfo->snd_ppid;
1305     sndinfo->snd_context = sinfo->snd_context;
1306     msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1307
1308     cmsg =
1309         (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1310     cmsg->cmsg_level = IPPROTO_SCTP;
1311     cmsg->cmsg_type = SCTP_PRINFO;
1312     cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1313     prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1314     memset(prinfo, 0, sizeof(*prinfo));
1315     prinfo->pr_policy = pinfo->pr_policy;
1316     prinfo->pr_value = pinfo->pr_value;
1317     msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1318 #  else
1319     cmsg = (struct cmsghdr *)cmsgbuf;
1320     cmsg->cmsg_level = IPPROTO_SCTP;
1321     cmsg->cmsg_type = SCTP_SNDRCV;
1322     cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1323     sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1324     memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1325     sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1326     sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1327 #   ifdef __FreeBSD__
1328     sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1329 #   endif
1330     sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1331     sndrcvinfo->sinfo_context = sinfo->snd_context;
1332     sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1333     msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1334 #  endif
1335
1336     ret = sendmsg(b->num, &msg, 0);
1337
1338     BIO_clear_retry_flags(b);
1339     if (ret <= 0) {
1340         if (BIO_dgram_should_retry(ret)) {
1341             BIO_set_retry_write(b);
1342             data->_errno = get_last_socket_error();
1343         }
1344     }
1345     return (ret);
1346 }
1347
1348 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1349 {
1350     long ret = 1;
1351     bio_dgram_sctp_data *data = NULL;
1352     socklen_t sockopt_len = 0;
1353     struct sctp_authkeyid authkeyid;
1354     struct sctp_authkey *authkey = NULL;
1355
1356     data = (bio_dgram_sctp_data *) b->ptr;
1357
1358     switch (cmd) {
1359     case BIO_CTRL_DGRAM_QUERY_MTU:
1360         /*
1361          * Set to maximum (2^14) and ignore user input to enable transport
1362          * protocol fragmentation. Returns always 2^14.
1363          */
1364         data->mtu = 16384;
1365         ret = data->mtu;
1366         break;
1367     case BIO_CTRL_DGRAM_SET_MTU:
1368         /*
1369          * Set to maximum (2^14) and ignore input to enable transport
1370          * protocol fragmentation. Returns always 2^14.
1371          */
1372         data->mtu = 16384;
1373         ret = data->mtu;
1374         break;
1375     case BIO_CTRL_DGRAM_SET_CONNECTED:
1376     case BIO_CTRL_DGRAM_CONNECT:
1377         /* Returns always -1. */
1378         ret = -1;
1379         break;
1380     case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1381         /*
1382          * SCTP doesn't need the DTLS timer Returns always 1.
1383          */
1384         break;
1385     case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1386         /*
1387          * We allow transport protocol fragmentation so this is irrelevant
1388          */
1389         ret = 0;
1390         break;
1391     case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1392         if (num > 0)
1393             data->in_handshake = 1;
1394         else
1395             data->in_handshake = 0;
1396
1397         ret =
1398             setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1399                        &data->in_handshake, sizeof(int));
1400         break;
1401     case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1402         /*
1403          * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1404          */
1405
1406         /* Get active key */
1407         sockopt_len = sizeof(struct sctp_authkeyid);
1408         ret =
1409             getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1410                        &sockopt_len);
1411         if (ret < 0)
1412             break;
1413
1414         /* Add new key */
1415         sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1416         authkey = OPENSSL_malloc(sockopt_len);
1417         if (authkey == NULL) {
1418             ret = -1;
1419             break;
1420         }
1421         memset(authkey, 0, sockopt_len);
1422         authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1423 #  ifndef __FreeBSD__
1424         /*
1425          * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1426          * and higher work without it.
1427          */
1428         authkey->sca_keylength = 64;
1429 #  endif
1430         memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1431
1432         ret =
1433             setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1434                        sockopt_len);
1435         OPENSSL_free(authkey);
1436         authkey = NULL;
1437         if (ret < 0)
1438             break;
1439
1440         /* Reset active key */
1441         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1442                          &authkeyid, sizeof(struct sctp_authkeyid));
1443         if (ret < 0)
1444             break;
1445
1446         break;
1447     case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1448         /* Returns 0 on success, -1 otherwise. */
1449
1450         /* Get active key */
1451         sockopt_len = sizeof(struct sctp_authkeyid);
1452         ret =
1453             getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1454                        &sockopt_len);
1455         if (ret < 0)
1456             break;
1457
1458         /* Set active key */
1459         authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1460         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1461                          &authkeyid, sizeof(struct sctp_authkeyid));
1462         if (ret < 0)
1463             break;
1464
1465         /*
1466          * CCS has been sent, so remember that and fall through to check if
1467          * we need to deactivate an old key
1468          */
1469         data->ccs_sent = 1;
1470
1471     case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1472         /* Returns 0 on success, -1 otherwise. */
1473
1474         /*
1475          * Has this command really been called or is this just a
1476          * fall-through?
1477          */
1478         if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1479             data->ccs_rcvd = 1;
1480
1481         /*
1482          * CSS has been both, received and sent, so deactivate an old key
1483          */
1484         if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1485             /* Get active key */
1486             sockopt_len = sizeof(struct sctp_authkeyid);
1487             ret =
1488                 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1489                            &authkeyid, &sockopt_len);
1490             if (ret < 0)
1491                 break;
1492
1493             /*
1494              * Deactivate key or delete second last key if
1495              * SCTP_AUTHENTICATION_EVENT is not available.
1496              */
1497             authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1498 #  ifdef SCTP_AUTH_DEACTIVATE_KEY
1499             sockopt_len = sizeof(struct sctp_authkeyid);
1500             ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1501                              &authkeyid, sockopt_len);
1502             if (ret < 0)
1503                 break;
1504 #  endif
1505 #  ifndef SCTP_AUTHENTICATION_EVENT
1506             if (authkeyid.scact_keynumber > 0) {
1507                 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1508                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1509                                  &authkeyid, sizeof(struct sctp_authkeyid));
1510                 if (ret < 0)
1511                     break;
1512             }
1513 #  endif
1514
1515             data->ccs_rcvd = 0;
1516             data->ccs_sent = 0;
1517         }
1518         break;
1519     case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1520         /* Returns the size of the copied struct. */
1521         if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1522             num = sizeof(struct bio_dgram_sctp_sndinfo);
1523
1524         memcpy(ptr, &(data->sndinfo), num);
1525         ret = num;
1526         break;
1527     case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1528         /* Returns the size of the copied struct. */
1529         if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1530             num = sizeof(struct bio_dgram_sctp_sndinfo);
1531
1532         memcpy(&(data->sndinfo), ptr, num);
1533         break;
1534     case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1535         /* Returns the size of the copied struct. */
1536         if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1537             num = sizeof(struct bio_dgram_sctp_rcvinfo);
1538
1539         memcpy(ptr, &data->rcvinfo, num);
1540
1541         ret = num;
1542         break;
1543     case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1544         /* Returns the size of the copied struct. */
1545         if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1546             num = sizeof(struct bio_dgram_sctp_rcvinfo);
1547
1548         memcpy(&(data->rcvinfo), ptr, num);
1549         break;
1550     case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1551         /* Returns the size of the copied struct. */
1552         if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1553             num = sizeof(struct bio_dgram_sctp_prinfo);
1554
1555         memcpy(ptr, &(data->prinfo), num);
1556         ret = num;
1557         break;
1558     case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1559         /* Returns the size of the copied struct. */
1560         if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1561             num = sizeof(struct bio_dgram_sctp_prinfo);
1562
1563         memcpy(&(data->prinfo), ptr, num);
1564         break;
1565     case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1566         /* Returns always 1. */
1567         if (num > 0)
1568             data->save_shutdown = 1;
1569         else
1570             data->save_shutdown = 0;
1571         break;
1572
1573     default:
1574         /*
1575          * Pass to default ctrl function to process SCTP unspecific commands
1576          */
1577         ret = dgram_ctrl(b, cmd, num, ptr);
1578         break;
1579     }
1580     return (ret);
1581 }
1582
1583 int BIO_dgram_sctp_notification_cb(BIO *b,
1584                                    void (*handle_notifications) (BIO *bio,
1585                                                                  void
1586                                                                  *context,
1587                                                                  void *buf),
1588                                    void *context)
1589 {
1590     bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1591
1592     if (handle_notifications != NULL) {
1593         data->handle_notifications = handle_notifications;
1594         data->notification_context = context;
1595     } else
1596         return -1;
1597
1598     return 0;
1599 }
1600
1601 /*
1602  * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1603  * @b: The BIO to check for the dry event
1604  *
1605  * Wait until the peer confirms all packets have been received, and so that
1606  * our kernel doesn't have anything to send anymore.  This is only received by
1607  * the peer's kernel, not the application.
1608  *
1609  * Returns:
1610  * -1 on error
1611  *  0 when not dry yet
1612  *  1 when dry
1613  */
1614 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1615 {
1616     int is_dry = 0;
1617     int sockflags = 0;
1618     int n, ret;
1619     union sctp_notification snp;
1620     struct msghdr msg;
1621     struct iovec iov;
1622 #  ifdef SCTP_EVENT
1623     struct sctp_event event;
1624 #  else
1625     struct sctp_event_subscribe event;
1626     socklen_t eventsize;
1627 #  endif
1628     bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1629
1630     /* set sender dry event */
1631 #  ifdef SCTP_EVENT
1632     memset(&event, 0, sizeof(event));
1633     event.se_assoc_id = 0;
1634     event.se_type = SCTP_SENDER_DRY_EVENT;
1635     event.se_on = 1;
1636     ret =
1637         setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1638                    sizeof(struct sctp_event));
1639 #  else
1640     eventsize = sizeof(struct sctp_event_subscribe);
1641     ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1642     if (ret < 0)
1643         return -1;
1644
1645     event.sctp_sender_dry_event = 1;
1646
1647     ret =
1648         setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1649                    sizeof(struct sctp_event_subscribe));
1650 #  endif
1651     if (ret < 0)
1652         return -1;
1653
1654     /* peek for notification */
1655     memset(&snp, 0, sizeof(snp));
1656     iov.iov_base = (char *)&snp;
1657     iov.iov_len = sizeof(union sctp_notification);
1658     msg.msg_name = NULL;
1659     msg.msg_namelen = 0;
1660     msg.msg_iov = &iov;
1661     msg.msg_iovlen = 1;
1662     msg.msg_control = NULL;
1663     msg.msg_controllen = 0;
1664     msg.msg_flags = 0;
1665
1666     n = recvmsg(b->num, &msg, MSG_PEEK);
1667     if (n <= 0) {
1668         if ((n < 0) && (get_last_socket_error() != EAGAIN)
1669             && (get_last_socket_error() != EWOULDBLOCK))
1670             return -1;
1671         else
1672             return 0;
1673     }
1674
1675     /* if we find a notification, process it and try again if necessary */
1676     while (msg.msg_flags & MSG_NOTIFICATION) {
1677         memset(&snp, 0, sizeof(snp));
1678         iov.iov_base = (char *)&snp;
1679         iov.iov_len = sizeof(union sctp_notification);
1680         msg.msg_name = NULL;
1681         msg.msg_namelen = 0;
1682         msg.msg_iov = &iov;
1683         msg.msg_iovlen = 1;
1684         msg.msg_control = NULL;
1685         msg.msg_controllen = 0;
1686         msg.msg_flags = 0;
1687
1688         n = recvmsg(b->num, &msg, 0);
1689         if (n <= 0) {
1690             if ((n < 0) && (get_last_socket_error() != EAGAIN)
1691                 && (get_last_socket_error() != EWOULDBLOCK))
1692                 return -1;
1693             else
1694                 return is_dry;
1695         }
1696
1697         if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1698             is_dry = 1;
1699
1700             /* disable sender dry event */
1701 #  ifdef SCTP_EVENT
1702             memset(&event, 0, sizeof(event));
1703             event.se_assoc_id = 0;
1704             event.se_type = SCTP_SENDER_DRY_EVENT;
1705             event.se_on = 0;
1706             ret =
1707                 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1708                            sizeof(struct sctp_event));
1709 #  else
1710             eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1711             ret =
1712                 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1713                            &eventsize);
1714             if (ret < 0)
1715                 return -1;
1716
1717             event.sctp_sender_dry_event = 0;
1718
1719             ret =
1720                 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1721                            sizeof(struct sctp_event_subscribe));
1722 #  endif
1723             if (ret < 0)
1724                 return -1;
1725         }
1726 #  ifdef SCTP_AUTHENTICATION_EVENT
1727         if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1728             dgram_sctp_handle_auth_free_key_event(b, &snp);
1729 #  endif
1730
1731         if (data->handle_notifications != NULL)
1732             data->handle_notifications(b, data->notification_context,
1733                                        (void *)&snp);
1734
1735         /* found notification, peek again */
1736         memset(&snp, 0, sizeof(snp));
1737         iov.iov_base = (char *)&snp;
1738         iov.iov_len = sizeof(union sctp_notification);
1739         msg.msg_name = NULL;
1740         msg.msg_namelen = 0;
1741         msg.msg_iov = &iov;
1742         msg.msg_iovlen = 1;
1743         msg.msg_control = NULL;
1744         msg.msg_controllen = 0;
1745         msg.msg_flags = 0;
1746
1747         /* if we have seen the dry already, don't wait */
1748         if (is_dry) {
1749             sockflags = fcntl(b->num, F_GETFL, 0);
1750             fcntl(b->num, F_SETFL, O_NONBLOCK);
1751         }
1752
1753         n = recvmsg(b->num, &msg, MSG_PEEK);
1754
1755         if (is_dry) {
1756             fcntl(b->num, F_SETFL, sockflags);
1757         }
1758
1759         if (n <= 0) {
1760             if ((n < 0) && (get_last_socket_error() != EAGAIN)
1761                 && (get_last_socket_error() != EWOULDBLOCK))
1762                 return -1;
1763             else
1764                 return is_dry;
1765         }
1766     }
1767
1768     /* read anything else */
1769     return is_dry;
1770 }
1771
1772 int BIO_dgram_sctp_msg_waiting(BIO *b)
1773 {
1774     int n, sockflags;
1775     union sctp_notification snp;
1776     struct msghdr msg;
1777     struct iovec iov;
1778     bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1779
1780     /* Check if there are any messages waiting to be read */
1781     do {
1782         memset(&snp, 0, sizeof(snp));
1783         iov.iov_base = (char *)&snp;
1784         iov.iov_len = sizeof(union sctp_notification);
1785         msg.msg_name = NULL;
1786         msg.msg_namelen = 0;
1787         msg.msg_iov = &iov;
1788         msg.msg_iovlen = 1;
1789         msg.msg_control = NULL;
1790         msg.msg_controllen = 0;
1791         msg.msg_flags = 0;
1792
1793         sockflags = fcntl(b->num, F_GETFL, 0);
1794         fcntl(b->num, F_SETFL, O_NONBLOCK);
1795         n = recvmsg(b->num, &msg, MSG_PEEK);
1796         fcntl(b->num, F_SETFL, sockflags);
1797
1798         /* if notification, process and try again */
1799         if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1800 #  ifdef SCTP_AUTHENTICATION_EVENT
1801             if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1802                 dgram_sctp_handle_auth_free_key_event(b, &snp);
1803 #  endif
1804
1805             memset(&snp, 0, sizeof(snp));
1806             iov.iov_base = (char *)&snp;
1807             iov.iov_len = sizeof(union sctp_notification);
1808             msg.msg_name = NULL;
1809             msg.msg_namelen = 0;
1810             msg.msg_iov = &iov;
1811             msg.msg_iovlen = 1;
1812             msg.msg_control = NULL;
1813             msg.msg_controllen = 0;
1814             msg.msg_flags = 0;
1815             n = recvmsg(b->num, &msg, 0);
1816
1817             if (data->handle_notifications != NULL)
1818                 data->handle_notifications(b, data->notification_context,
1819                                            (void *)&snp);
1820         }
1821
1822     } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1823
1824     /* Return 1 if there is a message to be read, return 0 otherwise. */
1825     if (n > 0)
1826         return 1;
1827     else
1828         return 0;
1829 }
1830
1831 static int dgram_sctp_puts(BIO *bp, const char *str)
1832 {
1833     int n, ret;
1834
1835     n = strlen(str);
1836     ret = dgram_sctp_write(bp, str, n);
1837     return (ret);
1838 }
1839 # endif
1840
1841 static int BIO_dgram_should_retry(int i)
1842 {
1843     int err;
1844
1845     if ((i == 0) || (i == -1)) {
1846         err = get_last_socket_error();
1847
1848 # if defined(OPENSSL_SYS_WINDOWS)
1849         /*
1850          * If the socket return value (i) is -1 and err is unexpectedly 0 at
1851          * this point, the error code was overwritten by another system call
1852          * before this error handling is called.
1853          */
1854 # endif
1855
1856         return (BIO_dgram_non_fatal_error(err));
1857     }
1858     return (0);
1859 }
1860
1861 int BIO_dgram_non_fatal_error(int err)
1862 {
1863     switch (err) {
1864 # if defined(OPENSSL_SYS_WINDOWS)
1865 #  if defined(WSAEWOULDBLOCK)
1866     case WSAEWOULDBLOCK:
1867 #  endif
1868 # endif
1869
1870 # ifdef EWOULDBLOCK
1871 #  ifdef WSAEWOULDBLOCK
1872 #   if WSAEWOULDBLOCK != EWOULDBLOCK
1873     case EWOULDBLOCK:
1874 #   endif
1875 #  else
1876     case EWOULDBLOCK:
1877 #  endif
1878 # endif
1879
1880 # ifdef EINTR
1881     case EINTR:
1882 # endif
1883
1884 # ifdef EAGAIN
1885 #  if EWOULDBLOCK != EAGAIN
1886     case EAGAIN:
1887 #  endif
1888 # endif
1889
1890 # ifdef EPROTO
1891     case EPROTO:
1892 # endif
1893
1894 # ifdef EINPROGRESS
1895     case EINPROGRESS:
1896 # endif
1897
1898 # ifdef EALREADY
1899     case EALREADY:
1900 # endif
1901
1902         return (1);
1903     default:
1904         break;
1905     }
1906     return (0);
1907 }
1908
1909 static void get_current_time(struct timeval *t)
1910 {
1911 # if defined(_WIN32)
1912     SYSTEMTIME st;
1913     union {
1914         unsigned __int64 ul;
1915         FILETIME ft;
1916     } now;
1917
1918     GetSystemTime(&st);
1919     SystemTimeToFileTime(&st, &now.ft);
1920 #  ifdef  __MINGW32__
1921     now.ul -= 116444736000000000ULL;
1922 #  else
1923     now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1924 #  endif
1925     t->tv_sec = (long)(now.ul / 10000000);
1926     t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1927 # else
1928     gettimeofday(t, NULL);
1929 # endif
1930 }
1931
1932 #endif