user_socket.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586
  1. /*-
  2. * Copyright (c) 1982, 1986, 1988, 1990, 1993
  3. * The Regents of the University of California.
  4. * Copyright (c) 2004 The FreeBSD Foundation
  5. * Copyright (c) 2004-2008 Robert N. M. Watson
  6. * Copyright (c) 2009-2010 Brad Penoff
  7. * Copyright (c) 2009-2010 Humaira Kamal
  8. * Copyright (c) 2011-2012 Irene Ruengeler
  9. * Copyright (c) 2011-2012 Michael Tuexen
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. */
  34. #include <netinet/sctp_os.h>
  35. #include <netinet/sctp_pcb.h>
  36. #include <netinet/sctputil.h>
  37. #include <netinet/sctp_var.h>
  38. #include <netinet/sctp_sysctl.h>
  39. #include <netinet/sctp_input.h>
  40. #include <netinet/sctp_peeloff.h>
  41. #include <netinet/sctp_callout.h>
  42. #include <netinet/sctp_crc32.h>
  43. #ifdef INET6
  44. #include <netinet6/sctp6_var.h>
  45. #endif
  46. #if defined(__FreeBSD__)
  47. #include <sys/param.h>
  48. #endif
  49. #if defined(__linux__)
  50. #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */
  51. #endif
  52. #if !defined(_WIN32)
  53. #if defined INET || defined INET6
  54. #include <netinet/udp.h>
  55. #endif
  56. #include <arpa/inet.h>
  57. #else
  58. #include <user_socketvar.h>
  59. #endif
  60. userland_mutex_t accept_mtx;
  61. userland_cond_t accept_cond;
  62. #ifdef _WIN32
  63. #include <time.h>
  64. #include <sys/timeb.h>
  65. #endif
  66. MALLOC_DEFINE(M_PCB, "sctp_pcb", "sctp pcb");
  67. MALLOC_DEFINE(M_SONAME, "sctp_soname", "sctp soname");
  68. #define MAXLEN_MBUF_CHAIN 32
  69. /* Prototypes */
  70. extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
  71. struct mbuf *top, struct mbuf *control, int flags,
  72. /* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */
  73. struct proc *p);
  74. extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id);
  75. extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id);
  76. static void init_sync(void) {
  77. #if defined(_WIN32)
  78. #if defined(INET) || defined(INET6)
  79. WSADATA wsaData;
  80. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  81. SCTP_PRINTF("WSAStartup failed\n");
  82. exit (-1);
  83. }
  84. #endif
  85. InitializeConditionVariable(&accept_cond);
  86. InitializeCriticalSection(&accept_mtx);
  87. #else
  88. pthread_mutexattr_t mutex_attr;
  89. pthread_mutexattr_init(&mutex_attr);
  90. #ifdef INVARIANTS
  91. pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
  92. #endif
  93. pthread_mutex_init(&accept_mtx, &mutex_attr);
  94. pthread_mutexattr_destroy(&mutex_attr);
  95. pthread_cond_init(&accept_cond, NULL);
  96. #endif
  97. }
  98. void
  99. usrsctp_init(uint16_t port,
  100. int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
  101. void (*debug_printf)(const char *format, ...))
  102. {
  103. init_sync();
  104. sctp_init(port, conn_output, debug_printf, 1);
  105. }
  106. void
  107. usrsctp_init_nothreads(uint16_t port,
  108. int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
  109. void (*debug_printf)(const char *format, ...))
  110. {
  111. init_sync();
  112. sctp_init(port, conn_output, debug_printf, 0);
  113. }
  114. /* Taken from usr/src/sys/kern/uipc_sockbuf.c and modified for __Userspace__*/
  115. /*
  116. * Socantsendmore indicates that no more data will be sent on the socket; it
  117. * would normally be applied to a socket when the user informs the system
  118. * that no more data is to be sent, by the protocol code (in case
  119. * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
  120. * received, and will normally be applied to the socket by a protocol when it
  121. * detects that the peer will send no more data. Data queued for reading in
  122. * the socket may yet be read.
  123. */
  124. void socantrcvmore_locked(struct socket *so)
  125. {
  126. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  127. so->so_rcv.sb_state |= SBS_CANTRCVMORE;
  128. sorwakeup_locked(so);
  129. }
  130. void socantrcvmore(struct socket *so)
  131. {
  132. SOCKBUF_LOCK(&so->so_rcv);
  133. socantrcvmore_locked(so);
  134. }
  135. void
  136. socantsendmore_locked(struct socket *so)
  137. {
  138. SOCKBUF_LOCK_ASSERT(&so->so_snd);
  139. so->so_snd.sb_state |= SBS_CANTSENDMORE;
  140. sowwakeup_locked(so);
  141. }
  142. void
  143. socantsendmore(struct socket *so)
  144. {
  145. SOCKBUF_LOCK(&so->so_snd);
  146. socantsendmore_locked(so);
  147. }
  148. /* Taken from usr/src/sys/kern/uipc_sockbuf.c and called within sctp_lower_sosend.
  149. */
  150. int
  151. sbwait(struct sockbuf *sb)
  152. {
  153. SOCKBUF_LOCK_ASSERT(sb);
  154. sb->sb_flags |= SB_WAIT;
  155. #if defined(_WIN32)
  156. if (SleepConditionVariableCS(&(sb->sb_cond), &(sb->sb_mtx), INFINITE))
  157. return (0);
  158. else
  159. return (-1);
  160. #else
  161. return (pthread_cond_wait(&(sb->sb_cond), &(sb->sb_mtx)));
  162. #endif
  163. }
  164. /* Taken from /src/sys/kern/uipc_socket.c
  165. * and modified for __Userspace__
  166. */
  167. static struct socket *
  168. soalloc(void)
  169. {
  170. struct socket *so;
  171. /*
  172. * soalloc() sets of socket layer state for a socket,
  173. * called only by socreate() and sonewconn().
  174. *
  175. * sodealloc() tears down socket layer state for a socket,
  176. * called only by sofree() and sonewconn().
  177. * __Userspace__ TODO : Make sure so is properly deallocated
  178. * when tearing down the connection.
  179. */
  180. so = (struct socket *)malloc(sizeof(struct socket));
  181. if (so == NULL) {
  182. return (NULL);
  183. }
  184. memset(so, 0, sizeof(struct socket));
  185. /* __Userspace__ Initializing the socket locks here */
  186. SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
  187. SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
  188. SOCKBUF_COND_INIT(&so->so_snd);
  189. SOCKBUF_COND_INIT(&so->so_rcv);
  190. SOCK_COND_INIT(so); /* timeo_cond */
  191. /* __Userspace__ Any ref counting required here? Will we have any use for aiojobq?
  192. What about gencnt and numopensockets?*/
  193. TAILQ_INIT(&so->so_aiojobq);
  194. return (so);
  195. }
  196. static void
  197. sodealloc(struct socket *so)
  198. {
  199. KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
  200. KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
  201. SOCKBUF_COND_DESTROY(&so->so_snd);
  202. SOCKBUF_COND_DESTROY(&so->so_rcv);
  203. SOCK_COND_DESTROY(so);
  204. SOCKBUF_LOCK_DESTROY(&so->so_snd);
  205. SOCKBUF_LOCK_DESTROY(&so->so_rcv);
  206. free(so);
  207. }
  208. /* Taken from /src/sys/kern/uipc_socket.c
  209. * and modified for __Userspace__
  210. */
  211. void
  212. sofree(struct socket *so)
  213. {
  214. struct socket *head;
  215. ACCEPT_LOCK_ASSERT();
  216. SOCK_LOCK_ASSERT(so);
  217. /* SS_NOFDREF unset in accept call. this condition seems irrelevant
  218. * for __Userspace__...
  219. */
  220. if (so->so_count != 0 ||
  221. (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
  222. SOCK_UNLOCK(so);
  223. ACCEPT_UNLOCK();
  224. return;
  225. }
  226. head = so->so_head;
  227. if (head != NULL) {
  228. KASSERT((so->so_qstate & SQ_COMP) != 0 ||
  229. (so->so_qstate & SQ_INCOMP) != 0,
  230. ("sofree: so_head != NULL, but neither SQ_COMP nor "
  231. "SQ_INCOMP"));
  232. KASSERT((so->so_qstate & SQ_COMP) == 0 ||
  233. (so->so_qstate & SQ_INCOMP) == 0,
  234. ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
  235. TAILQ_REMOVE(&head->so_incomp, so, so_list);
  236. head->so_incqlen--;
  237. so->so_qstate &= ~SQ_INCOMP;
  238. so->so_head = NULL;
  239. }
  240. KASSERT((so->so_qstate & SQ_COMP) == 0 &&
  241. (so->so_qstate & SQ_INCOMP) == 0,
  242. ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
  243. so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
  244. if (so->so_options & SCTP_SO_ACCEPTCONN) {
  245. KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
  246. KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
  247. }
  248. SOCK_UNLOCK(so);
  249. ACCEPT_UNLOCK();
  250. sctp_close(so); /* was... sctp_detach(so); */
  251. /*
  252. * From this point on, we assume that no other references to this
  253. * socket exist anywhere else in the stack. Therefore, no locks need
  254. * to be acquired or held.
  255. *
  256. * We used to do a lot of socket buffer and socket locking here, as
  257. * well as invoke sorflush() and perform wakeups. The direct call to
  258. * dom_dispose() and sbrelease_internal() are an inlining of what was
  259. * necessary from sorflush().
  260. *
  261. * Notice that the socket buffer and kqueue state are torn down
  262. * before calling pru_detach. This means that protocols should not
  263. * assume they can perform socket wakeups, etc, in their detach code.
  264. */
  265. sodealloc(so);
  266. }
  267. /* Taken from /src/sys/kern/uipc_socket.c */
  268. void
  269. soabort(struct socket *so)
  270. {
  271. #if defined(INET6)
  272. struct sctp_inpcb *inp;
  273. #endif
  274. #if defined(INET6)
  275. inp = (struct sctp_inpcb *)so->so_pcb;
  276. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  277. sctp6_abort(so);
  278. } else {
  279. #if defined(INET)
  280. sctp_abort(so);
  281. #endif
  282. }
  283. #elif defined(INET)
  284. sctp_abort(so);
  285. #endif
  286. ACCEPT_LOCK();
  287. SOCK_LOCK(so);
  288. sofree(so);
  289. }
  290. /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_connect (sctp_usrreq.c).
  291. * We use sctp_connect for send_one_init_real in ms1.
  292. */
  293. void
  294. soisconnecting(struct socket *so)
  295. {
  296. SOCK_LOCK(so);
  297. so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
  298. so->so_state |= SS_ISCONNECTING;
  299. SOCK_UNLOCK(so);
  300. }
  301. /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_disconnect (sctp_usrreq.c).
  302. * TODO Do we use sctp_disconnect?
  303. */
  304. void
  305. soisdisconnecting(struct socket *so)
  306. {
  307. /*
  308. * Note: This code assumes that SOCK_LOCK(so) and
  309. * SOCKBUF_LOCK(&so->so_rcv) are the same.
  310. */
  311. SOCKBUF_LOCK(&so->so_rcv);
  312. so->so_state &= ~SS_ISCONNECTING;
  313. so->so_state |= SS_ISDISCONNECTING;
  314. so->so_rcv.sb_state |= SBS_CANTRCVMORE;
  315. sorwakeup_locked(so);
  316. SOCKBUF_LOCK(&so->so_snd);
  317. so->so_snd.sb_state |= SBS_CANTSENDMORE;
  318. sowwakeup_locked(so);
  319. wakeup("dummy",so);
  320. /* requires 2 args but this was in orig */
  321. /* wakeup(&so->so_timeo); */
  322. }
  323. /* Taken from sys/kern/kern_synch.c and
  324. modified for __Userspace__
  325. */
  326. /*
  327. * Make all threads sleeping on the specified identifier runnable.
  328. * Associating wakeup with so_timeo identifier and timeo_cond
  329. * condition variable. TODO. If we use iterator thread then we need to
  330. * modify wakeup so it can distinguish between iterator identifier and
  331. * timeo identifier.
  332. */
  333. void
  334. wakeup(void *ident, struct socket *so)
  335. {
  336. SOCK_LOCK(so);
  337. #if defined(_WIN32)
  338. WakeAllConditionVariable(&(so)->timeo_cond);
  339. #else
  340. pthread_cond_broadcast(&(so)->timeo_cond);
  341. #endif
  342. SOCK_UNLOCK(so);
  343. }
  344. /*
  345. * Make a thread sleeping on the specified identifier runnable.
  346. * May wake more than one thread if a target thread is currently
  347. * swapped out.
  348. */
  349. void
  350. wakeup_one(void *ident)
  351. {
  352. /* __Userspace__ Check: We are using accept_cond for wakeup_one.
  353. It seems that wakeup_one is only called within
  354. soisconnected() and sonewconn() with ident &head->so_timeo
  355. head is so->so_head, which is back pointer to listen socket
  356. This seems to indicate that the use of accept_cond is correct
  357. since socket where accepts occur is so_head in all
  358. subsidiary sockets.
  359. */
  360. ACCEPT_LOCK();
  361. #if defined(_WIN32)
  362. WakeAllConditionVariable(&accept_cond);
  363. #else
  364. pthread_cond_broadcast(&accept_cond);
  365. #endif
  366. ACCEPT_UNLOCK();
  367. }
  368. /* Called within sctp_process_cookie_[existing/new] */
  369. void
  370. soisconnected(struct socket *so)
  371. {
  372. struct socket *head;
  373. ACCEPT_LOCK();
  374. SOCK_LOCK(so);
  375. so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
  376. so->so_state |= SS_ISCONNECTED;
  377. head = so->so_head;
  378. if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
  379. SOCK_UNLOCK(so);
  380. TAILQ_REMOVE(&head->so_incomp, so, so_list);
  381. head->so_incqlen--;
  382. so->so_qstate &= ~SQ_INCOMP;
  383. TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  384. head->so_qlen++;
  385. so->so_qstate |= SQ_COMP;
  386. ACCEPT_UNLOCK();
  387. sorwakeup(head);
  388. wakeup_one(&head->so_timeo);
  389. return;
  390. }
  391. SOCK_UNLOCK(so);
  392. ACCEPT_UNLOCK();
  393. wakeup(&so->so_timeo, so);
  394. sorwakeup(so);
  395. sowwakeup(so);
  396. }
  397. /* called within sctp_handle_cookie_echo */
  398. struct socket *
  399. sonewconn(struct socket *head, int connstatus)
  400. {
  401. struct socket *so;
  402. int over;
  403. ACCEPT_LOCK();
  404. over = (head->so_qlen > 3 * head->so_qlimit / 2);
  405. ACCEPT_UNLOCK();
  406. #ifdef REGRESSION
  407. if (regression_sonewconn_earlytest && over)
  408. #else
  409. if (over)
  410. #endif
  411. return (NULL);
  412. so = soalloc();
  413. if (so == NULL)
  414. return (NULL);
  415. so->so_head = head;
  416. so->so_type = head->so_type;
  417. so->so_options = head->so_options &~ SCTP_SO_ACCEPTCONN;
  418. so->so_linger = head->so_linger;
  419. so->so_state = head->so_state | SS_NOFDREF;
  420. so->so_dom = head->so_dom;
  421. #ifdef MAC
  422. SOCK_LOCK(head);
  423. mac_create_socket_from_socket(head, so);
  424. SOCK_UNLOCK(head);
  425. #endif
  426. if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
  427. sodealloc(so);
  428. return (NULL);
  429. }
  430. switch (head->so_dom) {
  431. #ifdef INET
  432. case AF_INET:
  433. if (sctp_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
  434. sodealloc(so);
  435. return (NULL);
  436. }
  437. break;
  438. #endif
  439. #ifdef INET6
  440. case AF_INET6:
  441. if (sctp6_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
  442. sodealloc(so);
  443. return (NULL);
  444. }
  445. break;
  446. #endif
  447. case AF_CONN:
  448. if (sctpconn_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
  449. sodealloc(so);
  450. return (NULL);
  451. }
  452. break;
  453. default:
  454. sodealloc(so);
  455. return (NULL);
  456. break;
  457. }
  458. so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
  459. so->so_snd.sb_lowat = head->so_snd.sb_lowat;
  460. so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
  461. so->so_snd.sb_timeo = head->so_snd.sb_timeo;
  462. so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
  463. so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
  464. so->so_state |= connstatus;
  465. ACCEPT_LOCK();
  466. if (connstatus) {
  467. TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  468. so->so_qstate |= SQ_COMP;
  469. head->so_qlen++;
  470. } else {
  471. /*
  472. * Keep removing sockets from the head until there's room for
  473. * us to insert on the tail. In pre-locking revisions, this
  474. * was a simple if (), but as we could be racing with other
  475. * threads and soabort() requires dropping locks, we must
  476. * loop waiting for the condition to be true.
  477. */
  478. while (head->so_incqlen > head->so_qlimit) {
  479. struct socket *sp;
  480. sp = TAILQ_FIRST(&head->so_incomp);
  481. TAILQ_REMOVE(&head->so_incomp, sp, so_list);
  482. head->so_incqlen--;
  483. sp->so_qstate &= ~SQ_INCOMP;
  484. sp->so_head = NULL;
  485. ACCEPT_UNLOCK();
  486. soabort(sp);
  487. ACCEPT_LOCK();
  488. }
  489. TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
  490. so->so_qstate |= SQ_INCOMP;
  491. head->so_incqlen++;
  492. }
  493. ACCEPT_UNLOCK();
  494. if (connstatus) {
  495. sorwakeup(head);
  496. wakeup_one(&head->so_timeo);
  497. }
  498. return (so);
  499. }
  500. /*
  501. Source: /src/sys/gnu/fs/xfs/FreeBSD/xfs_ioctl.c
  502. */
  503. static __inline__ int
  504. copy_to_user(void *dst, void *src, size_t len) {
  505. memcpy(dst, src, len);
  506. return 0;
  507. }
  508. static __inline__ int
  509. copy_from_user(void *dst, void *src, size_t len) {
  510. memcpy(dst, src, len);
  511. return 0;
  512. }
  513. /*
  514. References:
  515. src/sys/dev/lmc/if_lmc.h:
  516. src/sys/powerpc/powerpc/copyinout.c
  517. src/sys/sys/systm.h
  518. */
  519. # define copyin(u, k, len) copy_from_user(k, u, len)
  520. /* References:
  521. src/sys/powerpc/powerpc/copyinout.c
  522. src/sys/sys/systm.h
  523. */
  524. # define copyout(k, u, len) copy_to_user(u, k, len)
  525. /* copyiniov definition copied/modified from src/sys/kern/kern_subr.c */
  526. int
  527. copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
  528. {
  529. u_int iovlen;
  530. *iov = NULL;
  531. if (iovcnt > UIO_MAXIOV)
  532. return (error);
  533. iovlen = iovcnt * sizeof (struct iovec);
  534. *iov = malloc(iovlen); /*, M_IOV, M_WAITOK); */
  535. error = copyin(iovp, *iov, iovlen);
  536. if (error) {
  537. free(*iov); /*, M_IOV); */
  538. *iov = NULL;
  539. }
  540. return (error);
  541. }
  542. /* (__Userspace__) version of uiomove */
  543. int
  544. uiomove(void *cp, int n, struct uio *uio)
  545. {
  546. struct iovec *iov;
  547. size_t cnt;
  548. int error = 0;
  549. if ((uio->uio_rw != UIO_READ) &&
  550. (uio->uio_rw != UIO_WRITE)) {
  551. return (EINVAL);
  552. }
  553. while (n > 0 && uio->uio_resid) {
  554. iov = uio->uio_iov;
  555. cnt = iov->iov_len;
  556. if (cnt == 0) {
  557. uio->uio_iov++;
  558. uio->uio_iovcnt--;
  559. continue;
  560. }
  561. if (cnt > (size_t)n)
  562. cnt = n;
  563. switch (uio->uio_segflg) {
  564. case UIO_USERSPACE:
  565. if (uio->uio_rw == UIO_READ)
  566. error = copyout(cp, iov->iov_base, cnt);
  567. else
  568. error = copyin(iov->iov_base, cp, cnt);
  569. if (error)
  570. goto out;
  571. break;
  572. case UIO_SYSSPACE:
  573. if (uio->uio_rw == UIO_READ)
  574. memcpy(iov->iov_base, cp, cnt);
  575. else
  576. memcpy(cp, iov->iov_base, cnt);
  577. break;
  578. }
  579. iov->iov_base = (char *)iov->iov_base + cnt;
  580. iov->iov_len -= cnt;
  581. uio->uio_resid -= cnt;
  582. uio->uio_offset += (off_t)cnt;
  583. cp = (char *)cp + cnt;
  584. n -= (int)cnt;
  585. }
  586. out:
  587. return (error);
  588. }
  589. /* Source: src/sys/kern/uipc_syscalls.c */
  590. int
  591. getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
  592. {
  593. struct sockaddr *sa;
  594. int error;
  595. if (len > SOCK_MAXADDRLEN)
  596. return (ENAMETOOLONG);
  597. if (len < offsetof(struct sockaddr, sa_data))
  598. return (EINVAL);
  599. MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
  600. error = copyin(uaddr, sa, len);
  601. if (error) {
  602. FREE(sa, M_SONAME);
  603. } else {
  604. #ifdef HAVE_SA_LEN
  605. sa->sa_len = len;
  606. #endif
  607. *namp = sa;
  608. }
  609. return (error);
  610. }
  611. int
  612. usrsctp_getsockopt(struct socket *so, int level, int option_name,
  613. void *option_value, socklen_t *option_len);
  614. sctp_assoc_t
  615. usrsctp_getassocid(struct socket *sock, struct sockaddr *sa)
  616. {
  617. struct sctp_paddrinfo sp;
  618. socklen_t siz;
  619. #ifndef HAVE_SA_LEN
  620. size_t sa_len;
  621. #endif
  622. /* First get the assoc id */
  623. siz = sizeof(sp);
  624. memset(&sp, 0, sizeof(sp));
  625. #ifdef HAVE_SA_LEN
  626. memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len);
  627. #else
  628. switch (sa->sa_family) {
  629. #ifdef INET
  630. case AF_INET:
  631. sa_len = sizeof(struct sockaddr_in);
  632. break;
  633. #endif
  634. #ifdef INET6
  635. case AF_INET6:
  636. sa_len = sizeof(struct sockaddr_in6);
  637. break;
  638. #endif
  639. case AF_CONN:
  640. sa_len = sizeof(struct sockaddr_conn);
  641. break;
  642. default:
  643. sa_len = 0;
  644. break;
  645. }
  646. memcpy((caddr_t)&sp.spinfo_address, sa, sa_len);
  647. #endif
  648. if (usrsctp_getsockopt(sock, IPPROTO_SCTP, SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) {
  649. /* We depend on the fact that 0 can never be returned */
  650. return ((sctp_assoc_t) 0);
  651. }
  652. return (sp.spinfo_assoc_id);
  653. }
  654. /* Taken from /src/lib/libc/net/sctp_sys_calls.c
  655. * and modified for __Userspace__
  656. * calling sctp_generic_sendmsg from this function
  657. */
  658. ssize_t
  659. userspace_sctp_sendmsg(struct socket *so,
  660. const void *data,
  661. size_t len,
  662. struct sockaddr *to,
  663. socklen_t tolen,
  664. uint32_t ppid,
  665. uint32_t flags,
  666. uint16_t stream_no,
  667. uint32_t timetolive,
  668. uint32_t context)
  669. {
  670. struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
  671. struct uio auio;
  672. struct iovec iov[1];
  673. memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
  674. sinfo->sinfo_ppid = ppid;
  675. sinfo->sinfo_flags = flags;
  676. sinfo->sinfo_stream = stream_no;
  677. sinfo->sinfo_timetolive = timetolive;
  678. sinfo->sinfo_context = context;
  679. sinfo->sinfo_assoc_id = 0;
  680. /* Perform error checks on destination (to) */
  681. if (tolen > SOCK_MAXADDRLEN) {
  682. errno = ENAMETOOLONG;
  683. return (-1);
  684. }
  685. if ((tolen > 0) &&
  686. ((to == NULL) || (tolen < (socklen_t)sizeof(struct sockaddr)))) {
  687. errno = EINVAL;
  688. return (-1);
  689. }
  690. if (data == NULL) {
  691. errno = EFAULT;
  692. return (-1);
  693. }
  694. /* Adding the following as part of defensive programming, in case the application
  695. does not do it when preparing the destination address.*/
  696. #ifdef HAVE_SA_LEN
  697. if (to != NULL) {
  698. to->sa_len = tolen;
  699. }
  700. #endif
  701. iov[0].iov_base = (caddr_t)data;
  702. iov[0].iov_len = len;
  703. auio.uio_iov = iov;
  704. auio.uio_iovcnt = 1;
  705. auio.uio_segflg = UIO_USERSPACE;
  706. auio.uio_rw = UIO_WRITE;
  707. auio.uio_offset = 0; /* XXX */
  708. auio.uio_resid = len;
  709. errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, 0, sinfo);
  710. if (errno == 0) {
  711. return (len - auio.uio_resid);
  712. } else {
  713. return (-1);
  714. }
  715. }
  716. ssize_t
  717. usrsctp_sendv(struct socket *so,
  718. const void *data,
  719. size_t len,
  720. struct sockaddr *to,
  721. int addrcnt,
  722. void *info,
  723. socklen_t infolen,
  724. unsigned int infotype,
  725. int flags)
  726. {
  727. struct sctp_sndrcvinfo sinfo;
  728. struct uio auio;
  729. struct iovec iov[1];
  730. int use_sinfo;
  731. sctp_assoc_t *assoc_id;
  732. if (so == NULL) {
  733. errno = EBADF;
  734. return (-1);
  735. }
  736. if (data == NULL) {
  737. errno = EFAULT;
  738. return (-1);
  739. }
  740. memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
  741. assoc_id = NULL;
  742. use_sinfo = 0;
  743. switch (infotype) {
  744. case SCTP_SENDV_NOINFO:
  745. if ((infolen != 0) || (info != NULL)) {
  746. errno = EINVAL;
  747. return (-1);
  748. }
  749. break;
  750. case SCTP_SENDV_SNDINFO:
  751. if ((info == NULL) || (infolen != sizeof(struct sctp_sndinfo))) {
  752. errno = EINVAL;
  753. return (-1);
  754. }
  755. sinfo.sinfo_stream = ((struct sctp_sndinfo *)info)->snd_sid;
  756. sinfo.sinfo_flags = ((struct sctp_sndinfo *)info)->snd_flags;
  757. sinfo.sinfo_ppid = ((struct sctp_sndinfo *)info)->snd_ppid;
  758. sinfo.sinfo_context = ((struct sctp_sndinfo *)info)->snd_context;
  759. sinfo.sinfo_assoc_id = ((struct sctp_sndinfo *)info)->snd_assoc_id;
  760. assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id);
  761. use_sinfo = 1;
  762. break;
  763. case SCTP_SENDV_PRINFO:
  764. if ((info == NULL) || (infolen != sizeof(struct sctp_prinfo))) {
  765. errno = EINVAL;
  766. return (-1);
  767. }
  768. sinfo.sinfo_stream = 0;
  769. sinfo.sinfo_flags = PR_SCTP_POLICY(((struct sctp_prinfo *)info)->pr_policy);
  770. sinfo.sinfo_timetolive = ((struct sctp_prinfo *)info)->pr_value;
  771. use_sinfo = 1;
  772. break;
  773. case SCTP_SENDV_AUTHINFO:
  774. errno = EINVAL;
  775. return (-1);
  776. case SCTP_SENDV_SPA:
  777. if ((info == NULL) || (infolen != sizeof(struct sctp_sendv_spa))) {
  778. errno = EINVAL;
  779. return (-1);
  780. }
  781. if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
  782. sinfo.sinfo_stream = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_sid;
  783. sinfo.sinfo_flags = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_flags;
  784. sinfo.sinfo_ppid = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_ppid;
  785. sinfo.sinfo_context = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_context;
  786. sinfo.sinfo_assoc_id = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id;
  787. assoc_id = &(((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id);
  788. } else {
  789. sinfo.sinfo_flags = 0;
  790. sinfo.sinfo_stream = 0;
  791. }
  792. if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_PRINFO_VALID) {
  793. sinfo.sinfo_flags |= PR_SCTP_POLICY(((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_policy);
  794. sinfo.sinfo_timetolive = ((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_value;
  795. }
  796. if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
  797. errno = EINVAL;
  798. return (-1);
  799. }
  800. use_sinfo = 1;
  801. break;
  802. default:
  803. errno = EINVAL;
  804. return (-1);
  805. }
  806. /* Perform error checks on destination (to) */
  807. if (addrcnt > 1) {
  808. errno = EINVAL;
  809. return (-1);
  810. }
  811. iov[0].iov_base = (caddr_t)data;
  812. iov[0].iov_len = len;
  813. auio.uio_iov = iov;
  814. auio.uio_iovcnt = 1;
  815. auio.uio_segflg = UIO_USERSPACE;
  816. auio.uio_rw = UIO_WRITE;
  817. auio.uio_offset = 0; /* XXX */
  818. auio.uio_resid = len;
  819. errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, flags, use_sinfo ? &sinfo : NULL);
  820. if (errno == 0) {
  821. if ((to != NULL) && (assoc_id != NULL)) {
  822. *assoc_id = usrsctp_getassocid(so, to);
  823. }
  824. return (len - auio.uio_resid);
  825. } else {
  826. return (-1);
  827. }
  828. }
  829. ssize_t
  830. userspace_sctp_sendmbuf(struct socket *so,
  831. struct mbuf* mbufdata,
  832. size_t len,
  833. struct sockaddr *to,
  834. socklen_t tolen,
  835. uint32_t ppid,
  836. uint32_t flags,
  837. uint16_t stream_no,
  838. uint32_t timetolive,
  839. uint32_t context)
  840. {
  841. struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
  842. /* struct uio auio;
  843. struct iovec iov[1]; */
  844. int error = 0;
  845. int uflags = 0;
  846. ssize_t retval;
  847. sinfo->sinfo_ppid = ppid;
  848. sinfo->sinfo_flags = flags;
  849. sinfo->sinfo_stream = stream_no;
  850. sinfo->sinfo_timetolive = timetolive;
  851. sinfo->sinfo_context = context;
  852. sinfo->sinfo_assoc_id = 0;
  853. /* Perform error checks on destination (to) */
  854. if (tolen > SOCK_MAXADDRLEN){
  855. error = (ENAMETOOLONG);
  856. goto sendmsg_return;
  857. }
  858. if (tolen < (socklen_t)offsetof(struct sockaddr, sa_data)){
  859. error = (EINVAL);
  860. goto sendmsg_return;
  861. }
  862. /* Adding the following as part of defensive programming, in case the application
  863. does not do it when preparing the destination address.*/
  864. #ifdef HAVE_SA_LEN
  865. to->sa_len = tolen;
  866. #endif
  867. error = sctp_lower_sosend(so, to, NULL/*uio*/,
  868. (struct mbuf *)mbufdata, (struct mbuf *)NULL,
  869. uflags, sinfo);
  870. sendmsg_return:
  871. /* TODO: Needs a condition for non-blocking when error is EWOULDBLOCK */
  872. if (0 == error)
  873. retval = len;
  874. else if (error == EWOULDBLOCK) {
  875. errno = EWOULDBLOCK;
  876. retval = -1;
  877. } else {
  878. SCTP_PRINTF("%s: error = %d\n", __func__, error);
  879. errno = error;
  880. retval = -1;
  881. }
  882. return (retval);
  883. }
  884. /* taken from usr.lib/sctp_sys_calls.c and needed here */
  885. #define SCTP_SMALL_IOVEC_SIZE 2
  886. /* Taken from /src/lib/libc/net/sctp_sys_calls.c
  887. * and modified for __Userspace__
  888. * calling sctp_generic_recvmsg from this function
  889. */
  890. ssize_t
  891. userspace_sctp_recvmsg(struct socket *so,
  892. void *dbuf,
  893. size_t len,
  894. struct sockaddr *from,
  895. socklen_t *fromlenp,
  896. struct sctp_sndrcvinfo *sinfo,
  897. int *msg_flags)
  898. {
  899. struct uio auio;
  900. struct iovec iov[SCTP_SMALL_IOVEC_SIZE];
  901. struct iovec *tiov;
  902. int iovlen = 1;
  903. int error = 0;
  904. ssize_t ulen;
  905. int i;
  906. socklen_t fromlen;
  907. iov[0].iov_base = dbuf;
  908. iov[0].iov_len = len;
  909. auio.uio_iov = iov;
  910. auio.uio_iovcnt = iovlen;
  911. auio.uio_segflg = UIO_USERSPACE;
  912. auio.uio_rw = UIO_READ;
  913. auio.uio_offset = 0; /* XXX */
  914. auio.uio_resid = 0;
  915. tiov = iov;
  916. for (i = 0; i <iovlen; i++, tiov++) {
  917. if ((auio.uio_resid += tiov->iov_len) < 0) {
  918. error = EINVAL;
  919. SCTP_PRINTF("%s: error = %d\n", __func__, error);
  920. return (-1);
  921. }
  922. }
  923. ulen = auio.uio_resid;
  924. if (fromlenp != NULL) {
  925. fromlen = *fromlenp;
  926. } else {
  927. fromlen = 0;
  928. }
  929. error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
  930. from, fromlen, msg_flags,
  931. (struct sctp_sndrcvinfo *)sinfo, 1);
  932. if (error) {
  933. if ((auio.uio_resid != ulen) &&
  934. (error == EINTR ||
  935. #if !defined(__NetBSD__)
  936. error == ERESTART ||
  937. #endif
  938. error == EWOULDBLOCK)) {
  939. error = 0;
  940. }
  941. }
  942. if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) {
  943. switch (from->sa_family) {
  944. #if defined(INET)
  945. case AF_INET:
  946. *fromlenp = sizeof(struct sockaddr_in);
  947. break;
  948. #endif
  949. #if defined(INET6)
  950. case AF_INET6:
  951. *fromlenp = sizeof(struct sockaddr_in6);
  952. break;
  953. #endif
  954. case AF_CONN:
  955. *fromlenp = sizeof(struct sockaddr_conn);
  956. break;
  957. default:
  958. *fromlenp = 0;
  959. break;
  960. }
  961. if (*fromlenp > fromlen) {
  962. *fromlenp = fromlen;
  963. }
  964. }
  965. if (error == 0) {
  966. /* ready return value */
  967. return (ulen - auio.uio_resid);
  968. } else {
  969. SCTP_PRINTF("%s: error = %d\n", __func__, error);
  970. return (-1);
  971. }
  972. }
  973. ssize_t
  974. usrsctp_recvv(struct socket *so,
  975. void *dbuf,
  976. size_t len,
  977. struct sockaddr *from,
  978. socklen_t *fromlenp,
  979. void *info,
  980. socklen_t *infolen,
  981. unsigned int *infotype,
  982. int *msg_flags)
  983. {
  984. struct uio auio;
  985. struct iovec iov[SCTP_SMALL_IOVEC_SIZE];
  986. struct iovec *tiov;
  987. int iovlen = 1;
  988. ssize_t ulen;
  989. int i;
  990. socklen_t fromlen;
  991. struct sctp_rcvinfo *rcv;
  992. struct sctp_recvv_rn *rn;
  993. struct sctp_extrcvinfo seinfo;
  994. if (so == NULL) {
  995. errno = EBADF;
  996. return (-1);
  997. }
  998. iov[0].iov_base = dbuf;
  999. iov[0].iov_len = len;
  1000. auio.uio_iov = iov;
  1001. auio.uio_iovcnt = iovlen;
  1002. auio.uio_segflg = UIO_USERSPACE;
  1003. auio.uio_rw = UIO_READ;
  1004. auio.uio_offset = 0; /* XXX */
  1005. auio.uio_resid = 0;
  1006. tiov = iov;
  1007. for (i = 0; i <iovlen; i++, tiov++) {
  1008. if ((auio.uio_resid += tiov->iov_len) < 0) {
  1009. errno = EINVAL;
  1010. return (-1);
  1011. }
  1012. }
  1013. ulen = auio.uio_resid;
  1014. if (fromlenp != NULL) {
  1015. fromlen = *fromlenp;
  1016. } else {
  1017. fromlen = 0;
  1018. }
  1019. errno = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
  1020. from, fromlen, msg_flags,
  1021. (struct sctp_sndrcvinfo *)&seinfo, 1);
  1022. if (errno) {
  1023. if ((auio.uio_resid != ulen) &&
  1024. (errno == EINTR ||
  1025. #if !defined(__NetBSD__)
  1026. errno == ERESTART ||
  1027. #endif
  1028. errno == EWOULDBLOCK)) {
  1029. errno = 0;
  1030. }
  1031. }
  1032. if (errno != 0) {
  1033. goto out;
  1034. }
  1035. if ((*msg_flags & MSG_NOTIFICATION) == 0) {
  1036. struct sctp_inpcb *inp;
  1037. inp = (struct sctp_inpcb *)so->so_pcb;
  1038. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
  1039. sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
  1040. *infolen >= (socklen_t)sizeof(struct sctp_recvv_rn) &&
  1041. seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL) {
  1042. rn = (struct sctp_recvv_rn *)info;
  1043. rn->recvv_rcvinfo.rcv_sid = seinfo.sinfo_stream;
  1044. rn->recvv_rcvinfo.rcv_ssn = seinfo.sinfo_ssn;
  1045. rn->recvv_rcvinfo.rcv_flags = seinfo.sinfo_flags;
  1046. rn->recvv_rcvinfo.rcv_ppid = seinfo.sinfo_ppid;
  1047. rn->recvv_rcvinfo.rcv_context = seinfo.sinfo_context;
  1048. rn->recvv_rcvinfo.rcv_tsn = seinfo.sinfo_tsn;
  1049. rn->recvv_rcvinfo.rcv_cumtsn = seinfo.sinfo_cumtsn;
  1050. rn->recvv_rcvinfo.rcv_assoc_id = seinfo.sinfo_assoc_id;
  1051. rn->recvv_nxtinfo.nxt_sid = seinfo.sreinfo_next_stream;
  1052. rn->recvv_nxtinfo.nxt_flags = 0;
  1053. if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
  1054. rn->recvv_nxtinfo.nxt_flags |= SCTP_UNORDERED;
  1055. }
  1056. if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
  1057. rn->recvv_nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
  1058. }
  1059. if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
  1060. rn->recvv_nxtinfo.nxt_flags |= SCTP_COMPLETE;
  1061. }
  1062. rn->recvv_nxtinfo.nxt_ppid = seinfo.sreinfo_next_ppid;
  1063. rn->recvv_nxtinfo.nxt_length = seinfo.sreinfo_next_length;
  1064. rn->recvv_nxtinfo.nxt_assoc_id = seinfo.sreinfo_next_aid;
  1065. *infolen = (socklen_t)sizeof(struct sctp_recvv_rn);
  1066. *infotype = SCTP_RECVV_RN;
  1067. } else if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
  1068. *infolen >= (socklen_t)sizeof(struct sctp_rcvinfo)) {
  1069. rcv = (struct sctp_rcvinfo *)info;
  1070. rcv->rcv_sid = seinfo.sinfo_stream;
  1071. rcv->rcv_ssn = seinfo.sinfo_ssn;
  1072. rcv->rcv_flags = seinfo.sinfo_flags;
  1073. rcv->rcv_ppid = seinfo.sinfo_ppid;
  1074. rcv->rcv_context = seinfo.sinfo_context;
  1075. rcv->rcv_tsn = seinfo.sinfo_tsn;
  1076. rcv->rcv_cumtsn = seinfo.sinfo_cumtsn;
  1077. rcv->rcv_assoc_id = seinfo.sinfo_assoc_id;
  1078. *infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
  1079. *infotype = SCTP_RECVV_RCVINFO;
  1080. } else {
  1081. *infotype = SCTP_RECVV_NOINFO;
  1082. *infolen = 0;
  1083. }
  1084. }
  1085. if ((fromlenp != NULL) &&
  1086. (fromlen > 0) &&
  1087. (from != NULL) &&
  1088. (ulen > auio.uio_resid)) {
  1089. switch (from->sa_family) {
  1090. #if defined(INET)
  1091. case AF_INET:
  1092. *fromlenp = sizeof(struct sockaddr_in);
  1093. break;
  1094. #endif
  1095. #if defined(INET6)
  1096. case AF_INET6:
  1097. *fromlenp = sizeof(struct sockaddr_in6);
  1098. break;
  1099. #endif
  1100. case AF_CONN:
  1101. *fromlenp = sizeof(struct sockaddr_conn);
  1102. break;
  1103. default:
  1104. *fromlenp = 0;
  1105. break;
  1106. }
  1107. if (*fromlenp > fromlen) {
  1108. *fromlenp = fromlen;
  1109. }
  1110. }
  1111. out:
  1112. if (errno == 0) {
  1113. /* ready return value */
  1114. return (ulen - auio.uio_resid);
  1115. } else {
  1116. return (-1);
  1117. }
  1118. }
  1119. /* Taken from /src/sys/kern/uipc_socket.c
  1120. * and modified for __Userspace__
  1121. * socreate returns a socket. The socket should be
  1122. * closed with soclose().
  1123. */
  1124. int
  1125. socreate(int dom, struct socket **aso, int type, int proto)
  1126. {
  1127. struct socket *so;
  1128. int error;
  1129. if ((dom != AF_CONN) && (dom != AF_INET) && (dom != AF_INET6)) {
  1130. return (EINVAL);
  1131. }
  1132. if ((type != SOCK_STREAM) && (type != SOCK_SEQPACKET)) {
  1133. return (EINVAL);
  1134. }
  1135. if (proto != IPPROTO_SCTP) {
  1136. return (EINVAL);
  1137. }
  1138. so = soalloc();
  1139. if (so == NULL) {
  1140. return (ENOBUFS);
  1141. }
  1142. /*
  1143. * so_incomp represents a queue of connections that
  1144. * must be completed at protocol level before being
  1145. * returned. so_comp field heads a list of sockets
  1146. * that are ready to be returned to the listening process
  1147. *__Userspace__ These queues are being used at a number of places like accept etc.
  1148. */
  1149. TAILQ_INIT(&so->so_incomp);
  1150. TAILQ_INIT(&so->so_comp);
  1151. so->so_type = type;
  1152. so->so_count = 1;
  1153. so->so_dom = dom;
  1154. /*
  1155. * Auto-sizing of socket buffers is managed by the protocols and
  1156. * the appropriate flags must be set in the pru_attach function.
  1157. * For __Userspace__ The pru_attach function in this case is sctp_attach.
  1158. */
  1159. switch (dom) {
  1160. #if defined(INET)
  1161. case AF_INET:
  1162. error = sctp_attach(so, proto, SCTP_DEFAULT_VRFID);
  1163. break;
  1164. #endif
  1165. #if defined(INET6)
  1166. case AF_INET6:
  1167. error = sctp6_attach(so, proto, SCTP_DEFAULT_VRFID);
  1168. break;
  1169. #endif
  1170. case AF_CONN:
  1171. error = sctpconn_attach(so, proto, SCTP_DEFAULT_VRFID);
  1172. break;
  1173. default:
  1174. error = EAFNOSUPPORT;
  1175. break;
  1176. }
  1177. if (error) {
  1178. KASSERT(so->so_count == 1, ("socreate: so_count %d", so->so_count));
  1179. so->so_count = 0;
  1180. sodealloc(so);
  1181. return (error);
  1182. }
  1183. *aso = so;
  1184. return (0);
  1185. }
  1186. /* Taken from /src/sys/kern/uipc_syscalls.c
  1187. * and modified for __Userspace__
  1188. * Removing struct thread td.
  1189. */
  1190. struct socket *
  1191. userspace_socket(int domain, int type, int protocol)
  1192. {
  1193. struct socket *so = NULL;
  1194. errno = socreate(domain, &so, type, protocol);
  1195. if (errno) {
  1196. return (NULL);
  1197. }
  1198. /*
  1199. * The original socket call returns the file descriptor fd.
  1200. * td->td_retval[0] = fd.
  1201. * We are returning struct socket *so.
  1202. */
  1203. return (so);
  1204. }
  1205. struct socket *
  1206. usrsctp_socket(int domain, int type, int protocol,
  1207. int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data,
  1208. size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info),
  1209. int (*send_cb)(struct socket *sock, uint32_t sb_free, void *ulp_info),
  1210. uint32_t sb_threshold,
  1211. void *ulp_info)
  1212. {
  1213. struct socket *so = NULL;
  1214. if ((protocol == IPPROTO_SCTP) && (SCTP_BASE_VAR(sctp_pcb_initialized) == 0)) {
  1215. errno = EPROTONOSUPPORT;
  1216. return (NULL);
  1217. }
  1218. if ((receive_cb == NULL) &&
  1219. ((send_cb != NULL) || (sb_threshold != 0) || (ulp_info != NULL))) {
  1220. errno = EINVAL;
  1221. return (NULL);
  1222. }
  1223. if ((domain == AF_CONN) && (SCTP_BASE_VAR(conn_output) == NULL)) {
  1224. errno = EAFNOSUPPORT;
  1225. return (NULL);
  1226. }
  1227. errno = socreate(domain, &so, type, protocol);
  1228. if (errno) {
  1229. return (NULL);
  1230. }
  1231. /*
  1232. * The original socket call returns the file descriptor fd.
  1233. * td->td_retval[0] = fd.
  1234. * We are returning struct socket *so.
  1235. */
  1236. register_recv_cb(so, receive_cb);
  1237. register_send_cb(so, sb_threshold, send_cb);
  1238. register_ulp_info(so, ulp_info);
  1239. return (so);
  1240. }
  1241. u_long sb_max = SB_MAX;
  1242. u_long sb_max_adj =
  1243. SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
  1244. static u_long sb_efficiency = 8; /* parameter for sbreserve() */
  1245. /*
  1246. * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
  1247. * become limiting if buffering efficiency is near the normal case.
  1248. */
  1249. int
  1250. sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so)
  1251. {
  1252. SOCKBUF_LOCK_ASSERT(sb);
  1253. sb->sb_mbmax = (u_int)min(cc * sb_efficiency, sb_max);
  1254. sb->sb_hiwat = (u_int)cc;
  1255. if (sb->sb_lowat > (int)sb->sb_hiwat)
  1256. sb->sb_lowat = (int)sb->sb_hiwat;
  1257. return (1);
  1258. }
  1259. static int
  1260. sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
  1261. {
  1262. int error;
  1263. SOCKBUF_LOCK(sb);
  1264. error = sbreserve_locked(sb, cc, so);
  1265. SOCKBUF_UNLOCK(sb);
  1266. return (error);
  1267. }
  1268. int
  1269. soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
  1270. {
  1271. SOCKBUF_LOCK(&so->so_snd);
  1272. SOCKBUF_LOCK(&so->so_rcv);
  1273. so->so_snd.sb_hiwat = (uint32_t)sndcc;
  1274. so->so_rcv.sb_hiwat = (uint32_t)rcvcc;
  1275. if (sbreserve_locked(&so->so_snd, sndcc, so) == 0) {
  1276. goto bad;
  1277. }
  1278. if (sbreserve_locked(&so->so_rcv, rcvcc, so) == 0) {
  1279. goto bad;
  1280. }
  1281. if (so->so_rcv.sb_lowat == 0)
  1282. so->so_rcv.sb_lowat = 1;
  1283. if (so->so_snd.sb_lowat == 0)
  1284. so->so_snd.sb_lowat = MCLBYTES;
  1285. if (so->so_snd.sb_lowat > (int)so->so_snd.sb_hiwat)
  1286. so->so_snd.sb_lowat = (int)so->so_snd.sb_hiwat;
  1287. SOCKBUF_UNLOCK(&so->so_rcv);
  1288. SOCKBUF_UNLOCK(&so->so_snd);
  1289. return (0);
  1290. bad:
  1291. SOCKBUF_UNLOCK(&so->so_rcv);
  1292. SOCKBUF_UNLOCK(&so->so_snd);
  1293. return (ENOBUFS);
  1294. }
  1295. /* Taken from /src/sys/kern/uipc_sockbuf.c
  1296. * and modified for __Userspace__
  1297. */
  1298. void
  1299. sowakeup(struct socket *so, struct sockbuf *sb)
  1300. {
  1301. SOCKBUF_LOCK_ASSERT(sb);
  1302. sb->sb_flags &= ~SB_SEL;
  1303. if (sb->sb_flags & SB_WAIT) {
  1304. sb->sb_flags &= ~SB_WAIT;
  1305. #if defined(_WIN32)
  1306. WakeAllConditionVariable(&(sb)->sb_cond);
  1307. #else
  1308. pthread_cond_broadcast(&(sb)->sb_cond);
  1309. #endif
  1310. }
  1311. SOCKBUF_UNLOCK(sb);
  1312. }
  1313. /* Taken from /src/sys/kern/uipc_socket.c
  1314. * and modified for __Userspace__
  1315. */
  1316. int
  1317. sobind(struct socket *so, struct sockaddr *nam)
  1318. {
  1319. switch (nam->sa_family) {
  1320. #if defined(INET)
  1321. case AF_INET:
  1322. return (sctp_bind(so, nam));
  1323. #endif
  1324. #if defined(INET6)
  1325. case AF_INET6:
  1326. return (sctp6_bind(so, nam, NULL));
  1327. #endif
  1328. case AF_CONN:
  1329. return (sctpconn_bind(so, nam));
  1330. default:
  1331. return EAFNOSUPPORT;
  1332. }
  1333. }
  1334. /* Taken from /src/sys/kern/uipc_syscalls.c
  1335. * and modified for __Userspace__
  1336. */
  1337. int
  1338. usrsctp_bind(struct socket *so, struct sockaddr *name, int namelen)
  1339. {
  1340. struct sockaddr *sa;
  1341. if (so == NULL) {
  1342. errno = EBADF;
  1343. return (-1);
  1344. }
  1345. if ((errno = getsockaddr(&sa, (caddr_t)name, namelen)) != 0)
  1346. return (-1);
  1347. errno = sobind(so, sa);
  1348. FREE(sa, M_SONAME);
  1349. if (errno) {
  1350. return (-1);
  1351. } else {
  1352. return (0);
  1353. }
  1354. }
  1355. int
  1356. userspace_bind(struct socket *so, struct sockaddr *name, int namelen)
  1357. {
  1358. return (usrsctp_bind(so, name, namelen));
  1359. }
  1360. /* Taken from /src/sys/kern/uipc_socket.c
  1361. * and modified for __Userspace__
  1362. */
  1363. int
  1364. solisten(struct socket *so, int backlog)
  1365. {
  1366. if (so == NULL) {
  1367. return (EBADF);
  1368. } else {
  1369. return (sctp_listen(so, backlog, NULL));
  1370. }
  1371. }
  1372. int
  1373. solisten_proto_check(struct socket *so)
  1374. {
  1375. SOCK_LOCK_ASSERT(so);
  1376. if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
  1377. SS_ISDISCONNECTING))
  1378. return (EINVAL);
  1379. return (0);
  1380. }
  1381. static int somaxconn = SOMAXCONN;
  1382. void
  1383. solisten_proto(struct socket *so, int backlog)
  1384. {
  1385. SOCK_LOCK_ASSERT(so);
  1386. if (backlog < 0 || backlog > somaxconn)
  1387. backlog = somaxconn;
  1388. so->so_qlimit = backlog;
  1389. so->so_options |= SCTP_SO_ACCEPTCONN;
  1390. }
  1391. /* Taken from /src/sys/kern/uipc_syscalls.c
  1392. * and modified for __Userspace__
  1393. */
  1394. int
  1395. usrsctp_listen(struct socket *so, int backlog)
  1396. {
  1397. errno = solisten(so, backlog);
  1398. if (errno) {
  1399. return (-1);
  1400. } else {
  1401. return (0);
  1402. }
  1403. }
  1404. int
  1405. userspace_listen(struct socket *so, int backlog)
  1406. {
  1407. return (usrsctp_listen(so, backlog));
  1408. }
  1409. /* Taken from /src/sys/kern/uipc_socket.c
  1410. * and modified for __Userspace__
  1411. */
  1412. int
  1413. soaccept(struct socket *so, struct sockaddr **nam)
  1414. {
  1415. int error;
  1416. SOCK_LOCK(so);
  1417. KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
  1418. so->so_state &= ~SS_NOFDREF;
  1419. SOCK_UNLOCK(so);
  1420. error = sctp_accept(so, nam);
  1421. return (error);
  1422. }
  1423. /* Taken from /src/sys/kern/uipc_syscalls.c
  1424. * kern_accept modified for __Userspace__
  1425. */
  1426. int
  1427. user_accept(struct socket *head, struct sockaddr **name, socklen_t *namelen, struct socket **ptr_accept_ret_sock)
  1428. {
  1429. struct sockaddr *sa = NULL;
  1430. int error;
  1431. struct socket *so = NULL;
  1432. if (name) {
  1433. *name = NULL;
  1434. }
  1435. if ((head->so_options & SCTP_SO_ACCEPTCONN) == 0) {
  1436. error = EINVAL;
  1437. goto done;
  1438. }
  1439. ACCEPT_LOCK();
  1440. if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
  1441. ACCEPT_UNLOCK();
  1442. error = EWOULDBLOCK;
  1443. goto noconnection;
  1444. }
  1445. while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
  1446. if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
  1447. head->so_error = ECONNABORTED;
  1448. break;
  1449. }
  1450. #if defined(_WIN32)
  1451. if (SleepConditionVariableCS(&accept_cond, &accept_mtx, INFINITE))
  1452. error = 0;
  1453. else
  1454. error = GetLastError();
  1455. #else
  1456. error = pthread_cond_wait(&accept_cond, &accept_mtx);
  1457. #endif
  1458. if (error) {
  1459. ACCEPT_UNLOCK();
  1460. goto noconnection;
  1461. }
  1462. }
  1463. if (head->so_error) {
  1464. error = head->so_error;
  1465. head->so_error = 0;
  1466. ACCEPT_UNLOCK();
  1467. goto noconnection;
  1468. }
  1469. so = TAILQ_FIRST(&head->so_comp);
  1470. KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
  1471. KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
  1472. /*
  1473. * Before changing the flags on the socket, we have to bump the
  1474. * reference count. Otherwise, if the protocol calls sofree(),
  1475. * the socket will be released due to a zero refcount.
  1476. */
  1477. SOCK_LOCK(so); /* soref() and so_state update */
  1478. soref(so); /* file descriptor reference */
  1479. TAILQ_REMOVE(&head->so_comp, so, so_list);
  1480. head->so_qlen--;
  1481. so->so_state |= (head->so_state & SS_NBIO);
  1482. so->so_qstate &= ~SQ_COMP;
  1483. so->so_head = NULL;
  1484. SOCK_UNLOCK(so);
  1485. ACCEPT_UNLOCK();
  1486. /*
  1487. * The original accept returns fd value via td->td_retval[0] = fd;
  1488. * we will return the socket for accepted connection.
  1489. */
  1490. error = soaccept(so, &sa);
  1491. if (error) {
  1492. /*
  1493. * return a namelen of zero for older code which might
  1494. * ignore the return value from accept.
  1495. */
  1496. if (name)
  1497. *namelen = 0;
  1498. goto noconnection;
  1499. }
  1500. if (sa == NULL) {
  1501. if (name)
  1502. *namelen = 0;
  1503. goto done;
  1504. }
  1505. if (name) {
  1506. #ifdef HAVE_SA_LEN
  1507. /* check sa_len before it is destroyed */
  1508. if (*namelen > sa->sa_len) {
  1509. *namelen = sa->sa_len;
  1510. }
  1511. #else
  1512. socklen_t sa_len;
  1513. switch (sa->sa_family) {
  1514. #ifdef INET
  1515. case AF_INET:
  1516. sa_len = sizeof(struct sockaddr_in);
  1517. break;
  1518. #endif
  1519. #ifdef INET6
  1520. case AF_INET6:
  1521. sa_len = sizeof(struct sockaddr_in6);
  1522. break;
  1523. #endif
  1524. case AF_CONN:
  1525. sa_len = sizeof(struct sockaddr_conn);
  1526. break;
  1527. default:
  1528. sa_len = 0;
  1529. break;
  1530. }
  1531. if (*namelen > sa_len) {
  1532. *namelen = sa_len;
  1533. }
  1534. #endif
  1535. *name = sa;
  1536. sa = NULL;
  1537. }
  1538. noconnection:
  1539. if (sa) {
  1540. FREE(sa, M_SONAME);
  1541. }
  1542. done:
  1543. *ptr_accept_ret_sock = so;
  1544. return (error);
  1545. }
  1546. /* Taken from /src/sys/kern/uipc_syscalls.c
  1547. * and modified for __Userspace__
  1548. */
  1549. /*
  1550. * accept1()
  1551. */
  1552. static int
  1553. accept1(struct socket *so, struct sockaddr *aname, socklen_t *anamelen, struct socket **ptr_accept_ret_sock)
  1554. {
  1555. struct sockaddr *name;
  1556. socklen_t namelen;
  1557. int error;
  1558. if (so == NULL) {
  1559. return (EBADF);
  1560. }
  1561. if (aname == NULL) {
  1562. return (user_accept(so, NULL, NULL, ptr_accept_ret_sock));
  1563. }
  1564. error = copyin(anamelen, &namelen, sizeof (namelen));
  1565. if (error)
  1566. return (error);
  1567. error = user_accept(so, &name, &namelen, ptr_accept_ret_sock);
  1568. /*
  1569. * return a namelen of zero for older code which might
  1570. * ignore the return value from accept.
  1571. */
  1572. if (error) {
  1573. (void) copyout(&namelen,
  1574. anamelen, sizeof(*anamelen));
  1575. return (error);
  1576. }
  1577. if (error == 0 && name != NULL) {
  1578. error = copyout(name, aname, namelen);
  1579. }
  1580. if (error == 0) {
  1581. error = copyout(&namelen, anamelen, sizeof(namelen));
  1582. }
  1583. if (name) {
  1584. FREE(name, M_SONAME);
  1585. }
  1586. return (error);
  1587. }
  1588. struct socket *
  1589. usrsctp_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen)
  1590. {
  1591. struct socket *accept_return_sock = NULL;
  1592. errno = accept1(so, aname, anamelen, &accept_return_sock);
  1593. if (errno) {
  1594. return (NULL);
  1595. } else {
  1596. return (accept_return_sock);
  1597. }
  1598. }
  1599. struct socket *
  1600. userspace_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen)
  1601. {
  1602. return (usrsctp_accept(so, aname, anamelen));
  1603. }
  1604. struct socket *
  1605. usrsctp_peeloff(struct socket *head, sctp_assoc_t id)
  1606. {
  1607. struct socket *so;
  1608. if ((errno = sctp_can_peel_off(head, id)) != 0) {
  1609. return (NULL);
  1610. }
  1611. if ((so = sonewconn(head, SS_ISCONNECTED)) == NULL) {
  1612. return (NULL);
  1613. }
  1614. ACCEPT_LOCK();
  1615. SOCK_LOCK(so);
  1616. soref(so);
  1617. TAILQ_REMOVE(&head->so_comp, so, so_list);
  1618. head->so_qlen--;
  1619. so->so_state |= (head->so_state & SS_NBIO);
  1620. so->so_qstate &= ~SQ_COMP;
  1621. so->so_head = NULL;
  1622. SOCK_UNLOCK(so);
  1623. ACCEPT_UNLOCK();
  1624. if ((errno = sctp_do_peeloff(head, so, id)) != 0) {
  1625. so->so_count = 0;
  1626. sodealloc(so);
  1627. return (NULL);
  1628. }
  1629. return (so);
  1630. }
  1631. int
  1632. sodisconnect(struct socket *so)
  1633. {
  1634. int error;
  1635. if ((so->so_state & SS_ISCONNECTED) == 0)
  1636. return (ENOTCONN);
  1637. if (so->so_state & SS_ISDISCONNECTING)
  1638. return (EALREADY);
  1639. error = sctp_disconnect(so);
  1640. return (error);
  1641. }
  1642. int
  1643. usrsctp_set_non_blocking(struct socket *so, int onoff)
  1644. {
  1645. if (so == NULL) {
  1646. errno = EBADF;
  1647. return (-1);
  1648. }
  1649. SOCK_LOCK(so);
  1650. if (onoff != 0) {
  1651. so->so_state |= SS_NBIO;
  1652. } else {
  1653. so->so_state &= ~SS_NBIO;
  1654. }
  1655. SOCK_UNLOCK(so);
  1656. return (0);
  1657. }
  1658. int
  1659. usrsctp_get_non_blocking(struct socket *so)
  1660. {
  1661. int result;
  1662. if (so == NULL) {
  1663. errno = EBADF;
  1664. return (-1);
  1665. }
  1666. SOCK_LOCK(so);
  1667. if (so->so_state & SS_NBIO) {
  1668. result = 1;
  1669. } else {
  1670. result = 0;
  1671. }
  1672. SOCK_UNLOCK(so);
  1673. return (result);
  1674. }
  1675. int
  1676. soconnect(struct socket *so, struct sockaddr *nam)
  1677. {
  1678. int error;
  1679. if (so->so_options & SCTP_SO_ACCEPTCONN)
  1680. return (EOPNOTSUPP);
  1681. /*
  1682. * If protocol is connection-based, can only connect once.
  1683. * Otherwise, if connected, try to disconnect first. This allows
  1684. * user to disconnect by connecting to, e.g., a null address.
  1685. */
  1686. if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && (sodisconnect(so) != 0)) {
  1687. error = EISCONN;
  1688. } else {
  1689. /*
  1690. * Prevent accumulated error from previous connection from
  1691. * biting us.
  1692. */
  1693. so->so_error = 0;
  1694. switch (nam->sa_family) {
  1695. #if defined(INET)
  1696. case AF_INET:
  1697. error = sctp_connect(so, nam);
  1698. break;
  1699. #endif
  1700. #if defined(INET6)
  1701. case AF_INET6:
  1702. error = sctp6_connect(so, nam);
  1703. break;
  1704. #endif
  1705. case AF_CONN:
  1706. error = sctpconn_connect(so, nam);
  1707. break;
  1708. default:
  1709. error = EAFNOSUPPORT;
  1710. }
  1711. }
  1712. return (error);
  1713. }
  1714. int user_connect(struct socket *so, struct sockaddr *sa)
  1715. {
  1716. int error;
  1717. int interrupted = 0;
  1718. if (so == NULL) {
  1719. error = EBADF;
  1720. goto done1;
  1721. }
  1722. if (so->so_state & SS_ISCONNECTING) {
  1723. error = EALREADY;
  1724. goto done1;
  1725. }
  1726. error = soconnect(so, sa);
  1727. if (error) {
  1728. goto bad;
  1729. }
  1730. if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
  1731. error = EINPROGRESS;
  1732. goto done1;
  1733. }
  1734. SOCK_LOCK(so);
  1735. while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  1736. #if defined(_WIN32)
  1737. if (SleepConditionVariableCS(SOCK_COND(so), SOCK_MTX(so), INFINITE))
  1738. error = 0;
  1739. else
  1740. error = -1;
  1741. #else
  1742. error = pthread_cond_wait(SOCK_COND(so), SOCK_MTX(so));
  1743. #endif
  1744. if (error) {
  1745. #if defined(__NetBSD__)
  1746. if (error == EINTR) {
  1747. #else
  1748. if (error == EINTR || error == ERESTART) {
  1749. #endif
  1750. interrupted = 1;
  1751. }
  1752. break;
  1753. }
  1754. }
  1755. if (error == 0) {
  1756. error = so->so_error;
  1757. so->so_error = 0;
  1758. }
  1759. SOCK_UNLOCK(so);
  1760. bad:
  1761. if (!interrupted) {
  1762. so->so_state &= ~SS_ISCONNECTING;
  1763. }
  1764. #if !defined(__NetBSD__)
  1765. if (error == ERESTART) {
  1766. error = EINTR;
  1767. }
  1768. #endif
  1769. done1:
  1770. return (error);
  1771. }
  1772. int usrsctp_connect(struct socket *so, struct sockaddr *name, int namelen)
  1773. {
  1774. struct sockaddr *sa = NULL;
  1775. errno = getsockaddr(&sa, (caddr_t)name, namelen);
  1776. if (errno)
  1777. return (-1);
  1778. errno = user_connect(so, sa);
  1779. FREE(sa, M_SONAME);
  1780. if (errno) {
  1781. return (-1);
  1782. } else {
  1783. return (0);
  1784. }
  1785. }
  1786. int userspace_connect(struct socket *so, struct sockaddr *name, int namelen)
  1787. {
  1788. return (usrsctp_connect(so, name, namelen));
  1789. }
  1790. #define SCTP_STACK_BUF_SIZE 2048
  1791. void
  1792. usrsctp_close(struct socket *so) {
  1793. if (so != NULL) {
  1794. if (so->so_options & SCTP_SO_ACCEPTCONN) {
  1795. struct socket *sp;
  1796. ACCEPT_LOCK();
  1797. while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
  1798. TAILQ_REMOVE(&so->so_comp, sp, so_list);
  1799. so->so_qlen--;
  1800. sp->so_qstate &= ~SQ_COMP;
  1801. sp->so_head = NULL;
  1802. ACCEPT_UNLOCK();
  1803. soabort(sp);
  1804. ACCEPT_LOCK();
  1805. }
  1806. ACCEPT_UNLOCK();
  1807. }
  1808. ACCEPT_LOCK();
  1809. SOCK_LOCK(so);
  1810. sorele(so);
  1811. }
  1812. }
  1813. void
  1814. userspace_close(struct socket *so)
  1815. {
  1816. usrsctp_close(so);
  1817. }
  1818. int
  1819. usrsctp_shutdown(struct socket *so, int how)
  1820. {
  1821. if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) {
  1822. errno = EINVAL;
  1823. return (-1);
  1824. }
  1825. if (so == NULL) {
  1826. errno = EBADF;
  1827. return (-1);
  1828. }
  1829. sctp_flush(so, how);
  1830. if (how != SHUT_WR)
  1831. socantrcvmore(so);
  1832. if (how != SHUT_RD) {
  1833. errno = sctp_shutdown(so);
  1834. if (errno) {
  1835. return (-1);
  1836. } else {
  1837. return (0);
  1838. }
  1839. }
  1840. return (0);
  1841. }
  1842. int
  1843. userspace_shutdown(struct socket *so, int how)
  1844. {
  1845. return (usrsctp_shutdown(so, how));
  1846. }
  1847. int
  1848. usrsctp_finish(void)
  1849. {
  1850. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  1851. return (0);
  1852. }
  1853. if (SCTP_INP_INFO_TRYLOCK()) {
  1854. if (!LIST_EMPTY(&SCTP_BASE_INFO(listhead))) {
  1855. SCTP_INP_INFO_RUNLOCK();
  1856. return (-1);
  1857. }
  1858. SCTP_INP_INFO_RUNLOCK();
  1859. } else {
  1860. return (-1);
  1861. }
  1862. sctp_finish();
  1863. #if defined(_WIN32)
  1864. DeleteConditionVariable(&accept_cond);
  1865. DeleteCriticalSection(&accept_mtx);
  1866. #if defined(INET) || defined(INET6)
  1867. WSACleanup();
  1868. #endif
  1869. #else
  1870. pthread_cond_destroy(&accept_cond);
  1871. pthread_mutex_destroy(&accept_mtx);
  1872. #endif
  1873. return (0);
  1874. }
  1875. int
  1876. userspace_finish(void)
  1877. {
  1878. return (usrsctp_finish());
  1879. }
  1880. /* needed from sctp_usrreq.c */
  1881. int
  1882. sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, void *p);
  1883. int
  1884. usrsctp_setsockopt(struct socket *so, int level, int option_name,
  1885. const void *option_value, socklen_t option_len)
  1886. {
  1887. if (so == NULL) {
  1888. errno = EBADF;
  1889. return (-1);
  1890. }
  1891. switch (level) {
  1892. case SOL_SOCKET:
  1893. {
  1894. switch (option_name) {
  1895. case SO_RCVBUF:
  1896. if (option_len < (socklen_t)sizeof(int)) {
  1897. errno = EINVAL;
  1898. return (-1);
  1899. } else {
  1900. int *buf_size;
  1901. buf_size = (int *)option_value;
  1902. if (*buf_size < 1) {
  1903. errno = EINVAL;
  1904. return (-1);
  1905. }
  1906. sbreserve(&so->so_rcv, (u_long)*buf_size, so);
  1907. return (0);
  1908. }
  1909. break;
  1910. case SO_SNDBUF:
  1911. if (option_len < (socklen_t)sizeof(int)) {
  1912. errno = EINVAL;
  1913. return (-1);
  1914. } else {
  1915. int *buf_size;
  1916. buf_size = (int *)option_value;
  1917. if (*buf_size < 1) {
  1918. errno = EINVAL;
  1919. return (-1);
  1920. }
  1921. sbreserve(&so->so_snd, (u_long)*buf_size, so);
  1922. return (0);
  1923. }
  1924. break;
  1925. case SO_LINGER:
  1926. if (option_len < (socklen_t)sizeof(struct linger)) {
  1927. errno = EINVAL;
  1928. return (-1);
  1929. } else {
  1930. struct linger *l;
  1931. l = (struct linger *)option_value;
  1932. so->so_linger = l->l_linger;
  1933. if (l->l_onoff) {
  1934. so->so_options |= SCTP_SO_LINGER;
  1935. } else {
  1936. so->so_options &= ~SCTP_SO_LINGER;
  1937. }
  1938. return (0);
  1939. }
  1940. default:
  1941. errno = EINVAL;
  1942. return (-1);
  1943. }
  1944. }
  1945. case IPPROTO_SCTP:
  1946. errno = sctp_setopt(so, option_name, (void *) option_value, (size_t)option_len, NULL);
  1947. if (errno) {
  1948. return (-1);
  1949. } else {
  1950. return (0);
  1951. }
  1952. default:
  1953. errno = ENOPROTOOPT;
  1954. return (-1);
  1955. }
  1956. }
  1957. int
  1958. userspace_setsockopt(struct socket *so, int level, int option_name,
  1959. const void *option_value, socklen_t option_len)
  1960. {
  1961. return (usrsctp_setsockopt(so, level, option_name, option_value, option_len));
  1962. }
  1963. /* needed from sctp_usrreq.c */
  1964. int
  1965. sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
  1966. void *p);
  1967. int
  1968. usrsctp_getsockopt(struct socket *so, int level, int option_name,
  1969. void *option_value, socklen_t *option_len)
  1970. {
  1971. if (so == NULL) {
  1972. errno = EBADF;
  1973. return (-1);
  1974. }
  1975. if (option_len == NULL) {
  1976. errno = EFAULT;
  1977. return (-1);
  1978. }
  1979. switch (level) {
  1980. case SOL_SOCKET:
  1981. switch (option_name) {
  1982. case SO_RCVBUF:
  1983. if (*option_len < (socklen_t)sizeof(int)) {
  1984. errno = EINVAL;
  1985. return (-1);
  1986. } else {
  1987. int *buf_size;
  1988. buf_size = (int *)option_value;
  1989. *buf_size = so->so_rcv.sb_hiwat;
  1990. *option_len = (socklen_t)sizeof(int);
  1991. return (0);
  1992. }
  1993. break;
  1994. case SO_SNDBUF:
  1995. if (*option_len < (socklen_t)sizeof(int)) {
  1996. errno = EINVAL;
  1997. return (-1);
  1998. } else {
  1999. int *buf_size;
  2000. buf_size = (int *)option_value;
  2001. *buf_size = so->so_snd.sb_hiwat;
  2002. *option_len = (socklen_t)sizeof(int);
  2003. return (0);
  2004. }
  2005. break;
  2006. case SO_LINGER:
  2007. if (*option_len < (socklen_t)sizeof(struct linger)) {
  2008. errno = EINVAL;
  2009. return (-1);
  2010. } else {
  2011. struct linger *l;
  2012. l = (struct linger *)option_value;
  2013. l->l_linger = so->so_linger;
  2014. if (so->so_options & SCTP_SO_LINGER) {
  2015. l->l_onoff = 1;
  2016. } else {
  2017. l->l_onoff = 0;
  2018. }
  2019. *option_len = (socklen_t)sizeof(struct linger);
  2020. return (0);
  2021. }
  2022. break;
  2023. case SO_ERROR:
  2024. if (*option_len < (socklen_t)sizeof(int)) {
  2025. errno = EINVAL;
  2026. return (-1);
  2027. } else {
  2028. int *intval;
  2029. intval = (int *)option_value;
  2030. *intval = so->so_error;
  2031. *option_len = (socklen_t)sizeof(int);
  2032. return (0);
  2033. }
  2034. break;
  2035. default:
  2036. errno = EINVAL;
  2037. return (-1);
  2038. }
  2039. case IPPROTO_SCTP:
  2040. {
  2041. size_t len;
  2042. len = (size_t)*option_len;
  2043. errno = sctp_getopt(so, option_name, option_value, &len, NULL);
  2044. *option_len = (socklen_t)len;
  2045. if (errno) {
  2046. return (-1);
  2047. } else {
  2048. return (0);
  2049. }
  2050. }
  2051. default:
  2052. errno = ENOPROTOOPT;
  2053. return (-1);
  2054. }
  2055. }
  2056. int
  2057. userspace_getsockopt(struct socket *so, int level, int option_name,
  2058. void *option_value, socklen_t *option_len)
  2059. {
  2060. return (usrsctp_getsockopt(so, level, option_name, option_value, option_len));
  2061. }
  2062. int
  2063. usrsctp_opt_info(struct socket *so, sctp_assoc_t id, int opt, void *arg, socklen_t *size)
  2064. {
  2065. if (arg == NULL) {
  2066. errno = EINVAL;
  2067. return (-1);
  2068. }
  2069. if ((id == SCTP_CURRENT_ASSOC) ||
  2070. (id == SCTP_ALL_ASSOC)) {
  2071. errno = EINVAL;
  2072. return (-1);
  2073. }
  2074. switch (opt) {
  2075. case SCTP_RTOINFO:
  2076. ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id;
  2077. break;
  2078. case SCTP_ASSOCINFO:
  2079. ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
  2080. break;
  2081. case SCTP_DEFAULT_SEND_PARAM:
  2082. ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
  2083. break;
  2084. case SCTP_PRIMARY_ADDR:
  2085. ((struct sctp_setprim *)arg)->ssp_assoc_id = id;
  2086. break;
  2087. case SCTP_PEER_ADDR_PARAMS:
  2088. ((struct sctp_paddrparams *)arg)->spp_assoc_id = id;
  2089. break;
  2090. case SCTP_MAXSEG:
  2091. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2092. break;
  2093. case SCTP_AUTH_KEY:
  2094. ((struct sctp_authkey *)arg)->sca_assoc_id = id;
  2095. break;
  2096. case SCTP_AUTH_ACTIVE_KEY:
  2097. ((struct sctp_authkeyid *)arg)->scact_assoc_id = id;
  2098. break;
  2099. case SCTP_DELAYED_SACK:
  2100. ((struct sctp_sack_info *)arg)->sack_assoc_id = id;
  2101. break;
  2102. case SCTP_CONTEXT:
  2103. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2104. break;
  2105. case SCTP_STATUS:
  2106. ((struct sctp_status *)arg)->sstat_assoc_id = id;
  2107. break;
  2108. case SCTP_GET_PEER_ADDR_INFO:
  2109. ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id;
  2110. break;
  2111. case SCTP_PEER_AUTH_CHUNKS:
  2112. ((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
  2113. break;
  2114. case SCTP_LOCAL_AUTH_CHUNKS:
  2115. ((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
  2116. break;
  2117. case SCTP_TIMEOUTS:
  2118. ((struct sctp_timeouts *)arg)->stimo_assoc_id = id;
  2119. break;
  2120. case SCTP_EVENT:
  2121. ((struct sctp_event *)arg)->se_assoc_id = id;
  2122. break;
  2123. case SCTP_DEFAULT_SNDINFO:
  2124. ((struct sctp_sndinfo *)arg)->snd_assoc_id = id;
  2125. break;
  2126. case SCTP_DEFAULT_PRINFO:
  2127. ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id;
  2128. break;
  2129. case SCTP_PEER_ADDR_THLDS:
  2130. ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id;
  2131. break;
  2132. case SCTP_REMOTE_UDP_ENCAPS_PORT:
  2133. ((struct sctp_udpencaps *)arg)->sue_assoc_id = id;
  2134. break;
  2135. case SCTP_ECN_SUPPORTED:
  2136. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2137. break;
  2138. case SCTP_PR_SUPPORTED:
  2139. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2140. break;
  2141. case SCTP_AUTH_SUPPORTED:
  2142. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2143. break;
  2144. case SCTP_ASCONF_SUPPORTED:
  2145. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2146. break;
  2147. case SCTP_RECONFIG_SUPPORTED:
  2148. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2149. break;
  2150. case SCTP_NRSACK_SUPPORTED:
  2151. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2152. break;
  2153. case SCTP_PKTDROP_SUPPORTED:
  2154. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2155. break;
  2156. case SCTP_MAX_BURST:
  2157. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2158. break;
  2159. case SCTP_ENABLE_STREAM_RESET:
  2160. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2161. break;
  2162. case SCTP_PR_STREAM_STATUS:
  2163. ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
  2164. break;
  2165. case SCTP_PR_ASSOC_STATUS:
  2166. ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
  2167. break;
  2168. case SCTP_MAX_CWND:
  2169. ((struct sctp_assoc_value *)arg)->assoc_id = id;
  2170. break;
  2171. default:
  2172. break;
  2173. }
  2174. return (usrsctp_getsockopt(so, IPPROTO_SCTP, opt, arg, size));
  2175. }
  2176. int
  2177. usrsctp_set_ulpinfo(struct socket *so, void *ulp_info)
  2178. {
  2179. return (register_ulp_info(so, ulp_info));
  2180. }
  2181. int
  2182. usrsctp_get_ulpinfo(struct socket *so, void **pulp_info)
  2183. {
  2184. return (retrieve_ulp_info(so, pulp_info));
  2185. }
  2186. int
  2187. usrsctp_bindx(struct socket *so, struct sockaddr *addrs, int addrcnt, int flags)
  2188. {
  2189. struct sockaddr *sa;
  2190. #ifdef INET
  2191. struct sockaddr_in *sin;
  2192. #endif
  2193. #ifdef INET6
  2194. struct sockaddr_in6 *sin6;
  2195. #endif
  2196. int i;
  2197. #if defined(INET) || defined(INET6)
  2198. uint16_t sport;
  2199. bool fix_port;
  2200. #endif
  2201. /* validate the flags */
  2202. if ((flags != SCTP_BINDX_ADD_ADDR) &&
  2203. (flags != SCTP_BINDX_REM_ADDR)) {
  2204. errno = EFAULT;
  2205. return (-1);
  2206. }
  2207. /* validate the address count and list */
  2208. if ((addrcnt <= 0) || (addrs == NULL)) {
  2209. errno = EINVAL;
  2210. return (-1);
  2211. }
  2212. #if defined(INET) || defined(INET6)
  2213. sport = 0;
  2214. fix_port = false;
  2215. #endif
  2216. /* First pre-screen the addresses */
  2217. sa = addrs;
  2218. for (i = 0; i < addrcnt; i++) {
  2219. switch (sa->sa_family) {
  2220. #ifdef INET
  2221. case AF_INET:
  2222. #ifdef HAVE_SA_LEN
  2223. if (sa->sa_len != sizeof(struct sockaddr_in)) {
  2224. errno = EINVAL;
  2225. return (-1);
  2226. }
  2227. #endif
  2228. sin = (struct sockaddr_in *)sa;
  2229. if (sin->sin_port) {
  2230. /* non-zero port, check or save */
  2231. if (sport) {
  2232. /* Check against our port */
  2233. if (sport != sin->sin_port) {
  2234. errno = EINVAL;
  2235. return (-1);
  2236. }
  2237. } else {
  2238. /* save off the port */
  2239. sport = sin->sin_port;
  2240. fix_port = (i > 0);
  2241. }
  2242. }
  2243. #ifndef HAVE_SA_LEN
  2244. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
  2245. #endif
  2246. break;
  2247. #endif
  2248. #ifdef INET6
  2249. case AF_INET6:
  2250. #ifdef HAVE_SA_LEN
  2251. if (sa->sa_len != sizeof(struct sockaddr_in6)) {
  2252. errno = EINVAL;
  2253. return (-1);
  2254. }
  2255. #endif
  2256. sin6 = (struct sockaddr_in6 *)sa;
  2257. if (sin6->sin6_port) {
  2258. /* non-zero port, check or save */
  2259. if (sport) {
  2260. /* Check against our port */
  2261. if (sport != sin6->sin6_port) {
  2262. errno = EINVAL;
  2263. return (-1);
  2264. }
  2265. } else {
  2266. /* save off the port */
  2267. sport = sin6->sin6_port;
  2268. fix_port = (i > 0);
  2269. }
  2270. }
  2271. #ifndef HAVE_SA_LEN
  2272. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
  2273. #endif
  2274. break;
  2275. #endif
  2276. default:
  2277. /* Invalid address family specified. */
  2278. errno = EAFNOSUPPORT;
  2279. return (-1);
  2280. }
  2281. #ifdef HAVE_SA_LEN
  2282. sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
  2283. #endif
  2284. }
  2285. sa = addrs;
  2286. for (i = 0; i < addrcnt; i++) {
  2287. #ifndef HAVE_SA_LEN
  2288. size_t sa_len;
  2289. #endif
  2290. #ifdef HAVE_SA_LEN
  2291. #if defined(INET) || defined(INET6)
  2292. if (fix_port) {
  2293. switch (sa->sa_family) {
  2294. #ifdef INET
  2295. case AF_INET:
  2296. ((struct sockaddr_in *)sa)->sin_port = sport;
  2297. break;
  2298. #endif
  2299. #ifdef INET6
  2300. case AF_INET6:
  2301. ((struct sockaddr_in6 *)sa)->sin6_port = sport;
  2302. break;
  2303. #endif
  2304. }
  2305. }
  2306. #endif
  2307. if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, sa, sa->sa_len) != 0) {
  2308. return (-1);
  2309. }
  2310. sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
  2311. #else
  2312. switch (sa->sa_family) {
  2313. #ifdef INET
  2314. case AF_INET:
  2315. sa_len = sizeof(struct sockaddr_in);
  2316. break;
  2317. #endif
  2318. #ifdef INET6
  2319. case AF_INET6:
  2320. sa_len = sizeof(struct sockaddr_in6);
  2321. break;
  2322. #endif
  2323. default:
  2324. sa_len = 0;
  2325. break;
  2326. }
  2327. /*
  2328. * Now, if there was a port mentioned, assure that the
  2329. * first address has that port to make sure it fails or
  2330. * succeeds correctly.
  2331. */
  2332. #if defined(INET) || defined(INET6)
  2333. if (fix_port) {
  2334. switch (sa->sa_family) {
  2335. #ifdef INET
  2336. case AF_INET:
  2337. ((struct sockaddr_in *)sa)->sin_port = sport;
  2338. break;
  2339. #endif
  2340. #ifdef INET6
  2341. case AF_INET6:
  2342. ((struct sockaddr_in6 *)sa)->sin6_port = sport;
  2343. break;
  2344. #endif
  2345. }
  2346. }
  2347. #endif
  2348. if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, sa, (socklen_t)sa_len) != 0) {
  2349. return (-1);
  2350. }
  2351. sa = (struct sockaddr *)((caddr_t)sa + sa_len);
  2352. #endif
  2353. }
  2354. return (0);
  2355. }
  2356. int
  2357. usrsctp_connectx(struct socket *so,
  2358. const struct sockaddr *addrs, int addrcnt,
  2359. sctp_assoc_t *id)
  2360. {
  2361. #if defined(INET) || defined(INET6)
  2362. char buf[SCTP_STACK_BUF_SIZE];
  2363. int i, ret, cnt, *aa;
  2364. char *cpto;
  2365. const struct sockaddr *at;
  2366. sctp_assoc_t *p_id;
  2367. size_t len = sizeof(int);
  2368. /* validate the address count and list */
  2369. if ((addrs == NULL) || (addrcnt <= 0)) {
  2370. errno = EINVAL;
  2371. return (-1);
  2372. }
  2373. at = addrs;
  2374. cnt = 0;
  2375. cpto = ((caddr_t)buf + sizeof(int));
  2376. /* validate all the addresses and get the size */
  2377. for (i = 0; i < addrcnt; i++) {
  2378. switch (at->sa_family) {
  2379. #ifdef INET
  2380. case AF_INET:
  2381. #ifdef HAVE_SA_LEN
  2382. if (at->sa_len != sizeof(struct sockaddr_in)) {
  2383. errno = EINVAL;
  2384. return (-1);
  2385. }
  2386. #endif
  2387. len += sizeof(struct sockaddr_in);
  2388. if (len > SCTP_STACK_BUF_SIZE) {
  2389. errno = ENOMEM;
  2390. return (-1);
  2391. }
  2392. memcpy(cpto, at, sizeof(struct sockaddr_in));
  2393. cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
  2394. at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in));
  2395. break;
  2396. #endif
  2397. #ifdef INET6
  2398. case AF_INET6:
  2399. #ifdef HAVE_SA_LEN
  2400. if (at->sa_len != sizeof(struct sockaddr_in6)) {
  2401. errno = EINVAL;
  2402. return (-1);
  2403. }
  2404. #endif
  2405. #ifdef INET
  2406. if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) {
  2407. len += sizeof(struct sockaddr_in);
  2408. if (len > SCTP_STACK_BUF_SIZE) {
  2409. errno = ENOMEM;
  2410. return (-1);
  2411. }
  2412. in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at);
  2413. cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
  2414. } else {
  2415. len += sizeof(struct sockaddr_in6);
  2416. if (len > SCTP_STACK_BUF_SIZE) {
  2417. errno = ENOMEM;
  2418. return (-1);
  2419. }
  2420. memcpy(cpto, at, sizeof(struct sockaddr_in6));
  2421. cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6));
  2422. }
  2423. #else
  2424. len += sizeof(struct sockaddr_in6);
  2425. if (len > SCTP_STACK_BUF_SIZE) {
  2426. errno = ENOMEM;
  2427. return (-1);
  2428. }
  2429. memcpy(cpto, at, sizeof(struct sockaddr_in6));
  2430. cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6));
  2431. #endif
  2432. at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in6));
  2433. break;
  2434. #endif
  2435. default:
  2436. errno = EINVAL;
  2437. return (-1);
  2438. }
  2439. cnt++;
  2440. }
  2441. aa = (int *)buf;
  2442. *aa = cnt;
  2443. ret = usrsctp_setsockopt(so, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, (socklen_t)len);
  2444. if ((ret == 0) && id) {
  2445. p_id = (sctp_assoc_t *)buf;
  2446. *id = *p_id;
  2447. }
  2448. return (ret);
  2449. #else
  2450. errno = EINVAL;
  2451. return (-1);
  2452. #endif
  2453. }
  2454. int
  2455. usrsctp_getpaddrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs)
  2456. {
  2457. struct sctp_getaddresses *addrs;
  2458. struct sockaddr *sa;
  2459. caddr_t lim;
  2460. socklen_t opt_len;
  2461. uint32_t size_of_addresses;
  2462. int cnt;
  2463. if (raddrs == NULL) {
  2464. errno = EFAULT;
  2465. return (-1);
  2466. }
  2467. /* When calling getsockopt(), the value contains the assoc_id. */
  2468. size_of_addresses = (uint32_t)id;
  2469. opt_len = (socklen_t)sizeof(uint32_t);
  2470. if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, &size_of_addresses, &opt_len) != 0) {
  2471. if (errno == ENOENT) {
  2472. return (0);
  2473. } else {
  2474. return (-1);
  2475. }
  2476. }
  2477. opt_len = (socklen_t)((size_t)size_of_addresses + sizeof(struct sctp_getaddresses));
  2478. addrs = calloc(1, (size_t)opt_len);
  2479. if (addrs == NULL) {
  2480. errno = ENOMEM;
  2481. return (-1);
  2482. }
  2483. addrs->sget_assoc_id = id;
  2484. /* Now lets get the array of addresses */
  2485. if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, addrs, &opt_len) != 0) {
  2486. free(addrs);
  2487. return (-1);
  2488. }
  2489. *raddrs = &addrs->addr[0].sa;
  2490. cnt = 0;
  2491. sa = &addrs->addr[0].sa;
  2492. lim = (caddr_t)addrs + opt_len;
  2493. #ifdef HAVE_SA_LEN
  2494. while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
  2495. sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
  2496. #else
  2497. while ((caddr_t)sa < lim) {
  2498. switch (sa->sa_family) {
  2499. #ifdef INET
  2500. case AF_INET:
  2501. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
  2502. break;
  2503. #endif
  2504. #ifdef INET6
  2505. case AF_INET6:
  2506. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
  2507. break;
  2508. #endif
  2509. case AF_CONN:
  2510. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn));
  2511. break;
  2512. default:
  2513. return (cnt);
  2514. break;
  2515. }
  2516. #endif
  2517. cnt++;
  2518. }
  2519. return (cnt);
  2520. }
  2521. void
  2522. usrsctp_freepaddrs(struct sockaddr *addrs)
  2523. {
  2524. /* Take away the hidden association id */
  2525. void *fr_addr;
  2526. fr_addr = (void *)((caddr_t)addrs - offsetof(struct sctp_getaddresses, addr));
  2527. /* Now free it */
  2528. free(fr_addr);
  2529. }
  2530. int
  2531. usrsctp_getladdrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs)
  2532. {
  2533. struct sctp_getaddresses *addrs;
  2534. struct sockaddr *sa;
  2535. caddr_t lim;
  2536. socklen_t opt_len;
  2537. uint32_t size_of_addresses;
  2538. int cnt;
  2539. if (raddrs == NULL) {
  2540. errno = EFAULT;
  2541. return (-1);
  2542. }
  2543. size_of_addresses = 0;
  2544. opt_len = (socklen_t)sizeof(uint32_t);
  2545. if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, &size_of_addresses, &opt_len) != 0) {
  2546. return (-1);
  2547. }
  2548. opt_len = (socklen_t)(size_of_addresses + sizeof(struct sctp_getaddresses));
  2549. addrs = calloc(1, (size_t)opt_len);
  2550. if (addrs == NULL) {
  2551. errno = ENOMEM;
  2552. return (-1);
  2553. }
  2554. addrs->sget_assoc_id = id;
  2555. /* Now lets get the array of addresses */
  2556. if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, &opt_len) != 0) {
  2557. free(addrs);
  2558. return (-1);
  2559. }
  2560. if (size_of_addresses == 0) {
  2561. free(addrs);
  2562. return (0);
  2563. }
  2564. *raddrs = &addrs->addr[0].sa;
  2565. cnt = 0;
  2566. sa = &addrs->addr[0].sa;
  2567. lim = (caddr_t)addrs + opt_len;
  2568. #ifdef HAVE_SA_LEN
  2569. while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
  2570. sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
  2571. #else
  2572. while ((caddr_t)sa < lim) {
  2573. switch (sa->sa_family) {
  2574. #ifdef INET
  2575. case AF_INET:
  2576. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
  2577. break;
  2578. #endif
  2579. #ifdef INET6
  2580. case AF_INET6:
  2581. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
  2582. break;
  2583. #endif
  2584. case AF_CONN:
  2585. sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn));
  2586. break;
  2587. default:
  2588. return (cnt);
  2589. break;
  2590. }
  2591. #endif
  2592. cnt++;
  2593. }
  2594. return (cnt);
  2595. }
  2596. void
  2597. usrsctp_freeladdrs(struct sockaddr *addrs)
  2598. {
  2599. /* Take away the hidden association id */
  2600. void *fr_addr;
  2601. fr_addr = (void *)((caddr_t)addrs - offsetof(struct sctp_getaddresses, addr));
  2602. /* Now free it */
  2603. free(fr_addr);
  2604. }
  2605. #ifdef INET
  2606. void
  2607. sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
  2608. sctp_route_t *ro, void *inp,
  2609. uint32_t vrf_id)
  2610. {
  2611. struct mbuf *m;
  2612. struct mbuf *m_orig;
  2613. int iovcnt;
  2614. int len;
  2615. struct ip *ip;
  2616. struct udphdr *udp;
  2617. struct sockaddr_in dst;
  2618. #if defined(_WIN32)
  2619. WSAMSG win_msg_hdr;
  2620. DWORD win_sent_len;
  2621. WSABUF send_iovec[MAXLEN_MBUF_CHAIN];
  2622. WSABUF winbuf;
  2623. #else
  2624. struct iovec send_iovec[MAXLEN_MBUF_CHAIN];
  2625. struct msghdr msg_hdr;
  2626. #endif
  2627. int use_udp_tunneling;
  2628. *result = 0;
  2629. m = SCTP_HEADER_TO_CHAIN(o_pak);
  2630. m_orig = m;
  2631. len = sizeof(struct ip);
  2632. if (SCTP_BUF_LEN(m) < len) {
  2633. if ((m = m_pullup(m, len)) == 0) {
  2634. SCTP_PRINTF("Can not get the IP header in the first mbuf.\n");
  2635. return;
  2636. }
  2637. }
  2638. ip = mtod(m, struct ip *);
  2639. use_udp_tunneling = (ip->ip_p == IPPROTO_UDP);
  2640. if (use_udp_tunneling) {
  2641. len = sizeof(struct ip) + sizeof(struct udphdr);
  2642. if (SCTP_BUF_LEN(m) < len) {
  2643. if ((m = m_pullup(m, len)) == 0) {
  2644. SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n");
  2645. return;
  2646. }
  2647. ip = mtod(m, struct ip *);
  2648. }
  2649. udp = (struct udphdr *)(ip + 1);
  2650. } else {
  2651. udp = NULL;
  2652. }
  2653. if (!use_udp_tunneling) {
  2654. if (ip->ip_src.s_addr == INADDR_ANY) {
  2655. /* TODO get addr of outgoing interface */
  2656. SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n");
  2657. }
  2658. /* TODO need to worry about ro->ro_dst as in ip_output? */
  2659. #if defined(__linux__) || defined(_WIN32) || (defined(__FreeBSD__) && (__FreeBSD_version >= 1100030))
  2660. /* need to put certain fields into network order for Linux */
  2661. ip->ip_len = htons(ip->ip_len);
  2662. #endif
  2663. }
  2664. memset((void *)&dst, 0, sizeof(struct sockaddr_in));
  2665. dst.sin_family = AF_INET;
  2666. dst.sin_addr.s_addr = ip->ip_dst.s_addr;
  2667. #ifdef HAVE_SIN_LEN
  2668. dst.sin_len = sizeof(struct sockaddr_in);
  2669. #endif
  2670. if (use_udp_tunneling) {
  2671. dst.sin_port = udp->uh_dport;
  2672. } else {
  2673. dst.sin_port = 0;
  2674. }
  2675. /* tweak the mbuf chain */
  2676. if (use_udp_tunneling) {
  2677. m_adj(m, sizeof(struct ip) + sizeof(struct udphdr));
  2678. }
  2679. for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) {
  2680. #if !defined(_WIN32)
  2681. send_iovec[iovcnt].iov_base = (caddr_t)m->m_data;
  2682. send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m);
  2683. #else
  2684. send_iovec[iovcnt].buf = (caddr_t)m->m_data;
  2685. send_iovec[iovcnt].len = SCTP_BUF_LEN(m);
  2686. #endif
  2687. }
  2688. if (m != NULL) {
  2689. SCTP_PRINTF("mbuf chain couldn't be copied completely\n");
  2690. goto free_mbuf;
  2691. }
  2692. #if !defined(_WIN32)
  2693. msg_hdr.msg_name = (struct sockaddr *) &dst;
  2694. msg_hdr.msg_namelen = sizeof(struct sockaddr_in);
  2695. msg_hdr.msg_iov = send_iovec;
  2696. msg_hdr.msg_iovlen = iovcnt;
  2697. msg_hdr.msg_control = NULL;
  2698. msg_hdr.msg_controllen = 0;
  2699. msg_hdr.msg_flags = 0;
  2700. if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) != -1)) {
  2701. if (sendmsg(SCTP_BASE_VAR(userspace_rawsctp), &msg_hdr, MSG_DONTWAIT) < 0) {
  2702. *result = errno;
  2703. }
  2704. }
  2705. if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) != -1)) {
  2706. if (sendmsg(SCTP_BASE_VAR(userspace_udpsctp), &msg_hdr, MSG_DONTWAIT) < 0) {
  2707. *result = errno;
  2708. }
  2709. }
  2710. #else
  2711. win_msg_hdr.name = (struct sockaddr *) &dst;
  2712. win_msg_hdr.namelen = sizeof(struct sockaddr_in);
  2713. win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec;
  2714. win_msg_hdr.dwBufferCount = iovcnt;
  2715. winbuf.len = 0;
  2716. winbuf.buf = NULL;
  2717. win_msg_hdr.Control = winbuf;
  2718. win_msg_hdr.dwFlags = 0;
  2719. if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) != -1)) {
  2720. if (WSASendTo(SCTP_BASE_VAR(userspace_rawsctp), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
  2721. *result = WSAGetLastError();
  2722. }
  2723. }
  2724. if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) != -1)) {
  2725. if (WSASendTo(SCTP_BASE_VAR(userspace_udpsctp), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
  2726. *result = WSAGetLastError();
  2727. }
  2728. }
  2729. #endif
  2730. free_mbuf:
  2731. sctp_m_freem(m_orig);
  2732. }
  2733. #endif
  2734. #if defined(INET6)
  2735. void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
  2736. struct route_in6 *ro, void *inp,
  2737. uint32_t vrf_id)
  2738. {
  2739. struct mbuf *m;
  2740. struct mbuf *m_orig;
  2741. int iovcnt;
  2742. int len;
  2743. struct ip6_hdr *ip6;
  2744. struct udphdr *udp;
  2745. struct sockaddr_in6 dst;
  2746. #if defined(_WIN32)
  2747. WSAMSG win_msg_hdr;
  2748. DWORD win_sent_len;
  2749. WSABUF send_iovec[MAXLEN_MBUF_CHAIN];
  2750. WSABUF winbuf;
  2751. #else
  2752. struct iovec send_iovec[MAXLEN_MBUF_CHAIN];
  2753. struct msghdr msg_hdr;
  2754. #endif
  2755. int use_udp_tunneling;
  2756. *result = 0;
  2757. m = SCTP_HEADER_TO_CHAIN(o_pak);
  2758. m_orig = m;
  2759. len = sizeof(struct ip6_hdr);
  2760. if (SCTP_BUF_LEN(m) < len) {
  2761. if ((m = m_pullup(m, len)) == 0) {
  2762. SCTP_PRINTF("Can not get the IP header in the first mbuf.\n");
  2763. return;
  2764. }
  2765. }
  2766. ip6 = mtod(m, struct ip6_hdr *);
  2767. use_udp_tunneling = (ip6->ip6_nxt == IPPROTO_UDP);
  2768. if (use_udp_tunneling) {
  2769. len = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
  2770. if (SCTP_BUF_LEN(m) < len) {
  2771. if ((m = m_pullup(m, len)) == 0) {
  2772. SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n");
  2773. return;
  2774. }
  2775. ip6 = mtod(m, struct ip6_hdr *);
  2776. }
  2777. udp = (struct udphdr *)(ip6 + 1);
  2778. } else {
  2779. udp = NULL;
  2780. }
  2781. if (!use_udp_tunneling) {
  2782. if (ip6->ip6_src.s6_addr == in6addr_any.s6_addr) {
  2783. /* TODO get addr of outgoing interface */
  2784. SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n");
  2785. }
  2786. /* TODO need to worry about ro->ro_dst as in ip_output? */
  2787. }
  2788. memset((void *)&dst, 0, sizeof(struct sockaddr_in6));
  2789. dst.sin6_family = AF_INET6;
  2790. dst.sin6_addr = ip6->ip6_dst;
  2791. #ifdef HAVE_SIN6_LEN
  2792. dst.sin6_len = sizeof(struct sockaddr_in6);
  2793. #endif
  2794. if (use_udp_tunneling) {
  2795. dst.sin6_port = udp->uh_dport;
  2796. } else {
  2797. dst.sin6_port = 0;
  2798. }
  2799. /* tweak the mbuf chain */
  2800. if (use_udp_tunneling) {
  2801. m_adj(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
  2802. } else {
  2803. m_adj(m, sizeof(struct ip6_hdr));
  2804. }
  2805. for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) {
  2806. #if !defined(_WIN32)
  2807. send_iovec[iovcnt].iov_base = (caddr_t)m->m_data;
  2808. send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m);
  2809. #else
  2810. send_iovec[iovcnt].buf = (caddr_t)m->m_data;
  2811. send_iovec[iovcnt].len = SCTP_BUF_LEN(m);
  2812. #endif
  2813. }
  2814. if (m != NULL) {
  2815. SCTP_PRINTF("mbuf chain couldn't be copied completely\n");
  2816. goto free_mbuf;
  2817. }
  2818. #if !defined(_WIN32)
  2819. msg_hdr.msg_name = (struct sockaddr *) &dst;
  2820. msg_hdr.msg_namelen = sizeof(struct sockaddr_in6);
  2821. msg_hdr.msg_iov = send_iovec;
  2822. msg_hdr.msg_iovlen = iovcnt;
  2823. msg_hdr.msg_control = NULL;
  2824. msg_hdr.msg_controllen = 0;
  2825. msg_hdr.msg_flags = 0;
  2826. if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) != -1)) {
  2827. if (sendmsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg_hdr, MSG_DONTWAIT)< 0) {
  2828. *result = errno;
  2829. }
  2830. }
  2831. if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) != -1)) {
  2832. if (sendmsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg_hdr, MSG_DONTWAIT) < 0) {
  2833. *result = errno;
  2834. }
  2835. }
  2836. #else
  2837. win_msg_hdr.name = (struct sockaddr *) &dst;
  2838. win_msg_hdr.namelen = sizeof(struct sockaddr_in6);
  2839. win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec;
  2840. win_msg_hdr.dwBufferCount = iovcnt;
  2841. winbuf.len = 0;
  2842. winbuf.buf = NULL;
  2843. win_msg_hdr.Control = winbuf;
  2844. win_msg_hdr.dwFlags = 0;
  2845. if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) != -1)) {
  2846. if (WSASendTo(SCTP_BASE_VAR(userspace_rawsctp6), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
  2847. *result = WSAGetLastError();
  2848. }
  2849. }
  2850. if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) != -1)) {
  2851. if (WSASendTo(SCTP_BASE_VAR(userspace_udpsctp6), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
  2852. *result = WSAGetLastError();
  2853. }
  2854. }
  2855. #endif
  2856. free_mbuf:
  2857. sctp_m_freem(m_orig);
  2858. }
  2859. #endif
  2860. void
  2861. usrsctp_register_address(void *addr)
  2862. {
  2863. struct sockaddr_conn sconn;
  2864. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  2865. sconn.sconn_family = AF_CONN;
  2866. #ifdef HAVE_SCONN_LEN
  2867. sconn.sconn_len = sizeof(struct sockaddr_conn);
  2868. #endif
  2869. sconn.sconn_port = 0;
  2870. sconn.sconn_addr = addr;
  2871. sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID,
  2872. NULL,
  2873. 0xffffffff,
  2874. 0,
  2875. "conn",
  2876. NULL,
  2877. (struct sockaddr *)&sconn,
  2878. 0,
  2879. 0);
  2880. }
  2881. void
  2882. usrsctp_deregister_address(void *addr)
  2883. {
  2884. struct sockaddr_conn sconn;
  2885. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  2886. sconn.sconn_family = AF_CONN;
  2887. #ifdef HAVE_SCONN_LEN
  2888. sconn.sconn_len = sizeof(struct sockaddr_conn);
  2889. #endif
  2890. sconn.sconn_port = 0;
  2891. sconn.sconn_addr = addr;
  2892. sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID,
  2893. (struct sockaddr *)&sconn,
  2894. 0xffffffff,
  2895. "conn");
  2896. }
  2897. #define PREAMBLE_FORMAT "\n%c %02d:%02d:%02d.%06ld "
  2898. #define PREAMBLE_LENGTH 19
  2899. #define HEADER "0000 "
  2900. #define TRAILER "# SCTP_PACKET\n"
  2901. char *
  2902. usrsctp_dumppacket(const void *buf, size_t len, int outbound)
  2903. {
  2904. size_t i, pos;
  2905. char *dump_buf, *packet;
  2906. struct tm t;
  2907. #ifdef _WIN32
  2908. struct timeb tb;
  2909. #else
  2910. struct timeval tv;
  2911. time_t sec;
  2912. #endif
  2913. if ((len == 0) || (buf == NULL)) {
  2914. return (NULL);
  2915. }
  2916. if ((dump_buf = malloc(PREAMBLE_LENGTH + strlen(HEADER) + 3 * len + strlen(TRAILER) + 1)) == NULL) {
  2917. return (NULL);
  2918. }
  2919. pos = 0;
  2920. #ifdef _WIN32
  2921. ftime(&tb);
  2922. localtime_s(&t, &tb.time);
  2923. #if defined(__MINGW32__)
  2924. if (snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT,
  2925. outbound ? 'O' : 'I',
  2926. t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm)) < 0) {
  2927. free(dump_buf);
  2928. return (NULL);
  2929. }
  2930. #else
  2931. if (_snprintf_s(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_LENGTH, PREAMBLE_FORMAT,
  2932. outbound ? 'O' : 'I',
  2933. t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm)) < 0) {
  2934. free(dump_buf);
  2935. return (NULL);
  2936. }
  2937. #endif
  2938. #else
  2939. gettimeofday(&tv, NULL);
  2940. sec = (time_t)tv.tv_sec;
  2941. localtime_r((const time_t *)&sec, &t);
  2942. if (snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT,
  2943. outbound ? 'O' : 'I',
  2944. t.tm_hour, t.tm_min, t.tm_sec, (long)tv.tv_usec) < 0) {
  2945. free(dump_buf);
  2946. return (NULL);
  2947. }
  2948. #endif
  2949. pos += PREAMBLE_LENGTH;
  2950. #if defined(_WIN32) && !defined(__MINGW32__)
  2951. strncpy_s(dump_buf + pos, strlen(HEADER) + 1, HEADER, strlen(HEADER));
  2952. #else
  2953. strcpy(dump_buf + pos, HEADER);
  2954. #endif
  2955. pos += strlen(HEADER);
  2956. packet = (char *)buf;
  2957. for (i = 0; i < len; i++) {
  2958. uint8_t byte, low, high;
  2959. byte = (uint8_t)packet[i];
  2960. high = byte / 16;
  2961. low = byte % 16;
  2962. dump_buf[pos++] = high < 10 ? '0' + high : 'a' + (high - 10);
  2963. dump_buf[pos++] = low < 10 ? '0' + low : 'a' + (low - 10);
  2964. dump_buf[pos++] = ' ';
  2965. }
  2966. #if defined(_WIN32) && !defined(__MINGW32__)
  2967. strncpy_s(dump_buf + pos, strlen(TRAILER) + 1, TRAILER, strlen(TRAILER));
  2968. #else
  2969. strcpy(dump_buf + pos, TRAILER);
  2970. #endif
  2971. pos += strlen(TRAILER);
  2972. dump_buf[pos++] = '\0';
  2973. return (dump_buf);
  2974. }
  2975. void
  2976. usrsctp_freedumpbuffer(char *buf)
  2977. {
  2978. free(buf);
  2979. }
  2980. void
  2981. usrsctp_enable_crc32c_offload(void)
  2982. {
  2983. SCTP_BASE_VAR(crc32c_offloaded) = 1;
  2984. }
  2985. void
  2986. usrsctp_disable_crc32c_offload(void)
  2987. {
  2988. SCTP_BASE_VAR(crc32c_offloaded) = 0;
  2989. }
  2990. /* Compute the CRC32C in network byte order */
  2991. uint32_t
  2992. usrsctp_crc32c(void *buffer, size_t length)
  2993. {
  2994. uint32_t base = 0xffffffff;
  2995. base = calculate_crc32c(0xffffffff, (unsigned char *)buffer, (unsigned int) length);
  2996. base = sctp_finalize_crc32c(base);
  2997. return (base);
  2998. }
  2999. void
  3000. usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bits)
  3001. {
  3002. struct sockaddr_conn src, dst;
  3003. struct mbuf *m, *mm;
  3004. struct sctphdr *sh;
  3005. struct sctp_chunkhdr *ch;
  3006. int remaining, offset;
  3007. SCTP_STAT_INCR(sctps_recvpackets);
  3008. SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
  3009. memset(&src, 0, sizeof(struct sockaddr_conn));
  3010. src.sconn_family = AF_CONN;
  3011. #ifdef HAVE_SCONN_LEN
  3012. src.sconn_len = sizeof(struct sockaddr_conn);
  3013. #endif
  3014. src.sconn_addr = addr;
  3015. memset(&dst, 0, sizeof(struct sockaddr_conn));
  3016. dst.sconn_family = AF_CONN;
  3017. #ifdef HAVE_SCONN_LEN
  3018. dst.sconn_len = sizeof(struct sockaddr_conn);
  3019. #endif
  3020. dst.sconn_addr = addr;
  3021. if ((m = sctp_get_mbuf_for_msg((unsigned int)length, 1, M_NOWAIT, 0, MT_DATA)) == NULL) {
  3022. return;
  3023. }
  3024. /* Set the lengths fields of the mbuf chain.
  3025. * This is expected by m_copyback().
  3026. */
  3027. remaining = (int)length;
  3028. for (mm = m; mm != NULL; mm = mm->m_next) {
  3029. mm->m_len = min((int)M_SIZE(mm), remaining);
  3030. m->m_pkthdr.len += mm->m_len;
  3031. remaining -= mm->m_len;
  3032. }
  3033. KASSERT(remaining == 0, ("usrsctp_conninput: %zu bytes left", remaining));
  3034. m_copyback(m, 0, (int)length, (caddr_t)buffer);
  3035. offset = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
  3036. if (SCTP_BUF_LEN(m) < offset) {
  3037. if ((m = m_pullup(m, offset)) == NULL) {
  3038. SCTP_STAT_INCR(sctps_hdrops);
  3039. return;
  3040. }
  3041. }
  3042. sh = mtod(m, struct sctphdr *);
  3043. ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
  3044. offset -= sizeof(struct sctp_chunkhdr);
  3045. src.sconn_port = sh->src_port;
  3046. dst.sconn_port = sh->dest_port;
  3047. sctp_common_input_processing(&m, 0, offset, (int)length,
  3048. (struct sockaddr *)&src,
  3049. (struct sockaddr *)&dst,
  3050. sh, ch,
  3051. SCTP_BASE_VAR(crc32c_offloaded) == 1 ? 0 : 1,
  3052. ecn_bits,
  3053. SCTP_DEFAULT_VRFID, 0);
  3054. if (m) {
  3055. sctp_m_freem(m);
  3056. }
  3057. return;
  3058. }
  3059. void usrsctp_handle_timers(uint32_t elapsed_milliseconds)
  3060. {
  3061. sctp_handle_tick(sctp_msecs_to_ticks(elapsed_milliseconds));
  3062. }
  3063. int
  3064. usrsctp_get_events(struct socket *so)
  3065. {
  3066. int events = 0;
  3067. if (so == NULL) {
  3068. errno = EBADF;
  3069. return -1;
  3070. }
  3071. SOCK_LOCK(so);
  3072. if (soreadable(so)) {
  3073. events |= SCTP_EVENT_READ;
  3074. }
  3075. if (sowriteable(so)) {
  3076. events |= SCTP_EVENT_WRITE;
  3077. }
  3078. if (so->so_error) {
  3079. events |= SCTP_EVENT_ERROR;
  3080. }
  3081. SOCK_UNLOCK(so);
  3082. return events;
  3083. }
  3084. int
  3085. usrsctp_set_upcall(struct socket *so, void (*upcall)(struct socket *, void *, int), void *arg)
  3086. {
  3087. if (so == NULL) {
  3088. errno = EBADF;
  3089. return (-1);
  3090. }
  3091. SOCK_LOCK(so);
  3092. so->so_upcall = upcall;
  3093. so->so_upcallarg = arg;
  3094. so->so_snd.sb_flags |= SB_UPCALL;
  3095. so->so_rcv.sb_flags |= SB_UPCALL;
  3096. SOCK_UNLOCK(so);
  3097. return (0);
  3098. }
  3099. #define USRSCTP_TUNABLE_SET_DEF(__field, __prefix) \
  3100. int usrsctp_tunable_set_ ## __field(uint32_t value) \
  3101. { \
  3102. if ((value < __prefix##_MIN) || \
  3103. (value > __prefix##_MAX)) { \
  3104. errno = EINVAL; \
  3105. return (-1); \
  3106. } else { \
  3107. SCTP_BASE_SYSCTL(__field) = value; \
  3108. return (0); \
  3109. } \
  3110. }
  3111. USRSCTP_TUNABLE_SET_DEF(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE)
  3112. USRSCTP_TUNABLE_SET_DEF(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE)
  3113. USRSCTP_TUNABLE_SET_DEF(sctp_chunkscale, SCTPCTL_CHUNKSCALE)
  3114. #define USRSCTP_SYSCTL_SET_DEF(__field, __prefix) \
  3115. int usrsctp_sysctl_set_ ## __field(uint32_t value) \
  3116. { \
  3117. if ((value < __prefix##_MIN) || \
  3118. (value > __prefix##_MAX)) { \
  3119. errno = EINVAL; \
  3120. return (-1); \
  3121. } else { \
  3122. SCTP_BASE_SYSCTL(__field) = value; \
  3123. return (0); \
  3124. } \
  3125. }
  3126. #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
  3127. #pragma GCC diagnostic push
  3128. #pragma GCC diagnostic ignored "-Wtype-limits"
  3129. #endif
  3130. USRSCTP_SYSCTL_SET_DEF(sctp_sendspace, SCTPCTL_MAXDGRAM)
  3131. USRSCTP_SYSCTL_SET_DEF(sctp_recvspace, SCTPCTL_RECVSPACE)
  3132. USRSCTP_SYSCTL_SET_DEF(sctp_auto_asconf, SCTPCTL_AUTOASCONF)
  3133. USRSCTP_SYSCTL_SET_DEF(sctp_ecn_enable, SCTPCTL_ECN_ENABLE)
  3134. USRSCTP_SYSCTL_SET_DEF(sctp_pr_enable, SCTPCTL_PR_ENABLE)
  3135. USRSCTP_SYSCTL_SET_DEF(sctp_auth_enable, SCTPCTL_AUTH_ENABLE)
  3136. USRSCTP_SYSCTL_SET_DEF(sctp_asconf_enable, SCTPCTL_ASCONF_ENABLE)
  3137. USRSCTP_SYSCTL_SET_DEF(sctp_reconfig_enable, SCTPCTL_RECONFIG_ENABLE)
  3138. USRSCTP_SYSCTL_SET_DEF(sctp_nrsack_enable, SCTPCTL_NRSACK_ENABLE)
  3139. USRSCTP_SYSCTL_SET_DEF(sctp_pktdrop_enable, SCTPCTL_PKTDROP_ENABLE)
  3140. USRSCTP_SYSCTL_SET_DEF(sctp_no_csum_on_loopback, SCTPCTL_LOOPBACK_NOCSUM)
  3141. USRSCTP_SYSCTL_SET_DEF(sctp_peer_chunk_oh, SCTPCTL_PEER_CHKOH)
  3142. USRSCTP_SYSCTL_SET_DEF(sctp_max_burst_default, SCTPCTL_MAXBURST)
  3143. USRSCTP_SYSCTL_SET_DEF(sctp_max_chunks_on_queue, SCTPCTL_MAXCHUNKS)
  3144. USRSCTP_SYSCTL_SET_DEF(sctp_min_split_point, SCTPCTL_MIN_SPLIT_POINT)
  3145. USRSCTP_SYSCTL_SET_DEF(sctp_delayed_sack_time_default, SCTPCTL_DELAYED_SACK_TIME)
  3146. USRSCTP_SYSCTL_SET_DEF(sctp_sack_freq_default, SCTPCTL_SACK_FREQ)
  3147. USRSCTP_SYSCTL_SET_DEF(sctp_system_free_resc_limit, SCTPCTL_SYS_RESOURCE)
  3148. USRSCTP_SYSCTL_SET_DEF(sctp_asoc_free_resc_limit, SCTPCTL_ASOC_RESOURCE)
  3149. USRSCTP_SYSCTL_SET_DEF(sctp_heartbeat_interval_default, SCTPCTL_HEARTBEAT_INTERVAL)
  3150. USRSCTP_SYSCTL_SET_DEF(sctp_pmtu_raise_time_default, SCTPCTL_PMTU_RAISE_TIME)
  3151. USRSCTP_SYSCTL_SET_DEF(sctp_shutdown_guard_time_default, SCTPCTL_SHUTDOWN_GUARD_TIME)
  3152. USRSCTP_SYSCTL_SET_DEF(sctp_secret_lifetime_default, SCTPCTL_SECRET_LIFETIME)
  3153. USRSCTP_SYSCTL_SET_DEF(sctp_rto_max_default, SCTPCTL_RTO_MAX)
  3154. USRSCTP_SYSCTL_SET_DEF(sctp_rto_min_default, SCTPCTL_RTO_MIN)
  3155. USRSCTP_SYSCTL_SET_DEF(sctp_rto_initial_default, SCTPCTL_RTO_INITIAL)
  3156. USRSCTP_SYSCTL_SET_DEF(sctp_init_rto_max_default, SCTPCTL_INIT_RTO_MAX)
  3157. USRSCTP_SYSCTL_SET_DEF(sctp_valid_cookie_life_default, SCTPCTL_VALID_COOKIE_LIFE)
  3158. USRSCTP_SYSCTL_SET_DEF(sctp_init_rtx_max_default, SCTPCTL_INIT_RTX_MAX)
  3159. USRSCTP_SYSCTL_SET_DEF(sctp_assoc_rtx_max_default, SCTPCTL_ASSOC_RTX_MAX)
  3160. USRSCTP_SYSCTL_SET_DEF(sctp_path_rtx_max_default, SCTPCTL_PATH_RTX_MAX)
  3161. USRSCTP_SYSCTL_SET_DEF(sctp_add_more_threshold, SCTPCTL_ADD_MORE_ON_OUTPUT)
  3162. USRSCTP_SYSCTL_SET_DEF(sctp_nr_incoming_streams_default, SCTPCTL_INCOMING_STREAMS)
  3163. USRSCTP_SYSCTL_SET_DEF(sctp_nr_outgoing_streams_default, SCTPCTL_OUTGOING_STREAMS)
  3164. USRSCTP_SYSCTL_SET_DEF(sctp_cmt_on_off, SCTPCTL_CMT_ON_OFF)
  3165. USRSCTP_SYSCTL_SET_DEF(sctp_cmt_use_dac, SCTPCTL_CMT_USE_DAC)
  3166. USRSCTP_SYSCTL_SET_DEF(sctp_use_cwnd_based_maxburst, SCTPCTL_CWND_MAXBURST)
  3167. USRSCTP_SYSCTL_SET_DEF(sctp_nat_friendly, SCTPCTL_NAT_FRIENDLY)
  3168. USRSCTP_SYSCTL_SET_DEF(sctp_L2_abc_variable, SCTPCTL_ABC_L_VAR)
  3169. USRSCTP_SYSCTL_SET_DEF(sctp_mbuf_threshold_count, SCTPCTL_MAX_CHAINED_MBUFS)
  3170. USRSCTP_SYSCTL_SET_DEF(sctp_do_drain, SCTPCTL_DO_SCTP_DRAIN)
  3171. USRSCTP_SYSCTL_SET_DEF(sctp_hb_maxburst, SCTPCTL_HB_MAX_BURST)
  3172. USRSCTP_SYSCTL_SET_DEF(sctp_abort_if_one_2_one_hits_limit, SCTPCTL_ABORT_AT_LIMIT)
  3173. USRSCTP_SYSCTL_SET_DEF(sctp_min_residual, SCTPCTL_MIN_RESIDUAL)
  3174. USRSCTP_SYSCTL_SET_DEF(sctp_max_retran_chunk, SCTPCTL_MAX_RETRAN_CHUNK)
  3175. USRSCTP_SYSCTL_SET_DEF(sctp_logging_level, SCTPCTL_LOGGING_LEVEL)
  3176. USRSCTP_SYSCTL_SET_DEF(sctp_default_cc_module, SCTPCTL_DEFAULT_CC_MODULE)
  3177. USRSCTP_SYSCTL_SET_DEF(sctp_default_frag_interleave, SCTPCTL_DEFAULT_FRAG_INTERLEAVE)
  3178. USRSCTP_SYSCTL_SET_DEF(sctp_mobility_base, SCTPCTL_MOBILITY_BASE)
  3179. USRSCTP_SYSCTL_SET_DEF(sctp_mobility_fasthandoff, SCTPCTL_MOBILITY_FASTHANDOFF)
  3180. USRSCTP_SYSCTL_SET_DEF(sctp_inits_include_nat_friendly, SCTPCTL_NAT_FRIENDLY_INITS)
  3181. USRSCTP_SYSCTL_SET_DEF(sctp_udp_tunneling_port, SCTPCTL_UDP_TUNNELING_PORT)
  3182. USRSCTP_SYSCTL_SET_DEF(sctp_enable_sack_immediately, SCTPCTL_SACK_IMMEDIATELY_ENABLE)
  3183. USRSCTP_SYSCTL_SET_DEF(sctp_vtag_time_wait, SCTPCTL_TIME_WAIT)
  3184. USRSCTP_SYSCTL_SET_DEF(sctp_blackhole, SCTPCTL_BLACKHOLE)
  3185. USRSCTP_SYSCTL_SET_DEF(sctp_diag_info_code, SCTPCTL_DIAG_INFO_CODE)
  3186. USRSCTP_SYSCTL_SET_DEF(sctp_fr_max_burst_default, SCTPCTL_FRMAXBURST)
  3187. USRSCTP_SYSCTL_SET_DEF(sctp_path_pf_threshold, SCTPCTL_PATH_PF_THRESHOLD)
  3188. USRSCTP_SYSCTL_SET_DEF(sctp_default_ss_module, SCTPCTL_DEFAULT_SS_MODULE)
  3189. USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_bw, SCTPCTL_RTTVAR_BW)
  3190. USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_rtt, SCTPCTL_RTTVAR_RTT)
  3191. USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_eqret, SCTPCTL_RTTVAR_EQRET)
  3192. USRSCTP_SYSCTL_SET_DEF(sctp_steady_step, SCTPCTL_RTTVAR_STEADYS)
  3193. USRSCTP_SYSCTL_SET_DEF(sctp_use_dccc_ecn, SCTPCTL_RTTVAR_DCCCECN)
  3194. USRSCTP_SYSCTL_SET_DEF(sctp_buffer_splitting, SCTPCTL_BUFFER_SPLITTING)
  3195. USRSCTP_SYSCTL_SET_DEF(sctp_initial_cwnd, SCTPCTL_INITIAL_CWND)
  3196. #ifdef SCTP_DEBUG
  3197. USRSCTP_SYSCTL_SET_DEF(sctp_debug_on, SCTPCTL_DEBUG)
  3198. #endif
  3199. #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
  3200. #pragma GCC diagnostic pop
  3201. #endif
  3202. #define USRSCTP_SYSCTL_GET_DEF(__field) \
  3203. uint32_t usrsctp_sysctl_get_ ## __field(void) { \
  3204. return SCTP_BASE_SYSCTL(__field); \
  3205. }
  3206. USRSCTP_SYSCTL_GET_DEF(sctp_sendspace)
  3207. USRSCTP_SYSCTL_GET_DEF(sctp_recvspace)
  3208. USRSCTP_SYSCTL_GET_DEF(sctp_auto_asconf)
  3209. USRSCTP_SYSCTL_GET_DEF(sctp_multiple_asconfs)
  3210. USRSCTP_SYSCTL_GET_DEF(sctp_ecn_enable)
  3211. USRSCTP_SYSCTL_GET_DEF(sctp_pr_enable)
  3212. USRSCTP_SYSCTL_GET_DEF(sctp_auth_enable)
  3213. USRSCTP_SYSCTL_GET_DEF(sctp_asconf_enable)
  3214. USRSCTP_SYSCTL_GET_DEF(sctp_reconfig_enable)
  3215. USRSCTP_SYSCTL_GET_DEF(sctp_nrsack_enable)
  3216. USRSCTP_SYSCTL_GET_DEF(sctp_pktdrop_enable)
  3217. USRSCTP_SYSCTL_GET_DEF(sctp_no_csum_on_loopback)
  3218. USRSCTP_SYSCTL_GET_DEF(sctp_peer_chunk_oh)
  3219. USRSCTP_SYSCTL_GET_DEF(sctp_max_burst_default)
  3220. USRSCTP_SYSCTL_GET_DEF(sctp_max_chunks_on_queue)
  3221. USRSCTP_SYSCTL_GET_DEF(sctp_hashtblsize)
  3222. USRSCTP_SYSCTL_GET_DEF(sctp_pcbtblsize)
  3223. USRSCTP_SYSCTL_GET_DEF(sctp_min_split_point)
  3224. USRSCTP_SYSCTL_GET_DEF(sctp_chunkscale)
  3225. USRSCTP_SYSCTL_GET_DEF(sctp_delayed_sack_time_default)
  3226. USRSCTP_SYSCTL_GET_DEF(sctp_sack_freq_default)
  3227. USRSCTP_SYSCTL_GET_DEF(sctp_system_free_resc_limit)
  3228. USRSCTP_SYSCTL_GET_DEF(sctp_asoc_free_resc_limit)
  3229. USRSCTP_SYSCTL_GET_DEF(sctp_heartbeat_interval_default)
  3230. USRSCTP_SYSCTL_GET_DEF(sctp_pmtu_raise_time_default)
  3231. USRSCTP_SYSCTL_GET_DEF(sctp_shutdown_guard_time_default)
  3232. USRSCTP_SYSCTL_GET_DEF(sctp_secret_lifetime_default)
  3233. USRSCTP_SYSCTL_GET_DEF(sctp_rto_max_default)
  3234. USRSCTP_SYSCTL_GET_DEF(sctp_rto_min_default)
  3235. USRSCTP_SYSCTL_GET_DEF(sctp_rto_initial_default)
  3236. USRSCTP_SYSCTL_GET_DEF(sctp_init_rto_max_default)
  3237. USRSCTP_SYSCTL_GET_DEF(sctp_valid_cookie_life_default)
  3238. USRSCTP_SYSCTL_GET_DEF(sctp_init_rtx_max_default)
  3239. USRSCTP_SYSCTL_GET_DEF(sctp_assoc_rtx_max_default)
  3240. USRSCTP_SYSCTL_GET_DEF(sctp_path_rtx_max_default)
  3241. USRSCTP_SYSCTL_GET_DEF(sctp_add_more_threshold)
  3242. USRSCTP_SYSCTL_GET_DEF(sctp_nr_incoming_streams_default)
  3243. USRSCTP_SYSCTL_GET_DEF(sctp_nr_outgoing_streams_default)
  3244. USRSCTP_SYSCTL_GET_DEF(sctp_cmt_on_off)
  3245. USRSCTP_SYSCTL_GET_DEF(sctp_cmt_use_dac)
  3246. USRSCTP_SYSCTL_GET_DEF(sctp_use_cwnd_based_maxburst)
  3247. USRSCTP_SYSCTL_GET_DEF(sctp_nat_friendly)
  3248. USRSCTP_SYSCTL_GET_DEF(sctp_L2_abc_variable)
  3249. USRSCTP_SYSCTL_GET_DEF(sctp_mbuf_threshold_count)
  3250. USRSCTP_SYSCTL_GET_DEF(sctp_do_drain)
  3251. USRSCTP_SYSCTL_GET_DEF(sctp_hb_maxburst)
  3252. USRSCTP_SYSCTL_GET_DEF(sctp_abort_if_one_2_one_hits_limit)
  3253. USRSCTP_SYSCTL_GET_DEF(sctp_min_residual)
  3254. USRSCTP_SYSCTL_GET_DEF(sctp_max_retran_chunk)
  3255. USRSCTP_SYSCTL_GET_DEF(sctp_logging_level)
  3256. USRSCTP_SYSCTL_GET_DEF(sctp_default_cc_module)
  3257. USRSCTP_SYSCTL_GET_DEF(sctp_default_frag_interleave)
  3258. USRSCTP_SYSCTL_GET_DEF(sctp_mobility_base)
  3259. USRSCTP_SYSCTL_GET_DEF(sctp_mobility_fasthandoff)
  3260. USRSCTP_SYSCTL_GET_DEF(sctp_inits_include_nat_friendly)
  3261. USRSCTP_SYSCTL_GET_DEF(sctp_udp_tunneling_port)
  3262. USRSCTP_SYSCTL_GET_DEF(sctp_enable_sack_immediately)
  3263. USRSCTP_SYSCTL_GET_DEF(sctp_vtag_time_wait)
  3264. USRSCTP_SYSCTL_GET_DEF(sctp_blackhole)
  3265. USRSCTP_SYSCTL_GET_DEF(sctp_diag_info_code)
  3266. USRSCTP_SYSCTL_GET_DEF(sctp_fr_max_burst_default)
  3267. USRSCTP_SYSCTL_GET_DEF(sctp_path_pf_threshold)
  3268. USRSCTP_SYSCTL_GET_DEF(sctp_default_ss_module)
  3269. USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_bw)
  3270. USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_rtt)
  3271. USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_eqret)
  3272. USRSCTP_SYSCTL_GET_DEF(sctp_steady_step)
  3273. USRSCTP_SYSCTL_GET_DEF(sctp_use_dccc_ecn)
  3274. USRSCTP_SYSCTL_GET_DEF(sctp_buffer_splitting)
  3275. USRSCTP_SYSCTL_GET_DEF(sctp_initial_cwnd)
  3276. #ifdef SCTP_DEBUG
  3277. USRSCTP_SYSCTL_GET_DEF(sctp_debug_on)
  3278. #endif
  3279. void usrsctp_get_stat(struct sctpstat *stat)
  3280. {
  3281. *stat = SCTP_BASE_STATS;
  3282. }