bss_dgram.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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. 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. /* fall-through */
  1337. case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
  1338. /* Returns 0 on success, -1 otherwise. */
  1339. /*
  1340. * Has this command really been called or is this just a
  1341. * fall-through?
  1342. */
  1343. if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
  1344. data->ccs_rcvd = 1;
  1345. /*
  1346. * CSS has been both, received and sent, so deactivate an old key
  1347. */
  1348. if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
  1349. /* Get active key */
  1350. sockopt_len = sizeof(struct sctp_authkeyid);
  1351. ret =
  1352. getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
  1353. &authkeyid, &sockopt_len);
  1354. if (ret < 0)
  1355. break;
  1356. /*
  1357. * Deactivate key or delete second last key if
  1358. * SCTP_AUTHENTICATION_EVENT is not available.
  1359. */
  1360. authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
  1361. # ifdef SCTP_AUTH_DEACTIVATE_KEY
  1362. sockopt_len = sizeof(struct sctp_authkeyid);
  1363. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
  1364. &authkeyid, sockopt_len);
  1365. if (ret < 0)
  1366. break;
  1367. # endif
  1368. # ifndef SCTP_AUTHENTICATION_EVENT
  1369. if (authkeyid.scact_keynumber > 0) {
  1370. authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
  1371. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
  1372. &authkeyid, sizeof(struct sctp_authkeyid));
  1373. if (ret < 0)
  1374. break;
  1375. }
  1376. # endif
  1377. data->ccs_rcvd = 0;
  1378. data->ccs_sent = 0;
  1379. }
  1380. break;
  1381. case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
  1382. /* Returns the size of the copied struct. */
  1383. if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
  1384. num = sizeof(struct bio_dgram_sctp_sndinfo);
  1385. memcpy(ptr, &(data->sndinfo), num);
  1386. ret = num;
  1387. break;
  1388. case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
  1389. /* Returns the size of the copied struct. */
  1390. if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
  1391. num = sizeof(struct bio_dgram_sctp_sndinfo);
  1392. memcpy(&(data->sndinfo), ptr, num);
  1393. break;
  1394. case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
  1395. /* Returns the size of the copied struct. */
  1396. if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
  1397. num = sizeof(struct bio_dgram_sctp_rcvinfo);
  1398. memcpy(ptr, &data->rcvinfo, num);
  1399. ret = num;
  1400. break;
  1401. case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
  1402. /* Returns the size of the copied struct. */
  1403. if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
  1404. num = sizeof(struct bio_dgram_sctp_rcvinfo);
  1405. memcpy(&(data->rcvinfo), ptr, num);
  1406. break;
  1407. case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
  1408. /* Returns the size of the copied struct. */
  1409. if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
  1410. num = sizeof(struct bio_dgram_sctp_prinfo);
  1411. memcpy(ptr, &(data->prinfo), num);
  1412. ret = num;
  1413. break;
  1414. case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
  1415. /* Returns the size of the copied struct. */
  1416. if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
  1417. num = sizeof(struct bio_dgram_sctp_prinfo);
  1418. memcpy(&(data->prinfo), ptr, num);
  1419. break;
  1420. case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
  1421. /* Returns always 1. */
  1422. if (num > 0)
  1423. data->save_shutdown = 1;
  1424. else
  1425. data->save_shutdown = 0;
  1426. break;
  1427. default:
  1428. /*
  1429. * Pass to default ctrl function to process SCTP unspecific commands
  1430. */
  1431. ret = dgram_ctrl(b, cmd, num, ptr);
  1432. break;
  1433. }
  1434. return ret;
  1435. }
  1436. int BIO_dgram_sctp_notification_cb(BIO *b,
  1437. void (*handle_notifications) (BIO *bio,
  1438. void
  1439. *context,
  1440. void *buf),
  1441. void *context)
  1442. {
  1443. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  1444. if (handle_notifications != NULL) {
  1445. data->handle_notifications = handle_notifications;
  1446. data->notification_context = context;
  1447. } else
  1448. return -1;
  1449. return 0;
  1450. }
  1451. /*
  1452. * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
  1453. * @b: The BIO to check for the dry event
  1454. *
  1455. * Wait until the peer confirms all packets have been received, and so that
  1456. * our kernel doesn't have anything to send anymore. This is only received by
  1457. * the peer's kernel, not the application.
  1458. *
  1459. * Returns:
  1460. * -1 on error
  1461. * 0 when not dry yet
  1462. * 1 when dry
  1463. */
  1464. int BIO_dgram_sctp_wait_for_dry(BIO *b)
  1465. {
  1466. int is_dry = 0;
  1467. int sockflags = 0;
  1468. int n, ret;
  1469. union sctp_notification snp;
  1470. struct msghdr msg;
  1471. struct iovec iov;
  1472. # ifdef SCTP_EVENT
  1473. struct sctp_event event;
  1474. # else
  1475. struct sctp_event_subscribe event;
  1476. socklen_t eventsize;
  1477. # endif
  1478. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  1479. /* set sender dry event */
  1480. # ifdef SCTP_EVENT
  1481. memset(&event, 0, sizeof(event));
  1482. event.se_assoc_id = 0;
  1483. event.se_type = SCTP_SENDER_DRY_EVENT;
  1484. event.se_on = 1;
  1485. ret =
  1486. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
  1487. sizeof(struct sctp_event));
  1488. # else
  1489. eventsize = sizeof(struct sctp_event_subscribe);
  1490. ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
  1491. if (ret < 0)
  1492. return -1;
  1493. event.sctp_sender_dry_event = 1;
  1494. ret =
  1495. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1496. sizeof(struct sctp_event_subscribe));
  1497. # endif
  1498. if (ret < 0)
  1499. return -1;
  1500. /* peek for notification */
  1501. memset(&snp, 0, sizeof(snp));
  1502. iov.iov_base = (char *)&snp;
  1503. iov.iov_len = sizeof(union sctp_notification);
  1504. msg.msg_name = NULL;
  1505. msg.msg_namelen = 0;
  1506. msg.msg_iov = &iov;
  1507. msg.msg_iovlen = 1;
  1508. msg.msg_control = NULL;
  1509. msg.msg_controllen = 0;
  1510. msg.msg_flags = 0;
  1511. n = recvmsg(b->num, &msg, MSG_PEEK);
  1512. if (n <= 0) {
  1513. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  1514. && (get_last_socket_error() != EWOULDBLOCK))
  1515. return -1;
  1516. else
  1517. return 0;
  1518. }
  1519. /* if we find a notification, process it and try again if necessary */
  1520. while (msg.msg_flags & MSG_NOTIFICATION) {
  1521. memset(&snp, 0, sizeof(snp));
  1522. iov.iov_base = (char *)&snp;
  1523. iov.iov_len = sizeof(union sctp_notification);
  1524. msg.msg_name = NULL;
  1525. msg.msg_namelen = 0;
  1526. msg.msg_iov = &iov;
  1527. msg.msg_iovlen = 1;
  1528. msg.msg_control = NULL;
  1529. msg.msg_controllen = 0;
  1530. msg.msg_flags = 0;
  1531. n = recvmsg(b->num, &msg, 0);
  1532. if (n <= 0) {
  1533. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  1534. && (get_last_socket_error() != EWOULDBLOCK))
  1535. return -1;
  1536. else
  1537. return is_dry;
  1538. }
  1539. if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
  1540. is_dry = 1;
  1541. /* disable sender dry event */
  1542. # ifdef SCTP_EVENT
  1543. memset(&event, 0, sizeof(event));
  1544. event.se_assoc_id = 0;
  1545. event.se_type = SCTP_SENDER_DRY_EVENT;
  1546. event.se_on = 0;
  1547. ret =
  1548. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
  1549. sizeof(struct sctp_event));
  1550. # else
  1551. eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
  1552. ret =
  1553. getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1554. &eventsize);
  1555. if (ret < 0)
  1556. return -1;
  1557. event.sctp_sender_dry_event = 0;
  1558. ret =
  1559. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1560. sizeof(struct sctp_event_subscribe));
  1561. # endif
  1562. if (ret < 0)
  1563. return -1;
  1564. }
  1565. # ifdef SCTP_AUTHENTICATION_EVENT
  1566. if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
  1567. dgram_sctp_handle_auth_free_key_event(b, &snp);
  1568. # endif
  1569. if (data->handle_notifications != NULL)
  1570. data->handle_notifications(b, data->notification_context,
  1571. (void *)&snp);
  1572. /* found notification, peek again */
  1573. memset(&snp, 0, sizeof(snp));
  1574. iov.iov_base = (char *)&snp;
  1575. iov.iov_len = sizeof(union sctp_notification);
  1576. msg.msg_name = NULL;
  1577. msg.msg_namelen = 0;
  1578. msg.msg_iov = &iov;
  1579. msg.msg_iovlen = 1;
  1580. msg.msg_control = NULL;
  1581. msg.msg_controllen = 0;
  1582. msg.msg_flags = 0;
  1583. /* if we have seen the dry already, don't wait */
  1584. if (is_dry) {
  1585. sockflags = fcntl(b->num, F_GETFL, 0);
  1586. fcntl(b->num, F_SETFL, O_NONBLOCK);
  1587. }
  1588. n = recvmsg(b->num, &msg, MSG_PEEK);
  1589. if (is_dry) {
  1590. fcntl(b->num, F_SETFL, sockflags);
  1591. }
  1592. if (n <= 0) {
  1593. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  1594. && (get_last_socket_error() != EWOULDBLOCK))
  1595. return -1;
  1596. else
  1597. return is_dry;
  1598. }
  1599. }
  1600. /* read anything else */
  1601. return is_dry;
  1602. }
  1603. int BIO_dgram_sctp_msg_waiting(BIO *b)
  1604. {
  1605. int n, sockflags;
  1606. union sctp_notification snp;
  1607. struct msghdr msg;
  1608. struct iovec iov;
  1609. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  1610. /* Check if there are any messages waiting to be read */
  1611. do {
  1612. memset(&snp, 0, sizeof(snp));
  1613. iov.iov_base = (char *)&snp;
  1614. iov.iov_len = sizeof(union sctp_notification);
  1615. msg.msg_name = NULL;
  1616. msg.msg_namelen = 0;
  1617. msg.msg_iov = &iov;
  1618. msg.msg_iovlen = 1;
  1619. msg.msg_control = NULL;
  1620. msg.msg_controllen = 0;
  1621. msg.msg_flags = 0;
  1622. sockflags = fcntl(b->num, F_GETFL, 0);
  1623. fcntl(b->num, F_SETFL, O_NONBLOCK);
  1624. n = recvmsg(b->num, &msg, MSG_PEEK);
  1625. fcntl(b->num, F_SETFL, sockflags);
  1626. /* if notification, process and try again */
  1627. if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
  1628. # ifdef SCTP_AUTHENTICATION_EVENT
  1629. if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
  1630. dgram_sctp_handle_auth_free_key_event(b, &snp);
  1631. # endif
  1632. memset(&snp, 0, sizeof(snp));
  1633. iov.iov_base = (char *)&snp;
  1634. iov.iov_len = sizeof(union sctp_notification);
  1635. msg.msg_name = NULL;
  1636. msg.msg_namelen = 0;
  1637. msg.msg_iov = &iov;
  1638. msg.msg_iovlen = 1;
  1639. msg.msg_control = NULL;
  1640. msg.msg_controllen = 0;
  1641. msg.msg_flags = 0;
  1642. n = recvmsg(b->num, &msg, 0);
  1643. if (data->handle_notifications != NULL)
  1644. data->handle_notifications(b, data->notification_context,
  1645. (void *)&snp);
  1646. }
  1647. } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
  1648. /* Return 1 if there is a message to be read, return 0 otherwise. */
  1649. if (n > 0)
  1650. return 1;
  1651. else
  1652. return 0;
  1653. }
  1654. static int dgram_sctp_puts(BIO *bp, const char *str)
  1655. {
  1656. int n, ret;
  1657. n = strlen(str);
  1658. ret = dgram_sctp_write(bp, str, n);
  1659. return ret;
  1660. }
  1661. # endif
  1662. static int BIO_dgram_should_retry(int i)
  1663. {
  1664. int err;
  1665. if ((i == 0) || (i == -1)) {
  1666. err = get_last_socket_error();
  1667. # if defined(OPENSSL_SYS_WINDOWS)
  1668. /*
  1669. * If the socket return value (i) is -1 and err is unexpectedly 0 at
  1670. * this point, the error code was overwritten by another system call
  1671. * before this error handling is called.
  1672. */
  1673. # endif
  1674. return BIO_dgram_non_fatal_error(err);
  1675. }
  1676. return 0;
  1677. }
  1678. int BIO_dgram_non_fatal_error(int err)
  1679. {
  1680. switch (err) {
  1681. # if defined(OPENSSL_SYS_WINDOWS)
  1682. # if defined(WSAEWOULDBLOCK)
  1683. case WSAEWOULDBLOCK:
  1684. # endif
  1685. # endif
  1686. # ifdef EWOULDBLOCK
  1687. # ifdef WSAEWOULDBLOCK
  1688. # if WSAEWOULDBLOCK != EWOULDBLOCK
  1689. case EWOULDBLOCK:
  1690. # endif
  1691. # else
  1692. case EWOULDBLOCK:
  1693. # endif
  1694. # endif
  1695. # ifdef EINTR
  1696. case EINTR:
  1697. # endif
  1698. # ifdef EAGAIN
  1699. # if EWOULDBLOCK != EAGAIN
  1700. case EAGAIN:
  1701. # endif
  1702. # endif
  1703. # ifdef EPROTO
  1704. case EPROTO:
  1705. # endif
  1706. # ifdef EINPROGRESS
  1707. case EINPROGRESS:
  1708. # endif
  1709. # ifdef EALREADY
  1710. case EALREADY:
  1711. # endif
  1712. return 1;
  1713. default:
  1714. break;
  1715. }
  1716. return 0;
  1717. }
  1718. static void get_current_time(struct timeval *t)
  1719. {
  1720. # if defined(_WIN32)
  1721. SYSTEMTIME st;
  1722. union {
  1723. unsigned __int64 ul;
  1724. FILETIME ft;
  1725. } now;
  1726. GetSystemTime(&st);
  1727. SystemTimeToFileTime(&st, &now.ft);
  1728. # ifdef __MINGW32__
  1729. now.ul -= 116444736000000000ULL;
  1730. # else
  1731. now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
  1732. # endif
  1733. t->tv_sec = (long)(now.ul / 10000000);
  1734. t->tv_usec = ((int)(now.ul % 10000000)) / 10;
  1735. # else
  1736. gettimeofday(t, NULL);
  1737. # endif
  1738. }
  1739. #endif