sctp_server.c 67 KB

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