bss_dgram.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842
  1. /*
  2. * Copyright 2005-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef _GNU_SOURCE
  10. # define _GNU_SOURCE
  11. #endif
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include "internal/time.h"
  15. #include "bio_local.h"
  16. #ifndef OPENSSL_NO_DGRAM
  17. # ifndef OPENSSL_NO_SCTP
  18. # include <netinet/sctp.h>
  19. # include <fcntl.h>
  20. # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
  21. # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
  22. # endif
  23. # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
  24. # define IP_MTU 14 /* linux is lame */
  25. # endif
  26. # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
  27. # define IPPROTO_IPV6 41 /* windows is lame */
  28. # endif
  29. # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
  30. /* Standard definition causes type-punning problems. */
  31. # undef IN6_IS_ADDR_V4MAPPED
  32. # define s6_addr32 __u6_addr.__u6_addr32
  33. # define IN6_IS_ADDR_V4MAPPED(a) \
  34. (((a)->s6_addr32[0] == 0) && \
  35. ((a)->s6_addr32[1] == 0) && \
  36. ((a)->s6_addr32[2] == htonl(0x0000ffff)))
  37. # endif
  38. /* Determine what method to use for BIO_sendmmsg and BIO_recvmmsg. */
  39. # define M_METHOD_NONE 0
  40. # define M_METHOD_RECVMMSG 1
  41. # define M_METHOD_RECVMSG 2
  42. # define M_METHOD_RECVFROM 3
  43. # define M_METHOD_WSARECVMSG 4
  44. # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  45. # if !(__GLIBC_PREREQ(2, 14))
  46. # undef NO_RECVMMSG
  47. /*
  48. * Some old glibc versions may have recvmmsg and MSG_WAITFORONE flag, but
  49. * not sendmmsg. We need both so force this to be disabled on these old
  50. * versions
  51. */
  52. # define NO_RECVMMSG
  53. # endif
  54. # endif
  55. # if defined(__GNU__)
  56. /* GNU/Hurd does not have IP_PKTINFO yet */
  57. #undef NO_RECVMSG
  58. #define NO_RECVMSG
  59. # endif
  60. # if !defined(M_METHOD)
  61. # if defined(OPENSSL_SYS_WINDOWS) && defined(BIO_HAVE_WSAMSG) && !defined(NO_WSARECVMSG)
  62. # define M_METHOD M_METHOD_WSARECVMSG
  63. # elif !defined(OPENSSL_SYS_WINDOWS) && defined(MSG_WAITFORONE) && !defined(NO_RECVMMSG)
  64. # define M_METHOD M_METHOD_RECVMMSG
  65. # elif !defined(OPENSSL_SYS_WINDOWS) && defined(CMSG_LEN) && !defined(NO_RECVMSG)
  66. # define M_METHOD M_METHOD_RECVMSG
  67. # elif !defined(NO_RECVFROM)
  68. # define M_METHOD M_METHOD_RECVFROM
  69. # else
  70. # define M_METHOD M_METHOD_NONE
  71. # endif
  72. # endif
  73. # if defined(OPENSSL_SYS_WINDOWS)
  74. # define BIO_CMSG_SPACE(x) WSA_CMSG_SPACE(x)
  75. # define BIO_CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
  76. # define BIO_CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
  77. # define BIO_CMSG_DATA(x) WSA_CMSG_DATA(x)
  78. # define BIO_CMSG_LEN(x) WSA_CMSG_LEN(x)
  79. # define MSGHDR_TYPE WSAMSG
  80. # define CMSGHDR_TYPE WSACMSGHDR
  81. # else
  82. # define MSGHDR_TYPE struct msghdr
  83. # define CMSGHDR_TYPE struct cmsghdr
  84. # define BIO_CMSG_SPACE(x) CMSG_SPACE(x)
  85. # define BIO_CMSG_FIRSTHDR(x) CMSG_FIRSTHDR(x)
  86. # define BIO_CMSG_NXTHDR(x, y) CMSG_NXTHDR(x, y)
  87. # define BIO_CMSG_DATA(x) CMSG_DATA(x)
  88. # define BIO_CMSG_LEN(x) CMSG_LEN(x)
  89. # endif
  90. # if M_METHOD == M_METHOD_RECVMMSG \
  91. || M_METHOD == M_METHOD_RECVMSG \
  92. || M_METHOD == M_METHOD_WSARECVMSG
  93. # if defined(__APPLE__)
  94. /*
  95. * CMSG_SPACE is not a constant expresson on OSX even though POSIX
  96. * says it's supposed to be. This should be adequate.
  97. */
  98. # define BIO_CMSG_ALLOC_LEN 64
  99. # else
  100. # if defined(IPV6_PKTINFO)
  101. # define BIO_CMSG_ALLOC_LEN_1 BIO_CMSG_SPACE(sizeof(struct in6_pktinfo))
  102. # else
  103. # define BIO_CMSG_ALLOC_LEN_1 0
  104. # endif
  105. # if defined(IP_PKTINFO)
  106. # define BIO_CMSG_ALLOC_LEN_2 BIO_CMSG_SPACE(sizeof(struct in_pktinfo))
  107. # else
  108. # define BIO_CMSG_ALLOC_LEN_2 0
  109. # endif
  110. # if defined(IP_RECVDSTADDR)
  111. # define BIO_CMSG_ALLOC_LEN_3 BIO_CMSG_SPACE(sizeof(struct in_addr))
  112. # else
  113. # define BIO_CMSG_ALLOC_LEN_3 0
  114. # endif
  115. # define BIO_MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  116. # define BIO_CMSG_ALLOC_LEN \
  117. BIO_MAX(BIO_CMSG_ALLOC_LEN_1, \
  118. BIO_MAX(BIO_CMSG_ALLOC_LEN_2, BIO_CMSG_ALLOC_LEN_3))
  119. # endif
  120. # if (defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)) && defined(IPV6_RECVPKTINFO)
  121. # define SUPPORT_LOCAL_ADDR
  122. # endif
  123. # endif
  124. # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
  125. static int dgram_write(BIO *h, const char *buf, int num);
  126. static int dgram_read(BIO *h, char *buf, int size);
  127. static int dgram_puts(BIO *h, const char *str);
  128. static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  129. static int dgram_new(BIO *h);
  130. static int dgram_free(BIO *data);
  131. static int dgram_clear(BIO *bio);
  132. static int dgram_sendmmsg(BIO *b, BIO_MSG *msg,
  133. size_t stride, size_t num_msg,
  134. uint64_t flags, size_t *num_processed);
  135. static int dgram_recvmmsg(BIO *b, BIO_MSG *msg,
  136. size_t stride, size_t num_msg,
  137. uint64_t flags, size_t *num_processed);
  138. # ifndef OPENSSL_NO_SCTP
  139. static int dgram_sctp_write(BIO *h, const char *buf, int num);
  140. static int dgram_sctp_read(BIO *h, char *buf, int size);
  141. static int dgram_sctp_puts(BIO *h, const char *str);
  142. static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  143. static int dgram_sctp_new(BIO *h);
  144. static int dgram_sctp_free(BIO *data);
  145. static int dgram_sctp_wait_for_dry(BIO *b);
  146. static int dgram_sctp_msg_waiting(BIO *b);
  147. # ifdef SCTP_AUTHENTICATION_EVENT
  148. static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
  149. *snp);
  150. # endif
  151. # endif
  152. static int BIO_dgram_should_retry(int s);
  153. static const BIO_METHOD methods_dgramp = {
  154. BIO_TYPE_DGRAM,
  155. "datagram socket",
  156. bwrite_conv,
  157. dgram_write,
  158. bread_conv,
  159. dgram_read,
  160. dgram_puts,
  161. NULL, /* dgram_gets, */
  162. dgram_ctrl,
  163. dgram_new,
  164. dgram_free,
  165. NULL, /* dgram_callback_ctrl */
  166. dgram_sendmmsg,
  167. dgram_recvmmsg,
  168. };
  169. # ifndef OPENSSL_NO_SCTP
  170. static const BIO_METHOD methods_dgramp_sctp = {
  171. BIO_TYPE_DGRAM_SCTP,
  172. "datagram sctp socket",
  173. bwrite_conv,
  174. dgram_sctp_write,
  175. bread_conv,
  176. dgram_sctp_read,
  177. dgram_sctp_puts,
  178. NULL, /* dgram_gets, */
  179. dgram_sctp_ctrl,
  180. dgram_sctp_new,
  181. dgram_sctp_free,
  182. NULL, /* dgram_callback_ctrl */
  183. NULL, /* sendmmsg */
  184. NULL, /* recvmmsg */
  185. };
  186. # endif
  187. typedef struct bio_dgram_data_st {
  188. BIO_ADDR peer;
  189. BIO_ADDR local_addr;
  190. unsigned int connected;
  191. unsigned int _errno;
  192. unsigned int mtu;
  193. OSSL_TIME next_timeout;
  194. OSSL_TIME socket_timeout;
  195. unsigned int peekmode;
  196. char local_addr_enabled;
  197. } bio_dgram_data;
  198. # ifndef OPENSSL_NO_SCTP
  199. typedef struct bio_dgram_sctp_save_message_st {
  200. BIO *bio;
  201. char *data;
  202. int length;
  203. } bio_dgram_sctp_save_message;
  204. /*
  205. * Note: bio_dgram_data must be first here
  206. * as we use dgram_ctrl for underlying dgram operations
  207. * which will cast this struct to a bio_dgram_data
  208. */
  209. typedef struct bio_dgram_sctp_data_st {
  210. bio_dgram_data dgram;
  211. struct bio_dgram_sctp_sndinfo sndinfo;
  212. struct bio_dgram_sctp_rcvinfo rcvinfo;
  213. struct bio_dgram_sctp_prinfo prinfo;
  214. BIO_dgram_sctp_notification_handler_fn handle_notifications;
  215. void *notification_context;
  216. int in_handshake;
  217. int ccs_rcvd;
  218. int ccs_sent;
  219. int save_shutdown;
  220. int peer_auth_tested;
  221. } bio_dgram_sctp_data;
  222. # endif
  223. const BIO_METHOD *BIO_s_datagram(void)
  224. {
  225. return &methods_dgramp;
  226. }
  227. BIO *BIO_new_dgram(int fd, int close_flag)
  228. {
  229. BIO *ret;
  230. ret = BIO_new(BIO_s_datagram());
  231. if (ret == NULL)
  232. return NULL;
  233. BIO_set_fd(ret, fd, close_flag);
  234. return ret;
  235. }
  236. static int dgram_new(BIO *bi)
  237. {
  238. bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
  239. if (data == NULL)
  240. return 0;
  241. bi->ptr = data;
  242. return 1;
  243. }
  244. static int dgram_free(BIO *a)
  245. {
  246. bio_dgram_data *data;
  247. if (a == NULL)
  248. return 0;
  249. if (!dgram_clear(a))
  250. return 0;
  251. data = (bio_dgram_data *)a->ptr;
  252. OPENSSL_free(data);
  253. return 1;
  254. }
  255. static int dgram_clear(BIO *a)
  256. {
  257. if (a == NULL)
  258. return 0;
  259. if (a->shutdown) {
  260. if (a->init) {
  261. BIO_closesocket(a->num);
  262. }
  263. a->init = 0;
  264. a->flags = 0;
  265. }
  266. return 1;
  267. }
  268. static void dgram_adjust_rcv_timeout(BIO *b)
  269. {
  270. # if defined(SO_RCVTIMEO)
  271. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  272. OSSL_TIME timeleft;
  273. /* Is a timer active? */
  274. if (!ossl_time_is_zero(data->next_timeout)) {
  275. /* Read current socket timeout */
  276. # ifdef OPENSSL_SYS_WINDOWS
  277. int timeout;
  278. int sz = sizeof(timeout);
  279. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  280. (void *)&timeout, &sz) < 0)
  281. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  282. "calling getsockopt()");
  283. else
  284. data->socket_timeout = ossl_ms2time(timeout);
  285. # else
  286. struct timeval tv;
  287. socklen_t sz = sizeof(tv);
  288. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv, &sz) < 0)
  289. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  290. "calling getsockopt()");
  291. else
  292. data->socket_timeout = ossl_time_from_timeval(tv);
  293. # endif
  294. /* Calculate time left until timer expires */
  295. timeleft = ossl_time_subtract(data->next_timeout, ossl_time_now());
  296. if (ossl_time_compare(timeleft, ossl_ticks2time(OSSL_TIME_US)) < 0)
  297. timeleft = ossl_ticks2time(OSSL_TIME_US);
  298. /*
  299. * Adjust socket timeout if next handshake message timer will expire
  300. * earlier.
  301. */
  302. if (ossl_time_is_zero(data->socket_timeout)
  303. || ossl_time_compare(data->socket_timeout, timeleft) >= 0) {
  304. # ifdef OPENSSL_SYS_WINDOWS
  305. timeout = (int)ossl_time2ms(timeleft);
  306. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  307. (void *)&timeout, sizeof(timeout)) < 0)
  308. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  309. "calling setsockopt()");
  310. # else
  311. tv = ossl_time_to_timeval(timeleft);
  312. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv,
  313. sizeof(tv)) < 0)
  314. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  315. "calling setsockopt()");
  316. # endif
  317. }
  318. }
  319. # endif
  320. }
  321. static void dgram_update_local_addr(BIO *b)
  322. {
  323. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  324. socklen_t addr_len = sizeof(data->local_addr);
  325. if (getsockname(b->num, &data->local_addr.sa, &addr_len) < 0)
  326. /*
  327. * This should not be possible, but zero-initialize and return
  328. * anyway.
  329. */
  330. BIO_ADDR_clear(&data->local_addr);
  331. }
  332. # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
  333. static int dgram_get_sock_family(BIO *b)
  334. {
  335. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  336. return data->local_addr.sa.sa_family;
  337. }
  338. # endif
  339. static void dgram_reset_rcv_timeout(BIO *b)
  340. {
  341. # if defined(SO_RCVTIMEO)
  342. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  343. /* Is a timer active? */
  344. if (!ossl_time_is_zero(data->next_timeout)) {
  345. # ifdef OPENSSL_SYS_WINDOWS
  346. int timeout = (int)ossl_time2ms(data->socket_timeout);
  347. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  348. (void *)&timeout, sizeof(timeout)) < 0)
  349. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  350. "calling setsockopt()");
  351. # else
  352. struct timeval tv = ossl_time_to_timeval(data->socket_timeout);
  353. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
  354. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  355. "calling setsockopt()");
  356. # endif
  357. }
  358. # endif
  359. }
  360. static int dgram_read(BIO *b, char *out, int outl)
  361. {
  362. int ret = 0;
  363. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  364. int flags = 0;
  365. BIO_ADDR peer;
  366. socklen_t len = sizeof(peer);
  367. if (out != NULL) {
  368. clear_socket_error();
  369. BIO_ADDR_clear(&peer);
  370. dgram_adjust_rcv_timeout(b);
  371. if (data->peekmode)
  372. flags = MSG_PEEK;
  373. ret = recvfrom(b->num, out, outl, flags,
  374. BIO_ADDR_sockaddr_noconst(&peer), &len);
  375. if (!data->connected && ret >= 0)
  376. BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
  377. BIO_clear_retry_flags(b);
  378. if (ret < 0) {
  379. if (BIO_dgram_should_retry(ret)) {
  380. BIO_set_retry_read(b);
  381. data->_errno = get_last_socket_error();
  382. }
  383. }
  384. dgram_reset_rcv_timeout(b);
  385. }
  386. return ret;
  387. }
  388. static int dgram_write(BIO *b, const char *in, int inl)
  389. {
  390. int ret;
  391. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  392. clear_socket_error();
  393. if (data->connected)
  394. ret = writesocket(b->num, in, inl);
  395. else {
  396. int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
  397. ret = sendto(b->num, in, inl, 0,
  398. BIO_ADDR_sockaddr(&data->peer), peerlen);
  399. }
  400. BIO_clear_retry_flags(b);
  401. if (ret <= 0) {
  402. if (BIO_dgram_should_retry(ret)) {
  403. BIO_set_retry_write(b);
  404. data->_errno = get_last_socket_error();
  405. }
  406. }
  407. return ret;
  408. }
  409. static long dgram_get_mtu_overhead(bio_dgram_data *data)
  410. {
  411. long ret;
  412. switch (BIO_ADDR_family(&data->peer)) {
  413. case AF_INET:
  414. /*
  415. * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
  416. */
  417. ret = 28;
  418. break;
  419. # if OPENSSL_USE_IPV6
  420. case AF_INET6:
  421. {
  422. # ifdef IN6_IS_ADDR_V4MAPPED
  423. struct in6_addr tmp_addr;
  424. if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
  425. && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
  426. /*
  427. * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
  428. */
  429. ret = 28;
  430. else
  431. # endif
  432. /*
  433. * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
  434. */
  435. ret = 48;
  436. }
  437. break;
  438. # endif
  439. default:
  440. /* We don't know. Go with the historical default */
  441. ret = 28;
  442. break;
  443. }
  444. return ret;
  445. }
  446. /* Enables appropriate destination address reception option on the socket. */
  447. # if defined(SUPPORT_LOCAL_ADDR)
  448. static int enable_local_addr(BIO *b, int enable) {
  449. int af = dgram_get_sock_family(b);
  450. if (af == AF_INET) {
  451. # if defined(IP_PKTINFO)
  452. /* IP_PKTINFO is preferred */
  453. if (setsockopt(b->num, IPPROTO_IP, IP_PKTINFO,
  454. (void *)&enable, sizeof(enable)) < 0)
  455. return 0;
  456. return 1;
  457. # elif defined(IP_RECVDSTADDR)
  458. /* Fall back to IP_RECVDSTADDR */
  459. if (setsockopt(b->num, IPPROTO_IP, IP_RECVDSTADDR,
  460. &enable, sizeof(enable)) < 0)
  461. return 0;
  462. return 1;
  463. # endif
  464. }
  465. # if OPENSSL_USE_IPV6
  466. if (af == AF_INET6) {
  467. # if defined(IPV6_RECVPKTINFO)
  468. if (setsockopt(b->num, IPPROTO_IPV6, IPV6_RECVPKTINFO,
  469. &enable, sizeof(enable)) < 0)
  470. return 0;
  471. return 1;
  472. # endif
  473. }
  474. # endif
  475. return 0;
  476. }
  477. # endif
  478. static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
  479. {
  480. long ret = 1;
  481. int *ip;
  482. bio_dgram_data *data = NULL;
  483. # ifndef __DJGPP__
  484. /* There are currently no cases where this is used on djgpp/watt32. */
  485. int sockopt_val = 0;
  486. # endif
  487. int d_errno;
  488. # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
  489. socklen_t sockopt_len; /* assume that system supporting IP_MTU is
  490. * modern enough to define socklen_t */
  491. socklen_t addr_len;
  492. BIO_ADDR addr;
  493. # endif
  494. data = (bio_dgram_data *)b->ptr;
  495. switch (cmd) {
  496. case BIO_CTRL_RESET:
  497. num = 0;
  498. ret = 0;
  499. break;
  500. case BIO_CTRL_INFO:
  501. ret = 0;
  502. break;
  503. case BIO_C_SET_FD:
  504. dgram_clear(b);
  505. b->num = *((int *)ptr);
  506. b->shutdown = (int)num;
  507. b->init = 1;
  508. dgram_update_local_addr(b);
  509. # if defined(SUPPORT_LOCAL_ADDR)
  510. if (data->local_addr_enabled) {
  511. if (enable_local_addr(b, 1) < 1)
  512. data->local_addr_enabled = 0;
  513. }
  514. # endif
  515. break;
  516. case BIO_C_GET_FD:
  517. if (b->init) {
  518. ip = (int *)ptr;
  519. if (ip != NULL)
  520. *ip = b->num;
  521. ret = b->num;
  522. } else
  523. ret = -1;
  524. break;
  525. case BIO_CTRL_GET_CLOSE:
  526. ret = b->shutdown;
  527. break;
  528. case BIO_CTRL_SET_CLOSE:
  529. b->shutdown = (int)num;
  530. break;
  531. case BIO_CTRL_PENDING:
  532. case BIO_CTRL_WPENDING:
  533. ret = 0;
  534. break;
  535. case BIO_CTRL_DUP:
  536. case BIO_CTRL_FLUSH:
  537. ret = 1;
  538. break;
  539. case BIO_CTRL_DGRAM_CONNECT:
  540. BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
  541. break;
  542. /* (Linux)kernel sets DF bit on outgoing IP packets */
  543. case BIO_CTRL_DGRAM_MTU_DISCOVER:
  544. # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
  545. addr_len = (socklen_t) sizeof(addr);
  546. BIO_ADDR_clear(&addr);
  547. if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
  548. ret = 0;
  549. break;
  550. }
  551. switch (addr.sa.sa_family) {
  552. case AF_INET:
  553. sockopt_val = IP_PMTUDISC_DO;
  554. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
  555. &sockopt_val, sizeof(sockopt_val))) < 0)
  556. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  557. "calling setsockopt()");
  558. break;
  559. # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
  560. case AF_INET6:
  561. sockopt_val = IPV6_PMTUDISC_DO;
  562. if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
  563. &sockopt_val, sizeof(sockopt_val))) < 0)
  564. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  565. "calling setsockopt()");
  566. break;
  567. # endif
  568. default:
  569. ret = -1;
  570. break;
  571. }
  572. # else
  573. ret = -1;
  574. # endif
  575. break;
  576. case BIO_CTRL_DGRAM_QUERY_MTU:
  577. # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
  578. addr_len = (socklen_t) sizeof(addr);
  579. BIO_ADDR_clear(&addr);
  580. if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
  581. ret = 0;
  582. break;
  583. }
  584. sockopt_len = sizeof(sockopt_val);
  585. switch (addr.sa.sa_family) {
  586. case AF_INET:
  587. if ((ret =
  588. getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
  589. &sockopt_len)) < 0 || sockopt_val < 0) {
  590. ret = 0;
  591. } else {
  592. /*
  593. * we assume that the transport protocol is UDP and no IP
  594. * options are used.
  595. */
  596. data->mtu = sockopt_val - 8 - 20;
  597. ret = data->mtu;
  598. }
  599. break;
  600. # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
  601. case AF_INET6:
  602. if ((ret =
  603. getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
  604. (void *)&sockopt_val, &sockopt_len)) < 0
  605. || sockopt_val < 0) {
  606. ret = 0;
  607. } else {
  608. /*
  609. * we assume that the transport protocol is UDP and no IPV6
  610. * options are used.
  611. */
  612. data->mtu = sockopt_val - 8 - 40;
  613. ret = data->mtu;
  614. }
  615. break;
  616. # endif
  617. default:
  618. ret = 0;
  619. break;
  620. }
  621. # else
  622. ret = 0;
  623. # endif
  624. break;
  625. case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
  626. ret = -dgram_get_mtu_overhead(data);
  627. switch (BIO_ADDR_family(&data->peer)) {
  628. case AF_INET:
  629. ret += 576;
  630. break;
  631. # if OPENSSL_USE_IPV6
  632. case AF_INET6:
  633. {
  634. # ifdef IN6_IS_ADDR_V4MAPPED
  635. struct in6_addr tmp_addr;
  636. if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
  637. && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
  638. ret += 576;
  639. else
  640. # endif
  641. ret += 1280;
  642. }
  643. break;
  644. # endif
  645. default:
  646. ret += 576;
  647. break;
  648. }
  649. break;
  650. case BIO_CTRL_DGRAM_GET_MTU:
  651. return data->mtu;
  652. case BIO_CTRL_DGRAM_SET_MTU:
  653. data->mtu = num;
  654. ret = num;
  655. break;
  656. case BIO_CTRL_DGRAM_SET_CONNECTED:
  657. if (ptr != NULL) {
  658. data->connected = 1;
  659. BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
  660. } else {
  661. data->connected = 0;
  662. BIO_ADDR_clear(&data->peer);
  663. }
  664. break;
  665. case BIO_CTRL_DGRAM_GET_PEER:
  666. ret = BIO_ADDR_sockaddr_size(&data->peer);
  667. /* FIXME: if num < ret, we will only return part of an address.
  668. That should bee an error, no? */
  669. if (num == 0 || num > ret)
  670. num = ret;
  671. memcpy(ptr, &data->peer, (ret = num));
  672. break;
  673. case BIO_CTRL_DGRAM_SET_PEER:
  674. BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
  675. break;
  676. case BIO_CTRL_DGRAM_DETECT_PEER_ADDR:
  677. {
  678. BIO_ADDR xaddr, *p = &data->peer;
  679. socklen_t xaddr_len = sizeof(xaddr.sa);
  680. if (BIO_ADDR_family(p) == AF_UNSPEC) {
  681. if (getpeername(b->num, (void *)&xaddr.sa, &xaddr_len) == 0
  682. && BIO_ADDR_family(&xaddr) != AF_UNSPEC) {
  683. p = &xaddr;
  684. } else {
  685. ret = 0;
  686. break;
  687. }
  688. }
  689. ret = BIO_ADDR_sockaddr_size(p);
  690. if (num == 0 || num > ret)
  691. num = ret;
  692. memcpy(ptr, p, (ret = num));
  693. }
  694. break;
  695. case BIO_C_SET_NBIO:
  696. if (!BIO_socket_nbio(b->num, num != 0))
  697. ret = 0;
  698. break;
  699. case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
  700. data->next_timeout = ossl_time_from_timeval(*(struct timeval *)ptr);
  701. break;
  702. # if defined(SO_RCVTIMEO)
  703. case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
  704. # ifdef OPENSSL_SYS_WINDOWS
  705. {
  706. struct timeval *tv = (struct timeval *)ptr;
  707. int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  708. if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  709. (void *)&timeout, sizeof(timeout))) < 0)
  710. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  711. "calling setsockopt()");
  712. }
  713. # else
  714. if ((ret = setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
  715. sizeof(struct timeval))) < 0)
  716. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  717. "calling setsockopt()");
  718. # endif
  719. break;
  720. case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
  721. {
  722. # ifdef OPENSSL_SYS_WINDOWS
  723. int sz = 0;
  724. int timeout;
  725. struct timeval *tv = (struct timeval *)ptr;
  726. sz = sizeof(timeout);
  727. if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  728. (void *)&timeout, &sz)) < 0) {
  729. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  730. "calling getsockopt()");
  731. } else {
  732. tv->tv_sec = timeout / 1000;
  733. tv->tv_usec = (timeout % 1000) * 1000;
  734. ret = sizeof(*tv);
  735. }
  736. # else
  737. socklen_t sz = sizeof(struct timeval);
  738. if ((ret = getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  739. ptr, &sz)) < 0) {
  740. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  741. "calling getsockopt()");
  742. } else {
  743. OPENSSL_assert((size_t)sz <= sizeof(struct timeval));
  744. ret = (int)sz;
  745. }
  746. # endif
  747. }
  748. break;
  749. # endif
  750. # if defined(SO_SNDTIMEO)
  751. case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
  752. # ifdef OPENSSL_SYS_WINDOWS
  753. {
  754. struct timeval *tv = (struct timeval *)ptr;
  755. int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  756. if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  757. (void *)&timeout, sizeof(timeout))) < 0)
  758. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  759. "calling setsockopt()");
  760. }
  761. # else
  762. if ((ret = setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
  763. sizeof(struct timeval))) < 0)
  764. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  765. "calling setsockopt()");
  766. # endif
  767. break;
  768. case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
  769. {
  770. # ifdef OPENSSL_SYS_WINDOWS
  771. int sz = 0;
  772. int timeout;
  773. struct timeval *tv = (struct timeval *)ptr;
  774. sz = sizeof(timeout);
  775. if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  776. (void *)&timeout, &sz)) < 0) {
  777. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  778. "calling getsockopt()");
  779. } else {
  780. tv->tv_sec = timeout / 1000;
  781. tv->tv_usec = (timeout % 1000) * 1000;
  782. ret = sizeof(*tv);
  783. }
  784. # else
  785. socklen_t sz = sizeof(struct timeval);
  786. if ((ret = getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  787. ptr, &sz)) < 0) {
  788. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  789. "calling getsockopt()");
  790. } else {
  791. OPENSSL_assert((size_t)sz <= sizeof(struct timeval));
  792. ret = (int)sz;
  793. }
  794. # endif
  795. }
  796. break;
  797. # endif
  798. case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
  799. /* fall-through */
  800. case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
  801. # ifdef OPENSSL_SYS_WINDOWS
  802. d_errno = (data->_errno == WSAETIMEDOUT);
  803. # else
  804. d_errno = (data->_errno == EAGAIN);
  805. # endif
  806. if (d_errno) {
  807. ret = 1;
  808. data->_errno = 0;
  809. } else
  810. ret = 0;
  811. break;
  812. # ifdef EMSGSIZE
  813. case BIO_CTRL_DGRAM_MTU_EXCEEDED:
  814. if (data->_errno == EMSGSIZE) {
  815. ret = 1;
  816. data->_errno = 0;
  817. } else
  818. ret = 0;
  819. break;
  820. # endif
  821. case BIO_CTRL_DGRAM_SET_DONT_FRAG:
  822. switch (data->peer.sa.sa_family) {
  823. case AF_INET:
  824. # if defined(IP_DONTFRAG)
  825. sockopt_val = num ? 1 : 0;
  826. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
  827. &sockopt_val, sizeof(sockopt_val))) < 0)
  828. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  829. "calling setsockopt()");
  830. # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
  831. sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT;
  832. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
  833. &sockopt_val, sizeof(sockopt_val))) < 0)
  834. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  835. "calling setsockopt()");
  836. # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
  837. sockopt_val = num ? 1 : 0;
  838. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
  839. (const char *)&sockopt_val,
  840. sizeof(sockopt_val))) < 0)
  841. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  842. "calling setsockopt()");
  843. # else
  844. ret = -1;
  845. # endif
  846. break;
  847. # if OPENSSL_USE_IPV6
  848. case AF_INET6:
  849. # if defined(IPV6_DONTFRAG)
  850. sockopt_val = num ? 1 : 0;
  851. if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
  852. (const void *)&sockopt_val,
  853. sizeof(sockopt_val))) < 0)
  854. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  855. "calling setsockopt()");
  856. # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
  857. sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT;
  858. if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
  859. &sockopt_val, sizeof(sockopt_val))) < 0)
  860. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  861. "calling setsockopt()");
  862. # else
  863. ret = -1;
  864. # endif
  865. break;
  866. # endif
  867. default:
  868. ret = -1;
  869. break;
  870. }
  871. break;
  872. case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
  873. ret = dgram_get_mtu_overhead(data);
  874. break;
  875. /*
  876. * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
  877. * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
  878. * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
  879. * value has been updated to a non-clashing value. However to preserve
  880. * binary compatibility we now respond to both the old value and the new one
  881. */
  882. case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
  883. case BIO_CTRL_DGRAM_SET_PEEK_MODE:
  884. data->peekmode = (unsigned int)num;
  885. break;
  886. case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_CAP:
  887. # if defined(SUPPORT_LOCAL_ADDR)
  888. ret = 1;
  889. # else
  890. ret = 0;
  891. # endif
  892. break;
  893. case BIO_CTRL_DGRAM_SET_LOCAL_ADDR_ENABLE:
  894. # if defined(SUPPORT_LOCAL_ADDR)
  895. num = num > 0;
  896. if (num != data->local_addr_enabled) {
  897. if (enable_local_addr(b, num) < 1) {
  898. ret = 0;
  899. break;
  900. }
  901. data->local_addr_enabled = (char)num;
  902. }
  903. # else
  904. ret = 0;
  905. # endif
  906. break;
  907. case BIO_CTRL_DGRAM_GET_LOCAL_ADDR_ENABLE:
  908. *(int *)ptr = data->local_addr_enabled;
  909. break;
  910. case BIO_CTRL_DGRAM_GET_EFFECTIVE_CAPS:
  911. ret = (long)(BIO_DGRAM_CAP_HANDLES_DST_ADDR
  912. | BIO_DGRAM_CAP_HANDLES_SRC_ADDR
  913. | BIO_DGRAM_CAP_PROVIDES_DST_ADDR
  914. | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR);
  915. break;
  916. case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
  917. case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
  918. {
  919. BIO_POLL_DESCRIPTOR *pd = ptr;
  920. pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
  921. pd->value.fd = b->num;
  922. }
  923. break;
  924. default:
  925. ret = 0;
  926. break;
  927. }
  928. /* Normalize if error */
  929. if (ret < 0)
  930. ret = -1;
  931. return ret;
  932. }
  933. static int dgram_puts(BIO *bp, const char *str)
  934. {
  935. int n, ret;
  936. n = strlen(str);
  937. ret = dgram_write(bp, str, n);
  938. return ret;
  939. }
  940. # if M_METHOD == M_METHOD_WSARECVMSG
  941. static void translate_msg_win(BIO *b, WSAMSG *mh, WSABUF *iov,
  942. unsigned char *control, BIO_MSG *msg)
  943. {
  944. iov->len = msg->data_len;
  945. iov->buf = msg->data;
  946. /* Windows requires namelen to be set exactly */
  947. mh->name = msg->peer != NULL ? &msg->peer->sa : NULL;
  948. if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
  949. mh->namelen = sizeof(struct sockaddr_in);
  950. # if OPENSSL_USE_IPV6
  951. else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
  952. mh->namelen = sizeof(struct sockaddr_in6);
  953. # endif
  954. else
  955. mh->namelen = 0;
  956. /*
  957. * When local address reception (IP_PKTINFO, etc.) is enabled, on Windows
  958. * this causes WSARecvMsg to fail if the control buffer is too small to hold
  959. * the structure, or if no control buffer is passed. So we need to give it
  960. * the control buffer even if we aren't actually going to examine the
  961. * result.
  962. */
  963. mh->lpBuffers = iov;
  964. mh->dwBufferCount = 1;
  965. mh->Control.len = BIO_CMSG_ALLOC_LEN;
  966. mh->Control.buf = control;
  967. mh->dwFlags = 0;
  968. }
  969. # endif
  970. # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG
  971. /* Translates a BIO_MSG to a msghdr and iovec. */
  972. static void translate_msg(BIO *b, struct msghdr *mh, struct iovec *iov,
  973. unsigned char *control, BIO_MSG *msg)
  974. {
  975. iov->iov_base = msg->data;
  976. iov->iov_len = msg->data_len;
  977. /* macOS requires msg_namelen be 0 if msg_name is NULL */
  978. mh->msg_name = msg->peer != NULL ? &msg->peer->sa : NULL;
  979. if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET)
  980. mh->msg_namelen = sizeof(struct sockaddr_in);
  981. # if OPENSSL_USE_IPV6
  982. else if (msg->peer != NULL && dgram_get_sock_family(b) == AF_INET6)
  983. mh->msg_namelen = sizeof(struct sockaddr_in6);
  984. # endif
  985. else
  986. mh->msg_namelen = 0;
  987. mh->msg_iov = iov;
  988. mh->msg_iovlen = 1;
  989. mh->msg_control = msg->local != NULL ? control : NULL;
  990. mh->msg_controllen = msg->local != NULL ? BIO_CMSG_ALLOC_LEN : 0;
  991. mh->msg_flags = 0;
  992. }
  993. # endif
  994. # if M_METHOD == M_METHOD_RECVMMSG || M_METHOD == M_METHOD_RECVMSG || M_METHOD == M_METHOD_WSARECVMSG
  995. /* Extracts destination address from the control buffer. */
  996. static int extract_local(BIO *b, MSGHDR_TYPE *mh, BIO_ADDR *local) {
  997. # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
  998. CMSGHDR_TYPE *cmsg;
  999. int af = dgram_get_sock_family(b);
  1000. for (cmsg = BIO_CMSG_FIRSTHDR(mh); cmsg != NULL;
  1001. cmsg = BIO_CMSG_NXTHDR(mh, cmsg)) {
  1002. if (af == AF_INET) {
  1003. if (cmsg->cmsg_level != IPPROTO_IP)
  1004. continue;
  1005. # if defined(IP_PKTINFO)
  1006. if (cmsg->cmsg_type != IP_PKTINFO)
  1007. continue;
  1008. local->s_in.sin_addr =
  1009. ((struct in_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi_addr;
  1010. # elif defined(IP_RECVDSTADDR)
  1011. if (cmsg->cmsg_type != IP_RECVDSTADDR)
  1012. continue;
  1013. local->s_in.sin_addr = *(struct in_addr *)BIO_CMSG_DATA(cmsg);
  1014. # endif
  1015. # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
  1016. {
  1017. bio_dgram_data *data = b->ptr;
  1018. local->s_in.sin_family = AF_INET;
  1019. local->s_in.sin_port = data->local_addr.s_in.sin_port;
  1020. }
  1021. return 1;
  1022. # endif
  1023. }
  1024. # if OPENSSL_USE_IPV6
  1025. else if (af == AF_INET6) {
  1026. if (cmsg->cmsg_level != IPPROTO_IPV6)
  1027. continue;
  1028. # if defined(IPV6_RECVPKTINFO)
  1029. if (cmsg->cmsg_type != IPV6_PKTINFO)
  1030. continue;
  1031. {
  1032. bio_dgram_data *data = b->ptr;
  1033. local->s_in6.sin6_addr =
  1034. ((struct in6_pktinfo *)BIO_CMSG_DATA(cmsg))->ipi6_addr;
  1035. local->s_in6.sin6_family = AF_INET6;
  1036. local->s_in6.sin6_port = data->local_addr.s_in6.sin6_port;
  1037. local->s_in6.sin6_scope_id =
  1038. data->local_addr.s_in6.sin6_scope_id;
  1039. local->s_in6.sin6_flowinfo = 0;
  1040. }
  1041. return 1;
  1042. # endif
  1043. }
  1044. # endif
  1045. }
  1046. # endif
  1047. return 0;
  1048. }
  1049. static int pack_local(BIO *b, MSGHDR_TYPE *mh, const BIO_ADDR *local) {
  1050. int af = dgram_get_sock_family(b);
  1051. # if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
  1052. CMSGHDR_TYPE *cmsg;
  1053. bio_dgram_data *data = b->ptr;
  1054. # endif
  1055. if (af == AF_INET) {
  1056. # if defined(IP_PKTINFO)
  1057. struct in_pktinfo *info;
  1058. # if defined(OPENSSL_SYS_WINDOWS)
  1059. cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
  1060. # else
  1061. cmsg = (CMSGHDR_TYPE *)mh->msg_control;
  1062. # endif
  1063. cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in_pktinfo));
  1064. cmsg->cmsg_level = IPPROTO_IP;
  1065. cmsg->cmsg_type = IP_PKTINFO;
  1066. info = (struct in_pktinfo *)BIO_CMSG_DATA(cmsg);
  1067. # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_CYGWIN)
  1068. info->ipi_spec_dst = local->s_in.sin_addr;
  1069. # endif
  1070. info->ipi_addr.s_addr = 0;
  1071. info->ipi_ifindex = 0;
  1072. /*
  1073. * We cannot override source port using this API, therefore
  1074. * ensure the application specified a source port of 0
  1075. * or the one we are bound to. (Better to error than silently
  1076. * ignore this.)
  1077. */
  1078. if (local->s_in.sin_port != 0
  1079. && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
  1080. ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
  1081. return 0;
  1082. }
  1083. # if defined(OPENSSL_SYS_WINDOWS)
  1084. mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
  1085. # else
  1086. mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_pktinfo));
  1087. # endif
  1088. return 1;
  1089. # elif defined(IP_SENDSRCADDR)
  1090. struct in_addr *info;
  1091. /*
  1092. * At least FreeBSD is very pedantic about using IP_SENDSRCADDR when we
  1093. * are not bound to 0.0.0.0 or ::, even if the address matches what we
  1094. * bound to. Support this by not packing the structure if the address
  1095. * matches our understanding of our local address. IP_SENDSRCADDR is a
  1096. * BSD thing, so we don't need an explicit test for BSD here.
  1097. */
  1098. if (local->s_in.sin_addr.s_addr == data->local_addr.s_in.sin_addr.s_addr) {
  1099. mh->msg_control = NULL;
  1100. mh->msg_controllen = 0;
  1101. return 1;
  1102. }
  1103. cmsg = (struct cmsghdr *)mh->msg_control;
  1104. cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in_addr));
  1105. cmsg->cmsg_level = IPPROTO_IP;
  1106. cmsg->cmsg_type = IP_SENDSRCADDR;
  1107. info = (struct in_addr *)BIO_CMSG_DATA(cmsg);
  1108. *info = local->s_in.sin_addr;
  1109. /* See comment above. */
  1110. if (local->s_in.sin_port != 0
  1111. && data->local_addr.s_in.sin_port != local->s_in.sin_port) {
  1112. ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
  1113. return 0;
  1114. }
  1115. mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in_addr));
  1116. return 1;
  1117. # endif
  1118. }
  1119. # if OPENSSL_USE_IPV6
  1120. else if (af == AF_INET6) {
  1121. # if defined(IPV6_PKTINFO)
  1122. struct in6_pktinfo *info;
  1123. # if defined(OPENSSL_SYS_WINDOWS)
  1124. cmsg = (CMSGHDR_TYPE *)mh->Control.buf;
  1125. # else
  1126. cmsg = (CMSGHDR_TYPE *)mh->msg_control;
  1127. # endif
  1128. cmsg->cmsg_len = BIO_CMSG_LEN(sizeof(struct in6_pktinfo));
  1129. cmsg->cmsg_level = IPPROTO_IPV6;
  1130. cmsg->cmsg_type = IPV6_PKTINFO;
  1131. info = (struct in6_pktinfo *)BIO_CMSG_DATA(cmsg);
  1132. info->ipi6_addr = local->s_in6.sin6_addr;
  1133. info->ipi6_ifindex = 0;
  1134. /*
  1135. * See comment above, but also applies to the other fields
  1136. * in sockaddr_in6.
  1137. */
  1138. if (local->s_in6.sin6_port != 0
  1139. && data->local_addr.s_in6.sin6_port != local->s_in6.sin6_port) {
  1140. ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
  1141. return 0;
  1142. }
  1143. if (local->s_in6.sin6_scope_id != 0
  1144. && data->local_addr.s_in6.sin6_scope_id != local->s_in6.sin6_scope_id) {
  1145. ERR_raise(ERR_LIB_BIO, BIO_R_PORT_MISMATCH);
  1146. return 0;
  1147. }
  1148. # if defined(OPENSSL_SYS_WINDOWS)
  1149. mh->Control.len = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
  1150. # else
  1151. mh->msg_controllen = BIO_CMSG_SPACE(sizeof(struct in6_pktinfo));
  1152. # endif
  1153. return 1;
  1154. # endif
  1155. }
  1156. # endif
  1157. return 0;
  1158. }
  1159. # endif
  1160. /*
  1161. * Converts flags passed to BIO_sendmmsg or BIO_recvmmsg to syscall flags. You
  1162. * should mask out any system flags returned by this function you cannot support
  1163. * in a particular circumstance. Currently no flags are defined.
  1164. */
  1165. # if M_METHOD != M_METHOD_NONE
  1166. static int translate_flags(uint64_t flags) {
  1167. return 0;
  1168. }
  1169. # endif
  1170. static int dgram_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
  1171. size_t num_msg, uint64_t flags, size_t *num_processed)
  1172. {
  1173. # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
  1174. int ret;
  1175. # endif
  1176. # if M_METHOD == M_METHOD_RECVMMSG
  1177. # define BIO_MAX_MSGS_PER_CALL 64
  1178. int sysflags;
  1179. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1180. size_t i;
  1181. struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
  1182. struct iovec iov[BIO_MAX_MSGS_PER_CALL];
  1183. unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
  1184. int have_local_enabled = data->local_addr_enabled;
  1185. # elif M_METHOD == M_METHOD_RECVMSG
  1186. int sysflags;
  1187. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1188. ossl_ssize_t l;
  1189. struct msghdr mh;
  1190. struct iovec iov;
  1191. unsigned char control[BIO_CMSG_ALLOC_LEN];
  1192. int have_local_enabled = data->local_addr_enabled;
  1193. # elif M_METHOD == M_METHOD_WSARECVMSG
  1194. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1195. int have_local_enabled = data->local_addr_enabled;
  1196. WSAMSG wmsg;
  1197. WSABUF wbuf;
  1198. DWORD num_bytes_sent = 0;
  1199. unsigned char control[BIO_CMSG_ALLOC_LEN];
  1200. # endif
  1201. # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
  1202. int sysflags;
  1203. # endif
  1204. if (num_msg == 0) {
  1205. *num_processed = 0;
  1206. return 1;
  1207. }
  1208. if (num_msg > OSSL_SSIZE_MAX)
  1209. num_msg = OSSL_SSIZE_MAX;
  1210. # if M_METHOD != M_METHOD_NONE
  1211. sysflags = translate_flags(flags);
  1212. # endif
  1213. # if M_METHOD == M_METHOD_RECVMMSG
  1214. /*
  1215. * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
  1216. * msghdr and struct iovec on the stack to support multithreaded use. Thus
  1217. * we place a fixed limit on the number of messages per call, in the
  1218. * expectation that we will be called again if there were more messages to
  1219. * be sent.
  1220. */
  1221. if (num_msg > BIO_MAX_MSGS_PER_CALL)
  1222. num_msg = BIO_MAX_MSGS_PER_CALL;
  1223. for (i = 0; i < num_msg; ++i) {
  1224. translate_msg(b, &mh[i].msg_hdr, &iov[i],
  1225. control[i], &BIO_MSG_N(msg, stride, i));
  1226. /* If local address was requested, it must have been enabled */
  1227. if (BIO_MSG_N(msg, stride, i).local != NULL) {
  1228. if (!have_local_enabled) {
  1229. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1230. *num_processed = 0;
  1231. return 0;
  1232. }
  1233. if (pack_local(b, &mh[i].msg_hdr,
  1234. BIO_MSG_N(msg, stride, i).local) < 1) {
  1235. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1236. *num_processed = 0;
  1237. return 0;
  1238. }
  1239. }
  1240. }
  1241. /* Do the batch */
  1242. ret = sendmmsg(b->num, mh, num_msg, sysflags);
  1243. if (ret < 0) {
  1244. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1245. *num_processed = 0;
  1246. return 0;
  1247. }
  1248. for (i = 0; i < (size_t)ret; ++i) {
  1249. BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
  1250. BIO_MSG_N(msg, stride, i).flags = 0;
  1251. }
  1252. *num_processed = (size_t)ret;
  1253. return 1;
  1254. # elif M_METHOD == M_METHOD_RECVMSG
  1255. /*
  1256. * If sendmsg is available, use it.
  1257. */
  1258. translate_msg(b, &mh, &iov, control, msg);
  1259. if (msg->local != NULL) {
  1260. if (!have_local_enabled) {
  1261. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1262. *num_processed = 0;
  1263. return 0;
  1264. }
  1265. if (pack_local(b, &mh, msg->local) < 1) {
  1266. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1267. *num_processed = 0;
  1268. return 0;
  1269. }
  1270. }
  1271. l = sendmsg(b->num, &mh, sysflags);
  1272. if (l < 0) {
  1273. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1274. *num_processed = 0;
  1275. return 0;
  1276. }
  1277. msg->data_len = (size_t)l;
  1278. msg->flags = 0;
  1279. *num_processed = 1;
  1280. return 1;
  1281. # elif M_METHOD == M_METHOD_WSARECVMSG || M_METHOD == M_METHOD_RECVFROM
  1282. # if M_METHOD == M_METHOD_WSARECVMSG
  1283. if (bio_WSASendMsg != NULL) {
  1284. /* WSASendMsg-based implementation for Windows. */
  1285. translate_msg_win(b, &wmsg, &wbuf, control, msg);
  1286. if (msg[0].local != NULL) {
  1287. if (!have_local_enabled) {
  1288. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1289. *num_processed = 0;
  1290. return 0;
  1291. }
  1292. if (pack_local(b, &wmsg, msg[0].local) < 1) {
  1293. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1294. *num_processed = 0;
  1295. return 0;
  1296. }
  1297. }
  1298. ret = WSASendMsg((SOCKET)b->num, &wmsg, 0, &num_bytes_sent, NULL, NULL);
  1299. if (ret < 0) {
  1300. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1301. *num_processed = 0;
  1302. return 0;
  1303. }
  1304. msg[0].data_len = num_bytes_sent;
  1305. msg[0].flags = 0;
  1306. *num_processed = 1;
  1307. return 1;
  1308. }
  1309. # endif
  1310. /*
  1311. * Fallback to sendto and send a single message.
  1312. */
  1313. if (msg[0].local != NULL) {
  1314. /*
  1315. * We cannot set the local address if using sendto
  1316. * so fail in this case
  1317. */
  1318. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1319. *num_processed = 0;
  1320. return 0;
  1321. }
  1322. ret = sendto(b->num, msg[0].data,
  1323. # if defined(OPENSSL_SYS_WINDOWS)
  1324. (int)msg[0].data_len,
  1325. # else
  1326. msg[0].data_len,
  1327. # endif
  1328. sysflags,
  1329. msg[0].peer != NULL ? BIO_ADDR_sockaddr(msg[0].peer) : NULL,
  1330. msg[0].peer != NULL ? BIO_ADDR_sockaddr_size(msg[0].peer) : 0);
  1331. if (ret <= 0) {
  1332. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1333. *num_processed = 0;
  1334. return 0;
  1335. }
  1336. msg[0].data_len = ret;
  1337. msg[0].flags = 0;
  1338. *num_processed = 1;
  1339. return 1;
  1340. # else
  1341. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  1342. *num_processed = 0;
  1343. return 0;
  1344. # endif
  1345. }
  1346. static int dgram_recvmmsg(BIO *b, BIO_MSG *msg,
  1347. size_t stride, size_t num_msg,
  1348. uint64_t flags, size_t *num_processed)
  1349. {
  1350. # if M_METHOD != M_METHOD_NONE && M_METHOD != M_METHOD_RECVMSG
  1351. int ret;
  1352. # endif
  1353. # if M_METHOD == M_METHOD_RECVMMSG
  1354. int sysflags;
  1355. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1356. size_t i;
  1357. struct mmsghdr mh[BIO_MAX_MSGS_PER_CALL];
  1358. struct iovec iov[BIO_MAX_MSGS_PER_CALL];
  1359. unsigned char control[BIO_MAX_MSGS_PER_CALL][BIO_CMSG_ALLOC_LEN];
  1360. int have_local_enabled = data->local_addr_enabled;
  1361. # elif M_METHOD == M_METHOD_RECVMSG
  1362. int sysflags;
  1363. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1364. ossl_ssize_t l;
  1365. struct msghdr mh;
  1366. struct iovec iov;
  1367. unsigned char control[BIO_CMSG_ALLOC_LEN];
  1368. int have_local_enabled = data->local_addr_enabled;
  1369. # elif M_METHOD == M_METHOD_WSARECVMSG
  1370. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  1371. int have_local_enabled = data->local_addr_enabled;
  1372. WSAMSG wmsg;
  1373. WSABUF wbuf;
  1374. DWORD num_bytes_received = 0;
  1375. unsigned char control[BIO_CMSG_ALLOC_LEN];
  1376. # endif
  1377. # if M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
  1378. int sysflags;
  1379. socklen_t slen;
  1380. # endif
  1381. if (num_msg == 0) {
  1382. *num_processed = 0;
  1383. return 1;
  1384. }
  1385. if (num_msg > OSSL_SSIZE_MAX)
  1386. num_msg = OSSL_SSIZE_MAX;
  1387. # if M_METHOD != M_METHOD_NONE
  1388. sysflags = translate_flags(flags);
  1389. # endif
  1390. # if M_METHOD == M_METHOD_RECVMMSG
  1391. /*
  1392. * In the sendmmsg/recvmmsg case, we need to allocate our translated struct
  1393. * msghdr and struct iovec on the stack to support multithreaded use. Thus
  1394. * we place a fixed limit on the number of messages per call, in the
  1395. * expectation that we will be called again if there were more messages to
  1396. * be sent.
  1397. */
  1398. if (num_msg > BIO_MAX_MSGS_PER_CALL)
  1399. num_msg = BIO_MAX_MSGS_PER_CALL;
  1400. for (i = 0; i < num_msg; ++i) {
  1401. translate_msg(b, &mh[i].msg_hdr, &iov[i],
  1402. control[i], &BIO_MSG_N(msg, stride, i));
  1403. /* If local address was requested, it must have been enabled */
  1404. if (BIO_MSG_N(msg, stride, i).local != NULL && !have_local_enabled) {
  1405. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1406. *num_processed = 0;
  1407. return 0;
  1408. }
  1409. }
  1410. /* Do the batch */
  1411. ret = recvmmsg(b->num, mh, num_msg, sysflags, NULL);
  1412. if (ret < 0) {
  1413. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1414. *num_processed = 0;
  1415. return 0;
  1416. }
  1417. for (i = 0; i < (size_t)ret; ++i) {
  1418. BIO_MSG_N(msg, stride, i).data_len = mh[i].msg_len;
  1419. BIO_MSG_N(msg, stride, i).flags = 0;
  1420. /*
  1421. * *(msg->peer) will have been filled in by recvmmsg;
  1422. * for msg->local we parse the control data returned
  1423. */
  1424. if (BIO_MSG_N(msg, stride, i).local != NULL)
  1425. if (extract_local(b, &mh[i].msg_hdr,
  1426. BIO_MSG_N(msg, stride, i).local) < 1)
  1427. /*
  1428. * It appears BSDs do not support local addresses for
  1429. * loopback sockets. In this case, just clear the local
  1430. * address, as for OS X and Windows in some circumstances
  1431. * (see below).
  1432. */
  1433. BIO_ADDR_clear(msg->local);
  1434. }
  1435. *num_processed = (size_t)ret;
  1436. return 1;
  1437. # elif M_METHOD == M_METHOD_RECVMSG
  1438. /*
  1439. * If recvmsg is available, use it.
  1440. */
  1441. translate_msg(b, &mh, &iov, control, msg);
  1442. /* If local address was requested, it must have been enabled */
  1443. if (msg->local != NULL && !have_local_enabled) {
  1444. /*
  1445. * If we have done at least one message, we must return the
  1446. * count; if we haven't done any, we can give an error code
  1447. */
  1448. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1449. *num_processed = 0;
  1450. return 0;
  1451. }
  1452. l = recvmsg(b->num, &mh, sysflags);
  1453. if (l < 0) {
  1454. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1455. *num_processed = 0;
  1456. return 0;
  1457. }
  1458. msg->data_len = (size_t)l;
  1459. msg->flags = 0;
  1460. if (msg->local != NULL)
  1461. if (extract_local(b, &mh, msg->local) < 1)
  1462. /*
  1463. * OS X exhibits odd behaviour where it appears that if a packet is
  1464. * sent before the receiving interface enables IP_PKTINFO, it will
  1465. * sometimes not have any control data returned even if the
  1466. * receiving interface enables IP_PKTINFO before calling recvmsg().
  1467. * This appears to occur non-deterministically. Presumably, OS X
  1468. * handles IP_PKTINFO at the time the packet is enqueued into a
  1469. * socket's receive queue, rather than at the time recvmsg() is
  1470. * called, unlike most other operating systems. Thus (if this
  1471. * hypothesis is correct) there is a race between where IP_PKTINFO
  1472. * is enabled by the process and when the kernel's network stack
  1473. * queues the incoming message.
  1474. *
  1475. * We cannot return the local address if we do not have it, but this
  1476. * is not a caller error either, so just return a zero address
  1477. * structure. This is similar to how we handle Windows loopback
  1478. * interfaces (see below). We enable this workaround for all
  1479. * platforms, not just Apple, as this kind of quirk in OS networking
  1480. * stacks seems to be common enough that failing hard if a local
  1481. * address is not provided appears to be too brittle.
  1482. */
  1483. BIO_ADDR_clear(msg->local);
  1484. *num_processed = 1;
  1485. return 1;
  1486. # elif M_METHOD == M_METHOD_RECVFROM || M_METHOD == M_METHOD_WSARECVMSG
  1487. # if M_METHOD == M_METHOD_WSARECVMSG
  1488. if (bio_WSARecvMsg != NULL) {
  1489. /* WSARecvMsg-based implementation for Windows. */
  1490. translate_msg_win(b, &wmsg, &wbuf, control, msg);
  1491. /* If local address was requested, it must have been enabled */
  1492. if (msg[0].local != NULL && !have_local_enabled) {
  1493. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1494. *num_processed = 0;
  1495. return 0;
  1496. }
  1497. ret = WSARecvMsg((SOCKET)b->num, &wmsg, &num_bytes_received, NULL, NULL);
  1498. if (ret < 0) {
  1499. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1500. *num_processed = 0;
  1501. return 0;
  1502. }
  1503. msg[0].data_len = num_bytes_received;
  1504. msg[0].flags = 0;
  1505. if (msg[0].local != NULL)
  1506. if (extract_local(b, &wmsg, msg[0].local) < 1)
  1507. /*
  1508. * On Windows, loopback is not a "proper" interface and it works
  1509. * differently; packets are essentially short-circuited and
  1510. * don't go through all of the normal processing. A consequence
  1511. * of this is that packets sent from the local machine to the
  1512. * local machine _will not have IP_PKTINFO_ even if the
  1513. * IP_PKTINFO socket option is enabled. WSARecvMsg just sets
  1514. * Control.len to 0 on returning.
  1515. *
  1516. * This applies regardless of whether the loopback address,
  1517. * 127.0.0.1 is used, or a local interface address (e.g.
  1518. * 192.168.1.1); in both cases IP_PKTINFO will not be present.
  1519. *
  1520. * We report this condition by setting the local BIO_ADDR's
  1521. * family to 0.
  1522. */
  1523. BIO_ADDR_clear(msg[0].local);
  1524. *num_processed = 1;
  1525. return 1;
  1526. }
  1527. # endif
  1528. /*
  1529. * Fallback to recvfrom and receive a single message.
  1530. */
  1531. if (msg[0].local != NULL) {
  1532. /*
  1533. * We cannot determine the local address if using recvfrom
  1534. * so fail in this case
  1535. */
  1536. ERR_raise(ERR_LIB_BIO, BIO_R_LOCAL_ADDR_NOT_AVAILABLE);
  1537. *num_processed = 0;
  1538. return 0;
  1539. }
  1540. slen = sizeof(*msg[0].peer);
  1541. ret = recvfrom(b->num, msg[0].data,
  1542. # if defined(OPENSSL_SYS_WINDOWS)
  1543. (int)msg[0].data_len,
  1544. # else
  1545. msg[0].data_len,
  1546. # endif
  1547. sysflags,
  1548. msg[0].peer != NULL ? &msg[0].peer->sa : NULL,
  1549. msg[0].peer != NULL ? &slen : NULL);
  1550. if (ret <= 0) {
  1551. ERR_raise(ERR_LIB_SYS, get_last_socket_error());
  1552. return 0;
  1553. }
  1554. msg[0].data_len = ret;
  1555. msg[0].flags = 0;
  1556. *num_processed = 1;
  1557. return 1;
  1558. # else
  1559. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  1560. *num_processed = 0;
  1561. return 0;
  1562. # endif
  1563. }
  1564. # ifndef OPENSSL_NO_SCTP
  1565. const BIO_METHOD *BIO_s_datagram_sctp(void)
  1566. {
  1567. return &methods_dgramp_sctp;
  1568. }
  1569. BIO *BIO_new_dgram_sctp(int fd, int close_flag)
  1570. {
  1571. BIO *bio;
  1572. int ret, optval = 20000;
  1573. int auth_data = 0, auth_forward = 0;
  1574. unsigned char *p;
  1575. struct sctp_authchunk auth;
  1576. struct sctp_authchunks *authchunks;
  1577. socklen_t sockopt_len;
  1578. # ifdef SCTP_AUTHENTICATION_EVENT
  1579. # ifdef SCTP_EVENT
  1580. struct sctp_event event;
  1581. # else
  1582. struct sctp_event_subscribe event;
  1583. # endif
  1584. # endif
  1585. bio = BIO_new(BIO_s_datagram_sctp());
  1586. if (bio == NULL)
  1587. return NULL;
  1588. BIO_set_fd(bio, fd, close_flag);
  1589. /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
  1590. auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
  1591. ret =
  1592. setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
  1593. sizeof(struct sctp_authchunk));
  1594. if (ret < 0) {
  1595. BIO_vfree(bio);
  1596. ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
  1597. "Ensure SCTP AUTH chunks are enabled in kernel");
  1598. return NULL;
  1599. }
  1600. auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
  1601. ret =
  1602. setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
  1603. sizeof(struct sctp_authchunk));
  1604. if (ret < 0) {
  1605. BIO_vfree(bio);
  1606. ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
  1607. "Ensure SCTP AUTH chunks are enabled in kernel");
  1608. return NULL;
  1609. }
  1610. /*
  1611. * Test if activation was successful. When using accept(), SCTP-AUTH has
  1612. * to be activated for the listening socket already, otherwise the
  1613. * connected socket won't use it. Similarly with connect(): the socket
  1614. * prior to connection must be activated for SCTP-AUTH
  1615. */
  1616. sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
  1617. authchunks = OPENSSL_zalloc(sockopt_len);
  1618. if (authchunks == NULL) {
  1619. BIO_vfree(bio);
  1620. return NULL;
  1621. }
  1622. ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
  1623. &sockopt_len);
  1624. if (ret < 0) {
  1625. OPENSSL_free(authchunks);
  1626. BIO_vfree(bio);
  1627. return NULL;
  1628. }
  1629. for (p = (unsigned char *)authchunks->gauth_chunks;
  1630. p < (unsigned char *)authchunks + sockopt_len;
  1631. p += sizeof(uint8_t)) {
  1632. if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
  1633. auth_data = 1;
  1634. if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
  1635. auth_forward = 1;
  1636. }
  1637. OPENSSL_free(authchunks);
  1638. if (!auth_data || !auth_forward) {
  1639. BIO_vfree(bio);
  1640. ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB,
  1641. "Ensure SCTP AUTH chunks are enabled on the "
  1642. "underlying socket");
  1643. return NULL;
  1644. }
  1645. # ifdef SCTP_AUTHENTICATION_EVENT
  1646. # ifdef SCTP_EVENT
  1647. memset(&event, 0, sizeof(event));
  1648. event.se_assoc_id = 0;
  1649. event.se_type = SCTP_AUTHENTICATION_EVENT;
  1650. event.se_on = 1;
  1651. ret =
  1652. setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
  1653. sizeof(struct sctp_event));
  1654. if (ret < 0) {
  1655. BIO_vfree(bio);
  1656. return NULL;
  1657. }
  1658. # else
  1659. sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
  1660. ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
  1661. if (ret < 0) {
  1662. BIO_vfree(bio);
  1663. return NULL;
  1664. }
  1665. event.sctp_authentication_event = 1;
  1666. ret =
  1667. setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1668. sizeof(struct sctp_event_subscribe));
  1669. if (ret < 0) {
  1670. BIO_vfree(bio);
  1671. return NULL;
  1672. }
  1673. # endif
  1674. # endif
  1675. /*
  1676. * Disable partial delivery by setting the min size larger than the max
  1677. * record size of 2^14 + 2048 + 13
  1678. */
  1679. ret =
  1680. setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
  1681. sizeof(optval));
  1682. if (ret < 0) {
  1683. BIO_vfree(bio);
  1684. return NULL;
  1685. }
  1686. return bio;
  1687. }
  1688. int BIO_dgram_is_sctp(BIO *bio)
  1689. {
  1690. return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
  1691. }
  1692. static int dgram_sctp_new(BIO *bi)
  1693. {
  1694. bio_dgram_sctp_data *data = NULL;
  1695. bi->init = 0;
  1696. bi->num = 0;
  1697. if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
  1698. return 0;
  1699. # ifdef SCTP_PR_SCTP_NONE
  1700. data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
  1701. # endif
  1702. bi->ptr = data;
  1703. bi->flags = 0;
  1704. return 1;
  1705. }
  1706. static int dgram_sctp_free(BIO *a)
  1707. {
  1708. bio_dgram_sctp_data *data;
  1709. if (a == NULL)
  1710. return 0;
  1711. if (!dgram_clear(a))
  1712. return 0;
  1713. data = (bio_dgram_sctp_data *) a->ptr;
  1714. if (data != NULL)
  1715. OPENSSL_free(data);
  1716. return 1;
  1717. }
  1718. # ifdef SCTP_AUTHENTICATION_EVENT
  1719. void dgram_sctp_handle_auth_free_key_event(BIO *b,
  1720. union sctp_notification *snp)
  1721. {
  1722. int ret;
  1723. struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
  1724. if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
  1725. struct sctp_authkeyid authkeyid;
  1726. /* delete key */
  1727. authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
  1728. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
  1729. &authkeyid, sizeof(struct sctp_authkeyid));
  1730. }
  1731. }
  1732. # endif
  1733. static int dgram_sctp_read(BIO *b, char *out, int outl)
  1734. {
  1735. int ret = 0, n = 0, i, optval;
  1736. socklen_t optlen;
  1737. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  1738. struct msghdr msg;
  1739. struct iovec iov;
  1740. struct cmsghdr *cmsg;
  1741. char cmsgbuf[512];
  1742. if (out != NULL) {
  1743. clear_socket_error();
  1744. do {
  1745. memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
  1746. iov.iov_base = out;
  1747. iov.iov_len = outl;
  1748. msg.msg_name = NULL;
  1749. msg.msg_namelen = 0;
  1750. msg.msg_iov = &iov;
  1751. msg.msg_iovlen = 1;
  1752. msg.msg_control = cmsgbuf;
  1753. msg.msg_controllen = 512;
  1754. msg.msg_flags = 0;
  1755. n = recvmsg(b->num, &msg, 0);
  1756. if (n <= 0) {
  1757. if (n < 0)
  1758. ret = n;
  1759. break;
  1760. }
  1761. if (msg.msg_controllen > 0) {
  1762. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
  1763. cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  1764. if (cmsg->cmsg_level != IPPROTO_SCTP)
  1765. continue;
  1766. # ifdef SCTP_RCVINFO
  1767. if (cmsg->cmsg_type == SCTP_RCVINFO) {
  1768. struct sctp_rcvinfo *rcvinfo;
  1769. rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
  1770. data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
  1771. data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
  1772. data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
  1773. data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
  1774. data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
  1775. data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
  1776. data->rcvinfo.rcv_context = rcvinfo->rcv_context;
  1777. }
  1778. # endif
  1779. # ifdef SCTP_SNDRCV
  1780. if (cmsg->cmsg_type == SCTP_SNDRCV) {
  1781. struct sctp_sndrcvinfo *sndrcvinfo;
  1782. sndrcvinfo =
  1783. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  1784. data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
  1785. data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
  1786. data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
  1787. data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
  1788. data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
  1789. data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
  1790. data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
  1791. }
  1792. # endif
  1793. }
  1794. }
  1795. if (msg.msg_flags & MSG_NOTIFICATION) {
  1796. union sctp_notification snp;
  1797. memcpy(&snp, out, sizeof(snp));
  1798. if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
  1799. # ifdef SCTP_EVENT
  1800. struct sctp_event event;
  1801. # else
  1802. struct sctp_event_subscribe event;
  1803. socklen_t eventsize;
  1804. # endif
  1805. /* disable sender dry event */
  1806. # ifdef SCTP_EVENT
  1807. memset(&event, 0, sizeof(event));
  1808. event.se_assoc_id = 0;
  1809. event.se_type = SCTP_SENDER_DRY_EVENT;
  1810. event.se_on = 0;
  1811. i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
  1812. sizeof(struct sctp_event));
  1813. if (i < 0) {
  1814. ret = i;
  1815. break;
  1816. }
  1817. # else
  1818. eventsize = sizeof(struct sctp_event_subscribe);
  1819. i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1820. &eventsize);
  1821. if (i < 0) {
  1822. ret = i;
  1823. break;
  1824. }
  1825. event.sctp_sender_dry_event = 0;
  1826. i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  1827. sizeof(struct sctp_event_subscribe));
  1828. if (i < 0) {
  1829. ret = i;
  1830. break;
  1831. }
  1832. # endif
  1833. }
  1834. # ifdef SCTP_AUTHENTICATION_EVENT
  1835. if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
  1836. dgram_sctp_handle_auth_free_key_event(b, &snp);
  1837. # endif
  1838. if (data->handle_notifications != NULL)
  1839. data->handle_notifications(b, data->notification_context,
  1840. (void *)out);
  1841. memset(&snp, 0, sizeof(snp));
  1842. memset(out, 0, outl);
  1843. } else {
  1844. ret += n;
  1845. }
  1846. }
  1847. while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
  1848. && (ret < outl));
  1849. if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
  1850. /* Partial message read, this should never happen! */
  1851. /*
  1852. * The buffer was too small, this means the peer sent a message
  1853. * that was larger than allowed.
  1854. */
  1855. if (ret == outl)
  1856. return -1;
  1857. /*
  1858. * Test if socket buffer can handle max record size (2^14 + 2048
  1859. * + 13)
  1860. */
  1861. optlen = (socklen_t) sizeof(int);
  1862. ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
  1863. if (ret >= 0)
  1864. OPENSSL_assert(optval >= 18445);
  1865. /*
  1866. * Test if SCTP doesn't partially deliver below max record size
  1867. * (2^14 + 2048 + 13)
  1868. */
  1869. optlen = (socklen_t) sizeof(int);
  1870. ret =
  1871. getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
  1872. &optval, &optlen);
  1873. if (ret >= 0)
  1874. OPENSSL_assert(optval >= 18445);
  1875. /*
  1876. * Partially delivered notification??? Probably a bug....
  1877. */
  1878. OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
  1879. /*
  1880. * Everything seems ok till now, so it's most likely a message
  1881. * dropped by PR-SCTP.
  1882. */
  1883. memset(out, 0, outl);
  1884. BIO_set_retry_read(b);
  1885. return -1;
  1886. }
  1887. BIO_clear_retry_flags(b);
  1888. if (ret < 0) {
  1889. if (BIO_dgram_should_retry(ret)) {
  1890. BIO_set_retry_read(b);
  1891. data->dgram._errno = get_last_socket_error();
  1892. }
  1893. }
  1894. /* Test if peer uses SCTP-AUTH before continuing */
  1895. if (!data->peer_auth_tested) {
  1896. int ii, auth_data = 0, auth_forward = 0;
  1897. unsigned char *p;
  1898. struct sctp_authchunks *authchunks;
  1899. optlen =
  1900. (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
  1901. authchunks = OPENSSL_malloc(optlen);
  1902. if (authchunks == NULL)
  1903. return -1;
  1904. memset(authchunks, 0, optlen);
  1905. ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
  1906. authchunks, &optlen);
  1907. if (ii >= 0)
  1908. for (p = (unsigned char *)authchunks->gauth_chunks;
  1909. p < (unsigned char *)authchunks + optlen;
  1910. p += sizeof(uint8_t)) {
  1911. if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
  1912. auth_data = 1;
  1913. if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
  1914. auth_forward = 1;
  1915. }
  1916. OPENSSL_free(authchunks);
  1917. if (!auth_data || !auth_forward) {
  1918. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  1919. return -1;
  1920. }
  1921. data->peer_auth_tested = 1;
  1922. }
  1923. }
  1924. return ret;
  1925. }
  1926. /*
  1927. * dgram_sctp_write - send message on SCTP socket
  1928. * @b: BIO to write to
  1929. * @in: data to send
  1930. * @inl: amount of bytes in @in to send
  1931. *
  1932. * Returns -1 on error or the sent amount of bytes on success
  1933. */
  1934. static int dgram_sctp_write(BIO *b, const char *in, int inl)
  1935. {
  1936. int ret;
  1937. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  1938. struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
  1939. struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
  1940. struct bio_dgram_sctp_sndinfo handshake_sinfo;
  1941. struct iovec iov[1];
  1942. struct msghdr msg;
  1943. struct cmsghdr *cmsg;
  1944. # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
  1945. char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
  1946. CMSG_SPACE(sizeof(struct sctp_prinfo))];
  1947. struct sctp_sndinfo *sndinfo;
  1948. struct sctp_prinfo *prinfo;
  1949. # else
  1950. char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  1951. struct sctp_sndrcvinfo *sndrcvinfo;
  1952. # endif
  1953. clear_socket_error();
  1954. /*
  1955. * If we're send anything else than application data, disable all user
  1956. * parameters and flags.
  1957. */
  1958. if (in[0] != 23) {
  1959. memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
  1960. # ifdef SCTP_SACK_IMMEDIATELY
  1961. handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
  1962. # endif
  1963. sinfo = &handshake_sinfo;
  1964. }
  1965. /* We can only send a shutdown alert if the socket is dry */
  1966. if (data->save_shutdown) {
  1967. ret = BIO_dgram_sctp_wait_for_dry(b);
  1968. if (ret < 0)
  1969. return -1;
  1970. if (ret == 0) {
  1971. BIO_clear_retry_flags(b);
  1972. BIO_set_retry_write(b);
  1973. return -1;
  1974. }
  1975. }
  1976. iov[0].iov_base = (char *)in;
  1977. iov[0].iov_len = inl;
  1978. msg.msg_name = NULL;
  1979. msg.msg_namelen = 0;
  1980. msg.msg_iov = iov;
  1981. msg.msg_iovlen = 1;
  1982. msg.msg_control = (caddr_t) cmsgbuf;
  1983. msg.msg_controllen = 0;
  1984. msg.msg_flags = 0;
  1985. # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
  1986. cmsg = (struct cmsghdr *)cmsgbuf;
  1987. cmsg->cmsg_level = IPPROTO_SCTP;
  1988. cmsg->cmsg_type = SCTP_SNDINFO;
  1989. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
  1990. sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
  1991. memset(sndinfo, 0, sizeof(*sndinfo));
  1992. sndinfo->snd_sid = sinfo->snd_sid;
  1993. sndinfo->snd_flags = sinfo->snd_flags;
  1994. sndinfo->snd_ppid = sinfo->snd_ppid;
  1995. sndinfo->snd_context = sinfo->snd_context;
  1996. msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
  1997. cmsg =
  1998. (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
  1999. cmsg->cmsg_level = IPPROTO_SCTP;
  2000. cmsg->cmsg_type = SCTP_PRINFO;
  2001. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
  2002. prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
  2003. memset(prinfo, 0, sizeof(*prinfo));
  2004. prinfo->pr_policy = pinfo->pr_policy;
  2005. prinfo->pr_value = pinfo->pr_value;
  2006. msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
  2007. # else
  2008. cmsg = (struct cmsghdr *)cmsgbuf;
  2009. cmsg->cmsg_level = IPPROTO_SCTP;
  2010. cmsg->cmsg_type = SCTP_SNDRCV;
  2011. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  2012. sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  2013. memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
  2014. sndrcvinfo->sinfo_stream = sinfo->snd_sid;
  2015. sndrcvinfo->sinfo_flags = sinfo->snd_flags;
  2016. # ifdef __FreeBSD__
  2017. sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
  2018. # endif
  2019. sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
  2020. sndrcvinfo->sinfo_context = sinfo->snd_context;
  2021. sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
  2022. msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
  2023. # endif
  2024. ret = sendmsg(b->num, &msg, 0);
  2025. BIO_clear_retry_flags(b);
  2026. if (ret <= 0) {
  2027. if (BIO_dgram_should_retry(ret)) {
  2028. BIO_set_retry_write(b);
  2029. data->dgram._errno = get_last_socket_error();
  2030. }
  2031. }
  2032. return ret;
  2033. }
  2034. static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
  2035. {
  2036. long ret = 1;
  2037. bio_dgram_sctp_data *data = NULL;
  2038. socklen_t sockopt_len = 0;
  2039. struct sctp_authkeyid authkeyid;
  2040. struct sctp_authkey *authkey = NULL;
  2041. data = (bio_dgram_sctp_data *) b->ptr;
  2042. switch (cmd) {
  2043. case BIO_CTRL_DGRAM_QUERY_MTU:
  2044. /*
  2045. * Set to maximum (2^14) and ignore user input to enable transport
  2046. * protocol fragmentation. Returns always 2^14.
  2047. */
  2048. data->dgram.mtu = 16384;
  2049. ret = data->dgram.mtu;
  2050. break;
  2051. case BIO_CTRL_DGRAM_SET_MTU:
  2052. /*
  2053. * Set to maximum (2^14) and ignore input to enable transport
  2054. * protocol fragmentation. Returns always 2^14.
  2055. */
  2056. data->dgram.mtu = 16384;
  2057. ret = data->dgram.mtu;
  2058. break;
  2059. case BIO_CTRL_DGRAM_SET_CONNECTED:
  2060. case BIO_CTRL_DGRAM_CONNECT:
  2061. /* Returns always -1. */
  2062. ret = -1;
  2063. break;
  2064. case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
  2065. /*
  2066. * SCTP doesn't need the DTLS timer Returns always 1.
  2067. */
  2068. break;
  2069. case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
  2070. /*
  2071. * We allow transport protocol fragmentation so this is irrelevant
  2072. */
  2073. ret = 0;
  2074. break;
  2075. case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
  2076. if (num > 0)
  2077. data->in_handshake = 1;
  2078. else
  2079. data->in_handshake = 0;
  2080. ret =
  2081. setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
  2082. &data->in_handshake, sizeof(int));
  2083. break;
  2084. case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
  2085. /*
  2086. * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
  2087. */
  2088. /* Get active key */
  2089. sockopt_len = sizeof(struct sctp_authkeyid);
  2090. ret =
  2091. getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
  2092. &sockopt_len);
  2093. if (ret < 0)
  2094. break;
  2095. /* Add new key */
  2096. sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
  2097. authkey = OPENSSL_malloc(sockopt_len);
  2098. if (authkey == NULL) {
  2099. ret = -1;
  2100. break;
  2101. }
  2102. memset(authkey, 0, sockopt_len);
  2103. authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
  2104. # ifndef __FreeBSD__
  2105. /*
  2106. * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
  2107. * and higher work without it.
  2108. */
  2109. authkey->sca_keylength = 64;
  2110. # endif
  2111. memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
  2112. ret =
  2113. setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
  2114. sockopt_len);
  2115. OPENSSL_free(authkey);
  2116. authkey = NULL;
  2117. if (ret < 0)
  2118. break;
  2119. /* Reset active key */
  2120. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
  2121. &authkeyid, sizeof(struct sctp_authkeyid));
  2122. if (ret < 0)
  2123. break;
  2124. break;
  2125. case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
  2126. /* Returns 0 on success, -1 otherwise. */
  2127. /* Get active key */
  2128. sockopt_len = sizeof(struct sctp_authkeyid);
  2129. ret =
  2130. getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
  2131. &sockopt_len);
  2132. if (ret < 0)
  2133. break;
  2134. /* Set active key */
  2135. authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
  2136. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
  2137. &authkeyid, sizeof(struct sctp_authkeyid));
  2138. if (ret < 0)
  2139. break;
  2140. /*
  2141. * CCS has been sent, so remember that and fall through to check if
  2142. * we need to deactivate an old key
  2143. */
  2144. data->ccs_sent = 1;
  2145. /* fall-through */
  2146. case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
  2147. /* Returns 0 on success, -1 otherwise. */
  2148. /*
  2149. * Has this command really been called or is this just a
  2150. * fall-through?
  2151. */
  2152. if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
  2153. data->ccs_rcvd = 1;
  2154. /*
  2155. * CSS has been both, received and sent, so deactivate an old key
  2156. */
  2157. if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
  2158. /* Get active key */
  2159. sockopt_len = sizeof(struct sctp_authkeyid);
  2160. ret =
  2161. getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
  2162. &authkeyid, &sockopt_len);
  2163. if (ret < 0)
  2164. break;
  2165. /*
  2166. * Deactivate key or delete second last key if
  2167. * SCTP_AUTHENTICATION_EVENT is not available.
  2168. */
  2169. authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
  2170. # ifdef SCTP_AUTH_DEACTIVATE_KEY
  2171. sockopt_len = sizeof(struct sctp_authkeyid);
  2172. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
  2173. &authkeyid, sockopt_len);
  2174. if (ret < 0)
  2175. break;
  2176. # endif
  2177. # ifndef SCTP_AUTHENTICATION_EVENT
  2178. if (authkeyid.scact_keynumber > 0) {
  2179. authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
  2180. ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
  2181. &authkeyid, sizeof(struct sctp_authkeyid));
  2182. if (ret < 0)
  2183. break;
  2184. }
  2185. # endif
  2186. data->ccs_rcvd = 0;
  2187. data->ccs_sent = 0;
  2188. }
  2189. break;
  2190. case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
  2191. /* Returns the size of the copied struct. */
  2192. if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
  2193. num = sizeof(struct bio_dgram_sctp_sndinfo);
  2194. memcpy(ptr, &(data->sndinfo), num);
  2195. ret = num;
  2196. break;
  2197. case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
  2198. /* Returns the size of the copied struct. */
  2199. if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
  2200. num = sizeof(struct bio_dgram_sctp_sndinfo);
  2201. memcpy(&(data->sndinfo), ptr, num);
  2202. break;
  2203. case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
  2204. /* Returns the size of the copied struct. */
  2205. if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
  2206. num = sizeof(struct bio_dgram_sctp_rcvinfo);
  2207. memcpy(ptr, &data->rcvinfo, num);
  2208. ret = num;
  2209. break;
  2210. case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
  2211. /* Returns the size of the copied struct. */
  2212. if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
  2213. num = sizeof(struct bio_dgram_sctp_rcvinfo);
  2214. memcpy(&(data->rcvinfo), ptr, num);
  2215. break;
  2216. case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
  2217. /* Returns the size of the copied struct. */
  2218. if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
  2219. num = sizeof(struct bio_dgram_sctp_prinfo);
  2220. memcpy(ptr, &(data->prinfo), num);
  2221. ret = num;
  2222. break;
  2223. case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
  2224. /* Returns the size of the copied struct. */
  2225. if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
  2226. num = sizeof(struct bio_dgram_sctp_prinfo);
  2227. memcpy(&(data->prinfo), ptr, num);
  2228. break;
  2229. case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
  2230. /* Returns always 1. */
  2231. if (num > 0)
  2232. data->save_shutdown = 1;
  2233. else
  2234. data->save_shutdown = 0;
  2235. break;
  2236. case BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY:
  2237. return dgram_sctp_wait_for_dry(b);
  2238. case BIO_CTRL_DGRAM_SCTP_MSG_WAITING:
  2239. return dgram_sctp_msg_waiting(b);
  2240. default:
  2241. /*
  2242. * Pass to default ctrl function to process SCTP unspecific commands
  2243. */
  2244. ret = dgram_ctrl(b, cmd, num, ptr);
  2245. break;
  2246. }
  2247. return ret;
  2248. }
  2249. int BIO_dgram_sctp_notification_cb(BIO *b,
  2250. BIO_dgram_sctp_notification_handler_fn handle_notifications,
  2251. void *context)
  2252. {
  2253. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  2254. if (handle_notifications != NULL) {
  2255. data->handle_notifications = handle_notifications;
  2256. data->notification_context = context;
  2257. } else
  2258. return -1;
  2259. return 0;
  2260. }
  2261. /*
  2262. * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
  2263. * @b: The BIO to check for the dry event
  2264. *
  2265. * Wait until the peer confirms all packets have been received, and so that
  2266. * our kernel doesn't have anything to send anymore. This is only received by
  2267. * the peer's kernel, not the application.
  2268. *
  2269. * Returns:
  2270. * -1 on error
  2271. * 0 when not dry yet
  2272. * 1 when dry
  2273. */
  2274. int BIO_dgram_sctp_wait_for_dry(BIO *b)
  2275. {
  2276. return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_WAIT_FOR_DRY, 0, NULL);
  2277. }
  2278. static int dgram_sctp_wait_for_dry(BIO *b)
  2279. {
  2280. int is_dry = 0;
  2281. int sockflags = 0;
  2282. int n, ret;
  2283. union sctp_notification snp;
  2284. struct msghdr msg;
  2285. struct iovec iov;
  2286. # ifdef SCTP_EVENT
  2287. struct sctp_event event;
  2288. # else
  2289. struct sctp_event_subscribe event;
  2290. socklen_t eventsize;
  2291. # endif
  2292. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  2293. /* set sender dry event */
  2294. # ifdef SCTP_EVENT
  2295. memset(&event, 0, sizeof(event));
  2296. event.se_assoc_id = 0;
  2297. event.se_type = SCTP_SENDER_DRY_EVENT;
  2298. event.se_on = 1;
  2299. ret =
  2300. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
  2301. sizeof(struct sctp_event));
  2302. # else
  2303. eventsize = sizeof(struct sctp_event_subscribe);
  2304. ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
  2305. if (ret < 0)
  2306. return -1;
  2307. event.sctp_sender_dry_event = 1;
  2308. ret =
  2309. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  2310. sizeof(struct sctp_event_subscribe));
  2311. # endif
  2312. if (ret < 0)
  2313. return -1;
  2314. /* peek for notification */
  2315. memset(&snp, 0, sizeof(snp));
  2316. iov.iov_base = (char *)&snp;
  2317. iov.iov_len = sizeof(union sctp_notification);
  2318. msg.msg_name = NULL;
  2319. msg.msg_namelen = 0;
  2320. msg.msg_iov = &iov;
  2321. msg.msg_iovlen = 1;
  2322. msg.msg_control = NULL;
  2323. msg.msg_controllen = 0;
  2324. msg.msg_flags = 0;
  2325. n = recvmsg(b->num, &msg, MSG_PEEK);
  2326. if (n <= 0) {
  2327. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  2328. && (get_last_socket_error() != EWOULDBLOCK))
  2329. return -1;
  2330. else
  2331. return 0;
  2332. }
  2333. /* if we find a notification, process it and try again if necessary */
  2334. while (msg.msg_flags & MSG_NOTIFICATION) {
  2335. memset(&snp, 0, sizeof(snp));
  2336. iov.iov_base = (char *)&snp;
  2337. iov.iov_len = sizeof(union sctp_notification);
  2338. msg.msg_name = NULL;
  2339. msg.msg_namelen = 0;
  2340. msg.msg_iov = &iov;
  2341. msg.msg_iovlen = 1;
  2342. msg.msg_control = NULL;
  2343. msg.msg_controllen = 0;
  2344. msg.msg_flags = 0;
  2345. n = recvmsg(b->num, &msg, 0);
  2346. if (n <= 0) {
  2347. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  2348. && (get_last_socket_error() != EWOULDBLOCK))
  2349. return -1;
  2350. else
  2351. return is_dry;
  2352. }
  2353. if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
  2354. is_dry = 1;
  2355. /* disable sender dry event */
  2356. # ifdef SCTP_EVENT
  2357. memset(&event, 0, sizeof(event));
  2358. event.se_assoc_id = 0;
  2359. event.se_type = SCTP_SENDER_DRY_EVENT;
  2360. event.se_on = 0;
  2361. ret =
  2362. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
  2363. sizeof(struct sctp_event));
  2364. # else
  2365. eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
  2366. ret =
  2367. getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  2368. &eventsize);
  2369. if (ret < 0)
  2370. return -1;
  2371. event.sctp_sender_dry_event = 0;
  2372. ret =
  2373. setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
  2374. sizeof(struct sctp_event_subscribe));
  2375. # endif
  2376. if (ret < 0)
  2377. return -1;
  2378. }
  2379. # ifdef SCTP_AUTHENTICATION_EVENT
  2380. if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
  2381. dgram_sctp_handle_auth_free_key_event(b, &snp);
  2382. # endif
  2383. if (data->handle_notifications != NULL)
  2384. data->handle_notifications(b, data->notification_context,
  2385. (void *)&snp);
  2386. /* found notification, peek again */
  2387. memset(&snp, 0, sizeof(snp));
  2388. iov.iov_base = (char *)&snp;
  2389. iov.iov_len = sizeof(union sctp_notification);
  2390. msg.msg_name = NULL;
  2391. msg.msg_namelen = 0;
  2392. msg.msg_iov = &iov;
  2393. msg.msg_iovlen = 1;
  2394. msg.msg_control = NULL;
  2395. msg.msg_controllen = 0;
  2396. msg.msg_flags = 0;
  2397. /* if we have seen the dry already, don't wait */
  2398. if (is_dry) {
  2399. sockflags = fcntl(b->num, F_GETFL, 0);
  2400. fcntl(b->num, F_SETFL, O_NONBLOCK);
  2401. }
  2402. n = recvmsg(b->num, &msg, MSG_PEEK);
  2403. if (is_dry) {
  2404. fcntl(b->num, F_SETFL, sockflags);
  2405. }
  2406. if (n <= 0) {
  2407. if ((n < 0) && (get_last_socket_error() != EAGAIN)
  2408. && (get_last_socket_error() != EWOULDBLOCK))
  2409. return -1;
  2410. else
  2411. return is_dry;
  2412. }
  2413. }
  2414. /* read anything else */
  2415. return is_dry;
  2416. }
  2417. int BIO_dgram_sctp_msg_waiting(BIO *b)
  2418. {
  2419. return (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SCTP_MSG_WAITING, 0, NULL);
  2420. }
  2421. static int dgram_sctp_msg_waiting(BIO *b)
  2422. {
  2423. int n, sockflags;
  2424. union sctp_notification snp;
  2425. struct msghdr msg;
  2426. struct iovec iov;
  2427. bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
  2428. /* Check if there are any messages waiting to be read */
  2429. do {
  2430. memset(&snp, 0, sizeof(snp));
  2431. iov.iov_base = (char *)&snp;
  2432. iov.iov_len = sizeof(union sctp_notification);
  2433. msg.msg_name = NULL;
  2434. msg.msg_namelen = 0;
  2435. msg.msg_iov = &iov;
  2436. msg.msg_iovlen = 1;
  2437. msg.msg_control = NULL;
  2438. msg.msg_controllen = 0;
  2439. msg.msg_flags = 0;
  2440. sockflags = fcntl(b->num, F_GETFL, 0);
  2441. fcntl(b->num, F_SETFL, O_NONBLOCK);
  2442. n = recvmsg(b->num, &msg, MSG_PEEK);
  2443. fcntl(b->num, F_SETFL, sockflags);
  2444. /* if notification, process and try again */
  2445. if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
  2446. # ifdef SCTP_AUTHENTICATION_EVENT
  2447. if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
  2448. dgram_sctp_handle_auth_free_key_event(b, &snp);
  2449. # endif
  2450. memset(&snp, 0, sizeof(snp));
  2451. iov.iov_base = (char *)&snp;
  2452. iov.iov_len = sizeof(union sctp_notification);
  2453. msg.msg_name = NULL;
  2454. msg.msg_namelen = 0;
  2455. msg.msg_iov = &iov;
  2456. msg.msg_iovlen = 1;
  2457. msg.msg_control = NULL;
  2458. msg.msg_controllen = 0;
  2459. msg.msg_flags = 0;
  2460. n = recvmsg(b->num, &msg, 0);
  2461. if (data->handle_notifications != NULL)
  2462. data->handle_notifications(b, data->notification_context,
  2463. (void *)&snp);
  2464. }
  2465. } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
  2466. /* Return 1 if there is a message to be read, return 0 otherwise. */
  2467. if (n > 0)
  2468. return 1;
  2469. else
  2470. return 0;
  2471. }
  2472. static int dgram_sctp_puts(BIO *bp, const char *str)
  2473. {
  2474. int n, ret;
  2475. n = strlen(str);
  2476. ret = dgram_sctp_write(bp, str, n);
  2477. return ret;
  2478. }
  2479. # endif
  2480. static int BIO_dgram_should_retry(int i)
  2481. {
  2482. int err;
  2483. if ((i == 0) || (i == -1)) {
  2484. err = get_last_socket_error();
  2485. # if defined(OPENSSL_SYS_WINDOWS)
  2486. /*
  2487. * If the socket return value (i) is -1 and err is unexpectedly 0 at
  2488. * this point, the error code was overwritten by another system call
  2489. * before this error handling is called.
  2490. */
  2491. # endif
  2492. return BIO_dgram_non_fatal_error(err);
  2493. }
  2494. return 0;
  2495. }
  2496. int BIO_dgram_non_fatal_error(int err)
  2497. {
  2498. switch (err) {
  2499. # if defined(OPENSSL_SYS_WINDOWS)
  2500. # if defined(WSAEWOULDBLOCK)
  2501. case WSAEWOULDBLOCK:
  2502. # endif
  2503. # endif
  2504. # ifdef EWOULDBLOCK
  2505. # ifdef WSAEWOULDBLOCK
  2506. # if WSAEWOULDBLOCK != EWOULDBLOCK
  2507. case EWOULDBLOCK:
  2508. # endif
  2509. # else
  2510. case EWOULDBLOCK:
  2511. # endif
  2512. # endif
  2513. # ifdef EINTR
  2514. case EINTR:
  2515. # endif
  2516. # ifdef EAGAIN
  2517. # if EWOULDBLOCK != EAGAIN
  2518. case EAGAIN:
  2519. # endif
  2520. # endif
  2521. # ifdef EPROTO
  2522. case EPROTO:
  2523. # endif
  2524. # ifdef EINPROGRESS
  2525. case EINPROGRESS:
  2526. # endif
  2527. # ifdef EALREADY
  2528. case EALREADY:
  2529. # endif
  2530. return 1;
  2531. default:
  2532. break;
  2533. }
  2534. return 0;
  2535. }
  2536. #endif