sctp_server.c 65 KB

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