2
0

sctp_server.c 87 KB

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