2
0

bss_dgram.c 87 KB

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