bss_dgram.c 87 KB

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