sctp_server.c 84 KB

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