socket.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. * Copyright (c) 2001-2002 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel reference Implementation
  10. *
  11. * These functions interface with the sockets layer to implement the
  12. * SCTP Extensions for the Sockets API.
  13. *
  14. * Note that the descriptions from the specification are USER level
  15. * functions--this file is the functions which populate the struct proto
  16. * for SCTP which is the BOTTOM of the sockets interface.
  17. *
  18. * The SCTP reference implementation is free software;
  19. * you can redistribute it and/or modify it under the terms of
  20. * the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * The SCTP reference implementation is distributed in the hope that it
  25. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  26. * ************************
  27. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. * See the GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with GNU CC; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 59 Temple Place - Suite 330,
  33. * Boston, MA 02111-1307, USA.
  34. *
  35. * Please send any bug reports or fixes you make to the
  36. * email address(es):
  37. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  38. *
  39. * Or submit a bug report through the following website:
  40. * http://www.sf.net/projects/lksctp
  41. *
  42. * Written or modified by:
  43. * La Monte H.P. Yarroll <piggy@acm.org>
  44. * Narasimha Budihal <narsi@refcode.org>
  45. * Karl Knutson <karl@athena.chicago.il.us>
  46. * Jon Grimm <jgrimm@us.ibm.com>
  47. * Xingang Guo <xingang.guo@intel.com>
  48. * Daisy Chang <daisyc@us.ibm.com>
  49. * Sridhar Samudrala <samudrala@us.ibm.com>
  50. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  51. * Ardelle Fan <ardelle.fan@intel.com>
  52. * Ryan Layer <rmlayer@us.ibm.com>
  53. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  54. * Kevin Gao <kevin.gao@intel.com>
  55. *
  56. * Any bugs reported given to us we will try to fix... any fixes shared will
  57. * be incorporated into the next SCTP release.
  58. */
  59. #include <linux/config.h>
  60. #include <linux/types.h>
  61. #include <linux/kernel.h>
  62. #include <linux/wait.h>
  63. #include <linux/time.h>
  64. #include <linux/ip.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <linux/crypto.h>
  69. #include <net/ip.h>
  70. #include <net/icmp.h>
  71. #include <net/route.h>
  72. #include <net/ipv6.h>
  73. #include <net/inet_common.h>
  74. #include <linux/socket.h> /* for sa_family_t */
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. /* WARNING: Please do not remove the SCTP_STATIC attribute to
  79. * any of the functions below as they are used to export functions
  80. * used by a project regression testsuite.
  81. */
  82. /* Forward declarations for internal helper functions. */
  83. static int sctp_writeable(struct sock *sk);
  84. static void sctp_wfree(struct sk_buff *skb);
  85. static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
  86. size_t msg_len);
  87. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
  88. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  89. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  90. static void sctp_wait_for_close(struct sock *sk, long timeo);
  91. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  92. union sctp_addr *addr, int len);
  93. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  94. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf(struct sctp_association *asoc,
  98. struct sctp_chunk *chunk);
  99. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  100. static int sctp_autobind(struct sock *sk);
  101. static void sctp_sock_migrate(struct sock *, struct sock *,
  102. struct sctp_association *, sctp_socket_type_t);
  103. static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
  104. extern kmem_cache_t *sctp_bucket_cachep;
  105. /* Get the sndbuf space available at the time on the association. */
  106. static inline int sctp_wspace(struct sctp_association *asoc)
  107. {
  108. struct sock *sk = asoc->base.sk;
  109. int amt = 0;
  110. if (asoc->ep->sndbuf_policy) {
  111. /* make sure that no association uses more than sk_sndbuf */
  112. amt = sk->sk_sndbuf - asoc->sndbuf_used;
  113. } else {
  114. /* do socket level accounting */
  115. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  116. }
  117. if (amt < 0)
  118. amt = 0;
  119. return amt;
  120. }
  121. /* Increment the used sndbuf space count of the corresponding association by
  122. * the size of the outgoing data chunk.
  123. * Also, set the skb destructor for sndbuf accounting later.
  124. *
  125. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  126. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  127. * destructor in the data chunk skb for the purpose of the sndbuf space
  128. * tracking.
  129. */
  130. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  131. {
  132. struct sctp_association *asoc = chunk->asoc;
  133. struct sock *sk = asoc->base.sk;
  134. /* The sndbuf space is tracked per association. */
  135. sctp_association_hold(asoc);
  136. skb_set_owner_w(chunk->skb, sk);
  137. chunk->skb->destructor = sctp_wfree;
  138. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  139. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  140. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  141. sizeof(struct sk_buff) +
  142. sizeof(struct sctp_chunk);
  143. sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
  144. sizeof(struct sk_buff) +
  145. sizeof(struct sctp_chunk);
  146. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  147. }
  148. /* Verify that this is a valid address. */
  149. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  150. int len)
  151. {
  152. struct sctp_af *af;
  153. /* Verify basic sockaddr. */
  154. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  155. if (!af)
  156. return -EINVAL;
  157. /* Is this a valid SCTP address? */
  158. if (!af->addr_valid(addr, sctp_sk(sk)))
  159. return -EINVAL;
  160. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  161. return -EINVAL;
  162. return 0;
  163. }
  164. /* Look up the association by its id. If this is not a UDP-style
  165. * socket, the ID field is always ignored.
  166. */
  167. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  168. {
  169. struct sctp_association *asoc = NULL;
  170. /* If this is not a UDP-style socket, assoc id should be ignored. */
  171. if (!sctp_style(sk, UDP)) {
  172. /* Return NULL if the socket state is not ESTABLISHED. It
  173. * could be a TCP-style listening socket or a socket which
  174. * hasn't yet called connect() to establish an association.
  175. */
  176. if (!sctp_sstate(sk, ESTABLISHED))
  177. return NULL;
  178. /* Get the first and the only association from the list. */
  179. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  180. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  181. struct sctp_association, asocs);
  182. return asoc;
  183. }
  184. /* Otherwise this is a UDP-style socket. */
  185. if (!id || (id == (sctp_assoc_t)-1))
  186. return NULL;
  187. spin_lock_bh(&sctp_assocs_id_lock);
  188. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  189. spin_unlock_bh(&sctp_assocs_id_lock);
  190. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  191. return NULL;
  192. return asoc;
  193. }
  194. /* Look up the transport from an address and an assoc id. If both address and
  195. * id are specified, the associations matching the address and the id should be
  196. * the same.
  197. */
  198. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  199. struct sockaddr_storage *addr,
  200. sctp_assoc_t id)
  201. {
  202. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  203. struct sctp_transport *transport;
  204. union sctp_addr *laddr = (union sctp_addr *)addr;
  205. laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
  206. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  207. (union sctp_addr *)addr,
  208. &transport);
  209. laddr->v4.sin_port = htons(laddr->v4.sin_port);
  210. if (!addr_asoc)
  211. return NULL;
  212. id_asoc = sctp_id2assoc(sk, id);
  213. if (id_asoc && (id_asoc != addr_asoc))
  214. return NULL;
  215. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  216. (union sctp_addr *)addr);
  217. return transport;
  218. }
  219. /* API 3.1.2 bind() - UDP Style Syntax
  220. * The syntax of bind() is,
  221. *
  222. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  223. *
  224. * sd - the socket descriptor returned by socket().
  225. * addr - the address structure (struct sockaddr_in or struct
  226. * sockaddr_in6 [RFC 2553]),
  227. * addr_len - the size of the address structure.
  228. */
  229. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  230. {
  231. int retval = 0;
  232. sctp_lock_sock(sk);
  233. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
  234. sk, uaddr, addr_len);
  235. /* Disallow binding twice. */
  236. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  237. retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
  238. addr_len);
  239. else
  240. retval = -EINVAL;
  241. sctp_release_sock(sk);
  242. return retval;
  243. }
  244. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  245. /* Verify this is a valid sockaddr. */
  246. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  247. union sctp_addr *addr, int len)
  248. {
  249. struct sctp_af *af;
  250. /* Check minimum size. */
  251. if (len < sizeof (struct sockaddr))
  252. return NULL;
  253. /* Does this PF support this AF? */
  254. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  255. return NULL;
  256. /* If we get this far, af is valid. */
  257. af = sctp_get_af_specific(addr->sa.sa_family);
  258. if (len < af->sockaddr_len)
  259. return NULL;
  260. return af;
  261. }
  262. /* Bind a local address either to an endpoint or to an association. */
  263. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  264. {
  265. struct sctp_sock *sp = sctp_sk(sk);
  266. struct sctp_endpoint *ep = sp->ep;
  267. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  268. struct sctp_af *af;
  269. unsigned short snum;
  270. int ret = 0;
  271. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
  272. sk, addr, len);
  273. /* Common sockaddr verification. */
  274. af = sctp_sockaddr_af(sp, addr, len);
  275. if (!af)
  276. return -EINVAL;
  277. /* PF specific bind() address verification. */
  278. if (!sp->pf->bind_verify(sp, addr))
  279. return -EADDRNOTAVAIL;
  280. snum= ntohs(addr->v4.sin_port);
  281. SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
  282. bp->port, snum);
  283. /* We must either be unbound, or bind to the same port. */
  284. if (bp->port && (snum != bp->port)) {
  285. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  286. " New port %d does not match existing port "
  287. "%d.\n", snum, bp->port);
  288. return -EINVAL;
  289. }
  290. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  291. return -EACCES;
  292. /* Make sure we are allowed to bind here.
  293. * The function sctp_get_port_local() does duplicate address
  294. * detection.
  295. */
  296. if ((ret = sctp_get_port_local(sk, addr))) {
  297. if (ret == (long) sk) {
  298. /* This endpoint has a conflicting address. */
  299. return -EINVAL;
  300. } else {
  301. return -EADDRINUSE;
  302. }
  303. }
  304. /* Refresh ephemeral port. */
  305. if (!bp->port)
  306. bp->port = inet_sk(sk)->num;
  307. /* Add the address to the bind address list. */
  308. sctp_local_bh_disable();
  309. sctp_write_lock(&ep->base.addr_lock);
  310. /* Use GFP_ATOMIC since BHs are disabled. */
  311. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  312. ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
  313. addr->v4.sin_port = htons(addr->v4.sin_port);
  314. sctp_write_unlock(&ep->base.addr_lock);
  315. sctp_local_bh_enable();
  316. /* Copy back into socket for getsockname() use. */
  317. if (!ret) {
  318. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  319. af->to_sk_saddr(addr, sk);
  320. }
  321. return ret;
  322. }
  323. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  324. *
  325. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  326. * at any one time. If a sender, after sending an ASCONF chunk, decides
  327. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  328. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  329. * subsequent ASCONF. Note this restriction binds each side, so at any
  330. * time two ASCONF may be in-transit on any given association (one sent
  331. * from each endpoint).
  332. */
  333. static int sctp_send_asconf(struct sctp_association *asoc,
  334. struct sctp_chunk *chunk)
  335. {
  336. int retval = 0;
  337. /* If there is an outstanding ASCONF chunk, queue it for later
  338. * transmission.
  339. */
  340. if (asoc->addip_last_asconf) {
  341. __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
  342. goto out;
  343. }
  344. /* Hold the chunk until an ASCONF_ACK is received. */
  345. sctp_chunk_hold(chunk);
  346. retval = sctp_primitive_ASCONF(asoc, chunk);
  347. if (retval)
  348. sctp_chunk_free(chunk);
  349. else
  350. asoc->addip_last_asconf = chunk;
  351. out:
  352. return retval;
  353. }
  354. /* Add a list of addresses as bind addresses to local endpoint or
  355. * association.
  356. *
  357. * Basically run through each address specified in the addrs/addrcnt
  358. * array/length pair, determine if it is IPv6 or IPv4 and call
  359. * sctp_do_bind() on it.
  360. *
  361. * If any of them fails, then the operation will be reversed and the
  362. * ones that were added will be removed.
  363. *
  364. * Only sctp_setsockopt_bindx() is supposed to call this function.
  365. */
  366. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  367. {
  368. int cnt;
  369. int retval = 0;
  370. void *addr_buf;
  371. struct sockaddr *sa_addr;
  372. struct sctp_af *af;
  373. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  374. sk, addrs, addrcnt);
  375. addr_buf = addrs;
  376. for (cnt = 0; cnt < addrcnt; cnt++) {
  377. /* The list may contain either IPv4 or IPv6 address;
  378. * determine the address length for walking thru the list.
  379. */
  380. sa_addr = (struct sockaddr *)addr_buf;
  381. af = sctp_get_af_specific(sa_addr->sa_family);
  382. if (!af) {
  383. retval = -EINVAL;
  384. goto err_bindx_add;
  385. }
  386. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  387. af->sockaddr_len);
  388. addr_buf += af->sockaddr_len;
  389. err_bindx_add:
  390. if (retval < 0) {
  391. /* Failed. Cleanup the ones that have been added */
  392. if (cnt > 0)
  393. sctp_bindx_rem(sk, addrs, cnt);
  394. return retval;
  395. }
  396. }
  397. return retval;
  398. }
  399. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  400. * associations that are part of the endpoint indicating that a list of local
  401. * addresses are added to the endpoint.
  402. *
  403. * If any of the addresses is already in the bind address list of the
  404. * association, we do not send the chunk for that association. But it will not
  405. * affect other associations.
  406. *
  407. * Only sctp_setsockopt_bindx() is supposed to call this function.
  408. */
  409. static int sctp_send_asconf_add_ip(struct sock *sk,
  410. struct sockaddr *addrs,
  411. int addrcnt)
  412. {
  413. struct sctp_sock *sp;
  414. struct sctp_endpoint *ep;
  415. struct sctp_association *asoc;
  416. struct sctp_bind_addr *bp;
  417. struct sctp_chunk *chunk;
  418. struct sctp_sockaddr_entry *laddr;
  419. union sctp_addr *addr;
  420. void *addr_buf;
  421. struct sctp_af *af;
  422. struct list_head *pos;
  423. struct list_head *p;
  424. int i;
  425. int retval = 0;
  426. if (!sctp_addip_enable)
  427. return retval;
  428. sp = sctp_sk(sk);
  429. ep = sp->ep;
  430. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  431. __FUNCTION__, sk, addrs, addrcnt);
  432. list_for_each(pos, &ep->asocs) {
  433. asoc = list_entry(pos, struct sctp_association, asocs);
  434. if (!asoc->peer.asconf_capable)
  435. continue;
  436. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  437. continue;
  438. if (!sctp_state(asoc, ESTABLISHED))
  439. continue;
  440. /* Check if any address in the packed array of addresses is
  441. * in the bind address list of the association. If so,
  442. * do not send the asconf chunk to its peer, but continue with
  443. * other associations.
  444. */
  445. addr_buf = addrs;
  446. for (i = 0; i < addrcnt; i++) {
  447. addr = (union sctp_addr *)addr_buf;
  448. af = sctp_get_af_specific(addr->v4.sin_family);
  449. if (!af) {
  450. retval = -EINVAL;
  451. goto out;
  452. }
  453. if (sctp_assoc_lookup_laddr(asoc, addr))
  454. break;
  455. addr_buf += af->sockaddr_len;
  456. }
  457. if (i < addrcnt)
  458. continue;
  459. /* Use the first address in bind addr list of association as
  460. * Address Parameter of ASCONF CHUNK.
  461. */
  462. sctp_read_lock(&asoc->base.addr_lock);
  463. bp = &asoc->base.bind_addr;
  464. p = bp->address_list.next;
  465. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  466. sctp_read_unlock(&asoc->base.addr_lock);
  467. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  468. addrcnt, SCTP_PARAM_ADD_IP);
  469. if (!chunk) {
  470. retval = -ENOMEM;
  471. goto out;
  472. }
  473. retval = sctp_send_asconf(asoc, chunk);
  474. /* FIXME: After sending the add address ASCONF chunk, we
  475. * cannot append the address to the association's binding
  476. * address list, because the new address may be used as the
  477. * source of a message sent to the peer before the ASCONF
  478. * chunk is received by the peer. So we should wait until
  479. * ASCONF_ACK is received.
  480. */
  481. }
  482. out:
  483. return retval;
  484. }
  485. /* Remove a list of addresses from bind addresses list. Do not remove the
  486. * last address.
  487. *
  488. * Basically run through each address specified in the addrs/addrcnt
  489. * array/length pair, determine if it is IPv6 or IPv4 and call
  490. * sctp_del_bind() on it.
  491. *
  492. * If any of them fails, then the operation will be reversed and the
  493. * ones that were removed will be added back.
  494. *
  495. * At least one address has to be left; if only one address is
  496. * available, the operation will return -EBUSY.
  497. *
  498. * Only sctp_setsockopt_bindx() is supposed to call this function.
  499. */
  500. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  501. {
  502. struct sctp_sock *sp = sctp_sk(sk);
  503. struct sctp_endpoint *ep = sp->ep;
  504. int cnt;
  505. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  506. int retval = 0;
  507. union sctp_addr saveaddr;
  508. void *addr_buf;
  509. struct sockaddr *sa_addr;
  510. struct sctp_af *af;
  511. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  512. sk, addrs, addrcnt);
  513. addr_buf = addrs;
  514. for (cnt = 0; cnt < addrcnt; cnt++) {
  515. /* If the bind address list is empty or if there is only one
  516. * bind address, there is nothing more to be removed (we need
  517. * at least one address here).
  518. */
  519. if (list_empty(&bp->address_list) ||
  520. (sctp_list_single_entry(&bp->address_list))) {
  521. retval = -EBUSY;
  522. goto err_bindx_rem;
  523. }
  524. /* The list may contain either IPv4 or IPv6 address;
  525. * determine the address length to copy the address to
  526. * saveaddr.
  527. */
  528. sa_addr = (struct sockaddr *)addr_buf;
  529. af = sctp_get_af_specific(sa_addr->sa_family);
  530. if (!af) {
  531. retval = -EINVAL;
  532. goto err_bindx_rem;
  533. }
  534. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  535. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  536. if (saveaddr.v4.sin_port != bp->port) {
  537. retval = -EINVAL;
  538. goto err_bindx_rem;
  539. }
  540. /* FIXME - There is probably a need to check if sk->sk_saddr and
  541. * sk->sk_rcv_addr are currently set to one of the addresses to
  542. * be removed. This is something which needs to be looked into
  543. * when we are fixing the outstanding issues with multi-homing
  544. * socket routing and failover schemes. Refer to comments in
  545. * sctp_do_bind(). -daisy
  546. */
  547. sctp_local_bh_disable();
  548. sctp_write_lock(&ep->base.addr_lock);
  549. retval = sctp_del_bind_addr(bp, &saveaddr);
  550. sctp_write_unlock(&ep->base.addr_lock);
  551. sctp_local_bh_enable();
  552. addr_buf += af->sockaddr_len;
  553. err_bindx_rem:
  554. if (retval < 0) {
  555. /* Failed. Add the ones that has been removed back */
  556. if (cnt > 0)
  557. sctp_bindx_add(sk, addrs, cnt);
  558. return retval;
  559. }
  560. }
  561. return retval;
  562. }
  563. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  564. * the associations that are part of the endpoint indicating that a list of
  565. * local addresses are removed from the endpoint.
  566. *
  567. * If any of the addresses is already in the bind address list of the
  568. * association, we do not send the chunk for that association. But it will not
  569. * affect other associations.
  570. *
  571. * Only sctp_setsockopt_bindx() is supposed to call this function.
  572. */
  573. static int sctp_send_asconf_del_ip(struct sock *sk,
  574. struct sockaddr *addrs,
  575. int addrcnt)
  576. {
  577. struct sctp_sock *sp;
  578. struct sctp_endpoint *ep;
  579. struct sctp_association *asoc;
  580. struct sctp_bind_addr *bp;
  581. struct sctp_chunk *chunk;
  582. union sctp_addr *laddr;
  583. void *addr_buf;
  584. struct sctp_af *af;
  585. struct list_head *pos;
  586. int i;
  587. int retval = 0;
  588. if (!sctp_addip_enable)
  589. return retval;
  590. sp = sctp_sk(sk);
  591. ep = sp->ep;
  592. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  593. __FUNCTION__, sk, addrs, addrcnt);
  594. list_for_each(pos, &ep->asocs) {
  595. asoc = list_entry(pos, struct sctp_association, asocs);
  596. if (!asoc->peer.asconf_capable)
  597. continue;
  598. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  599. continue;
  600. if (!sctp_state(asoc, ESTABLISHED))
  601. continue;
  602. /* Check if any address in the packed array of addresses is
  603. * not present in the bind address list of the association.
  604. * If so, do not send the asconf chunk to its peer, but
  605. * continue with other associations.
  606. */
  607. addr_buf = addrs;
  608. for (i = 0; i < addrcnt; i++) {
  609. laddr = (union sctp_addr *)addr_buf;
  610. af = sctp_get_af_specific(laddr->v4.sin_family);
  611. if (!af) {
  612. retval = -EINVAL;
  613. goto out;
  614. }
  615. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  616. break;
  617. addr_buf += af->sockaddr_len;
  618. }
  619. if (i < addrcnt)
  620. continue;
  621. /* Find one address in the association's bind address list
  622. * that is not in the packed array of addresses. This is to
  623. * make sure that we do not delete all the addresses in the
  624. * association.
  625. */
  626. sctp_read_lock(&asoc->base.addr_lock);
  627. bp = &asoc->base.bind_addr;
  628. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  629. addrcnt, sp);
  630. sctp_read_unlock(&asoc->base.addr_lock);
  631. if (!laddr)
  632. continue;
  633. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  634. SCTP_PARAM_DEL_IP);
  635. if (!chunk) {
  636. retval = -ENOMEM;
  637. goto out;
  638. }
  639. retval = sctp_send_asconf(asoc, chunk);
  640. /* FIXME: After sending the delete address ASCONF chunk, we
  641. * cannot remove the addresses from the association's bind
  642. * address list, because there maybe some packet send to
  643. * the delete addresses, so we should wait until ASCONF_ACK
  644. * packet is received.
  645. */
  646. }
  647. out:
  648. return retval;
  649. }
  650. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  651. *
  652. * API 8.1
  653. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  654. * int flags);
  655. *
  656. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  657. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  658. * or IPv6 addresses.
  659. *
  660. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  661. * Section 3.1.2 for this usage.
  662. *
  663. * addrs is a pointer to an array of one or more socket addresses. Each
  664. * address is contained in its appropriate structure (i.e. struct
  665. * sockaddr_in or struct sockaddr_in6) the family of the address type
  666. * must be used to distengish the address length (note that this
  667. * representation is termed a "packed array" of addresses). The caller
  668. * specifies the number of addresses in the array with addrcnt.
  669. *
  670. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  671. * -1, and sets errno to the appropriate error code.
  672. *
  673. * For SCTP, the port given in each socket address must be the same, or
  674. * sctp_bindx() will fail, setting errno to EINVAL.
  675. *
  676. * The flags parameter is formed from the bitwise OR of zero or more of
  677. * the following currently defined flags:
  678. *
  679. * SCTP_BINDX_ADD_ADDR
  680. *
  681. * SCTP_BINDX_REM_ADDR
  682. *
  683. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  684. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  685. * addresses from the association. The two flags are mutually exclusive;
  686. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  687. * not remove all addresses from an association; sctp_bindx() will
  688. * reject such an attempt with EINVAL.
  689. *
  690. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  691. * additional addresses with an endpoint after calling bind(). Or use
  692. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  693. * socket is associated with so that no new association accepted will be
  694. * associated with those addresses. If the endpoint supports dynamic
  695. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  696. * endpoint to send the appropriate message to the peer to change the
  697. * peers address lists.
  698. *
  699. * Adding and removing addresses from a connected association is
  700. * optional functionality. Implementations that do not support this
  701. * functionality should return EOPNOTSUPP.
  702. *
  703. * Basically do nothing but copying the addresses from user to kernel
  704. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  705. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
  706. *
  707. * We don't use copy_from_user() for optimization: we first do the
  708. * sanity checks (buffer size -fast- and access check-healthy
  709. * pointer); if all of those succeed, then we can alloc the memory
  710. * (expensive operation) needed to copy the data to kernel. Then we do
  711. * the copying without checking the user space area
  712. * (__copy_from_user()).
  713. *
  714. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  715. * it.
  716. *
  717. * sk The sk of the socket
  718. * addrs The pointer to the addresses in user land
  719. * addrssize Size of the addrs buffer
  720. * op Operation to perform (add or remove, see the flags of
  721. * sctp_bindx)
  722. *
  723. * Returns 0 if ok, <0 errno code on error.
  724. */
  725. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  726. struct sockaddr __user *addrs,
  727. int addrs_size, int op)
  728. {
  729. struct sockaddr *kaddrs;
  730. int err;
  731. int addrcnt = 0;
  732. int walk_size = 0;
  733. struct sockaddr *sa_addr;
  734. void *addr_buf;
  735. struct sctp_af *af;
  736. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  737. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  738. if (unlikely(addrs_size <= 0))
  739. return -EINVAL;
  740. /* Check the user passed a healthy pointer. */
  741. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  742. return -EFAULT;
  743. /* Alloc space for the address array in kernel memory. */
  744. kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
  745. if (unlikely(!kaddrs))
  746. return -ENOMEM;
  747. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  748. kfree(kaddrs);
  749. return -EFAULT;
  750. }
  751. /* Walk through the addrs buffer and count the number of addresses. */
  752. addr_buf = kaddrs;
  753. while (walk_size < addrs_size) {
  754. sa_addr = (struct sockaddr *)addr_buf;
  755. af = sctp_get_af_specific(sa_addr->sa_family);
  756. /* If the address family is not supported or if this address
  757. * causes the address buffer to overflow return EINVAL.
  758. */
  759. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  760. kfree(kaddrs);
  761. return -EINVAL;
  762. }
  763. addrcnt++;
  764. addr_buf += af->sockaddr_len;
  765. walk_size += af->sockaddr_len;
  766. }
  767. /* Do the work. */
  768. switch (op) {
  769. case SCTP_BINDX_ADD_ADDR:
  770. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  771. if (err)
  772. goto out;
  773. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  774. break;
  775. case SCTP_BINDX_REM_ADDR:
  776. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  777. if (err)
  778. goto out;
  779. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  780. break;
  781. default:
  782. err = -EINVAL;
  783. break;
  784. };
  785. out:
  786. kfree(kaddrs);
  787. return err;
  788. }
  789. /* API 3.1.4 close() - UDP Style Syntax
  790. * Applications use close() to perform graceful shutdown (as described in
  791. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  792. * by a UDP-style socket.
  793. *
  794. * The syntax is
  795. *
  796. * ret = close(int sd);
  797. *
  798. * sd - the socket descriptor of the associations to be closed.
  799. *
  800. * To gracefully shutdown a specific association represented by the
  801. * UDP-style socket, an application should use the sendmsg() call,
  802. * passing no user data, but including the appropriate flag in the
  803. * ancillary data (see Section xxxx).
  804. *
  805. * If sd in the close() call is a branched-off socket representing only
  806. * one association, the shutdown is performed on that association only.
  807. *
  808. * 4.1.6 close() - TCP Style Syntax
  809. *
  810. * Applications use close() to gracefully close down an association.
  811. *
  812. * The syntax is:
  813. *
  814. * int close(int sd);
  815. *
  816. * sd - the socket descriptor of the association to be closed.
  817. *
  818. * After an application calls close() on a socket descriptor, no further
  819. * socket operations will succeed on that descriptor.
  820. *
  821. * API 7.1.4 SO_LINGER
  822. *
  823. * An application using the TCP-style socket can use this option to
  824. * perform the SCTP ABORT primitive. The linger option structure is:
  825. *
  826. * struct linger {
  827. * int l_onoff; // option on/off
  828. * int l_linger; // linger time
  829. * };
  830. *
  831. * To enable the option, set l_onoff to 1. If the l_linger value is set
  832. * to 0, calling close() is the same as the ABORT primitive. If the
  833. * value is set to a negative value, the setsockopt() call will return
  834. * an error. If the value is set to a positive value linger_time, the
  835. * close() can be blocked for at most linger_time ms. If the graceful
  836. * shutdown phase does not finish during this period, close() will
  837. * return but the graceful shutdown phase continues in the system.
  838. */
  839. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  840. {
  841. struct sctp_endpoint *ep;
  842. struct sctp_association *asoc;
  843. struct list_head *pos, *temp;
  844. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  845. sctp_lock_sock(sk);
  846. sk->sk_shutdown = SHUTDOWN_MASK;
  847. ep = sctp_sk(sk)->ep;
  848. /* Walk all associations on a socket, not on an endpoint. */
  849. list_for_each_safe(pos, temp, &ep->asocs) {
  850. asoc = list_entry(pos, struct sctp_association, asocs);
  851. if (sctp_style(sk, TCP)) {
  852. /* A closed association can still be in the list if
  853. * it belongs to a TCP-style listening socket that is
  854. * not yet accepted. If so, free it. If not, send an
  855. * ABORT or SHUTDOWN based on the linger options.
  856. */
  857. if (sctp_state(asoc, CLOSED)) {
  858. sctp_unhash_established(asoc);
  859. sctp_association_free(asoc);
  860. } else if (sock_flag(sk, SOCK_LINGER) &&
  861. !sk->sk_lingertime)
  862. sctp_primitive_ABORT(asoc, NULL);
  863. else
  864. sctp_primitive_SHUTDOWN(asoc, NULL);
  865. } else
  866. sctp_primitive_SHUTDOWN(asoc, NULL);
  867. }
  868. /* Clean up any skbs sitting on the receive queue. */
  869. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  870. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  871. /* On a TCP-style socket, block for at most linger_time if set. */
  872. if (sctp_style(sk, TCP) && timeout)
  873. sctp_wait_for_close(sk, timeout);
  874. /* This will run the backlog queue. */
  875. sctp_release_sock(sk);
  876. /* Supposedly, no process has access to the socket, but
  877. * the net layers still may.
  878. */
  879. sctp_local_bh_disable();
  880. sctp_bh_lock_sock(sk);
  881. /* Hold the sock, since sk_common_release() will put sock_put()
  882. * and we have just a little more cleanup.
  883. */
  884. sock_hold(sk);
  885. sk_common_release(sk);
  886. sctp_bh_unlock_sock(sk);
  887. sctp_local_bh_enable();
  888. sock_put(sk);
  889. SCTP_DBG_OBJCNT_DEC(sock);
  890. }
  891. /* Handle EPIPE error. */
  892. static int sctp_error(struct sock *sk, int flags, int err)
  893. {
  894. if (err == -EPIPE)
  895. err = sock_error(sk) ? : -EPIPE;
  896. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  897. send_sig(SIGPIPE, current, 0);
  898. return err;
  899. }
  900. /* API 3.1.3 sendmsg() - UDP Style Syntax
  901. *
  902. * An application uses sendmsg() and recvmsg() calls to transmit data to
  903. * and receive data from its peer.
  904. *
  905. * ssize_t sendmsg(int socket, const struct msghdr *message,
  906. * int flags);
  907. *
  908. * socket - the socket descriptor of the endpoint.
  909. * message - pointer to the msghdr structure which contains a single
  910. * user message and possibly some ancillary data.
  911. *
  912. * See Section 5 for complete description of the data
  913. * structures.
  914. *
  915. * flags - flags sent or received with the user message, see Section
  916. * 5 for complete description of the flags.
  917. *
  918. * Note: This function could use a rewrite especially when explicit
  919. * connect support comes in.
  920. */
  921. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  922. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  923. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  924. struct msghdr *msg, size_t msg_len)
  925. {
  926. struct sctp_sock *sp;
  927. struct sctp_endpoint *ep;
  928. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  929. struct sctp_transport *transport, *chunk_tp;
  930. struct sctp_chunk *chunk;
  931. union sctp_addr to;
  932. struct sockaddr *msg_name = NULL;
  933. struct sctp_sndrcvinfo default_sinfo = { 0 };
  934. struct sctp_sndrcvinfo *sinfo;
  935. struct sctp_initmsg *sinit;
  936. sctp_assoc_t associd = 0;
  937. sctp_cmsgs_t cmsgs = { NULL };
  938. int err;
  939. sctp_scope_t scope;
  940. long timeo;
  941. __u16 sinfo_flags = 0;
  942. struct sctp_datamsg *datamsg;
  943. struct list_head *pos;
  944. int msg_flags = msg->msg_flags;
  945. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  946. sk, msg, msg_len);
  947. err = 0;
  948. sp = sctp_sk(sk);
  949. ep = sp->ep;
  950. SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
  951. /* We cannot send a message over a TCP-style listening socket. */
  952. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  953. err = -EPIPE;
  954. goto out_nounlock;
  955. }
  956. /* Parse out the SCTP CMSGs. */
  957. err = sctp_msghdr_parse(msg, &cmsgs);
  958. if (err) {
  959. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  960. goto out_nounlock;
  961. }
  962. /* Fetch the destination address for this packet. This
  963. * address only selects the association--it is not necessarily
  964. * the address we will send to.
  965. * For a peeled-off socket, msg_name is ignored.
  966. */
  967. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  968. int msg_namelen = msg->msg_namelen;
  969. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  970. msg_namelen);
  971. if (err)
  972. return err;
  973. if (msg_namelen > sizeof(to))
  974. msg_namelen = sizeof(to);
  975. memcpy(&to, msg->msg_name, msg_namelen);
  976. SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
  977. "0x%x:%u.\n",
  978. to.v4.sin_addr.s_addr, to.v4.sin_port);
  979. to.v4.sin_port = ntohs(to.v4.sin_port);
  980. msg_name = msg->msg_name;
  981. }
  982. sinfo = cmsgs.info;
  983. sinit = cmsgs.init;
  984. /* Did the user specify SNDRCVINFO? */
  985. if (sinfo) {
  986. sinfo_flags = sinfo->sinfo_flags;
  987. associd = sinfo->sinfo_assoc_id;
  988. }
  989. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  990. msg_len, sinfo_flags);
  991. /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
  992. if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
  993. err = -EINVAL;
  994. goto out_nounlock;
  995. }
  996. /* If MSG_EOF is set, no data can be sent. Disallow sending zero
  997. * length messages when MSG_EOF|MSG_ABORT is not set.
  998. * If MSG_ABORT is set, the message length could be non zero with
  999. * the msg_iov set to the user abort reason.
  1000. */
  1001. if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
  1002. (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
  1003. err = -EINVAL;
  1004. goto out_nounlock;
  1005. }
  1006. /* If MSG_ADDR_OVER is set, there must be an address
  1007. * specified in msg_name.
  1008. */
  1009. if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
  1010. err = -EINVAL;
  1011. goto out_nounlock;
  1012. }
  1013. transport = NULL;
  1014. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1015. sctp_lock_sock(sk);
  1016. /* If a msg_name has been specified, assume this is to be used. */
  1017. if (msg_name) {
  1018. /* Look for a matching association on the endpoint. */
  1019. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1020. if (!asoc) {
  1021. /* If we could not find a matching association on the
  1022. * endpoint, make sure that it is not a TCP-style
  1023. * socket that already has an association or there is
  1024. * no peeled-off association on another socket.
  1025. */
  1026. if ((sctp_style(sk, TCP) &&
  1027. sctp_sstate(sk, ESTABLISHED)) ||
  1028. sctp_endpoint_is_peeled_off(ep, &to)) {
  1029. err = -EADDRNOTAVAIL;
  1030. goto out_unlock;
  1031. }
  1032. }
  1033. } else {
  1034. asoc = sctp_id2assoc(sk, associd);
  1035. if (!asoc) {
  1036. err = -EPIPE;
  1037. goto out_unlock;
  1038. }
  1039. }
  1040. if (asoc) {
  1041. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1042. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1043. * socket that has an association in CLOSED state. This can
  1044. * happen when an accepted socket has an association that is
  1045. * already CLOSED.
  1046. */
  1047. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1048. err = -EPIPE;
  1049. goto out_unlock;
  1050. }
  1051. if (sinfo_flags & MSG_EOF) {
  1052. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1053. asoc);
  1054. sctp_primitive_SHUTDOWN(asoc, NULL);
  1055. err = 0;
  1056. goto out_unlock;
  1057. }
  1058. if (sinfo_flags & MSG_ABORT) {
  1059. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1060. sctp_primitive_ABORT(asoc, msg);
  1061. err = 0;
  1062. goto out_unlock;
  1063. }
  1064. }
  1065. /* Do we need to create the association? */
  1066. if (!asoc) {
  1067. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1068. if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
  1069. err = -EINVAL;
  1070. goto out_unlock;
  1071. }
  1072. /* Check for invalid stream against the stream counts,
  1073. * either the default or the user specified stream counts.
  1074. */
  1075. if (sinfo) {
  1076. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1077. /* Check against the defaults. */
  1078. if (sinfo->sinfo_stream >=
  1079. sp->initmsg.sinit_num_ostreams) {
  1080. err = -EINVAL;
  1081. goto out_unlock;
  1082. }
  1083. } else {
  1084. /* Check against the requested. */
  1085. if (sinfo->sinfo_stream >=
  1086. sinit->sinit_num_ostreams) {
  1087. err = -EINVAL;
  1088. goto out_unlock;
  1089. }
  1090. }
  1091. }
  1092. /*
  1093. * API 3.1.2 bind() - UDP Style Syntax
  1094. * If a bind() or sctp_bindx() is not called prior to a
  1095. * sendmsg() call that initiates a new association, the
  1096. * system picks an ephemeral port and will choose an address
  1097. * set equivalent to binding with a wildcard address.
  1098. */
  1099. if (!ep->base.bind_addr.port) {
  1100. if (sctp_autobind(sk)) {
  1101. err = -EAGAIN;
  1102. goto out_unlock;
  1103. }
  1104. }
  1105. scope = sctp_scope(&to);
  1106. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1107. if (!new_asoc) {
  1108. err = -ENOMEM;
  1109. goto out_unlock;
  1110. }
  1111. asoc = new_asoc;
  1112. /* If the SCTP_INIT ancillary data is specified, set all
  1113. * the association init values accordingly.
  1114. */
  1115. if (sinit) {
  1116. if (sinit->sinit_num_ostreams) {
  1117. asoc->c.sinit_num_ostreams =
  1118. sinit->sinit_num_ostreams;
  1119. }
  1120. if (sinit->sinit_max_instreams) {
  1121. asoc->c.sinit_max_instreams =
  1122. sinit->sinit_max_instreams;
  1123. }
  1124. if (sinit->sinit_max_attempts) {
  1125. asoc->max_init_attempts
  1126. = sinit->sinit_max_attempts;
  1127. }
  1128. if (sinit->sinit_max_init_timeo) {
  1129. asoc->max_init_timeo =
  1130. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1131. }
  1132. }
  1133. /* Prime the peer's transport structures. */
  1134. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
  1135. if (!transport) {
  1136. err = -ENOMEM;
  1137. goto out_free;
  1138. }
  1139. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1140. if (err < 0) {
  1141. err = -ENOMEM;
  1142. goto out_free;
  1143. }
  1144. }
  1145. /* ASSERT: we have a valid association at this point. */
  1146. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1147. if (!sinfo) {
  1148. /* If the user didn't specify SNDRCVINFO, make up one with
  1149. * some defaults.
  1150. */
  1151. default_sinfo.sinfo_stream = asoc->default_stream;
  1152. default_sinfo.sinfo_flags = asoc->default_flags;
  1153. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1154. default_sinfo.sinfo_context = asoc->default_context;
  1155. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1156. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1157. sinfo = &default_sinfo;
  1158. }
  1159. /* API 7.1.7, the sndbuf size per association bounds the
  1160. * maximum size of data that can be sent in a single send call.
  1161. */
  1162. if (msg_len > sk->sk_sndbuf) {
  1163. err = -EMSGSIZE;
  1164. goto out_free;
  1165. }
  1166. /* If fragmentation is disabled and the message length exceeds the
  1167. * association fragmentation point, return EMSGSIZE. The I-D
  1168. * does not specify what this error is, but this looks like
  1169. * a great fit.
  1170. */
  1171. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1172. err = -EMSGSIZE;
  1173. goto out_free;
  1174. }
  1175. if (sinfo) {
  1176. /* Check for invalid stream. */
  1177. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1178. err = -EINVAL;
  1179. goto out_free;
  1180. }
  1181. }
  1182. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1183. if (!sctp_wspace(asoc)) {
  1184. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1185. if (err)
  1186. goto out_free;
  1187. }
  1188. /* If an address is passed with the sendto/sendmsg call, it is used
  1189. * to override the primary destination address in the TCP model, or
  1190. * when MSG_ADDR_OVER flag is set in the UDP model.
  1191. */
  1192. if ((sctp_style(sk, TCP) && msg_name) ||
  1193. (sinfo_flags & MSG_ADDR_OVER)) {
  1194. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1195. if (!chunk_tp) {
  1196. err = -EINVAL;
  1197. goto out_free;
  1198. }
  1199. } else
  1200. chunk_tp = NULL;
  1201. /* Auto-connect, if we aren't connected already. */
  1202. if (sctp_state(asoc, CLOSED)) {
  1203. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  1204. if (err < 0)
  1205. goto out_free;
  1206. SCTP_DEBUG_PRINTK("We associated primitively.\n");
  1207. }
  1208. /* Break the message into multiple chunks of maximum size. */
  1209. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1210. if (!datamsg) {
  1211. err = -ENOMEM;
  1212. goto out_free;
  1213. }
  1214. /* Now send the (possibly) fragmented message. */
  1215. list_for_each(pos, &datamsg->chunks) {
  1216. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  1217. sctp_datamsg_track(chunk);
  1218. /* Do accounting for the write space. */
  1219. sctp_set_owner_w(chunk);
  1220. chunk->transport = chunk_tp;
  1221. /* Send it to the lower layers. Note: all chunks
  1222. * must either fail or succeed. The lower layer
  1223. * works that way today. Keep it that way or this
  1224. * breaks.
  1225. */
  1226. err = sctp_primitive_SEND(asoc, chunk);
  1227. /* Did the lower layer accept the chunk? */
  1228. if (err)
  1229. sctp_chunk_free(chunk);
  1230. SCTP_DEBUG_PRINTK("We sent primitively.\n");
  1231. }
  1232. sctp_datamsg_free(datamsg);
  1233. if (err)
  1234. goto out_free;
  1235. else
  1236. err = msg_len;
  1237. /* If we are already past ASSOCIATE, the lower
  1238. * layers are responsible for association cleanup.
  1239. */
  1240. goto out_unlock;
  1241. out_free:
  1242. if (new_asoc)
  1243. sctp_association_free(asoc);
  1244. out_unlock:
  1245. sctp_release_sock(sk);
  1246. out_nounlock:
  1247. return sctp_error(sk, msg_flags, err);
  1248. #if 0
  1249. do_sock_err:
  1250. if (msg_len)
  1251. err = msg_len;
  1252. else
  1253. err = sock_error(sk);
  1254. goto out;
  1255. do_interrupted:
  1256. if (msg_len)
  1257. err = msg_len;
  1258. goto out;
  1259. #endif /* 0 */
  1260. }
  1261. /* This is an extended version of skb_pull() that removes the data from the
  1262. * start of a skb even when data is spread across the list of skb's in the
  1263. * frag_list. len specifies the total amount of data that needs to be removed.
  1264. * when 'len' bytes could be removed from the skb, it returns 0.
  1265. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1266. * could not be removed.
  1267. */
  1268. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1269. {
  1270. struct sk_buff *list;
  1271. int skb_len = skb_headlen(skb);
  1272. int rlen;
  1273. if (len <= skb_len) {
  1274. __skb_pull(skb, len);
  1275. return 0;
  1276. }
  1277. len -= skb_len;
  1278. __skb_pull(skb, skb_len);
  1279. for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
  1280. rlen = sctp_skb_pull(list, len);
  1281. skb->len -= (len-rlen);
  1282. skb->data_len -= (len-rlen);
  1283. if (!rlen)
  1284. return 0;
  1285. len = rlen;
  1286. }
  1287. return len;
  1288. }
  1289. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1290. *
  1291. * ssize_t recvmsg(int socket, struct msghdr *message,
  1292. * int flags);
  1293. *
  1294. * socket - the socket descriptor of the endpoint.
  1295. * message - pointer to the msghdr structure which contains a single
  1296. * user message and possibly some ancillary data.
  1297. *
  1298. * See Section 5 for complete description of the data
  1299. * structures.
  1300. *
  1301. * flags - flags sent or received with the user message, see Section
  1302. * 5 for complete description of the flags.
  1303. */
  1304. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1305. SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1306. struct msghdr *msg, size_t len, int noblock,
  1307. int flags, int *addr_len)
  1308. {
  1309. struct sctp_ulpevent *event = NULL;
  1310. struct sctp_sock *sp = sctp_sk(sk);
  1311. struct sk_buff *skb;
  1312. int copied;
  1313. int err = 0;
  1314. int skb_len;
  1315. SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
  1316. "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
  1317. "len", len, "knoblauch", noblock,
  1318. "flags", flags, "addr_len", addr_len);
  1319. sctp_lock_sock(sk);
  1320. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1321. err = -ENOTCONN;
  1322. goto out;
  1323. }
  1324. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1325. if (!skb)
  1326. goto out;
  1327. /* Get the total length of the skb including any skb's in the
  1328. * frag_list.
  1329. */
  1330. skb_len = skb->len;
  1331. copied = skb_len;
  1332. if (copied > len)
  1333. copied = len;
  1334. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1335. event = sctp_skb2event(skb);
  1336. if (err)
  1337. goto out_free;
  1338. sock_recv_timestamp(msg, sk, skb);
  1339. if (sctp_ulpevent_is_notification(event)) {
  1340. msg->msg_flags |= MSG_NOTIFICATION;
  1341. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1342. } else {
  1343. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1344. }
  1345. /* Check if we allow SCTP_SNDRCVINFO. */
  1346. if (sp->subscribe.sctp_data_io_event)
  1347. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1348. #if 0
  1349. /* FIXME: we should be calling IP/IPv6 layers. */
  1350. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1351. ip_cmsg_recv(msg, skb);
  1352. #endif
  1353. err = copied;
  1354. /* If skb's length exceeds the user's buffer, update the skb and
  1355. * push it back to the receive_queue so that the next call to
  1356. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1357. */
  1358. if (skb_len > copied) {
  1359. msg->msg_flags &= ~MSG_EOR;
  1360. if (flags & MSG_PEEK)
  1361. goto out_free;
  1362. sctp_skb_pull(skb, copied);
  1363. skb_queue_head(&sk->sk_receive_queue, skb);
  1364. /* When only partial message is copied to the user, increase
  1365. * rwnd by that amount. If all the data in the skb is read,
  1366. * rwnd is updated when the event is freed.
  1367. */
  1368. sctp_assoc_rwnd_increase(event->asoc, copied);
  1369. goto out;
  1370. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1371. (event->msg_flags & MSG_EOR))
  1372. msg->msg_flags |= MSG_EOR;
  1373. else
  1374. msg->msg_flags &= ~MSG_EOR;
  1375. out_free:
  1376. if (flags & MSG_PEEK) {
  1377. /* Release the skb reference acquired after peeking the skb in
  1378. * sctp_skb_recv_datagram().
  1379. */
  1380. kfree_skb(skb);
  1381. } else {
  1382. /* Free the event which includes releasing the reference to
  1383. * the owner of the skb, freeing the skb and updating the
  1384. * rwnd.
  1385. */
  1386. sctp_ulpevent_free(event);
  1387. }
  1388. out:
  1389. sctp_release_sock(sk);
  1390. return err;
  1391. }
  1392. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1393. *
  1394. * This option is a on/off flag. If enabled no SCTP message
  1395. * fragmentation will be performed. Instead if a message being sent
  1396. * exceeds the current PMTU size, the message will NOT be sent and
  1397. * instead a error will be indicated to the user.
  1398. */
  1399. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1400. char __user *optval, int optlen)
  1401. {
  1402. int val;
  1403. if (optlen < sizeof(int))
  1404. return -EINVAL;
  1405. if (get_user(val, (int __user *)optval))
  1406. return -EFAULT;
  1407. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1408. return 0;
  1409. }
  1410. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1411. int optlen)
  1412. {
  1413. if (optlen != sizeof(struct sctp_event_subscribe))
  1414. return -EINVAL;
  1415. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1416. return -EFAULT;
  1417. return 0;
  1418. }
  1419. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1420. *
  1421. * This socket option is applicable to the UDP-style socket only. When
  1422. * set it will cause associations that are idle for more than the
  1423. * specified number of seconds to automatically close. An association
  1424. * being idle is defined an association that has NOT sent or received
  1425. * user data. The special value of '0' indicates that no automatic
  1426. * close of any associations should be performed. The option expects an
  1427. * integer defining the number of seconds of idle time before an
  1428. * association is closed.
  1429. */
  1430. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1431. int optlen)
  1432. {
  1433. struct sctp_sock *sp = sctp_sk(sk);
  1434. /* Applicable to UDP-style socket only */
  1435. if (sctp_style(sk, TCP))
  1436. return -EOPNOTSUPP;
  1437. if (optlen != sizeof(int))
  1438. return -EINVAL;
  1439. if (copy_from_user(&sp->autoclose, optval, optlen))
  1440. return -EFAULT;
  1441. sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
  1442. return 0;
  1443. }
  1444. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1445. *
  1446. * Applications can enable or disable heartbeats for any peer address of
  1447. * an association, modify an address's heartbeat interval, force a
  1448. * heartbeat to be sent immediately, and adjust the address's maximum
  1449. * number of retransmissions sent before an address is considered
  1450. * unreachable. The following structure is used to access and modify an
  1451. * address's parameters:
  1452. *
  1453. * struct sctp_paddrparams {
  1454. * sctp_assoc_t spp_assoc_id;
  1455. * struct sockaddr_storage spp_address;
  1456. * uint32_t spp_hbinterval;
  1457. * uint16_t spp_pathmaxrxt;
  1458. * };
  1459. *
  1460. * spp_assoc_id - (UDP style socket) This is filled in the application,
  1461. * and identifies the association for this query.
  1462. * spp_address - This specifies which address is of interest.
  1463. * spp_hbinterval - This contains the value of the heartbeat interval,
  1464. * in milliseconds. A value of 0, when modifying the
  1465. * parameter, specifies that the heartbeat on this
  1466. * address should be disabled. A value of UINT32_MAX
  1467. * (4294967295), when modifying the parameter,
  1468. * specifies that a heartbeat should be sent
  1469. * immediately to the peer address, and the current
  1470. * interval should remain unchanged.
  1471. * spp_pathmaxrxt - This contains the maximum number of
  1472. * retransmissions before this address shall be
  1473. * considered unreachable.
  1474. */
  1475. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  1476. char __user *optval, int optlen)
  1477. {
  1478. struct sctp_paddrparams params;
  1479. struct sctp_transport *trans;
  1480. int error;
  1481. if (optlen != sizeof(struct sctp_paddrparams))
  1482. return -EINVAL;
  1483. if (copy_from_user(&params, optval, optlen))
  1484. return -EFAULT;
  1485. /*
  1486. * API 7. Socket Options (setting the default value for the endpoint)
  1487. * All options that support specific settings on an association by
  1488. * filling in either an association id variable or a sockaddr_storage
  1489. * SHOULD also support setting of the same value for the entire endpoint
  1490. * (i.e. future associations). To accomplish this the following logic is
  1491. * used when setting one of these options:
  1492. * c) If neither the sockaddr_storage or association identification is
  1493. * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
  1494. * the association identification is 0, the settings are a default
  1495. * and to be applied to the endpoint (all future associations).
  1496. */
  1497. /* update default value for endpoint (all future associations) */
  1498. if (!params.spp_assoc_id &&
  1499. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  1500. /* Manual heartbeat on an endpoint is invalid. */
  1501. if (0xffffffff == params.spp_hbinterval)
  1502. return -EINVAL;
  1503. else if (params.spp_hbinterval)
  1504. sctp_sk(sk)->paddrparam.spp_hbinterval =
  1505. params.spp_hbinterval;
  1506. if (params.spp_pathmaxrxt)
  1507. sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
  1508. params.spp_pathmaxrxt;
  1509. return 0;
  1510. }
  1511. trans = sctp_addr_id2transport(sk, &params.spp_address,
  1512. params.spp_assoc_id);
  1513. if (!trans)
  1514. return -EINVAL;
  1515. /* Applications can enable or disable heartbeats for any peer address
  1516. * of an association, modify an address's heartbeat interval, force a
  1517. * heartbeat to be sent immediately, and adjust the address's maximum
  1518. * number of retransmissions sent before an address is considered
  1519. * unreachable.
  1520. *
  1521. * The value of the heartbeat interval, in milliseconds. A value of
  1522. * UINT32_MAX (4294967295), when modifying the parameter, specifies
  1523. * that a heartbeat should be sent immediately to the peer address,
  1524. * and the current interval should remain unchanged.
  1525. */
  1526. if (0xffffffff == params.spp_hbinterval) {
  1527. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1528. if (error)
  1529. return error;
  1530. } else {
  1531. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  1532. * when modifying the parameter, specifies that the heartbeat on this
  1533. * address should be disabled.
  1534. */
  1535. if (params.spp_hbinterval) {
  1536. trans->hb_allowed = 1;
  1537. trans->hb_interval =
  1538. msecs_to_jiffies(params.spp_hbinterval);
  1539. } else
  1540. trans->hb_allowed = 0;
  1541. }
  1542. /* spp_pathmaxrxt contains the maximum number of retransmissions
  1543. * before this address shall be considered unreachable.
  1544. */
  1545. if (params.spp_pathmaxrxt)
  1546. trans->max_retrans = params.spp_pathmaxrxt;
  1547. return 0;
  1548. }
  1549. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  1550. *
  1551. * Applications can specify protocol parameters for the default association
  1552. * initialization. The option name argument to setsockopt() and getsockopt()
  1553. * is SCTP_INITMSG.
  1554. *
  1555. * Setting initialization parameters is effective only on an unconnected
  1556. * socket (for UDP-style sockets only future associations are effected
  1557. * by the change). With TCP-style sockets, this option is inherited by
  1558. * sockets derived from a listener socket.
  1559. */
  1560. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
  1561. {
  1562. struct sctp_initmsg sinit;
  1563. struct sctp_sock *sp = sctp_sk(sk);
  1564. if (optlen != sizeof(struct sctp_initmsg))
  1565. return -EINVAL;
  1566. if (copy_from_user(&sinit, optval, optlen))
  1567. return -EFAULT;
  1568. if (sinit.sinit_num_ostreams)
  1569. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  1570. if (sinit.sinit_max_instreams)
  1571. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  1572. if (sinit.sinit_max_attempts)
  1573. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  1574. if (sinit.sinit_max_init_timeo)
  1575. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  1576. return 0;
  1577. }
  1578. /*
  1579. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  1580. *
  1581. * Applications that wish to use the sendto() system call may wish to
  1582. * specify a default set of parameters that would normally be supplied
  1583. * through the inclusion of ancillary data. This socket option allows
  1584. * such an application to set the default sctp_sndrcvinfo structure.
  1585. * The application that wishes to use this socket option simply passes
  1586. * in to this call the sctp_sndrcvinfo structure defined in Section
  1587. * 5.2.2) The input parameters accepted by this call include
  1588. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  1589. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  1590. * to this call if the caller is using the UDP model.
  1591. */
  1592. static int sctp_setsockopt_default_send_param(struct sock *sk,
  1593. char __user *optval, int optlen)
  1594. {
  1595. struct sctp_sndrcvinfo info;
  1596. struct sctp_association *asoc;
  1597. struct sctp_sock *sp = sctp_sk(sk);
  1598. if (optlen != sizeof(struct sctp_sndrcvinfo))
  1599. return -EINVAL;
  1600. if (copy_from_user(&info, optval, optlen))
  1601. return -EFAULT;
  1602. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  1603. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  1604. return -EINVAL;
  1605. if (asoc) {
  1606. asoc->default_stream = info.sinfo_stream;
  1607. asoc->default_flags = info.sinfo_flags;
  1608. asoc->default_ppid = info.sinfo_ppid;
  1609. asoc->default_context = info.sinfo_context;
  1610. asoc->default_timetolive = info.sinfo_timetolive;
  1611. } else {
  1612. sp->default_stream = info.sinfo_stream;
  1613. sp->default_flags = info.sinfo_flags;
  1614. sp->default_ppid = info.sinfo_ppid;
  1615. sp->default_context = info.sinfo_context;
  1616. sp->default_timetolive = info.sinfo_timetolive;
  1617. }
  1618. return 0;
  1619. }
  1620. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  1621. *
  1622. * Requests that the local SCTP stack use the enclosed peer address as
  1623. * the association primary. The enclosed address must be one of the
  1624. * association peer's addresses.
  1625. */
  1626. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  1627. int optlen)
  1628. {
  1629. struct sctp_prim prim;
  1630. struct sctp_transport *trans;
  1631. if (optlen != sizeof(struct sctp_prim))
  1632. return -EINVAL;
  1633. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  1634. return -EFAULT;
  1635. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  1636. if (!trans)
  1637. return -EINVAL;
  1638. sctp_assoc_set_primary(trans->asoc, trans);
  1639. return 0;
  1640. }
  1641. /*
  1642. * 7.1.5 SCTP_NODELAY
  1643. *
  1644. * Turn on/off any Nagle-like algorithm. This means that packets are
  1645. * generally sent as soon as possible and no unnecessary delays are
  1646. * introduced, at the cost of more packets in the network. Expects an
  1647. * integer boolean flag.
  1648. */
  1649. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  1650. int optlen)
  1651. {
  1652. int val;
  1653. if (optlen < sizeof(int))
  1654. return -EINVAL;
  1655. if (get_user(val, (int __user *)optval))
  1656. return -EFAULT;
  1657. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  1658. return 0;
  1659. }
  1660. /*
  1661. *
  1662. * 7.1.1 SCTP_RTOINFO
  1663. *
  1664. * The protocol parameters used to initialize and bound retransmission
  1665. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  1666. * and modify these parameters.
  1667. * All parameters are time values, in milliseconds. A value of 0, when
  1668. * modifying the parameters, indicates that the current value should not
  1669. * be changed.
  1670. *
  1671. */
  1672. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
  1673. struct sctp_rtoinfo rtoinfo;
  1674. struct sctp_association *asoc;
  1675. if (optlen != sizeof (struct sctp_rtoinfo))
  1676. return -EINVAL;
  1677. if (copy_from_user(&rtoinfo, optval, optlen))
  1678. return -EFAULT;
  1679. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  1680. /* Set the values to the specific association */
  1681. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  1682. return -EINVAL;
  1683. if (asoc) {
  1684. if (rtoinfo.srto_initial != 0)
  1685. asoc->rto_initial =
  1686. msecs_to_jiffies(rtoinfo.srto_initial);
  1687. if (rtoinfo.srto_max != 0)
  1688. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  1689. if (rtoinfo.srto_min != 0)
  1690. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  1691. } else {
  1692. /* If there is no association or the association-id = 0
  1693. * set the values to the endpoint.
  1694. */
  1695. struct sctp_sock *sp = sctp_sk(sk);
  1696. if (rtoinfo.srto_initial != 0)
  1697. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  1698. if (rtoinfo.srto_max != 0)
  1699. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  1700. if (rtoinfo.srto_min != 0)
  1701. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  1702. }
  1703. return 0;
  1704. }
  1705. /*
  1706. *
  1707. * 7.1.2 SCTP_ASSOCINFO
  1708. *
  1709. * This option is used to tune the the maximum retransmission attempts
  1710. * of the association.
  1711. * Returns an error if the new association retransmission value is
  1712. * greater than the sum of the retransmission value of the peer.
  1713. * See [SCTP] for more information.
  1714. *
  1715. */
  1716. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
  1717. {
  1718. struct sctp_assocparams assocparams;
  1719. struct sctp_association *asoc;
  1720. if (optlen != sizeof(struct sctp_assocparams))
  1721. return -EINVAL;
  1722. if (copy_from_user(&assocparams, optval, optlen))
  1723. return -EFAULT;
  1724. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  1725. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  1726. return -EINVAL;
  1727. /* Set the values to the specific association */
  1728. if (asoc) {
  1729. if (assocparams.sasoc_asocmaxrxt != 0)
  1730. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  1731. if (assocparams.sasoc_cookie_life != 0) {
  1732. asoc->cookie_life.tv_sec =
  1733. assocparams.sasoc_cookie_life / 1000;
  1734. asoc->cookie_life.tv_usec =
  1735. (assocparams.sasoc_cookie_life % 1000)
  1736. * 1000;
  1737. }
  1738. } else {
  1739. /* Set the values to the endpoint */
  1740. struct sctp_sock *sp = sctp_sk(sk);
  1741. if (assocparams.sasoc_asocmaxrxt != 0)
  1742. sp->assocparams.sasoc_asocmaxrxt =
  1743. assocparams.sasoc_asocmaxrxt;
  1744. if (assocparams.sasoc_cookie_life != 0)
  1745. sp->assocparams.sasoc_cookie_life =
  1746. assocparams.sasoc_cookie_life;
  1747. }
  1748. return 0;
  1749. }
  1750. /*
  1751. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  1752. *
  1753. * This socket option is a boolean flag which turns on or off mapped V4
  1754. * addresses. If this option is turned on and the socket is type
  1755. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  1756. * If this option is turned off, then no mapping will be done of V4
  1757. * addresses and a user will receive both PF_INET6 and PF_INET type
  1758. * addresses on the socket.
  1759. */
  1760. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  1761. {
  1762. int val;
  1763. struct sctp_sock *sp = sctp_sk(sk);
  1764. if (optlen < sizeof(int))
  1765. return -EINVAL;
  1766. if (get_user(val, (int __user *)optval))
  1767. return -EFAULT;
  1768. if (val)
  1769. sp->v4mapped = 1;
  1770. else
  1771. sp->v4mapped = 0;
  1772. return 0;
  1773. }
  1774. /*
  1775. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  1776. *
  1777. * This socket option specifies the maximum size to put in any outgoing
  1778. * SCTP chunk. If a message is larger than this size it will be
  1779. * fragmented by SCTP into the specified size. Note that the underlying
  1780. * SCTP implementation may fragment into smaller sized chunks when the
  1781. * PMTU of the underlying association is smaller than the value set by
  1782. * the user.
  1783. */
  1784. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  1785. {
  1786. struct sctp_association *asoc;
  1787. struct list_head *pos;
  1788. struct sctp_sock *sp = sctp_sk(sk);
  1789. int val;
  1790. if (optlen < sizeof(int))
  1791. return -EINVAL;
  1792. if (get_user(val, (int __user *)optval))
  1793. return -EFAULT;
  1794. if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
  1795. return -EINVAL;
  1796. sp->user_frag = val;
  1797. if (val) {
  1798. /* Update the frag_point of the existing associations. */
  1799. list_for_each(pos, &(sp->ep->asocs)) {
  1800. asoc = list_entry(pos, struct sctp_association, asocs);
  1801. asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
  1802. }
  1803. }
  1804. return 0;
  1805. }
  1806. /*
  1807. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  1808. *
  1809. * Requests that the peer mark the enclosed address as the association
  1810. * primary. The enclosed address must be one of the association's
  1811. * locally bound addresses. The following structure is used to make a
  1812. * set primary request:
  1813. */
  1814. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  1815. int optlen)
  1816. {
  1817. struct sctp_sock *sp;
  1818. struct sctp_endpoint *ep;
  1819. struct sctp_association *asoc = NULL;
  1820. struct sctp_setpeerprim prim;
  1821. struct sctp_chunk *chunk;
  1822. int err;
  1823. sp = sctp_sk(sk);
  1824. ep = sp->ep;
  1825. if (!sctp_addip_enable)
  1826. return -EPERM;
  1827. if (optlen != sizeof(struct sctp_setpeerprim))
  1828. return -EINVAL;
  1829. if (copy_from_user(&prim, optval, optlen))
  1830. return -EFAULT;
  1831. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  1832. if (!asoc)
  1833. return -EINVAL;
  1834. if (!asoc->peer.asconf_capable)
  1835. return -EPERM;
  1836. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  1837. return -EPERM;
  1838. if (!sctp_state(asoc, ESTABLISHED))
  1839. return -ENOTCONN;
  1840. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  1841. return -EADDRNOTAVAIL;
  1842. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  1843. chunk = sctp_make_asconf_set_prim(asoc,
  1844. (union sctp_addr *)&prim.sspp_addr);
  1845. if (!chunk)
  1846. return -ENOMEM;
  1847. err = sctp_send_asconf(asoc, chunk);
  1848. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  1849. return err;
  1850. }
  1851. static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
  1852. int optlen)
  1853. {
  1854. __u32 val;
  1855. if (optlen < sizeof(__u32))
  1856. return -EINVAL;
  1857. if (copy_from_user(&val, optval, sizeof(__u32)))
  1858. return -EFAULT;
  1859. sctp_sk(sk)->adaption_ind = val;
  1860. return 0;
  1861. }
  1862. /* API 6.2 setsockopt(), getsockopt()
  1863. *
  1864. * Applications use setsockopt() and getsockopt() to set or retrieve
  1865. * socket options. Socket options are used to change the default
  1866. * behavior of sockets calls. They are described in Section 7.
  1867. *
  1868. * The syntax is:
  1869. *
  1870. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  1871. * int __user *optlen);
  1872. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  1873. * int optlen);
  1874. *
  1875. * sd - the socket descript.
  1876. * level - set to IPPROTO_SCTP for all SCTP options.
  1877. * optname - the option name.
  1878. * optval - the buffer to store the value of the option.
  1879. * optlen - the size of the buffer.
  1880. */
  1881. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  1882. char __user *optval, int optlen)
  1883. {
  1884. int retval = 0;
  1885. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  1886. sk, optname);
  1887. /* I can hardly begin to describe how wrong this is. This is
  1888. * so broken as to be worse than useless. The API draft
  1889. * REALLY is NOT helpful here... I am not convinced that the
  1890. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  1891. * are at all well-founded.
  1892. */
  1893. if (level != SOL_SCTP) {
  1894. struct sctp_af *af = sctp_sk(sk)->pf->af;
  1895. retval = af->setsockopt(sk, level, optname, optval, optlen);
  1896. goto out_nounlock;
  1897. }
  1898. sctp_lock_sock(sk);
  1899. switch (optname) {
  1900. case SCTP_SOCKOPT_BINDX_ADD:
  1901. /* 'optlen' is the size of the addresses buffer. */
  1902. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  1903. optlen, SCTP_BINDX_ADD_ADDR);
  1904. break;
  1905. case SCTP_SOCKOPT_BINDX_REM:
  1906. /* 'optlen' is the size of the addresses buffer. */
  1907. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  1908. optlen, SCTP_BINDX_REM_ADDR);
  1909. break;
  1910. case SCTP_DISABLE_FRAGMENTS:
  1911. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  1912. break;
  1913. case SCTP_EVENTS:
  1914. retval = sctp_setsockopt_events(sk, optval, optlen);
  1915. break;
  1916. case SCTP_AUTOCLOSE:
  1917. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  1918. break;
  1919. case SCTP_PEER_ADDR_PARAMS:
  1920. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  1921. break;
  1922. case SCTP_INITMSG:
  1923. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  1924. break;
  1925. case SCTP_DEFAULT_SEND_PARAM:
  1926. retval = sctp_setsockopt_default_send_param(sk, optval,
  1927. optlen);
  1928. break;
  1929. case SCTP_PRIMARY_ADDR:
  1930. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  1931. break;
  1932. case SCTP_SET_PEER_PRIMARY_ADDR:
  1933. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  1934. break;
  1935. case SCTP_NODELAY:
  1936. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  1937. break;
  1938. case SCTP_RTOINFO:
  1939. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  1940. break;
  1941. case SCTP_ASSOCINFO:
  1942. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  1943. break;
  1944. case SCTP_I_WANT_MAPPED_V4_ADDR:
  1945. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  1946. break;
  1947. case SCTP_MAXSEG:
  1948. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  1949. break;
  1950. case SCTP_ADAPTION_LAYER:
  1951. retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
  1952. break;
  1953. default:
  1954. retval = -ENOPROTOOPT;
  1955. break;
  1956. };
  1957. sctp_release_sock(sk);
  1958. out_nounlock:
  1959. return retval;
  1960. }
  1961. /* API 3.1.6 connect() - UDP Style Syntax
  1962. *
  1963. * An application may use the connect() call in the UDP model to initiate an
  1964. * association without sending data.
  1965. *
  1966. * The syntax is:
  1967. *
  1968. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  1969. *
  1970. * sd: the socket descriptor to have a new association added to.
  1971. *
  1972. * nam: the address structure (either struct sockaddr_in or struct
  1973. * sockaddr_in6 defined in RFC2553 [7]).
  1974. *
  1975. * len: the size of the address.
  1976. */
  1977. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
  1978. int addr_len)
  1979. {
  1980. struct sctp_sock *sp;
  1981. struct sctp_endpoint *ep;
  1982. struct sctp_association *asoc;
  1983. struct sctp_transport *transport;
  1984. union sctp_addr to;
  1985. struct sctp_af *af;
  1986. sctp_scope_t scope;
  1987. long timeo;
  1988. int err = 0;
  1989. sctp_lock_sock(sk);
  1990. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
  1991. __FUNCTION__, sk, uaddr, addr_len);
  1992. sp = sctp_sk(sk);
  1993. ep = sp->ep;
  1994. /* connect() cannot be done on a socket that is already in ESTABLISHED
  1995. * state - UDP-style peeled off socket or a TCP-style socket that
  1996. * is already connected.
  1997. * It cannot be done even on a TCP-style listening socket.
  1998. */
  1999. if (sctp_sstate(sk, ESTABLISHED) ||
  2000. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  2001. err = -EISCONN;
  2002. goto out_unlock;
  2003. }
  2004. err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
  2005. if (err)
  2006. goto out_unlock;
  2007. if (addr_len > sizeof(to))
  2008. addr_len = sizeof(to);
  2009. memcpy(&to, uaddr, addr_len);
  2010. to.v4.sin_port = ntohs(to.v4.sin_port);
  2011. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  2012. if (asoc) {
  2013. if (asoc->state >= SCTP_STATE_ESTABLISHED)
  2014. err = -EISCONN;
  2015. else
  2016. err = -EALREADY;
  2017. goto out_unlock;
  2018. }
  2019. /* If we could not find a matching association on the endpoint,
  2020. * make sure that there is no peeled-off association matching the
  2021. * peer address even on another socket.
  2022. */
  2023. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  2024. err = -EADDRNOTAVAIL;
  2025. goto out_unlock;
  2026. }
  2027. /* If a bind() or sctp_bindx() is not called prior to a connect()
  2028. * call, the system picks an ephemeral port and will choose an address
  2029. * set equivalent to binding with a wildcard address.
  2030. */
  2031. if (!ep->base.bind_addr.port) {
  2032. if (sctp_autobind(sk)) {
  2033. err = -EAGAIN;
  2034. goto out_unlock;
  2035. }
  2036. }
  2037. scope = sctp_scope(&to);
  2038. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  2039. if (!asoc) {
  2040. err = -ENOMEM;
  2041. goto out_unlock;
  2042. }
  2043. /* Prime the peer's transport structures. */
  2044. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
  2045. if (!transport) {
  2046. sctp_association_free(asoc);
  2047. goto out_unlock;
  2048. }
  2049. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  2050. if (err < 0) {
  2051. sctp_association_free(asoc);
  2052. goto out_unlock;
  2053. }
  2054. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  2055. if (err < 0) {
  2056. sctp_association_free(asoc);
  2057. goto out_unlock;
  2058. }
  2059. /* Initialize sk's dport and daddr for getpeername() */
  2060. inet_sk(sk)->dport = htons(asoc->peer.port);
  2061. af = sctp_get_af_specific(to.sa.sa_family);
  2062. af->to_sk_daddr(&to, sk);
  2063. timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2064. err = sctp_wait_for_connect(asoc, &timeo);
  2065. out_unlock:
  2066. sctp_release_sock(sk);
  2067. return err;
  2068. }
  2069. /* FIXME: Write comments. */
  2070. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2071. {
  2072. return -EOPNOTSUPP; /* STUB */
  2073. }
  2074. /* 4.1.4 accept() - TCP Style Syntax
  2075. *
  2076. * Applications use accept() call to remove an established SCTP
  2077. * association from the accept queue of the endpoint. A new socket
  2078. * descriptor will be returned from accept() to represent the newly
  2079. * formed association.
  2080. */
  2081. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2082. {
  2083. struct sctp_sock *sp;
  2084. struct sctp_endpoint *ep;
  2085. struct sock *newsk = NULL;
  2086. struct sctp_association *asoc;
  2087. long timeo;
  2088. int error = 0;
  2089. sctp_lock_sock(sk);
  2090. sp = sctp_sk(sk);
  2091. ep = sp->ep;
  2092. if (!sctp_style(sk, TCP)) {
  2093. error = -EOPNOTSUPP;
  2094. goto out;
  2095. }
  2096. if (!sctp_sstate(sk, LISTENING)) {
  2097. error = -EINVAL;
  2098. goto out;
  2099. }
  2100. timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2101. error = sctp_wait_for_accept(sk, timeo);
  2102. if (error)
  2103. goto out;
  2104. /* We treat the list of associations on the endpoint as the accept
  2105. * queue and pick the first association on the list.
  2106. */
  2107. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2108. newsk = sp->pf->create_accept_sk(sk, asoc);
  2109. if (!newsk) {
  2110. error = -ENOMEM;
  2111. goto out;
  2112. }
  2113. /* Populate the fields of the newsk from the oldsk and migrate the
  2114. * asoc to the newsk.
  2115. */
  2116. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2117. out:
  2118. sctp_release_sock(sk);
  2119. *err = error;
  2120. return newsk;
  2121. }
  2122. /* The SCTP ioctl handler. */
  2123. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2124. {
  2125. return -ENOIOCTLCMD;
  2126. }
  2127. /* This is the function which gets called during socket creation to
  2128. * initialized the SCTP-specific portion of the sock.
  2129. * The sock structure should already be zero-filled memory.
  2130. */
  2131. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2132. {
  2133. struct sctp_endpoint *ep;
  2134. struct sctp_sock *sp;
  2135. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2136. sp = sctp_sk(sk);
  2137. /* Initialize the SCTP per socket area. */
  2138. switch (sk->sk_type) {
  2139. case SOCK_SEQPACKET:
  2140. sp->type = SCTP_SOCKET_UDP;
  2141. break;
  2142. case SOCK_STREAM:
  2143. sp->type = SCTP_SOCKET_TCP;
  2144. break;
  2145. default:
  2146. return -ESOCKTNOSUPPORT;
  2147. }
  2148. /* Initialize default send parameters. These parameters can be
  2149. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2150. */
  2151. sp->default_stream = 0;
  2152. sp->default_ppid = 0;
  2153. sp->default_flags = 0;
  2154. sp->default_context = 0;
  2155. sp->default_timetolive = 0;
  2156. /* Initialize default setup parameters. These parameters
  2157. * can be modified with the SCTP_INITMSG socket option or
  2158. * overridden by the SCTP_INIT CMSG.
  2159. */
  2160. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2161. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2162. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2163. sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
  2164. /* Initialize default RTO related parameters. These parameters can
  2165. * be modified for with the SCTP_RTOINFO socket option.
  2166. */
  2167. sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
  2168. sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
  2169. sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
  2170. /* Initialize default association related parameters. These parameters
  2171. * can be modified with the SCTP_ASSOCINFO socket option.
  2172. */
  2173. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2174. sp->assocparams.sasoc_number_peer_destinations = 0;
  2175. sp->assocparams.sasoc_peer_rwnd = 0;
  2176. sp->assocparams.sasoc_local_rwnd = 0;
  2177. sp->assocparams.sasoc_cookie_life =
  2178. jiffies_to_msecs(sctp_valid_cookie_life);
  2179. /* Initialize default event subscriptions. By default, all the
  2180. * options are off.
  2181. */
  2182. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2183. /* Default Peer Address Parameters. These defaults can
  2184. * be modified via SCTP_PEER_ADDR_PARAMS
  2185. */
  2186. sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
  2187. sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
  2188. /* If enabled no SCTP message fragmentation will be performed.
  2189. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2190. */
  2191. sp->disable_fragments = 0;
  2192. /* Turn on/off any Nagle-like algorithm. */
  2193. sp->nodelay = 1;
  2194. /* Enable by default. */
  2195. sp->v4mapped = 1;
  2196. /* Auto-close idle associations after the configured
  2197. * number of seconds. A value of 0 disables this
  2198. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2199. * for UDP-style sockets only.
  2200. */
  2201. sp->autoclose = 0;
  2202. /* User specified fragmentation limit. */
  2203. sp->user_frag = 0;
  2204. sp->adaption_ind = 0;
  2205. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2206. /* Control variables for partial data delivery. */
  2207. sp->pd_mode = 0;
  2208. skb_queue_head_init(&sp->pd_lobby);
  2209. /* Create a per socket endpoint structure. Even if we
  2210. * change the data structure relationships, this may still
  2211. * be useful for storing pre-connect address information.
  2212. */
  2213. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2214. if (!ep)
  2215. return -ENOMEM;
  2216. sp->ep = ep;
  2217. sp->hmac = NULL;
  2218. SCTP_DBG_OBJCNT_INC(sock);
  2219. return 0;
  2220. }
  2221. /* Cleanup any SCTP per socket resources. */
  2222. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2223. {
  2224. struct sctp_endpoint *ep;
  2225. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2226. /* Release our hold on the endpoint. */
  2227. ep = sctp_sk(sk)->ep;
  2228. sctp_endpoint_free(ep);
  2229. return 0;
  2230. }
  2231. /* API 4.1.7 shutdown() - TCP Style Syntax
  2232. * int shutdown(int socket, int how);
  2233. *
  2234. * sd - the socket descriptor of the association to be closed.
  2235. * how - Specifies the type of shutdown. The values are
  2236. * as follows:
  2237. * SHUT_RD
  2238. * Disables further receive operations. No SCTP
  2239. * protocol action is taken.
  2240. * SHUT_WR
  2241. * Disables further send operations, and initiates
  2242. * the SCTP shutdown sequence.
  2243. * SHUT_RDWR
  2244. * Disables further send and receive operations
  2245. * and initiates the SCTP shutdown sequence.
  2246. */
  2247. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2248. {
  2249. struct sctp_endpoint *ep;
  2250. struct sctp_association *asoc;
  2251. if (!sctp_style(sk, TCP))
  2252. return;
  2253. if (how & SEND_SHUTDOWN) {
  2254. ep = sctp_sk(sk)->ep;
  2255. if (!list_empty(&ep->asocs)) {
  2256. asoc = list_entry(ep->asocs.next,
  2257. struct sctp_association, asocs);
  2258. sctp_primitive_SHUTDOWN(asoc, NULL);
  2259. }
  2260. }
  2261. }
  2262. /* 7.2.1 Association Status (SCTP_STATUS)
  2263. * Applications can retrieve current status information about an
  2264. * association, including association state, peer receiver window size,
  2265. * number of unacked data chunks, and number of data chunks pending
  2266. * receipt. This information is read-only.
  2267. */
  2268. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2269. char __user *optval,
  2270. int __user *optlen)
  2271. {
  2272. struct sctp_status status;
  2273. struct sctp_association *asoc = NULL;
  2274. struct sctp_transport *transport;
  2275. sctp_assoc_t associd;
  2276. int retval = 0;
  2277. if (len != sizeof(status)) {
  2278. retval = -EINVAL;
  2279. goto out;
  2280. }
  2281. if (copy_from_user(&status, optval, sizeof(status))) {
  2282. retval = -EFAULT;
  2283. goto out;
  2284. }
  2285. associd = status.sstat_assoc_id;
  2286. asoc = sctp_id2assoc(sk, associd);
  2287. if (!asoc) {
  2288. retval = -EINVAL;
  2289. goto out;
  2290. }
  2291. transport = asoc->peer.primary_path;
  2292. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2293. status.sstat_state = asoc->state;
  2294. status.sstat_rwnd = asoc->peer.rwnd;
  2295. status.sstat_unackdata = asoc->unack_data;
  2296. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2297. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2298. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2299. status.sstat_fragmentation_point = asoc->frag_point;
  2300. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2301. memcpy(&status.sstat_primary.spinfo_address,
  2302. &(transport->ipaddr), sizeof(union sctp_addr));
  2303. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2304. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2305. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2306. status.sstat_primary.spinfo_state = transport->active;
  2307. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2308. status.sstat_primary.spinfo_srtt = transport->srtt;
  2309. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2310. status.sstat_primary.spinfo_mtu = transport->pmtu;
  2311. if (put_user(len, optlen)) {
  2312. retval = -EFAULT;
  2313. goto out;
  2314. }
  2315. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2316. len, status.sstat_state, status.sstat_rwnd,
  2317. status.sstat_assoc_id);
  2318. if (copy_to_user(optval, &status, len)) {
  2319. retval = -EFAULT;
  2320. goto out;
  2321. }
  2322. out:
  2323. return (retval);
  2324. }
  2325. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2326. *
  2327. * Applications can retrieve information about a specific peer address
  2328. * of an association, including its reachability state, congestion
  2329. * window, and retransmission timer values. This information is
  2330. * read-only.
  2331. */
  2332. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  2333. char __user *optval,
  2334. int __user *optlen)
  2335. {
  2336. struct sctp_paddrinfo pinfo;
  2337. struct sctp_transport *transport;
  2338. int retval = 0;
  2339. if (len != sizeof(pinfo)) {
  2340. retval = -EINVAL;
  2341. goto out;
  2342. }
  2343. if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
  2344. retval = -EFAULT;
  2345. goto out;
  2346. }
  2347. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  2348. pinfo.spinfo_assoc_id);
  2349. if (!transport)
  2350. return -EINVAL;
  2351. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2352. pinfo.spinfo_state = transport->active;
  2353. pinfo.spinfo_cwnd = transport->cwnd;
  2354. pinfo.spinfo_srtt = transport->srtt;
  2355. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  2356. pinfo.spinfo_mtu = transport->pmtu;
  2357. if (put_user(len, optlen)) {
  2358. retval = -EFAULT;
  2359. goto out;
  2360. }
  2361. if (copy_to_user(optval, &pinfo, len)) {
  2362. retval = -EFAULT;
  2363. goto out;
  2364. }
  2365. out:
  2366. return (retval);
  2367. }
  2368. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  2369. *
  2370. * This option is a on/off flag. If enabled no SCTP message
  2371. * fragmentation will be performed. Instead if a message being sent
  2372. * exceeds the current PMTU size, the message will NOT be sent and
  2373. * instead a error will be indicated to the user.
  2374. */
  2375. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  2376. char __user *optval, int __user *optlen)
  2377. {
  2378. int val;
  2379. if (len < sizeof(int))
  2380. return -EINVAL;
  2381. len = sizeof(int);
  2382. val = (sctp_sk(sk)->disable_fragments == 1);
  2383. if (put_user(len, optlen))
  2384. return -EFAULT;
  2385. if (copy_to_user(optval, &val, len))
  2386. return -EFAULT;
  2387. return 0;
  2388. }
  2389. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  2390. *
  2391. * This socket option is used to specify various notifications and
  2392. * ancillary data the user wishes to receive.
  2393. */
  2394. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  2395. int __user *optlen)
  2396. {
  2397. if (len != sizeof(struct sctp_event_subscribe))
  2398. return -EINVAL;
  2399. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  2400. return -EFAULT;
  2401. return 0;
  2402. }
  2403. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  2404. *
  2405. * This socket option is applicable to the UDP-style socket only. When
  2406. * set it will cause associations that are idle for more than the
  2407. * specified number of seconds to automatically close. An association
  2408. * being idle is defined an association that has NOT sent or received
  2409. * user data. The special value of '0' indicates that no automatic
  2410. * close of any associations should be performed. The option expects an
  2411. * integer defining the number of seconds of idle time before an
  2412. * association is closed.
  2413. */
  2414. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2415. {
  2416. /* Applicable to UDP-style socket only */
  2417. if (sctp_style(sk, TCP))
  2418. return -EOPNOTSUPP;
  2419. if (len != sizeof(int))
  2420. return -EINVAL;
  2421. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
  2422. return -EFAULT;
  2423. return 0;
  2424. }
  2425. /* Helper routine to branch off an association to a new socket. */
  2426. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  2427. struct socket **sockp)
  2428. {
  2429. struct sock *sk = asoc->base.sk;
  2430. struct socket *sock;
  2431. int err = 0;
  2432. /* An association cannot be branched off from an already peeled-off
  2433. * socket, nor is this supported for tcp style sockets.
  2434. */
  2435. if (!sctp_style(sk, UDP))
  2436. return -EINVAL;
  2437. /* Create a new socket. */
  2438. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  2439. if (err < 0)
  2440. return err;
  2441. /* Populate the fields of the newsk from the oldsk and migrate the
  2442. * asoc to the newsk.
  2443. */
  2444. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  2445. *sockp = sock;
  2446. return err;
  2447. }
  2448. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2449. {
  2450. sctp_peeloff_arg_t peeloff;
  2451. struct socket *newsock;
  2452. int retval = 0;
  2453. struct sctp_association *asoc;
  2454. if (len != sizeof(sctp_peeloff_arg_t))
  2455. return -EINVAL;
  2456. if (copy_from_user(&peeloff, optval, len))
  2457. return -EFAULT;
  2458. asoc = sctp_id2assoc(sk, peeloff.associd);
  2459. if (!asoc) {
  2460. retval = -EINVAL;
  2461. goto out;
  2462. }
  2463. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  2464. retval = sctp_do_peeloff(asoc, &newsock);
  2465. if (retval < 0)
  2466. goto out;
  2467. /* Map the socket to an unused fd that can be returned to the user. */
  2468. retval = sock_map_fd(newsock);
  2469. if (retval < 0) {
  2470. sock_release(newsock);
  2471. goto out;
  2472. }
  2473. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  2474. __FUNCTION__, sk, asoc, newsock->sk, retval);
  2475. /* Return the fd mapped to the new socket. */
  2476. peeloff.sd = retval;
  2477. if (copy_to_user(optval, &peeloff, len))
  2478. retval = -EFAULT;
  2479. out:
  2480. return retval;
  2481. }
  2482. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  2483. *
  2484. * Applications can enable or disable heartbeats for any peer address of
  2485. * an association, modify an address's heartbeat interval, force a
  2486. * heartbeat to be sent immediately, and adjust the address's maximum
  2487. * number of retransmissions sent before an address is considered
  2488. * unreachable. The following structure is used to access and modify an
  2489. * address's parameters:
  2490. *
  2491. * struct sctp_paddrparams {
  2492. * sctp_assoc_t spp_assoc_id;
  2493. * struct sockaddr_storage spp_address;
  2494. * uint32_t spp_hbinterval;
  2495. * uint16_t spp_pathmaxrxt;
  2496. * };
  2497. *
  2498. * spp_assoc_id - (UDP style socket) This is filled in the application,
  2499. * and identifies the association for this query.
  2500. * spp_address - This specifies which address is of interest.
  2501. * spp_hbinterval - This contains the value of the heartbeat interval,
  2502. * in milliseconds. A value of 0, when modifying the
  2503. * parameter, specifies that the heartbeat on this
  2504. * address should be disabled. A value of UINT32_MAX
  2505. * (4294967295), when modifying the parameter,
  2506. * specifies that a heartbeat should be sent
  2507. * immediately to the peer address, and the current
  2508. * interval should remain unchanged.
  2509. * spp_pathmaxrxt - This contains the maximum number of
  2510. * retransmissions before this address shall be
  2511. * considered unreachable.
  2512. */
  2513. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  2514. char __user *optval, int __user *optlen)
  2515. {
  2516. struct sctp_paddrparams params;
  2517. struct sctp_transport *trans;
  2518. if (len != sizeof(struct sctp_paddrparams))
  2519. return -EINVAL;
  2520. if (copy_from_user(&params, optval, len))
  2521. return -EFAULT;
  2522. /* If no association id is specified retrieve the default value
  2523. * for the endpoint that will be used for all future associations
  2524. */
  2525. if (!params.spp_assoc_id &&
  2526. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  2527. params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
  2528. params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
  2529. goto done;
  2530. }
  2531. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2532. params.spp_assoc_id);
  2533. if (!trans)
  2534. return -EINVAL;
  2535. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  2536. * when modifying the parameter, specifies that the heartbeat on this
  2537. * address should be disabled.
  2538. */
  2539. if (!trans->hb_allowed)
  2540. params.spp_hbinterval = 0;
  2541. else
  2542. params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
  2543. /* spp_pathmaxrxt contains the maximum number of retransmissions
  2544. * before this address shall be considered unreachable.
  2545. */
  2546. params.spp_pathmaxrxt = trans->max_retrans;
  2547. done:
  2548. if (copy_to_user(optval, &params, len))
  2549. return -EFAULT;
  2550. if (put_user(len, optlen))
  2551. return -EFAULT;
  2552. return 0;
  2553. }
  2554. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2555. *
  2556. * Applications can specify protocol parameters for the default association
  2557. * initialization. The option name argument to setsockopt() and getsockopt()
  2558. * is SCTP_INITMSG.
  2559. *
  2560. * Setting initialization parameters is effective only on an unconnected
  2561. * socket (for UDP-style sockets only future associations are effected
  2562. * by the change). With TCP-style sockets, this option is inherited by
  2563. * sockets derived from a listener socket.
  2564. */
  2565. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2566. {
  2567. if (len != sizeof(struct sctp_initmsg))
  2568. return -EINVAL;
  2569. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  2570. return -EFAULT;
  2571. return 0;
  2572. }
  2573. static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
  2574. char __user *optval, int __user *optlen)
  2575. {
  2576. sctp_assoc_t id;
  2577. struct sctp_association *asoc;
  2578. struct list_head *pos;
  2579. int cnt = 0;
  2580. if (len != sizeof(sctp_assoc_t))
  2581. return -EINVAL;
  2582. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2583. return -EFAULT;
  2584. /* For UDP-style sockets, id specifies the association to query. */
  2585. asoc = sctp_id2assoc(sk, id);
  2586. if (!asoc)
  2587. return -EINVAL;
  2588. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2589. cnt ++;
  2590. }
  2591. return cnt;
  2592. }
  2593. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  2594. char __user *optval, int __user *optlen)
  2595. {
  2596. struct sctp_association *asoc;
  2597. struct list_head *pos;
  2598. int cnt = 0;
  2599. struct sctp_getaddrs getaddrs;
  2600. struct sctp_transport *from;
  2601. void __user *to;
  2602. union sctp_addr temp;
  2603. struct sctp_sock *sp = sctp_sk(sk);
  2604. int addrlen;
  2605. if (len != sizeof(struct sctp_getaddrs))
  2606. return -EINVAL;
  2607. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  2608. return -EFAULT;
  2609. if (getaddrs.addr_num <= 0) return -EINVAL;
  2610. /* For UDP-style sockets, id specifies the association to query. */
  2611. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2612. if (!asoc)
  2613. return -EINVAL;
  2614. to = (void __user *)getaddrs.addrs;
  2615. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2616. from = list_entry(pos, struct sctp_transport, transports);
  2617. memcpy(&temp, &from->ipaddr, sizeof(temp));
  2618. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2619. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  2620. temp.v4.sin_port = htons(temp.v4.sin_port);
  2621. if (copy_to_user(to, &temp, addrlen))
  2622. return -EFAULT;
  2623. to += addrlen ;
  2624. cnt ++;
  2625. if (cnt >= getaddrs.addr_num) break;
  2626. }
  2627. getaddrs.addr_num = cnt;
  2628. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
  2629. return -EFAULT;
  2630. return 0;
  2631. }
  2632. static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
  2633. char __user *optval,
  2634. int __user *optlen)
  2635. {
  2636. sctp_assoc_t id;
  2637. struct sctp_bind_addr *bp;
  2638. struct sctp_association *asoc;
  2639. struct list_head *pos;
  2640. struct sctp_sockaddr_entry *addr;
  2641. rwlock_t *addr_lock;
  2642. unsigned long flags;
  2643. int cnt = 0;
  2644. if (len != sizeof(sctp_assoc_t))
  2645. return -EINVAL;
  2646. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2647. return -EFAULT;
  2648. /*
  2649. * For UDP-style sockets, id specifies the association to query.
  2650. * If the id field is set to the value '0' then the locally bound
  2651. * addresses are returned without regard to any particular
  2652. * association.
  2653. */
  2654. if (0 == id) {
  2655. bp = &sctp_sk(sk)->ep->base.bind_addr;
  2656. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  2657. } else {
  2658. asoc = sctp_id2assoc(sk, id);
  2659. if (!asoc)
  2660. return -EINVAL;
  2661. bp = &asoc->base.bind_addr;
  2662. addr_lock = &asoc->base.addr_lock;
  2663. }
  2664. sctp_read_lock(addr_lock);
  2665. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  2666. * addresses from the global local address list.
  2667. */
  2668. if (sctp_list_single_entry(&bp->address_list)) {
  2669. addr = list_entry(bp->address_list.next,
  2670. struct sctp_sockaddr_entry, list);
  2671. if (sctp_is_any(&addr->a)) {
  2672. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2673. list_for_each(pos, &sctp_local_addr_list) {
  2674. addr = list_entry(pos,
  2675. struct sctp_sockaddr_entry,
  2676. list);
  2677. if ((PF_INET == sk->sk_family) &&
  2678. (AF_INET6 == addr->a.sa.sa_family))
  2679. continue;
  2680. cnt++;
  2681. }
  2682. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2683. flags);
  2684. } else {
  2685. cnt = 1;
  2686. }
  2687. goto done;
  2688. }
  2689. list_for_each(pos, &bp->address_list) {
  2690. cnt ++;
  2691. }
  2692. done:
  2693. sctp_read_unlock(addr_lock);
  2694. return cnt;
  2695. }
  2696. /* Helper function that copies local addresses to user and returns the number
  2697. * of addresses copied.
  2698. */
  2699. static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, int max_addrs,
  2700. void __user *to)
  2701. {
  2702. struct list_head *pos;
  2703. struct sctp_sockaddr_entry *addr;
  2704. unsigned long flags;
  2705. union sctp_addr temp;
  2706. int cnt = 0;
  2707. int addrlen;
  2708. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2709. list_for_each(pos, &sctp_local_addr_list) {
  2710. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2711. if ((PF_INET == sk->sk_family) &&
  2712. (AF_INET6 == addr->a.sa.sa_family))
  2713. continue;
  2714. memcpy(&temp, &addr->a, sizeof(temp));
  2715. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2716. &temp);
  2717. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2718. temp.v4.sin_port = htons(port);
  2719. if (copy_to_user(to, &temp, addrlen)) {
  2720. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2721. flags);
  2722. return -EFAULT;
  2723. }
  2724. to += addrlen;
  2725. cnt ++;
  2726. if (cnt >= max_addrs) break;
  2727. }
  2728. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  2729. return cnt;
  2730. }
  2731. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  2732. char __user *optval, int __user *optlen)
  2733. {
  2734. struct sctp_bind_addr *bp;
  2735. struct sctp_association *asoc;
  2736. struct list_head *pos;
  2737. int cnt = 0;
  2738. struct sctp_getaddrs getaddrs;
  2739. struct sctp_sockaddr_entry *addr;
  2740. void __user *to;
  2741. union sctp_addr temp;
  2742. struct sctp_sock *sp = sctp_sk(sk);
  2743. int addrlen;
  2744. rwlock_t *addr_lock;
  2745. int err = 0;
  2746. if (len != sizeof(struct sctp_getaddrs))
  2747. return -EINVAL;
  2748. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  2749. return -EFAULT;
  2750. if (getaddrs.addr_num <= 0) return -EINVAL;
  2751. /*
  2752. * For UDP-style sockets, id specifies the association to query.
  2753. * If the id field is set to the value '0' then the locally bound
  2754. * addresses are returned without regard to any particular
  2755. * association.
  2756. */
  2757. if (0 == getaddrs.assoc_id) {
  2758. bp = &sctp_sk(sk)->ep->base.bind_addr;
  2759. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  2760. } else {
  2761. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2762. if (!asoc)
  2763. return -EINVAL;
  2764. bp = &asoc->base.bind_addr;
  2765. addr_lock = &asoc->base.addr_lock;
  2766. }
  2767. to = getaddrs.addrs;
  2768. sctp_read_lock(addr_lock);
  2769. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  2770. * addresses from the global local address list.
  2771. */
  2772. if (sctp_list_single_entry(&bp->address_list)) {
  2773. addr = list_entry(bp->address_list.next,
  2774. struct sctp_sockaddr_entry, list);
  2775. if (sctp_is_any(&addr->a)) {
  2776. cnt = sctp_copy_laddrs_to_user(sk, bp->port,
  2777. getaddrs.addr_num, to);
  2778. if (cnt < 0) {
  2779. err = cnt;
  2780. goto unlock;
  2781. }
  2782. goto copy_getaddrs;
  2783. }
  2784. }
  2785. list_for_each(pos, &bp->address_list) {
  2786. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2787. memcpy(&temp, &addr->a, sizeof(temp));
  2788. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2789. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2790. temp.v4.sin_port = htons(temp.v4.sin_port);
  2791. if (copy_to_user(to, &temp, addrlen)) {
  2792. err = -EFAULT;
  2793. goto unlock;
  2794. }
  2795. to += addrlen;
  2796. cnt ++;
  2797. if (cnt >= getaddrs.addr_num) break;
  2798. }
  2799. copy_getaddrs:
  2800. getaddrs.addr_num = cnt;
  2801. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
  2802. err = -EFAULT;
  2803. unlock:
  2804. sctp_read_unlock(addr_lock);
  2805. return err;
  2806. }
  2807. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2808. *
  2809. * Requests that the local SCTP stack use the enclosed peer address as
  2810. * the association primary. The enclosed address must be one of the
  2811. * association peer's addresses.
  2812. */
  2813. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  2814. char __user *optval, int __user *optlen)
  2815. {
  2816. struct sctp_prim prim;
  2817. struct sctp_association *asoc;
  2818. struct sctp_sock *sp = sctp_sk(sk);
  2819. if (len != sizeof(struct sctp_prim))
  2820. return -EINVAL;
  2821. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2822. return -EFAULT;
  2823. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  2824. if (!asoc)
  2825. return -EINVAL;
  2826. if (!asoc->peer.primary_path)
  2827. return -ENOTCONN;
  2828. asoc->peer.primary_path->ipaddr.v4.sin_port =
  2829. htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
  2830. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  2831. sizeof(union sctp_addr));
  2832. asoc->peer.primary_path->ipaddr.v4.sin_port =
  2833. ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
  2834. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  2835. (union sctp_addr *)&prim.ssp_addr);
  2836. if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
  2837. return -EFAULT;
  2838. return 0;
  2839. }
  2840. /*
  2841. * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
  2842. *
  2843. * Requests that the local endpoint set the specified Adaption Layer
  2844. * Indication parameter for all future INIT and INIT-ACK exchanges.
  2845. */
  2846. static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
  2847. char __user *optval, int __user *optlen)
  2848. {
  2849. __u32 val;
  2850. if (len < sizeof(__u32))
  2851. return -EINVAL;
  2852. len = sizeof(__u32);
  2853. val = sctp_sk(sk)->adaption_ind;
  2854. if (put_user(len, optlen))
  2855. return -EFAULT;
  2856. if (copy_to_user(optval, &val, len))
  2857. return -EFAULT;
  2858. return 0;
  2859. }
  2860. /*
  2861. *
  2862. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2863. *
  2864. * Applications that wish to use the sendto() system call may wish to
  2865. * specify a default set of parameters that would normally be supplied
  2866. * through the inclusion of ancillary data. This socket option allows
  2867. * such an application to set the default sctp_sndrcvinfo structure.
  2868. * The application that wishes to use this socket option simply passes
  2869. * in to this call the sctp_sndrcvinfo structure defined in Section
  2870. * 5.2.2) The input parameters accepted by this call include
  2871. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2872. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2873. * to this call if the caller is using the UDP model.
  2874. *
  2875. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  2876. */
  2877. static int sctp_getsockopt_default_send_param(struct sock *sk,
  2878. int len, char __user *optval,
  2879. int __user *optlen)
  2880. {
  2881. struct sctp_sndrcvinfo info;
  2882. struct sctp_association *asoc;
  2883. struct sctp_sock *sp = sctp_sk(sk);
  2884. if (len != sizeof(struct sctp_sndrcvinfo))
  2885. return -EINVAL;
  2886. if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
  2887. return -EFAULT;
  2888. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2889. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2890. return -EINVAL;
  2891. if (asoc) {
  2892. info.sinfo_stream = asoc->default_stream;
  2893. info.sinfo_flags = asoc->default_flags;
  2894. info.sinfo_ppid = asoc->default_ppid;
  2895. info.sinfo_context = asoc->default_context;
  2896. info.sinfo_timetolive = asoc->default_timetolive;
  2897. } else {
  2898. info.sinfo_stream = sp->default_stream;
  2899. info.sinfo_flags = sp->default_flags;
  2900. info.sinfo_ppid = sp->default_ppid;
  2901. info.sinfo_context = sp->default_context;
  2902. info.sinfo_timetolive = sp->default_timetolive;
  2903. }
  2904. if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
  2905. return -EFAULT;
  2906. return 0;
  2907. }
  2908. /*
  2909. *
  2910. * 7.1.5 SCTP_NODELAY
  2911. *
  2912. * Turn on/off any Nagle-like algorithm. This means that packets are
  2913. * generally sent as soon as possible and no unnecessary delays are
  2914. * introduced, at the cost of more packets in the network. Expects an
  2915. * integer boolean flag.
  2916. */
  2917. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  2918. char __user *optval, int __user *optlen)
  2919. {
  2920. int val;
  2921. if (len < sizeof(int))
  2922. return -EINVAL;
  2923. len = sizeof(int);
  2924. val = (sctp_sk(sk)->nodelay == 1);
  2925. if (put_user(len, optlen))
  2926. return -EFAULT;
  2927. if (copy_to_user(optval, &val, len))
  2928. return -EFAULT;
  2929. return 0;
  2930. }
  2931. /*
  2932. *
  2933. * 7.1.1 SCTP_RTOINFO
  2934. *
  2935. * The protocol parameters used to initialize and bound retransmission
  2936. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2937. * and modify these parameters.
  2938. * All parameters are time values, in milliseconds. A value of 0, when
  2939. * modifying the parameters, indicates that the current value should not
  2940. * be changed.
  2941. *
  2942. */
  2943. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  2944. char __user *optval,
  2945. int __user *optlen) {
  2946. struct sctp_rtoinfo rtoinfo;
  2947. struct sctp_association *asoc;
  2948. if (len != sizeof (struct sctp_rtoinfo))
  2949. return -EINVAL;
  2950. if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
  2951. return -EFAULT;
  2952. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2953. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2954. return -EINVAL;
  2955. /* Values corresponding to the specific association. */
  2956. if (asoc) {
  2957. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  2958. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  2959. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  2960. } else {
  2961. /* Values corresponding to the endpoint. */
  2962. struct sctp_sock *sp = sctp_sk(sk);
  2963. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  2964. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  2965. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  2966. }
  2967. if (put_user(len, optlen))
  2968. return -EFAULT;
  2969. if (copy_to_user(optval, &rtoinfo, len))
  2970. return -EFAULT;
  2971. return 0;
  2972. }
  2973. /*
  2974. *
  2975. * 7.1.2 SCTP_ASSOCINFO
  2976. *
  2977. * This option is used to tune the the maximum retransmission attempts
  2978. * of the association.
  2979. * Returns an error if the new association retransmission value is
  2980. * greater than the sum of the retransmission value of the peer.
  2981. * See [SCTP] for more information.
  2982. *
  2983. */
  2984. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  2985. char __user *optval,
  2986. int __user *optlen)
  2987. {
  2988. struct sctp_assocparams assocparams;
  2989. struct sctp_association *asoc;
  2990. struct list_head *pos;
  2991. int cnt = 0;
  2992. if (len != sizeof (struct sctp_assocparams))
  2993. return -EINVAL;
  2994. if (copy_from_user(&assocparams, optval,
  2995. sizeof (struct sctp_assocparams)))
  2996. return -EFAULT;
  2997. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2998. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2999. return -EINVAL;
  3000. /* Values correspoinding to the specific association */
  3001. if (asoc) {
  3002. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  3003. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  3004. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  3005. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  3006. * 1000) +
  3007. (asoc->cookie_life.tv_usec
  3008. / 1000);
  3009. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3010. cnt ++;
  3011. }
  3012. assocparams.sasoc_number_peer_destinations = cnt;
  3013. } else {
  3014. /* Values corresponding to the endpoint */
  3015. struct sctp_sock *sp = sctp_sk(sk);
  3016. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  3017. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  3018. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  3019. assocparams.sasoc_cookie_life =
  3020. sp->assocparams.sasoc_cookie_life;
  3021. assocparams.sasoc_number_peer_destinations =
  3022. sp->assocparams.
  3023. sasoc_number_peer_destinations;
  3024. }
  3025. if (put_user(len, optlen))
  3026. return -EFAULT;
  3027. if (copy_to_user(optval, &assocparams, len))
  3028. return -EFAULT;
  3029. return 0;
  3030. }
  3031. /*
  3032. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  3033. *
  3034. * This socket option is a boolean flag which turns on or off mapped V4
  3035. * addresses. If this option is turned on and the socket is type
  3036. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  3037. * If this option is turned off, then no mapping will be done of V4
  3038. * addresses and a user will receive both PF_INET6 and PF_INET type
  3039. * addresses on the socket.
  3040. */
  3041. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  3042. char __user *optval, int __user *optlen)
  3043. {
  3044. int val;
  3045. struct sctp_sock *sp = sctp_sk(sk);
  3046. if (len < sizeof(int))
  3047. return -EINVAL;
  3048. len = sizeof(int);
  3049. val = sp->v4mapped;
  3050. if (put_user(len, optlen))
  3051. return -EFAULT;
  3052. if (copy_to_user(optval, &val, len))
  3053. return -EFAULT;
  3054. return 0;
  3055. }
  3056. /*
  3057. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  3058. *
  3059. * This socket option specifies the maximum size to put in any outgoing
  3060. * SCTP chunk. If a message is larger than this size it will be
  3061. * fragmented by SCTP into the specified size. Note that the underlying
  3062. * SCTP implementation may fragment into smaller sized chunks when the
  3063. * PMTU of the underlying association is smaller than the value set by
  3064. * the user.
  3065. */
  3066. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  3067. char __user *optval, int __user *optlen)
  3068. {
  3069. int val;
  3070. if (len < sizeof(int))
  3071. return -EINVAL;
  3072. len = sizeof(int);
  3073. val = sctp_sk(sk)->user_frag;
  3074. if (put_user(len, optlen))
  3075. return -EFAULT;
  3076. if (copy_to_user(optval, &val, len))
  3077. return -EFAULT;
  3078. return 0;
  3079. }
  3080. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  3081. char __user *optval, int __user *optlen)
  3082. {
  3083. int retval = 0;
  3084. int len;
  3085. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
  3086. /* I can hardly begin to describe how wrong this is. This is
  3087. * so broken as to be worse than useless. The API draft
  3088. * REALLY is NOT helpful here... I am not convinced that the
  3089. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  3090. * are at all well-founded.
  3091. */
  3092. if (level != SOL_SCTP) {
  3093. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3094. retval = af->getsockopt(sk, level, optname, optval, optlen);
  3095. return retval;
  3096. }
  3097. if (get_user(len, optlen))
  3098. return -EFAULT;
  3099. sctp_lock_sock(sk);
  3100. switch (optname) {
  3101. case SCTP_STATUS:
  3102. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  3103. break;
  3104. case SCTP_DISABLE_FRAGMENTS:
  3105. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  3106. optlen);
  3107. break;
  3108. case SCTP_EVENTS:
  3109. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  3110. break;
  3111. case SCTP_AUTOCLOSE:
  3112. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  3113. break;
  3114. case SCTP_SOCKOPT_PEELOFF:
  3115. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  3116. break;
  3117. case SCTP_PEER_ADDR_PARAMS:
  3118. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  3119. optlen);
  3120. break;
  3121. case SCTP_INITMSG:
  3122. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  3123. break;
  3124. case SCTP_GET_PEER_ADDRS_NUM:
  3125. retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
  3126. optlen);
  3127. break;
  3128. case SCTP_GET_LOCAL_ADDRS_NUM:
  3129. retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
  3130. optlen);
  3131. break;
  3132. case SCTP_GET_PEER_ADDRS:
  3133. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  3134. optlen);
  3135. break;
  3136. case SCTP_GET_LOCAL_ADDRS:
  3137. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  3138. optlen);
  3139. break;
  3140. case SCTP_DEFAULT_SEND_PARAM:
  3141. retval = sctp_getsockopt_default_send_param(sk, len,
  3142. optval, optlen);
  3143. break;
  3144. case SCTP_PRIMARY_ADDR:
  3145. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  3146. break;
  3147. case SCTP_NODELAY:
  3148. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  3149. break;
  3150. case SCTP_RTOINFO:
  3151. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  3152. break;
  3153. case SCTP_ASSOCINFO:
  3154. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  3155. break;
  3156. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3157. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  3158. break;
  3159. case SCTP_MAXSEG:
  3160. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  3161. break;
  3162. case SCTP_GET_PEER_ADDR_INFO:
  3163. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  3164. optlen);
  3165. break;
  3166. case SCTP_ADAPTION_LAYER:
  3167. retval = sctp_getsockopt_adaption_layer(sk, len, optval,
  3168. optlen);
  3169. break;
  3170. default:
  3171. retval = -ENOPROTOOPT;
  3172. break;
  3173. };
  3174. sctp_release_sock(sk);
  3175. return retval;
  3176. }
  3177. static void sctp_hash(struct sock *sk)
  3178. {
  3179. /* STUB */
  3180. }
  3181. static void sctp_unhash(struct sock *sk)
  3182. {
  3183. /* STUB */
  3184. }
  3185. /* Check if port is acceptable. Possibly find first available port.
  3186. *
  3187. * The port hash table (contained in the 'global' SCTP protocol storage
  3188. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  3189. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  3190. * list (the list number is the port number hashed out, so as you
  3191. * would expect from a hash function, all the ports in a given list have
  3192. * such a number that hashes out to the same list number; you were
  3193. * expecting that, right?); so each list has a set of ports, with a
  3194. * link to the socket (struct sock) that uses it, the port number and
  3195. * a fastreuse flag (FIXME: NPI ipg).
  3196. */
  3197. static struct sctp_bind_bucket *sctp_bucket_create(
  3198. struct sctp_bind_hashbucket *head, unsigned short snum);
  3199. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  3200. {
  3201. struct sctp_bind_hashbucket *head; /* hash list */
  3202. struct sctp_bind_bucket *pp; /* hash list port iterator */
  3203. unsigned short snum;
  3204. int ret;
  3205. /* NOTE: Remember to put this back to net order. */
  3206. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  3207. snum = addr->v4.sin_port;
  3208. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  3209. sctp_local_bh_disable();
  3210. if (snum == 0) {
  3211. /* Search for an available port.
  3212. *
  3213. * 'sctp_port_rover' was the last port assigned, so
  3214. * we start to search from 'sctp_port_rover +
  3215. * 1'. What we do is first check if port 'rover' is
  3216. * already in the hash table; if not, we use that; if
  3217. * it is, we try next.
  3218. */
  3219. int low = sysctl_local_port_range[0];
  3220. int high = sysctl_local_port_range[1];
  3221. int remaining = (high - low) + 1;
  3222. int rover;
  3223. int index;
  3224. sctp_spin_lock(&sctp_port_alloc_lock);
  3225. rover = sctp_port_rover;
  3226. do {
  3227. rover++;
  3228. if ((rover < low) || (rover > high))
  3229. rover = low;
  3230. index = sctp_phashfn(rover);
  3231. head = &sctp_port_hashtable[index];
  3232. sctp_spin_lock(&head->lock);
  3233. for (pp = head->chain; pp; pp = pp->next)
  3234. if (pp->port == rover)
  3235. goto next;
  3236. break;
  3237. next:
  3238. sctp_spin_unlock(&head->lock);
  3239. } while (--remaining > 0);
  3240. sctp_port_rover = rover;
  3241. sctp_spin_unlock(&sctp_port_alloc_lock);
  3242. /* Exhausted local port range during search? */
  3243. ret = 1;
  3244. if (remaining <= 0)
  3245. goto fail;
  3246. /* OK, here is the one we will use. HEAD (the port
  3247. * hash table list entry) is non-NULL and we hold it's
  3248. * mutex.
  3249. */
  3250. snum = rover;
  3251. } else {
  3252. /* We are given an specific port number; we verify
  3253. * that it is not being used. If it is used, we will
  3254. * exahust the search in the hash list corresponding
  3255. * to the port number (snum) - we detect that with the
  3256. * port iterator, pp being NULL.
  3257. */
  3258. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  3259. sctp_spin_lock(&head->lock);
  3260. for (pp = head->chain; pp; pp = pp->next) {
  3261. if (pp->port == snum)
  3262. goto pp_found;
  3263. }
  3264. }
  3265. pp = NULL;
  3266. goto pp_not_found;
  3267. pp_found:
  3268. if (!hlist_empty(&pp->owner)) {
  3269. /* We had a port hash table hit - there is an
  3270. * available port (pp != NULL) and it is being
  3271. * used by other socket (pp->owner not empty); that other
  3272. * socket is going to be sk2.
  3273. */
  3274. int reuse = sk->sk_reuse;
  3275. struct sock *sk2;
  3276. struct hlist_node *node;
  3277. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  3278. if (pp->fastreuse && sk->sk_reuse)
  3279. goto success;
  3280. /* Run through the list of sockets bound to the port
  3281. * (pp->port) [via the pointers bind_next and
  3282. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  3283. * we get the endpoint they describe and run through
  3284. * the endpoint's list of IP (v4 or v6) addresses,
  3285. * comparing each of the addresses with the address of
  3286. * the socket sk. If we find a match, then that means
  3287. * that this port/socket (sk) combination are already
  3288. * in an endpoint.
  3289. */
  3290. sk_for_each_bound(sk2, node, &pp->owner) {
  3291. struct sctp_endpoint *ep2;
  3292. ep2 = sctp_sk(sk2)->ep;
  3293. if (reuse && sk2->sk_reuse)
  3294. continue;
  3295. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  3296. sctp_sk(sk))) {
  3297. ret = (long)sk2;
  3298. goto fail_unlock;
  3299. }
  3300. }
  3301. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  3302. }
  3303. pp_not_found:
  3304. /* If there was a hash table miss, create a new port. */
  3305. ret = 1;
  3306. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  3307. goto fail_unlock;
  3308. /* In either case (hit or miss), make sure fastreuse is 1 only
  3309. * if sk->sk_reuse is too (that is, if the caller requested
  3310. * SO_REUSEADDR on this socket -sk-).
  3311. */
  3312. if (hlist_empty(&pp->owner))
  3313. pp->fastreuse = sk->sk_reuse ? 1 : 0;
  3314. else if (pp->fastreuse && !sk->sk_reuse)
  3315. pp->fastreuse = 0;
  3316. /* We are set, so fill up all the data in the hash table
  3317. * entry, tie the socket list information with the rest of the
  3318. * sockets FIXME: Blurry, NPI (ipg).
  3319. */
  3320. success:
  3321. inet_sk(sk)->num = snum;
  3322. if (!sctp_sk(sk)->bind_hash) {
  3323. sk_add_bind_node(sk, &pp->owner);
  3324. sctp_sk(sk)->bind_hash = pp;
  3325. }
  3326. ret = 0;
  3327. fail_unlock:
  3328. sctp_spin_unlock(&head->lock);
  3329. fail:
  3330. sctp_local_bh_enable();
  3331. addr->v4.sin_port = htons(addr->v4.sin_port);
  3332. return ret;
  3333. }
  3334. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  3335. * port is requested.
  3336. */
  3337. static int sctp_get_port(struct sock *sk, unsigned short snum)
  3338. {
  3339. long ret;
  3340. union sctp_addr addr;
  3341. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3342. /* Set up a dummy address struct from the sk. */
  3343. af->from_sk(&addr, sk);
  3344. addr.v4.sin_port = htons(snum);
  3345. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  3346. ret = sctp_get_port_local(sk, &addr);
  3347. return (ret ? 1 : 0);
  3348. }
  3349. /*
  3350. * 3.1.3 listen() - UDP Style Syntax
  3351. *
  3352. * By default, new associations are not accepted for UDP style sockets.
  3353. * An application uses listen() to mark a socket as being able to
  3354. * accept new associations.
  3355. */
  3356. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  3357. {
  3358. struct sctp_sock *sp = sctp_sk(sk);
  3359. struct sctp_endpoint *ep = sp->ep;
  3360. /* Only UDP style sockets that are not peeled off are allowed to
  3361. * listen().
  3362. */
  3363. if (!sctp_style(sk, UDP))
  3364. return -EINVAL;
  3365. /* If backlog is zero, disable listening. */
  3366. if (!backlog) {
  3367. if (sctp_sstate(sk, CLOSED))
  3368. return 0;
  3369. sctp_unhash_endpoint(ep);
  3370. sk->sk_state = SCTP_SS_CLOSED;
  3371. }
  3372. /* Return if we are already listening. */
  3373. if (sctp_sstate(sk, LISTENING))
  3374. return 0;
  3375. /*
  3376. * If a bind() or sctp_bindx() is not called prior to a listen()
  3377. * call that allows new associations to be accepted, the system
  3378. * picks an ephemeral port and will choose an address set equivalent
  3379. * to binding with a wildcard address.
  3380. *
  3381. * This is not currently spelled out in the SCTP sockets
  3382. * extensions draft, but follows the practice as seen in TCP
  3383. * sockets.
  3384. */
  3385. if (!ep->base.bind_addr.port) {
  3386. if (sctp_autobind(sk))
  3387. return -EAGAIN;
  3388. }
  3389. sk->sk_state = SCTP_SS_LISTENING;
  3390. sctp_hash_endpoint(ep);
  3391. return 0;
  3392. }
  3393. /*
  3394. * 4.1.3 listen() - TCP Style Syntax
  3395. *
  3396. * Applications uses listen() to ready the SCTP endpoint for accepting
  3397. * inbound associations.
  3398. */
  3399. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  3400. {
  3401. struct sctp_sock *sp = sctp_sk(sk);
  3402. struct sctp_endpoint *ep = sp->ep;
  3403. /* If backlog is zero, disable listening. */
  3404. if (!backlog) {
  3405. if (sctp_sstate(sk, CLOSED))
  3406. return 0;
  3407. sctp_unhash_endpoint(ep);
  3408. sk->sk_state = SCTP_SS_CLOSED;
  3409. }
  3410. if (sctp_sstate(sk, LISTENING))
  3411. return 0;
  3412. /*
  3413. * If a bind() or sctp_bindx() is not called prior to a listen()
  3414. * call that allows new associations to be accepted, the system
  3415. * picks an ephemeral port and will choose an address set equivalent
  3416. * to binding with a wildcard address.
  3417. *
  3418. * This is not currently spelled out in the SCTP sockets
  3419. * extensions draft, but follows the practice as seen in TCP
  3420. * sockets.
  3421. */
  3422. if (!ep->base.bind_addr.port) {
  3423. if (sctp_autobind(sk))
  3424. return -EAGAIN;
  3425. }
  3426. sk->sk_state = SCTP_SS_LISTENING;
  3427. sk->sk_max_ack_backlog = backlog;
  3428. sctp_hash_endpoint(ep);
  3429. return 0;
  3430. }
  3431. /*
  3432. * Move a socket to LISTENING state.
  3433. */
  3434. int sctp_inet_listen(struct socket *sock, int backlog)
  3435. {
  3436. struct sock *sk = sock->sk;
  3437. struct crypto_tfm *tfm=NULL;
  3438. int err = -EINVAL;
  3439. if (unlikely(backlog < 0))
  3440. goto out;
  3441. sctp_lock_sock(sk);
  3442. if (sock->state != SS_UNCONNECTED)
  3443. goto out;
  3444. /* Allocate HMAC for generating cookie. */
  3445. if (sctp_hmac_alg) {
  3446. tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
  3447. if (!tfm) {
  3448. err = -ENOSYS;
  3449. goto out;
  3450. }
  3451. }
  3452. switch (sock->type) {
  3453. case SOCK_SEQPACKET:
  3454. err = sctp_seqpacket_listen(sk, backlog);
  3455. break;
  3456. case SOCK_STREAM:
  3457. err = sctp_stream_listen(sk, backlog);
  3458. break;
  3459. default:
  3460. break;
  3461. };
  3462. if (err)
  3463. goto cleanup;
  3464. /* Store away the transform reference. */
  3465. sctp_sk(sk)->hmac = tfm;
  3466. out:
  3467. sctp_release_sock(sk);
  3468. return err;
  3469. cleanup:
  3470. if (tfm)
  3471. sctp_crypto_free_tfm(tfm);
  3472. goto out;
  3473. }
  3474. /*
  3475. * This function is done by modeling the current datagram_poll() and the
  3476. * tcp_poll(). Note that, based on these implementations, we don't
  3477. * lock the socket in this function, even though it seems that,
  3478. * ideally, locking or some other mechanisms can be used to ensure
  3479. * the integrity of the counters (sndbuf and wmem_queued) used
  3480. * in this place. We assume that we don't need locks either until proven
  3481. * otherwise.
  3482. *
  3483. * Another thing to note is that we include the Async I/O support
  3484. * here, again, by modeling the current TCP/UDP code. We don't have
  3485. * a good way to test with it yet.
  3486. */
  3487. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  3488. {
  3489. struct sock *sk = sock->sk;
  3490. struct sctp_sock *sp = sctp_sk(sk);
  3491. unsigned int mask;
  3492. poll_wait(file, sk->sk_sleep, wait);
  3493. /* A TCP-style listening socket becomes readable when the accept queue
  3494. * is not empty.
  3495. */
  3496. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3497. return (!list_empty(&sp->ep->asocs)) ?
  3498. (POLLIN | POLLRDNORM) : 0;
  3499. mask = 0;
  3500. /* Is there any exceptional events? */
  3501. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  3502. mask |= POLLERR;
  3503. if (sk->sk_shutdown == SHUTDOWN_MASK)
  3504. mask |= POLLHUP;
  3505. /* Is it readable? Reconsider this code with TCP-style support. */
  3506. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  3507. (sk->sk_shutdown & RCV_SHUTDOWN))
  3508. mask |= POLLIN | POLLRDNORM;
  3509. /* The association is either gone or not ready. */
  3510. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  3511. return mask;
  3512. /* Is it writable? */
  3513. if (sctp_writeable(sk)) {
  3514. mask |= POLLOUT | POLLWRNORM;
  3515. } else {
  3516. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  3517. /*
  3518. * Since the socket is not locked, the buffer
  3519. * might be made available after the writeable check and
  3520. * before the bit is set. This could cause a lost I/O
  3521. * signal. tcp_poll() has a race breaker for this race
  3522. * condition. Based on their implementation, we put
  3523. * in the following code to cover it as well.
  3524. */
  3525. if (sctp_writeable(sk))
  3526. mask |= POLLOUT | POLLWRNORM;
  3527. }
  3528. return mask;
  3529. }
  3530. /********************************************************************
  3531. * 2nd Level Abstractions
  3532. ********************************************************************/
  3533. static struct sctp_bind_bucket *sctp_bucket_create(
  3534. struct sctp_bind_hashbucket *head, unsigned short snum)
  3535. {
  3536. struct sctp_bind_bucket *pp;
  3537. pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
  3538. SCTP_DBG_OBJCNT_INC(bind_bucket);
  3539. if (pp) {
  3540. pp->port = snum;
  3541. pp->fastreuse = 0;
  3542. INIT_HLIST_HEAD(&pp->owner);
  3543. if ((pp->next = head->chain) != NULL)
  3544. pp->next->pprev = &pp->next;
  3545. head->chain = pp;
  3546. pp->pprev = &head->chain;
  3547. }
  3548. return pp;
  3549. }
  3550. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  3551. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  3552. {
  3553. if (hlist_empty(&pp->owner)) {
  3554. if (pp->next)
  3555. pp->next->pprev = pp->pprev;
  3556. *(pp->pprev) = pp->next;
  3557. kmem_cache_free(sctp_bucket_cachep, pp);
  3558. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  3559. }
  3560. }
  3561. /* Release this socket's reference to a local port. */
  3562. static inline void __sctp_put_port(struct sock *sk)
  3563. {
  3564. struct sctp_bind_hashbucket *head =
  3565. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  3566. struct sctp_bind_bucket *pp;
  3567. sctp_spin_lock(&head->lock);
  3568. pp = sctp_sk(sk)->bind_hash;
  3569. __sk_del_bind_node(sk);
  3570. sctp_sk(sk)->bind_hash = NULL;
  3571. inet_sk(sk)->num = 0;
  3572. sctp_bucket_destroy(pp);
  3573. sctp_spin_unlock(&head->lock);
  3574. }
  3575. void sctp_put_port(struct sock *sk)
  3576. {
  3577. sctp_local_bh_disable();
  3578. __sctp_put_port(sk);
  3579. sctp_local_bh_enable();
  3580. }
  3581. /*
  3582. * The system picks an ephemeral port and choose an address set equivalent
  3583. * to binding with a wildcard address.
  3584. * One of those addresses will be the primary address for the association.
  3585. * This automatically enables the multihoming capability of SCTP.
  3586. */
  3587. static int sctp_autobind(struct sock *sk)
  3588. {
  3589. union sctp_addr autoaddr;
  3590. struct sctp_af *af;
  3591. unsigned short port;
  3592. /* Initialize a local sockaddr structure to INADDR_ANY. */
  3593. af = sctp_sk(sk)->pf->af;
  3594. port = htons(inet_sk(sk)->num);
  3595. af->inaddr_any(&autoaddr, port);
  3596. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  3597. }
  3598. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  3599. *
  3600. * From RFC 2292
  3601. * 4.2 The cmsghdr Structure *
  3602. *
  3603. * When ancillary data is sent or received, any number of ancillary data
  3604. * objects can be specified by the msg_control and msg_controllen members of
  3605. * the msghdr structure, because each object is preceded by
  3606. * a cmsghdr structure defining the object's length (the cmsg_len member).
  3607. * Historically Berkeley-derived implementations have passed only one object
  3608. * at a time, but this API allows multiple objects to be
  3609. * passed in a single call to sendmsg() or recvmsg(). The following example
  3610. * shows two ancillary data objects in a control buffer.
  3611. *
  3612. * |<--------------------------- msg_controllen -------------------------->|
  3613. * | |
  3614. *
  3615. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  3616. *
  3617. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  3618. * | | |
  3619. *
  3620. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  3621. *
  3622. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  3623. * | | | | |
  3624. *
  3625. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3626. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  3627. *
  3628. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  3629. *
  3630. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3631. * ^
  3632. * |
  3633. *
  3634. * msg_control
  3635. * points here
  3636. */
  3637. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  3638. sctp_cmsgs_t *cmsgs)
  3639. {
  3640. struct cmsghdr *cmsg;
  3641. for (cmsg = CMSG_FIRSTHDR(msg);
  3642. cmsg != NULL;
  3643. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  3644. if (!CMSG_OK(msg, cmsg))
  3645. return -EINVAL;
  3646. /* Should we parse this header or ignore? */
  3647. if (cmsg->cmsg_level != IPPROTO_SCTP)
  3648. continue;
  3649. /* Strictly check lengths following example in SCM code. */
  3650. switch (cmsg->cmsg_type) {
  3651. case SCTP_INIT:
  3652. /* SCTP Socket API Extension
  3653. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  3654. *
  3655. * This cmsghdr structure provides information for
  3656. * initializing new SCTP associations with sendmsg().
  3657. * The SCTP_INITMSG socket option uses this same data
  3658. * structure. This structure is not used for
  3659. * recvmsg().
  3660. *
  3661. * cmsg_level cmsg_type cmsg_data[]
  3662. * ------------ ------------ ----------------------
  3663. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  3664. */
  3665. if (cmsg->cmsg_len !=
  3666. CMSG_LEN(sizeof(struct sctp_initmsg)))
  3667. return -EINVAL;
  3668. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  3669. break;
  3670. case SCTP_SNDRCV:
  3671. /* SCTP Socket API Extension
  3672. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  3673. *
  3674. * This cmsghdr structure specifies SCTP options for
  3675. * sendmsg() and describes SCTP header information
  3676. * about a received message through recvmsg().
  3677. *
  3678. * cmsg_level cmsg_type cmsg_data[]
  3679. * ------------ ------------ ----------------------
  3680. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  3681. */
  3682. if (cmsg->cmsg_len !=
  3683. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  3684. return -EINVAL;
  3685. cmsgs->info =
  3686. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  3687. /* Minimally, validate the sinfo_flags. */
  3688. if (cmsgs->info->sinfo_flags &
  3689. ~(MSG_UNORDERED | MSG_ADDR_OVER |
  3690. MSG_ABORT | MSG_EOF))
  3691. return -EINVAL;
  3692. break;
  3693. default:
  3694. return -EINVAL;
  3695. };
  3696. }
  3697. return 0;
  3698. }
  3699. /*
  3700. * Wait for a packet..
  3701. * Note: This function is the same function as in core/datagram.c
  3702. * with a few modifications to make lksctp work.
  3703. */
  3704. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  3705. {
  3706. int error;
  3707. DEFINE_WAIT(wait);
  3708. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  3709. /* Socket errors? */
  3710. error = sock_error(sk);
  3711. if (error)
  3712. goto out;
  3713. if (!skb_queue_empty(&sk->sk_receive_queue))
  3714. goto ready;
  3715. /* Socket shut down? */
  3716. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3717. goto out;
  3718. /* Sequenced packets can come disconnected. If so we report the
  3719. * problem.
  3720. */
  3721. error = -ENOTCONN;
  3722. /* Is there a good reason to think that we may receive some data? */
  3723. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  3724. goto out;
  3725. /* Handle signals. */
  3726. if (signal_pending(current))
  3727. goto interrupted;
  3728. /* Let another process have a go. Since we are going to sleep
  3729. * anyway. Note: This may cause odd behaviors if the message
  3730. * does not fit in the user's buffer, but this seems to be the
  3731. * only way to honor MSG_DONTWAIT realistically.
  3732. */
  3733. sctp_release_sock(sk);
  3734. *timeo_p = schedule_timeout(*timeo_p);
  3735. sctp_lock_sock(sk);
  3736. ready:
  3737. finish_wait(sk->sk_sleep, &wait);
  3738. return 0;
  3739. interrupted:
  3740. error = sock_intr_errno(*timeo_p);
  3741. out:
  3742. finish_wait(sk->sk_sleep, &wait);
  3743. *err = error;
  3744. return error;
  3745. }
  3746. /* Receive a datagram.
  3747. * Note: This is pretty much the same routine as in core/datagram.c
  3748. * with a few changes to make lksctp work.
  3749. */
  3750. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  3751. int noblock, int *err)
  3752. {
  3753. int error;
  3754. struct sk_buff *skb;
  3755. long timeo;
  3756. /* Caller is allowed not to check sk->sk_err before calling. */
  3757. error = sock_error(sk);
  3758. if (error)
  3759. goto no_packet;
  3760. timeo = sock_rcvtimeo(sk, noblock);
  3761. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  3762. timeo, MAX_SCHEDULE_TIMEOUT);
  3763. do {
  3764. /* Again only user level code calls this function,
  3765. * so nothing interrupt level
  3766. * will suddenly eat the receive_queue.
  3767. *
  3768. * Look at current nfs client by the way...
  3769. * However, this function was corrent in any case. 8)
  3770. */
  3771. if (flags & MSG_PEEK) {
  3772. unsigned long cpu_flags;
  3773. sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
  3774. cpu_flags);
  3775. skb = skb_peek(&sk->sk_receive_queue);
  3776. if (skb)
  3777. atomic_inc(&skb->users);
  3778. sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
  3779. cpu_flags);
  3780. } else {
  3781. skb = skb_dequeue(&sk->sk_receive_queue);
  3782. }
  3783. if (skb)
  3784. return skb;
  3785. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3786. break;
  3787. /* User doesn't want to wait. */
  3788. error = -EAGAIN;
  3789. if (!timeo)
  3790. goto no_packet;
  3791. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  3792. return NULL;
  3793. no_packet:
  3794. *err = error;
  3795. return NULL;
  3796. }
  3797. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  3798. static void __sctp_write_space(struct sctp_association *asoc)
  3799. {
  3800. struct sock *sk = asoc->base.sk;
  3801. struct socket *sock = sk->sk_socket;
  3802. if ((sctp_wspace(asoc) > 0) && sock) {
  3803. if (waitqueue_active(&asoc->wait))
  3804. wake_up_interruptible(&asoc->wait);
  3805. if (sctp_writeable(sk)) {
  3806. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  3807. wake_up_interruptible(sk->sk_sleep);
  3808. /* Note that we try to include the Async I/O support
  3809. * here by modeling from the current TCP/UDP code.
  3810. * We have not tested with it yet.
  3811. */
  3812. if (sock->fasync_list &&
  3813. !(sk->sk_shutdown & SEND_SHUTDOWN))
  3814. sock_wake_async(sock, 2, POLL_OUT);
  3815. }
  3816. }
  3817. }
  3818. /* Do accounting for the sndbuf space.
  3819. * Decrement the used sndbuf space of the corresponding association by the
  3820. * data size which was just transmitted(freed).
  3821. */
  3822. static void sctp_wfree(struct sk_buff *skb)
  3823. {
  3824. struct sctp_association *asoc;
  3825. struct sctp_chunk *chunk;
  3826. struct sock *sk;
  3827. /* Get the saved chunk pointer. */
  3828. chunk = *((struct sctp_chunk **)(skb->cb));
  3829. asoc = chunk->asoc;
  3830. sk = asoc->base.sk;
  3831. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  3832. sizeof(struct sk_buff) +
  3833. sizeof(struct sctp_chunk);
  3834. sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
  3835. sizeof(struct sk_buff) +
  3836. sizeof(struct sctp_chunk);
  3837. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  3838. sock_wfree(skb);
  3839. __sctp_write_space(asoc);
  3840. sctp_association_put(asoc);
  3841. }
  3842. /* Helper function to wait for space in the sndbuf. */
  3843. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  3844. size_t msg_len)
  3845. {
  3846. struct sock *sk = asoc->base.sk;
  3847. int err = 0;
  3848. long current_timeo = *timeo_p;
  3849. DEFINE_WAIT(wait);
  3850. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  3851. asoc, (long)(*timeo_p), msg_len);
  3852. /* Increment the association's refcnt. */
  3853. sctp_association_hold(asoc);
  3854. /* Wait on the association specific sndbuf space. */
  3855. for (;;) {
  3856. prepare_to_wait_exclusive(&asoc->wait, &wait,
  3857. TASK_INTERRUPTIBLE);
  3858. if (!*timeo_p)
  3859. goto do_nonblock;
  3860. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  3861. asoc->base.dead)
  3862. goto do_error;
  3863. if (signal_pending(current))
  3864. goto do_interrupted;
  3865. if (msg_len <= sctp_wspace(asoc))
  3866. break;
  3867. /* Let another process have a go. Since we are going
  3868. * to sleep anyway.
  3869. */
  3870. sctp_release_sock(sk);
  3871. current_timeo = schedule_timeout(current_timeo);
  3872. sctp_lock_sock(sk);
  3873. *timeo_p = current_timeo;
  3874. }
  3875. out:
  3876. finish_wait(&asoc->wait, &wait);
  3877. /* Release the association's refcnt. */
  3878. sctp_association_put(asoc);
  3879. return err;
  3880. do_error:
  3881. err = -EPIPE;
  3882. goto out;
  3883. do_interrupted:
  3884. err = sock_intr_errno(*timeo_p);
  3885. goto out;
  3886. do_nonblock:
  3887. err = -EAGAIN;
  3888. goto out;
  3889. }
  3890. /* If socket sndbuf has changed, wake up all per association waiters. */
  3891. void sctp_write_space(struct sock *sk)
  3892. {
  3893. struct sctp_association *asoc;
  3894. struct list_head *pos;
  3895. /* Wake up the tasks in each wait queue. */
  3896. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  3897. asoc = list_entry(pos, struct sctp_association, asocs);
  3898. __sctp_write_space(asoc);
  3899. }
  3900. }
  3901. /* Is there any sndbuf space available on the socket?
  3902. *
  3903. * Note that wmem_queued is the sum of the send buffers on all of the
  3904. * associations on the same socket. For a UDP-style socket with
  3905. * multiple associations, it is possible for it to be "unwriteable"
  3906. * prematurely. I assume that this is acceptable because
  3907. * a premature "unwriteable" is better than an accidental "writeable" which
  3908. * would cause an unwanted block under certain circumstances. For the 1-1
  3909. * UDP-style sockets or TCP-style sockets, this code should work.
  3910. * - Daisy
  3911. */
  3912. static int sctp_writeable(struct sock *sk)
  3913. {
  3914. int amt = 0;
  3915. amt = sk->sk_sndbuf - sk->sk_wmem_queued;
  3916. if (amt < 0)
  3917. amt = 0;
  3918. return amt;
  3919. }
  3920. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  3921. * returns immediately with EINPROGRESS.
  3922. */
  3923. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  3924. {
  3925. struct sock *sk = asoc->base.sk;
  3926. int err = 0;
  3927. long current_timeo = *timeo_p;
  3928. DEFINE_WAIT(wait);
  3929. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  3930. (long)(*timeo_p));
  3931. /* Increment the association's refcnt. */
  3932. sctp_association_hold(asoc);
  3933. for (;;) {
  3934. prepare_to_wait_exclusive(&asoc->wait, &wait,
  3935. TASK_INTERRUPTIBLE);
  3936. if (!*timeo_p)
  3937. goto do_nonblock;
  3938. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3939. break;
  3940. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  3941. asoc->base.dead)
  3942. goto do_error;
  3943. if (signal_pending(current))
  3944. goto do_interrupted;
  3945. if (sctp_state(asoc, ESTABLISHED))
  3946. break;
  3947. /* Let another process have a go. Since we are going
  3948. * to sleep anyway.
  3949. */
  3950. sctp_release_sock(sk);
  3951. current_timeo = schedule_timeout(current_timeo);
  3952. sctp_lock_sock(sk);
  3953. *timeo_p = current_timeo;
  3954. }
  3955. out:
  3956. finish_wait(&asoc->wait, &wait);
  3957. /* Release the association's refcnt. */
  3958. sctp_association_put(asoc);
  3959. return err;
  3960. do_error:
  3961. if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
  3962. asoc->max_init_attempts)
  3963. err = -ETIMEDOUT;
  3964. else
  3965. err = -ECONNREFUSED;
  3966. goto out;
  3967. do_interrupted:
  3968. err = sock_intr_errno(*timeo_p);
  3969. goto out;
  3970. do_nonblock:
  3971. err = -EINPROGRESS;
  3972. goto out;
  3973. }
  3974. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  3975. {
  3976. struct sctp_endpoint *ep;
  3977. int err = 0;
  3978. DEFINE_WAIT(wait);
  3979. ep = sctp_sk(sk)->ep;
  3980. for (;;) {
  3981. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  3982. TASK_INTERRUPTIBLE);
  3983. if (list_empty(&ep->asocs)) {
  3984. sctp_release_sock(sk);
  3985. timeo = schedule_timeout(timeo);
  3986. sctp_lock_sock(sk);
  3987. }
  3988. err = -EINVAL;
  3989. if (!sctp_sstate(sk, LISTENING))
  3990. break;
  3991. err = 0;
  3992. if (!list_empty(&ep->asocs))
  3993. break;
  3994. err = sock_intr_errno(timeo);
  3995. if (signal_pending(current))
  3996. break;
  3997. err = -EAGAIN;
  3998. if (!timeo)
  3999. break;
  4000. }
  4001. finish_wait(sk->sk_sleep, &wait);
  4002. return err;
  4003. }
  4004. void sctp_wait_for_close(struct sock *sk, long timeout)
  4005. {
  4006. DEFINE_WAIT(wait);
  4007. do {
  4008. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4009. if (list_empty(&sctp_sk(sk)->ep->asocs))
  4010. break;
  4011. sctp_release_sock(sk);
  4012. timeout = schedule_timeout(timeout);
  4013. sctp_lock_sock(sk);
  4014. } while (!signal_pending(current) && timeout);
  4015. finish_wait(sk->sk_sleep, &wait);
  4016. }
  4017. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  4018. * and its messages to the newsk.
  4019. */
  4020. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  4021. struct sctp_association *assoc,
  4022. sctp_socket_type_t type)
  4023. {
  4024. struct sctp_sock *oldsp = sctp_sk(oldsk);
  4025. struct sctp_sock *newsp = sctp_sk(newsk);
  4026. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4027. struct sctp_endpoint *newep = newsp->ep;
  4028. struct sk_buff *skb, *tmp;
  4029. struct sctp_ulpevent *event;
  4030. /* Migrate socket buffer sizes and all the socket level options to the
  4031. * new socket.
  4032. */
  4033. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  4034. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  4035. /* Brute force copy old sctp opt. */
  4036. inet_sk_copy_descendant(newsk, oldsk);
  4037. /* Restore the ep value that was overwritten with the above structure
  4038. * copy.
  4039. */
  4040. newsp->ep = newep;
  4041. newsp->hmac = NULL;
  4042. /* Hook this new socket in to the bind_hash list. */
  4043. pp = sctp_sk(oldsk)->bind_hash;
  4044. sk_add_bind_node(newsk, &pp->owner);
  4045. sctp_sk(newsk)->bind_hash = pp;
  4046. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  4047. /* Move any messages in the old socket's receive queue that are for the
  4048. * peeled off association to the new socket's receive queue.
  4049. */
  4050. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  4051. event = sctp_skb2event(skb);
  4052. if (event->asoc == assoc) {
  4053. __skb_unlink(skb, skb->list);
  4054. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  4055. }
  4056. }
  4057. /* Clean up any messages pending delivery due to partial
  4058. * delivery. Three cases:
  4059. * 1) No partial deliver; no work.
  4060. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  4061. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  4062. */
  4063. skb_queue_head_init(&newsp->pd_lobby);
  4064. sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
  4065. if (sctp_sk(oldsk)->pd_mode) {
  4066. struct sk_buff_head *queue;
  4067. /* Decide which queue to move pd_lobby skbs to. */
  4068. if (assoc->ulpq.pd_mode) {
  4069. queue = &newsp->pd_lobby;
  4070. } else
  4071. queue = &newsk->sk_receive_queue;
  4072. /* Walk through the pd_lobby, looking for skbs that
  4073. * need moved to the new socket.
  4074. */
  4075. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  4076. event = sctp_skb2event(skb);
  4077. if (event->asoc == assoc) {
  4078. __skb_unlink(skb, skb->list);
  4079. __skb_queue_tail(queue, skb);
  4080. }
  4081. }
  4082. /* Clear up any skbs waiting for the partial
  4083. * delivery to finish.
  4084. */
  4085. if (assoc->ulpq.pd_mode)
  4086. sctp_clear_pd(oldsk);
  4087. }
  4088. /* Set the type of socket to indicate that it is peeled off from the
  4089. * original UDP-style socket or created with the accept() call on a
  4090. * TCP-style socket..
  4091. */
  4092. newsp->type = type;
  4093. /* Migrate the association to the new socket. */
  4094. sctp_assoc_migrate(assoc, newsk);
  4095. /* If the association on the newsk is already closed before accept()
  4096. * is called, set RCV_SHUTDOWN flag.
  4097. */
  4098. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  4099. newsk->sk_shutdown |= RCV_SHUTDOWN;
  4100. newsk->sk_state = SCTP_SS_ESTABLISHED;
  4101. }
  4102. /* This proto struct describes the ULP interface for SCTP. */
  4103. struct proto sctp_prot = {
  4104. .name = "SCTP",
  4105. .owner = THIS_MODULE,
  4106. .close = sctp_close,
  4107. .connect = sctp_connect,
  4108. .disconnect = sctp_disconnect,
  4109. .accept = sctp_accept,
  4110. .ioctl = sctp_ioctl,
  4111. .init = sctp_init_sock,
  4112. .destroy = sctp_destroy_sock,
  4113. .shutdown = sctp_shutdown,
  4114. .setsockopt = sctp_setsockopt,
  4115. .getsockopt = sctp_getsockopt,
  4116. .sendmsg = sctp_sendmsg,
  4117. .recvmsg = sctp_recvmsg,
  4118. .bind = sctp_bind,
  4119. .backlog_rcv = sctp_backlog_rcv,
  4120. .hash = sctp_hash,
  4121. .unhash = sctp_unhash,
  4122. .get_port = sctp_get_port,
  4123. .obj_size = sizeof(struct sctp_sock),
  4124. };
  4125. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4126. struct proto sctpv6_prot = {
  4127. .name = "SCTPv6",
  4128. .owner = THIS_MODULE,
  4129. .close = sctp_close,
  4130. .connect = sctp_connect,
  4131. .disconnect = sctp_disconnect,
  4132. .accept = sctp_accept,
  4133. .ioctl = sctp_ioctl,
  4134. .init = sctp_init_sock,
  4135. .destroy = sctp_destroy_sock,
  4136. .shutdown = sctp_shutdown,
  4137. .setsockopt = sctp_setsockopt,
  4138. .getsockopt = sctp_getsockopt,
  4139. .sendmsg = sctp_sendmsg,
  4140. .recvmsg = sctp_recvmsg,
  4141. .bind = sctp_bind,
  4142. .backlog_rcv = sctp_backlog_rcv,
  4143. .hash = sctp_hash,
  4144. .unhash = sctp_unhash,
  4145. .get_port = sctp_get_port,
  4146. .obj_size = sizeof(struct sctp6_sock),
  4147. };
  4148. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */