2
0

bss_dgram.c 57 KB

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