bss_dgram.c 56 KB

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