sctp_server.c 85 KB

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