bss_dgram.c 57 KB

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