2
0

bss_dgram.c 57 KB

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