2
0

bss_dgram.c 86 KB

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