bss_dgram.c 87 KB

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