sctp_server.c 86 KB

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