bss_dgram.c 62 KB

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