sctp_server.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2008 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * sctp one to many
  20. */
  21. /*
  22. * History:
  23. * --------
  24. * 2008-08-07 initial version (andrei)
  25. * 2009-02-27 blacklist support (andrei)
  26. * 2009-04-28 sctp stats & events macros (andrei)
  27. */
  28. /*!
  29. * \file
  30. * \brief SIP-router core ::
  31. * \ingroup core
  32. * Module: \ref core
  33. */
  34. #ifdef USE_SCTP
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <netinet/in_systm.h>
  41. #include <netinet/ip.h>
  42. #include <netinet/sctp.h>
  43. #include <errno.h>
  44. #include <arpa/inet.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include "sctp_server.h"
  48. #include "sctp_options.h"
  49. #include "globals.h"
  50. #include "config.h"
  51. #include "dprint.h"
  52. #include "receive.h"
  53. #include "mem/mem.h"
  54. #include "ip_addr.h"
  55. #include "cfg/cfg_struct.h"
  56. #ifdef USE_DST_BLACKLIST
  57. #include "dst_blacklist.h"
  58. #endif /* USE_DST_BLACKLIST */
  59. #include "timer_ticks.h"
  60. #include "clist.h"
  61. #include "error.h"
  62. #include "timer.h"
  63. #include "sctp_stats.h"
  64. #include "sctp_ev.h"
  65. static atomic_t* sctp_conn_no;
  66. #define ABORT_REASON_MAX_ASSOCS \
  67. "Maximum configured number of open associations exceeded"
  68. /* check if the underlying OS supports sctp
  69. returns 0 if yes, -1 on error */
  70. int sctp_check_support()
  71. {
  72. int s;
  73. char buf[256];
  74. s = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
  75. if (s!=-1){
  76. close(s);
  77. if (sctp_check_compiled_sockopts(buf, sizeof(buf))!=0){
  78. LOG(L_WARN, "WARNING: sctp: your ser version was compiled"
  79. " without support for the following sctp options: %s"
  80. ", which might cause unforseen problems \n", buf);
  81. LOG(L_WARN, "WARNING: sctp: please consider recompiling ser with"
  82. " an upgraded sctp library version\n");
  83. }
  84. return 0;
  85. }
  86. return -1;
  87. }
  88. /* append a token to a buffer (uses space between tokens) */
  89. inline static void append_tok2buf(char* buf, int blen, char* tok)
  90. {
  91. char* p;
  92. char* end;
  93. int len;
  94. if (buf && blen){
  95. end=buf+blen;
  96. p=memchr(buf, 0, blen);
  97. if (p==0) goto error;
  98. if (p!=buf && p<(end-1)){
  99. *p=' ';
  100. p++;
  101. }
  102. len=MIN_int(strlen(tok), end-1-p);
  103. memcpy(p, tok, len);
  104. p[len]=0;
  105. }
  106. error:
  107. return;
  108. }
  109. /* check if support fot all the needed sockopts was compiled;
  110. an ascii list of the unsuported options is returned in buf
  111. returns 0 on success and -number of unsuported options on failure
  112. (<0 on failure)
  113. */
  114. int sctp_check_compiled_sockopts(char* buf, int size)
  115. {
  116. int err;
  117. err=0;
  118. if (buf && (size>0)) *buf=0; /* "" */
  119. #ifndef SCTP_FRAGMENT_INTERLEAVE
  120. err++;
  121. append_tok2buf(buf, size, "SCTP_FRAGMENT_INTERLEAVE");
  122. #endif
  123. #ifndef SCTP_PARTIAL_DELIVERY_POINT
  124. err++;
  125. append_tok2buf(buf, size, "SCTP_PARTIAL_DELIVERY_POINT");
  126. #endif
  127. #ifndef SCTP_NODELAY
  128. err++;
  129. append_tok2buf(buf, size, "SCTP_NODELAY");
  130. #endif
  131. #ifndef SCTP_DISABLE_FRAGMENTS
  132. err++;
  133. append_tok2buf(buf, size, "SCTP_DISABLE_FRAGMENTS");
  134. #endif
  135. #ifndef SCTP_AUTOCLOSE
  136. err++;
  137. append_tok2buf(buf, size, "SCTP_AUTOCLOSE");
  138. #endif
  139. #ifndef SCTP_EVENTS
  140. err++;
  141. append_tok2buf(buf, size, "SCTP_EVENTS");
  142. #endif
  143. return -err;
  144. }
  145. /* init all the sockaddr_union members of the socket_info struct
  146. returns 0 on success and -1 on error */
  147. inline static int sctp_init_su(struct socket_info* sock_info)
  148. {
  149. union sockaddr_union* addr;
  150. struct addr_info* ai;
  151. addr=&sock_info->su;
  152. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  153. LOG(L_ERR, "ERROR: sctp_init_su: could not init sockaddr_union for"
  154. "primary sctp address %.*s:%d\n",
  155. sock_info->address_str.len, sock_info->address_str.s,
  156. sock_info->port_no );
  157. goto error;
  158. }
  159. for (ai=sock_info->addr_info_lst; ai; ai=ai->next)
  160. if (init_su(&ai->su, &ai->address, sock_info->port_no)<0){
  161. LOG(L_ERR, "ERROR: sctp_init_su: could not init"
  162. "backup sctp sockaddr_union for %.*s:%d\n",
  163. ai->address_str.len, ai->address_str.s,
  164. sock_info->port_no );
  165. goto error;
  166. }
  167. return 0;
  168. error:
  169. return -1;
  170. }
  171. /** set a socket option (wrapper over setsockopt).
  172. * @param err_prefix - if 0 no error message is printed on failure, if !=0
  173. * it will be prepended to the error message.
  174. * @return 0 on success, -1 on error */
  175. int sctp_setsockopt(int s, int level, int optname,
  176. void* optval, socklen_t optlen, char* err_prefix)
  177. {
  178. if (setsockopt(s, level, optname, optval, optlen) ==-1){
  179. if (err_prefix)
  180. ERR("%s: %s [%d]\n", err_prefix, strerror(errno), errno);
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. /** get a socket option (wrapper over getsockopt).
  186. * @param err_prefix - if 0 no error message is printed on failure, if !=0
  187. * it will be prepended to the error message.
  188. * @return 0 on success, -1 on error */
  189. int sctp_getsockopt(int s, int level, int optname,
  190. void* optval, socklen_t* optlen, char* err_prefix)
  191. {
  192. if (getsockopt(s, level, optname, optval, optlen) ==-1){
  193. if (err_prefix)
  194. ERR("%s: %s [%d]\n", err_prefix, strerror(errno), errno);
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. /** get the os defaults for cfg options with os correspondents.
  200. * @param s - intialized sctp socket
  201. * @param cfg - filled with the os defaults
  202. * @return -1 on error, 0 on success
  203. */
  204. int sctp_get_os_defaults(struct cfg_group_sctp* cfg)
  205. {
  206. int optval;
  207. socklen_t optlen;
  208. int s;
  209. #ifdef SCTP_RTOINFO
  210. struct sctp_rtoinfo rto;
  211. #endif /* SCTP_RTOINFO */
  212. #ifdef SCTP_ASSOCINFO
  213. struct sctp_assocparams ap;
  214. #endif /* SCTP_ASSOCINFO */
  215. #ifdef SCTP_INITMSG
  216. struct sctp_initmsg im;
  217. #endif /* SCTP_INITMSG */
  218. #ifdef SCTP_PEER_ADDR_PARAMS
  219. struct sctp_paddrparams pp;
  220. #endif /* SCTP_PEER_ADDR_PARAMS */
  221. #ifdef SCTP_DELAYED_SACK
  222. struct sctp_sack_info sa;
  223. #elif defined SCTP_DELAYED_ACK_TIME /* old version */
  224. struct sctp_assoc_value sa;
  225. #endif /* SCTP_DELAYED_SACK */
  226. #ifdef SCTP_MAX_BURST
  227. struct sctp_assoc_value av;
  228. #endif /* SCTP_MAX_BURST */
  229. s = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
  230. if (s==-1)
  231. return -1;
  232. /* SO_RCVBUF */
  233. optlen=sizeof(int);
  234. if (sctp_getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&optval,
  235. &optlen, "SO_RCVBUF")==0){
  236. /* success => hack to set the "default" values*/
  237. #ifdef __OS_linux
  238. optval/=2; /* in linux getsockopt() returns 2*set_value */
  239. #endif
  240. cfg->so_rcvbuf=optval;
  241. }
  242. /* SO_SNDBUF */
  243. optlen=sizeof(int);
  244. if (sctp_getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&optval,
  245. &optlen, "SO_SNDBUF")==0){
  246. /* success => hack to set the "default" values*/
  247. #ifdef __OS_linux
  248. optval/=2; /* in linux getsockopt() returns 2*set_value */
  249. #endif
  250. cfg->so_sndbuf=optval;
  251. }
  252. /* SCTP_RTOINFO -> srto_initial, srto_min, srto_max */
  253. #ifdef SCTP_RTOINFO
  254. optlen=sizeof(rto);
  255. rto.srto_assoc_id=0;
  256. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_RTOINFO, (void*)&rto,
  257. &optlen, "SCTP_RTOINFO")==0){
  258. /* success => hack to set the "default" values*/
  259. cfg->srto_initial=rto.srto_initial;
  260. cfg->srto_min=rto.srto_min;
  261. cfg->srto_max=rto.srto_max;
  262. }
  263. #endif /* SCTP_RTOINFO */
  264. #ifdef SCTP_ASSOCINFO
  265. optlen=sizeof(ap);
  266. ap.sasoc_assoc_id=0;
  267. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_ASSOCINFO, (void*)&ap,
  268. &optlen, "SCTP_ASSOCINFO")==0){
  269. /* success => hack to set the "default" values*/
  270. cfg->asocmaxrxt=ap.sasoc_asocmaxrxt;
  271. }
  272. #endif /* SCTP_ASSOCINFO */
  273. #ifdef SCTP_INITMSG
  274. optlen=sizeof(im);
  275. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_INITMSG, (void*)&im,
  276. &optlen, "SCTP_INITMSG")==0){
  277. /* success => hack to set the "default" values*/
  278. cfg->init_max_attempts=im.sinit_max_attempts;
  279. cfg->init_max_timeo=im.sinit_max_init_timeo;
  280. }
  281. #endif /* SCTP_INITMSG */
  282. #ifdef SCTP_PEER_ADDR_PARAMS
  283. optlen=sizeof(pp);
  284. memset(&pp, 0, sizeof(pp)); /* get defaults */
  285. /* set the AF, needed on older linux kernels even for INADDR_ANY */
  286. pp.spp_address.ss_family=AF_INET;
  287. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, (void*)&pp,
  288. &optlen, "SCTP_PEER_ADDR_PARAMS")==0){
  289. /* success => hack to set the "default" values*/
  290. cfg->hbinterval=pp.spp_hbinterval;
  291. cfg->pathmaxrxt=pp.spp_pathmaxrxt;
  292. }
  293. #endif /* SCTP_PEER_ADDR_PARAMS */
  294. #if defined SCTP_DELAYED_SACK || defined SCTP_DELAYED_ACK_TIME
  295. optlen=sizeof(sa);
  296. memset(&sa, 0, sizeof(sa));
  297. #ifdef SCTP_DELAYED_SACK
  298. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_DELAYED_SACK, (void*)&sa,
  299. &optlen, "SCTP_DELAYED_SACK")==0){
  300. /* success => hack to set the "default" values*/
  301. cfg->sack_delay=sa.sack_delay;
  302. cfg->sack_freq=sa.sack_freq;
  303. }
  304. #else /* old sctp lib version which uses SCTP_DELAYED_ACK_TIME */
  305. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_DELAYED_ACK_TIME, (void*)&sa,
  306. &optlen, "SCTP_DELAYED_ACK_TIME")==0){
  307. /* success => hack to set the "default" values*/
  308. cfg->sack_delay=sa.assoc_value;
  309. cfg->sack_freq=0; /* unknown */
  310. }
  311. #endif /* SCTP_DELAYED_SACK */
  312. #endif /* SCTP_DELAYED_SACK | SCTP_DELAYED_ACK_TIME*/
  313. #ifdef SCTP_MAX_BURST
  314. optlen=sizeof(av);
  315. av.assoc_id=0;
  316. if (sctp_getsockopt(s, IPPROTO_SCTP, SCTP_MAX_BURST, (void*)&av,
  317. &optlen, "SCTP_MAX_BURST")==0){
  318. /* success => hack to set the "default" values*/
  319. cfg->max_burst=av.assoc_value;
  320. }
  321. #endif /* SCTP_MAX_BURST */
  322. close(s);
  323. return 0;
  324. }
  325. /* set common (for one to many and one to one) sctp socket options
  326. tries to ignore non-critical errors (it will only log them), for
  327. improved portability (for example older linux kernel version support
  328. only a limited number of sctp socket options)
  329. returns 0 on success, -1 on error
  330. WARNING: please keep it sync'ed w/ sctp_check_compiled_sockopts() */
  331. static int sctp_init_sock_opt_common(int s, int af)
  332. {
  333. int optval;
  334. int pd_point;
  335. int saved_errno;
  336. socklen_t optlen;
  337. int sctp_err;
  338. #ifdef SCTP_RTOINFO
  339. struct sctp_rtoinfo rto;
  340. #endif /* SCTP_RTOINFO */
  341. #ifdef SCTP_ASSOCINFO
  342. struct sctp_assocparams ap;
  343. #endif /* SCTP_ASSOCINFO */
  344. #ifdef SCTP_INITMSG
  345. struct sctp_initmsg im;
  346. #endif /* SCTP_INITMSG */
  347. #ifdef SCTP_PEER_ADDR_PARAMS
  348. struct sctp_paddrparams pp;
  349. #endif /* SCTP_PEER_ADDR_PARAMS */
  350. #ifdef SCTP_DELAYED_SACK
  351. struct sctp_sack_info sa;
  352. #elif defined SCTP_DELAYED_ACK_TIME /* old version */
  353. struct sctp_assoc_value sa;
  354. #endif /* SCTP_DELAYED_SACK */
  355. #ifdef SCTP_MAX_BURST
  356. struct sctp_assoc_value av;
  357. #endif /* SCTP_MAX_BURST */
  358. #ifdef __OS_linux
  359. union {
  360. struct sctp_event_subscribe s;
  361. char padding[sizeof(struct sctp_event_subscribe)+sizeof(__u8)];
  362. } es;
  363. #else
  364. struct sctp_event_subscribe es;
  365. #endif
  366. struct sctp_event_subscribe* ev_s;
  367. sctp_err=0;
  368. #ifdef __OS_linux
  369. ev_s=&es.s;
  370. #else
  371. ev_s=&es;
  372. #endif
  373. /* set tos */
  374. optval = tos;
  375. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
  376. LOG(L_WARN, "WARNING: sctp_init_sock_opt_common: setsockopt tos: %s\n",
  377. strerror(errno));
  378. /* continue since this is not critical */
  379. }
  380. /* set receive buffer: SO_RCVBUF*/
  381. if (cfg_get(sctp, sctp_cfg, so_rcvbuf)){
  382. optval=cfg_get(sctp, sctp_cfg, so_rcvbuf);
  383. if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
  384. (void*)&optval, sizeof(optval)) ==-1){
  385. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt:"
  386. " SO_RCVBUF (%d): %s\n", optval, strerror(errno));
  387. /* continue, non-critical */
  388. }
  389. }
  390. /* set send buffer: SO_SNDBUF */
  391. if (cfg_get(sctp, sctp_cfg, so_sndbuf)){
  392. optval=cfg_get(sctp, sctp_cfg, so_sndbuf);
  393. if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
  394. (void*)&optval, sizeof(optval)) ==-1){
  395. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt:"
  396. " SO_SNDBUF (%d): %s\n", optval, strerror(errno));
  397. /* continue, non-critical */
  398. }
  399. }
  400. /* set reuseaddr */
  401. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  402. (void*)&optval, sizeof(optval))==-1){
  403. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt:"
  404. " SO_REUSEADDR (%d): %s\n", optval, strerror(errno));
  405. /* continue, non-critical */
  406. }
  407. /* disable fragments interleave (SCTP_FRAGMENT_INTERLEAVE) --
  408. * we don't want partial delivery, so fragment interleave must be off too
  409. */
  410. #ifdef SCTP_FRAGMENT_INTERLEAVE
  411. optval=0;
  412. if (setsockopt(s, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE ,
  413. (void*)&optval, sizeof(optval)) ==-1){
  414. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  415. "SCTP_FRAGMENT_INTERLEAVE: %s\n", strerror(errno));
  416. sctp_err++;
  417. /* try to continue */
  418. }
  419. #else
  420. #warning no sctp lib support for SCTP_FRAGMENT_INTERLEAVE, consider upgrading
  421. #endif /* SCTP_FRAGMENT_INTERLEAVE */
  422. /* turn off partial delivery: on linux setting SCTP_PARTIAL_DELIVERY_POINT
  423. * to 0 or a very large number seems to be enough, however the portable
  424. * way to do it is to set it to the socket receive buffer size
  425. * (this is the maximum value allowed in the sctp api draft) */
  426. #ifdef SCTP_PARTIAL_DELIVERY_POINT
  427. optlen=sizeof(optval);
  428. if (getsockopt(s, SOL_SOCKET, SO_RCVBUF,
  429. (void*)&optval, &optlen) ==-1){
  430. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: getsockopt: "
  431. "SO_RCVBUF: %s\n", strerror(errno));
  432. /* try to continue */
  433. optval=0;
  434. }
  435. #ifdef __OS_linux
  436. optval/=2; /* in linux getsockopt() returns twice the set value */
  437. #endif
  438. pd_point=optval;
  439. saved_errno=0;
  440. while(pd_point &&
  441. setsockopt(s, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
  442. (void*)&pd_point, sizeof(pd_point)) ==-1){
  443. if (!saved_errno)
  444. saved_errno=errno;
  445. pd_point--;
  446. }
  447. if (pd_point!=optval){
  448. if (pd_point==0){
  449. /* all attempts failed */
  450. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  451. "SCTP_PARTIAL_DELIVERY_POINT (%d): %s\n",
  452. optval, strerror(errno));
  453. sctp_err++;
  454. /* try to continue */
  455. }else{
  456. /* success but to a lower value (might not be disabled) */
  457. LOG(L_WARN, "setsockopt SCTP_PARTIAL_DELIVERY_POINT set to %d, but"
  458. " the socket rcvbuf is %d (higher values fail with"
  459. " \"%s\" [%d])\n",
  460. pd_point, optval, strerror(saved_errno), saved_errno);
  461. }
  462. }
  463. #else
  464. #warning no sctp lib support for SCTP_PARTIAL_DELIVERY_POINT, consider upgrading
  465. #endif /* SCTP_PARTIAL_DELIVERY_POINT */
  466. /* nagle / no delay */
  467. #ifdef SCTP_NODELAY
  468. optval=1;
  469. if (setsockopt(s, IPPROTO_SCTP, SCTP_NODELAY,
  470. (void*)&optval, sizeof(optval)) ==-1){
  471. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  472. "SCTP_NODELAY: %s\n", strerror(errno));
  473. sctp_err++;
  474. /* non critical, try to continue */
  475. }
  476. #else
  477. #warning no sctp lib support for SCTP_NODELAY, consider upgrading
  478. #endif /* SCTP_NODELAY */
  479. /* enable message fragmentation (SCTP_DISABLE_FRAGMENTS) (on send) */
  480. #ifdef SCTP_DISABLE_FRAGMENTS
  481. optval=0;
  482. if (setsockopt(s, IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS,
  483. (void*)&optval, sizeof(optval)) ==-1){
  484. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  485. "SCTP_DISABLE_FRAGMENTS: %s\n", strerror(errno));
  486. sctp_err++;
  487. /* non critical, try to continue */
  488. }
  489. #else
  490. #warning no sctp lib support for SCTP_DISABLE_FRAGMENTS, consider upgrading
  491. #endif /* SCTP_DISABLE_FRAGMENTS */
  492. /* set autoclose */
  493. #ifdef SCTP_AUTOCLOSE
  494. optval=cfg_get(sctp, sctp_cfg, autoclose);
  495. if (setsockopt(s, IPPROTO_SCTP, SCTP_AUTOCLOSE,
  496. (void*)&optval, sizeof(optval)) ==-1){
  497. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  498. "SCTP_AUTOCLOSE: %s (critical)\n", strerror(errno));
  499. /* critical: w/o autoclose we could have sctp connection living
  500. forever (if the remote side doesn't close them) */
  501. sctp_err++;
  502. goto error;
  503. }
  504. #else
  505. #error SCTP_AUTOCLOSE not supported, please upgrade your sctp library
  506. #endif /* SCTP_AUTOCLOSE */
  507. /* set rtoinfo options: srto_initial, srto_min, srto_max */
  508. #ifdef SCTP_RTOINFO
  509. memset(&rto, 0, sizeof(rto));
  510. rto.srto_initial=cfg_get(sctp, sctp_cfg, srto_initial);
  511. rto.srto_min=cfg_get(sctp, sctp_cfg, srto_min);
  512. rto.srto_max=cfg_get(sctp, sctp_cfg, srto_max);
  513. if (rto.srto_initial || rto.srto_min || rto.srto_max){
  514. /* if at least one is non-null => we have to set it */
  515. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_RTOINFO, (void*)&rto,
  516. sizeof(rto), "setsockopt: SCTP_RTOINFO")!=0){
  517. sctp_err++;
  518. /* non critical, try to continue */
  519. }
  520. }
  521. #else
  522. #warning no sctp lib support for SCTP_RTOINFO, consider upgrading
  523. #endif /* SCTP_RTOINFO */
  524. /* set associnfo options: assocmaxrxt */
  525. #ifdef SCTP_ASSOCINFO
  526. memset(&ap, 0, sizeof(ap));
  527. ap.sasoc_asocmaxrxt=cfg_get(sctp, sctp_cfg, asocmaxrxt);
  528. if (ap.sasoc_asocmaxrxt){
  529. /* if at least one is non-null => we have to set it */
  530. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_ASSOCINFO, (void*)&ap,
  531. sizeof(ap), "setsockopt: SCTP_ASSOCINFO")!=0){
  532. sctp_err++;
  533. /* non critical, try to continue */
  534. }
  535. }
  536. #else
  537. #warning no sctp lib support for SCTP_ASSOCINFO, consider upgrading
  538. #endif /* SCTP_ASOCINFO */
  539. /* set initmsg options: init_max_attempts & init_max_init_timeo */
  540. #ifdef SCTP_INITMSG
  541. memset(&im, 0, sizeof(im));
  542. im.sinit_max_attempts=cfg_get(sctp, sctp_cfg, init_max_attempts);
  543. im.sinit_max_init_timeo=cfg_get(sctp, sctp_cfg, init_max_timeo);
  544. if (im.sinit_max_attempts || im.sinit_max_init_timeo){
  545. /* if at least one is non-null => we have to set it */
  546. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_INITMSG, (void*)&im,
  547. sizeof(im), "setsockopt: SCTP_INITMSG")!=0){
  548. sctp_err++;
  549. /* non critical, try to continue */
  550. }
  551. }
  552. #else
  553. #warning no sctp lib support for SCTP_INITMSG, consider upgrading
  554. #endif /* SCTP_INITMSG */
  555. /* set sctp peer addr options: hbinterval & pathmaxrxt */
  556. #ifdef SCTP_PEER_ADDR_PARAMS
  557. memset(&pp, 0, sizeof(pp));
  558. pp.spp_address.ss_family=af;
  559. pp.spp_hbinterval=cfg_get(sctp, sctp_cfg, hbinterval);
  560. pp.spp_pathmaxrxt=cfg_get(sctp, sctp_cfg, pathmaxrxt);
  561. if (pp.spp_hbinterval || pp.spp_pathmaxrxt){
  562. if (pp.spp_hbinterval > 0)
  563. pp.spp_flags=SPP_HB_ENABLE;
  564. else if (pp.spp_hbinterval==-1){
  565. pp.spp_flags=SPP_HB_DISABLE;
  566. pp.spp_hbinterval=0;
  567. }
  568. #ifdef __OS_linux
  569. if (pp.spp_pathmaxrxt){
  570. /* hack to work on linux, pathmaxrxt is set only if
  571. SPP_PMTUD_ENABLE */
  572. pp.spp_flags|=SPP_PMTUD_ENABLE;
  573. }
  574. #endif /*__OS_linux */
  575. /* if at least one is non-null => we have to set it */
  576. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, (void*)&pp,
  577. sizeof(pp), "setsockopt: SCTP_PEER_ADDR_PARAMS")!=0){
  578. sctp_err++;
  579. /* non critical, try to continue */
  580. }
  581. }
  582. #else
  583. #warning no sctp lib support for SCTP_PEER_ADDR_PARAMS, consider upgrading
  584. #endif /* SCTP_PEER_ADDR_PARAMS */
  585. /* set delayed ack options: sack_delay & sack_freq */
  586. #if defined SCTP_DELAYED_SACK || defined SCTP_DELAYED_ACK_TIME
  587. memset(&sa, 0, sizeof(sa));
  588. #ifdef SCTP_DELAYED_SACK
  589. sa.sack_delay=cfg_get(sctp, sctp_cfg, sack_delay);
  590. sa.sack_freq=cfg_get(sctp, sctp_cfg, sack_freq);
  591. if (sa.sack_delay || sa.sack_freq){
  592. /* if at least one is non-null => we have to set it */
  593. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_DELAYED_SACK, (void*)&sa,
  594. sizeof(sa), "setsockopt: SCTP_DELAYED_SACK")!=0){
  595. sctp_err++;
  596. /* non critical, try to continue */
  597. }
  598. }
  599. #else /* old sctp lib version which uses SCTP_DELAYED_ACK_TIME */
  600. sa.assoc_value=cfg_get(sctp, sctp_cfg, sack_delay);
  601. if (sa.assoc_value){
  602. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_DELAYED_ACK_TIME, (void*)&sa,
  603. sizeof(sa), "setsockopt: SCTP_DELAYED_ACK_TIME")!=0){
  604. sctp_err++;
  605. /* non critical, try to continue */
  606. }
  607. }
  608. if (cfg_get(sctp, sctp_cfg, sack_freq)){
  609. WARN("could not set sctp sack_freq, please upgrade your sctp"
  610. " library\n");
  611. ((struct cfg_group_sctp*)sctp_cfg)->sack_freq=0;
  612. }
  613. #endif /* SCTP_DELAYED_SACK */
  614. #else /* SCTP_DELAYED_SACK | SCTP_DELAYED_ACK*/
  615. #warning no sctp lib support for SCTP_DELAYED_SACK, consider upgrading
  616. #endif /* SCTP_DELAYED_SACK | SCTP_DELAYED_ACK_TIME*/
  617. /* set max burst option */
  618. #ifdef SCTP_MAX_BURST
  619. memset(&av, 0, sizeof(av));
  620. av.assoc_value=cfg_get(sctp, sctp_cfg, max_burst);
  621. if (av.assoc_value){
  622. if (sctp_setsockopt(s, IPPROTO_SCTP, SCTP_MAX_BURST, (void*)&av,
  623. sizeof(av), "setsockopt: SCTP_MAX_BURST")!=0){
  624. sctp_err++;
  625. /* non critical, try to continue */
  626. }
  627. }
  628. #else
  629. #warning no sctp lib support for SCTP_MAX_BURST, consider upgrading
  630. #endif /* SCTP_MAX_BURST */
  631. memset(&es, 0, sizeof(es));
  632. /* SCTP_EVENTS for SCTP_SNDRCV (sctp_data_io_event) -> per message
  633. * information in sctp_sndrcvinfo */
  634. ev_s->sctp_data_io_event=1;
  635. /* enable association event notifications */
  636. ev_s->sctp_association_event=1; /* SCTP_ASSOC_CHANGE */
  637. ev_s->sctp_address_event=1; /* enable address events notifications */
  638. ev_s->sctp_send_failure_event=1; /* SCTP_SEND_FAILED */
  639. ev_s->sctp_peer_error_event=1; /* SCTP_REMOTE_ERROR */
  640. ev_s->sctp_shutdown_event=1; /* SCTP_SHUTDOWN_EVENT */
  641. ev_s->sctp_partial_delivery_event=1; /* SCTP_PARTIAL_DELIVERY_EVENT */
  642. /* ev_s->sctp_adaptation_layer_event=1; - not supported by lksctp<=1.0.6*/
  643. /* ev_s->sctp_authentication_event=1; -- not supported on linux 2.6.25 */
  644. /* enable the SCTP_EVENTS */
  645. #ifdef SCTP_EVENTS
  646. if (setsockopt(s, IPPROTO_SCTP, SCTP_EVENTS, ev_s, sizeof(*ev_s))==-1){
  647. /* on linux the checks for the struct sctp_event_subscribe size
  648. are too strict, making certain lksctp/kernel combination
  649. unworkable => since we don't use the extra information
  650. (sctp_authentication_event) added in newer version, we can
  651. try with different sizes) */
  652. #ifdef __OS_linux
  653. /* 1. lksctp 1.0.9 with kernel < 2.6.26 -> kernel expects
  654. the structure without the authentication event member */
  655. if (setsockopt(s, IPPROTO_SCTP, SCTP_EVENTS, ev_s, sizeof(*ev_s)-1)==0)
  656. goto ev_success;
  657. /* 2. lksctp < 1.0.9? with kernel >= 2.6.26: the sctp.h structure
  658. does not have the authentication member, but the newer kernels
  659. check only for optlen > sizeof(...) => we should never reach
  660. this point. */
  661. /* 3. just to be foolproof if we reached this point, try
  662. with a bigger size before giving up (out of desperation) */
  663. if (setsockopt(s, IPPROTO_SCTP, SCTP_EVENTS, ev_s, sizeof(es))==0)
  664. goto ev_success;
  665. #endif
  666. LOG(L_ERR, "ERROR: sctp_init_sock_opt_common: setsockopt: "
  667. "SCTP_EVENTS: %s\n", strerror(errno));
  668. sctp_err++;
  669. goto error; /* critical */
  670. }
  671. #ifdef __OS_linux
  672. ev_success:
  673. #endif
  674. #else
  675. #error no sctp lib support for SCTP_EVENTS, consider upgrading
  676. #endif /* SCTP_EVENTS */
  677. if (sctp_err){
  678. LOG(L_ERR, "ERROR: sctp: setting some sctp sockopts failed, "
  679. "consider upgrading your kernel\n");
  680. }
  681. return 0;
  682. error:
  683. return -1;
  684. }
  685. /* bind all addresses from sock (sockaddr_unions)
  686. returns 0 on success, .1 on error */
  687. static int sctp_bind_sock(struct socket_info* sock_info)
  688. {
  689. struct addr_info* ai;
  690. union sockaddr_union* addr;
  691. addr=&sock_info->su;
  692. /* bind the addresses*/
  693. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  694. LOG(L_ERR, "ERROR: sctp_bind_sock: bind(%x, %p, %d) on %s: %s\n",
  695. sock_info->socket, &addr->s,
  696. (unsigned)sockaddru_len(*addr),
  697. sock_info->address_str.s,
  698. strerror(errno));
  699. #ifdef USE_IPV6
  700. if (addr->s.sa_family==AF_INET6)
  701. LOG(L_ERR, "ERROR: sctp_bind_sock: might be caused by using a "
  702. "link local address, try site local or global\n");
  703. #endif
  704. goto error;
  705. }
  706. for (ai=sock_info->addr_info_lst; ai; ai=ai->next)
  707. if (sctp_bindx(sock_info->socket, &ai->su.s, 1, SCTP_BINDX_ADD_ADDR)
  708. ==-1){
  709. LOG(L_ERR, "ERROR: sctp_bind_sock: sctp_bindx(%x, %.*s:%d, 1, ...)"
  710. " on %s:%d : [%d] %s (trying to continue)\n",
  711. sock_info->socket,
  712. ai->address_str.len, ai->address_str.s,
  713. sock_info->port_no,
  714. sock_info->address_str.s, sock_info->port_no,
  715. errno, strerror(errno));
  716. #ifdef USE_IPV6
  717. if (ai->su.s.sa_family==AF_INET6)
  718. LOG(L_ERR, "ERROR: sctp_bind_sock: might be caused by using a "
  719. "link local address, try site local or global\n");
  720. #endif
  721. /* try to continue, a secondary address bind failure is not
  722. * critical */
  723. }
  724. return 0;
  725. error:
  726. return -1;
  727. }
  728. /* init, bind & start listening on the corresp. sctp socket
  729. returns 0 on success, -1 on error */
  730. int sctp_init_sock(struct socket_info* sock_info)
  731. {
  732. union sockaddr_union* addr;
  733. sock_info->proto=PROTO_SCTP;
  734. addr=&sock_info->su;
  735. if (sctp_init_su(sock_info)!=0)
  736. goto error;
  737. sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_SEQPACKET,
  738. IPPROTO_SCTP);
  739. if (sock_info->socket==-1){
  740. LOG(L_ERR, "ERROR: sctp_init_sock: socket: %s\n", strerror(errno));
  741. goto error;
  742. }
  743. INFO("sctp: socket %d initialized (%p)\n", sock_info->socket, sock_info);
  744. /* make socket non-blocking */
  745. #if 0
  746. /* recvmsg must block so use blocking sockets
  747. * and send with MSG_DONTWAIT */
  748. optval=fcntl(sock_info->socket, F_GETFL);
  749. if (optval==-1){
  750. LOG(L_ERR, "ERROR: init_sctp: fnctl failed: (%d) %s\n",
  751. errno, strerror(errno));
  752. goto error;
  753. }
  754. if (fcntl(sock_info->socket, F_SETFL, optval|O_NONBLOCK)==-1){
  755. LOG(L_ERR, "ERROR: init_sctp: fcntl: set non-blocking failed:"
  756. " (%d) %s\n", errno, strerror(errno));
  757. goto error;
  758. }
  759. #endif
  760. /* set sock opts */
  761. if (sctp_init_sock_opt_common(sock_info->socket, sock_info->address.af)!=0)
  762. goto error;
  763. /* SCTP_EVENTS for send dried out -> present in the draft not yet
  764. * present in linux (might help to detect when we could send again to
  765. * some peer, kind of poor's man poll on write, based on received
  766. * SCTP_SENDER_DRY_EVENTs */
  767. if (sctp_bind_sock(sock_info)<0)
  768. goto error;
  769. if (listen(sock_info->socket, 1)<0){
  770. LOG(L_ERR, "ERROR: sctp_init_sock: listen(%x, 1) on %s: %s\n",
  771. sock_info->socket, sock_info->address_str.s,
  772. strerror(errno));
  773. goto error;
  774. }
  775. return 0;
  776. error:
  777. return -1;
  778. }
  779. #define USE_SCTP_OO
  780. #ifdef USE_SCTP_OO
  781. /* init, bind & start listening on the corresp. sctp socket, using
  782. sctp one-to-one mode
  783. returns 0 on success, -1 on error */
  784. int sctp_init_sock_oo(struct socket_info* sock_info)
  785. {
  786. union sockaddr_union* addr;
  787. int optval;
  788. sock_info->proto=PROTO_SCTP;
  789. addr=&sock_info->su;
  790. if (sctp_init_su(sock_info)!=0)
  791. goto error;
  792. sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_STREAM,
  793. IPPROTO_SCTP);
  794. if (sock_info->socket==-1){
  795. LOG(L_ERR, "ERROR: sctp_init_sock_oo: socket: %s\n", strerror(errno));
  796. goto error;
  797. }
  798. INFO("sctp:oo socket %d initialized (%p)\n", sock_info->socket, sock_info);
  799. /* make socket non-blocking */
  800. optval=fcntl(sock_info->socket, F_GETFL);
  801. if (optval==-1){
  802. LOG(L_ERR, "ERROR: sctp_init_sock_oo: fnctl failed: (%d) %s\n",
  803. errno, strerror(errno));
  804. goto error;
  805. }
  806. if (fcntl(sock_info->socket, F_SETFL, optval|O_NONBLOCK)==-1){
  807. LOG(L_ERR, "ERROR: sctp_init_sock_oo: fcntl: set non-blocking failed:"
  808. " (%d) %s\n", errno, strerror(errno));
  809. goto error;
  810. }
  811. /* set sock opts */
  812. if (sctp_init_sock_opt_common(sock_info->socket, sock_info->address.af)!=0)
  813. goto error;
  814. #ifdef SCTP_REUSE_PORT
  815. /* set reuse port */
  816. optval=1;
  817. if (setsockopt(sock_info->socket, IPPROTO_SCTP, SCTP_REUSE_PORT ,
  818. (void*)&optval, sizeof(optval)) ==-1){
  819. LOG(L_ERR, "ERROR: sctp_init_sock_oo: setsockopt: "
  820. "SCTP_REUSE_PORT: %s\n", strerror(errno));
  821. goto error;
  822. }
  823. #endif /* SCTP_REUSE_PORT */
  824. if (sctp_bind_sock(sock_info)<0)
  825. goto error;
  826. if (listen(sock_info->socket, 1)<0){
  827. LOG(L_ERR, "ERROR: sctp_init_sock_oo: listen(%x, 1) on %s: %s\n",
  828. sock_info->socket, sock_info->address_str.s,
  829. strerror(errno));
  830. goto error;
  831. }
  832. return 0;
  833. error:
  834. return -1;
  835. }
  836. #endif /* USE_SCTP_OO */
  837. #ifdef SCTP_CONN_REUSE
  838. /* we need SCTP_ADDR_HASH for being able to make inquires related to existing
  839. sctp association to a particular address (optional) */
  840. /*#define SCTP_ADDR_HASH*/
  841. #define SCTP_ID_HASH_SIZE 1024 /* must be 2^k */
  842. #define SCTP_ASSOC_HASH_SIZE 1024 /* must be 2^k */
  843. #define SCTP_ADDR_HASH_SIZE 1024 /* must be 2^k */
  844. /* lock method */
  845. #ifdef GEN_LOCK_T_UNLIMITED
  846. #define SCTP_HASH_LOCK_PER_BUCKET
  847. #elif defined GEN_LOCK_SET_T_UNLIMITED
  848. #define SCTP_HASH_LOCK_SET
  849. #else
  850. #define SCTP_HASH_ONE_LOCK
  851. #endif
  852. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  853. /* lock included in the hash bucket */
  854. #define LOCK_SCTP_ID_H(h) lock_get(&sctp_con_id_hash[(h)].lock)
  855. #define UNLOCK_SCTP_ID_H(h) lock_release(&sctp_con_id_hash[(h)].lock)
  856. #define LOCK_SCTP_ASSOC_H(h) lock_get(&sctp_con_assoc_hash[(h)].lock)
  857. #define UNLOCK_SCTP_ASSOC_H(h) lock_release(&sctp_con_assoc_hash[(h)].lock)
  858. #define LOCK_SCTP_ADDR_H(h) lock_get(&sctp_con_addr_hash[(h)].lock)
  859. #define UNLOCK_SCTP_ADDR_H(h) lock_release(&sctp_con_addr_hash[(h)].lock)
  860. #elif defined SCTP_HASH_LOCK_SET
  861. static gen_lock_set_t* sctp_con_id_h_lock_set=0;
  862. static gen_lock_set_t* sctp_con_assoc_h_lock_set=0;
  863. static gen_lock_set_t* sctp_con_addr_h_lock_set=0;
  864. #define LOCK_SCTP_ID_H(h) lock_set_get(sctp_con_id_h_lock_set, (h))
  865. #define UNLOCK_SCTP_ID_H(h) lock_set_release(sctp_con_id_h_lock_set, (h))
  866. #define LOCK_SCTP_ASSOC_H(h) lock_set_get(sctp_con_assoc_h_lock_set, (h))
  867. #define UNLOCK_SCTP_ASSOC_H(h) \
  868. lock_set_release(sctp_con_assoc_h_lock_set, (h))
  869. #define LOCK_SCTP_ADDR_H(h) lock_set_get(sctp_con_addr_h_lock_set, (h))
  870. #define UNLOCK_SCTP_ADDR_H(h) lock_set_release(sctp_con_addr_h_lock_set, (h))
  871. #else /* use only one lock */
  872. static gen_lock_t* sctp_con_id_h_lock=0;
  873. static gen_lock_t* sctp_con_assoc_h_lock=0;
  874. static gen_lock_t* sctp_con_addr_h_lock=0;
  875. #define LOCK_SCTP_ID_H(h) lock_get(sctp_con_id_h_lock)
  876. #define UNLOCK_SCTP_ID_H(h) lock_release(sctp_con_id_hlock)
  877. #define LOCK_SCTP_ASSOC_H(h) lock_get(sctp_con_assoc_h_lock)
  878. #define UNLOCK_SCTP_ASSOC_H(h) lock_release(sctp_con_assoc_h_lock)
  879. #define LOCK_SCTP_ADDR_H(h) lock_get(sctp_con_addr_h_lock)
  880. #define UNLOCK_SCTP_ADDR_H(h) lock_release(sctp_con_addr_h_lock)
  881. #endif /* SCTP_HASH_LOCK_PER_BUCKET */
  882. /* sctp connection flags */
  883. #define SCTP_CON_UP_SEEN 1
  884. #define SCTP_CON_RCV_SEEN 2
  885. #define SCTP_CON_DOWN_SEEN 4
  886. struct sctp_connection{
  887. unsigned int id; /**< ser unique global id */
  888. unsigned int assoc_id; /**< sctp assoc id (can be reused for new assocs)*/
  889. struct socket_info* si; /**< local socket used */
  890. unsigned flags; /**< internal flags UP_SEEN, RCV_SEEN, DOWN_SEEN */
  891. ticks_t start;
  892. ticks_t expire;
  893. union sockaddr_union remote; /**< remote ip & port */
  894. };
  895. struct sctp_lst_connector{
  896. /* id hash */
  897. struct sctp_con_elem* next_id;
  898. struct sctp_con_elem* prev_id;
  899. /* assoc hash */
  900. struct sctp_con_elem* next_assoc;
  901. struct sctp_con_elem* prev_assoc;
  902. #ifdef SCTP_ADDR_HASH
  903. /* addr hash */
  904. struct sctp_con_elem* next_addr;
  905. struct sctp_con_elem* prev_addr;
  906. #endif /* SCTP_ADDR_HASH */
  907. };
  908. struct sctp_con_elem{
  909. struct sctp_lst_connector l; /* must be first */
  910. atomic_t refcnt;
  911. /* data */
  912. struct sctp_connection con;
  913. };
  914. struct sctp_con_id_hash_head{
  915. struct sctp_lst_connector l; /* must be first */
  916. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  917. gen_lock_t lock;
  918. #endif /* SCTP_HASH_LOCK_PER_BUCKET */
  919. };
  920. struct sctp_con_assoc_hash_head{
  921. struct sctp_lst_connector l; /* must be first */
  922. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  923. gen_lock_t lock;
  924. #endif /* SCTP_HASH_LOCK_PER_BUCKET */
  925. };
  926. #ifdef SCTP_ADDR_HASH
  927. struct sctp_con_addr_hash_head{
  928. struct sctp_lst_connector l; /* must be first */
  929. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  930. gen_lock_t lock;
  931. #endif /* SCTP_HASH_LOCK_PER_BUCKET */
  932. };
  933. #endif /* SCTP_ADDR_HASH */
  934. static struct sctp_con_id_hash_head* sctp_con_id_hash;
  935. static struct sctp_con_assoc_hash_head* sctp_con_assoc_hash;
  936. #ifdef SCTP_ADDR_HASH
  937. static struct sctp_con_addr_hash_head* sctp_con_addr_hash;
  938. #endif /* SCTP_ADDR_HASH */
  939. static atomic_t* sctp_id;
  940. static atomic_t* sctp_conn_tracked;
  941. #define get_sctp_con_id_hash(id) ((id) % SCTP_ID_HASH_SIZE)
  942. #define get_sctp_con_assoc_hash(assoc_id) ((assoc_id) % SCTP_ASSOC_HASH_SIZE)
  943. #ifdef SCTP_ADDR_HASH
  944. static inline unsigned get_sctp_con_addr_hash(union sockaddr_union* remote,
  945. struct socket_info* si)
  946. {
  947. struct ip_addr ip;
  948. unsigned short port;
  949. unsigned h;
  950. su2ip_addr(&ip, remote);
  951. port=su_getport(remote);
  952. if (likely(ip.len==4))
  953. h=ip.u.addr32[0]^port;
  954. else if (ip.len==16)
  955. h=ip.u.addr32[0]^ip.u.addr32[1]^ip.u.addr32[2]^ ip.u.addr32[3]^port;
  956. else
  957. h=0; /* error */
  958. /* make sure the first bits are influenced by all 32
  959. * (the first log2(SCTP_ADDR_HASH_SIZE) bits should be a mix of all
  960. * 32)*/
  961. h ^= h>>17;
  962. h ^= h>>7;
  963. return h & (SCTP_ADDR_HASH_SIZE-1);
  964. }
  965. #endif /* SCTP_ADDR_HASH */
  966. /** destroy sctp conn hashes. */
  967. void destroy_sctp_con_tracking()
  968. {
  969. int r;
  970. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  971. if (sctp_con_id_hash)
  972. for(r=0; r<SCTP_ID_HASH_SIZE; r++)
  973. lock_destroy(&sctp_con_id_hash[r].lock);
  974. if (sctp_con_assoc_hash)
  975. for(r=0; r<SCTP_ASSOC_HASH_SIZE; r++)
  976. lock_destroy(&sctp_con_assoc_hash[r].lock);
  977. # ifdef SCTP_ADDR_HASH
  978. if (sctp_con_addr_hash)
  979. for(r=0; r<SCTP_ADDR_HASH_SIZE; r++)
  980. lock_destroy(&sctp_con_addr_hash[r].lock);
  981. # endif /* SCTP_ADDR_HASH */
  982. #elif defined SCTP_HASH_LOCK_SET
  983. if (sctp_con_id_h_lock_set){
  984. lock_set_destroy(sctp_con_id_h_lock_set);
  985. lock_set_dealloc(sctp_con_id_h_lock_set);
  986. sctp_con_id_h_lock_set=0;
  987. }
  988. if (sctp_con_assoc_h_lock_set){
  989. lock_set_destroy(sctp_con_assoc_h_lock_set);
  990. lock_set_dealloc(sctp_con_assoc_h_lock_set);
  991. sctp_con_assoc_h_lock_set=0;
  992. }
  993. # ifdef SCTP_ADDR_HASH
  994. if (sctp_con_addr_h_lock_set){
  995. lock_set_destroy(sctp_con_addr_h_lock_set);
  996. lock_set_dealloc(sctp_con_addr_h_lock_set);
  997. sctp_con_addr_h_lock_set=0;
  998. }
  999. # endif /* SCTP_ADDR_HASH */
  1000. #else /* SCTP_HASH_ONE_LOCK */
  1001. if (sctp_con_id_h_lock){
  1002. lock_destroy(sctp_con_id_h_lock);
  1003. lock_dealloc(sctp_con_id_h_lock);
  1004. sctp_con_id_h_lock=0;
  1005. }
  1006. if (sctp_con_assoc_h_lock){
  1007. lock_destroy(sctp_con_assoc_h_lock);
  1008. lock_dealloc(sctp_con_assoc_h_lock);
  1009. sctp_con_assoc_h_lock=0;
  1010. }
  1011. # ifdef SCTP_ADDR_HASH
  1012. if (sctp_con_addr_h_lock){
  1013. lock_destroy(sctp_con_addr_h_lock);
  1014. lock_dealloc(sctp_con_addr_h_lock);
  1015. sctp_con_addr_h_lock=0;
  1016. }
  1017. # endif /* SCTP_ADDR_HASH */
  1018. #endif /* SCTP_HASH_LOCK_PER_BUCKET/SCTP_HASH_LOCK_SET/one lock */
  1019. if (sctp_con_id_hash){
  1020. shm_free(sctp_con_id_hash);
  1021. sctp_con_id_hash=0;
  1022. }
  1023. if (sctp_con_assoc_hash){
  1024. shm_free(sctp_con_assoc_hash);
  1025. sctp_con_assoc_hash=0;
  1026. }
  1027. #ifdef SCTP_ADDR_HASH
  1028. if (sctp_con_addr_hash){
  1029. shm_free(sctp_con_addr_hash);
  1030. sctp_con_addr_hash=0;
  1031. }
  1032. #endif /* SCTP_ADDR_HASH */
  1033. if (sctp_id){
  1034. shm_free(sctp_id);
  1035. sctp_id=0;
  1036. }
  1037. if (sctp_conn_tracked){
  1038. shm_free(sctp_conn_tracked);
  1039. sctp_conn_tracked=0;
  1040. }
  1041. }
  1042. /** intializaze sctp_conn hashes.
  1043. * @return 0 on success, <0 on error
  1044. */
  1045. int init_sctp_con_tracking()
  1046. {
  1047. int r, ret;
  1048. sctp_con_id_hash=shm_malloc(SCTP_ID_HASH_SIZE*sizeof(*sctp_con_id_hash));
  1049. sctp_con_assoc_hash=shm_malloc(SCTP_ASSOC_HASH_SIZE*
  1050. sizeof(*sctp_con_assoc_hash));
  1051. #ifdef SCTP_ADDR_HASH
  1052. sctp_con_addr_hash=shm_malloc(SCTP_ADDR_HASH_SIZE*
  1053. sizeof(*sctp_con_addr_hash));
  1054. #endif /* SCTP_ADDR_HASH */
  1055. sctp_id=shm_malloc(sizeof(*sctp_id));
  1056. sctp_conn_tracked=shm_malloc(sizeof(*sctp_conn_tracked));
  1057. if (sctp_con_id_hash==0 || sctp_con_assoc_hash==0 ||
  1058. #ifdef SCTP_ADDR_HASH
  1059. sctp_con_addr_hash==0 ||
  1060. #endif /* SCTP_ADDR_HASH */
  1061. sctp_id==0 || sctp_conn_tracked==0){
  1062. ERR("sctp init: memory allocation error\n");
  1063. ret=E_OUT_OF_MEM;
  1064. goto error;
  1065. }
  1066. atomic_set(sctp_id, 0);
  1067. atomic_set(sctp_conn_tracked, 0);
  1068. for (r=0; r<SCTP_ID_HASH_SIZE; r++)
  1069. clist_init(&sctp_con_id_hash[r], l.next_id, l.prev_id);
  1070. for (r=0; r<SCTP_ASSOC_HASH_SIZE; r++)
  1071. clist_init(&sctp_con_assoc_hash[r], l.next_assoc, l.prev_assoc);
  1072. #ifdef SCTP_ADDR_HASH
  1073. for (r=0; r<SCTP_ADDR_HASH_SIZE; r++)
  1074. clist_init(&sctp_con_addr_hash[r], l.next_addr, l.prev_addr);
  1075. #endif /* SCTP_ADDR_HASH */
  1076. #ifdef SCTP_HASH_LOCK_PER_BUCKET
  1077. for (r=0; r<SCTP_ID_HASH_SIZE; r++){
  1078. if (lock_init(&sctp_con_id_hash[r].lock)==0){
  1079. ret=-1;
  1080. ERR("sctp init: failed to initialize locks\n");
  1081. goto error;
  1082. }
  1083. }
  1084. for (r=0; r<SCTP_ASSOC_HASH_SIZE; r++){
  1085. if (lock_init(&sctp_con_assoc_hash[r].lock)==0){
  1086. ret=-1;
  1087. ERR("sctp init: failed to initialize locks\n");
  1088. goto error;
  1089. }
  1090. }
  1091. # ifdef SCTP_ADDR_HASH
  1092. for (r=0; r<SCTP_ADDR_HASH_SIZE; r++){
  1093. if (lock_init(&sctp_con_addr_hash[r].lock)==0){
  1094. ret=-1;
  1095. ERR("sctp init: failed to initialize locks\n");
  1096. goto error;
  1097. }
  1098. }
  1099. # endif /* SCTP_ADDR_HASH */
  1100. #elif defined SCTP_HASH_LOCK_SET
  1101. sctp_con_id_h_lock_set=lock_set_alloc(SCTP_ID_HASH_SIZE);
  1102. sctp_con_assoc_h_lock_set=lock_set_alloc(SCTP_ASSOC_HASH_SIZE);
  1103. # ifdef SCTP_ADDR_HASH
  1104. sctp_con_addr_h_lock_set=lock_set_alloc(SCTP_ADDR_HASH_SIZE);
  1105. # endif /* SCTP_ADDR_HASH */
  1106. if (sctp_con_id_h_lock_set==0 || sctp_con_assoc_h_lock_set==0
  1107. # ifdef SCTP_ADDR_HASH
  1108. || sctp_con_addr_h_lock_set==0
  1109. # endif /* SCTP_ADDR_HASH */
  1110. ){
  1111. ret=E_OUT_OF_MEM;
  1112. ERR("sctp_init: failed to alloc lock sets\n");
  1113. goto error;
  1114. }
  1115. if (lock_set_init(sctp_con_id_h_lock_set)==0){
  1116. lock_set_dealloc(sctp_con_id_h_lock_set);
  1117. sctp_con_id_h_lock_set=0;
  1118. ret=-1;
  1119. ERR("sctp init: failed to initialize lock set\n");
  1120. goto error;
  1121. }
  1122. if (lock_set_init(sctp_con_assoc_h_lock_set)==0){
  1123. lock_set_dealloc(sctp_con_assoc_h_lock_set);
  1124. sctp_con_assoc_h_lock_set=0;
  1125. ret=-1;
  1126. ERR("sctp init: failed to initialize lock set\n");
  1127. goto error;
  1128. }
  1129. # ifdef SCTP_ADDR_HASH
  1130. if (lock_set_init(sctp_con_addr_h_lock_set)==0){
  1131. lock_set_dealloc(sctp_con_addr_h_lock_set);
  1132. sctp_con_addr_h_lock_set=0;
  1133. ret=-1;
  1134. ERR("sctp init: failed to initialize lock set\n");
  1135. goto error;
  1136. }
  1137. # endif /* SCTP_ADDR_HASH */
  1138. #else /* SCTP_HASH_ONE_LOCK */
  1139. sctp_con_id_h_lock=lock_alloc();
  1140. sctp_con_assoc_h_lock=lock_alloc();
  1141. # ifdef SCTP_ADDR_HASH
  1142. sctp_con_addr_h_lock=lock_alloc();
  1143. # endif /* SCTP_ADDR_HASH */
  1144. if (sctp_con_id_h_lock==0 || sctp_con_assoc_h_lock==0
  1145. # ifdef SCTP_ADDR_HASH
  1146. || sctp_con_addr_h_lock==0
  1147. # endif /* SCTP_ADDR_HASH */
  1148. ){
  1149. ret=E_OUT_OF_MEM;
  1150. ERR("sctp init: failed to alloc locks\n");
  1151. goto error;
  1152. }
  1153. if (lock_init(sctp_con_id_h_lock)==0){
  1154. lock_dealloc(sctp_con_id_h_lock);
  1155. sctp_con_id_h_lock=0;
  1156. ret=-1;
  1157. ERR("sctp init: failed to initialize lock\n");
  1158. goto error;
  1159. }
  1160. if (lock_init(sctp_con_assoc_h_lock)==0){
  1161. lock_dealloc(sctp_con_assoc_h_lock);
  1162. sctp_con_assoc_h_lock=0;
  1163. ret=-1;
  1164. ERR("sctp init: failed to initialize lock\n");
  1165. goto error;
  1166. }
  1167. # ifdef SCTP_ADDR_HASH
  1168. if (lock_init(sctp_con_addr_h_lock)==0){
  1169. lock_dealloc(sctp_con_addr_h_lock);
  1170. sctp_con_addr_h_lock=0;
  1171. ret=-1;
  1172. ERR("sctp init: failed to initialize lock\n");
  1173. goto error;
  1174. }
  1175. # endif /* SCTP_ADDR_HASH */
  1176. #endif /* SCTP_HASH_LOCK_PER_BUCKET/SCTP_HASH_LOCK_SET/one lock */
  1177. return 0;
  1178. error:
  1179. destroy_sctp_con_tracking();
  1180. return ret;
  1181. }
  1182. #if 0
  1183. /** adds "e" to the hashes, safe locking version.*/
  1184. static void sctp_con_add(struct sctp_con_elem* e)
  1185. {
  1186. unsigned hash;
  1187. DBG("sctp_con_add(%p) ( ser id %d, assoc_id %d)\n",
  1188. e, e->con.id, e->con.assoc_id);
  1189. e->l.next_id=e->l.prev_id=0;
  1190. e->l.next_assoc=e->l.prev_assoc=0;
  1191. #ifdef SCTP_ADDR_HASH
  1192. e->l.next_addr=e->l.prev_addr=0;
  1193. e->refcnt.val+=3; /* account for the 3 lists */
  1194. #else /* SCTP_ADDR_HASH */
  1195. e->refcnt.val+=2; /* account for the 2 lists */
  1196. #endif /* SCTP_ADDR_HASH */
  1197. hash=get_sctp_con_id_hash(e->con.id);
  1198. DBG("adding to con id hash %d\n", hash);
  1199. LOCK_SCTP_ID_H(hash);
  1200. clist_insert(&sctp_con_id_hash[hash], e, l.next_id, l.prev_id);
  1201. UNLOCK_SCTP_ID_H(hash);
  1202. hash=get_sctp_con_assoc_hash(e->con.assoc_id);
  1203. DBG("adding to assoc_id hash %d\n", hash);
  1204. LOCK_SCTP_ASSOC_H(hash);
  1205. clist_insert(&sctp_con_assoc_hash[hash], e,
  1206. l.next_assoc, l.prev_assoc);
  1207. UNLOCK_SCTP_ASSOC_H(hash);
  1208. #ifdef SCTP_ADDR_HASH
  1209. hash=get_sctp_con_addr_hash(&e->con.remote, e->con.si);
  1210. DBG("adding to addr hash %d\n", hash);
  1211. LOCK_SCTP_ADDR_H(hash);
  1212. clist_insert(&sctp_con_addr_hash[hash], e,
  1213. l.next_addr, l.prev_addr);
  1214. UNLOCK_SCTP_ADDR_H(hash);
  1215. #endif /* SCTP_ADDR_HASH */
  1216. atomic_inc(sctp_conn_tracked);
  1217. }
  1218. #endif
  1219. /** helper internal del elem function, the id hash must be locked.
  1220. * WARNING: the id hash(h) _must_ be locked (LOCK_SCTP_ID_H(h)).
  1221. * @param h - id hash
  1222. * @param e - sctp_con_elem to delete (from all the hashes)
  1223. * @return 0 if the id hash was unlocked, 1 if it's still locked */
  1224. inline static int _sctp_con_del_id_locked(unsigned h, struct sctp_con_elem* e)
  1225. {
  1226. unsigned assoc_id_h;
  1227. int deref; /* delayed de-reference counter */
  1228. int locked;
  1229. #ifdef SCTP_ADDR_HASH
  1230. unsigned addr_h;
  1231. #endif /* SCTP_ADDR_HASH */
  1232. locked=1;
  1233. clist_rm(e, l.next_id, l.prev_id);
  1234. e->l.next_id=e->l.prev_id=0; /* mark it as id unhashed */
  1235. /* delay atomic dereference, so that we'll perform only one
  1236. atomic op. even for multiple derefs. It also has the
  1237. nice side-effect that the entry will be guaranteed to be
  1238. referenced until we perform the delayed deref. at the end,
  1239. so we don't need to keep some lock to prevent somebody from
  1240. deleting the entry from under us */
  1241. deref=1; /* removed from one list => deref once */
  1242. /* remove it from the assoc hash if needed */
  1243. if (likely(e->l.next_assoc)){
  1244. UNLOCK_SCTP_ID_H(h);
  1245. locked=0; /* no longer id-locked */
  1246. /* we haven't dec. refcnt, so it's still safe to use e */
  1247. assoc_id_h=get_sctp_con_assoc_hash(e->con.assoc_id);
  1248. LOCK_SCTP_ASSOC_H(assoc_id_h);
  1249. /* make sure nobody removed it in the meantime */
  1250. if (likely(e->l.next_assoc)){
  1251. clist_rm(e, l.next_assoc, l.prev_assoc);
  1252. e->l.next_assoc=e->l.prev_assoc=0; /* mark it as removed */
  1253. deref++; /* rm'ed from the assoc list => inc. delayed deref. */
  1254. }
  1255. UNLOCK_SCTP_ASSOC_H(assoc_id_h);
  1256. }
  1257. #ifdef SCTP_ADDR_HASH
  1258. /* remove it from the addr. hash if needed */
  1259. if (likely(e->l.next_addr)){
  1260. if (unlikely(locked)){
  1261. UNLOCK_SCTP_ID_H(h);
  1262. locked=0; /* no longer id-locked */
  1263. }
  1264. addr_h=get_sctp_con_addr_hash(&e->con.remote, e->con.si);
  1265. LOCK_SCTP_ADDR_H(addr_h);
  1266. /* make sure nobody removed it in the meantime */
  1267. if (likely(e->l.next_addr)){
  1268. clist_rm(e, l.next_addr, l.prev_addr);
  1269. e->l.next_addr=e->l.prev_addr=0; /* mark it as removed */
  1270. deref++; /* rm'ed from the addr list => inc. delayed deref. */
  1271. }
  1272. UNLOCK_SCTP_ADDR_H(addr_h);
  1273. }
  1274. #endif /* SCTP_ADDR_HASH */
  1275. /* performed delayed de-reference */
  1276. if (atomic_add(&e->refcnt, -deref)==0){
  1277. atomic_dec(sctp_conn_tracked);
  1278. shm_free(e);
  1279. }
  1280. else
  1281. DBG("del assoc post-deref (kept): ser id %d, assoc_id %d,"
  1282. " post-refcnt %d, deref %d, post-tracked %d\n",
  1283. e->con.id, e->con.assoc_id, atomic_get(&e->refcnt), deref,
  1284. atomic_get(sctp_conn_tracked));
  1285. return locked;
  1286. }
  1287. /** helper internal del elem function, the assoc hash must be locked.
  1288. * WARNING: the assoc hash(h) _must_ be locked (LOCK_SCTP_ASSOC_H(h)).
  1289. * @param h - assoc hash
  1290. * @param e - sctp_con_elem to delete (from all the hashes)
  1291. * @return 0 if the assoc hash was unlocked, 1 if it's still locked */
  1292. inline static int _sctp_con_del_assoc_locked(unsigned h,
  1293. struct sctp_con_elem* e)
  1294. {
  1295. unsigned id_hash;
  1296. int deref; /* delayed de-reference counter */
  1297. int locked;
  1298. #ifdef SCTP_ADDR_HASH
  1299. unsigned addr_h;
  1300. #endif /* SCTP_ADDR_HASH */
  1301. locked=1;
  1302. clist_rm(e, l.next_assoc, l.prev_assoc);
  1303. e->l.next_assoc=e->l.prev_assoc=0; /* mark it as assoc unhashed */
  1304. /* delay atomic dereference, so that we'll perform only one
  1305. atomic op. even for multiple derefs. It also has the
  1306. nice side-effect that the entry will be guaranteed to be
  1307. referenced until we perform the delayed deref. at the end,
  1308. so we don't need to keep some lock to prevent somebody from
  1309. deleting the entry from under us */
  1310. deref=1; /* removed from one list => deref once */
  1311. /* remove it from the id hash if needed */
  1312. if (likely(e->l.next_id)){
  1313. UNLOCK_SCTP_ASSOC_H(h);
  1314. locked=0; /* no longer assoc-hash-locked */
  1315. /* we have a ref. to it so it's still safe to use e */
  1316. id_hash=get_sctp_con_id_hash(e->con.id);
  1317. LOCK_SCTP_ID_H(id_hash);
  1318. /* make sure nobody removed it in the meantime */
  1319. if (likely(e->l.next_id)){
  1320. clist_rm(e, l.next_id, l.prev_id);
  1321. e->l.next_id=e->l.prev_id=0; /* mark it as removed */
  1322. deref++; /* rm'ed from the id list => inc. delayed deref. */
  1323. }
  1324. UNLOCK_SCTP_ID_H(id_hash);
  1325. }
  1326. #ifdef SCTP_ADDR_HASH
  1327. /* remove it from the addr. hash if needed */
  1328. if (likely(e->l.next_addr)){
  1329. if (unlikely(locked)){
  1330. UNLOCK_SCTP_ASSOC_H(h);
  1331. locked=0; /* no longer id-locked */
  1332. }
  1333. addr_h=get_sctp_con_addr_hash(&e->con.remote, e->con.si);
  1334. LOCK_SCTP_ADDR_H(addr_h);
  1335. /* make sure nobody removed it in the meantime */
  1336. if (likely(e->l.next_addr)){
  1337. clist_rm(e, l.next_addr, l.prev_addr);
  1338. e->l.next_addr=e->l.prev_addr=0; /* mark it as removed */
  1339. deref++; /* rm'ed from the addr list => inc. delayed deref. */
  1340. }
  1341. UNLOCK_SCTP_ADDR_H(addr_h);
  1342. }
  1343. #endif /* SCTP_ADDR_HASH */
  1344. if (atomic_add(&e->refcnt, -deref)==0){
  1345. atomic_dec(sctp_conn_tracked);
  1346. shm_free(e);
  1347. }
  1348. else
  1349. DBG("del assoc post-deref (kept): ser id %d, assoc_id %d,"
  1350. " post-refcnt %d, deref %d, post-tracked %d\n",
  1351. e->con.id, e->con.assoc_id, atomic_get(&e->refcnt), deref,
  1352. atomic_get(sctp_conn_tracked));
  1353. return locked;
  1354. }
  1355. #ifdef SCTP_ADDR_HASH
  1356. /** helper internal del elem function, the addr hash must be locked.
  1357. * WARNING: the addr hash(h) _must_ be locked (LOCK_SCTP_ADDR_H(h)).
  1358. * @param h - addr hash
  1359. * @param e - sctp_con_elem to delete (from all the hashes)
  1360. * @return 0 if the addr hash was unlocked, 1 if it's still locked */
  1361. inline static int _sctp_con_del_addr_locked(unsigned h,
  1362. struct sctp_con_elem* e)
  1363. {
  1364. unsigned id_hash;
  1365. unsigned assoc_id_h;
  1366. int deref; /* delayed de-reference counter */
  1367. int locked;
  1368. locked=1;
  1369. clist_rm(e, l.next_addr, l.prev_addr);
  1370. e->l.next_addr=e->l.prev_addr=0; /* mark it as addr unhashed */
  1371. /* delay atomic dereference, so that we'll perform only one
  1372. atomic op. even for multiple derefs. It also has the
  1373. nice side-effect that the entry will be guaranteed to be
  1374. referenced until we perform the delayed deref. at the end,
  1375. so we don't need to keep some lock to prevent somebody from
  1376. deleting the entry from under us */
  1377. deref=1; /* removed from one list => deref once */
  1378. /* remove it from the id hash if needed */
  1379. if (likely(e->l.next_id)){
  1380. UNLOCK_SCTP_ADDR_H(h);
  1381. locked=0; /* no longer addr-hash-locked */
  1382. /* we have a ref. to it so it's still safe to use e */
  1383. id_hash=get_sctp_con_id_hash(e->con.id);
  1384. LOCK_SCTP_ID_H(id_hash);
  1385. /* make sure nobody removed it in the meantime */
  1386. if (likely(e->l.next_id)){
  1387. clist_rm(e, l.next_id, l.prev_id);
  1388. e->l.next_id=e->l.prev_id=0; /* mark it as removed */
  1389. deref++; /* rm'ed from the id list => inc. delayed deref. */
  1390. }
  1391. UNLOCK_SCTP_ID_H(id_hash);
  1392. }
  1393. /* remove it from the assoc hash if needed */
  1394. if (likely(e->l.next_assoc)){
  1395. if (locked){
  1396. UNLOCK_SCTP_ADDR_H(h);
  1397. locked=0; /* no longer addr-hash-locked */
  1398. }
  1399. /* we haven't dec. refcnt, so it's still safe to use e */
  1400. assoc_id_h=get_sctp_con_assoc_hash(e->con.assoc_id);
  1401. LOCK_SCTP_ASSOC_H(assoc_id_h);
  1402. /* make sure nobody removed it in the meantime */
  1403. if (likely(e->l.next_assoc)){
  1404. clist_rm(e, l.next_assoc, l.prev_assoc);
  1405. e->l.next_assoc=e->l.prev_assoc=0; /* mark it as removed */
  1406. deref++; /* rm'ed from the assoc list => inc. delayed deref. */
  1407. }
  1408. UNLOCK_SCTP_ASSOC_H(assoc_id_h);
  1409. }
  1410. if (atomic_add(&e->refcnt, -deref)==0){
  1411. atomic_dec(sctp_conn_tracked);
  1412. shm_free(e);
  1413. }
  1414. else
  1415. DBG("del assoc post-deref (kept): ser id %d, assoc_id %d,"
  1416. " post-refcnt %d, deref %d, post-tracked %d\n",
  1417. e->con.id, e->con.assoc_id, atomic_get(&e->refcnt), deref,
  1418. atomic_get(sctp_conn_tracked));
  1419. return locked;
  1420. }
  1421. #endif /* SCTP_ADDR_HASH */
  1422. /** delete all tracked associations entries.
  1423. */
  1424. void sctp_con_tracking_flush()
  1425. {
  1426. unsigned h;
  1427. struct sctp_con_elem* e;
  1428. struct sctp_con_elem* tmp;
  1429. for (h=0; h<SCTP_ID_HASH_SIZE; h++){
  1430. again:
  1431. LOCK_SCTP_ID_H(h);
  1432. clist_foreach_safe(&sctp_con_id_hash[h], e, tmp, l.next_id) {
  1433. if (_sctp_con_del_id_locked(h, e)==0){
  1434. /* unlocked, need to lock again and restart the list */
  1435. goto again;
  1436. }
  1437. }
  1438. UNLOCK_SCTP_ID_H(h);
  1439. }
  1440. }
  1441. /** using id, get the corresponding sctp assoc & socket.
  1442. * @param id - ser unique assoc id
  1443. * @param si - result parameter, filled with the socket info on success
  1444. * @param remote - result parameter, filled with the address and port
  1445. * @param del - if 1 delete the entry,
  1446. * @return assoc_id (!=0) on success & sets si, 0 on not found
  1447. * si and remote will not be touched on failure.
  1448. *
  1449. */
  1450. int sctp_con_get_assoc(unsigned int id, struct socket_info** si,
  1451. union sockaddr_union *remote, int del)
  1452. {
  1453. unsigned h;
  1454. ticks_t now;
  1455. struct sctp_con_elem* e;
  1456. struct sctp_con_elem* tmp;
  1457. int ret;
  1458. ret=0;
  1459. now=get_ticks_raw();
  1460. h=get_sctp_con_id_hash(id);
  1461. #if 0
  1462. again:
  1463. #endif
  1464. LOCK_SCTP_ID_H(h);
  1465. clist_foreach_safe(&sctp_con_id_hash[h], e, tmp, l.next_id){
  1466. if(e->con.id==id){
  1467. ret=e->con.assoc_id;
  1468. *si=e->con.si;
  1469. *remote=e->con.remote;
  1470. if (del){
  1471. if (_sctp_con_del_id_locked(h, e)==0)
  1472. goto skip_unlock;
  1473. }else
  1474. e->con.expire=now +
  1475. S_TO_TICKS(cfg_get(sctp, sctp_cfg, autoclose));
  1476. break;
  1477. }
  1478. #if 0
  1479. else if (TICKS_LT(e->con.expire, now)){
  1480. WARN("sctp con: found expired assoc %d, id %d (%d s ago)\n",
  1481. e->con.assoc_id, e->con.id,
  1482. TICKS_TO_S(now-e->con.expire));
  1483. if (_sctp_con_del_id_locked(h, e)==0)
  1484. goto again; /* if unlocked need to restart the list */
  1485. }
  1486. #endif
  1487. }
  1488. UNLOCK_SCTP_ID_H(h);
  1489. skip_unlock:
  1490. return ret;
  1491. }
  1492. /** using the assoc_id, remote addr. & socket, get the corresp. internal id.
  1493. * @param assoc_id - sctp assoc id
  1494. * @param si - socket on which the packet was received
  1495. * @param del - if 1 delete the entry,
  1496. * @return assoc_id (!=0) on success, 0 on not found
  1497. */
  1498. int sctp_con_get_id(unsigned int assoc_id, union sockaddr_union* remote,
  1499. struct socket_info* si, int del)
  1500. {
  1501. unsigned h;
  1502. ticks_t now;
  1503. struct sctp_con_elem* e;
  1504. struct sctp_con_elem* tmp;
  1505. int ret;
  1506. ret=0;
  1507. now=get_ticks_raw();
  1508. h=get_sctp_con_assoc_hash(assoc_id);
  1509. #if 0
  1510. again:
  1511. #endif
  1512. LOCK_SCTP_ASSOC_H(h);
  1513. clist_foreach_safe(&sctp_con_assoc_hash[h], e, tmp, l.next_assoc){
  1514. if(e->con.assoc_id==assoc_id && e->con.si==si &&
  1515. su_cmp(remote, &e->con.remote)){
  1516. ret=e->con.id;
  1517. if (del){
  1518. if (_sctp_con_del_assoc_locked(h, e)==0)
  1519. goto skip_unlock;
  1520. }else
  1521. e->con.expire=now +
  1522. S_TO_TICKS(cfg_get(sctp, sctp_cfg, autoclose));
  1523. break;
  1524. }
  1525. #if 0
  1526. else if (TICKS_LT(e->con.expire, now)){
  1527. WARN("sctp con: found expired assoc %d, id %d (%d s ago)\n",
  1528. e->con.assoc_id, e->con.id,
  1529. TICKS_TO_S(now-e->con.expire));
  1530. if (_sctp_con_del_assoc_locked(h, e)==0)
  1531. goto again; /* if unlocked need to restart the list */
  1532. }
  1533. #endif
  1534. }
  1535. UNLOCK_SCTP_ASSOC_H(h);
  1536. skip_unlock:
  1537. return ret;
  1538. }
  1539. #ifdef SCTP_ADDR_HASH
  1540. /** using the dest. & source socket, get the corresponding id and assoc_id
  1541. * @param remote - peer address & port
  1542. * @param si - local source socket
  1543. * @param assoc_id - result, filled with the sctp assoc_id
  1544. * @param del - if 1 delete the entry,
  1545. * @return ser id (!=0) on success, 0 on not found
  1546. */
  1547. int sctp_con_addr_get_id_assoc(union sockaddr_union* remote,
  1548. struct socket_info* si,
  1549. int* assoc_id, int del)
  1550. {
  1551. unsigned h;
  1552. ticks_t now;
  1553. struct sctp_con_elem* e;
  1554. struct sctp_con_elem* tmp;
  1555. int ret;
  1556. ret=0;
  1557. *assoc_id=0;
  1558. now=get_ticks_raw();
  1559. h=get_sctp_con_addr_hash(remote, si);
  1560. again:
  1561. LOCK_SCTP_ADDR_H(h);
  1562. clist_foreach_safe(&sctp_con_addr_hash[h], e, tmp, l.next_addr){
  1563. if(su_cmp(remote, &e->con.remote) && e->con.si==si){
  1564. ret=e->con.id;
  1565. *assoc_id=e->con.assoc_id;
  1566. if (del){
  1567. if (_sctp_con_del_addr_locked(h, e)==0)
  1568. goto skip_unlock;
  1569. }else
  1570. e->con.expire=now +
  1571. S_TO_TICKS(cfg_get(sctp, sctp_cfg, autoclose));
  1572. break;
  1573. }
  1574. #if 0
  1575. else if (TICKS_LT(e->con.expire, now)){
  1576. WARN("sctp con: found expired assoc %d, id %d (%d s ago)\n",
  1577. e->con.assoc_id, e->con.id,
  1578. TICKS_TO_S(now-e->con.expire));
  1579. if (_sctp_con_del_addr_locked(h, e)==0)
  1580. goto again; /* if unlocked need to restart the list */
  1581. }
  1582. #endif
  1583. }
  1584. UNLOCK_SCTP_ADDR_H(h);
  1585. skip_unlock:
  1586. return ret;
  1587. }
  1588. #endif /* SCTP_ADDR_HASH */
  1589. /** del con tracking for (assod_id, si).
  1590. * @return 0 on success, -1 on error (not found)
  1591. */
  1592. #define sctp_con_del_assoc(assoc_id, si) \
  1593. (-(sctp_con_get_id((assoc_id), (si), 1)==0))
  1594. /** create a new sctp con elem.
  1595. * @param id - ser connection id
  1596. * @param assoc_id - sctp assoc id
  1597. * @param si - corresp. socket
  1598. * @param remote - remote side
  1599. * @return pointer to shm allocated sctp_con_elem on success, 0 on error
  1600. */
  1601. struct sctp_con_elem* sctp_con_new(unsigned id, unsigned assoc_id,
  1602. struct socket_info* si,
  1603. union sockaddr_union* remote)
  1604. {
  1605. struct sctp_con_elem* e;
  1606. e=shm_malloc(sizeof(*e));
  1607. if (unlikely(e==0))
  1608. goto error;
  1609. e->l.next_id=e->l.prev_id=0;
  1610. e->l.next_assoc=e->l.prev_assoc=0;
  1611. atomic_set(&e->refcnt, 0);
  1612. e->con.id=id;
  1613. e->con.assoc_id=assoc_id;
  1614. e->con.si=si;
  1615. e->con.flags=0;
  1616. if (likely(remote))
  1617. e->con.remote=*remote;
  1618. else
  1619. memset(&e->con.remote, 0, sizeof(e->con.remote));
  1620. e->con.start=get_ticks_raw();
  1621. e->con.expire=e->con.start +
  1622. S_TO_TICKS(cfg_get(sctp, sctp_cfg, autoclose));
  1623. return e;
  1624. error:
  1625. return 0;
  1626. }
  1627. /** handles every ev on sctp assoc_id.
  1628. * @return ser id on success (!=0) or 0 on not found/error
  1629. */
  1630. static int sctp_con_track(int assoc_id, struct socket_info* si,
  1631. union sockaddr_union* remote, int ev)
  1632. {
  1633. int id;
  1634. unsigned hash;
  1635. unsigned assoc_hash;
  1636. struct sctp_con_elem* e;
  1637. struct sctp_con_elem* tmp;
  1638. id=0;
  1639. DBG("sctp_con_track(%d, %p, %d) \n", assoc_id, si, ev);
  1640. /* search for (assoc_id, si) */
  1641. assoc_hash=get_sctp_con_assoc_hash(assoc_id);
  1642. LOCK_SCTP_ASSOC_H(assoc_hash);
  1643. clist_foreach_safe(&sctp_con_assoc_hash[assoc_hash], e, tmp,
  1644. l.next_assoc){
  1645. /* we need to use the remote side address, because at least
  1646. on linux assoc_id are immediately reused (even if sctp
  1647. autoclose is off) and so it's possible that the association
  1648. id we saved is already closed and assigned to another
  1649. association by the time we search for it) */
  1650. if(e->con.assoc_id==assoc_id && e->con.si==si &&
  1651. su_cmp(remote, &e->con.remote)){
  1652. if (ev==SCTP_CON_DOWN_SEEN){
  1653. if (e->con.flags & SCTP_CON_UP_SEEN){
  1654. /* DOWN after UP => delete */
  1655. id=e->con.id;
  1656. /* do delete */
  1657. if (_sctp_con_del_assoc_locked(assoc_hash, e)==0)
  1658. goto found; /* skip unlock */
  1659. }else{
  1660. /* DOWN after DOWN => error
  1661. DOWN after RCV w/ no UP -> not possible
  1662. since we never create a tracking entry on RCV
  1663. only */
  1664. BUG("unexpected flags: %x for assoc_id %d, id %d"
  1665. ", sctp con %p\n", e->con.flags, assoc_id,
  1666. e->con.id, e);
  1667. /* do delete */
  1668. if (_sctp_con_del_assoc_locked(assoc_hash, e)==0)
  1669. goto found; /* skip unlock */
  1670. }
  1671. }else if (ev==SCTP_CON_RCV_SEEN){
  1672. /* RCV after UP or DOWN => just mark RCV as seen */
  1673. id=e->con.id;
  1674. e->con.flags |= SCTP_CON_RCV_SEEN;
  1675. }else{
  1676. /* SCTP_CON_UP */
  1677. if (e->con.flags & SCTP_CON_DOWN_SEEN){
  1678. /* UP after DOWN => delete */
  1679. id=e->con.id;
  1680. /* do delete */
  1681. if (_sctp_con_del_assoc_locked(assoc_hash, e)==0)
  1682. goto found; /* skip unlock */
  1683. }else{
  1684. /* UP after UP or after RCVD => BUG */
  1685. BUG("connection with same assoc_id (%d) already"
  1686. " present, flags %x\n",
  1687. assoc_id, e->con.flags);
  1688. }
  1689. }
  1690. UNLOCK_SCTP_ASSOC_H(assoc_hash);
  1691. goto found;
  1692. }
  1693. }
  1694. /* not found */
  1695. if (unlikely(ev!=SCTP_CON_RCV_SEEN)){
  1696. /* UP or DOWN and no tracking entry => create new tracking entry
  1697. for both of them (because we can have a re-ordered DOWN before
  1698. the UP) */
  1699. again:
  1700. id=atomic_add(sctp_id, 1);
  1701. if (unlikely(id==0)){
  1702. /* overflow and 0 is not a valid id */
  1703. goto again;
  1704. }
  1705. e=sctp_con_new(id, assoc_id, si, remote);
  1706. if (likely(e)){
  1707. e->con.flags=ev;
  1708. e->l.next_id=e->l.prev_id=0;
  1709. e->l.next_assoc=e->l.prev_assoc=0;
  1710. #ifdef SCTP_ADDR_HASH
  1711. e->l.next_addr=e->l.prev_addr=0;
  1712. e->refcnt.val+=3; /* account for the 3 lists */
  1713. #else /* SCTP_ADDR_HASH */
  1714. e->refcnt.val+=2; /* account for the 2 lists */
  1715. #endif /* SCTP_ADDR_HASH */
  1716. /* already locked */
  1717. clist_insert(&sctp_con_assoc_hash[assoc_hash], e,
  1718. l.next_assoc, l.prev_assoc);
  1719. hash=get_sctp_con_id_hash(e->con.id);
  1720. LOCK_SCTP_ID_H(hash);
  1721. clist_insert(&sctp_con_id_hash[hash], e,
  1722. l.next_id, l.prev_id);
  1723. UNLOCK_SCTP_ID_H(hash);
  1724. #ifdef SCTP_ADDR_HASH
  1725. hash=get_sctp_con_addr_hash(&e->con.remote, e->con.si);
  1726. LOCK_SCTP_ADDR_H(hash);
  1727. clist_insert(&sctp_con_addr_hash[hash], e,
  1728. l.next_addr, l.prev_addr);
  1729. UNLOCK_SCTP_ADDR_H(hash);
  1730. #endif /* SCTP_ADDR_HASH */
  1731. atomic_inc(sctp_conn_tracked);
  1732. }
  1733. } /* else not found and RCV -> ignore
  1734. We cannot create a new entry because we don't know when to
  1735. delete it (we can have UP DOWN RCV which would result in a
  1736. tracking entry living forever). This means that if we receive
  1737. a msg. on an assoc. before it's UP notification we won't know
  1738. the id for connection reuse, but since happens very rarely it's
  1739. an acceptable tradeoff */
  1740. UNLOCK_SCTP_ASSOC_H(assoc_hash);
  1741. if (unlikely(e==0)){
  1742. ERR("memory allocation failure\n");
  1743. goto error;
  1744. }
  1745. found:
  1746. return id;
  1747. error:
  1748. return 0;
  1749. }
  1750. #else /* SCTP_CONN_REUSE */
  1751. void sctp_con_tracking_flush() {}
  1752. #endif /* SCTP_CONN_REUSE */
  1753. int init_sctp()
  1754. {
  1755. int ret;
  1756. ret=0;
  1757. if (INIT_SCTP_STATS()!=0){
  1758. ERR("sctp init: failed to intialize sctp stats\n");
  1759. goto error;
  1760. }
  1761. /* sctp options must be initialized before calling this function */
  1762. sctp_conn_no=shm_malloc(sizeof(*sctp_conn_tracked));
  1763. if ( sctp_conn_no==0){
  1764. ERR("sctp init: memory allocation error\n");
  1765. ret=E_OUT_OF_MEM;
  1766. goto error;
  1767. }
  1768. atomic_set(sctp_conn_no, 0);
  1769. #ifdef SCTP_CONN_REUSE
  1770. return init_sctp_con_tracking();
  1771. #endif
  1772. error:
  1773. return ret;
  1774. }
  1775. void destroy_sctp()
  1776. {
  1777. if (sctp_conn_no){
  1778. shm_free(sctp_conn_no);
  1779. sctp_conn_no=0;
  1780. }
  1781. #ifdef SCTP_CONN_REUSE
  1782. destroy_sctp_con_tracking();
  1783. #endif
  1784. DESTROY_SCTP_STATS();
  1785. }
  1786. static int sctp_msg_send_ext(struct dest_info* dst, char* buf, unsigned len,
  1787. struct sctp_sndrcvinfo* sndrcv_info);
  1788. #define SCTP_SEND_FIRST_ASSOCID 1 /* sctp_raw_send flag */
  1789. static int sctp_raw_send(int socket, char* buf, unsigned len,
  1790. union sockaddr_union* to,
  1791. struct sctp_sndrcvinfo* sndrcv_info,
  1792. int flags);
  1793. /* debugging: return a string name for SCTP_ASSOC_CHANGE state */
  1794. static char* sctp_assoc_change_state2s(short int state)
  1795. {
  1796. char* s;
  1797. switch(state){
  1798. case SCTP_COMM_UP:
  1799. s="SCTP_COMM_UP";
  1800. break;
  1801. case SCTP_COMM_LOST:
  1802. s="SCTP_COMM_LOST";
  1803. break;
  1804. case SCTP_RESTART:
  1805. s="SCTP_RESTART";
  1806. break;
  1807. case SCTP_SHUTDOWN_COMP:
  1808. s="SCTP_SHUTDOWN_COMP";
  1809. break;
  1810. case SCTP_CANT_STR_ASSOC:
  1811. s="SCTP_CANT_STR_ASSOC";
  1812. break;
  1813. default:
  1814. s="UNKNOWN";
  1815. break;
  1816. };
  1817. return s;
  1818. }
  1819. /* debugging: return a string name for a SCTP_PEER_ADDR_CHANGE state */
  1820. static char* sctp_paddr_change_state2s(unsigned int state)
  1821. {
  1822. char* s;
  1823. switch (state){
  1824. case SCTP_ADDR_AVAILABLE:
  1825. s="SCTP_ADDR_AVAILABLE";
  1826. break;
  1827. case SCTP_ADDR_UNREACHABLE:
  1828. s="SCTP_ADDR_UNREACHABLE";
  1829. break;
  1830. case SCTP_ADDR_REMOVED:
  1831. s="SCTP_ADDR_REMOVED";
  1832. break;
  1833. case SCTP_ADDR_ADDED:
  1834. s="SCTP_ADDR_ADDED";
  1835. break;
  1836. case SCTP_ADDR_MADE_PRIM:
  1837. s="SCTP_ADDR_MADE_PRIM";
  1838. break;
  1839. /* not supported by lksctp 1.0.6
  1840. case SCTP_ADDR_CONFIRMED:
  1841. s="SCTP_ADDR_CONFIRMED";
  1842. break;
  1843. */
  1844. default:
  1845. s="UNKNOWN";
  1846. break;
  1847. }
  1848. return s;
  1849. }
  1850. /* handle SCTP_SEND_FAILED notifications: if packet marked for retries
  1851. * retry the send (with 0 assoc_id)
  1852. * returns 0 on success, -1 on failure
  1853. */
  1854. static int sctp_handle_send_failed(struct socket_info* si,
  1855. union sockaddr_union* su,
  1856. char* buf, unsigned len)
  1857. {
  1858. union sctp_notification* snp;
  1859. struct sctp_sndrcvinfo sinfo;
  1860. struct dest_info dst;
  1861. char* data;
  1862. unsigned data_len;
  1863. int retries;
  1864. int ret;
  1865. #ifdef HAVE_SCTP_SNDRCVINFO_PR_POLICY
  1866. int send_ttl;
  1867. #endif
  1868. ret=-1;
  1869. SCTP_STATS_SEND_FAILED();
  1870. snp=(union sctp_notification*) buf;
  1871. retries=snp->sn_send_failed.ssf_info.sinfo_context;
  1872. /* don't retry on explicit remote error
  1873. * (unfortunately we can't be more picky than this, we get no
  1874. * indication in the SEND_FAILED notification for other error
  1875. * reasons (e.g. ABORT received, INIT timeout a.s.o)
  1876. */
  1877. if (retries && (snp->sn_send_failed.ssf_error==0)) {
  1878. DBG("sctp: RETRY-ing (%d)\n", retries);
  1879. SCTP_STATS_SEND_FORCE_RETRY();
  1880. retries--;
  1881. data=(char*)snp->sn_send_failed.ssf_data;
  1882. data_len=snp->sn_send_failed.ssf_length -
  1883. sizeof(struct sctp_send_failed);
  1884. memset(&sinfo, 0, sizeof(sinfo));
  1885. sinfo.sinfo_flags=SCTP_UNORDERED;
  1886. #ifdef HAVE_SCTP_SNDRCVINFO_PR_POLICY
  1887. if ((send_ttl=cfg_get(sctp, sctp_cfg, send_ttl))){
  1888. sinfo.sinfo_pr_policy=SCTP_PR_SCTP_TTL;
  1889. sinfo.sinfo_pr_value=send_ttl;
  1890. }else
  1891. sinfo.info_pr_policy=SCTP_PR_SCTP_NONE;
  1892. #else
  1893. sinfo.sinfo_timetolive=cfg_get(sctp, sctp_cfg, send_ttl);
  1894. #endif
  1895. sinfo.sinfo_context=retries;
  1896. dst.to=*su;
  1897. dst.send_sock=si;
  1898. dst.id=0;
  1899. dst.proto=PROTO_SCTP;
  1900. #ifdef USE_COMP
  1901. dst.comp=COMP_NONE;
  1902. #endif
  1903. ret=sctp_msg_send_ext(&dst, data, data_len, &sinfo);
  1904. }
  1905. #ifdef USE_DST_BLACKLIST
  1906. else if (cfg_get(core, core_cfg, use_dst_blacklist) &&
  1907. cfg_get(sctp, sctp_cfg, send_retries)) {
  1908. /* blacklist only if send_retries is on, if off we blacklist
  1909. from SCTP_ASSOC_CHANGE: SCTP_COMM_LOST/SCTP_CANT_STR_ASSOC
  1910. which is better (because we can tell connect errors from send
  1911. errors and we blacklist a failed dst only once) */
  1912. dst_blacklist_su(BLST_ERR_SEND, PROTO_SCTP, su, 0);
  1913. }
  1914. #endif /* USE_DST_BLACKLIST */
  1915. return (ret>0)?0:ret;
  1916. }
  1917. /* handle SCTP_ASOC_CHANGE notifications: map ser global sctp ids
  1918. * to kernel asoc_ids. The global ids are needed because the kernel ones
  1919. * might get reused after a close and so they are not unique for ser's
  1920. * lifetime. We need a unique id to match replies to the association on
  1921. * which we received the corresponding request (so that we can send them
  1922. * back on the same asoc & socket if still opened).
  1923. * returns 0 on success, -1 on failure
  1924. */
  1925. static int sctp_handle_assoc_change(struct socket_info* si,
  1926. union sockaddr_union* su,
  1927. union sctp_notification* snp
  1928. )
  1929. {
  1930. int ret;
  1931. int state;
  1932. int assoc_id;
  1933. struct sctp_sndrcvinfo sinfo;
  1934. struct ip_addr ip; /* used only on error, for debugging */
  1935. state=snp->sn_assoc_change.sac_state;
  1936. assoc_id=snp->sn_assoc_change.sac_assoc_id;
  1937. ret=-1;
  1938. switch(state){
  1939. case SCTP_COMM_UP:
  1940. SCTP_STATS_ESTABLISHED();
  1941. atomic_inc(sctp_conn_no);
  1942. #ifdef SCTP_CONN_REUSE
  1943. /* new connection, track it */
  1944. if (likely(cfg_get(sctp, sctp_cfg, assoc_tracking)))
  1945. sctp_con_track(assoc_id, si, su, SCTP_CON_UP_SEEN);
  1946. #if 0
  1947. again:
  1948. id=atomic_add(sctp_id, 1);
  1949. if (unlikely(id==0)){
  1950. /* overflow and 0 is not a valid id */
  1951. goto again;
  1952. }
  1953. e=sctp_con_new(id, assoc_id, si, su);
  1954. if (unlikely(e==0)){
  1955. ERR("memory allocation failure\n");
  1956. }else{
  1957. sctp_con_add(e);
  1958. ret=0;
  1959. }
  1960. #endif
  1961. #endif /* SCTP_CONN_REUSE */
  1962. if (unlikely((unsigned)atomic_get(sctp_conn_no) >
  1963. (unsigned)cfg_get(sctp, sctp_cfg, max_assocs))){
  1964. /* maximum assoc exceeded => we'll have to immediately
  1965. close it */
  1966. memset(&sinfo, 0, sizeof(sinfo));
  1967. sinfo.sinfo_flags=SCTP_UNORDERED | SCTP_ABORT;
  1968. sinfo.sinfo_assoc_id=assoc_id;
  1969. ret=sctp_raw_send(si->socket, ABORT_REASON_MAX_ASSOCS,
  1970. sizeof(ABORT_REASON_MAX_ASSOCS)-1,
  1971. su, &sinfo, 0);
  1972. if (ret<0){
  1973. su2ip_addr(&ip, su);
  1974. WARN("failed to ABORT new sctp association %d (%s:%d):"
  1975. " %s (%d)\n", assoc_id, ip_addr2a(&ip),
  1976. su_getport(su), strerror(errno), errno);
  1977. }else{
  1978. SCTP_STATS_LOCAL_REJECT();
  1979. }
  1980. }
  1981. break;
  1982. case SCTP_COMM_LOST:
  1983. SCTP_STATS_COMM_LOST();
  1984. #ifdef USE_DST_BLACKLIST
  1985. /* blacklist only if send_retries is turned off (if on we don't
  1986. know here if we did retry or we are at the first error) */
  1987. if (cfg_get(core, core_cfg, use_dst_blacklist) &&
  1988. (cfg_get(sctp, sctp_cfg, send_retries)==0))
  1989. dst_blacklist_su(BLST_ERR_SEND, PROTO_SCTP, su, 0);
  1990. #endif /* USE_DST_BLACKLIST */
  1991. /* no break */
  1992. case SCTP_SHUTDOWN_COMP:
  1993. atomic_dec(sctp_conn_no);
  1994. #ifdef SCTP_CONN_REUSE
  1995. /* connection down*/
  1996. if (likely(cfg_get(sctp, sctp_cfg, assoc_tracking)))
  1997. sctp_con_track(assoc_id, si, su, SCTP_CON_DOWN_SEEN);
  1998. #if 0
  1999. if (unlikely(sctp_con_del_assoc(assoc_id, si)!=0))
  2000. WARN("sctp con: tried to remove inexistent connection\n");
  2001. else
  2002. ret=0;
  2003. #endif
  2004. #endif /* SCTP_CONN_REUSE */
  2005. break;
  2006. case SCTP_RESTART:
  2007. /* do nothing on restart */
  2008. break;
  2009. case SCTP_CANT_STR_ASSOC:
  2010. SCTP_STATS_CONNECT_FAILED();
  2011. /* do nothing when failing to start an assoc
  2012. (in this case we never see SCTP_COMM_UP so we never
  2013. track the assoc) */
  2014. #ifdef USE_DST_BLACKLIST
  2015. /* blacklist only if send_retries is turned off (if on we don't
  2016. know here if we did retry or we are at the first error) */
  2017. if (cfg_get(core, core_cfg, use_dst_blacklist) &&
  2018. (cfg_get(sctp, sctp_cfg, send_retries)==0))
  2019. dst_blacklist_su(BLST_ERR_CONNECT, PROTO_SCTP, su, 0);
  2020. #endif /* USE_DST_BLACKLIST */
  2021. break;
  2022. default:
  2023. break;
  2024. }
  2025. return ret;
  2026. }
  2027. static int sctp_handle_notification(struct socket_info* si,
  2028. union sockaddr_union* su,
  2029. char* buf, unsigned len)
  2030. {
  2031. union sctp_notification* snp;
  2032. char su_buf[SU2A_MAX_STR_SIZE];
  2033. #define SNOT DBG
  2034. #define ERR_LEN_TOO_SMALL(length, val, bind_addr, from_su, text) \
  2035. if (unlikely((length)<(val))){\
  2036. SNOT("ERROR: sctp notification from %s on %.*s:%d: " \
  2037. text " too short (%d bytes instead of %d bytes)\n", \
  2038. su2a((from_su), sizeof(*(from_su))), \
  2039. (bind_addr)->name.len, (bind_addr)->name.s, \
  2040. (bind_addr)->port_no, (int)(length), (int)(val)); \
  2041. goto error; \
  2042. }
  2043. if (len < sizeof(snp->sn_header)){
  2044. LOG(L_ERR, "ERROR: sctp_handle_notification: invalid length %d "
  2045. "on %.*s:%d, from %s\n",
  2046. len, si->name.len, si->name.s, si->port_no,
  2047. su2a(su, sizeof(*su)));
  2048. goto error;
  2049. }
  2050. snp=(union sctp_notification*) buf;
  2051. switch(snp->sn_header.sn_type){
  2052. case SCTP_REMOTE_ERROR:
  2053. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_remote_error), si, su,
  2054. "SCTP_REMOTE_ERROR");
  2055. SCTP_EV_REMOTE_ERROR(&si->address, si->port_no, su,
  2056. ntohs(snp->sn_remote_error.sre_error) );
  2057. SNOT("sctp notification from %s on %.*s:%d: SCTP_REMOTE_ERROR:"
  2058. " %d, len %d\n, assoc_id %d",
  2059. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2060. si->port_no,
  2061. ntohs(snp->sn_remote_error.sre_error),
  2062. ntohs(snp->sn_remote_error.sre_length),
  2063. snp->sn_remote_error.sre_assoc_id
  2064. );
  2065. break;
  2066. case SCTP_SEND_FAILED:
  2067. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_send_failed), si, su,
  2068. "SCTP_SEND_FAILED");
  2069. SCTP_EV_SEND_FAILED(&si->address, si->port_no, su,
  2070. snp->sn_send_failed.ssf_error);
  2071. SNOT("sctp notification from %s on %.*s:%d: SCTP_SEND_FAILED:"
  2072. " error %d, assoc_id %d, flags %x\n",
  2073. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2074. si->port_no, snp->sn_send_failed.ssf_error,
  2075. snp->sn_send_failed.ssf_assoc_id,
  2076. snp->sn_send_failed.ssf_flags);
  2077. sctp_handle_send_failed(si, su, buf, len);
  2078. break;
  2079. case SCTP_PEER_ADDR_CHANGE:
  2080. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_paddr_change), si, su,
  2081. "SCTP_PEER_ADDR_CHANGE");
  2082. SCTP_EV_PEER_ADDR_CHANGE(&si->address, si->port_no, su,
  2083. sctp_paddr_change_state2s(snp->sn_paddr_change.spc_state),
  2084. snp->sn_paddr_change.spc_state,
  2085. &snp->sn_paddr_change.spc_aaddr);
  2086. strcpy(su_buf, su2a((union sockaddr_union*)
  2087. &snp->sn_paddr_change.spc_aaddr,
  2088. sizeof(snp->sn_paddr_change.spc_aaddr)));
  2089. SNOT("sctp notification from %s on %.*s:%d: SCTP_PEER_ADDR_CHANGE"
  2090. ": %s: %s: assoc_id %d \n",
  2091. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2092. si->port_no, su_buf,
  2093. sctp_paddr_change_state2s(snp->sn_paddr_change.spc_state),
  2094. snp->sn_paddr_change.spc_assoc_id
  2095. );
  2096. break;
  2097. case SCTP_SHUTDOWN_EVENT:
  2098. SCTP_STATS_REMOTE_SHUTDOWN();
  2099. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_shutdown_event), si, su,
  2100. "SCTP_SHUTDOWN_EVENT");
  2101. SCTP_EV_SHUTDOWN_EVENT(&si->address, si->port_no, su);
  2102. SNOT("sctp notification from %s on %.*s:%d: SCTP_SHUTDOWN_EVENT:"
  2103. " assoc_id %d\n",
  2104. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2105. si->port_no, snp->sn_shutdown_event.sse_assoc_id);
  2106. break;
  2107. case SCTP_ASSOC_CHANGE:
  2108. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_assoc_change), si, su,
  2109. "SCTP_ASSOC_CHANGE");
  2110. SCTP_EV_ASSOC_CHANGE(&si->address, si->port_no, su,
  2111. sctp_assoc_change_state2s(snp->sn_assoc_change.sac_state),
  2112. snp->sn_assoc_change.sac_state);
  2113. SNOT("sctp notification from %s on %.*s:%d: SCTP_ASSOC_CHANGE"
  2114. ": %s: assoc_id %d, ostreams %d, istreams %d\n",
  2115. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2116. si->port_no,
  2117. sctp_assoc_change_state2s(snp->sn_assoc_change.sac_state),
  2118. snp->sn_assoc_change.sac_assoc_id,
  2119. snp->sn_assoc_change.sac_outbound_streams,
  2120. snp->sn_assoc_change.sac_inbound_streams
  2121. );
  2122. sctp_handle_assoc_change(si, su, snp);
  2123. break;
  2124. #ifdef SCTP_ADAPTION_INDICATION
  2125. case SCTP_ADAPTION_INDICATION:
  2126. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_adaption_event), si, su,
  2127. "SCTP_ADAPTION_INDICATION");
  2128. SNOT("sctp notification from %s on %.*s:%d: "
  2129. "SCTP_ADAPTION_INDICATION \n",
  2130. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2131. si->port_no);
  2132. break;
  2133. #endif /* SCTP_ADAPTION_INDICATION */
  2134. case SCTP_PARTIAL_DELIVERY_EVENT:
  2135. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_pdapi_event), si, su,
  2136. "SCTP_PARTIAL_DELIVERY_EVENT");
  2137. ERR("sctp notification from %s on %.*s:%d: "
  2138. "SCTP_PARTIAL_DELIVERY_EVENT not supported: %d %s,"
  2139. "assoc_id %d\n",
  2140. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2141. si->port_no, snp->sn_pdapi_event.pdapi_indication,
  2142. (snp->sn_pdapi_event.pdapi_indication==
  2143. SCTP_PARTIAL_DELIVERY_ABORTED)? " PD ABORTED":"",
  2144. snp->sn_pdapi_event.pdapi_assoc_id);
  2145. break;
  2146. #ifdef SCTP_SENDER_DRY_EVENT /* new, not yet supported */
  2147. case SCTP_SENDER_DRY_EVENT:
  2148. ERR_LEN_TOO_SMALL(len, sizeof(struct sctp_sender_dry_event),
  2149. si, su, "SCTP_SENDER_DRY_EVENT");
  2150. SCTP_EV_REMOTE_ERROR(&si->address, si->port_no, su);
  2151. SNOT("sctp notification from %s on %.*s:%d: "
  2152. "SCTP_SENDER_DRY_EVENT on %d\n",
  2153. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2154. si->port_no, snp->sn_sender_dry_event.sender_dry_assoc_id);
  2155. break;
  2156. #endif /* SCTP_SENDER_DRY_EVENT */
  2157. default:
  2158. SNOT("sctp notification from %s on %.*s:%d: UNKNOWN (%d)\n",
  2159. su2a(su, sizeof(*su)), si->name.len, si->name.s,
  2160. si->port_no, snp->sn_header.sn_type);
  2161. }
  2162. return 0;
  2163. error:
  2164. return -1;
  2165. #undef ERR_LEN_TOO_SMALL
  2166. }
  2167. int sctp_rcv_loop()
  2168. {
  2169. unsigned len;
  2170. static char buf [BUF_SIZE+1];
  2171. char *tmp;
  2172. struct receive_info ri;
  2173. struct sctp_sndrcvinfo* sinfo;
  2174. struct msghdr msg;
  2175. struct iovec iov[1];
  2176. struct cmsghdr* cmsg;
  2177. /* use a larger buffer then needed in case some other ancillary info
  2178. * is enabled */
  2179. char cbuf[CMSG_SPACE(sizeof(*sinfo))+CMSG_SPACE(1024)];
  2180. ri.bind_address=bind_address; /* this will not change */
  2181. ri.dst_port=bind_address->port_no;
  2182. ri.dst_ip=bind_address->address;
  2183. ri.proto=PROTO_SCTP;
  2184. ri.proto_reserved2=0;
  2185. iov[0].iov_base=buf;
  2186. iov[0].iov_len=BUF_SIZE;
  2187. msg.msg_iov=iov;
  2188. msg.msg_iovlen=1;
  2189. msg.msg_flags=0;
  2190. /* initialize the config framework */
  2191. if (cfg_child_init()) goto error;
  2192. for(;;){
  2193. msg.msg_name=&ri.src_su.s;
  2194. msg.msg_namelen=sockaddru_len(bind_address->su);
  2195. msg.msg_control=cbuf;
  2196. msg.msg_controllen=sizeof(cbuf);
  2197. sinfo=0;
  2198. len=recvmsg(bind_address->socket, &msg, 0);
  2199. if (len==-1){
  2200. if (errno==EAGAIN){
  2201. DBG("sctp_rcv_loop: EAGAIN on sctp socket\n");
  2202. continue;
  2203. }
  2204. LOG(L_ERR, "ERROR: sctp_rcv_loop: sctp_recvmsg on %d (%p):"
  2205. "[%d] %s\n", bind_address->socket, bind_address,
  2206. errno, strerror(errno));
  2207. if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED))
  2208. continue; /* goto skip;*/
  2209. else goto error;
  2210. }
  2211. if (unlikely(msg.msg_flags & MSG_NOTIFICATION)){
  2212. /* intercept useful notifications */
  2213. sctp_handle_notification(bind_address, &ri.src_su, buf, len);
  2214. continue;
  2215. }else if (unlikely(!(msg.msg_flags & MSG_EOR))){
  2216. LOG(L_ERR, "ERROR: sctp_rcv_loop: partial delivery not"
  2217. "supported\n");
  2218. continue;
  2219. }
  2220. su2ip_addr(&ri.src_ip, &ri.src_su);
  2221. ri.src_port=su_getport(&ri.src_su);
  2222. /* get ancillary data */
  2223. for (cmsg=CMSG_FIRSTHDR(&msg); cmsg; cmsg=CMSG_NXTHDR(&msg, cmsg)){
  2224. if (likely((cmsg->cmsg_level==IPPROTO_SCTP) &&
  2225. ((cmsg->cmsg_type==SCTP_SNDRCV)
  2226. #ifdef SCTP_EXT
  2227. || (cmsg->cmsg_type==SCTP_EXTRCV)
  2228. #endif
  2229. ) && (cmsg->cmsg_len>=CMSG_LEN(sizeof(*sinfo)))) ){
  2230. sinfo=(struct sctp_sndrcvinfo*)CMSG_DATA(cmsg);
  2231. DBG("sctp recv: message from %s:%d stream %d ppid %x"
  2232. " flags %x%s tsn %u" " cumtsn %u assoc_id %d\n",
  2233. ip_addr2a(&ri.src_ip), ri.src_port,
  2234. sinfo->sinfo_stream, sinfo->sinfo_ppid,
  2235. sinfo->sinfo_flags,
  2236. (sinfo->sinfo_flags&SCTP_UNORDERED)?
  2237. " (SCTP_UNORDERED)":"",
  2238. sinfo->sinfo_tsn, sinfo->sinfo_cumtsn,
  2239. sinfo->sinfo_assoc_id);
  2240. break;
  2241. }
  2242. }
  2243. /* we 0-term the messages for debugging */
  2244. buf[len]=0; /* no need to save the previous char */
  2245. /* sanity checks */
  2246. if (len<MIN_SCTP_PACKET) {
  2247. tmp=ip_addr2a(&ri.src_ip);
  2248. DBG("sctp_rcv_loop: probing packet received from %s:%d\n",
  2249. tmp, ri.src_port);
  2250. continue;
  2251. }
  2252. if (ri.src_port==0){
  2253. tmp=ip_addr2a(&ri.src_ip);
  2254. LOG(L_INFO, "sctp_rcv_loop: dropping 0 port packet from %s:0\n",
  2255. tmp);
  2256. continue;
  2257. }
  2258. #ifdef USE_COMP
  2259. ri.comp=COMP_NONE;
  2260. #endif
  2261. /* update the local config */
  2262. cfg_update();
  2263. #ifdef SCTP_CONN_REUSE
  2264. if (likely(cfg_get(sctp, sctp_cfg, assoc_tracking) && sinfo)){
  2265. ri.proto_reserved1 = sctp_con_track(sinfo->sinfo_assoc_id,
  2266. ri.bind_address,
  2267. &ri.src_su,
  2268. SCTP_CON_RCV_SEEN);
  2269. /* debugging */
  2270. if (unlikely(ri.proto_reserved1==0))
  2271. DBG("no tracked assoc. found for assoc_id %d, from %s\n",
  2272. sinfo->sinfo_assoc_id,
  2273. su2a(&ri.src_su, sizeof(ri.src_su)));
  2274. #if 0
  2275. ri.proto_reserved1=
  2276. sctp_con_get_id(sinfo->sinfo_assoc_id, ri.bind_address, 0);
  2277. #endif
  2278. }else
  2279. ri.proto_reserved1=0;
  2280. #else /* SCTP_CONN_REUSE */
  2281. ri.proto_received1=0;
  2282. #endif /* SCTP_CONN_REUSE */
  2283. receive_msg(buf, len, &ri);
  2284. }
  2285. error:
  2286. return -1;
  2287. }
  2288. /** low level sctp non-blocking send.
  2289. * @param socket - sctp socket to send on.
  2290. * @param buf - data.
  2291. * @param len - lenght of the data.
  2292. * @param to - destination in ser sockaddr_union format.
  2293. * @param sndrcv_info - sctp_sndrcvinfo structure pointer, pre-filled.
  2294. * @param flags - can have one of the following values (or'ed):
  2295. * SCTP_SEND_FIRST_ASSOCID - try to send first to assoc_id
  2296. * and only if that fails use "to".
  2297. * @return the numbers of bytes sent on success (>=0) and -1 on error.
  2298. * On error errno is set too.
  2299. */
  2300. static int sctp_raw_send(int socket, char* buf, unsigned len,
  2301. union sockaddr_union* to,
  2302. struct sctp_sndrcvinfo* sndrcv_info,
  2303. int flags)
  2304. {
  2305. int n;
  2306. int tolen;
  2307. int try_assoc_id;
  2308. #if 0
  2309. struct ip_addr ip; /* used only on error, for debugging */
  2310. #endif
  2311. struct msghdr msg;
  2312. struct iovec iov[1];
  2313. struct sctp_sndrcvinfo* sinfo;
  2314. struct cmsghdr* cmsg;
  2315. /* make sure msg_control will point to properly aligned data */
  2316. union {
  2317. struct cmsghdr cm;
  2318. char cbuf[CMSG_SPACE(sizeof(*sinfo))];
  2319. }ctrl_un;
  2320. iov[0].iov_base=buf;
  2321. iov[0].iov_len=len;
  2322. msg.msg_iov=iov;
  2323. msg.msg_iovlen=1;
  2324. msg.msg_flags=0; /* not used on send (use instead sinfo_flags) */
  2325. msg.msg_control=ctrl_un.cbuf;
  2326. msg.msg_controllen=sizeof(ctrl_un.cbuf);
  2327. cmsg=CMSG_FIRSTHDR(&msg);
  2328. cmsg->cmsg_level=IPPROTO_SCTP;
  2329. cmsg->cmsg_type=SCTP_SNDRCV;
  2330. cmsg->cmsg_len=CMSG_LEN(sizeof(*sinfo));
  2331. sinfo=(struct sctp_sndrcvinfo*)CMSG_DATA(cmsg);
  2332. *sinfo=*sndrcv_info;
  2333. /* some systems need msg_controllen set to the actual size and not
  2334. * something bigger (e.g. openbsd) */
  2335. msg.msg_controllen=cmsg->cmsg_len;
  2336. try_assoc_id= ((flags & SCTP_SEND_FIRST_ASSOCID) && sinfo->sinfo_assoc_id);
  2337. /* if assoc_id is set it means we want to send on association assoc_id
  2338. and only if it's not opened any longer use the addresses */
  2339. if (try_assoc_id){
  2340. /* on linux msg->name has priority over assoc_id. To try first assoc_id
  2341. * and then "to", one has to call first sendmsg() with msg->name==0 and
  2342. * sinfo->assoc_id set. If it returns EPIPE => association is no longer
  2343. * open => call again sendmsg() this time with msg->name!=0.
  2344. * on freebsd assoc_id has priority over msg->name and moreover the
  2345. * send falls back automatically to the address if the assoc_id is
  2346. * closed, so a single call to sendmsg(msg->name, sinfo->assoc_id ) is
  2347. * enough. If one tries calling with msg->name==0 and the association
  2348. * is no longer open send will return ENOENT.
  2349. * on solaris it seems one must always use a dst address (assoc_id
  2350. * will be ignored).
  2351. */
  2352. #ifdef __OS_linux
  2353. msg.msg_name=0;
  2354. msg.msg_namelen=0;
  2355. #elif defined __OS_freebsd
  2356. tolen=sockaddru_len(*to);
  2357. msg.msg_name=&to->s;
  2358. msg.msg_namelen=tolen;
  2359. #else /* __OS_* */
  2360. /* fallback for solaris and others, sent back to
  2361. the address recorded (not exactly what we want, but there's
  2362. no way to fallback to "to") */
  2363. tolen=sockaddru_len(*to);
  2364. msg.msg_name=&to->s;
  2365. msg.msg_namelen=tolen;
  2366. #endif /* __OS_* */
  2367. }else{
  2368. tolen=sockaddru_len(*to);
  2369. msg.msg_name=&to->s;
  2370. msg.msg_namelen=tolen;
  2371. }
  2372. again:
  2373. n=sendmsg(socket, &msg, MSG_DONTWAIT);
  2374. if (n==-1){
  2375. #ifdef __OS_linux
  2376. if ((errno==EPIPE) && try_assoc_id){
  2377. /* try again, this time with null assoc_id and non-null msg.name */
  2378. DBG("sctp raw sendmsg: assoc already closed (EPIPE), retrying with"
  2379. " assoc_id=0\n");
  2380. tolen=sockaddru_len(*to);
  2381. msg.msg_name=&to->s;
  2382. msg.msg_namelen=tolen;
  2383. sinfo->sinfo_assoc_id=0;
  2384. try_assoc_id=0;
  2385. goto again;
  2386. }
  2387. #elif defined __OS_freebsd
  2388. if ((errno==ENOENT)){
  2389. /* it didn't work, no retrying */
  2390. WARN("unexpected sendmsg() failure (ENOENT),"
  2391. " assoc_id %d\n", sinfo->sinfo_assoc_id);
  2392. }
  2393. #else /* __OS_* */
  2394. if ((errno==ENOENT || errno==EPIPE) && try_assoc_id){
  2395. /* in case the sctp stack prioritises assoc_id over msg->name,
  2396. try again with 0 assoc_id and msg->name set to "to" */
  2397. WARN("unexpected ENOENT or EPIPE (assoc_id %d),"
  2398. "trying automatic recovery... (please report along with"
  2399. "your OS version)\n", sinfo->sinfo_assoc_id);
  2400. tolen=sockaddru_len(*to);
  2401. msg.msg_name=&to->s;
  2402. msg.msg_namelen=tolen;
  2403. sinfo->sinfo_assoc_id=0;
  2404. try_assoc_id=0;
  2405. goto again;
  2406. }
  2407. #endif /* __OS_* */
  2408. #if 0
  2409. if (errno==EINTR) goto again;
  2410. su2ip_addr(&ip, to);
  2411. LOG(L_ERR, "ERROR: sctp_raw_send: sendmsg(sock,%p,%d,0,%s:%d,...):"
  2412. " %s(%d)\n", buf, len, ip_addr2a(&ip), su_getport(to),
  2413. strerror(errno), errno);
  2414. if (errno==EINVAL) {
  2415. LOG(L_CRIT,"CRITICAL: invalid sendmsg parameters\n"
  2416. "one possible reason is the server is bound to localhost and\n"
  2417. "attempts to send to the net\n");
  2418. }else if (errno==EAGAIN || errno==EWOULDBLOCK){
  2419. SCTP_STATS_SENDQ_FULL();
  2420. LOG(L_ERR, "ERROR: sctp_msg_send: failed to send, send buffers"
  2421. " full\n");
  2422. }
  2423. #endif
  2424. }
  2425. return n;
  2426. }
  2427. /* send buf:len over sctp to dst using sndrcv_info (uses send_sock,
  2428. * to and id from dest_info)
  2429. * returns the numbers of bytes sent on success (>=0) and -1 on error
  2430. */
  2431. static int sctp_msg_send_ext(struct dest_info* dst, char* buf, unsigned len,
  2432. struct sctp_sndrcvinfo* sndrcv_info)
  2433. {
  2434. int n;
  2435. int tolen;
  2436. struct ip_addr ip; /* used only on error, for debugging */
  2437. struct msghdr msg;
  2438. struct iovec iov[1];
  2439. struct socket_info* si;
  2440. struct sctp_sndrcvinfo* sinfo;
  2441. struct cmsghdr* cmsg;
  2442. /* make sure msg_control will point to properly aligned data */
  2443. union {
  2444. struct cmsghdr cm;
  2445. char cbuf[CMSG_SPACE(sizeof(*sinfo))];
  2446. }ctrl_un;
  2447. #ifdef SCTP_CONN_REUSE
  2448. int assoc_id;
  2449. union sockaddr_union to;
  2450. #ifdef SCTP_ADDR_HASH
  2451. int tmp_id, tmp_assoc_id;
  2452. #endif /* SCTP_ADDR_HASH */
  2453. #endif /* SCTP_CONN_REUSE */
  2454. iov[0].iov_base=buf;
  2455. iov[0].iov_len=len;
  2456. msg.msg_iov=iov;
  2457. msg.msg_iovlen=1;
  2458. msg.msg_flags=0; /* not used on send (use instead sinfo_flags) */
  2459. msg.msg_control=ctrl_un.cbuf;
  2460. msg.msg_controllen=sizeof(ctrl_un.cbuf);
  2461. cmsg=CMSG_FIRSTHDR(&msg);
  2462. cmsg->cmsg_level=IPPROTO_SCTP;
  2463. cmsg->cmsg_type=SCTP_SNDRCV;
  2464. cmsg->cmsg_len=CMSG_LEN(sizeof(*sinfo));
  2465. sinfo=(struct sctp_sndrcvinfo*)CMSG_DATA(cmsg);
  2466. *sinfo=*sndrcv_info;
  2467. /* some systems need msg_controllen set to the actual size and not
  2468. * something bigger (e.g. openbsd) */
  2469. msg.msg_controllen=cmsg->cmsg_len;
  2470. si=dst->send_sock;
  2471. #ifdef SCTP_CONN_REUSE
  2472. /* if dst->id is set it means we want to send on association with
  2473. ser id dst->id if still opened and only if closed use dst->to */
  2474. assoc_id=0;
  2475. if ((dst->id) && cfg_get(sctp, sctp_cfg, assoc_reuse) &&
  2476. cfg_get(sctp, sctp_cfg, assoc_tracking) &&
  2477. (assoc_id=sctp_con_get_assoc(dst->id, &si, &to, 0))){
  2478. DBG("sctp: sending on sctp assoc_id %d (ser id %d)\n",
  2479. assoc_id, dst->id);
  2480. sinfo->sinfo_assoc_id=assoc_id;
  2481. /* on linux msg->name has priority over assoc_id. To try first assoc_id
  2482. * and then dst, one has to call first sendmsg() with msg->name==0 and
  2483. * sinfo->assoc_id set. If it returns EPIPE => association is no longer
  2484. * open => call again sendmsg() this time with msg->name!=0.
  2485. * on freebsd assoc_id has priority over msg->name and moreover the
  2486. * send falls back automatically to the address if the assoc_id is
  2487. * closed, so a single call to sendmsg(msg->name, sinfo->assoc_id ) is
  2488. * enough. If one tries calling with msg->name==0 and the association
  2489. * is no longer open send will return ENOENT.
  2490. * on solaris it seems one must always use a dst address (assoc_id
  2491. * will be ignored).
  2492. */
  2493. #ifdef __OS_linux
  2494. DBG("sctp: linux: trying with 0 msg_name\n");
  2495. msg.msg_name=0;
  2496. msg.msg_namelen=0;
  2497. #elif defined __OS_freebsd
  2498. tolen=sockaddru_len(dst->to);
  2499. msg.msg_name=&dst->to.s;
  2500. msg.msg_namelen=tolen;
  2501. #else /* __OS_* */
  2502. /* fallback for solaris and others, sent back to
  2503. the address recorded (not exactly what we want, but there's
  2504. no way to fallback to dst->to) */
  2505. tolen=sockaddru_len(dst->to);
  2506. msg.msg_name=&dst->to.s;
  2507. msg.msg_namelen=tolen;
  2508. #endif /* __OS_* */
  2509. }else{
  2510. #ifdef SCTP_ADDR_HASH
  2511. /* update timeout for the assoc identified by (dst->to, dst->si) */
  2512. if (likely(cfg_get(sctp, sctp_cfg, assoc_tracking))){
  2513. tmp_id=sctp_con_addr_get_id_assoc(&dst->to, dst->send_sock,
  2514. &tmp_assoc_id, 0);
  2515. DBG("sctp send: timeout updated ser id %d, sctp assoc_id %d\n",
  2516. tmp_id, tmp_assoc_id);
  2517. if (tmp_id==0 /* not tracked/found */ &&
  2518. (unsigned)atomic_get(sctp_conn_tracked) >=
  2519. (unsigned)cfg_get(sctp, sctp_cfg, max_assocs)){
  2520. ERR("maximum number of sctp associations exceeded\n");
  2521. goto error;
  2522. }
  2523. }
  2524. #endif /* SCTP_ADDR_HASH */
  2525. tolen=sockaddru_len(dst->to);
  2526. msg.msg_name=&dst->to.s;
  2527. msg.msg_namelen=tolen;
  2528. }
  2529. #else /* SCTP_CONN_REUSE */
  2530. tolen=sockaddru_len(dst->to);
  2531. msg.msg_name=&dst->to.s;
  2532. msg.msg_namelen=tolen;
  2533. #endif /* SCTP_CONN_REUSE */
  2534. again:
  2535. n=sendmsg(si->socket, &msg, MSG_DONTWAIT);
  2536. if (n==-1){
  2537. #ifdef SCTP_CONN_REUSE
  2538. #ifdef __OS_linux
  2539. if ((errno==EPIPE) && assoc_id){
  2540. /* try again, this time with null assoc_id and non-null msg.name */
  2541. DBG("sctp sendmsg: assoc already closed (EPIPE), retrying with"
  2542. " assoc_id=0\n");
  2543. tolen=sockaddru_len(dst->to);
  2544. msg.msg_name=&dst->to.s;
  2545. msg.msg_namelen=tolen;
  2546. sinfo->sinfo_assoc_id=0;
  2547. goto again;
  2548. }
  2549. #elif defined __OS_freebsd
  2550. if ((errno==ENOENT)){
  2551. /* it didn't work, no retrying */
  2552. WARN("sctp sendmsg: unexpected sendmsg() failure (ENOENT),"
  2553. " assoc_id %d\n", assoc_id);
  2554. }
  2555. #else /* __OS_* */
  2556. if ((errno==ENOENT || errno==EPIPE) && assoc_id){
  2557. /* in case the sctp stack prioritises assoc_id over msg->name,
  2558. try again with 0 assoc_id and msg->name set to dst->to */
  2559. WARN("sctp sendmsg: unexpected ENOENT or EPIPE (assoc_id %d),"
  2560. "trying automatic recovery... (please report along with"
  2561. "your OS version)\n", assoc_id);
  2562. tolen=sockaddru_len(dst->to);
  2563. msg.msg_name=&dst->to.s;
  2564. msg.msg_namelen=tolen;
  2565. sinfo->sinfo_assoc_id=0;
  2566. goto again;
  2567. }
  2568. #endif /* __OS_* */
  2569. #endif /* SCTP_CONN_REUSE */
  2570. su2ip_addr(&ip, &dst->to);
  2571. LOG(L_ERR, "ERROR: sctp_msg_send: sendmsg(sock,%p,%d,0,%s:%d,...):"
  2572. " %s(%d)\n", buf, len, ip_addr2a(&ip), su_getport(&dst->to),
  2573. strerror(errno), errno);
  2574. if (errno==EINTR) goto again;
  2575. if (errno==EINVAL) {
  2576. LOG(L_CRIT,"CRITICAL: invalid sendmsg parameters\n"
  2577. "one possible reason is the server is bound to localhost and\n"
  2578. "attempts to send to the net\n");
  2579. }else if (errno==EAGAIN || errno==EWOULDBLOCK){
  2580. SCTP_STATS_SENDQ_FULL();
  2581. LOG(L_ERR, "ERROR: sctp_msg_send: failed to send, send buffers"
  2582. " full\n");
  2583. }
  2584. }
  2585. return n;
  2586. #ifdef SCTP_CONN_REUSE
  2587. #ifdef SCTP_ADDR_HASH
  2588. error:
  2589. return -1;
  2590. #endif /* SCTP_ADDR_HASH */
  2591. #endif /* SCTP_CONN_REUSE */
  2592. }
  2593. /* wrapper around sctp_msg_send_ext():
  2594. * send buf:len over udp to dst (uses only the to, send_sock and id members
  2595. * from dst)
  2596. * returns the numbers of bytes sent on success (>=0) and -1 on error
  2597. */
  2598. int sctp_msg_send(struct dest_info* dst, char* buf, unsigned len)
  2599. {
  2600. struct sctp_sndrcvinfo sinfo;
  2601. #ifdef HAVE_SCTP_SNDRCVINFO_PR_POLICY
  2602. int send_ttl;
  2603. #endif
  2604. memset(&sinfo, 0, sizeof(sinfo));
  2605. sinfo.sinfo_flags=SCTP_UNORDERED;
  2606. #ifdef HAVE_SCTP_SNDRCVINFO_PR_POLICY
  2607. if ((send_ttl=cfg_get(sctp, sctp_cfg, send_ttl))){
  2608. sinfo.sinfo_pr_policy=SCTP_PR_SCTP_TTL;
  2609. sinfo.sinfo_pr_value=send_ttl;
  2610. }else
  2611. sinfo->sinfo_pr_policy=SCTP_PR_SCTP_NONE;
  2612. #else
  2613. sinfo.sinfo_timetolive=cfg_get(sctp, sctp_cfg, send_ttl);
  2614. #endif
  2615. sinfo.sinfo_context=cfg_get(sctp, sctp_cfg, send_retries);
  2616. return sctp_msg_send_ext(dst, buf, len, &sinfo);
  2617. }
  2618. /** generic sctp info (basic stats).*/
  2619. void sctp_get_info(struct sctp_gen_info* i)
  2620. {
  2621. if (i){
  2622. i->sctp_connections_no=atomic_get(sctp_conn_no);
  2623. #ifdef SCTP_CONN_REUSE
  2624. if (likely(cfg_get(sctp, sctp_cfg, assoc_tracking)))
  2625. i->sctp_tracked_no=atomic_get(sctp_conn_tracked);
  2626. else
  2627. i->sctp_tracked_no=-1;
  2628. #else /* SCTP_CONN_REUSE */
  2629. i->sctp_tracked_no=-1;
  2630. #endif /* SCTP_CONN_REUSE */
  2631. i->sctp_total_connections=atomic_get(sctp_id);
  2632. }
  2633. }
  2634. #endif /* USE_SCTP */